1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
213126Sahl 
22789Sahrens /*
235977Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <assert.h>
28789Sahrens #include <ctype.h>
29789Sahrens #include <errno.h>
30789Sahrens #include <libdevinfo.h>
31789Sahrens #include <libintl.h>
32789Sahrens #include <math.h>
33789Sahrens #include <stdio.h>
34789Sahrens #include <stdlib.h>
35789Sahrens #include <strings.h>
36789Sahrens #include <unistd.h>
375367Sahrens #include <stddef.h>
38789Sahrens #include <zone.h>
392082Seschrock #include <fcntl.h>
40789Sahrens #include <sys/mntent.h>
41789Sahrens #include <sys/mnttab.h>
421294Slling #include <sys/mount.h>
434543Smarks #include <sys/avl.h>
444543Smarks #include <priv.h>
454543Smarks #include <pwd.h>
464543Smarks #include <grp.h>
474543Smarks #include <stddef.h>
484543Smarks #include <ucred.h>
49789Sahrens 
50789Sahrens #include <sys/spa.h>
512676Seschrock #include <sys/zap.h>
52789Sahrens #include <libzfs.h>
53789Sahrens 
54789Sahrens #include "zfs_namecheck.h"
55789Sahrens #include "zfs_prop.h"
56789Sahrens #include "libzfs_impl.h"
574543Smarks #include "zfs_deleg.h"
58789Sahrens 
594007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
604007Smmusante 
61789Sahrens /*
62789Sahrens  * Given a single type (not a mask of types), return the type in a human
63789Sahrens  * readable form.
64789Sahrens  */
65789Sahrens const char *
66789Sahrens zfs_type_to_name(zfs_type_t type)
67789Sahrens {
68789Sahrens 	switch (type) {
69789Sahrens 	case ZFS_TYPE_FILESYSTEM:
70789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
71789Sahrens 	case ZFS_TYPE_SNAPSHOT:
72789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
73789Sahrens 	case ZFS_TYPE_VOLUME:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
75789Sahrens 	}
76789Sahrens 
77789Sahrens 	return (NULL);
78789Sahrens }
79789Sahrens 
80789Sahrens /*
81789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
82789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
83789Sahrens  * We guess what the type would have been based on the path and the mask of
84789Sahrens  * acceptable types.
85789Sahrens  */
86789Sahrens static const char *
87789Sahrens path_to_str(const char *path, int types)
88789Sahrens {
89789Sahrens 	/*
90789Sahrens 	 * When given a single type, always report the exact type.
91789Sahrens 	 */
92789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
93789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
94789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
95789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
96789Sahrens 	if (types == ZFS_TYPE_VOLUME)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
98789Sahrens 
99789Sahrens 	/*
100789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
101789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
102789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
103789Sahrens 	 * snapshot attribute and try again.
104789Sahrens 	 */
105789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
106789Sahrens 		if (strchr(path, '@') != NULL)
107789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
108789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
109789Sahrens 	}
110789Sahrens 
111789Sahrens 
112789Sahrens 	/*
113789Sahrens 	 * The user has requested either filesystems or volumes.
114789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
115789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
116789Sahrens 	 */
117789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
118789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
119789Sahrens 
120789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
121789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
122789Sahrens }
123789Sahrens 
124789Sahrens /*
125789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
126789Sahrens  * provide a more meaningful error message.  We place a more useful message in
127789Sahrens  * 'buf' detailing exactly why the name was not valid.
128789Sahrens  */
129789Sahrens static int
1305326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1315326Sek110237     boolean_t modifying)
132789Sahrens {
133789Sahrens 	namecheck_err_t why;
134789Sahrens 	char what;
135789Sahrens 
136789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1372082Seschrock 		if (hdl != NULL) {
138789Sahrens 			switch (why) {
1391003Slling 			case NAME_ERR_TOOLONG:
1402082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1412082Seschrock 				    "name is too long"));
1421003Slling 				break;
1431003Slling 
144789Sahrens 			case NAME_ERR_LEADING_SLASH:
1452082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1462082Seschrock 				    "leading slash in name"));
147789Sahrens 				break;
148789Sahrens 
149789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1502082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512082Seschrock 				    "empty component in name"));
152789Sahrens 				break;
153789Sahrens 
154789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1552082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562082Seschrock 				    "trailing slash in name"));
157789Sahrens 				break;
158789Sahrens 
159789Sahrens 			case NAME_ERR_INVALCHAR:
1602082Seschrock 				zfs_error_aux(hdl,
161789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1622082Seschrock 				    "'%c' in name"), what);
163789Sahrens 				break;
164789Sahrens 
165789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1662082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1672082Seschrock 				    "multiple '@' delimiters in name"));
168789Sahrens 				break;
1692856Snd150628 
1702856Snd150628 			case NAME_ERR_NOLETTER:
1712856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1722856Snd150628 				    "pool doesn't begin with a letter"));
1732856Snd150628 				break;
1742856Snd150628 
1752856Snd150628 			case NAME_ERR_RESERVED:
1762856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1772856Snd150628 				    "name is reserved"));
1782856Snd150628 				break;
1792856Snd150628 
1802856Snd150628 			case NAME_ERR_DISKLIKE:
1812856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1822856Snd150628 				    "reserved disk name"));
1832856Snd150628 				break;
184789Sahrens 			}
185789Sahrens 		}
186789Sahrens 
187789Sahrens 		return (0);
188789Sahrens 	}
189789Sahrens 
190789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1912082Seschrock 		if (hdl != NULL)
1922082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1932082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
194789Sahrens 		return (0);
195789Sahrens 	}
196789Sahrens 
1972199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1982199Sahrens 		if (hdl != NULL)
1992199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2003413Smmusante 			    "missing '@' delimiter in snapshot name"));
2012199Sahrens 		return (0);
2022199Sahrens 	}
2032199Sahrens 
2045326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2055326Sek110237 		if (hdl != NULL)
2065326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2075326Sek110237 			    "invalid character %c in name"), '%');
2085326Sek110237 		return (0);
2095326Sek110237 	}
2105326Sek110237 
2112082Seschrock 	return (-1);
212789Sahrens }
213789Sahrens 
214789Sahrens int
215789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
216789Sahrens {
2176423Sgw25295 	if (type == ZFS_TYPE_POOL)
2186423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2195326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
220789Sahrens }
221789Sahrens 
222789Sahrens /*
2232676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2242676Seschrock  * properties into a separate nvlist.
2252676Seschrock  */
2264217Seschrock static nvlist_t *
2274217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2282676Seschrock {
2292676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2302676Seschrock 	nvpair_t *elem;
2312676Seschrock 	nvlist_t *propval;
2324217Seschrock 	nvlist_t *nvl;
2334217Seschrock 
2344217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2354217Seschrock 		(void) no_memory(hdl);
2364217Seschrock 		return (NULL);
2374217Seschrock 	}
2382676Seschrock 
2392676Seschrock 	elem = NULL;
2404217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2412676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2422676Seschrock 			continue;
2432676Seschrock 
2442676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2454217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2464217Seschrock 			nvlist_free(nvl);
2474217Seschrock 			(void) no_memory(hdl);
2484217Seschrock 			return (NULL);
2494217Seschrock 		}
2502676Seschrock 	}
2512676Seschrock 
2524217Seschrock 	return (nvl);
2532676Seschrock }
2542676Seschrock 
2556865Srm160521 static zpool_handle_t *
2566865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2576865Srm160521 {
2586865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2596865Srm160521 	zpool_handle_t *zph;
2606865Srm160521 
2616865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2626865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2636865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2646865Srm160521 		hdl->libzfs_pool_handles = zph;
2656865Srm160521 	}
2666865Srm160521 	return (zph);
2676865Srm160521 }
2686865Srm160521 
2696865Srm160521 static zpool_handle_t *
2706865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2716865Srm160521 {
2726865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2736865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2746865Srm160521 
2756865Srm160521 	while ((zph != NULL) &&
2766865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2776865Srm160521 		zph = zph->zpool_next;
2786865Srm160521 	return (zph);
2796865Srm160521 }
2806865Srm160521 
2816865Srm160521 /*
2826865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2836865Srm160521  * If a handle to that pool already exists then that handle is returned.
2846865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2856865Srm160521  */
2866865Srm160521 static zpool_handle_t *
2876865Srm160521 zpool_handle(zfs_handle_t *zhp)
2886865Srm160521 {
2896865Srm160521 	char *pool_name;
2906865Srm160521 	int len;
2916865Srm160521 	zpool_handle_t *zph;
2926865Srm160521 
2936865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2946865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2956865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2966865Srm160521 
2976865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
2986865Srm160521 	if (zph == NULL)
2996865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
3006865Srm160521 
3016865Srm160521 	free(pool_name);
3026865Srm160521 	return (zph);
3036865Srm160521 }
3046865Srm160521 
3056865Srm160521 void
3066865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3076865Srm160521 {
3086865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3096865Srm160521 
3106865Srm160521 	while (zph != NULL) {
3116865Srm160521 		next = zph->zpool_next;
3126865Srm160521 		zpool_close(zph);
3136865Srm160521 		zph = next;
3146865Srm160521 	}
3156865Srm160521 	hdl->libzfs_pool_handles = NULL;
3166865Srm160521 }
3176865Srm160521 
3182676Seschrock /*
319789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
320789Sahrens  */
321789Sahrens static int
322789Sahrens get_stats(zfs_handle_t *zhp)
323789Sahrens {
324789Sahrens 	zfs_cmd_t zc = { 0 };
3252676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3264217Seschrock 	nvlist_t *allprops, *userprops;
327789Sahrens 
328789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
329789Sahrens 
3302676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
3312082Seschrock 		return (-1);
3321356Seschrock 
3332082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
3341356Seschrock 		if (errno == ENOMEM) {
3352676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3362676Seschrock 				zcmd_free_nvlists(&zc);
3372082Seschrock 				return (-1);
3382676Seschrock 			}
3391356Seschrock 		} else {
3402676Seschrock 			zcmd_free_nvlists(&zc);
3411356Seschrock 			return (-1);
3421356Seschrock 		}
3431356Seschrock 	}
344789Sahrens 
3452885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
346789Sahrens 
3474217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
3482676Seschrock 		zcmd_free_nvlists(&zc);
3492082Seschrock 		return (-1);
3502082Seschrock 	}
351789Sahrens 
3522676Seschrock 	zcmd_free_nvlists(&zc);
3532676Seschrock 
3544217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3554217Seschrock 		nvlist_free(allprops);
3562676Seschrock 		return (-1);
3574217Seschrock 	}
3584217Seschrock 
3594217Seschrock 	nvlist_free(zhp->zfs_props);
3604217Seschrock 	nvlist_free(zhp->zfs_user_props);
3614217Seschrock 
3624217Seschrock 	zhp->zfs_props = allprops;
3634217Seschrock 	zhp->zfs_user_props = userprops;
3642082Seschrock 
365789Sahrens 	return (0);
366789Sahrens }
367789Sahrens 
368789Sahrens /*
369789Sahrens  * Refresh the properties currently stored in the handle.
370789Sahrens  */
371789Sahrens void
372789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
373789Sahrens {
374789Sahrens 	(void) get_stats(zhp);
375789Sahrens }
376789Sahrens 
377789Sahrens /*
378789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
379789Sahrens  * zfs_iter_* to create child handles on the fly.
380789Sahrens  */
381789Sahrens zfs_handle_t *
3822082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
383789Sahrens {
3842082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3854543Smarks 	char *logstr;
3862082Seschrock 
3872082Seschrock 	if (zhp == NULL)
3882082Seschrock 		return (NULL);
3892082Seschrock 
3902082Seschrock 	zhp->zfs_hdl = hdl;
391789Sahrens 
3924543Smarks 	/*
3934543Smarks 	 * Preserve history log string.
3944543Smarks 	 * any changes performed here will be
3954543Smarks 	 * logged as an internal event.
3964543Smarks 	 */
3974543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3984543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3991758Sahrens top:
400789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
401789Sahrens 
402789Sahrens 	if (get_stats(zhp) != 0) {
4034543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
404789Sahrens 		free(zhp);
405789Sahrens 		return (NULL);
406789Sahrens 	}
407789Sahrens 
4081758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4091758Sahrens 		zfs_cmd_t zc = { 0 };
4101758Sahrens 
4111758Sahrens 		/*
4121758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4131758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4141758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4151758Sahrens 		 * can't be mounted.  However, it could also be that we
4161758Sahrens 		 * have crashed in the middle of one of those
4171758Sahrens 		 * operations, in which case we need to get rid of the
4181758Sahrens 		 * inconsistent state.  We do that by either rolling
4191758Sahrens 		 * back to the previous snapshot (which will fail if
4201758Sahrens 		 * there is none), or destroying the filesystem.  Note
4211758Sahrens 		 * that if we are still in the middle of an active
4221758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4231758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4241758Sahrens 		 */
4251758Sahrens 
4261758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4271758Sahrens 
4282885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4292082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4301758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
4311758Sahrens 		} else {
4321758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
4331758Sahrens 		}
4341758Sahrens 
4351758Sahrens 		/*
4365331Samw 		 * If we can successfully destroy it, pretend that it
4371758Sahrens 		 * never existed.
4381758Sahrens 		 */
4392082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
4404543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4411758Sahrens 			free(zhp);
4421758Sahrens 			errno = ENOENT;
4431758Sahrens 			return (NULL);
4441758Sahrens 		}
4455367Sahrens 		/* If we can successfully roll it back, reget the stats */
4465367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
4475367Sahrens 			goto top;
4481758Sahrens 	}
4491758Sahrens 
450789Sahrens 	/*
451789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
452789Sahrens 	 * the high-level type.
453789Sahrens 	 */
4542885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4552885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4562885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4572885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4582885Sahrens 	else
4592885Sahrens 		abort();
4602885Sahrens 
461789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
462789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
463789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
464789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
465789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
466789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
467789Sahrens 	else
4682082Seschrock 		abort();	/* we should never see any other types */
469789Sahrens 
4704543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
4716865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
472789Sahrens 	return (zhp);
473789Sahrens }
474789Sahrens 
475789Sahrens /*
476789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
477789Sahrens  * argument is a mask of acceptable types.  The function will print an
478789Sahrens  * appropriate error message and return NULL if it can't be opened.
479789Sahrens  */
480789Sahrens zfs_handle_t *
4812082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
482789Sahrens {
483789Sahrens 	zfs_handle_t *zhp;
4842082Seschrock 	char errbuf[1024];
4852082Seschrock 
4862082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4872082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
488789Sahrens 
489789Sahrens 	/*
4902082Seschrock 	 * Validate the name before we even try to open it.
491789Sahrens 	 */
4925326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4932082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4942082Seschrock 		    "invalid dataset name"));
4952082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
496789Sahrens 		return (NULL);
497789Sahrens 	}
498789Sahrens 
499789Sahrens 	/*
500789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
501789Sahrens 	 */
502789Sahrens 	errno = 0;
5032082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5043237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
505789Sahrens 		return (NULL);
506789Sahrens 	}
507789Sahrens 
508789Sahrens 	if (!(types & zhp->zfs_type)) {
5092082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5102142Seschrock 		zfs_close(zhp);
511789Sahrens 		return (NULL);
512789Sahrens 	}
513789Sahrens 
514789Sahrens 	return (zhp);
515789Sahrens }
516789Sahrens 
517789Sahrens /*
518789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
519789Sahrens  */
520789Sahrens void
521789Sahrens zfs_close(zfs_handle_t *zhp)
522789Sahrens {
523789Sahrens 	if (zhp->zfs_mntopts)
524789Sahrens 		free(zhp->zfs_mntopts);
5252676Seschrock 	nvlist_free(zhp->zfs_props);
5262676Seschrock 	nvlist_free(zhp->zfs_user_props);
527789Sahrens 	free(zhp);
528789Sahrens }
529789Sahrens 
5305713Srm160521 int
5315713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
5325713Srm160521 {
5336865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
5346865Srm160521 
5355713Srm160521 	if (zpool_handle == NULL)
5365713Srm160521 		return (-1);
5375713Srm160521 
5385713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
5395713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
5405713Srm160521 	return (0);
5415713Srm160521 }
5425713Srm160521 
5435713Srm160521 /*
5445713Srm160521  * The choice of reservation property depends on the SPA version.
5455713Srm160521  */
5465713Srm160521 static int
5475713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5485713Srm160521 {
5495713Srm160521 	int spa_version;
5505713Srm160521 
5515713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5525713Srm160521 		return (-1);
5535713Srm160521 
5545713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5555713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5565713Srm160521 	else
5575713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5585713Srm160521 
5595713Srm160521 	return (0);
5605713Srm160521 }
5615713Srm160521 
5623912Slling /*
5632676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
5642676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
5652676Seschrock  * strings.
566789Sahrens  */
5677184Stimh nvlist_t *
5687184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
5695094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
570789Sahrens {
5712676Seschrock 	nvpair_t *elem;
5722676Seschrock 	uint64_t intval;
5732676Seschrock 	char *strval;
5745094Slling 	zfs_prop_t prop;
5752676Seschrock 	nvlist_t *ret;
5765331Samw 	int chosen_normal = -1;
5775331Samw 	int chosen_utf = -1;
5782676Seschrock 
5795094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5805094Slling 		(void) no_memory(hdl);
5815094Slling 		return (NULL);
582789Sahrens 	}
583789Sahrens 
5842676Seschrock 	elem = NULL;
5852676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5865094Slling 		const char *propname = nvpair_name(elem);
5872676Seschrock 
5882676Seschrock 		/*
5892676Seschrock 		 * Make sure this property is valid and applies to this type.
5902676Seschrock 		 */
5915094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
5925094Slling 			if (!zfs_prop_user(propname)) {
5932676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5945094Slling 				    "invalid property '%s'"), propname);
5955094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5965094Slling 				goto error;
5975094Slling 			}
5985094Slling 
5995094Slling 			/*
6005094Slling 			 * If this is a user property, make sure it's a
6015094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
6025094Slling 			 */
6035094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
6045094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6055094Slling 				    "'%s' must be a string"), propname);
6065094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6075094Slling 				goto error;
6085094Slling 			}
6095094Slling 
6105094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
6115094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6125094Slling 				    "property name '%s' is too long"),
6132676Seschrock 				    propname);
6142676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6152676Seschrock 				goto error;
6162676Seschrock 			}
6172676Seschrock 
6182676Seschrock 			(void) nvpair_value_string(elem, &strval);
6192676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
6202676Seschrock 				(void) no_memory(hdl);
6212676Seschrock 				goto error;
6222676Seschrock 			}
6232676Seschrock 			continue;
624789Sahrens 		}
6252676Seschrock 
6267265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
6277265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6287265Sahrens 			    "this property can not be modified for snapshots"));
6297265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
6307265Sahrens 			goto error;
6317265Sahrens 		}
6327265Sahrens 
6332676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
6342676Seschrock 			zfs_error_aux(hdl,
6352676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
6362676Seschrock 			    "apply to datasets of this type"), propname);
6372676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
6382676Seschrock 			goto error;
6392676Seschrock 		}
6402676Seschrock 
6412676Seschrock 		if (zfs_prop_readonly(prop) &&
6425331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
6432676Seschrock 			zfs_error_aux(hdl,
6442676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
6452676Seschrock 			    propname);
6462676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
6472676Seschrock 			goto error;
6482676Seschrock 		}
6492676Seschrock 
6505094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
6515094Slling 		    &strval, &intval, errbuf) != 0)
6522676Seschrock 			goto error;
6532676Seschrock 
6542676Seschrock 		/*
6552676Seschrock 		 * Perform some additional checks for specific properties.
6562676Seschrock 		 */
6572676Seschrock 		switch (prop) {
6584577Sahrens 		case ZFS_PROP_VERSION:
6594577Sahrens 		{
6604577Sahrens 			int version;
6614577Sahrens 
6624577Sahrens 			if (zhp == NULL)
6634577Sahrens 				break;
6644577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6654577Sahrens 			if (intval < version) {
6664577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6674577Sahrens 				    "Can not downgrade; already at version %u"),
6684577Sahrens 				    version);
6694577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6704577Sahrens 				goto error;
6714577Sahrens 			}
6724577Sahrens 			break;
6734577Sahrens 		}
6744577Sahrens 
6752676Seschrock 		case ZFS_PROP_RECORDSIZE:
6762676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
6772676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
6782676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
6792676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
6802082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6812676Seschrock 				    "'%s' must be power of 2 from %u "
6822676Seschrock 				    "to %uk"), propname,
6832676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
6842676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
6852676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6862676Seschrock 				goto error;
687789Sahrens 			}
688789Sahrens 			break;
689789Sahrens 
6903126Sahl 		case ZFS_PROP_SHAREISCSI:
6913126Sahl 			if (strcmp(strval, "off") != 0 &&
6923126Sahl 			    strcmp(strval, "on") != 0 &&
6933126Sahl 			    strcmp(strval, "type=disk") != 0) {
6943126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6953126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6963126Sahl 				    propname);
6973126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6983126Sahl 				goto error;
6993126Sahl 			}
7003126Sahl 
7013126Sahl 			break;
7023126Sahl 
7032676Seschrock 		case ZFS_PROP_MOUNTPOINT:
7044778Srm160521 		{
7054778Srm160521 			namecheck_err_t why;
7064778Srm160521 
7072676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
7082676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
7092676Seschrock 				break;
7102676Seschrock 
7114778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
7124778Srm160521 				switch (why) {
7134778Srm160521 				case NAME_ERR_LEADING_SLASH:
7144778Srm160521 					zfs_error_aux(hdl,
7154778Srm160521 					    dgettext(TEXT_DOMAIN,
7164778Srm160521 					    "'%s' must be an absolute path, "
7174778Srm160521 					    "'none', or 'legacy'"), propname);
7184778Srm160521 					break;
7194778Srm160521 				case NAME_ERR_TOOLONG:
7204778Srm160521 					zfs_error_aux(hdl,
7214778Srm160521 					    dgettext(TEXT_DOMAIN,
7224778Srm160521 					    "component of '%s' is too long"),
7234778Srm160521 					    propname);
7244778Srm160521 					break;
7254778Srm160521 				}
7262676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7272676Seschrock 				goto error;
728789Sahrens 			}
7294778Srm160521 		}
7304778Srm160521 
7313126Sahl 			/*FALLTHRU*/
7323126Sahl 
7335331Samw 		case ZFS_PROP_SHARESMB:
7343126Sahl 		case ZFS_PROP_SHARENFS:
7353126Sahl 			/*
7365331Samw 			 * For the mountpoint and sharenfs or sharesmb
7375331Samw 			 * properties, check if it can be set in a
7385331Samw 			 * global/non-global zone based on
7393126Sahl 			 * the zoned property value:
7403126Sahl 			 *
7413126Sahl 			 *		global zone	    non-global zone
7423126Sahl 			 * --------------------------------------------------
7433126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
7443126Sahl 			 *		sharenfs (no)	    sharenfs (no)
7455331Samw 			 *		sharesmb (no)	    sharesmb (no)
7463126Sahl 			 *
7473126Sahl 			 * zoned=off	mountpoint (yes)	N/A
7483126Sahl 			 *		sharenfs (yes)
7495331Samw 			 *		sharesmb (yes)
7503126Sahl 			 */
7512676Seschrock 			if (zoned) {
7522676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
7532676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7542676Seschrock 					    "'%s' cannot be set on "
7552676Seschrock 					    "dataset in a non-global zone"),
7562676Seschrock 					    propname);
7572676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7582676Seschrock 					    errbuf);
7592676Seschrock 					goto error;
7605331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
7615331Samw 				    prop == ZFS_PROP_SHARESMB) {
7622676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7632676Seschrock 					    "'%s' cannot be set in "
7642676Seschrock 					    "a non-global zone"), propname);
7652676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7662676Seschrock 					    errbuf);
7672676Seschrock 					goto error;
7682676Seschrock 				}
7692676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
7702676Seschrock 				/*
7712676Seschrock 				 * If zoned property is 'off', this must be in
7722676Seschrock 				 * a globle zone. If not, something is wrong.
7732676Seschrock 				 */
7742676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7752676Seschrock 				    "'%s' cannot be set while dataset "
7762676Seschrock 				    "'zoned' property is set"), propname);
7772676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
7782676Seschrock 				goto error;
7792676Seschrock 			}
7803126Sahl 
7814180Sdougm 			/*
7824180Sdougm 			 * At this point, it is legitimate to set the
7834180Sdougm 			 * property. Now we want to make sure that the
7844180Sdougm 			 * property value is valid if it is sharenfs.
7854180Sdougm 			 */
7865331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
7875331Samw 			    prop == ZFS_PROP_SHARESMB) &&
7884217Seschrock 			    strcmp(strval, "on") != 0 &&
7894217Seschrock 			    strcmp(strval, "off") != 0) {
7905331Samw 				zfs_share_proto_t proto;
7915331Samw 
7925331Samw 				if (prop == ZFS_PROP_SHARESMB)
7935331Samw 					proto = PROTO_SMB;
7945331Samw 				else
7955331Samw 					proto = PROTO_NFS;
7964180Sdougm 
7974180Sdougm 				/*
7985331Samw 				 * Must be an valid sharing protocol
7995331Samw 				 * option string so init the libshare
8005331Samw 				 * in order to enable the parser and
8015331Samw 				 * then parse the options. We use the
8025331Samw 				 * control API since we don't care about
8035331Samw 				 * the current configuration and don't
8044180Sdougm 				 * want the overhead of loading it
8054180Sdougm 				 * until we actually do something.
8064180Sdougm 				 */
8074180Sdougm 
8084217Seschrock 				if (zfs_init_libshare(hdl,
8094217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
8104217Seschrock 					/*
8114217Seschrock 					 * An error occurred so we can't do
8124217Seschrock 					 * anything
8134217Seschrock 					 */
8144217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8154217Seschrock 					    "'%s' cannot be set: problem "
8164217Seschrock 					    "in share initialization"),
8174217Seschrock 					    propname);
8184217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8194217Seschrock 					    errbuf);
8204217Seschrock 					goto error;
8214217Seschrock 				}
8224217Seschrock 
8235331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
8244217Seschrock 					/*
8254217Seschrock 					 * There was an error in parsing so
8264217Seschrock 					 * deal with it by issuing an error
8274217Seschrock 					 * message and leaving after
8284217Seschrock 					 * uninitializing the the libshare
8294217Seschrock 					 * interface.
8304217Seschrock 					 */
8314217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8324217Seschrock 					    "'%s' cannot be set to invalid "
8334217Seschrock 					    "options"), propname);
8344217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8354217Seschrock 					    errbuf);
8364217Seschrock 					zfs_uninit_libshare(hdl);
8374217Seschrock 					goto error;
8384217Seschrock 				}
8394180Sdougm 				zfs_uninit_libshare(hdl);
8404180Sdougm 			}
8414180Sdougm 
8423126Sahl 			break;
8435331Samw 		case ZFS_PROP_UTF8ONLY:
8445331Samw 			chosen_utf = (int)intval;
8455331Samw 			break;
8465331Samw 		case ZFS_PROP_NORMALIZE:
8475331Samw 			chosen_normal = (int)intval;
8485331Samw 			break;
8492676Seschrock 		}
8502676Seschrock 
8512676Seschrock 		/*
8522676Seschrock 		 * For changes to existing volumes, we have some additional
8532676Seschrock 		 * checks to enforce.
8542676Seschrock 		 */
8552676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
8562676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
8572676Seschrock 			    ZFS_PROP_VOLSIZE);
8582676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
8592676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
8602676Seschrock 			char buf[64];
8612676Seschrock 
8622676Seschrock 			switch (prop) {
8632676Seschrock 			case ZFS_PROP_RESERVATION:
8645378Sck153898 			case ZFS_PROP_REFRESERVATION:
8652676Seschrock 				if (intval > volsize) {
8662676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8672676Seschrock 					    "'%s' is greater than current "
8682676Seschrock 					    "volume size"), propname);
8692676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8702676Seschrock 					    errbuf);
8712676Seschrock 					goto error;
8722676Seschrock 				}
8732676Seschrock 				break;
8742676Seschrock 
8752676Seschrock 			case ZFS_PROP_VOLSIZE:
8762676Seschrock 				if (intval % blocksize != 0) {
8772676Seschrock 					zfs_nicenum(blocksize, buf,
8782676Seschrock 					    sizeof (buf));
8792676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8802676Seschrock 					    "'%s' must be a multiple of "
8812676Seschrock 					    "volume block size (%s)"),
8822676Seschrock 					    propname, buf);
8832676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8842676Seschrock 					    errbuf);
8852676Seschrock 					goto error;
8862676Seschrock 				}
8872676Seschrock 
8882676Seschrock 				if (intval == 0) {
8892676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8902676Seschrock 					    "'%s' cannot be zero"),
8912676Seschrock 					    propname);
8922676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8932676Seschrock 					    errbuf);
8942676Seschrock 					goto error;
895789Sahrens 				}
8963126Sahl 				break;
897789Sahrens 			}
898789Sahrens 		}
899789Sahrens 	}
900789Sahrens 
9012676Seschrock 	/*
9025331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
9035331Samw 	 * enforce rejection of non-UTF8 names.
9045331Samw 	 *
9055331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
9065331Samw 	 * was explicitly not chosen, it is an error.
9075331Samw 	 */
9085498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
9095331Samw 		if (nvlist_add_uint64(ret,
9105331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
9115331Samw 			(void) no_memory(hdl);
9125331Samw 			goto error;
9135331Samw 		}
9145498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
9155331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9165331Samw 		    "'%s' must be set 'on' if normalization chosen"),
9175331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
9185331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9195331Samw 		goto error;
9205331Samw 	}
9215331Samw 
9225331Samw 	/*
9232676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
9242676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
9252676Seschrock 	 */
9262676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
9272676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
9282676Seschrock 	    &intval) == 0) {
9292676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
9302676Seschrock 		    ZFS_PROP_VOLSIZE);
9315481Sck153898 		uint64_t old_reservation;
9322676Seschrock 		uint64_t new_reservation;
9335481Sck153898 		zfs_prop_t resv_prop;
9345713Srm160521 
9355713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
9365481Sck153898 			goto error;
9375481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
9382676Seschrock 
9392676Seschrock 		if (old_volsize == old_reservation &&
9405481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
9412676Seschrock 		    &new_reservation) != 0) {
9422676Seschrock 			if (nvlist_add_uint64(ret,
9435481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
9442676Seschrock 				(void) no_memory(hdl);
9452676Seschrock 				goto error;
9462676Seschrock 			}
9472676Seschrock 		}
9482676Seschrock 	}
9492676Seschrock 	return (ret);
9502676Seschrock 
9512676Seschrock error:
9522676Seschrock 	nvlist_free(ret);
9532676Seschrock 	return (NULL);
954789Sahrens }
955789Sahrens 
9564543Smarks static int
9574543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9584543Smarks     uint64_t *ret_who)
9594543Smarks {
9604543Smarks 	struct passwd *pwd;
9614543Smarks 	struct group *grp;
9624543Smarks 	uid_t id;
9634543Smarks 
9644543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9654543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9664543Smarks 		*ret_who = -1;
9674543Smarks 		return (0);
9684543Smarks 	}
9694543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9704543Smarks 		return (EZFS_BADWHO);
9714543Smarks 
9724543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9734543Smarks 	    strcmp(who, "everyone") == 0) {
9744543Smarks 		*ret_who = -1;
9754543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9764543Smarks 		return (0);
9774543Smarks 	}
9784543Smarks 
9794543Smarks 	pwd = getpwnam(who);
9804543Smarks 	grp = getgrnam(who);
9814543Smarks 
9824543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9834543Smarks 		*ret_who = pwd->pw_uid;
9844543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9854543Smarks 		*ret_who = grp->gr_gid;
9864543Smarks 	} else if (pwd) {
9874543Smarks 		*ret_who = pwd->pw_uid;
9884543Smarks 		*who_type = ZFS_DELEG_USER;
9894543Smarks 	} else if (grp) {
9904543Smarks 		*ret_who = grp->gr_gid;
9914543Smarks 		*who_type = ZFS_DELEG_GROUP;
9924543Smarks 	} else {
9934543Smarks 		char *end;
9944543Smarks 
9954543Smarks 		id = strtol(who, &end, 10);
9964543Smarks 		if (errno != 0 || *end != '\0') {
9974543Smarks 			return (EZFS_BADWHO);
9984543Smarks 		} else {
9994543Smarks 			*ret_who = id;
10004543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
10014543Smarks 				*who_type = ZFS_DELEG_USER;
10024543Smarks 		}
10034543Smarks 	}
10044543Smarks 
10054543Smarks 	return (0);
10064543Smarks }
10074543Smarks 
10084543Smarks static void
10094543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
10104543Smarks {
10114543Smarks 	if (perms_nvp != NULL) {
10124543Smarks 		verify(nvlist_add_nvlist(who_nvp,
10134543Smarks 		    name, perms_nvp) == 0);
10144543Smarks 	} else {
10154543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
10164543Smarks 	}
10174543Smarks }
10184543Smarks 
10194543Smarks static void
10204543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
10214543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
10224543Smarks     nvlist_t *sets_nvp)
10234543Smarks {
10244543Smarks 	boolean_t do_perms, do_sets;
10254543Smarks 	char name[ZFS_MAX_DELEG_NAME];
10264543Smarks 
10274543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
10284543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
10294543Smarks 
10304543Smarks 	if (!do_perms && !do_sets)
10314543Smarks 		do_perms = do_sets = B_TRUE;
10324543Smarks 
10334543Smarks 	if (do_perms) {
10344543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
10354543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10364543Smarks 		    whostr : (void *)&whoid);
10374543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
10384543Smarks 	}
10394543Smarks 	if (do_sets) {
10404543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
10414543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10424543Smarks 		    whostr : (void *)&whoid);
10434543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
10444543Smarks 	}
10454543Smarks }
10464543Smarks 
10474543Smarks static void
10484543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
10494543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
10504543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10514543Smarks {
10524543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10534543Smarks 		helper(who_type, whoid, whostr, 0,
10544543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10554543Smarks 	} else {
10564543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10574543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10584543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10594543Smarks 		}
10604543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10614543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10624543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10634543Smarks 		}
10644543Smarks 	}
10654543Smarks }
10664543Smarks 
10674543Smarks /*
10684543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10694543Smarks  *
10704543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10714543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10724543Smarks  * base attribute named stored in the dsl.
10734543Smarks  * Arguments:
10744543Smarks  *
10754543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10764543Smarks  *           whostr may be null for everyone or create perms.
10774543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10784543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10795331Samw  * perms:    common separated list of permissions.  May be null if user
10804543Smarks  *           is requested to remove permissions by who.
10814543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10824543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10834543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10844543Smarks  *           The output nvp will look something like this.
10854543Smarks  *              ul$1234 -> {create ; destroy }
10864543Smarks  *              Ul$1234 -> { @myset }
10874543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10884543Smarks  */
10894543Smarks int
10904543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10914543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10924543Smarks {
10934543Smarks 	nvlist_t *who_nvp;
10944543Smarks 	nvlist_t *perms_nvp = NULL;
10954543Smarks 	nvlist_t *sets_nvp = NULL;
10964543Smarks 	char errbuf[1024];
10974787Sahrens 	char *who_tok, *perm;
10984543Smarks 	int error;
10994543Smarks 
11004543Smarks 	*nvp = NULL;
11014543Smarks 
11024543Smarks 	if (perms) {
11034543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
11044543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11054543Smarks 			return (1);
11064543Smarks 		}
11074543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
11084543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11094543Smarks 			nvlist_free(perms_nvp);
11104543Smarks 			return (1);
11114543Smarks 		}
11124543Smarks 	}
11134543Smarks 
11144543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
11154543Smarks 		if (perms_nvp)
11164543Smarks 			nvlist_free(perms_nvp);
11174543Smarks 		if (sets_nvp)
11184543Smarks 			nvlist_free(sets_nvp);
11194543Smarks 		return (1);
11204543Smarks 	}
11214543Smarks 
11224543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
11234543Smarks 		namecheck_err_t why;
11244543Smarks 		char what;
11254543Smarks 
11264543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
11274787Sahrens 			nvlist_free(who_nvp);
11284787Sahrens 			if (perms_nvp)
11294787Sahrens 				nvlist_free(perms_nvp);
11304787Sahrens 			if (sets_nvp)
11314787Sahrens 				nvlist_free(sets_nvp);
11324787Sahrens 
11334543Smarks 			switch (why) {
11344543Smarks 			case NAME_ERR_NO_AT:
11354543Smarks 				zfs_error_aux(zhp->zfs_hdl,
11364543Smarks 				    dgettext(TEXT_DOMAIN,
11374543Smarks 				    "set definition must begin with an '@' "
11384543Smarks 				    "character"));
11394543Smarks 			}
11404543Smarks 			return (zfs_error(zhp->zfs_hdl,
11414543Smarks 			    EZFS_BADPERMSET, whostr));
11424543Smarks 		}
11434543Smarks 	}
11444543Smarks 
11454543Smarks 	/*
11464543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
11474543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
11484543Smarks 	 * other sets_nvp will have only permssion set names in it.
11494543Smarks 	 */
11504787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11514787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11524787Sahrens 
11534787Sahrens 		if (perm_canonical) {
11544787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11554787Sahrens 			    perm_canonical) == 0);
11564787Sahrens 		} else if (perm[0] == '@') {
11574787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11584787Sahrens 		} else {
11594787Sahrens 			nvlist_free(who_nvp);
11604787Sahrens 			nvlist_free(perms_nvp);
11614787Sahrens 			nvlist_free(sets_nvp);
11624787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11634543Smarks 		}
11644543Smarks 	}
11654543Smarks 
11664543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11674543Smarks 		who_tok = strtok(whostr, ",");
11684543Smarks 		if (who_tok == NULL) {
11694543Smarks 			nvlist_free(who_nvp);
11704787Sahrens 			if (perms_nvp)
11714787Sahrens 				nvlist_free(perms_nvp);
11724543Smarks 			if (sets_nvp)
11734543Smarks 				nvlist_free(sets_nvp);
11744543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11754543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11764543Smarks 			    whostr);
11774543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11784543Smarks 		}
11794543Smarks 	}
11804543Smarks 
11814543Smarks 	/*
11824543Smarks 	 * Now create the nvlist(s)
11834543Smarks 	 */
11844543Smarks 	do {
11854543Smarks 		uint64_t who_id;
11864543Smarks 
11874543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11884543Smarks 		    &who_id);
11894543Smarks 		if (error) {
11904543Smarks 			nvlist_free(who_nvp);
11914787Sahrens 			if (perms_nvp)
11924787Sahrens 				nvlist_free(perms_nvp);
11934543Smarks 			if (sets_nvp)
11944543Smarks 				nvlist_free(sets_nvp);
11954543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11964543Smarks 			    dgettext(TEXT_DOMAIN,
11974543Smarks 			    "Unable to determine uid/gid for "
11984543Smarks 			    "%s "), who_tok);
11994543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
12004543Smarks 		}
12014543Smarks 
12024543Smarks 		/*
12034543Smarks 		 * add entries for both local and descendent when required
12044543Smarks 		 */
12054543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
12064543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
12074543Smarks 
12084543Smarks 	} while (who_tok = strtok(NULL, ","));
12094543Smarks 	*nvp = who_nvp;
12104543Smarks 	return (0);
12114543Smarks }
12124543Smarks 
12134543Smarks static int
12144543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
12154543Smarks {
12164543Smarks 	zfs_cmd_t zc = { 0 };
12174543Smarks 	int error;
12184543Smarks 	char errbuf[1024];
12194543Smarks 
12204543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
12214543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
12224543Smarks 	    zhp->zfs_name);
12234543Smarks 
12245094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
12254543Smarks 		return (-1);
12264543Smarks 
12274543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12284543Smarks 	zc.zc_perm_action = unset;
12294543Smarks 
12304543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
12314543Smarks 	if (error && errno == ENOTSUP) {
12324543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
12334543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
12344543Smarks 		zcmd_free_nvlists(&zc);
12354543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
12364543Smarks 	} else if (error) {
12374543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
12384543Smarks 	}
12394543Smarks 	zcmd_free_nvlists(&zc);
12404543Smarks 
12414543Smarks 	return (error);
12424543Smarks }
12434543Smarks 
12444543Smarks int
12454543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
12464543Smarks {
12474543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12484543Smarks }
12494543Smarks 
12504543Smarks int
12514543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12524543Smarks {
12534543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12544543Smarks }
12554543Smarks 
12564543Smarks static int
12574543Smarks perm_compare(const void *arg1, const void *arg2)
12584543Smarks {
12594543Smarks 	const zfs_perm_node_t *node1 = arg1;
12604543Smarks 	const zfs_perm_node_t *node2 = arg2;
12614543Smarks 	int ret;
12624543Smarks 
12634543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12644543Smarks 
12654543Smarks 	if (ret > 0)
12664543Smarks 		return (1);
12674543Smarks 	if (ret < 0)
12684543Smarks 		return (-1);
12694543Smarks 	else
12704543Smarks 		return (0);
12714543Smarks }
12724543Smarks 
12734543Smarks static void
12744543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12754543Smarks {
12764543Smarks 	zfs_perm_node_t *permnode;
12775367Sahrens 	void *cookie = NULL;
12785367Sahrens 
12795367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12804543Smarks 		free(permnode);
12815367Sahrens 	avl_destroy(tree);
12824543Smarks }
12834543Smarks 
12844543Smarks static void
12854543Smarks zfs_destroy_tree(avl_tree_t *tree)
12864543Smarks {
12874543Smarks 	zfs_allow_node_t *allownode;
12885367Sahrens 	void *cookie = NULL;
12895367Sahrens 
12904543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12914543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12924543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12934543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12944543Smarks 		free(allownode);
12954543Smarks 	}
12965367Sahrens 	avl_destroy(tree);
12974543Smarks }
12984543Smarks 
12994543Smarks void
13004543Smarks zfs_free_allows(zfs_allow_t *allow)
13014543Smarks {
13024543Smarks 	zfs_allow_t *allownext;
13034543Smarks 	zfs_allow_t *freeallow;
13044543Smarks 
13054543Smarks 	allownext = allow;
13064543Smarks 	while (allownext) {
13074543Smarks 		zfs_destroy_tree(&allownext->z_sets);
13084543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
13094543Smarks 		zfs_destroy_tree(&allownext->z_user);
13104543Smarks 		zfs_destroy_tree(&allownext->z_group);
13114543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
13124543Smarks 		freeallow = allownext;
13134543Smarks 		allownext = allownext->z_next;
13144543Smarks 		free(freeallow);
13154543Smarks 	}
13164543Smarks }
13174543Smarks 
13184543Smarks static zfs_allow_t *
13194543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
13204543Smarks {
13214543Smarks 	zfs_allow_t *ptree;
13224543Smarks 
13234543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
13244543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
13254543Smarks 		return (NULL);
13264543Smarks 	}
13274543Smarks 
13284543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
13294543Smarks 	avl_create(&ptree->z_sets,
13304543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13314543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13324543Smarks 	avl_create(&ptree->z_crperms,
13334543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13344543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13354543Smarks 	avl_create(&ptree->z_user,
13364543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13374543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13384543Smarks 	avl_create(&ptree->z_group,
13394543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13404543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13414543Smarks 	avl_create(&ptree->z_everyone,
13424543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13434543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13444543Smarks 
13454543Smarks 	if (prev)
13464543Smarks 		prev->z_next = ptree;
13474543Smarks 	ptree->z_next = NULL;
13484543Smarks 	return (ptree);
13494543Smarks }
13504543Smarks 
13514543Smarks /*
13524543Smarks  * Add permissions to the appropriate AVL permission tree.
13534543Smarks  * The appropriate tree may not be the requested tree.
13544543Smarks  * For example if ld indicates a local permission, but
13554543Smarks  * same permission also exists as a descendent permission
13564543Smarks  * then the permission will be removed from the descendent
13574543Smarks  * tree and add the the local+descendent tree.
13584543Smarks  */
13594543Smarks static int
13604543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13614543Smarks     char *perm, char ld)
13624543Smarks {
13634543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13644543Smarks 	zfs_perm_node_t *newnode;
13654543Smarks 	avl_index_t where, where2;
13664543Smarks 	avl_tree_t *tree, *altree;
13674543Smarks 
13684543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13694543Smarks 
13704543Smarks 	if (ld == ZFS_DELEG_NA) {
13714543Smarks 		tree =  &allownode->z_localdescend;
13724543Smarks 		altree = &allownode->z_descend;
13734543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13744543Smarks 		tree = &allownode->z_local;
13754543Smarks 		altree = &allownode->z_descend;
13764543Smarks 	} else {
13774543Smarks 		tree = &allownode->z_descend;
13784543Smarks 		altree = &allownode->z_local;
13794543Smarks 	}
13804543Smarks 	permnode = avl_find(tree, &pnode, &where);
13814543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13824543Smarks 
13834543Smarks 	if (permnode2) {
13844543Smarks 		avl_remove(altree, permnode2);
13854543Smarks 		free(permnode2);
13864543Smarks 		if (permnode == NULL) {
13874543Smarks 			tree =  &allownode->z_localdescend;
13884543Smarks 		}
13894543Smarks 	}
13904543Smarks 
13914543Smarks 	/*
13924543Smarks 	 * Now insert new permission in either requested location
13934543Smarks 	 * local/descendent or into ld when perm will exist in both.
13944543Smarks 	 */
13954543Smarks 	if (permnode == NULL) {
13964543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13974543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
13984543Smarks 			return (-1);
13994543Smarks 		}
14004543Smarks 		*newnode = pnode;
14014543Smarks 		avl_add(tree, newnode);
14024543Smarks 	}
14034543Smarks 	return (0);
14044543Smarks }
14054577Sahrens 
14064543Smarks /*
14074543Smarks  * Uggh, this is going to be a bit complicated.
14084543Smarks  * we have an nvlist coming out of the kernel that
14094543Smarks  * will indicate where the permission is set and then
14104543Smarks  * it will contain allow of the various "who's", and what
14114543Smarks  * their permissions are.  To further complicate this
14124543Smarks  * we will then have to coalesce the local,descendent
14134543Smarks  * and local+descendent permissions where appropriate.
14144543Smarks  * The kernel only knows about a permission as being local
14154543Smarks  * or descendent, but not both.
14164543Smarks  *
14174543Smarks  * In order to make this easier for zfs_main to deal with
14184543Smarks  * a series of AVL trees will be used to maintain
14194543Smarks  * all of this, primarily for sorting purposes as well
14204543Smarks  * as the ability to quickly locate a specific entry.
14214543Smarks  *
14224543Smarks  * What we end up with are tree's for sets, create perms,
14234543Smarks  * user, groups and everyone.  With each of those trees
14244543Smarks  * we have subtrees for local, descendent and local+descendent
14254543Smarks  * permissions.
14264543Smarks  */
14274543Smarks int
14284543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
14294543Smarks {
14304543Smarks 	zfs_cmd_t zc = { 0 };
14314543Smarks 	int error;
14324543Smarks 	nvlist_t *nvlist;
14334543Smarks 	nvlist_t *permnv, *sourcenv;
14344543Smarks 	nvpair_t *who_pair, *source_pair;
14354543Smarks 	nvpair_t *perm_pair;
14364543Smarks 	char errbuf[1024];
14374543Smarks 	zfs_allow_t *zallowp, *newallowp;
14384543Smarks 	char  ld;
14394543Smarks 	char *nvpname;
14404543Smarks 	uid_t	uid;
14414543Smarks 	gid_t	gid;
14424543Smarks 	avl_tree_t *tree;
14434543Smarks 	avl_index_t where;
14444543Smarks 
14454543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14464543Smarks 
14474543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
14484543Smarks 		return (-1);
14494543Smarks 
14504543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14514543Smarks 		if (errno == ENOMEM) {
14524543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14534543Smarks 				zcmd_free_nvlists(&zc);
14544543Smarks 				return (-1);
14554543Smarks 			}
14564543Smarks 		} else if (errno == ENOTSUP) {
14574543Smarks 			zcmd_free_nvlists(&zc);
14584543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14594543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14604543Smarks 			return (zfs_error(zhp->zfs_hdl,
14614543Smarks 			    EZFS_BADVERSION, errbuf));
14624543Smarks 		} else {
14634543Smarks 			zcmd_free_nvlists(&zc);
14644543Smarks 			return (-1);
14654543Smarks 		}
14664543Smarks 	}
14674543Smarks 
14684543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14694543Smarks 		zcmd_free_nvlists(&zc);
14704543Smarks 		return (-1);
14714543Smarks 	}
14724543Smarks 
14734543Smarks 	zcmd_free_nvlists(&zc);
14744543Smarks 
14754543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14764543Smarks 
14774543Smarks 	if (source_pair == NULL) {
14784543Smarks 		*zfs_perms = NULL;
14794543Smarks 		return (0);
14804543Smarks 	}
14814543Smarks 
14824543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14834543Smarks 	if (*zfs_perms == NULL) {
14844543Smarks 		return (0);
14854543Smarks 	}
14864543Smarks 
14874543Smarks 	zallowp = *zfs_perms;
14884543Smarks 
14894543Smarks 	for (;;) {
14904543Smarks 		struct passwd *pwd;
14914543Smarks 		struct group *grp;
14924543Smarks 		zfs_allow_node_t *allownode;
14934543Smarks 		zfs_allow_node_t  findallownode;
14944543Smarks 		zfs_allow_node_t *newallownode;
14954543Smarks 
14964543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14974543Smarks 		    nvpair_name(source_pair),
14984543Smarks 		    sizeof (zallowp->z_setpoint));
14994543Smarks 
15004543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
15014543Smarks 			goto abort;
15024543Smarks 
15034543Smarks 		/*
15044543Smarks 		 * Make sure nvlist is composed correctly
15054543Smarks 		 */
15064543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
15074543Smarks 			goto abort;
15084543Smarks 		}
15094543Smarks 
15104543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
15114543Smarks 		if (who_pair == NULL) {
15124543Smarks 			goto abort;
15134543Smarks 		}
15144543Smarks 
15154543Smarks 		do {
15164543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
15174543Smarks 			if (error) {
15184543Smarks 				goto abort;
15194543Smarks 			}
15204543Smarks 
15214543Smarks 			/*
15224543Smarks 			 * First build up the key to use
15234543Smarks 			 * for looking up in the various
15244543Smarks 			 * who trees.
15254543Smarks 			 */
15264543Smarks 			ld = nvpair_name(who_pair)[1];
15274543Smarks 			nvpname = nvpair_name(who_pair);
15284543Smarks 			switch (nvpair_name(who_pair)[0]) {
15294543Smarks 			case ZFS_DELEG_USER:
15304543Smarks 			case ZFS_DELEG_USER_SETS:
15314543Smarks 				tree = &zallowp->z_user;
15324543Smarks 				uid = atol(&nvpname[3]);
15334543Smarks 				pwd = getpwuid(uid);
15344543Smarks 				(void) snprintf(findallownode.z_key,
15354543Smarks 				    sizeof (findallownode.z_key), "user %s",
15364543Smarks 				    (pwd) ? pwd->pw_name :
15374543Smarks 				    &nvpair_name(who_pair)[3]);
15384543Smarks 				break;
15394543Smarks 			case ZFS_DELEG_GROUP:
15404543Smarks 			case ZFS_DELEG_GROUP_SETS:
15414543Smarks 				tree = &zallowp->z_group;
15424543Smarks 				gid = atol(&nvpname[3]);
15434543Smarks 				grp = getgrgid(gid);
15444543Smarks 				(void) snprintf(findallownode.z_key,
15454543Smarks 				    sizeof (findallownode.z_key), "group %s",
15464543Smarks 				    (grp) ? grp->gr_name :
15474543Smarks 				    &nvpair_name(who_pair)[3]);
15484543Smarks 				break;
15494543Smarks 			case ZFS_DELEG_CREATE:
15504543Smarks 			case ZFS_DELEG_CREATE_SETS:
15514543Smarks 				tree = &zallowp->z_crperms;
15524543Smarks 				(void) strlcpy(findallownode.z_key, "",
15534543Smarks 				    sizeof (findallownode.z_key));
15544543Smarks 				break;
15554543Smarks 			case ZFS_DELEG_EVERYONE:
15564543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15574543Smarks 				(void) snprintf(findallownode.z_key,
15584543Smarks 				    sizeof (findallownode.z_key), "everyone");
15594543Smarks 				tree = &zallowp->z_everyone;
15604543Smarks 				break;
15614543Smarks 			case ZFS_DELEG_NAMED_SET:
15624543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15634543Smarks 				(void) snprintf(findallownode.z_key,
15644543Smarks 				    sizeof (findallownode.z_key), "%s",
15654543Smarks 				    &nvpair_name(who_pair)[3]);
15664543Smarks 				tree = &zallowp->z_sets;
15674543Smarks 				break;
15684543Smarks 			}
15694543Smarks 
15704543Smarks 			/*
15714543Smarks 			 * Place who in tree
15724543Smarks 			 */
15734543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15744543Smarks 			if (allownode == NULL) {
15754543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15764543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15774543Smarks 					goto abort;
15784543Smarks 				}
15794543Smarks 				avl_create(&newallownode->z_localdescend,
15804543Smarks 				    perm_compare,
15814543Smarks 				    sizeof (zfs_perm_node_t),
15824543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15834543Smarks 				avl_create(&newallownode->z_local,
15844543Smarks 				    perm_compare,
15854543Smarks 				    sizeof (zfs_perm_node_t),
15864543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15874543Smarks 				avl_create(&newallownode->z_descend,
15884543Smarks 				    perm_compare,
15894543Smarks 				    sizeof (zfs_perm_node_t),
15904543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15914543Smarks 				(void) strlcpy(newallownode->z_key,
15924543Smarks 				    findallownode.z_key,
15934543Smarks 				    sizeof (findallownode.z_key));
15944543Smarks 				avl_insert(tree, newallownode, where);
15954543Smarks 				allownode = newallownode;
15964543Smarks 			}
15974543Smarks 
15984543Smarks 			/*
15994543Smarks 			 * Now iterate over the permissions and
16004543Smarks 			 * place them in the appropriate local,
16014543Smarks 			 * descendent or local+descendent tree.
16024543Smarks 			 *
16034543Smarks 			 * The permissions are added to the tree
16044543Smarks 			 * via zfs_coalesce_perm().
16054543Smarks 			 */
16064543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
16074543Smarks 			if (perm_pair == NULL)
16084543Smarks 				goto abort;
16094543Smarks 			do {
16104543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
16114543Smarks 				    nvpair_name(perm_pair), ld) != 0)
16124543Smarks 					goto abort;
16134543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
16144543Smarks 			    perm_pair));
16154543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
16164543Smarks 
16174543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
16184543Smarks 		if (source_pair == NULL)
16194543Smarks 			break;
16204543Smarks 
16214543Smarks 		/*
16224543Smarks 		 * allocate another node from the link list of
16234543Smarks 		 * zfs_allow_t structures
16244543Smarks 		 */
16254543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
16264543Smarks 		    nvpair_name(source_pair));
16274543Smarks 		if (newallowp == NULL) {
16284543Smarks 			goto abort;
16294543Smarks 		}
16304543Smarks 		zallowp = newallowp;
16314543Smarks 	}
16324543Smarks 	nvlist_free(nvlist);
16334543Smarks 	return (0);
16344543Smarks abort:
16354543Smarks 	zfs_free_allows(*zfs_perms);
16364543Smarks 	nvlist_free(nvlist);
16374543Smarks 	return (-1);
16384543Smarks }
16394543Smarks 
16405993Smarks static char *
16415993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
16425993Smarks {
16435993Smarks 	/*
16445993Smarks 	 * Don't put newlines on end of lines
16455993Smarks 	 */
16465993Smarks 	switch (note) {
16475993Smarks 	case ZFS_DELEG_NOTE_CREATE:
16485993Smarks 		return (dgettext(TEXT_DOMAIN,
16495993Smarks 		    "Must also have the 'mount' ability"));
16505993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16515993Smarks 		return (dgettext(TEXT_DOMAIN,
16525993Smarks 		    "Must also have the 'mount' ability"));
16535993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16545993Smarks 		return (dgettext(TEXT_DOMAIN,
16555993Smarks 		    "Must also have the 'mount' ability"));
16565993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16575993Smarks 		return (dgettext(TEXT_DOMAIN,
16585993Smarks 		    "Must also have the 'mount' ability"));
16595993Smarks 	case ZFS_DELEG_NOTE_CLONE:
16605993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16615993Smarks 		    "ability and 'mount'\n"
16625993Smarks 		    "\t\t\t\tability in the origin file system"));
16635993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16645993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16655993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16665993Smarks 	case ZFS_DELEG_NOTE_RENAME:
16675993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16685993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16695993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16705993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16715993Smarks 		    " and 'create' ability"));
16725993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16735993Smarks 		return (dgettext(TEXT_DOMAIN,
16745993Smarks 		    "Allows changing any user property"));
16755993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16765993Smarks 		return (dgettext(TEXT_DOMAIN,
16775993Smarks 		    "Must also have the permission that is being\n"
16785993Smarks 		    "\t\t\t\tallowed"));
16795993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16805993Smarks 		return (dgettext(TEXT_DOMAIN,
16815993Smarks 		    "Allows mount/umount of ZFS datasets"));
16825993Smarks 	case ZFS_DELEG_NOTE_SHARE:
16835993Smarks 		return (dgettext(TEXT_DOMAIN,
16845993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16855993Smarks 		    "\t\t\t\tprotocols"));
16865993Smarks 	case ZFS_DELEG_NOTE_NONE:
16875993Smarks 	default:
16885993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16895993Smarks 	}
16905993Smarks }
16915993Smarks 
16925993Smarks typedef enum {
16935993Smarks 	ZFS_DELEG_SUBCOMMAND,
16945993Smarks 	ZFS_DELEG_PROP,
16955993Smarks 	ZFS_DELEG_OTHER
16965993Smarks } zfs_deleg_perm_type_t;
16975993Smarks 
16985993Smarks /*
16995993Smarks  * is the permission a subcommand or other?
17005993Smarks  */
17015993Smarks zfs_deleg_perm_type_t
17025993Smarks zfs_deleg_perm_type(const char *perm)
17035993Smarks {
17045993Smarks 	if (strcmp(perm, "userprop") == 0)
17055993Smarks 		return (ZFS_DELEG_OTHER);
17065993Smarks 	else
17075993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
17085993Smarks }
17095993Smarks 
17105993Smarks static char *
17115993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
17125993Smarks {
17135993Smarks 	switch (type) {
17145993Smarks 	case ZFS_DELEG_SUBCOMMAND:
17155993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
17165993Smarks 	case ZFS_DELEG_PROP:
17175993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
17185993Smarks 	case ZFS_DELEG_OTHER:
17195993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
17205993Smarks 	}
17215993Smarks 	return ("");
17225993Smarks }
17235993Smarks 
17245993Smarks /*ARGSUSED*/
17255993Smarks static int
17265993Smarks zfs_deleg_prop_cb(int prop, void *cb)
17275993Smarks {
17285993Smarks 	if (zfs_prop_delegatable(prop))
17295993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
17305993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
17315993Smarks 
17325993Smarks 	return (ZPROP_CONT);
17335993Smarks }
17345993Smarks 
17355993Smarks void
17365993Smarks zfs_deleg_permissions(void)
17375993Smarks {
17385993Smarks 	int i;
17395993Smarks 
17405993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
17415993Smarks 	    "TYPE", "NOTES");
17425993Smarks 
17435993Smarks 	/*
17445993Smarks 	 * First print out the subcommands
17455993Smarks 	 */
17465993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
17475993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17485993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17495993Smarks 		    zfs_deleg_perm_type_str(
17505993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17515993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17525993Smarks 	}
17535993Smarks 
17545993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17555993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17565993Smarks }
17575993Smarks 
1758789Sahrens /*
1759789Sahrens  * Given a property name and value, set the property for the given dataset.
1760789Sahrens  */
1761789Sahrens int
17622676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1763789Sahrens {
1764789Sahrens 	zfs_cmd_t zc = { 0 };
17652676Seschrock 	int ret = -1;
17662676Seschrock 	prop_changelist_t *cl = NULL;
17672082Seschrock 	char errbuf[1024];
17682082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17692676Seschrock 	nvlist_t *nvl = NULL, *realprops;
17702676Seschrock 	zfs_prop_t prop;
17716168Shs24103 	int do_prefix = 1;
17722082Seschrock 
17732082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
17742676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
17752082Seschrock 	    zhp->zfs_name);
17762082Seschrock 
17772676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
17782676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
17792676Seschrock 		(void) no_memory(hdl);
17802676Seschrock 		goto error;
1781789Sahrens 	}
1782789Sahrens 
17837184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
17842676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
17852676Seschrock 		goto error;
17865094Slling 
17872676Seschrock 	nvlist_free(nvl);
17882676Seschrock 	nvl = realprops;
17892676Seschrock 
17902676Seschrock 	prop = zfs_name_to_prop(propname);
17912676Seschrock 
1792789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
17932676Seschrock 		goto error;
1794789Sahrens 
1795789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17962082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17972082Seschrock 		    "child dataset with inherited mountpoint is used "
17982082Seschrock 		    "in a non-global zone"));
17992082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1800789Sahrens 		goto error;
1801789Sahrens 	}
1802789Sahrens 
18036168Shs24103 
18046168Shs24103 	/* do not unmount dataset if canmount is being set to noauto */
18056168Shs24103 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
18066168Shs24103 		do_prefix = 0;
18076168Shs24103 
18086168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
18096168Shs24103 			goto error;
1810789Sahrens 
1811789Sahrens 	/*
1812789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1813789Sahrens 	 */
1814789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1815789Sahrens 
18165094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
18172676Seschrock 		goto error;
18182676Seschrock 
18194543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1820789Sahrens 	if (ret != 0) {
1821789Sahrens 		switch (errno) {
1822789Sahrens 
1823789Sahrens 		case ENOSPC:
1824789Sahrens 			/*
1825789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1826789Sahrens 			 * something different; setting a quota or reservation
1827789Sahrens 			 * doesn't use any disk space.
1828789Sahrens 			 */
1829789Sahrens 			switch (prop) {
1830789Sahrens 			case ZFS_PROP_QUOTA:
18315378Sck153898 			case ZFS_PROP_REFQUOTA:
18322082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18332082Seschrock 				    "size is less than current used or "
18342082Seschrock 				    "reserved space"));
18352082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1836789Sahrens 				break;
1837789Sahrens 
1838789Sahrens 			case ZFS_PROP_RESERVATION:
18395378Sck153898 			case ZFS_PROP_REFRESERVATION:
18402082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18412082Seschrock 				    "size is greater than available space"));
18422082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1843789Sahrens 				break;
1844789Sahrens 
1845789Sahrens 			default:
18462082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1847789Sahrens 				break;
1848789Sahrens 			}
1849789Sahrens 			break;
1850789Sahrens 
1851789Sahrens 		case EBUSY:
18522082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
18532082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
18542082Seschrock 			else
18552676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1856789Sahrens 			break;
1857789Sahrens 
18581175Slling 		case EROFS:
18592082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18601175Slling 			break;
18611175Slling 
18623886Sahl 		case ENOTSUP:
18633886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18645977Smarks 			    "pool and or dataset must be upgraded to set this "
18654603Sahrens 			    "property or value"));
18663886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
18673886Sahl 			break;
18683886Sahl 
18697042Sgw25295 		case ERANGE:
18707042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
18717042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18727042Sgw25295 				    "property setting is not allowed on "
18737042Sgw25295 				    "bootable datasets"));
18747042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
18757042Sgw25295 			} else {
18767042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
18777042Sgw25295 			}
18787042Sgw25295 			break;
18797042Sgw25295 
1880789Sahrens 		case EOVERFLOW:
1881789Sahrens 			/*
1882789Sahrens 			 * This platform can't address a volume this big.
1883789Sahrens 			 */
1884789Sahrens #ifdef _ILP32
1885789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
18862082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1887789Sahrens 				break;
1888789Sahrens 			}
1889789Sahrens #endif
18902082Seschrock 			/* FALLTHROUGH */
1891789Sahrens 		default:
18922082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1893789Sahrens 		}
1894789Sahrens 	} else {
18956168Shs24103 		if (do_prefix)
18966168Shs24103 			ret = changelist_postfix(cl);
18976168Shs24103 
1898789Sahrens 		/*
1899789Sahrens 		 * Refresh the statistics so the new property value
1900789Sahrens 		 * is reflected.
1901789Sahrens 		 */
19026168Shs24103 		if (ret == 0)
19032676Seschrock 			(void) get_stats(zhp);
1904789Sahrens 	}
1905789Sahrens 
1906789Sahrens error:
19072676Seschrock 	nvlist_free(nvl);
19082676Seschrock 	zcmd_free_nvlists(&zc);
19092676Seschrock 	if (cl)
19102676Seschrock 		changelist_free(cl);
1911789Sahrens 	return (ret);
1912789Sahrens }
1913789Sahrens 
1914789Sahrens /*
1915789Sahrens  * Given a property, inherit the value from the parent dataset.
1916789Sahrens  */
1917789Sahrens int
19182676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1919789Sahrens {
1920789Sahrens 	zfs_cmd_t zc = { 0 };
1921789Sahrens 	int ret;
1922789Sahrens 	prop_changelist_t *cl;
19232082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
19242082Seschrock 	char errbuf[1024];
19252676Seschrock 	zfs_prop_t prop;
19262082Seschrock 
19272082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
19282082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1929789Sahrens 
19305094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
19312676Seschrock 		/*
19322676Seschrock 		 * For user properties, the amount of work we have to do is very
19332676Seschrock 		 * small, so just do it here.
19342676Seschrock 		 */
19352676Seschrock 		if (!zfs_prop_user(propname)) {
19362676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19372676Seschrock 			    "invalid property"));
19382676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
19392676Seschrock 		}
19402676Seschrock 
19412676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19422676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
19432676Seschrock 
19444849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
19452676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
19462676Seschrock 
19472676Seschrock 		return (0);
19482676Seschrock 	}
19492676Seschrock 
1950789Sahrens 	/*
1951789Sahrens 	 * Verify that this property is inheritable.
1952789Sahrens 	 */
19532082Seschrock 	if (zfs_prop_readonly(prop))
19542082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
19552082Seschrock 
19562082Seschrock 	if (!zfs_prop_inheritable(prop))
19572082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1958789Sahrens 
1959789Sahrens 	/*
1960789Sahrens 	 * Check to see if the value applies to this type
1961789Sahrens 	 */
19622082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
19632082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1964789Sahrens 
19653443Srm160521 	/*
19663443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
19673443Srm160521 	 */
19683443Srm160521 	propname = zfs_prop_to_name(prop);
1969789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19702676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1971789Sahrens 
1972789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1973789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
19742082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19752082Seschrock 		    "dataset is used in a non-global zone"));
19762082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1977789Sahrens 	}
1978789Sahrens 
1979789Sahrens 	/*
1980789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1981789Sahrens 	 */
1982789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1983789Sahrens 		return (-1);
1984789Sahrens 
1985789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19862082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19872082Seschrock 		    "child dataset with inherited mountpoint is used "
19882082Seschrock 		    "in a non-global zone"));
19892082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1990789Sahrens 		goto error;
1991789Sahrens 	}
1992789Sahrens 
1993789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1994789Sahrens 		goto error;
1995789Sahrens 
19964849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
19972082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1998789Sahrens 	} else {
1999789Sahrens 
20002169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
2001789Sahrens 			goto error;
2002789Sahrens 
2003789Sahrens 		/*
2004789Sahrens 		 * Refresh the statistics so the new property is reflected.
2005789Sahrens 		 */
2006789Sahrens 		(void) get_stats(zhp);
2007789Sahrens 	}
2008789Sahrens 
2009789Sahrens error:
2010789Sahrens 	changelist_free(cl);
2011789Sahrens 	return (ret);
2012789Sahrens }
2013789Sahrens 
2014789Sahrens /*
20151356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
20161356Seschrock  * extract them appropriately.
20171356Seschrock  */
20181356Seschrock static uint64_t
20191356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20201356Seschrock {
20211356Seschrock 	nvlist_t *nv;
20221356Seschrock 	uint64_t value;
20231356Seschrock 
20242885Sahrens 	*source = NULL;
20251356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20261356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20275094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
20285094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20291356Seschrock 	} else {
20301356Seschrock 		value = zfs_prop_default_numeric(prop);
20311356Seschrock 		*source = "";
20321356Seschrock 	}
20331356Seschrock 
20341356Seschrock 	return (value);
20351356Seschrock }
20361356Seschrock 
20371356Seschrock static char *
20381356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20391356Seschrock {
20401356Seschrock 	nvlist_t *nv;
20411356Seschrock 	char *value;
20421356Seschrock 
20432885Sahrens 	*source = NULL;
20441356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20451356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20465094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
20475094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20481356Seschrock 	} else {
20491356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
20501356Seschrock 			value = "";
20511356Seschrock 		*source = "";
20521356Seschrock 	}
20531356Seschrock 
20541356Seschrock 	return (value);
20551356Seschrock }
20561356Seschrock 
20571356Seschrock /*
2058789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2059789Sahrens  * zfs_prop_get_int() are built using this interface.
2060789Sahrens  *
2061789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2062789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2063789Sahrens  * If they differ from the on-disk values, report the current values and mark
2064789Sahrens  * the source "temporary".
2065789Sahrens  */
20662082Seschrock static int
20675094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
20682082Seschrock     char **source, uint64_t *val)
2069789Sahrens {
20705147Srm160521 	zfs_cmd_t zc = { 0 };
20715592Stimh 	nvlist_t *zplprops = NULL;
2072789Sahrens 	struct mnttab mnt;
20733265Sahrens 	char *mntopt_on = NULL;
20743265Sahrens 	char *mntopt_off = NULL;
2075789Sahrens 
2076789Sahrens 	*source = NULL;
2077789Sahrens 
20783265Sahrens 	switch (prop) {
20793265Sahrens 	case ZFS_PROP_ATIME:
20803265Sahrens 		mntopt_on = MNTOPT_ATIME;
20813265Sahrens 		mntopt_off = MNTOPT_NOATIME;
20823265Sahrens 		break;
20833265Sahrens 
20843265Sahrens 	case ZFS_PROP_DEVICES:
20853265Sahrens 		mntopt_on = MNTOPT_DEVICES;
20863265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
20873265Sahrens 		break;
20883265Sahrens 
20893265Sahrens 	case ZFS_PROP_EXEC:
20903265Sahrens 		mntopt_on = MNTOPT_EXEC;
20913265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
20923265Sahrens 		break;
20933265Sahrens 
20943265Sahrens 	case ZFS_PROP_READONLY:
20953265Sahrens 		mntopt_on = MNTOPT_RO;
20963265Sahrens 		mntopt_off = MNTOPT_RW;
20973265Sahrens 		break;
20983265Sahrens 
20993265Sahrens 	case ZFS_PROP_SETUID:
21003265Sahrens 		mntopt_on = MNTOPT_SETUID;
21013265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
21023265Sahrens 		break;
21033265Sahrens 
21043265Sahrens 	case ZFS_PROP_XATTR:
21053265Sahrens 		mntopt_on = MNTOPT_XATTR;
21063265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
21073265Sahrens 		break;
21085331Samw 
21095331Samw 	case ZFS_PROP_NBMAND:
21105331Samw 		mntopt_on = MNTOPT_NBMAND;
21115331Samw 		mntopt_off = MNTOPT_NONBMAND;
21125331Samw 		break;
21133265Sahrens 	}
21143265Sahrens 
21152474Seschrock 	/*
21162474Seschrock 	 * Because looking up the mount options is potentially expensive
21172474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
21182474Seschrock 	 * we're looking up a property which requires its presence.
21192474Seschrock 	 */
21202474Seschrock 	if (!zhp->zfs_mntcheck &&
21213265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
21223265Sahrens 		struct mnttab entry, search = { 0 };
21233265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
21242474Seschrock 
21252474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
21262474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
21273265Sahrens 		rewind(mnttab);
21283265Sahrens 
21293265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
21303265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
21313265Sahrens 			    entry.mnt_mntopts);
21323265Sahrens 			if (zhp->zfs_mntopts == NULL)
21333265Sahrens 				return (-1);
21343265Sahrens 		}
21352474Seschrock 
21362474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
21372474Seschrock 	}
21382474Seschrock 
2139789Sahrens 	if (zhp->zfs_mntopts == NULL)
2140789Sahrens 		mnt.mnt_mntopts = "";
2141789Sahrens 	else
2142789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2143789Sahrens 
2144789Sahrens 	switch (prop) {
2145789Sahrens 	case ZFS_PROP_ATIME:
21463265Sahrens 	case ZFS_PROP_DEVICES:
21473265Sahrens 	case ZFS_PROP_EXEC:
21483265Sahrens 	case ZFS_PROP_READONLY:
21493265Sahrens 	case ZFS_PROP_SETUID:
21503265Sahrens 	case ZFS_PROP_XATTR:
21515331Samw 	case ZFS_PROP_NBMAND:
21522082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21532082Seschrock 
21543265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
21552082Seschrock 			*val = B_TRUE;
2156789Sahrens 			if (src)
21575094Slling 				*src = ZPROP_SRC_TEMPORARY;
21583265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
21592082Seschrock 			*val = B_FALSE;
2160789Sahrens 			if (src)
21615094Slling 				*src = ZPROP_SRC_TEMPORARY;
2162789Sahrens 		}
21632082Seschrock 		break;
2164789Sahrens 
21653265Sahrens 	case ZFS_PROP_CANMOUNT:
21662082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21676168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
21683417Srm160521 			*source = zhp->zfs_name;
21693417Srm160521 		else
21703417Srm160521 			*source = "";	/* default */
21712082Seschrock 		break;
2172789Sahrens 
2173789Sahrens 	case ZFS_PROP_QUOTA:
21745378Sck153898 	case ZFS_PROP_REFQUOTA:
2175789Sahrens 	case ZFS_PROP_RESERVATION:
21765378Sck153898 	case ZFS_PROP_REFRESERVATION:
21772885Sahrens 		*val = getprop_uint64(zhp, prop, source);
21782885Sahrens 		if (*val == 0)
2179789Sahrens 			*source = "";	/* default */
2180789Sahrens 		else
2181789Sahrens 			*source = zhp->zfs_name;
21822082Seschrock 		break;
2183789Sahrens 
2184789Sahrens 	case ZFS_PROP_MOUNTED:
21852082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
21862082Seschrock 		break;
2187789Sahrens 
21883377Seschrock 	case ZFS_PROP_NUMCLONES:
21893377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
21903377Seschrock 		break;
21913377Seschrock 
21925147Srm160521 	case ZFS_PROP_VERSION:
21935498Stimh 	case ZFS_PROP_NORMALIZE:
21945498Stimh 	case ZFS_PROP_UTF8ONLY:
21955498Stimh 	case ZFS_PROP_CASE:
21965498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
21975498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
21985473Srm160521 			return (-1);
21995147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
22005498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
22015498Stimh 			zcmd_free_nvlists(&zc);
22025147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22035498Stimh 			    "unable to get %s property"),
22045498Stimh 			    zfs_prop_to_name(prop));
22055147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
22065147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
22075147Srm160521 		}
22085498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
22095498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
22105498Stimh 		    val) != 0) {
22115498Stimh 			zcmd_free_nvlists(&zc);
22125498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22135498Stimh 			    "unable to get %s property"),
22145498Stimh 			    zfs_prop_to_name(prop));
22155498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
22165498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
22175498Stimh 		}
22185592Stimh 		if (zplprops)
22195592Stimh 			nvlist_free(zplprops);
22205498Stimh 		zcmd_free_nvlists(&zc);
22215147Srm160521 		break;
22225147Srm160521 
2223789Sahrens 	default:
22244577Sahrens 		switch (zfs_prop_get_type(prop)) {
22254787Sahrens 		case PROP_TYPE_NUMBER:
22264787Sahrens 		case PROP_TYPE_INDEX:
22274577Sahrens 			*val = getprop_uint64(zhp, prop, source);
22284577Sahrens 			break;
22294577Sahrens 
22304787Sahrens 		case PROP_TYPE_STRING:
22314577Sahrens 		default:
22324577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22334577Sahrens 			    "cannot get non-numeric property"));
22344577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
22354577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
22364577Sahrens 		}
2237789Sahrens 	}
2238789Sahrens 
2239789Sahrens 	return (0);
2240789Sahrens }
2241789Sahrens 
2242789Sahrens /*
2243789Sahrens  * Calculate the source type, given the raw source string.
2244789Sahrens  */
2245789Sahrens static void
22465094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2247789Sahrens     char *statbuf, size_t statlen)
2248789Sahrens {
22495094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2250789Sahrens 		return;
2251789Sahrens 
2252789Sahrens 	if (source == NULL) {
22535094Slling 		*srctype = ZPROP_SRC_NONE;
2254789Sahrens 	} else if (source[0] == '\0') {
22555094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2256789Sahrens 	} else {
2257789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
22585094Slling 			*srctype = ZPROP_SRC_LOCAL;
2259789Sahrens 		} else {
2260789Sahrens 			(void) strlcpy(statbuf, source, statlen);
22615094Slling 			*srctype = ZPROP_SRC_INHERITED;
2262789Sahrens 		}
2263789Sahrens 	}
2264789Sahrens 
2265789Sahrens }
2266789Sahrens 
2267789Sahrens /*
2268789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2269789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2270789Sahrens  * human-readable form.
2271789Sahrens  *
2272789Sahrens  * Returns 0 on success, or -1 on error.
2273789Sahrens  */
2274789Sahrens int
2275789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
22765094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2277789Sahrens {
2278789Sahrens 	char *source = NULL;
2279789Sahrens 	uint64_t val;
2280789Sahrens 	char *str;
22812676Seschrock 	const char *strval;
2282789Sahrens 
2283789Sahrens 	/*
2284789Sahrens 	 * Check to see if this property applies to our object
2285789Sahrens 	 */
2286789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2287789Sahrens 		return (-1);
2288789Sahrens 
2289789Sahrens 	if (src)
22905094Slling 		*src = ZPROP_SRC_NONE;
2291789Sahrens 
2292789Sahrens 	switch (prop) {
2293789Sahrens 	case ZFS_PROP_CREATION:
2294789Sahrens 		/*
2295789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2296789Sahrens 		 * this into a string unless 'literal' is specified.
2297789Sahrens 		 */
2298789Sahrens 		{
22992885Sahrens 			val = getprop_uint64(zhp, prop, &source);
23002885Sahrens 			time_t time = (time_t)val;
2301789Sahrens 			struct tm t;
2302789Sahrens 
2303789Sahrens 			if (literal ||
2304789Sahrens 			    localtime_r(&time, &t) == NULL ||
2305789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2306789Sahrens 			    &t) == 0)
23072885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2308789Sahrens 		}
2309789Sahrens 		break;
2310789Sahrens 
2311789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2312789Sahrens 		/*
2313789Sahrens 		 * Getting the precise mountpoint can be tricky.
2314789Sahrens 		 *
2315789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2316789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2317789Sahrens 		 *    after our ancestor and append it to the inherited value.
2318789Sahrens 		 *
2319789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2320789Sahrens 		 * root to any values we return.
2321789Sahrens 		 */
23226865Srm160521 
23231356Seschrock 		str = getprop_string(zhp, prop, &source);
23241356Seschrock 
23256612Sgw25295 		if (str[0] == '/') {
23266865Srm160521 			char buf[MAXPATHLEN];
23276865Srm160521 			char *root = buf;
23281356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2329789Sahrens 
2330789Sahrens 			if (relpath[0] == '/')
2331789Sahrens 				relpath++;
23326612Sgw25295 
23336865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
23346865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
23356865Srm160521 			    (strcmp(root, "-") == 0))
23366865Srm160521 				root[0] = '\0';
23376612Sgw25295 			/*
23386612Sgw25295 			 * Special case an alternate root of '/'. This will
23396612Sgw25295 			 * avoid having multiple leading slashes in the
23406612Sgw25295 			 * mountpoint path.
23416612Sgw25295 			 */
23426612Sgw25295 			if (strcmp(root, "/") == 0)
23436612Sgw25295 				root++;
23446612Sgw25295 
23456612Sgw25295 			/*
23466612Sgw25295 			 * If the mountpoint is '/' then skip over this
23476612Sgw25295 			 * if we are obtaining either an alternate root or
23486612Sgw25295 			 * an inherited mountpoint.
23496612Sgw25295 			 */
23506612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
23516612Sgw25295 			    relpath[0] != '\0'))
23521356Seschrock 				str++;
2353789Sahrens 
2354789Sahrens 			if (relpath[0] == '\0')
2355789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
23561356Seschrock 				    root, str);
2357789Sahrens 			else
2358789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
23591356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2360789Sahrens 				    relpath);
2361789Sahrens 		} else {
2362789Sahrens 			/* 'legacy' or 'none' */
23631356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2364789Sahrens 		}
2365789Sahrens 
2366789Sahrens 		break;
2367789Sahrens 
2368789Sahrens 	case ZFS_PROP_ORIGIN:
23692885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2370789Sahrens 		    proplen);
2371789Sahrens 		/*
2372789Sahrens 		 * If there is no parent at all, return failure to indicate that
2373789Sahrens 		 * it doesn't apply to this dataset.
2374789Sahrens 		 */
2375789Sahrens 		if (propbuf[0] == '\0')
2376789Sahrens 			return (-1);
2377789Sahrens 		break;
2378789Sahrens 
2379789Sahrens 	case ZFS_PROP_QUOTA:
23805378Sck153898 	case ZFS_PROP_REFQUOTA:
2381789Sahrens 	case ZFS_PROP_RESERVATION:
23825378Sck153898 	case ZFS_PROP_REFRESERVATION:
23835378Sck153898 
23842082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23852082Seschrock 			return (-1);
2386789Sahrens 
2387789Sahrens 		/*
2388789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2389789Sahrens 		 * (unless literal is set), and indicate that it's the default
2390789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2391789Sahrens 		 * that its set locally.
2392789Sahrens 		 */
2393789Sahrens 		if (val == 0) {
2394789Sahrens 			if (literal)
2395789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2396789Sahrens 			else
2397789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2398789Sahrens 		} else {
2399789Sahrens 			if (literal)
24002856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24013912Slling 				    (u_longlong_t)val);
2402789Sahrens 			else
2403789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2404789Sahrens 		}
2405789Sahrens 		break;
2406789Sahrens 
2407789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
24082082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24092082Seschrock 			return (-1);
24102856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
24112856Snd150628 		    val / 100, (longlong_t)val % 100);
2412789Sahrens 		break;
2413789Sahrens 
2414789Sahrens 	case ZFS_PROP_TYPE:
2415789Sahrens 		switch (zhp->zfs_type) {
2416789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2417789Sahrens 			str = "filesystem";
2418789Sahrens 			break;
2419789Sahrens 		case ZFS_TYPE_VOLUME:
2420789Sahrens 			str = "volume";
2421789Sahrens 			break;
2422789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2423789Sahrens 			str = "snapshot";
2424789Sahrens 			break;
2425789Sahrens 		default:
24262082Seschrock 			abort();
2427789Sahrens 		}
2428789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2429789Sahrens 		break;
2430789Sahrens 
2431789Sahrens 	case ZFS_PROP_MOUNTED:
2432789Sahrens 		/*
2433789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2434789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2435789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2436789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2437789Sahrens 		 */
24382082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
24392082Seschrock 		    src, &source, &val) != 0)
24402082Seschrock 			return (-1);
24412082Seschrock 		if (val)
2442789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2443789Sahrens 		else
2444789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2445789Sahrens 		break;
2446789Sahrens 
2447789Sahrens 	case ZFS_PROP_NAME:
2448789Sahrens 		/*
2449789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2450789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2451789Sahrens 		 * consumers.
2452789Sahrens 		 */
2453789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2454789Sahrens 		break;
2455789Sahrens 
2456789Sahrens 	default:
24574577Sahrens 		switch (zfs_prop_get_type(prop)) {
24584787Sahrens 		case PROP_TYPE_NUMBER:
24594577Sahrens 			if (get_numeric_property(zhp, prop, src,
24604577Sahrens 			    &source, &val) != 0)
24614577Sahrens 				return (-1);
24624577Sahrens 			if (literal)
24634577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
24644577Sahrens 				    (u_longlong_t)val);
24654577Sahrens 			else
24664577Sahrens 				zfs_nicenum(val, propbuf, proplen);
24674577Sahrens 			break;
24684577Sahrens 
24694787Sahrens 		case PROP_TYPE_STRING:
24704577Sahrens 			(void) strlcpy(propbuf,
24714577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
24724577Sahrens 			break;
24734577Sahrens 
24744787Sahrens 		case PROP_TYPE_INDEX:
24754861Sahrens 			if (get_numeric_property(zhp, prop, src,
24764861Sahrens 			    &source, &val) != 0)
24774861Sahrens 				return (-1);
24784861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
24794577Sahrens 				return (-1);
24804577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
24814577Sahrens 			break;
24824577Sahrens 
24834577Sahrens 		default:
24844577Sahrens 			abort();
24854577Sahrens 		}
2486789Sahrens 	}
2487789Sahrens 
2488789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2489789Sahrens 
2490789Sahrens 	return (0);
2491789Sahrens }
2492789Sahrens 
2493789Sahrens /*
2494789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2495789Sahrens  * the given property is the appropriate type; should only be used with
2496789Sahrens  * hard-coded property types.
2497789Sahrens  */
2498789Sahrens uint64_t
2499789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2500789Sahrens {
2501789Sahrens 	char *source;
25022082Seschrock 	uint64_t val;
25032082Seschrock 
25045367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
25052082Seschrock 
25062082Seschrock 	return (val);
2507789Sahrens }
2508789Sahrens 
25095713Srm160521 int
25105713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
25115713Srm160521 {
25125713Srm160521 	char buf[64];
25135713Srm160521 
25145713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
25155713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
25165713Srm160521 }
25175713Srm160521 
2518789Sahrens /*
2519789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2520789Sahrens  */
2521789Sahrens int
2522789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
25235094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2524789Sahrens {
2525789Sahrens 	char *source;
2526789Sahrens 
2527789Sahrens 	/*
2528789Sahrens 	 * Check to see if this property applies to our object
2529789Sahrens 	 */
25304849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
25313237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
25322082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
25332082Seschrock 		    zfs_prop_to_name(prop)));
25344849Sahrens 	}
2535789Sahrens 
2536789Sahrens 	if (src)
25375094Slling 		*src = ZPROP_SRC_NONE;
2538789Sahrens 
25392082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
25402082Seschrock 		return (-1);
2541789Sahrens 
2542789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2543789Sahrens 
2544789Sahrens 	return (0);
2545789Sahrens }
2546789Sahrens 
2547789Sahrens /*
2548789Sahrens  * Returns the name of the given zfs handle.
2549789Sahrens  */
2550789Sahrens const char *
2551789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2552789Sahrens {
2553789Sahrens 	return (zhp->zfs_name);
2554789Sahrens }
2555789Sahrens 
2556789Sahrens /*
2557789Sahrens  * Returns the type of the given zfs handle.
2558789Sahrens  */
2559789Sahrens zfs_type_t
2560789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2561789Sahrens {
2562789Sahrens 	return (zhp->zfs_type);
2563789Sahrens }
2564789Sahrens 
2565789Sahrens /*
25661356Seschrock  * Iterate over all child filesystems
2567789Sahrens  */
2568789Sahrens int
25691356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2570789Sahrens {
2571789Sahrens 	zfs_cmd_t zc = { 0 };
2572789Sahrens 	zfs_handle_t *nzhp;
2573789Sahrens 	int ret;
2574789Sahrens 
25755367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25765367Sahrens 		return (0);
25775367Sahrens 
2578789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25792082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2580789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2581789Sahrens 		/*
2582789Sahrens 		 * Ignore private dataset names.
2583789Sahrens 		 */
2584789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2585789Sahrens 			continue;
2586789Sahrens 
2587789Sahrens 		/*
2588789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2589789Sahrens 		 * that the pool has since been removed.
2590789Sahrens 		 */
25912082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25922082Seschrock 		    zc.zc_name)) == NULL)
2593789Sahrens 			continue;
2594789Sahrens 
2595789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2596789Sahrens 			return (ret);
2597789Sahrens 	}
2598789Sahrens 
2599789Sahrens 	/*
2600789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2601789Sahrens 	 * returned, then the underlying dataset has been removed since we
2602789Sahrens 	 * obtained the handle.
2603789Sahrens 	 */
2604789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26052082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26062082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2607789Sahrens 
26081356Seschrock 	return (0);
26091356Seschrock }
26101356Seschrock 
26111356Seschrock /*
26121356Seschrock  * Iterate over all snapshots
26131356Seschrock  */
26141356Seschrock int
26151356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26161356Seschrock {
26171356Seschrock 	zfs_cmd_t zc = { 0 };
26181356Seschrock 	zfs_handle_t *nzhp;
26191356Seschrock 	int ret;
2620789Sahrens 
26215367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
26225367Sahrens 		return (0);
26235367Sahrens 
2624789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26252082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
26262082Seschrock 	    &zc) == 0;
2627789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2628789Sahrens 
26292082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26302082Seschrock 		    zc.zc_name)) == NULL)
2631789Sahrens 			continue;
2632789Sahrens 
2633789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2634789Sahrens 			return (ret);
2635789Sahrens 	}
2636789Sahrens 
2637789Sahrens 	/*
2638789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2639789Sahrens 	 * returned, then the underlying dataset has been removed since we
2640789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2641789Sahrens 	 */
2642789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26432082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26442082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2645789Sahrens 
2646789Sahrens 	return (0);
2647789Sahrens }
2648789Sahrens 
2649789Sahrens /*
26501356Seschrock  * Iterate over all children, snapshots and filesystems
26511356Seschrock  */
26521356Seschrock int
26531356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26541356Seschrock {
26551356Seschrock 	int ret;
26561356Seschrock 
26571356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
26581356Seschrock 		return (ret);
26591356Seschrock 
26601356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
26611356Seschrock }
26621356Seschrock 
26631356Seschrock /*
2664789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2665789Sahrens  * Can return NULL if this is a pool.
2666789Sahrens  */
2667789Sahrens static int
2668789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2669789Sahrens {
2670789Sahrens 	char *loc;
2671789Sahrens 
2672789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2673789Sahrens 		return (-1);
2674789Sahrens 
2675789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2676789Sahrens 	buf[loc - path] = '\0';
2677789Sahrens 
2678789Sahrens 	return (0);
2679789Sahrens }
2680789Sahrens 
2681789Sahrens /*
26824490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
26834490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26844490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
26854490Svb160487  * length of already existing prefix of the given path.  We also fetch the
26864490Svb160487  * 'zoned' property, which is used to validate property settings when creating
26874490Svb160487  * new datasets.
2688789Sahrens  */
2689789Sahrens static int
26904490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
26914490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2692789Sahrens {
2693789Sahrens 	zfs_cmd_t zc = { 0 };
2694789Sahrens 	char parent[ZFS_MAXNAMELEN];
2695789Sahrens 	char *slash;
26961356Seschrock 	zfs_handle_t *zhp;
26972082Seschrock 	char errbuf[1024];
26982082Seschrock 
26992082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
27002082Seschrock 	    path);
2701789Sahrens 
2702789Sahrens 	/* get parent, and check to see if this is just a pool */
2703789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
27042082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27052082Seschrock 		    "missing dataset name"));
27062082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2707789Sahrens 	}
2708789Sahrens 
2709789Sahrens 	/* check to see if the pool exists */
2710789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2711789Sahrens 		slash = parent + strlen(parent);
2712789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2713789Sahrens 	zc.zc_name[slash - parent] = '\0';
27142082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2715789Sahrens 	    errno == ENOENT) {
27162082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27172082Seschrock 		    "no such pool '%s'"), zc.zc_name);
27182082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2719789Sahrens 	}
2720789Sahrens 
2721789Sahrens 	/* check to see if the parent dataset exists */
27224490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27234490Svb160487 		if (errno == ENOENT && accept_ancestor) {
27244490Svb160487 			/*
27254490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
27264490Svb160487 			 */
27274490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27284490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27294490Svb160487 				    "no such pool '%s'"), zc.zc_name);
27304490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27314490Svb160487 			}
27324490Svb160487 		} else if (errno == ENOENT) {
27332082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27342082Seschrock 			    "parent does not exist"));
27352082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27364490Svb160487 		} else
27372082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2738789Sahrens 	}
2739789Sahrens 
27402676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2741789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
27422676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
27432082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27441356Seschrock 		zfs_close(zhp);
2745789Sahrens 		return (-1);
2746789Sahrens 	}
2747789Sahrens 
2748789Sahrens 	/* make sure parent is a filesystem */
27491356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
27502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27512082Seschrock 		    "parent is not a filesystem"));
27522082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27531356Seschrock 		zfs_close(zhp);
2754789Sahrens 		return (-1);
2755789Sahrens 	}
2756789Sahrens 
27571356Seschrock 	zfs_close(zhp);
27584490Svb160487 	if (prefixlen != NULL)
27594490Svb160487 		*prefixlen = strlen(parent);
27604490Svb160487 	return (0);
27614490Svb160487 }
27624490Svb160487 
27634490Svb160487 /*
27644490Svb160487  * Finds whether the dataset of the given type(s) exists.
27654490Svb160487  */
27664490Svb160487 boolean_t
27674490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
27684490Svb160487 {
27694490Svb160487 	zfs_handle_t *zhp;
27704490Svb160487 
27715326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
27724490Svb160487 		return (B_FALSE);
27734490Svb160487 
27744490Svb160487 	/*
27754490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27764490Svb160487 	 */
27774490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27784490Svb160487 		int ds_type = zhp->zfs_type;
27794490Svb160487 
27804490Svb160487 		zfs_close(zhp);
27814490Svb160487 		if (types & ds_type)
27824490Svb160487 			return (B_TRUE);
27834490Svb160487 	}
27844490Svb160487 	return (B_FALSE);
27854490Svb160487 }
27864490Svb160487 
27874490Svb160487 /*
27885367Sahrens  * Given a path to 'target', create all the ancestors between
27895367Sahrens  * the prefixlen portion of the path, and the target itself.
27905367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
27915367Sahrens  */
27925367Sahrens int
27935367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
27945367Sahrens {
27955367Sahrens 	zfs_handle_t *h;
27965367Sahrens 	char *cp;
27975367Sahrens 	const char *opname;
27985367Sahrens 
27995367Sahrens 	/* make sure prefix exists */
28005367Sahrens 	cp = target + prefixlen;
28015367Sahrens 	if (*cp != '/') {
28025367Sahrens 		assert(strchr(cp, '/') == NULL);
28035367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28045367Sahrens 	} else {
28055367Sahrens 		*cp = '\0';
28065367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28075367Sahrens 		*cp = '/';
28085367Sahrens 	}
28095367Sahrens 	if (h == NULL)
28105367Sahrens 		return (-1);
28115367Sahrens 	zfs_close(h);
28125367Sahrens 
28135367Sahrens 	/*
28145367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28155367Sahrens 	 * up to the prefixlen-long one.
28165367Sahrens 	 */
28175367Sahrens 	for (cp = target + prefixlen + 1;
28185367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28195367Sahrens 		char *logstr;
28205367Sahrens 
28215367Sahrens 		*cp = '\0';
28225367Sahrens 
28235367Sahrens 		h = make_dataset_handle(hdl, target);
28245367Sahrens 		if (h) {
28255367Sahrens 			/* it already exists, nothing to do here */
28265367Sahrens 			zfs_close(h);
28275367Sahrens 			continue;
28285367Sahrens 		}
28295367Sahrens 
28305367Sahrens 		logstr = hdl->libzfs_log_str;
28315367Sahrens 		hdl->libzfs_log_str = NULL;
28325367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28335367Sahrens 		    NULL) != 0) {
28345367Sahrens 			hdl->libzfs_log_str = logstr;
28355367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28365367Sahrens 			goto ancestorerr;
28375367Sahrens 		}
28385367Sahrens 
28395367Sahrens 		hdl->libzfs_log_str = logstr;
28405367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28415367Sahrens 		if (h == NULL) {
28425367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28435367Sahrens 			goto ancestorerr;
28445367Sahrens 		}
28455367Sahrens 
28465367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28475367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28485367Sahrens 			goto ancestorerr;
28495367Sahrens 		}
28505367Sahrens 
28515367Sahrens 		if (zfs_share(h) != 0) {
28525367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28535367Sahrens 			goto ancestorerr;
28545367Sahrens 		}
28555367Sahrens 
28565367Sahrens 		zfs_close(h);
28575367Sahrens 	}
28585367Sahrens 
28595367Sahrens 	return (0);
28605367Sahrens 
28615367Sahrens ancestorerr:
28625367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28635367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28645367Sahrens 	return (-1);
28655367Sahrens }
28665367Sahrens 
28675367Sahrens /*
28684490Svb160487  * Creates non-existing ancestors of the given path.
28694490Svb160487  */
28704490Svb160487 int
28714490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28724490Svb160487 {
28734490Svb160487 	int prefix;
28744490Svb160487 	uint64_t zoned;
28754490Svb160487 	char *path_copy;
28764490Svb160487 	int rc;
28774490Svb160487 
28784490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28794490Svb160487 		return (-1);
28804490Svb160487 
28814490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28824490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28834490Svb160487 		free(path_copy);
28844490Svb160487 	}
28854490Svb160487 	if (path_copy == NULL || rc != 0)
28864490Svb160487 		return (-1);
28874490Svb160487 
2888789Sahrens 	return (0);
2889789Sahrens }
2890789Sahrens 
2891789Sahrens /*
28922676Seschrock  * Create a new filesystem or volume.
2893789Sahrens  */
2894789Sahrens int
28952082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28962676Seschrock     nvlist_t *props)
2897789Sahrens {
2898789Sahrens 	zfs_cmd_t zc = { 0 };
2899789Sahrens 	int ret;
2900789Sahrens 	uint64_t size = 0;
2901789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
29022082Seschrock 	char errbuf[1024];
29032676Seschrock 	uint64_t zoned;
29042082Seschrock 
29052082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29062082Seschrock 	    "cannot create '%s'"), path);
2907789Sahrens 
2908789Sahrens 	/* validate the path, taking care to note the extended error message */
29095326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
29102082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2911789Sahrens 
2912789Sahrens 	/* validate parents exist */
29134490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2914789Sahrens 		return (-1);
2915789Sahrens 
2916789Sahrens 	/*
2917789Sahrens 	 * The failure modes when creating a dataset of a different type over
2918789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2919789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2920789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2921789Sahrens 	 * first try to see if the dataset exists.
2922789Sahrens 	 */
2923789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
29245094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
29252082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29262082Seschrock 		    "dataset already exists"));
29272082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2928789Sahrens 	}
2929789Sahrens 
2930789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2931789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2932789Sahrens 	else
2933789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2934789Sahrens 
29357184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
29363912Slling 	    zoned, NULL, errbuf)) == 0)
29372676Seschrock 		return (-1);
29382676Seschrock 
2939789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29401133Seschrock 		/*
29411133Seschrock 		 * If we are creating a volume, the size and block size must
29421133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29431133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29441133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29451133Seschrock 		 * zero.
29461133Seschrock 		 */
29472676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
29482676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
29492676Seschrock 			nvlist_free(props);
29502082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29512676Seschrock 			    "missing volume size"));
29522676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2953789Sahrens 		}
2954789Sahrens 
29552676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
29562676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29572676Seschrock 		    &blocksize)) != 0) {
29582676Seschrock 			if (ret == ENOENT) {
29592676Seschrock 				blocksize = zfs_prop_default_numeric(
29602676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29612676Seschrock 			} else {
29622676Seschrock 				nvlist_free(props);
29632676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29642676Seschrock 				    "missing volume block size"));
29652676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29662676Seschrock 			}
29672676Seschrock 		}
29682676Seschrock 
29692676Seschrock 		if (size == 0) {
29702676Seschrock 			nvlist_free(props);
29712082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29722676Seschrock 			    "volume size cannot be zero"));
29732676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29741133Seschrock 		}
29751133Seschrock 
29761133Seschrock 		if (size % blocksize != 0) {
29772676Seschrock 			nvlist_free(props);
29782082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29792676Seschrock 			    "volume size must be a multiple of volume block "
29802676Seschrock 			    "size"));
29812676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29821133Seschrock 		}
2983789Sahrens 	}
2984789Sahrens 
29855094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
29862676Seschrock 		return (-1);
29872676Seschrock 	nvlist_free(props);
29882676Seschrock 
2989789Sahrens 	/* create the dataset */
29904543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2991789Sahrens 
29923912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29932082Seschrock 		ret = zvol_create_link(hdl, path);
29943912Slling 		if (ret) {
29953912Slling 			(void) zfs_standard_error(hdl, errno,
29963912Slling 			    dgettext(TEXT_DOMAIN,
29973912Slling 			    "Volume successfully created, but device links "
29983912Slling 			    "were not created"));
29993912Slling 			zcmd_free_nvlists(&zc);
30003912Slling 			return (-1);
30013912Slling 		}
30023912Slling 	}
3003789Sahrens 
30042676Seschrock 	zcmd_free_nvlists(&zc);
30052676Seschrock 
3006789Sahrens 	/* check for failure */
3007789Sahrens 	if (ret != 0) {
3008789Sahrens 		char parent[ZFS_MAXNAMELEN];
3009789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3010789Sahrens 
3011789Sahrens 		switch (errno) {
3012789Sahrens 		case ENOENT:
30132082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30142082Seschrock 			    "no such parent '%s'"), parent);
30152082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3016789Sahrens 
3017789Sahrens 		case EINVAL:
30182082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30193413Smmusante 			    "parent '%s' is not a filesystem"), parent);
30202082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3021789Sahrens 
3022789Sahrens 		case EDOM:
30232082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30242676Seschrock 			    "volume block size must be power of 2 from "
30252676Seschrock 			    "%u to %uk"),
3026789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3027789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
30282082Seschrock 
30292676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30302082Seschrock 
30314603Sahrens 		case ENOTSUP:
30324603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30334603Sahrens 			    "pool must be upgraded to set this "
30344603Sahrens 			    "property or value"));
30354603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3036789Sahrens #ifdef _ILP32
3037789Sahrens 		case EOVERFLOW:
3038789Sahrens 			/*
3039789Sahrens 			 * This platform can't address a volume this big.
3040789Sahrens 			 */
30412082Seschrock 			if (type == ZFS_TYPE_VOLUME)
30422082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
30432082Seschrock 				    errbuf));
3044789Sahrens #endif
30452082Seschrock 			/* FALLTHROUGH */
3046789Sahrens 		default:
30472082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3048789Sahrens 		}
3049789Sahrens 	}
3050789Sahrens 
3051789Sahrens 	return (0);
3052789Sahrens }
3053789Sahrens 
3054789Sahrens /*
3055789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3056789Sahrens  * isn't mounted, and that there are no active dependents.
3057789Sahrens  */
3058789Sahrens int
3059789Sahrens zfs_destroy(zfs_handle_t *zhp)
3060789Sahrens {
3061789Sahrens 	zfs_cmd_t zc = { 0 };
3062789Sahrens 
3063789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3064789Sahrens 
30652676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30663126Sahl 		/*
30674543Smarks 		 * If user doesn't have permissions to unshare volume, then
30684543Smarks 		 * abort the request.  This would only happen for a
30694543Smarks 		 * non-privileged user.
30703126Sahl 		 */
30714543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30724543Smarks 			return (-1);
30734543Smarks 		}
30743126Sahl 
30752082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3076789Sahrens 			return (-1);
3077789Sahrens 
3078789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3079789Sahrens 	} else {
3080789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3081789Sahrens 	}
3082789Sahrens 
30834543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30843237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30852082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30862082Seschrock 		    zhp->zfs_name));
30872199Sahrens 	}
3088789Sahrens 
3089789Sahrens 	remove_mountpoint(zhp);
3090789Sahrens 
3091789Sahrens 	return (0);
3092789Sahrens }
3093789Sahrens 
30942199Sahrens struct destroydata {
30952199Sahrens 	char *snapname;
30962199Sahrens 	boolean_t gotone;
30973265Sahrens 	boolean_t closezhp;
30982199Sahrens };
30992199Sahrens 
31002199Sahrens static int
31012199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
31022199Sahrens {
31032199Sahrens 	struct destroydata *dd = arg;
31042199Sahrens 	zfs_handle_t *szhp;
31052199Sahrens 	char name[ZFS_MAXNAMELEN];
31063265Sahrens 	boolean_t closezhp = dd->closezhp;
31073265Sahrens 	int rv;
31082199Sahrens 
31092676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31102676Seschrock 	(void) strlcat(name, "@", sizeof (name));
31112676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
31122199Sahrens 
31132199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31142199Sahrens 	if (szhp) {
31152199Sahrens 		dd->gotone = B_TRUE;
31162199Sahrens 		zfs_close(szhp);
31172199Sahrens 	}
31182199Sahrens 
31192199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31202199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
31212199Sahrens 		/*
31222199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31232199Sahrens 		 * return an error, because then we wouldn't visit all
31242199Sahrens 		 * the volumes.
31252199Sahrens 		 */
31262199Sahrens 	}
31272199Sahrens 
31283265Sahrens 	dd->closezhp = B_TRUE;
31293265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
31303265Sahrens 	if (closezhp)
31313265Sahrens 		zfs_close(zhp);
31323265Sahrens 	return (rv);
31332199Sahrens }
31342199Sahrens 
31352199Sahrens /*
31362199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31372199Sahrens  */
31382199Sahrens int
31392199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
31402199Sahrens {
31412199Sahrens 	zfs_cmd_t zc = { 0 };
31422199Sahrens 	int ret;
31432199Sahrens 	struct destroydata dd = { 0 };
31442199Sahrens 
31452199Sahrens 	dd.snapname = snapname;
31462199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
31472199Sahrens 
31482199Sahrens 	if (!dd.gotone) {
31493237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31502199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
31512199Sahrens 		    zhp->zfs_name, snapname));
31522199Sahrens 	}
31532199Sahrens 
31542199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31552676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
31562199Sahrens 
31574543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
31582199Sahrens 	if (ret != 0) {
31592199Sahrens 		char errbuf[1024];
31602199Sahrens 
31612199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31622199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31632199Sahrens 
31642199Sahrens 		switch (errno) {
31652199Sahrens 		case EEXIST:
31662199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31672199Sahrens 			    "snapshot is cloned"));
31682199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31692199Sahrens 
31702199Sahrens 		default:
31712199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31722199Sahrens 			    errbuf));
31732199Sahrens 		}
31742199Sahrens 	}
31752199Sahrens 
31762199Sahrens 	return (0);
31772199Sahrens }
31782199Sahrens 
3179789Sahrens /*
3180789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3181789Sahrens  */
3182789Sahrens int
31832676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3184789Sahrens {
3185789Sahrens 	zfs_cmd_t zc = { 0 };
3186789Sahrens 	char parent[ZFS_MAXNAMELEN];
3187789Sahrens 	int ret;
31882082Seschrock 	char errbuf[1024];
31892082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31902676Seschrock 	zfs_type_t type;
31912676Seschrock 	uint64_t zoned;
3192789Sahrens 
3193789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3194789Sahrens 
31952082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31962082Seschrock 	    "cannot create '%s'"), target);
31972082Seschrock 
3198789Sahrens 	/* validate the target name */
31995326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
32002082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3201789Sahrens 
3202789Sahrens 	/* validate parents exist */
32034490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3204789Sahrens 		return (-1);
3205789Sahrens 
3206789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3207789Sahrens 
3208789Sahrens 	/* do the clone */
32092676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3210789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32112744Snn35248 		type = ZFS_TYPE_VOLUME;
32122676Seschrock 	} else {
3213789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32142744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
32152676Seschrock 	}
32162676Seschrock 
32172676Seschrock 	if (props) {
32187184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
32197184Stimh 		    zhp, errbuf)) == NULL)
32202676Seschrock 			return (-1);
32212676Seschrock 
32225094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
32232676Seschrock 			nvlist_free(props);
32242676Seschrock 			return (-1);
32252676Seschrock 		}
32262676Seschrock 
32272676Seschrock 		nvlist_free(props);
32282676Seschrock 	}
3229789Sahrens 
3230789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
32312676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
32324543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3233789Sahrens 
32342676Seschrock 	zcmd_free_nvlists(&zc);
32352676Seschrock 
3236789Sahrens 	if (ret != 0) {
3237789Sahrens 		switch (errno) {
3238789Sahrens 
3239789Sahrens 		case ENOENT:
3240789Sahrens 			/*
3241789Sahrens 			 * The parent doesn't exist.  We should have caught this
3242789Sahrens 			 * above, but there may a race condition that has since
3243789Sahrens 			 * destroyed the parent.
3244789Sahrens 			 *
3245789Sahrens 			 * At this point, we don't know whether it's the source
3246789Sahrens 			 * that doesn't exist anymore, or whether the target
3247789Sahrens 			 * dataset doesn't exist.
3248789Sahrens 			 */
32492082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32502082Seschrock 			    "no such parent '%s'"), parent);
32512082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
32522082Seschrock 
32532082Seschrock 		case EXDEV:
32542082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32552082Seschrock 			    "source and target pools differ"));
32562082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
32572082Seschrock 			    errbuf));
32582082Seschrock 
32592082Seschrock 		default:
32602082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32612082Seschrock 			    errbuf));
32622082Seschrock 		}
32632676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32642082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32652082Seschrock 	}
32662082Seschrock 
32672082Seschrock 	return (ret);
32682082Seschrock }
32692082Seschrock 
32702082Seschrock typedef struct promote_data {
32712082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32722082Seschrock 	const char *cb_target;
32732082Seschrock 	const char *cb_errbuf;
32742082Seschrock 	uint64_t cb_pivot_txg;
32752082Seschrock } promote_data_t;
32762082Seschrock 
32772082Seschrock static int
32782082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32792082Seschrock {
32802082Seschrock 	promote_data_t *pd = data;
32812082Seschrock 	zfs_handle_t *szhp;
32822082Seschrock 	char snapname[MAXPATHLEN];
32833265Sahrens 	int rv = 0;
32842082Seschrock 
32852082Seschrock 	/* We don't care about snapshots after the pivot point */
32863265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32873265Sahrens 		zfs_close(zhp);
32882082Seschrock 		return (0);
32893265Sahrens 	}
32902082Seschrock 
32912417Sahrens 	/* Remove the device link if it's a zvol. */
32922676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32932417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32942082Seschrock 
32952082Seschrock 	/* Check for conflicting names */
32962676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32972676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
32982082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
32992082Seschrock 	if (szhp != NULL) {
33002082Seschrock 		zfs_close(szhp);
33012082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
33022082Seschrock 		    "snapshot name '%s' from origin \n"
33032082Seschrock 		    "conflicts with '%s' from target"),
33042082Seschrock 		    zhp->zfs_name, snapname);
33053265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
33062082Seschrock 	}
33073265Sahrens 	zfs_close(zhp);
33083265Sahrens 	return (rv);
33092082Seschrock }
33102082Seschrock 
33112417Sahrens static int
33122417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
33132417Sahrens {
33142417Sahrens 	promote_data_t *pd = data;
33152417Sahrens 
33162417Sahrens 	/* We don't care about snapshots after the pivot point */
33173265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
33183265Sahrens 		/* Create the device link if it's a zvol. */
33193265Sahrens 		if (ZFS_IS_VOLUME(zhp))
33203265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
33213265Sahrens 	}
33223265Sahrens 
33233265Sahrens 	zfs_close(zhp);
33242417Sahrens 	return (0);
33252417Sahrens }
33262417Sahrens 
33272082Seschrock /*
33282082Seschrock  * Promotes the given clone fs to be the clone parent.
33292082Seschrock  */
33302082Seschrock int
33312082Seschrock zfs_promote(zfs_handle_t *zhp)
33322082Seschrock {
33332082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33342082Seschrock 	zfs_cmd_t zc = { 0 };
33352082Seschrock 	char parent[MAXPATHLEN];
33362082Seschrock 	char *cp;
33372082Seschrock 	int ret;
33382082Seschrock 	zfs_handle_t *pzhp;
33392082Seschrock 	promote_data_t pd;
33402082Seschrock 	char errbuf[1024];
33412082Seschrock 
33422082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33432082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
33442082Seschrock 
33452082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
33462082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33472082Seschrock 		    "snapshots can not be promoted"));
33482082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33492082Seschrock 	}
33502082Seschrock 
33515367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
33522082Seschrock 	if (parent[0] == '\0') {
33532082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33542082Seschrock 		    "not a cloned filesystem"));
33552082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33562082Seschrock 	}
33572082Seschrock 	cp = strchr(parent, '@');
33582082Seschrock 	*cp = '\0';
33592082Seschrock 
33602082Seschrock 	/* Walk the snapshots we will be moving */
33615367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
33622082Seschrock 	if (pzhp == NULL)
33632082Seschrock 		return (-1);
33642082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33652082Seschrock 	zfs_close(pzhp);
33662082Seschrock 	pd.cb_target = zhp->zfs_name;
33672082Seschrock 	pd.cb_errbuf = errbuf;
33685094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
33692082Seschrock 	if (pzhp == NULL)
33702082Seschrock 		return (-1);
33712082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33722082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33732082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33742417Sahrens 	if (ret != 0) {
33752417Sahrens 		zfs_close(pzhp);
33762082Seschrock 		return (-1);
33772417Sahrens 	}
33782082Seschrock 
33792082Seschrock 	/* issue the ioctl */
33805367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
33812676Seschrock 	    sizeof (zc.zc_value));
33822082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33834543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33842082Seschrock 
33852082Seschrock 	if (ret != 0) {
33862417Sahrens 		int save_errno = errno;
33872417Sahrens 
33882417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33892417Sahrens 		zfs_close(pzhp);
33902417Sahrens 
33912417Sahrens 		switch (save_errno) {
3392789Sahrens 		case EEXIST:
3393789Sahrens 			/*
33942082Seschrock 			 * There is a conflicting snapshot name.  We
33952082Seschrock 			 * should have caught this above, but they could
33962082Seschrock 			 * have renamed something in the mean time.
3397789Sahrens 			 */
33982082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33992082Seschrock 			    "conflicting snapshot name from parent '%s'"),
34002082Seschrock 			    parent);
34012082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3402789Sahrens 
3403789Sahrens 		default:
34042417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3405789Sahrens 		}
34062417Sahrens 	} else {
34072417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3408789Sahrens 	}
3409789Sahrens 
34102417Sahrens 	zfs_close(pzhp);
3411789Sahrens 	return (ret);
3412789Sahrens }
3413789Sahrens 
34144007Smmusante struct createdata {
34154007Smmusante 	const char *cd_snapname;
34164007Smmusante 	int cd_ifexists;
34174007Smmusante };
34184007Smmusante 
34192199Sahrens static int
34202199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
34212199Sahrens {
34224007Smmusante 	struct createdata *cd = arg;
34232676Seschrock 	int ret;
34242199Sahrens 
34252199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
34262199Sahrens 		char name[MAXPATHLEN];
34272199Sahrens 
34282676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
34292676Seschrock 		(void) strlcat(name, "@", sizeof (name));
34304007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
34314007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
34324007Smmusante 		    cd->cd_ifexists);
34332199Sahrens 		/*
34342199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
34352199Sahrens 		 * return an error, because then we wouldn't visit all
34362199Sahrens 		 * the volumes.
34372199Sahrens 		 */
34382199Sahrens 	}
34392676Seschrock 
34404007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
34412676Seschrock 
34422676Seschrock 	zfs_close(zhp);
34432676Seschrock 
34442676Seschrock 	return (ret);
34452199Sahrens }
34462199Sahrens 
3447789Sahrens /*
34483504Sahl  * Takes a snapshot of the given dataset.
3449789Sahrens  */
3450789Sahrens int
34517265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
34527265Sahrens     nvlist_t *props)
3453789Sahrens {
3454789Sahrens 	const char *delim;
34557265Sahrens 	char parent[ZFS_MAXNAMELEN];
3456789Sahrens 	zfs_handle_t *zhp;
3457789Sahrens 	zfs_cmd_t zc = { 0 };
3458789Sahrens 	int ret;
34592082Seschrock 	char errbuf[1024];
34602082Seschrock 
34612082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34622082Seschrock 	    "cannot snapshot '%s'"), path);
34632082Seschrock 
34642082Seschrock 	/* validate the target name */
34655326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
34662082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3467789Sahrens 
34687265Sahrens 	if (props) {
34697265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
34707265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
34717265Sahrens 			return (-1);
34727265Sahrens 
34737265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
34747265Sahrens 			nvlist_free(props);
34757265Sahrens 			return (-1);
34767265Sahrens 		}
34777265Sahrens 
34787265Sahrens 		nvlist_free(props);
34797265Sahrens 	}
34807265Sahrens 
3481789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34822199Sahrens 	delim = strchr(path, '@');
3483789Sahrens 	(void) strncpy(parent, path, delim - path);
3484789Sahrens 	parent[delim - path] = '\0';
3485789Sahrens 
34862082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3487789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
34887265Sahrens 		zcmd_free_nvlists(&zc);
3489789Sahrens 		return (-1);
3490789Sahrens 	}
3491789Sahrens 
34922199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34932676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34944543Smarks 	if (ZFS_IS_VOLUME(zhp))
34954543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34964543Smarks 	else
34974543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34982199Sahrens 	zc.zc_cookie = recursive;
34994543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
35002199Sahrens 
35017265Sahrens 	zcmd_free_nvlists(&zc);
35027265Sahrens 
35032199Sahrens 	/*
35042199Sahrens 	 * if it was recursive, the one that actually failed will be in
35052199Sahrens 	 * zc.zc_name.
35062199Sahrens 	 */
35074543Smarks 	if (ret != 0)
35084543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35094543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
35104543Smarks 
35112199Sahrens 	if (ret == 0 && recursive) {
35124007Smmusante 		struct createdata cd;
35134007Smmusante 
35144007Smmusante 		cd.cd_snapname = delim + 1;
35154007Smmusante 		cd.cd_ifexists = B_FALSE;
35164007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
35172199Sahrens 	}
3518789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
35192082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
35202199Sahrens 		if (ret != 0) {
35214543Smarks 			(void) zfs_standard_error(hdl, errno,
35224543Smarks 			    dgettext(TEXT_DOMAIN,
35234543Smarks 			    "Volume successfully snapshotted, but device links "
35244543Smarks 			    "were not created"));
35254543Smarks 			zfs_close(zhp);
35264543Smarks 			return (-1);
35272199Sahrens 		}
3528789Sahrens 	}
3529789Sahrens 
35302082Seschrock 	if (ret != 0)
35312082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3532789Sahrens 
3533789Sahrens 	zfs_close(zhp);
3534789Sahrens 
3535789Sahrens 	return (ret);
3536789Sahrens }
3537789Sahrens 
3538789Sahrens /*
35391294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
35401294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
35411294Slling  * is a dependent and we should just destroy it without checking the transaction
35421294Slling  * group.
3543789Sahrens  */
35441294Slling typedef struct rollback_data {
35451294Slling 	const char	*cb_target;		/* the snapshot */
35461294Slling 	uint64_t	cb_create;		/* creation time reference */
35475749Sahrens 	boolean_t	cb_error;
35482082Seschrock 	boolean_t	cb_dependent;
35495749Sahrens 	boolean_t	cb_force;
35501294Slling } rollback_data_t;
35511294Slling 
35521294Slling static int
35531294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
35541294Slling {
35551294Slling 	rollback_data_t *cbp = data;
35561294Slling 
35571294Slling 	if (!cbp->cb_dependent) {
35581294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
35591294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
35601294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
35611294Slling 		    cbp->cb_create) {
35624543Smarks 			char *logstr;
35631294Slling 
35642082Seschrock 			cbp->cb_dependent = B_TRUE;
35655446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
35665446Sahrens 			    rollback_destroy, cbp);
35672082Seschrock 			cbp->cb_dependent = B_FALSE;
35681294Slling 
35694543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
35704543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
35715446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
35724543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
35731294Slling 		}
35741294Slling 	} else {
35755749Sahrens 		/* We must destroy this clone; first unmount it */
35765749Sahrens 		prop_changelist_t *clp;
35775749Sahrens 
35785749Sahrens 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
35795749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
35805749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
35815749Sahrens 			cbp->cb_error = B_TRUE;
35825749Sahrens 			zfs_close(zhp);
35835749Sahrens 			return (0);
35845749Sahrens 		}
35855749Sahrens 		if (zfs_destroy(zhp) != 0)
35865749Sahrens 			cbp->cb_error = B_TRUE;
35875749Sahrens 		else
35885749Sahrens 			changelist_remove(clp, zhp->zfs_name);
35895751Sahrens 		(void) changelist_postfix(clp);
35905749Sahrens 		changelist_free(clp);
35911294Slling 	}
35921294Slling 
35931294Slling 	zfs_close(zhp);
35941294Slling 	return (0);
35951294Slling }
35961294Slling 
35971294Slling /*
35985446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35995446Sahrens  * data changes since then and making it the active dataset.
36005446Sahrens  *
36015446Sahrens  * Any snapshots more recent than the target are destroyed, along with
36025446Sahrens  * their dependents.
36031294Slling  */
36045446Sahrens int
36055749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3606789Sahrens {
36075446Sahrens 	rollback_data_t cb = { 0 };
36085446Sahrens 	int err;
3609789Sahrens 	zfs_cmd_t zc = { 0 };
36105713Srm160521 	boolean_t restore_resv = 0;
36115713Srm160521 	uint64_t old_volsize, new_volsize;
36125713Srm160521 	zfs_prop_t resv_prop;
3613789Sahrens 
3614789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3615789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3616789Sahrens 
36175446Sahrens 	/*
36185446Sahrens 	 * Destroy all recent snapshots and its dependends.
36195446Sahrens 	 */
36205749Sahrens 	cb.cb_force = force;
36215446Sahrens 	cb.cb_target = snap->zfs_name;
36225446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
36235446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
36245446Sahrens 
36255749Sahrens 	if (cb.cb_error)
36265749Sahrens 		return (-1);
36275446Sahrens 
36285446Sahrens 	/*
36295446Sahrens 	 * Now that we have verified that the snapshot is the latest,
36305446Sahrens 	 * rollback to the given snapshot.
36315446Sahrens 	 */
36325446Sahrens 
36335713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
36345713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
36355713Srm160521 			return (-1);
36365713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
36375713Srm160521 			return (-1);
36385713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36395713Srm160521 		restore_resv =
36405713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
36415713Srm160521 	}
3642789Sahrens 
3643789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3644789Sahrens 
36452676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3646789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3647789Sahrens 	else
3648789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3649789Sahrens 
3650789Sahrens 	/*
36515446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
36525446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
36535446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
36545446Sahrens 	 * an unlikely race condition where the user has taken a
36555446Sahrens 	 * snapshot since we verified that this was the most recent.
36565713Srm160521 	 *
3657789Sahrens 	 */
36585446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
36593237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
36602082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
36612082Seschrock 		    zhp->zfs_name);
36625717Srm160521 		return (err);
36635717Srm160521 	}
36645713Srm160521 
36655713Srm160521 	/*
36665713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
36675713Srm160521 	 * rollback reservation and the volsize has changed then set
36685713Srm160521 	 * the reservation property to the post-rollback volsize.
36695713Srm160521 	 * Make a new handle since the rollback closed the dataset.
36705713Srm160521 	 */
36715717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
36725717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
36735717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
36745717Srm160521 			zfs_close(zhp);
36755713Srm160521 			return (err);
36765717Srm160521 		}
36775713Srm160521 		if (restore_resv) {
36785713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36795713Srm160521 			if (old_volsize != new_volsize)
36805717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
36815717Srm160521 				    new_volsize);
36825713Srm160521 		}
36835713Srm160521 		zfs_close(zhp);
3684789Sahrens 	}
36855446Sahrens 	return (err);
36861294Slling }
36871294Slling 
36881294Slling /*
3689789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3690789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3691789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3692789Sahrens  * libzfs_graph.c.
3693789Sahrens  */
3694789Sahrens int
36952474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36962474Seschrock     zfs_iter_f func, void *data)
3697789Sahrens {
3698789Sahrens 	char **dependents;
3699789Sahrens 	size_t count;
3700789Sahrens 	int i;
3701789Sahrens 	zfs_handle_t *child;
3702789Sahrens 	int ret = 0;
3703789Sahrens 
37042474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
37052474Seschrock 	    &dependents, &count) != 0)
37062474Seschrock 		return (-1);
37072474Seschrock 
3708789Sahrens 	for (i = 0; i < count; i++) {
37092082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
37102082Seschrock 		    dependents[i])) == NULL)
3711789Sahrens 			continue;
3712789Sahrens 
3713789Sahrens 		if ((ret = func(child, data)) != 0)
3714789Sahrens 			break;
3715789Sahrens 	}
3716789Sahrens 
3717789Sahrens 	for (i = 0; i < count; i++)
3718789Sahrens 		free(dependents[i]);
3719789Sahrens 	free(dependents);
3720789Sahrens 
3721789Sahrens 	return (ret);
3722789Sahrens }
3723789Sahrens 
3724789Sahrens /*
3725789Sahrens  * Renames the given dataset.
3726789Sahrens  */
3727789Sahrens int
37284490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3729789Sahrens {
3730789Sahrens 	int ret;
3731789Sahrens 	zfs_cmd_t zc = { 0 };
3732789Sahrens 	char *delim;
37334007Smmusante 	prop_changelist_t *cl = NULL;
37344007Smmusante 	zfs_handle_t *zhrp = NULL;
37354007Smmusante 	char *parentname = NULL;
3736789Sahrens 	char parent[ZFS_MAXNAMELEN];
37372082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37382082Seschrock 	char errbuf[1024];
3739789Sahrens 
3740789Sahrens 	/* if we have the same exact name, just return success */
3741789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3742789Sahrens 		return (0);
3743789Sahrens 
37442082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37452082Seschrock 	    "cannot rename to '%s'"), target);
37462082Seschrock 
3747789Sahrens 	/*
3748789Sahrens 	 * Make sure the target name is valid
3749789Sahrens 	 */
3750789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
37512665Snd150628 		if ((strchr(target, '@') == NULL) ||
37522665Snd150628 		    *target == '@') {
37532665Snd150628 			/*
37542665Snd150628 			 * Snapshot target name is abbreviated,
37552665Snd150628 			 * reconstruct full dataset name
37562665Snd150628 			 */
37572665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
37582665Snd150628 			    sizeof (parent));
37592665Snd150628 			delim = strchr(parent, '@');
37602665Snd150628 			if (strchr(target, '@') == NULL)
37612665Snd150628 				*(++delim) = '\0';
37622665Snd150628 			else
37632665Snd150628 				*delim = '\0';
37642665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
37652665Snd150628 			target = parent;
37662665Snd150628 		} else {
37672665Snd150628 			/*
37682665Snd150628 			 * Make sure we're renaming within the same dataset.
37692665Snd150628 			 */
37702665Snd150628 			delim = strchr(target, '@');
37712665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
37722665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
37732665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37742665Snd150628 				    "snapshots must be part of same "
37752665Snd150628 				    "dataset"));
37762665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
37773912Slling 				    errbuf));
37782665Snd150628 			}
3779789Sahrens 		}
37805326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37812665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3782789Sahrens 	} else {
37834007Smmusante 		if (recursive) {
37844007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37854007Smmusante 			    "recursive rename must be a snapshot"));
37864007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
37874007Smmusante 		}
37884007Smmusante 
37895326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37902665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37912676Seschrock 		uint64_t unused;
37922676Seschrock 
3793789Sahrens 		/* validate parents */
37944490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3795789Sahrens 			return (-1);
3796789Sahrens 
3797789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3798789Sahrens 
3799789Sahrens 		/* make sure we're in the same pool */
3800789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3801789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3802789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
38032082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38042082Seschrock 			    "datasets must be within same pool"));
38052082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3806789Sahrens 		}
38072440Snd150628 
38082440Snd150628 		/* new name cannot be a child of the current dataset name */
38092440Snd150628 		if (strncmp(parent, zhp->zfs_name,
38103912Slling 		    strlen(zhp->zfs_name)) == 0) {
38112440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38122440Snd150628 			    "New dataset name cannot be a descendent of "
38132440Snd150628 			    "current dataset name"));
38142440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
38152440Snd150628 		}
3816789Sahrens 	}
3817789Sahrens 
38182082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
38192082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
38202082Seschrock 
3821789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3822789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
38232082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38242082Seschrock 		    "dataset is used in a non-global zone"));
38252082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3826789Sahrens 	}
3827789Sahrens 
38284007Smmusante 	if (recursive) {
38294007Smmusante 		struct destroydata dd;
38304007Smmusante 
38314183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
38324183Smmusante 		if (parentname == NULL) {
38334183Smmusante 			ret = -1;
38344183Smmusante 			goto error;
38354183Smmusante 		}
38364007Smmusante 		delim = strchr(parentname, '@');
38374007Smmusante 		*delim = '\0';
38385094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
38394007Smmusante 		if (zhrp == NULL) {
38404183Smmusante 			ret = -1;
38414183Smmusante 			goto error;
38424007Smmusante 		}
38434007Smmusante 
38444007Smmusante 		dd.snapname = delim + 1;
38454007Smmusante 		dd.gotone = B_FALSE;
38464183Smmusante 		dd.closezhp = B_TRUE;
38474007Smmusante 
38484007Smmusante 		/* We remove any zvol links prior to renaming them */
38494007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
38504007Smmusante 		if (ret) {
38514007Smmusante 			goto error;
38524007Smmusante 		}
38534007Smmusante 	} else {
38544007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
38554007Smmusante 			return (-1);
38564007Smmusante 
38574007Smmusante 		if (changelist_haszonedchild(cl)) {
38584007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38594007Smmusante 			    "child dataset with inherited mountpoint is used "
38604007Smmusante 			    "in a non-global zone"));
38614007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
38624007Smmusante 			goto error;
38634007Smmusante 		}
38644007Smmusante 
38654007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
38664007Smmusante 			goto error;
3867789Sahrens 	}
3868789Sahrens 
38692676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3870789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3871789Sahrens 	else
3872789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3873789Sahrens 
38742665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
38752676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
38762665Snd150628 
38774007Smmusante 	zc.zc_cookie = recursive;
38784007Smmusante 
38794543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
38804007Smmusante 		/*
38814007Smmusante 		 * if it was recursive, the one that actually failed will
38824007Smmusante 		 * be in zc.zc_name
38834007Smmusante 		 */
38844007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38855367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
38864007Smmusante 
38874007Smmusante 		if (recursive && errno == EEXIST) {
38884007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38894007Smmusante 			    "a child dataset already has a snapshot "
38904007Smmusante 			    "with the new name"));
38914801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38924007Smmusante 		} else {
38934007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
38944007Smmusante 		}
3895789Sahrens 
3896789Sahrens 		/*
3897789Sahrens 		 * On failure, we still want to remount any filesystems that
3898789Sahrens 		 * were previously mounted, so we don't alter the system state.
3899789Sahrens 		 */
39004007Smmusante 		if (recursive) {
39014007Smmusante 			struct createdata cd;
39024007Smmusante 
39034007Smmusante 			/* only create links for datasets that had existed */
39044007Smmusante 			cd.cd_snapname = delim + 1;
39054007Smmusante 			cd.cd_ifexists = B_TRUE;
39064007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
39074007Smmusante 			    &cd);
39084007Smmusante 		} else {
39094007Smmusante 			(void) changelist_postfix(cl);
39104007Smmusante 		}
3911789Sahrens 	} else {
39124007Smmusante 		if (recursive) {
39134007Smmusante 			struct createdata cd;
39144007Smmusante 
39154007Smmusante 			/* only create links for datasets that had existed */
39164007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
39174007Smmusante 			cd.cd_ifexists = B_TRUE;
39184007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
39194007Smmusante 			    &cd);
39204007Smmusante 		} else {
39214007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
39224007Smmusante 			ret = changelist_postfix(cl);
39234007Smmusante 		}
3924789Sahrens 	}
3925789Sahrens 
3926789Sahrens error:
39274007Smmusante 	if (parentname) {
39284007Smmusante 		free(parentname);
39294007Smmusante 	}
39304007Smmusante 	if (zhrp) {
39314007Smmusante 		zfs_close(zhrp);
39324007Smmusante 	}
39334007Smmusante 	if (cl) {
39344007Smmusante 		changelist_free(cl);
39354007Smmusante 	}
3936789Sahrens 	return (ret);
3937789Sahrens }
3938789Sahrens 
3939789Sahrens /*
3940789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3941789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3942789Sahrens  */
3943789Sahrens int
39442082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3945789Sahrens {
39464007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
39474007Smmusante }
39484007Smmusante 
39494007Smmusante static int
39504007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
39514007Smmusante {
3952789Sahrens 	zfs_cmd_t zc = { 0 };
39532082Seschrock 	di_devlink_handle_t dhdl;
39544543Smarks 	priv_set_t *priv_effective;
39554543Smarks 	int privileged;
3956789Sahrens 
3957789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3958789Sahrens 
3959789Sahrens 	/*
3960789Sahrens 	 * Issue the appropriate ioctl.
3961789Sahrens 	 */
39622082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3963789Sahrens 		switch (errno) {
3964789Sahrens 		case EEXIST:
3965789Sahrens 			/*
3966789Sahrens 			 * Silently ignore the case where the link already
3967789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3968789Sahrens 			 * times without errors.
3969789Sahrens 			 */
3970789Sahrens 			return (0);
3971789Sahrens 
39724007Smmusante 		case ENOENT:
39734007Smmusante 			/*
39744007Smmusante 			 * Dataset does not exist in the kernel.  If we
39754007Smmusante 			 * don't care (see zfs_rename), then ignore the
39764007Smmusante 			 * error quietly.
39774007Smmusante 			 */
39784007Smmusante 			if (ifexists) {
39794007Smmusante 				return (0);
39804007Smmusante 			}
39814007Smmusante 
39824007Smmusante 			/* FALLTHROUGH */
39834007Smmusante 
3984789Sahrens 		default:
39853237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39862082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39872082Seschrock 			    "for '%s'"), dataset));
3988789Sahrens 		}
3989789Sahrens 	}
3990789Sahrens 
3991789Sahrens 	/*
39924543Smarks 	 * If privileged call devfsadm and wait for the links to
39934543Smarks 	 * magically appear.
39944543Smarks 	 * Otherwise, print out an informational message.
3995789Sahrens 	 */
39964543Smarks 
39974543Smarks 	priv_effective = priv_allocset();
39984543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
39994543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
40004543Smarks 	priv_freeset(priv_effective);
40014543Smarks 
40024543Smarks 	if (privileged) {
40034543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
40044543Smarks 		    DI_MAKE_LINK)) == NULL) {
40054543Smarks 			zfs_error_aux(hdl, strerror(errno));
4006*7301SEric.Taylor@Sun.COM 			(void) zfs_error_fmt(hdl, errno,
40074543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
40084543Smarks 			    "for '%s'"), dataset);
40094543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
40104543Smarks 			return (-1);
40114543Smarks 		} else {
40124543Smarks 			(void) di_devlink_fini(&dhdl);
40134543Smarks 		}
4014789Sahrens 	} else {
40154543Smarks 		char pathname[MAXPATHLEN];
40164543Smarks 		struct stat64 statbuf;
40174543Smarks 		int i;
40184543Smarks 
40194543Smarks #define	MAX_WAIT	10
40204543Smarks 
40214543Smarks 		/*
40224543Smarks 		 * This is the poor mans way of waiting for the link
40234543Smarks 		 * to show up.  If after 10 seconds we still don't
40244543Smarks 		 * have it, then print out a message.
40254543Smarks 		 */
40264543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
40274543Smarks 		    dataset);
40284543Smarks 
40294543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
40304543Smarks 			if (stat64(pathname, &statbuf) == 0)
40314543Smarks 				break;
40324543Smarks 			(void) sleep(1);
40334543Smarks 		}
40344543Smarks 		if (i == MAX_WAIT)
40354543Smarks 			(void) printf(gettext("%s may not be immediately "
40364543Smarks 			    "available\n"), pathname);
4037789Sahrens 	}
4038789Sahrens 
4039789Sahrens 	return (0);
4040789Sahrens }
4041789Sahrens 
4042789Sahrens /*
4043789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4044789Sahrens  */
4045789Sahrens int
40462082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4047789Sahrens {
4048789Sahrens 	zfs_cmd_t zc = { 0 };
4049789Sahrens 
4050789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4051789Sahrens 
40522082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4053789Sahrens 		switch (errno) {
4054789Sahrens 		case ENXIO:
4055789Sahrens 			/*
4056789Sahrens 			 * Silently ignore the case where the link no longer
4057789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4058789Sahrens 			 * times without errors.
4059789Sahrens 			 */
4060789Sahrens 			return (0);
4061789Sahrens 
4062789Sahrens 		default:
40633237Slling 			return (zfs_standard_error_fmt(hdl, errno,
40642082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
40652082Seschrock 			    "links for '%s'"), dataset));
4066789Sahrens 		}
4067789Sahrens 	}
4068789Sahrens 
4069789Sahrens 	return (0);
4070789Sahrens }
40712676Seschrock 
40722676Seschrock nvlist_t *
40732676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
40742676Seschrock {
40752676Seschrock 	return (zhp->zfs_user_props);
40762676Seschrock }
40772676Seschrock 
40782676Seschrock /*
40793912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
40803912Slling  * display, and their maximum widths.  This does two main things:
40813912Slling  *
40823912Slling  *      - If this is a list of all properties, then expand the list to include
40833912Slling  *        all native properties, and set a flag so that for each dataset we look
40843912Slling  *        for new unique user properties and add them to the list.
40853912Slling  *
40863912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
40873912Slling  *        so that we can size the column appropriately.
40883912Slling  */
40893912Slling int
40905094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
40913912Slling {
40923912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40935094Slling 	zprop_list_t *entry;
40945094Slling 	zprop_list_t **last, **start;
40953912Slling 	nvlist_t *userprops, *propval;
40963912Slling 	nvpair_t *elem;
40973912Slling 	char *strval;
40983912Slling 	char buf[ZFS_MAXPROPLEN];
40993912Slling 
41005094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
41013912Slling 		return (-1);
41022676Seschrock 
41032676Seschrock 	userprops = zfs_get_user_props(zhp);
41042676Seschrock 
41052676Seschrock 	entry = *plp;
41062676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
41072676Seschrock 		/*
41082676Seschrock 		 * Go through and add any user properties as necessary.  We
41092676Seschrock 		 * start by incrementing our list pointer to the first
41102676Seschrock 		 * non-native property.
41112676Seschrock 		 */
41122676Seschrock 		start = plp;
41132676Seschrock 		while (*start != NULL) {
41145094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
41152676Seschrock 				break;
41162676Seschrock 			start = &(*start)->pl_next;
41172676Seschrock 		}
41182676Seschrock 
41192676Seschrock 		elem = NULL;
41202676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
41212676Seschrock 			/*
41222676Seschrock 			 * See if we've already found this property in our list.
41232676Seschrock 			 */
41242676Seschrock 			for (last = start; *last != NULL;
41252676Seschrock 			    last = &(*last)->pl_next) {
41262676Seschrock 				if (strcmp((*last)->pl_user_prop,
41272676Seschrock 				    nvpair_name(elem)) == 0)
41282676Seschrock 					break;
41292676Seschrock 			}
41302676Seschrock 
41312676Seschrock 			if (*last == NULL) {
41322676Seschrock 				if ((entry = zfs_alloc(hdl,
41335094Slling 				    sizeof (zprop_list_t))) == NULL ||
41342676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
41352676Seschrock 				    nvpair_name(elem)))) == NULL) {
41362676Seschrock 					free(entry);
41372676Seschrock 					return (-1);
41382676Seschrock 				}
41392676Seschrock 
41405094Slling 				entry->pl_prop = ZPROP_INVAL;
41412676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
41422676Seschrock 				entry->pl_all = B_TRUE;
41432676Seschrock 				*last = entry;
41442676Seschrock 			}
41452676Seschrock 		}
41462676Seschrock 	}
41472676Seschrock 
41482676Seschrock 	/*
41492676Seschrock 	 * Now go through and check the width of any non-fixed columns
41502676Seschrock 	 */
41512676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
41522676Seschrock 		if (entry->pl_fixed)
41532676Seschrock 			continue;
41542676Seschrock 
41555094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
41562676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
41572676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
41582676Seschrock 				if (strlen(buf) > entry->pl_width)
41592676Seschrock 					entry->pl_width = strlen(buf);
41602676Seschrock 			}
41612676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
41622676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
41632676Seschrock 			verify(nvlist_lookup_string(propval,
41645094Slling 			    ZPROP_VALUE, &strval) == 0);
41652676Seschrock 			if (strlen(strval) > entry->pl_width)
41662676Seschrock 				entry->pl_width = strlen(strval);
41672676Seschrock 		}
41682676Seschrock 	}
41692676Seschrock 
41702676Seschrock 	return (0);
41712676Seschrock }
41724543Smarks 
41734543Smarks int
41744543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
41754543Smarks {
41764543Smarks 	zfs_cmd_t zc = { 0 };
41774543Smarks 	nvlist_t *nvp;
41784543Smarks 	gid_t gid;
41794543Smarks 	uid_t uid;
41804543Smarks 	const gid_t *groups;
41814543Smarks 	int group_cnt;
41824543Smarks 	int error;
41834543Smarks 
41844543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
41854543Smarks 		return (no_memory(hdl));
41864543Smarks 
41874543Smarks 	uid = ucred_geteuid(cred);
41884543Smarks 	gid = ucred_getegid(cred);
41894543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
41904543Smarks 
41914543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
41924543Smarks 		return (1);
41934543Smarks 
41944543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
41954543Smarks 		nvlist_free(nvp);
41964543Smarks 		return (1);
41974543Smarks 	}
41984543Smarks 
41994543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
42004543Smarks 		nvlist_free(nvp);
42014543Smarks 		return (1);
42024543Smarks 	}
42034543Smarks 
42044543Smarks 	if (nvlist_add_uint32_array(nvp,
42054543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
42064543Smarks 		nvlist_free(nvp);
42074543Smarks 		return (1);
42084543Smarks 	}
42094543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42104543Smarks 
42115094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
42124543Smarks 		return (-1);
42134543Smarks 
42144543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
42154543Smarks 	nvlist_free(nvp);
42164543Smarks 	return (error);
42174543Smarks }
42184543Smarks 
42194543Smarks int
42204543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
42215331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
42224543Smarks {
42234543Smarks 	zfs_cmd_t zc = { 0 };
42244543Smarks 	int error;
42254543Smarks 
42264543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42274543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
42284543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
42294543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
42305331Samw 	zc.zc_share.z_sharetype = operation;
42314543Smarks 	zc.zc_share.z_sharemax = sharemax;
42324543Smarks 
42334543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
42344543Smarks 	return (error);
42354543Smarks }
4236