1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
213126Sahl 
22789Sahrens /*
235977Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens #include <assert.h>
30789Sahrens #include <ctype.h>
31789Sahrens #include <errno.h>
32789Sahrens #include <libdevinfo.h>
33789Sahrens #include <libintl.h>
34789Sahrens #include <math.h>
35789Sahrens #include <stdio.h>
36789Sahrens #include <stdlib.h>
37789Sahrens #include <strings.h>
38789Sahrens #include <unistd.h>
395367Sahrens #include <stddef.h>
40789Sahrens #include <zone.h>
412082Seschrock #include <fcntl.h>
42789Sahrens #include <sys/mntent.h>
43789Sahrens #include <sys/mnttab.h>
441294Slling #include <sys/mount.h>
454543Smarks #include <sys/avl.h>
464543Smarks #include <priv.h>
474543Smarks #include <pwd.h>
484543Smarks #include <grp.h>
494543Smarks #include <stddef.h>
504543Smarks #include <ucred.h>
51789Sahrens 
52789Sahrens #include <sys/spa.h>
532676Seschrock #include <sys/zap.h>
54789Sahrens #include <libzfs.h>
55789Sahrens 
56789Sahrens #include "zfs_namecheck.h"
57789Sahrens #include "zfs_prop.h"
58789Sahrens #include "libzfs_impl.h"
594543Smarks #include "zfs_deleg.h"
60789Sahrens 
614007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
624007Smmusante 
63789Sahrens /*
64789Sahrens  * Given a single type (not a mask of types), return the type in a human
65789Sahrens  * readable form.
66789Sahrens  */
67789Sahrens const char *
68789Sahrens zfs_type_to_name(zfs_type_t type)
69789Sahrens {
70789Sahrens 	switch (type) {
71789Sahrens 	case ZFS_TYPE_FILESYSTEM:
72789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73789Sahrens 	case ZFS_TYPE_SNAPSHOT:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75789Sahrens 	case ZFS_TYPE_VOLUME:
76789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
77789Sahrens 	}
78789Sahrens 
79789Sahrens 	return (NULL);
80789Sahrens }
81789Sahrens 
82789Sahrens /*
83789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
84789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
85789Sahrens  * We guess what the type would have been based on the path and the mask of
86789Sahrens  * acceptable types.
87789Sahrens  */
88789Sahrens static const char *
89789Sahrens path_to_str(const char *path, int types)
90789Sahrens {
91789Sahrens 	/*
92789Sahrens 	 * When given a single type, always report the exact type.
93789Sahrens 	 */
94789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
95789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98789Sahrens 	if (types == ZFS_TYPE_VOLUME)
99789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
100789Sahrens 
101789Sahrens 	/*
102789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
103789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
104789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105789Sahrens 	 * snapshot attribute and try again.
106789Sahrens 	 */
107789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
108789Sahrens 		if (strchr(path, '@') != NULL)
109789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111789Sahrens 	}
112789Sahrens 
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * The user has requested either filesystems or volumes.
116789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118789Sahrens 	 */
119789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121789Sahrens 
122789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124789Sahrens }
125789Sahrens 
126789Sahrens /*
127789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128789Sahrens  * provide a more meaningful error message.  We place a more useful message in
129789Sahrens  * 'buf' detailing exactly why the name was not valid.
130789Sahrens  */
131789Sahrens static int
1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1335326Sek110237     boolean_t modifying)
134789Sahrens {
135789Sahrens 	namecheck_err_t why;
136789Sahrens 	char what;
137789Sahrens 
138789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1392082Seschrock 		if (hdl != NULL) {
140789Sahrens 			switch (why) {
1411003Slling 			case NAME_ERR_TOOLONG:
1422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432082Seschrock 				    "name is too long"));
1441003Slling 				break;
1451003Slling 
146789Sahrens 			case NAME_ERR_LEADING_SLASH:
1472082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482082Seschrock 				    "leading slash in name"));
149789Sahrens 				break;
150789Sahrens 
151789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532082Seschrock 				    "empty component in name"));
154789Sahrens 				break;
155789Sahrens 
156789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1572082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582082Seschrock 				    "trailing slash in name"));
159789Sahrens 				break;
160789Sahrens 
161789Sahrens 			case NAME_ERR_INVALCHAR:
1622082Seschrock 				zfs_error_aux(hdl,
163789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1642082Seschrock 				    "'%c' in name"), what);
165789Sahrens 				break;
166789Sahrens 
167789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1682082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692082Seschrock 				    "multiple '@' delimiters in name"));
170789Sahrens 				break;
1712856Snd150628 
1722856Snd150628 			case NAME_ERR_NOLETTER:
1732856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1742856Snd150628 				    "pool doesn't begin with a letter"));
1752856Snd150628 				break;
1762856Snd150628 
1772856Snd150628 			case NAME_ERR_RESERVED:
1782856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1792856Snd150628 				    "name is reserved"));
1802856Snd150628 				break;
1812856Snd150628 
1822856Snd150628 			case NAME_ERR_DISKLIKE:
1832856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1842856Snd150628 				    "reserved disk name"));
1852856Snd150628 				break;
186789Sahrens 			}
187789Sahrens 		}
188789Sahrens 
189789Sahrens 		return (0);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1932082Seschrock 		if (hdl != NULL)
1942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1952082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
196789Sahrens 		return (0);
197789Sahrens 	}
198789Sahrens 
1992199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2002199Sahrens 		if (hdl != NULL)
2012199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2023413Smmusante 			    "missing '@' delimiter in snapshot name"));
2032199Sahrens 		return (0);
2042199Sahrens 	}
2052199Sahrens 
2065326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2075326Sek110237 		if (hdl != NULL)
2085326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2095326Sek110237 			    "invalid character %c in name"), '%');
2105326Sek110237 		return (0);
2115326Sek110237 	}
2125326Sek110237 
2132082Seschrock 	return (-1);
214789Sahrens }
215789Sahrens 
216789Sahrens int
217789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218789Sahrens {
2196423Sgw25295 	if (type == ZFS_TYPE_POOL)
2206423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2215326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222789Sahrens }
223789Sahrens 
224789Sahrens /*
2252676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2262676Seschrock  * properties into a separate nvlist.
2272676Seschrock  */
2284217Seschrock static nvlist_t *
2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2302676Seschrock {
2312676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2322676Seschrock 	nvpair_t *elem;
2332676Seschrock 	nvlist_t *propval;
2344217Seschrock 	nvlist_t *nvl;
2354217Seschrock 
2364217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2374217Seschrock 		(void) no_memory(hdl);
2384217Seschrock 		return (NULL);
2394217Seschrock 	}
2402676Seschrock 
2412676Seschrock 	elem = NULL;
2424217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2432676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2442676Seschrock 			continue;
2452676Seschrock 
2462676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2474217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2484217Seschrock 			nvlist_free(nvl);
2494217Seschrock 			(void) no_memory(hdl);
2504217Seschrock 			return (NULL);
2514217Seschrock 		}
2522676Seschrock 	}
2532676Seschrock 
2544217Seschrock 	return (nvl);
2552676Seschrock }
2562676Seschrock 
257*6865Srm160521 static zpool_handle_t *
258*6865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
259*6865Srm160521 {
260*6865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
261*6865Srm160521 	zpool_handle_t *zph;
262*6865Srm160521 
263*6865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
264*6865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
265*6865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
266*6865Srm160521 		hdl->libzfs_pool_handles = zph;
267*6865Srm160521 	}
268*6865Srm160521 	return (zph);
269*6865Srm160521 }
270*6865Srm160521 
271*6865Srm160521 static zpool_handle_t *
272*6865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
273*6865Srm160521 {
274*6865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
275*6865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
276*6865Srm160521 
277*6865Srm160521 	while ((zph != NULL) &&
278*6865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
279*6865Srm160521 		zph = zph->zpool_next;
280*6865Srm160521 	return (zph);
281*6865Srm160521 }
282*6865Srm160521 
283*6865Srm160521 /*
284*6865Srm160521  * Returns a handle to the pool that contains the provided dataset.
285*6865Srm160521  * If a handle to that pool already exists then that handle is returned.
286*6865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
287*6865Srm160521  */
288*6865Srm160521 static zpool_handle_t *
289*6865Srm160521 zpool_handle(zfs_handle_t *zhp)
290*6865Srm160521 {
291*6865Srm160521 	char *pool_name;
292*6865Srm160521 	int len;
293*6865Srm160521 	zpool_handle_t *zph;
294*6865Srm160521 
295*6865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
296*6865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
297*6865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
298*6865Srm160521 
299*6865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
300*6865Srm160521 	if (zph == NULL)
301*6865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
302*6865Srm160521 
303*6865Srm160521 	free(pool_name);
304*6865Srm160521 	return (zph);
305*6865Srm160521 }
306*6865Srm160521 
307*6865Srm160521 void
308*6865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
309*6865Srm160521 {
310*6865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
311*6865Srm160521 
312*6865Srm160521 	while (zph != NULL) {
313*6865Srm160521 		next = zph->zpool_next;
314*6865Srm160521 		zpool_close(zph);
315*6865Srm160521 		zph = next;
316*6865Srm160521 	}
317*6865Srm160521 	hdl->libzfs_pool_handles = NULL;
318*6865Srm160521 }
319*6865Srm160521 
3202676Seschrock /*
321789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
322789Sahrens  */
323789Sahrens static int
324789Sahrens get_stats(zfs_handle_t *zhp)
325789Sahrens {
326789Sahrens 	zfs_cmd_t zc = { 0 };
3272676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3284217Seschrock 	nvlist_t *allprops, *userprops;
329789Sahrens 
330789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
331789Sahrens 
3322676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
3332082Seschrock 		return (-1);
3341356Seschrock 
3352082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
3361356Seschrock 		if (errno == ENOMEM) {
3372676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3382676Seschrock 				zcmd_free_nvlists(&zc);
3392082Seschrock 				return (-1);
3402676Seschrock 			}
3411356Seschrock 		} else {
3422676Seschrock 			zcmd_free_nvlists(&zc);
3431356Seschrock 			return (-1);
3441356Seschrock 		}
3451356Seschrock 	}
346789Sahrens 
3472885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
348789Sahrens 
3494217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
3502676Seschrock 		zcmd_free_nvlists(&zc);
3512082Seschrock 		return (-1);
3522082Seschrock 	}
353789Sahrens 
3542676Seschrock 	zcmd_free_nvlists(&zc);
3552676Seschrock 
3564217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3574217Seschrock 		nvlist_free(allprops);
3582676Seschrock 		return (-1);
3594217Seschrock 	}
3604217Seschrock 
3614217Seschrock 	nvlist_free(zhp->zfs_props);
3624217Seschrock 	nvlist_free(zhp->zfs_user_props);
3634217Seschrock 
3644217Seschrock 	zhp->zfs_props = allprops;
3654217Seschrock 	zhp->zfs_user_props = userprops;
3662082Seschrock 
367789Sahrens 	return (0);
368789Sahrens }
369789Sahrens 
370789Sahrens /*
371789Sahrens  * Refresh the properties currently stored in the handle.
372789Sahrens  */
373789Sahrens void
374789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
375789Sahrens {
376789Sahrens 	(void) get_stats(zhp);
377789Sahrens }
378789Sahrens 
379789Sahrens /*
380789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
381789Sahrens  * zfs_iter_* to create child handles on the fly.
382789Sahrens  */
383789Sahrens zfs_handle_t *
3842082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
385789Sahrens {
3862082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3874543Smarks 	char *logstr;
3882082Seschrock 
3892082Seschrock 	if (zhp == NULL)
3902082Seschrock 		return (NULL);
3912082Seschrock 
3922082Seschrock 	zhp->zfs_hdl = hdl;
393789Sahrens 
3944543Smarks 	/*
3954543Smarks 	 * Preserve history log string.
3964543Smarks 	 * any changes performed here will be
3974543Smarks 	 * logged as an internal event.
3984543Smarks 	 */
3994543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
4004543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
4011758Sahrens top:
402789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
403789Sahrens 
404789Sahrens 	if (get_stats(zhp) != 0) {
4054543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
406789Sahrens 		free(zhp);
407789Sahrens 		return (NULL);
408789Sahrens 	}
409789Sahrens 
4101758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4111758Sahrens 		zfs_cmd_t zc = { 0 };
4121758Sahrens 
4131758Sahrens 		/*
4141758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4151758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4161758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4171758Sahrens 		 * can't be mounted.  However, it could also be that we
4181758Sahrens 		 * have crashed in the middle of one of those
4191758Sahrens 		 * operations, in which case we need to get rid of the
4201758Sahrens 		 * inconsistent state.  We do that by either rolling
4211758Sahrens 		 * back to the previous snapshot (which will fail if
4221758Sahrens 		 * there is none), or destroying the filesystem.  Note
4231758Sahrens 		 * that if we are still in the middle of an active
4241758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4251758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4261758Sahrens 		 */
4271758Sahrens 
4281758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4291758Sahrens 
4302885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4312082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4321758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
4331758Sahrens 		} else {
4341758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
4351758Sahrens 		}
4361758Sahrens 
4371758Sahrens 		/*
4385331Samw 		 * If we can successfully destroy it, pretend that it
4391758Sahrens 		 * never existed.
4401758Sahrens 		 */
4412082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
4424543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4431758Sahrens 			free(zhp);
4441758Sahrens 			errno = ENOENT;
4451758Sahrens 			return (NULL);
4461758Sahrens 		}
4475367Sahrens 		/* If we can successfully roll it back, reget the stats */
4485367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
4495367Sahrens 			goto top;
4501758Sahrens 	}
4511758Sahrens 
452789Sahrens 	/*
453789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
454789Sahrens 	 * the high-level type.
455789Sahrens 	 */
4562885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4572885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4582885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4592885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4602885Sahrens 	else
4612885Sahrens 		abort();
4622885Sahrens 
463789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
464789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
465789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
466789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
467789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
468789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
469789Sahrens 	else
4702082Seschrock 		abort();	/* we should never see any other types */
471789Sahrens 
4724543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
473*6865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
474789Sahrens 	return (zhp);
475789Sahrens }
476789Sahrens 
477789Sahrens /*
478789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
479789Sahrens  * argument is a mask of acceptable types.  The function will print an
480789Sahrens  * appropriate error message and return NULL if it can't be opened.
481789Sahrens  */
482789Sahrens zfs_handle_t *
4832082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
484789Sahrens {
485789Sahrens 	zfs_handle_t *zhp;
4862082Seschrock 	char errbuf[1024];
4872082Seschrock 
4882082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4892082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
490789Sahrens 
491789Sahrens 	/*
4922082Seschrock 	 * Validate the name before we even try to open it.
493789Sahrens 	 */
4945326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4962082Seschrock 		    "invalid dataset name"));
4972082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
498789Sahrens 		return (NULL);
499789Sahrens 	}
500789Sahrens 
501789Sahrens 	/*
502789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
503789Sahrens 	 */
504789Sahrens 	errno = 0;
5052082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5063237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
507789Sahrens 		return (NULL);
508789Sahrens 	}
509789Sahrens 
510789Sahrens 	if (!(types & zhp->zfs_type)) {
5112082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5122142Seschrock 		zfs_close(zhp);
513789Sahrens 		return (NULL);
514789Sahrens 	}
515789Sahrens 
516789Sahrens 	return (zhp);
517789Sahrens }
518789Sahrens 
519789Sahrens /*
520789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
521789Sahrens  */
522789Sahrens void
523789Sahrens zfs_close(zfs_handle_t *zhp)
524789Sahrens {
525789Sahrens 	if (zhp->zfs_mntopts)
526789Sahrens 		free(zhp->zfs_mntopts);
5272676Seschrock 	nvlist_free(zhp->zfs_props);
5282676Seschrock 	nvlist_free(zhp->zfs_user_props);
529789Sahrens 	free(zhp);
530789Sahrens }
531789Sahrens 
5325713Srm160521 int
5335713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
5345713Srm160521 {
535*6865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
536*6865Srm160521 
5375713Srm160521 	if (zpool_handle == NULL)
5385713Srm160521 		return (-1);
5395713Srm160521 
5405713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
5415713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
5425713Srm160521 	return (0);
5435713Srm160521 }
5445713Srm160521 
5455713Srm160521 /*
5465713Srm160521  * The choice of reservation property depends on the SPA version.
5475713Srm160521  */
5485713Srm160521 static int
5495713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5505713Srm160521 {
5515713Srm160521 	int spa_version;
5525713Srm160521 
5535713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5545713Srm160521 		return (-1);
5555713Srm160521 
5565713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5575713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5585713Srm160521 	else
5595713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5605713Srm160521 
5615713Srm160521 	return (0);
5625713Srm160521 }
5635713Srm160521 
5643912Slling /*
5652676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
5662676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
5672676Seschrock  * strings.
568789Sahrens  */
5695094Slling static nvlist_t *
5705094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
5715094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
572789Sahrens {
5732676Seschrock 	nvpair_t *elem;
5742676Seschrock 	uint64_t intval;
5752676Seschrock 	char *strval;
5765094Slling 	zfs_prop_t prop;
5772676Seschrock 	nvlist_t *ret;
5785331Samw 	int chosen_normal = -1;
5795331Samw 	int chosen_utf = -1;
5802676Seschrock 
5812676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
5822676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5833413Smmusante 		    "snapshot properties cannot be modified"));
5842676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5855094Slling 		return (NULL);
5865094Slling 	}
5875094Slling 
5885094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5895094Slling 		(void) no_memory(hdl);
5905094Slling 		return (NULL);
591789Sahrens 	}
592789Sahrens 
5932676Seschrock 	elem = NULL;
5942676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5955094Slling 		const char *propname = nvpair_name(elem);
5962676Seschrock 
5972676Seschrock 		/*
5982676Seschrock 		 * Make sure this property is valid and applies to this type.
5992676Seschrock 		 */
6005094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
6015094Slling 			if (!zfs_prop_user(propname)) {
6022676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6035094Slling 				    "invalid property '%s'"), propname);
6045094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6055094Slling 				goto error;
6065094Slling 			}
6075094Slling 
6085094Slling 			/*
6095094Slling 			 * If this is a user property, make sure it's a
6105094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
6115094Slling 			 */
6125094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
6135094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6145094Slling 				    "'%s' must be a string"), propname);
6155094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6165094Slling 				goto error;
6175094Slling 			}
6185094Slling 
6195094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
6205094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6215094Slling 				    "property name '%s' is too long"),
6222676Seschrock 				    propname);
6232676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6242676Seschrock 				goto error;
6252676Seschrock 			}
6262676Seschrock 
6272676Seschrock 			(void) nvpair_value_string(elem, &strval);
6282676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
6292676Seschrock 				(void) no_memory(hdl);
6302676Seschrock 				goto error;
6312676Seschrock 			}
6322676Seschrock 			continue;
633789Sahrens 		}
6342676Seschrock 
6352676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
6362676Seschrock 			zfs_error_aux(hdl,
6372676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
6382676Seschrock 			    "apply to datasets of this type"), propname);
6392676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
6402676Seschrock 			goto error;
6412676Seschrock 		}
6422676Seschrock 
6432676Seschrock 		if (zfs_prop_readonly(prop) &&
6445331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
6452676Seschrock 			zfs_error_aux(hdl,
6462676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
6472676Seschrock 			    propname);
6482676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
6492676Seschrock 			goto error;
6502676Seschrock 		}
6512676Seschrock 
6525094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
6535094Slling 		    &strval, &intval, errbuf) != 0)
6542676Seschrock 			goto error;
6552676Seschrock 
6562676Seschrock 		/*
6572676Seschrock 		 * Perform some additional checks for specific properties.
6582676Seschrock 		 */
6592676Seschrock 		switch (prop) {
6604577Sahrens 		case ZFS_PROP_VERSION:
6614577Sahrens 		{
6624577Sahrens 			int version;
6634577Sahrens 
6644577Sahrens 			if (zhp == NULL)
6654577Sahrens 				break;
6664577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6674577Sahrens 			if (intval < version) {
6684577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6694577Sahrens 				    "Can not downgrade; already at version %u"),
6704577Sahrens 				    version);
6714577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6724577Sahrens 				goto error;
6734577Sahrens 			}
6744577Sahrens 			break;
6754577Sahrens 		}
6764577Sahrens 
6772676Seschrock 		case ZFS_PROP_RECORDSIZE:
6782676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
6792676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
6802676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
6812676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
6822082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6832676Seschrock 				    "'%s' must be power of 2 from %u "
6842676Seschrock 				    "to %uk"), propname,
6852676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
6862676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
6872676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6882676Seschrock 				goto error;
689789Sahrens 			}
690789Sahrens 			break;
691789Sahrens 
6923126Sahl 		case ZFS_PROP_SHAREISCSI:
6933126Sahl 			if (strcmp(strval, "off") != 0 &&
6943126Sahl 			    strcmp(strval, "on") != 0 &&
6953126Sahl 			    strcmp(strval, "type=disk") != 0) {
6963126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6973126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6983126Sahl 				    propname);
6993126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7003126Sahl 				goto error;
7013126Sahl 			}
7023126Sahl 
7033126Sahl 			break;
7043126Sahl 
7052676Seschrock 		case ZFS_PROP_MOUNTPOINT:
7064778Srm160521 		{
7074778Srm160521 			namecheck_err_t why;
7084778Srm160521 
7092676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
7102676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
7112676Seschrock 				break;
7122676Seschrock 
7134778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
7144778Srm160521 				switch (why) {
7154778Srm160521 				case NAME_ERR_LEADING_SLASH:
7164778Srm160521 					zfs_error_aux(hdl,
7174778Srm160521 					    dgettext(TEXT_DOMAIN,
7184778Srm160521 					    "'%s' must be an absolute path, "
7194778Srm160521 					    "'none', or 'legacy'"), propname);
7204778Srm160521 					break;
7214778Srm160521 				case NAME_ERR_TOOLONG:
7224778Srm160521 					zfs_error_aux(hdl,
7234778Srm160521 					    dgettext(TEXT_DOMAIN,
7244778Srm160521 					    "component of '%s' is too long"),
7254778Srm160521 					    propname);
7264778Srm160521 					break;
7274778Srm160521 				}
7282676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7292676Seschrock 				goto error;
730789Sahrens 			}
7314778Srm160521 		}
7324778Srm160521 
7333126Sahl 			/*FALLTHRU*/
7343126Sahl 
7355331Samw 		case ZFS_PROP_SHARESMB:
7363126Sahl 		case ZFS_PROP_SHARENFS:
7373126Sahl 			/*
7385331Samw 			 * For the mountpoint and sharenfs or sharesmb
7395331Samw 			 * properties, check if it can be set in a
7405331Samw 			 * global/non-global zone based on
7413126Sahl 			 * the zoned property value:
7423126Sahl 			 *
7433126Sahl 			 *		global zone	    non-global zone
7443126Sahl 			 * --------------------------------------------------
7453126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
7463126Sahl 			 *		sharenfs (no)	    sharenfs (no)
7475331Samw 			 *		sharesmb (no)	    sharesmb (no)
7483126Sahl 			 *
7493126Sahl 			 * zoned=off	mountpoint (yes)	N/A
7503126Sahl 			 *		sharenfs (yes)
7515331Samw 			 *		sharesmb (yes)
7523126Sahl 			 */
7532676Seschrock 			if (zoned) {
7542676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
7552676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7562676Seschrock 					    "'%s' cannot be set on "
7572676Seschrock 					    "dataset in a non-global zone"),
7582676Seschrock 					    propname);
7592676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7602676Seschrock 					    errbuf);
7612676Seschrock 					goto error;
7625331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
7635331Samw 				    prop == ZFS_PROP_SHARESMB) {
7642676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7652676Seschrock 					    "'%s' cannot be set in "
7662676Seschrock 					    "a non-global zone"), propname);
7672676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7682676Seschrock 					    errbuf);
7692676Seschrock 					goto error;
7702676Seschrock 				}
7712676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
7722676Seschrock 				/*
7732676Seschrock 				 * If zoned property is 'off', this must be in
7742676Seschrock 				 * a globle zone. If not, something is wrong.
7752676Seschrock 				 */
7762676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7772676Seschrock 				    "'%s' cannot be set while dataset "
7782676Seschrock 				    "'zoned' property is set"), propname);
7792676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
7802676Seschrock 				goto error;
7812676Seschrock 			}
7823126Sahl 
7834180Sdougm 			/*
7844180Sdougm 			 * At this point, it is legitimate to set the
7854180Sdougm 			 * property. Now we want to make sure that the
7864180Sdougm 			 * property value is valid if it is sharenfs.
7874180Sdougm 			 */
7885331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
7895331Samw 			    prop == ZFS_PROP_SHARESMB) &&
7904217Seschrock 			    strcmp(strval, "on") != 0 &&
7914217Seschrock 			    strcmp(strval, "off") != 0) {
7925331Samw 				zfs_share_proto_t proto;
7935331Samw 
7945331Samw 				if (prop == ZFS_PROP_SHARESMB)
7955331Samw 					proto = PROTO_SMB;
7965331Samw 				else
7975331Samw 					proto = PROTO_NFS;
7984180Sdougm 
7994180Sdougm 				/*
8005331Samw 				 * Must be an valid sharing protocol
8015331Samw 				 * option string so init the libshare
8025331Samw 				 * in order to enable the parser and
8035331Samw 				 * then parse the options. We use the
8045331Samw 				 * control API since we don't care about
8055331Samw 				 * the current configuration and don't
8064180Sdougm 				 * want the overhead of loading it
8074180Sdougm 				 * until we actually do something.
8084180Sdougm 				 */
8094180Sdougm 
8104217Seschrock 				if (zfs_init_libshare(hdl,
8114217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
8124217Seschrock 					/*
8134217Seschrock 					 * An error occurred so we can't do
8144217Seschrock 					 * anything
8154217Seschrock 					 */
8164217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8174217Seschrock 					    "'%s' cannot be set: problem "
8184217Seschrock 					    "in share initialization"),
8194217Seschrock 					    propname);
8204217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8214217Seschrock 					    errbuf);
8224217Seschrock 					goto error;
8234217Seschrock 				}
8244217Seschrock 
8255331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
8264217Seschrock 					/*
8274217Seschrock 					 * There was an error in parsing so
8284217Seschrock 					 * deal with it by issuing an error
8294217Seschrock 					 * message and leaving after
8304217Seschrock 					 * uninitializing the the libshare
8314217Seschrock 					 * interface.
8324217Seschrock 					 */
8334217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8344217Seschrock 					    "'%s' cannot be set to invalid "
8354217Seschrock 					    "options"), propname);
8364217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8374217Seschrock 					    errbuf);
8384217Seschrock 					zfs_uninit_libshare(hdl);
8394217Seschrock 					goto error;
8404217Seschrock 				}
8414180Sdougm 				zfs_uninit_libshare(hdl);
8424180Sdougm 			}
8434180Sdougm 
8443126Sahl 			break;
8455331Samw 		case ZFS_PROP_UTF8ONLY:
8465331Samw 			chosen_utf = (int)intval;
8475331Samw 			break;
8485331Samw 		case ZFS_PROP_NORMALIZE:
8495331Samw 			chosen_normal = (int)intval;
8505331Samw 			break;
8512676Seschrock 		}
8522676Seschrock 
8532676Seschrock 		/*
8542676Seschrock 		 * For changes to existing volumes, we have some additional
8552676Seschrock 		 * checks to enforce.
8562676Seschrock 		 */
8572676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
8582676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
8592676Seschrock 			    ZFS_PROP_VOLSIZE);
8602676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
8612676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
8622676Seschrock 			char buf[64];
8632676Seschrock 
8642676Seschrock 			switch (prop) {
8652676Seschrock 			case ZFS_PROP_RESERVATION:
8665378Sck153898 			case ZFS_PROP_REFRESERVATION:
8672676Seschrock 				if (intval > volsize) {
8682676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8692676Seschrock 					    "'%s' is greater than current "
8702676Seschrock 					    "volume size"), propname);
8712676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8722676Seschrock 					    errbuf);
8732676Seschrock 					goto error;
8742676Seschrock 				}
8752676Seschrock 				break;
8762676Seschrock 
8772676Seschrock 			case ZFS_PROP_VOLSIZE:
8782676Seschrock 				if (intval % blocksize != 0) {
8792676Seschrock 					zfs_nicenum(blocksize, buf,
8802676Seschrock 					    sizeof (buf));
8812676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8822676Seschrock 					    "'%s' must be a multiple of "
8832676Seschrock 					    "volume block size (%s)"),
8842676Seschrock 					    propname, buf);
8852676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8862676Seschrock 					    errbuf);
8872676Seschrock 					goto error;
8882676Seschrock 				}
8892676Seschrock 
8902676Seschrock 				if (intval == 0) {
8912676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8922676Seschrock 					    "'%s' cannot be zero"),
8932676Seschrock 					    propname);
8942676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8952676Seschrock 					    errbuf);
8962676Seschrock 					goto error;
897789Sahrens 				}
8983126Sahl 				break;
899789Sahrens 			}
900789Sahrens 		}
901789Sahrens 	}
902789Sahrens 
9032676Seschrock 	/*
9045331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
9055331Samw 	 * enforce rejection of non-UTF8 names.
9065331Samw 	 *
9075331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
9085331Samw 	 * was explicitly not chosen, it is an error.
9095331Samw 	 */
9105498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
9115331Samw 		if (nvlist_add_uint64(ret,
9125331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
9135331Samw 			(void) no_memory(hdl);
9145331Samw 			goto error;
9155331Samw 		}
9165498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
9175331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9185331Samw 		    "'%s' must be set 'on' if normalization chosen"),
9195331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
9205331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9215331Samw 		goto error;
9225331Samw 	}
9235331Samw 
9245331Samw 	/*
9252676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
9262676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
9272676Seschrock 	 */
9282676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
9292676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
9302676Seschrock 	    &intval) == 0) {
9312676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
9322676Seschrock 		    ZFS_PROP_VOLSIZE);
9335481Sck153898 		uint64_t old_reservation;
9342676Seschrock 		uint64_t new_reservation;
9355481Sck153898 		zfs_prop_t resv_prop;
9365713Srm160521 
9375713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
9385481Sck153898 			goto error;
9395481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
9402676Seschrock 
9412676Seschrock 		if (old_volsize == old_reservation &&
9425481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
9432676Seschrock 		    &new_reservation) != 0) {
9442676Seschrock 			if (nvlist_add_uint64(ret,
9455481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
9462676Seschrock 				(void) no_memory(hdl);
9472676Seschrock 				goto error;
9482676Seschrock 			}
9492676Seschrock 		}
9502676Seschrock 	}
9512676Seschrock 	return (ret);
9522676Seschrock 
9532676Seschrock error:
9542676Seschrock 	nvlist_free(ret);
9552676Seschrock 	return (NULL);
956789Sahrens }
957789Sahrens 
9584543Smarks static int
9594543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9604543Smarks     uint64_t *ret_who)
9614543Smarks {
9624543Smarks 	struct passwd *pwd;
9634543Smarks 	struct group *grp;
9644543Smarks 	uid_t id;
9654543Smarks 
9664543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9674543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9684543Smarks 		*ret_who = -1;
9694543Smarks 		return (0);
9704543Smarks 	}
9714543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9724543Smarks 		return (EZFS_BADWHO);
9734543Smarks 
9744543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9754543Smarks 	    strcmp(who, "everyone") == 0) {
9764543Smarks 		*ret_who = -1;
9774543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9784543Smarks 		return (0);
9794543Smarks 	}
9804543Smarks 
9814543Smarks 	pwd = getpwnam(who);
9824543Smarks 	grp = getgrnam(who);
9834543Smarks 
9844543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9854543Smarks 		*ret_who = pwd->pw_uid;
9864543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9874543Smarks 		*ret_who = grp->gr_gid;
9884543Smarks 	} else if (pwd) {
9894543Smarks 		*ret_who = pwd->pw_uid;
9904543Smarks 		*who_type = ZFS_DELEG_USER;
9914543Smarks 	} else if (grp) {
9924543Smarks 		*ret_who = grp->gr_gid;
9934543Smarks 		*who_type = ZFS_DELEG_GROUP;
9944543Smarks 	} else {
9954543Smarks 		char *end;
9964543Smarks 
9974543Smarks 		id = strtol(who, &end, 10);
9984543Smarks 		if (errno != 0 || *end != '\0') {
9994543Smarks 			return (EZFS_BADWHO);
10004543Smarks 		} else {
10014543Smarks 			*ret_who = id;
10024543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
10034543Smarks 				*who_type = ZFS_DELEG_USER;
10044543Smarks 		}
10054543Smarks 	}
10064543Smarks 
10074543Smarks 	return (0);
10084543Smarks }
10094543Smarks 
10104543Smarks static void
10114543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
10124543Smarks {
10134543Smarks 	if (perms_nvp != NULL) {
10144543Smarks 		verify(nvlist_add_nvlist(who_nvp,
10154543Smarks 		    name, perms_nvp) == 0);
10164543Smarks 	} else {
10174543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
10184543Smarks 	}
10194543Smarks }
10204543Smarks 
10214543Smarks static void
10224543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
10234543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
10244543Smarks     nvlist_t *sets_nvp)
10254543Smarks {
10264543Smarks 	boolean_t do_perms, do_sets;
10274543Smarks 	char name[ZFS_MAX_DELEG_NAME];
10284543Smarks 
10294543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
10304543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
10314543Smarks 
10324543Smarks 	if (!do_perms && !do_sets)
10334543Smarks 		do_perms = do_sets = B_TRUE;
10344543Smarks 
10354543Smarks 	if (do_perms) {
10364543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
10374543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10384543Smarks 		    whostr : (void *)&whoid);
10394543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
10404543Smarks 	}
10414543Smarks 	if (do_sets) {
10424543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
10434543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10444543Smarks 		    whostr : (void *)&whoid);
10454543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
10464543Smarks 	}
10474543Smarks }
10484543Smarks 
10494543Smarks static void
10504543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
10514543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
10524543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10534543Smarks {
10544543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10554543Smarks 		helper(who_type, whoid, whostr, 0,
10564543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10574543Smarks 	} else {
10584543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10594543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10604543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10614543Smarks 		}
10624543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10634543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10644543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10654543Smarks 		}
10664543Smarks 	}
10674543Smarks }
10684543Smarks 
10694543Smarks /*
10704543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10714543Smarks  *
10724543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10734543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10744543Smarks  * base attribute named stored in the dsl.
10754543Smarks  * Arguments:
10764543Smarks  *
10774543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10784543Smarks  *           whostr may be null for everyone or create perms.
10794543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10804543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10815331Samw  * perms:    common separated list of permissions.  May be null if user
10824543Smarks  *           is requested to remove permissions by who.
10834543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10844543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10854543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10864543Smarks  *           The output nvp will look something like this.
10874543Smarks  *              ul$1234 -> {create ; destroy }
10884543Smarks  *              Ul$1234 -> { @myset }
10894543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10904543Smarks  */
10914543Smarks int
10924543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10934543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10944543Smarks {
10954543Smarks 	nvlist_t *who_nvp;
10964543Smarks 	nvlist_t *perms_nvp = NULL;
10974543Smarks 	nvlist_t *sets_nvp = NULL;
10984543Smarks 	char errbuf[1024];
10994787Sahrens 	char *who_tok, *perm;
11004543Smarks 	int error;
11014543Smarks 
11024543Smarks 	*nvp = NULL;
11034543Smarks 
11044543Smarks 	if (perms) {
11054543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
11064543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11074543Smarks 			return (1);
11084543Smarks 		}
11094543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
11104543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11114543Smarks 			nvlist_free(perms_nvp);
11124543Smarks 			return (1);
11134543Smarks 		}
11144543Smarks 	}
11154543Smarks 
11164543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
11174543Smarks 		if (perms_nvp)
11184543Smarks 			nvlist_free(perms_nvp);
11194543Smarks 		if (sets_nvp)
11204543Smarks 			nvlist_free(sets_nvp);
11214543Smarks 		return (1);
11224543Smarks 	}
11234543Smarks 
11244543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
11254543Smarks 		namecheck_err_t why;
11264543Smarks 		char what;
11274543Smarks 
11284543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
11294787Sahrens 			nvlist_free(who_nvp);
11304787Sahrens 			if (perms_nvp)
11314787Sahrens 				nvlist_free(perms_nvp);
11324787Sahrens 			if (sets_nvp)
11334787Sahrens 				nvlist_free(sets_nvp);
11344787Sahrens 
11354543Smarks 			switch (why) {
11364543Smarks 			case NAME_ERR_NO_AT:
11374543Smarks 				zfs_error_aux(zhp->zfs_hdl,
11384543Smarks 				    dgettext(TEXT_DOMAIN,
11394543Smarks 				    "set definition must begin with an '@' "
11404543Smarks 				    "character"));
11414543Smarks 			}
11424543Smarks 			return (zfs_error(zhp->zfs_hdl,
11434543Smarks 			    EZFS_BADPERMSET, whostr));
11444543Smarks 		}
11454543Smarks 	}
11464543Smarks 
11474543Smarks 	/*
11484543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
11494543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
11504543Smarks 	 * other sets_nvp will have only permssion set names in it.
11514543Smarks 	 */
11524787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11534787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11544787Sahrens 
11554787Sahrens 		if (perm_canonical) {
11564787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11574787Sahrens 			    perm_canonical) == 0);
11584787Sahrens 		} else if (perm[0] == '@') {
11594787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11604787Sahrens 		} else {
11614787Sahrens 			nvlist_free(who_nvp);
11624787Sahrens 			nvlist_free(perms_nvp);
11634787Sahrens 			nvlist_free(sets_nvp);
11644787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11654543Smarks 		}
11664543Smarks 	}
11674543Smarks 
11684543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11694543Smarks 		who_tok = strtok(whostr, ",");
11704543Smarks 		if (who_tok == NULL) {
11714543Smarks 			nvlist_free(who_nvp);
11724787Sahrens 			if (perms_nvp)
11734787Sahrens 				nvlist_free(perms_nvp);
11744543Smarks 			if (sets_nvp)
11754543Smarks 				nvlist_free(sets_nvp);
11764543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11774543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11784543Smarks 			    whostr);
11794543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11804543Smarks 		}
11814543Smarks 	}
11824543Smarks 
11834543Smarks 	/*
11844543Smarks 	 * Now create the nvlist(s)
11854543Smarks 	 */
11864543Smarks 	do {
11874543Smarks 		uint64_t who_id;
11884543Smarks 
11894543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11904543Smarks 		    &who_id);
11914543Smarks 		if (error) {
11924543Smarks 			nvlist_free(who_nvp);
11934787Sahrens 			if (perms_nvp)
11944787Sahrens 				nvlist_free(perms_nvp);
11954543Smarks 			if (sets_nvp)
11964543Smarks 				nvlist_free(sets_nvp);
11974543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11984543Smarks 			    dgettext(TEXT_DOMAIN,
11994543Smarks 			    "Unable to determine uid/gid for "
12004543Smarks 			    "%s "), who_tok);
12014543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
12024543Smarks 		}
12034543Smarks 
12044543Smarks 		/*
12054543Smarks 		 * add entries for both local and descendent when required
12064543Smarks 		 */
12074543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
12084543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
12094543Smarks 
12104543Smarks 	} while (who_tok = strtok(NULL, ","));
12114543Smarks 	*nvp = who_nvp;
12124543Smarks 	return (0);
12134543Smarks }
12144543Smarks 
12154543Smarks static int
12164543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
12174543Smarks {
12184543Smarks 	zfs_cmd_t zc = { 0 };
12194543Smarks 	int error;
12204543Smarks 	char errbuf[1024];
12214543Smarks 
12224543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
12234543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
12244543Smarks 	    zhp->zfs_name);
12254543Smarks 
12265094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
12274543Smarks 		return (-1);
12284543Smarks 
12294543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12304543Smarks 	zc.zc_perm_action = unset;
12314543Smarks 
12324543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
12334543Smarks 	if (error && errno == ENOTSUP) {
12344543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
12354543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
12364543Smarks 		zcmd_free_nvlists(&zc);
12374543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
12384543Smarks 	} else if (error) {
12394543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
12404543Smarks 	}
12414543Smarks 	zcmd_free_nvlists(&zc);
12424543Smarks 
12434543Smarks 	return (error);
12444543Smarks }
12454543Smarks 
12464543Smarks int
12474543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
12484543Smarks {
12494543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12504543Smarks }
12514543Smarks 
12524543Smarks int
12534543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12544543Smarks {
12554543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12564543Smarks }
12574543Smarks 
12584543Smarks static int
12594543Smarks perm_compare(const void *arg1, const void *arg2)
12604543Smarks {
12614543Smarks 	const zfs_perm_node_t *node1 = arg1;
12624543Smarks 	const zfs_perm_node_t *node2 = arg2;
12634543Smarks 	int ret;
12644543Smarks 
12654543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12664543Smarks 
12674543Smarks 	if (ret > 0)
12684543Smarks 		return (1);
12694543Smarks 	if (ret < 0)
12704543Smarks 		return (-1);
12714543Smarks 	else
12724543Smarks 		return (0);
12734543Smarks }
12744543Smarks 
12754543Smarks static void
12764543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12774543Smarks {
12784543Smarks 	zfs_perm_node_t *permnode;
12795367Sahrens 	void *cookie = NULL;
12805367Sahrens 
12815367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12824543Smarks 		free(permnode);
12835367Sahrens 	avl_destroy(tree);
12844543Smarks }
12854543Smarks 
12864543Smarks static void
12874543Smarks zfs_destroy_tree(avl_tree_t *tree)
12884543Smarks {
12894543Smarks 	zfs_allow_node_t *allownode;
12905367Sahrens 	void *cookie = NULL;
12915367Sahrens 
12924543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12934543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12944543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12954543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12964543Smarks 		free(allownode);
12974543Smarks 	}
12985367Sahrens 	avl_destroy(tree);
12994543Smarks }
13004543Smarks 
13014543Smarks void
13024543Smarks zfs_free_allows(zfs_allow_t *allow)
13034543Smarks {
13044543Smarks 	zfs_allow_t *allownext;
13054543Smarks 	zfs_allow_t *freeallow;
13064543Smarks 
13074543Smarks 	allownext = allow;
13084543Smarks 	while (allownext) {
13094543Smarks 		zfs_destroy_tree(&allownext->z_sets);
13104543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
13114543Smarks 		zfs_destroy_tree(&allownext->z_user);
13124543Smarks 		zfs_destroy_tree(&allownext->z_group);
13134543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
13144543Smarks 		freeallow = allownext;
13154543Smarks 		allownext = allownext->z_next;
13164543Smarks 		free(freeallow);
13174543Smarks 	}
13184543Smarks }
13194543Smarks 
13204543Smarks static zfs_allow_t *
13214543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
13224543Smarks {
13234543Smarks 	zfs_allow_t *ptree;
13244543Smarks 
13254543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
13264543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
13274543Smarks 		return (NULL);
13284543Smarks 	}
13294543Smarks 
13304543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
13314543Smarks 	avl_create(&ptree->z_sets,
13324543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13334543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13344543Smarks 	avl_create(&ptree->z_crperms,
13354543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13364543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13374543Smarks 	avl_create(&ptree->z_user,
13384543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13394543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13404543Smarks 	avl_create(&ptree->z_group,
13414543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13424543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13434543Smarks 	avl_create(&ptree->z_everyone,
13444543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13454543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13464543Smarks 
13474543Smarks 	if (prev)
13484543Smarks 		prev->z_next = ptree;
13494543Smarks 	ptree->z_next = NULL;
13504543Smarks 	return (ptree);
13514543Smarks }
13524543Smarks 
13534543Smarks /*
13544543Smarks  * Add permissions to the appropriate AVL permission tree.
13554543Smarks  * The appropriate tree may not be the requested tree.
13564543Smarks  * For example if ld indicates a local permission, but
13574543Smarks  * same permission also exists as a descendent permission
13584543Smarks  * then the permission will be removed from the descendent
13594543Smarks  * tree and add the the local+descendent tree.
13604543Smarks  */
13614543Smarks static int
13624543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13634543Smarks     char *perm, char ld)
13644543Smarks {
13654543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13664543Smarks 	zfs_perm_node_t *newnode;
13674543Smarks 	avl_index_t where, where2;
13684543Smarks 	avl_tree_t *tree, *altree;
13694543Smarks 
13704543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13714543Smarks 
13724543Smarks 	if (ld == ZFS_DELEG_NA) {
13734543Smarks 		tree =  &allownode->z_localdescend;
13744543Smarks 		altree = &allownode->z_descend;
13754543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13764543Smarks 		tree = &allownode->z_local;
13774543Smarks 		altree = &allownode->z_descend;
13784543Smarks 	} else {
13794543Smarks 		tree = &allownode->z_descend;
13804543Smarks 		altree = &allownode->z_local;
13814543Smarks 	}
13824543Smarks 	permnode = avl_find(tree, &pnode, &where);
13834543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13844543Smarks 
13854543Smarks 	if (permnode2) {
13864543Smarks 		avl_remove(altree, permnode2);
13874543Smarks 		free(permnode2);
13884543Smarks 		if (permnode == NULL) {
13894543Smarks 			tree =  &allownode->z_localdescend;
13904543Smarks 		}
13914543Smarks 	}
13924543Smarks 
13934543Smarks 	/*
13944543Smarks 	 * Now insert new permission in either requested location
13954543Smarks 	 * local/descendent or into ld when perm will exist in both.
13964543Smarks 	 */
13974543Smarks 	if (permnode == NULL) {
13984543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13994543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
14004543Smarks 			return (-1);
14014543Smarks 		}
14024543Smarks 		*newnode = pnode;
14034543Smarks 		avl_add(tree, newnode);
14044543Smarks 	}
14054543Smarks 	return (0);
14064543Smarks }
14074577Sahrens 
14084543Smarks /*
14094543Smarks  * Uggh, this is going to be a bit complicated.
14104543Smarks  * we have an nvlist coming out of the kernel that
14114543Smarks  * will indicate where the permission is set and then
14124543Smarks  * it will contain allow of the various "who's", and what
14134543Smarks  * their permissions are.  To further complicate this
14144543Smarks  * we will then have to coalesce the local,descendent
14154543Smarks  * and local+descendent permissions where appropriate.
14164543Smarks  * The kernel only knows about a permission as being local
14174543Smarks  * or descendent, but not both.
14184543Smarks  *
14194543Smarks  * In order to make this easier for zfs_main to deal with
14204543Smarks  * a series of AVL trees will be used to maintain
14214543Smarks  * all of this, primarily for sorting purposes as well
14224543Smarks  * as the ability to quickly locate a specific entry.
14234543Smarks  *
14244543Smarks  * What we end up with are tree's for sets, create perms,
14254543Smarks  * user, groups and everyone.  With each of those trees
14264543Smarks  * we have subtrees for local, descendent and local+descendent
14274543Smarks  * permissions.
14284543Smarks  */
14294543Smarks int
14304543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
14314543Smarks {
14324543Smarks 	zfs_cmd_t zc = { 0 };
14334543Smarks 	int error;
14344543Smarks 	nvlist_t *nvlist;
14354543Smarks 	nvlist_t *permnv, *sourcenv;
14364543Smarks 	nvpair_t *who_pair, *source_pair;
14374543Smarks 	nvpair_t *perm_pair;
14384543Smarks 	char errbuf[1024];
14394543Smarks 	zfs_allow_t *zallowp, *newallowp;
14404543Smarks 	char  ld;
14414543Smarks 	char *nvpname;
14424543Smarks 	uid_t	uid;
14434543Smarks 	gid_t	gid;
14444543Smarks 	avl_tree_t *tree;
14454543Smarks 	avl_index_t where;
14464543Smarks 
14474543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14484543Smarks 
14494543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
14504543Smarks 		return (-1);
14514543Smarks 
14524543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14534543Smarks 		if (errno == ENOMEM) {
14544543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14554543Smarks 				zcmd_free_nvlists(&zc);
14564543Smarks 				return (-1);
14574543Smarks 			}
14584543Smarks 		} else if (errno == ENOTSUP) {
14594543Smarks 			zcmd_free_nvlists(&zc);
14604543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14614543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14624543Smarks 			return (zfs_error(zhp->zfs_hdl,
14634543Smarks 			    EZFS_BADVERSION, errbuf));
14644543Smarks 		} else {
14654543Smarks 			zcmd_free_nvlists(&zc);
14664543Smarks 			return (-1);
14674543Smarks 		}
14684543Smarks 	}
14694543Smarks 
14704543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14714543Smarks 		zcmd_free_nvlists(&zc);
14724543Smarks 		return (-1);
14734543Smarks 	}
14744543Smarks 
14754543Smarks 	zcmd_free_nvlists(&zc);
14764543Smarks 
14774543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14784543Smarks 
14794543Smarks 	if (source_pair == NULL) {
14804543Smarks 		*zfs_perms = NULL;
14814543Smarks 		return (0);
14824543Smarks 	}
14834543Smarks 
14844543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14854543Smarks 	if (*zfs_perms == NULL) {
14864543Smarks 		return (0);
14874543Smarks 	}
14884543Smarks 
14894543Smarks 	zallowp = *zfs_perms;
14904543Smarks 
14914543Smarks 	for (;;) {
14924543Smarks 		struct passwd *pwd;
14934543Smarks 		struct group *grp;
14944543Smarks 		zfs_allow_node_t *allownode;
14954543Smarks 		zfs_allow_node_t  findallownode;
14964543Smarks 		zfs_allow_node_t *newallownode;
14974543Smarks 
14984543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14994543Smarks 		    nvpair_name(source_pair),
15004543Smarks 		    sizeof (zallowp->z_setpoint));
15014543Smarks 
15024543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
15034543Smarks 			goto abort;
15044543Smarks 
15054543Smarks 		/*
15064543Smarks 		 * Make sure nvlist is composed correctly
15074543Smarks 		 */
15084543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
15094543Smarks 			goto abort;
15104543Smarks 		}
15114543Smarks 
15124543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
15134543Smarks 		if (who_pair == NULL) {
15144543Smarks 			goto abort;
15154543Smarks 		}
15164543Smarks 
15174543Smarks 		do {
15184543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
15194543Smarks 			if (error) {
15204543Smarks 				goto abort;
15214543Smarks 			}
15224543Smarks 
15234543Smarks 			/*
15244543Smarks 			 * First build up the key to use
15254543Smarks 			 * for looking up in the various
15264543Smarks 			 * who trees.
15274543Smarks 			 */
15284543Smarks 			ld = nvpair_name(who_pair)[1];
15294543Smarks 			nvpname = nvpair_name(who_pair);
15304543Smarks 			switch (nvpair_name(who_pair)[0]) {
15314543Smarks 			case ZFS_DELEG_USER:
15324543Smarks 			case ZFS_DELEG_USER_SETS:
15334543Smarks 				tree = &zallowp->z_user;
15344543Smarks 				uid = atol(&nvpname[3]);
15354543Smarks 				pwd = getpwuid(uid);
15364543Smarks 				(void) snprintf(findallownode.z_key,
15374543Smarks 				    sizeof (findallownode.z_key), "user %s",
15384543Smarks 				    (pwd) ? pwd->pw_name :
15394543Smarks 				    &nvpair_name(who_pair)[3]);
15404543Smarks 				break;
15414543Smarks 			case ZFS_DELEG_GROUP:
15424543Smarks 			case ZFS_DELEG_GROUP_SETS:
15434543Smarks 				tree = &zallowp->z_group;
15444543Smarks 				gid = atol(&nvpname[3]);
15454543Smarks 				grp = getgrgid(gid);
15464543Smarks 				(void) snprintf(findallownode.z_key,
15474543Smarks 				    sizeof (findallownode.z_key), "group %s",
15484543Smarks 				    (grp) ? grp->gr_name :
15494543Smarks 				    &nvpair_name(who_pair)[3]);
15504543Smarks 				break;
15514543Smarks 			case ZFS_DELEG_CREATE:
15524543Smarks 			case ZFS_DELEG_CREATE_SETS:
15534543Smarks 				tree = &zallowp->z_crperms;
15544543Smarks 				(void) strlcpy(findallownode.z_key, "",
15554543Smarks 				    sizeof (findallownode.z_key));
15564543Smarks 				break;
15574543Smarks 			case ZFS_DELEG_EVERYONE:
15584543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15594543Smarks 				(void) snprintf(findallownode.z_key,
15604543Smarks 				    sizeof (findallownode.z_key), "everyone");
15614543Smarks 				tree = &zallowp->z_everyone;
15624543Smarks 				break;
15634543Smarks 			case ZFS_DELEG_NAMED_SET:
15644543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15654543Smarks 				(void) snprintf(findallownode.z_key,
15664543Smarks 				    sizeof (findallownode.z_key), "%s",
15674543Smarks 				    &nvpair_name(who_pair)[3]);
15684543Smarks 				tree = &zallowp->z_sets;
15694543Smarks 				break;
15704543Smarks 			}
15714543Smarks 
15724543Smarks 			/*
15734543Smarks 			 * Place who in tree
15744543Smarks 			 */
15754543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15764543Smarks 			if (allownode == NULL) {
15774543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15784543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15794543Smarks 					goto abort;
15804543Smarks 				}
15814543Smarks 				avl_create(&newallownode->z_localdescend,
15824543Smarks 				    perm_compare,
15834543Smarks 				    sizeof (zfs_perm_node_t),
15844543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15854543Smarks 				avl_create(&newallownode->z_local,
15864543Smarks 				    perm_compare,
15874543Smarks 				    sizeof (zfs_perm_node_t),
15884543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15894543Smarks 				avl_create(&newallownode->z_descend,
15904543Smarks 				    perm_compare,
15914543Smarks 				    sizeof (zfs_perm_node_t),
15924543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15934543Smarks 				(void) strlcpy(newallownode->z_key,
15944543Smarks 				    findallownode.z_key,
15954543Smarks 				    sizeof (findallownode.z_key));
15964543Smarks 				avl_insert(tree, newallownode, where);
15974543Smarks 				allownode = newallownode;
15984543Smarks 			}
15994543Smarks 
16004543Smarks 			/*
16014543Smarks 			 * Now iterate over the permissions and
16024543Smarks 			 * place them in the appropriate local,
16034543Smarks 			 * descendent or local+descendent tree.
16044543Smarks 			 *
16054543Smarks 			 * The permissions are added to the tree
16064543Smarks 			 * via zfs_coalesce_perm().
16074543Smarks 			 */
16084543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
16094543Smarks 			if (perm_pair == NULL)
16104543Smarks 				goto abort;
16114543Smarks 			do {
16124543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
16134543Smarks 				    nvpair_name(perm_pair), ld) != 0)
16144543Smarks 					goto abort;
16154543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
16164543Smarks 			    perm_pair));
16174543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
16184543Smarks 
16194543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
16204543Smarks 		if (source_pair == NULL)
16214543Smarks 			break;
16224543Smarks 
16234543Smarks 		/*
16244543Smarks 		 * allocate another node from the link list of
16254543Smarks 		 * zfs_allow_t structures
16264543Smarks 		 */
16274543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
16284543Smarks 		    nvpair_name(source_pair));
16294543Smarks 		if (newallowp == NULL) {
16304543Smarks 			goto abort;
16314543Smarks 		}
16324543Smarks 		zallowp = newallowp;
16334543Smarks 	}
16344543Smarks 	nvlist_free(nvlist);
16354543Smarks 	return (0);
16364543Smarks abort:
16374543Smarks 	zfs_free_allows(*zfs_perms);
16384543Smarks 	nvlist_free(nvlist);
16394543Smarks 	return (-1);
16404543Smarks }
16414543Smarks 
16425993Smarks static char *
16435993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
16445993Smarks {
16455993Smarks 	/*
16465993Smarks 	 * Don't put newlines on end of lines
16475993Smarks 	 */
16485993Smarks 	switch (note) {
16495993Smarks 	case ZFS_DELEG_NOTE_CREATE:
16505993Smarks 		return (dgettext(TEXT_DOMAIN,
16515993Smarks 		    "Must also have the 'mount' ability"));
16525993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16535993Smarks 		return (dgettext(TEXT_DOMAIN,
16545993Smarks 		    "Must also have the 'mount' ability"));
16555993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16565993Smarks 		return (dgettext(TEXT_DOMAIN,
16575993Smarks 		    "Must also have the 'mount' ability"));
16585993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16595993Smarks 		return (dgettext(TEXT_DOMAIN,
16605993Smarks 		    "Must also have the 'mount' ability"));
16615993Smarks 	case ZFS_DELEG_NOTE_CLONE:
16625993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16635993Smarks 		    "ability and 'mount'\n"
16645993Smarks 		    "\t\t\t\tability in the origin file system"));
16655993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16665993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16675993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16685993Smarks 	case ZFS_DELEG_NOTE_RENAME:
16695993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16705993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16715993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16725993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16735993Smarks 		    " and 'create' ability"));
16745993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16755993Smarks 		return (dgettext(TEXT_DOMAIN,
16765993Smarks 		    "Allows changing any user property"));
16775993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16785993Smarks 		return (dgettext(TEXT_DOMAIN,
16795993Smarks 		    "Must also have the permission that is being\n"
16805993Smarks 		    "\t\t\t\tallowed"));
16815993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16825993Smarks 		return (dgettext(TEXT_DOMAIN,
16835993Smarks 		    "Allows mount/umount of ZFS datasets"));
16845993Smarks 	case ZFS_DELEG_NOTE_SHARE:
16855993Smarks 		return (dgettext(TEXT_DOMAIN,
16865993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16875993Smarks 		    "\t\t\t\tprotocols"));
16885993Smarks 	case ZFS_DELEG_NOTE_NONE:
16895993Smarks 	default:
16905993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16915993Smarks 	}
16925993Smarks }
16935993Smarks 
16945993Smarks typedef enum {
16955993Smarks 	ZFS_DELEG_SUBCOMMAND,
16965993Smarks 	ZFS_DELEG_PROP,
16975993Smarks 	ZFS_DELEG_OTHER
16985993Smarks } zfs_deleg_perm_type_t;
16995993Smarks 
17005993Smarks /*
17015993Smarks  * is the permission a subcommand or other?
17025993Smarks  */
17035993Smarks zfs_deleg_perm_type_t
17045993Smarks zfs_deleg_perm_type(const char *perm)
17055993Smarks {
17065993Smarks 	if (strcmp(perm, "userprop") == 0)
17075993Smarks 		return (ZFS_DELEG_OTHER);
17085993Smarks 	else
17095993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
17105993Smarks }
17115993Smarks 
17125993Smarks static char *
17135993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
17145993Smarks {
17155993Smarks 	switch (type) {
17165993Smarks 	case ZFS_DELEG_SUBCOMMAND:
17175993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
17185993Smarks 	case ZFS_DELEG_PROP:
17195993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
17205993Smarks 	case ZFS_DELEG_OTHER:
17215993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
17225993Smarks 	}
17235993Smarks 	return ("");
17245993Smarks }
17255993Smarks 
17265993Smarks /*ARGSUSED*/
17275993Smarks static int
17285993Smarks zfs_deleg_prop_cb(int prop, void *cb)
17295993Smarks {
17305993Smarks 	if (zfs_prop_delegatable(prop))
17315993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
17325993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
17335993Smarks 
17345993Smarks 	return (ZPROP_CONT);
17355993Smarks }
17365993Smarks 
17375993Smarks void
17385993Smarks zfs_deleg_permissions(void)
17395993Smarks {
17405993Smarks 	int i;
17415993Smarks 
17425993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
17435993Smarks 	    "TYPE", "NOTES");
17445993Smarks 
17455993Smarks 	/*
17465993Smarks 	 * First print out the subcommands
17475993Smarks 	 */
17485993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
17495993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17505993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17515993Smarks 		    zfs_deleg_perm_type_str(
17525993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17535993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17545993Smarks 	}
17555993Smarks 
17565993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17575993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17585993Smarks }
17595993Smarks 
1760789Sahrens /*
1761789Sahrens  * Given a property name and value, set the property for the given dataset.
1762789Sahrens  */
1763789Sahrens int
17642676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1765789Sahrens {
1766789Sahrens 	zfs_cmd_t zc = { 0 };
17672676Seschrock 	int ret = -1;
17682676Seschrock 	prop_changelist_t *cl = NULL;
17692082Seschrock 	char errbuf[1024];
17702082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17712676Seschrock 	nvlist_t *nvl = NULL, *realprops;
17722676Seschrock 	zfs_prop_t prop;
17736168Shs24103 	int do_prefix = 1;
17742082Seschrock 
17752082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
17762676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
17772082Seschrock 	    zhp->zfs_name);
17782082Seschrock 
17792676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
17802676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
17812676Seschrock 		(void) no_memory(hdl);
17822676Seschrock 		goto error;
1783789Sahrens 	}
1784789Sahrens 
17855094Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
17862676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
17872676Seschrock 		goto error;
17885094Slling 
17892676Seschrock 	nvlist_free(nvl);
17902676Seschrock 	nvl = realprops;
17912676Seschrock 
17922676Seschrock 	prop = zfs_name_to_prop(propname);
17932676Seschrock 
1794789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
17952676Seschrock 		goto error;
1796789Sahrens 
1797789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17982082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17992082Seschrock 		    "child dataset with inherited mountpoint is used "
18002082Seschrock 		    "in a non-global zone"));
18012082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1802789Sahrens 		goto error;
1803789Sahrens 	}
1804789Sahrens 
18056168Shs24103 
18066168Shs24103 	/* do not unmount dataset if canmount is being set to noauto */
18076168Shs24103 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
18086168Shs24103 		do_prefix = 0;
18096168Shs24103 
18106168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
18116168Shs24103 			goto error;
1812789Sahrens 
1813789Sahrens 	/*
1814789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1815789Sahrens 	 */
1816789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1817789Sahrens 
18185094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
18192676Seschrock 		goto error;
18202676Seschrock 
18214543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1822789Sahrens 	if (ret != 0) {
1823789Sahrens 		switch (errno) {
1824789Sahrens 
1825789Sahrens 		case ENOSPC:
1826789Sahrens 			/*
1827789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1828789Sahrens 			 * something different; setting a quota or reservation
1829789Sahrens 			 * doesn't use any disk space.
1830789Sahrens 			 */
1831789Sahrens 			switch (prop) {
1832789Sahrens 			case ZFS_PROP_QUOTA:
18335378Sck153898 			case ZFS_PROP_REFQUOTA:
18342082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18352082Seschrock 				    "size is less than current used or "
18362082Seschrock 				    "reserved space"));
18372082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1838789Sahrens 				break;
1839789Sahrens 
1840789Sahrens 			case ZFS_PROP_RESERVATION:
18415378Sck153898 			case ZFS_PROP_REFRESERVATION:
18422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18432082Seschrock 				    "size is greater than available space"));
18442082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1845789Sahrens 				break;
1846789Sahrens 
1847789Sahrens 			default:
18482082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1849789Sahrens 				break;
1850789Sahrens 			}
1851789Sahrens 			break;
1852789Sahrens 
1853789Sahrens 		case EBUSY:
18542082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
18552082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
18562082Seschrock 			else
18572676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1858789Sahrens 			break;
1859789Sahrens 
18601175Slling 		case EROFS:
18612082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18621175Slling 			break;
18631175Slling 
18643886Sahl 		case ENOTSUP:
18653886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18665977Smarks 			    "pool and or dataset must be upgraded to set this "
18674603Sahrens 			    "property or value"));
18683886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
18693886Sahl 			break;
18703886Sahl 
1871789Sahrens 		case EOVERFLOW:
1872789Sahrens 			/*
1873789Sahrens 			 * This platform can't address a volume this big.
1874789Sahrens 			 */
1875789Sahrens #ifdef _ILP32
1876789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
18772082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1878789Sahrens 				break;
1879789Sahrens 			}
1880789Sahrens #endif
18812082Seschrock 			/* FALLTHROUGH */
1882789Sahrens 		default:
18832082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1884789Sahrens 		}
1885789Sahrens 	} else {
18866168Shs24103 		if (do_prefix)
18876168Shs24103 			ret = changelist_postfix(cl);
18886168Shs24103 
1889789Sahrens 		/*
1890789Sahrens 		 * Refresh the statistics so the new property value
1891789Sahrens 		 * is reflected.
1892789Sahrens 		 */
18936168Shs24103 		if (ret == 0)
18942676Seschrock 			(void) get_stats(zhp);
1895789Sahrens 	}
1896789Sahrens 
1897789Sahrens error:
18982676Seschrock 	nvlist_free(nvl);
18992676Seschrock 	zcmd_free_nvlists(&zc);
19002676Seschrock 	if (cl)
19012676Seschrock 		changelist_free(cl);
1902789Sahrens 	return (ret);
1903789Sahrens }
1904789Sahrens 
1905789Sahrens /*
1906789Sahrens  * Given a property, inherit the value from the parent dataset.
1907789Sahrens  */
1908789Sahrens int
19092676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1910789Sahrens {
1911789Sahrens 	zfs_cmd_t zc = { 0 };
1912789Sahrens 	int ret;
1913789Sahrens 	prop_changelist_t *cl;
19142082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
19152082Seschrock 	char errbuf[1024];
19162676Seschrock 	zfs_prop_t prop;
19172082Seschrock 
19182082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
19192082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1920789Sahrens 
19215094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
19222676Seschrock 		/*
19232676Seschrock 		 * For user properties, the amount of work we have to do is very
19242676Seschrock 		 * small, so just do it here.
19252676Seschrock 		 */
19262676Seschrock 		if (!zfs_prop_user(propname)) {
19272676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19282676Seschrock 			    "invalid property"));
19292676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
19302676Seschrock 		}
19312676Seschrock 
19322676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19332676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
19342676Seschrock 
19354849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
19362676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
19372676Seschrock 
19382676Seschrock 		return (0);
19392676Seschrock 	}
19402676Seschrock 
1941789Sahrens 	/*
1942789Sahrens 	 * Verify that this property is inheritable.
1943789Sahrens 	 */
19442082Seschrock 	if (zfs_prop_readonly(prop))
19452082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
19462082Seschrock 
19472082Seschrock 	if (!zfs_prop_inheritable(prop))
19482082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1949789Sahrens 
1950789Sahrens 	/*
1951789Sahrens 	 * Check to see if the value applies to this type
1952789Sahrens 	 */
19532082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
19542082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1955789Sahrens 
19563443Srm160521 	/*
19573443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
19583443Srm160521 	 */
19593443Srm160521 	propname = zfs_prop_to_name(prop);
1960789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19612676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1962789Sahrens 
1963789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1964789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
19652082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19662082Seschrock 		    "dataset is used in a non-global zone"));
19672082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1968789Sahrens 	}
1969789Sahrens 
1970789Sahrens 	/*
1971789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1972789Sahrens 	 */
1973789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1974789Sahrens 		return (-1);
1975789Sahrens 
1976789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19772082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19782082Seschrock 		    "child dataset with inherited mountpoint is used "
19792082Seschrock 		    "in a non-global zone"));
19802082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1981789Sahrens 		goto error;
1982789Sahrens 	}
1983789Sahrens 
1984789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1985789Sahrens 		goto error;
1986789Sahrens 
19874849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
19882082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1989789Sahrens 	} else {
1990789Sahrens 
19912169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1992789Sahrens 			goto error;
1993789Sahrens 
1994789Sahrens 		/*
1995789Sahrens 		 * Refresh the statistics so the new property is reflected.
1996789Sahrens 		 */
1997789Sahrens 		(void) get_stats(zhp);
1998789Sahrens 	}
1999789Sahrens 
2000789Sahrens error:
2001789Sahrens 	changelist_free(cl);
2002789Sahrens 	return (ret);
2003789Sahrens }
2004789Sahrens 
2005789Sahrens /*
20061356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
20071356Seschrock  * extract them appropriately.
20081356Seschrock  */
20091356Seschrock static uint64_t
20101356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20111356Seschrock {
20121356Seschrock 	nvlist_t *nv;
20131356Seschrock 	uint64_t value;
20141356Seschrock 
20152885Sahrens 	*source = NULL;
20161356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20171356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20185094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
20195094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20201356Seschrock 	} else {
20211356Seschrock 		value = zfs_prop_default_numeric(prop);
20221356Seschrock 		*source = "";
20231356Seschrock 	}
20241356Seschrock 
20251356Seschrock 	return (value);
20261356Seschrock }
20271356Seschrock 
20281356Seschrock static char *
20291356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20301356Seschrock {
20311356Seschrock 	nvlist_t *nv;
20321356Seschrock 	char *value;
20331356Seschrock 
20342885Sahrens 	*source = NULL;
20351356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20361356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20375094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
20385094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20391356Seschrock 	} else {
20401356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
20411356Seschrock 			value = "";
20421356Seschrock 		*source = "";
20431356Seschrock 	}
20441356Seschrock 
20451356Seschrock 	return (value);
20461356Seschrock }
20471356Seschrock 
20481356Seschrock /*
2049789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2050789Sahrens  * zfs_prop_get_int() are built using this interface.
2051789Sahrens  *
2052789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2053789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2054789Sahrens  * If they differ from the on-disk values, report the current values and mark
2055789Sahrens  * the source "temporary".
2056789Sahrens  */
20572082Seschrock static int
20585094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
20592082Seschrock     char **source, uint64_t *val)
2060789Sahrens {
20615147Srm160521 	zfs_cmd_t zc = { 0 };
20625592Stimh 	nvlist_t *zplprops = NULL;
2063789Sahrens 	struct mnttab mnt;
20643265Sahrens 	char *mntopt_on = NULL;
20653265Sahrens 	char *mntopt_off = NULL;
2066789Sahrens 
2067789Sahrens 	*source = NULL;
2068789Sahrens 
20693265Sahrens 	switch (prop) {
20703265Sahrens 	case ZFS_PROP_ATIME:
20713265Sahrens 		mntopt_on = MNTOPT_ATIME;
20723265Sahrens 		mntopt_off = MNTOPT_NOATIME;
20733265Sahrens 		break;
20743265Sahrens 
20753265Sahrens 	case ZFS_PROP_DEVICES:
20763265Sahrens 		mntopt_on = MNTOPT_DEVICES;
20773265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
20783265Sahrens 		break;
20793265Sahrens 
20803265Sahrens 	case ZFS_PROP_EXEC:
20813265Sahrens 		mntopt_on = MNTOPT_EXEC;
20823265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
20833265Sahrens 		break;
20843265Sahrens 
20853265Sahrens 	case ZFS_PROP_READONLY:
20863265Sahrens 		mntopt_on = MNTOPT_RO;
20873265Sahrens 		mntopt_off = MNTOPT_RW;
20883265Sahrens 		break;
20893265Sahrens 
20903265Sahrens 	case ZFS_PROP_SETUID:
20913265Sahrens 		mntopt_on = MNTOPT_SETUID;
20923265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
20933265Sahrens 		break;
20943265Sahrens 
20953265Sahrens 	case ZFS_PROP_XATTR:
20963265Sahrens 		mntopt_on = MNTOPT_XATTR;
20973265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
20983265Sahrens 		break;
20995331Samw 
21005331Samw 	case ZFS_PROP_NBMAND:
21015331Samw 		mntopt_on = MNTOPT_NBMAND;
21025331Samw 		mntopt_off = MNTOPT_NONBMAND;
21035331Samw 		break;
21043265Sahrens 	}
21053265Sahrens 
21062474Seschrock 	/*
21072474Seschrock 	 * Because looking up the mount options is potentially expensive
21082474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
21092474Seschrock 	 * we're looking up a property which requires its presence.
21102474Seschrock 	 */
21112474Seschrock 	if (!zhp->zfs_mntcheck &&
21123265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
21133265Sahrens 		struct mnttab entry, search = { 0 };
21143265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
21152474Seschrock 
21162474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
21172474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
21183265Sahrens 		rewind(mnttab);
21193265Sahrens 
21203265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
21213265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
21223265Sahrens 			    entry.mnt_mntopts);
21233265Sahrens 			if (zhp->zfs_mntopts == NULL)
21243265Sahrens 				return (-1);
21253265Sahrens 		}
21262474Seschrock 
21272474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
21282474Seschrock 	}
21292474Seschrock 
2130789Sahrens 	if (zhp->zfs_mntopts == NULL)
2131789Sahrens 		mnt.mnt_mntopts = "";
2132789Sahrens 	else
2133789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2134789Sahrens 
2135789Sahrens 	switch (prop) {
2136789Sahrens 	case ZFS_PROP_ATIME:
21373265Sahrens 	case ZFS_PROP_DEVICES:
21383265Sahrens 	case ZFS_PROP_EXEC:
21393265Sahrens 	case ZFS_PROP_READONLY:
21403265Sahrens 	case ZFS_PROP_SETUID:
21413265Sahrens 	case ZFS_PROP_XATTR:
21425331Samw 	case ZFS_PROP_NBMAND:
21432082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21442082Seschrock 
21453265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
21462082Seschrock 			*val = B_TRUE;
2147789Sahrens 			if (src)
21485094Slling 				*src = ZPROP_SRC_TEMPORARY;
21493265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
21502082Seschrock 			*val = B_FALSE;
2151789Sahrens 			if (src)
21525094Slling 				*src = ZPROP_SRC_TEMPORARY;
2153789Sahrens 		}
21542082Seschrock 		break;
2155789Sahrens 
21563265Sahrens 	case ZFS_PROP_CANMOUNT:
21572082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21586168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
21593417Srm160521 			*source = zhp->zfs_name;
21603417Srm160521 		else
21613417Srm160521 			*source = "";	/* default */
21622082Seschrock 		break;
2163789Sahrens 
2164789Sahrens 	case ZFS_PROP_QUOTA:
21655378Sck153898 	case ZFS_PROP_REFQUOTA:
2166789Sahrens 	case ZFS_PROP_RESERVATION:
21675378Sck153898 	case ZFS_PROP_REFRESERVATION:
21682885Sahrens 		*val = getprop_uint64(zhp, prop, source);
21692885Sahrens 		if (*val == 0)
2170789Sahrens 			*source = "";	/* default */
2171789Sahrens 		else
2172789Sahrens 			*source = zhp->zfs_name;
21732082Seschrock 		break;
2174789Sahrens 
2175789Sahrens 	case ZFS_PROP_MOUNTED:
21762082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
21772082Seschrock 		break;
2178789Sahrens 
21793377Seschrock 	case ZFS_PROP_NUMCLONES:
21803377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
21813377Seschrock 		break;
21823377Seschrock 
21835147Srm160521 	case ZFS_PROP_VERSION:
21845498Stimh 	case ZFS_PROP_NORMALIZE:
21855498Stimh 	case ZFS_PROP_UTF8ONLY:
21865498Stimh 	case ZFS_PROP_CASE:
21875498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
21885498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
21895473Srm160521 			return (-1);
21905147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21915498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
21925498Stimh 			zcmd_free_nvlists(&zc);
21935147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21945498Stimh 			    "unable to get %s property"),
21955498Stimh 			    zfs_prop_to_name(prop));
21965147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
21975147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
21985147Srm160521 		}
21995498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
22005498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
22015498Stimh 		    val) != 0) {
22025498Stimh 			zcmd_free_nvlists(&zc);
22035498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22045498Stimh 			    "unable to get %s property"),
22055498Stimh 			    zfs_prop_to_name(prop));
22065498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
22075498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
22085498Stimh 		}
22095592Stimh 		if (zplprops)
22105592Stimh 			nvlist_free(zplprops);
22115498Stimh 		zcmd_free_nvlists(&zc);
22125147Srm160521 		break;
22135147Srm160521 
2214789Sahrens 	default:
22154577Sahrens 		switch (zfs_prop_get_type(prop)) {
22164787Sahrens 		case PROP_TYPE_NUMBER:
22174787Sahrens 		case PROP_TYPE_INDEX:
22184577Sahrens 			*val = getprop_uint64(zhp, prop, source);
22194577Sahrens 			break;
22204577Sahrens 
22214787Sahrens 		case PROP_TYPE_STRING:
22224577Sahrens 		default:
22234577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22244577Sahrens 			    "cannot get non-numeric property"));
22254577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
22264577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
22274577Sahrens 		}
2228789Sahrens 	}
2229789Sahrens 
2230789Sahrens 	return (0);
2231789Sahrens }
2232789Sahrens 
2233789Sahrens /*
2234789Sahrens  * Calculate the source type, given the raw source string.
2235789Sahrens  */
2236789Sahrens static void
22375094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2238789Sahrens     char *statbuf, size_t statlen)
2239789Sahrens {
22405094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2241789Sahrens 		return;
2242789Sahrens 
2243789Sahrens 	if (source == NULL) {
22445094Slling 		*srctype = ZPROP_SRC_NONE;
2245789Sahrens 	} else if (source[0] == '\0') {
22465094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2247789Sahrens 	} else {
2248789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
22495094Slling 			*srctype = ZPROP_SRC_LOCAL;
2250789Sahrens 		} else {
2251789Sahrens 			(void) strlcpy(statbuf, source, statlen);
22525094Slling 			*srctype = ZPROP_SRC_INHERITED;
2253789Sahrens 		}
2254789Sahrens 	}
2255789Sahrens 
2256789Sahrens }
2257789Sahrens 
2258789Sahrens /*
2259789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2260789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2261789Sahrens  * human-readable form.
2262789Sahrens  *
2263789Sahrens  * Returns 0 on success, or -1 on error.
2264789Sahrens  */
2265789Sahrens int
2266789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
22675094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2268789Sahrens {
2269789Sahrens 	char *source = NULL;
2270789Sahrens 	uint64_t val;
2271789Sahrens 	char *str;
22722676Seschrock 	const char *strval;
2273789Sahrens 
2274789Sahrens 	/*
2275789Sahrens 	 * Check to see if this property applies to our object
2276789Sahrens 	 */
2277789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2278789Sahrens 		return (-1);
2279789Sahrens 
2280789Sahrens 	if (src)
22815094Slling 		*src = ZPROP_SRC_NONE;
2282789Sahrens 
2283789Sahrens 	switch (prop) {
2284789Sahrens 	case ZFS_PROP_CREATION:
2285789Sahrens 		/*
2286789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2287789Sahrens 		 * this into a string unless 'literal' is specified.
2288789Sahrens 		 */
2289789Sahrens 		{
22902885Sahrens 			val = getprop_uint64(zhp, prop, &source);
22912885Sahrens 			time_t time = (time_t)val;
2292789Sahrens 			struct tm t;
2293789Sahrens 
2294789Sahrens 			if (literal ||
2295789Sahrens 			    localtime_r(&time, &t) == NULL ||
2296789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2297789Sahrens 			    &t) == 0)
22982885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2299789Sahrens 		}
2300789Sahrens 		break;
2301789Sahrens 
2302789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2303789Sahrens 		/*
2304789Sahrens 		 * Getting the precise mountpoint can be tricky.
2305789Sahrens 		 *
2306789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2307789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2308789Sahrens 		 *    after our ancestor and append it to the inherited value.
2309789Sahrens 		 *
2310789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2311789Sahrens 		 * root to any values we return.
2312789Sahrens 		 */
2313*6865Srm160521 
23141356Seschrock 		str = getprop_string(zhp, prop, &source);
23151356Seschrock 
23166612Sgw25295 		if (str[0] == '/') {
2317*6865Srm160521 			char buf[MAXPATHLEN];
2318*6865Srm160521 			char *root = buf;
23191356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2320789Sahrens 
2321789Sahrens 			if (relpath[0] == '/')
2322789Sahrens 				relpath++;
23236612Sgw25295 
2324*6865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
2325*6865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2326*6865Srm160521 			    (strcmp(root, "-") == 0))
2327*6865Srm160521 				root[0] = '\0';
23286612Sgw25295 			/*
23296612Sgw25295 			 * Special case an alternate root of '/'. This will
23306612Sgw25295 			 * avoid having multiple leading slashes in the
23316612Sgw25295 			 * mountpoint path.
23326612Sgw25295 			 */
23336612Sgw25295 			if (strcmp(root, "/") == 0)
23346612Sgw25295 				root++;
23356612Sgw25295 
23366612Sgw25295 			/*
23376612Sgw25295 			 * If the mountpoint is '/' then skip over this
23386612Sgw25295 			 * if we are obtaining either an alternate root or
23396612Sgw25295 			 * an inherited mountpoint.
23406612Sgw25295 			 */
23416612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
23426612Sgw25295 			    relpath[0] != '\0'))
23431356Seschrock 				str++;
2344789Sahrens 
2345789Sahrens 			if (relpath[0] == '\0')
2346789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
23471356Seschrock 				    root, str);
2348789Sahrens 			else
2349789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
23501356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2351789Sahrens 				    relpath);
2352789Sahrens 		} else {
2353789Sahrens 			/* 'legacy' or 'none' */
23541356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2355789Sahrens 		}
2356789Sahrens 
2357789Sahrens 		break;
2358789Sahrens 
2359789Sahrens 	case ZFS_PROP_ORIGIN:
23602885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2361789Sahrens 		    proplen);
2362789Sahrens 		/*
2363789Sahrens 		 * If there is no parent at all, return failure to indicate that
2364789Sahrens 		 * it doesn't apply to this dataset.
2365789Sahrens 		 */
2366789Sahrens 		if (propbuf[0] == '\0')
2367789Sahrens 			return (-1);
2368789Sahrens 		break;
2369789Sahrens 
2370789Sahrens 	case ZFS_PROP_QUOTA:
23715378Sck153898 	case ZFS_PROP_REFQUOTA:
2372789Sahrens 	case ZFS_PROP_RESERVATION:
23735378Sck153898 	case ZFS_PROP_REFRESERVATION:
23745378Sck153898 
23752082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23762082Seschrock 			return (-1);
2377789Sahrens 
2378789Sahrens 		/*
2379789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2380789Sahrens 		 * (unless literal is set), and indicate that it's the default
2381789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2382789Sahrens 		 * that its set locally.
2383789Sahrens 		 */
2384789Sahrens 		if (val == 0) {
2385789Sahrens 			if (literal)
2386789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2387789Sahrens 			else
2388789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2389789Sahrens 		} else {
2390789Sahrens 			if (literal)
23912856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
23923912Slling 				    (u_longlong_t)val);
2393789Sahrens 			else
2394789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2395789Sahrens 		}
2396789Sahrens 		break;
2397789Sahrens 
2398789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
23992082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24002082Seschrock 			return (-1);
24012856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
24022856Snd150628 		    val / 100, (longlong_t)val % 100);
2403789Sahrens 		break;
2404789Sahrens 
2405789Sahrens 	case ZFS_PROP_TYPE:
2406789Sahrens 		switch (zhp->zfs_type) {
2407789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2408789Sahrens 			str = "filesystem";
2409789Sahrens 			break;
2410789Sahrens 		case ZFS_TYPE_VOLUME:
2411789Sahrens 			str = "volume";
2412789Sahrens 			break;
2413789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2414789Sahrens 			str = "snapshot";
2415789Sahrens 			break;
2416789Sahrens 		default:
24172082Seschrock 			abort();
2418789Sahrens 		}
2419789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2420789Sahrens 		break;
2421789Sahrens 
2422789Sahrens 	case ZFS_PROP_MOUNTED:
2423789Sahrens 		/*
2424789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2425789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2426789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2427789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2428789Sahrens 		 */
24292082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
24302082Seschrock 		    src, &source, &val) != 0)
24312082Seschrock 			return (-1);
24322082Seschrock 		if (val)
2433789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2434789Sahrens 		else
2435789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2436789Sahrens 		break;
2437789Sahrens 
2438789Sahrens 	case ZFS_PROP_NAME:
2439789Sahrens 		/*
2440789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2441789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2442789Sahrens 		 * consumers.
2443789Sahrens 		 */
2444789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2445789Sahrens 		break;
2446789Sahrens 
2447789Sahrens 	default:
24484577Sahrens 		switch (zfs_prop_get_type(prop)) {
24494787Sahrens 		case PROP_TYPE_NUMBER:
24504577Sahrens 			if (get_numeric_property(zhp, prop, src,
24514577Sahrens 			    &source, &val) != 0)
24524577Sahrens 				return (-1);
24534577Sahrens 			if (literal)
24544577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
24554577Sahrens 				    (u_longlong_t)val);
24564577Sahrens 			else
24574577Sahrens 				zfs_nicenum(val, propbuf, proplen);
24584577Sahrens 			break;
24594577Sahrens 
24604787Sahrens 		case PROP_TYPE_STRING:
24614577Sahrens 			(void) strlcpy(propbuf,
24624577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
24634577Sahrens 			break;
24644577Sahrens 
24654787Sahrens 		case PROP_TYPE_INDEX:
24664861Sahrens 			if (get_numeric_property(zhp, prop, src,
24674861Sahrens 			    &source, &val) != 0)
24684861Sahrens 				return (-1);
24694861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
24704577Sahrens 				return (-1);
24714577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
24724577Sahrens 			break;
24734577Sahrens 
24744577Sahrens 		default:
24754577Sahrens 			abort();
24764577Sahrens 		}
2477789Sahrens 	}
2478789Sahrens 
2479789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2480789Sahrens 
2481789Sahrens 	return (0);
2482789Sahrens }
2483789Sahrens 
2484789Sahrens /*
2485789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2486789Sahrens  * the given property is the appropriate type; should only be used with
2487789Sahrens  * hard-coded property types.
2488789Sahrens  */
2489789Sahrens uint64_t
2490789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2491789Sahrens {
2492789Sahrens 	char *source;
24932082Seschrock 	uint64_t val;
24942082Seschrock 
24955367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
24962082Seschrock 
24972082Seschrock 	return (val);
2498789Sahrens }
2499789Sahrens 
25005713Srm160521 int
25015713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
25025713Srm160521 {
25035713Srm160521 	char buf[64];
25045713Srm160521 
25055713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
25065713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
25075713Srm160521 }
25085713Srm160521 
2509789Sahrens /*
2510789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2511789Sahrens  */
2512789Sahrens int
2513789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
25145094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2515789Sahrens {
2516789Sahrens 	char *source;
2517789Sahrens 
2518789Sahrens 	/*
2519789Sahrens 	 * Check to see if this property applies to our object
2520789Sahrens 	 */
25214849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
25223237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
25232082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
25242082Seschrock 		    zfs_prop_to_name(prop)));
25254849Sahrens 	}
2526789Sahrens 
2527789Sahrens 	if (src)
25285094Slling 		*src = ZPROP_SRC_NONE;
2529789Sahrens 
25302082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
25312082Seschrock 		return (-1);
2532789Sahrens 
2533789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2534789Sahrens 
2535789Sahrens 	return (0);
2536789Sahrens }
2537789Sahrens 
2538789Sahrens /*
2539789Sahrens  * Returns the name of the given zfs handle.
2540789Sahrens  */
2541789Sahrens const char *
2542789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2543789Sahrens {
2544789Sahrens 	return (zhp->zfs_name);
2545789Sahrens }
2546789Sahrens 
2547789Sahrens /*
2548789Sahrens  * Returns the type of the given zfs handle.
2549789Sahrens  */
2550789Sahrens zfs_type_t
2551789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2552789Sahrens {
2553789Sahrens 	return (zhp->zfs_type);
2554789Sahrens }
2555789Sahrens 
2556789Sahrens /*
25571356Seschrock  * Iterate over all child filesystems
2558789Sahrens  */
2559789Sahrens int
25601356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2561789Sahrens {
2562789Sahrens 	zfs_cmd_t zc = { 0 };
2563789Sahrens 	zfs_handle_t *nzhp;
2564789Sahrens 	int ret;
2565789Sahrens 
25665367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25675367Sahrens 		return (0);
25685367Sahrens 
2569789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25702082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2571789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2572789Sahrens 		/*
2573789Sahrens 		 * Ignore private dataset names.
2574789Sahrens 		 */
2575789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2576789Sahrens 			continue;
2577789Sahrens 
2578789Sahrens 		/*
2579789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2580789Sahrens 		 * that the pool has since been removed.
2581789Sahrens 		 */
25822082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25832082Seschrock 		    zc.zc_name)) == NULL)
2584789Sahrens 			continue;
2585789Sahrens 
2586789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2587789Sahrens 			return (ret);
2588789Sahrens 	}
2589789Sahrens 
2590789Sahrens 	/*
2591789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2592789Sahrens 	 * returned, then the underlying dataset has been removed since we
2593789Sahrens 	 * obtained the handle.
2594789Sahrens 	 */
2595789Sahrens 	if (errno != ESRCH && errno != ENOENT)
25962082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
25972082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2598789Sahrens 
25991356Seschrock 	return (0);
26001356Seschrock }
26011356Seschrock 
26021356Seschrock /*
26031356Seschrock  * Iterate over all snapshots
26041356Seschrock  */
26051356Seschrock int
26061356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26071356Seschrock {
26081356Seschrock 	zfs_cmd_t zc = { 0 };
26091356Seschrock 	zfs_handle_t *nzhp;
26101356Seschrock 	int ret;
2611789Sahrens 
26125367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
26135367Sahrens 		return (0);
26145367Sahrens 
2615789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26162082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
26172082Seschrock 	    &zc) == 0;
2618789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2619789Sahrens 
26202082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26212082Seschrock 		    zc.zc_name)) == NULL)
2622789Sahrens 			continue;
2623789Sahrens 
2624789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2625789Sahrens 			return (ret);
2626789Sahrens 	}
2627789Sahrens 
2628789Sahrens 	/*
2629789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2630789Sahrens 	 * returned, then the underlying dataset has been removed since we
2631789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2632789Sahrens 	 */
2633789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26342082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26352082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2636789Sahrens 
2637789Sahrens 	return (0);
2638789Sahrens }
2639789Sahrens 
2640789Sahrens /*
26411356Seschrock  * Iterate over all children, snapshots and filesystems
26421356Seschrock  */
26431356Seschrock int
26441356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26451356Seschrock {
26461356Seschrock 	int ret;
26471356Seschrock 
26481356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
26491356Seschrock 		return (ret);
26501356Seschrock 
26511356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
26521356Seschrock }
26531356Seschrock 
26541356Seschrock /*
2655789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2656789Sahrens  * Can return NULL if this is a pool.
2657789Sahrens  */
2658789Sahrens static int
2659789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2660789Sahrens {
2661789Sahrens 	char *loc;
2662789Sahrens 
2663789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2664789Sahrens 		return (-1);
2665789Sahrens 
2666789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2667789Sahrens 	buf[loc - path] = '\0';
2668789Sahrens 
2669789Sahrens 	return (0);
2670789Sahrens }
2671789Sahrens 
2672789Sahrens /*
26734490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
26744490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26754490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
26764490Svb160487  * length of already existing prefix of the given path.  We also fetch the
26774490Svb160487  * 'zoned' property, which is used to validate property settings when creating
26784490Svb160487  * new datasets.
2679789Sahrens  */
2680789Sahrens static int
26814490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
26824490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2683789Sahrens {
2684789Sahrens 	zfs_cmd_t zc = { 0 };
2685789Sahrens 	char parent[ZFS_MAXNAMELEN];
2686789Sahrens 	char *slash;
26871356Seschrock 	zfs_handle_t *zhp;
26882082Seschrock 	char errbuf[1024];
26892082Seschrock 
26902082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
26912082Seschrock 	    path);
2692789Sahrens 
2693789Sahrens 	/* get parent, and check to see if this is just a pool */
2694789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
26952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26962082Seschrock 		    "missing dataset name"));
26972082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2698789Sahrens 	}
2699789Sahrens 
2700789Sahrens 	/* check to see if the pool exists */
2701789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2702789Sahrens 		slash = parent + strlen(parent);
2703789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2704789Sahrens 	zc.zc_name[slash - parent] = '\0';
27052082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2706789Sahrens 	    errno == ENOENT) {
27072082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27082082Seschrock 		    "no such pool '%s'"), zc.zc_name);
27092082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2710789Sahrens 	}
2711789Sahrens 
2712789Sahrens 	/* check to see if the parent dataset exists */
27134490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27144490Svb160487 		if (errno == ENOENT && accept_ancestor) {
27154490Svb160487 			/*
27164490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
27174490Svb160487 			 */
27184490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27194490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27204490Svb160487 				    "no such pool '%s'"), zc.zc_name);
27214490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27224490Svb160487 			}
27234490Svb160487 		} else if (errno == ENOENT) {
27242082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27252082Seschrock 			    "parent does not exist"));
27262082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27274490Svb160487 		} else
27282082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2729789Sahrens 	}
2730789Sahrens 
27312676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2732789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
27332676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
27342082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27351356Seschrock 		zfs_close(zhp);
2736789Sahrens 		return (-1);
2737789Sahrens 	}
2738789Sahrens 
2739789Sahrens 	/* make sure parent is a filesystem */
27401356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
27412082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27422082Seschrock 		    "parent is not a filesystem"));
27432082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27441356Seschrock 		zfs_close(zhp);
2745789Sahrens 		return (-1);
2746789Sahrens 	}
2747789Sahrens 
27481356Seschrock 	zfs_close(zhp);
27494490Svb160487 	if (prefixlen != NULL)
27504490Svb160487 		*prefixlen = strlen(parent);
27514490Svb160487 	return (0);
27524490Svb160487 }
27534490Svb160487 
27544490Svb160487 /*
27554490Svb160487  * Finds whether the dataset of the given type(s) exists.
27564490Svb160487  */
27574490Svb160487 boolean_t
27584490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
27594490Svb160487 {
27604490Svb160487 	zfs_handle_t *zhp;
27614490Svb160487 
27625326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
27634490Svb160487 		return (B_FALSE);
27644490Svb160487 
27654490Svb160487 	/*
27664490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27674490Svb160487 	 */
27684490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27694490Svb160487 		int ds_type = zhp->zfs_type;
27704490Svb160487 
27714490Svb160487 		zfs_close(zhp);
27724490Svb160487 		if (types & ds_type)
27734490Svb160487 			return (B_TRUE);
27744490Svb160487 	}
27754490Svb160487 	return (B_FALSE);
27764490Svb160487 }
27774490Svb160487 
27784490Svb160487 /*
27795367Sahrens  * Given a path to 'target', create all the ancestors between
27805367Sahrens  * the prefixlen portion of the path, and the target itself.
27815367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
27825367Sahrens  */
27835367Sahrens int
27845367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
27855367Sahrens {
27865367Sahrens 	zfs_handle_t *h;
27875367Sahrens 	char *cp;
27885367Sahrens 	const char *opname;
27895367Sahrens 
27905367Sahrens 	/* make sure prefix exists */
27915367Sahrens 	cp = target + prefixlen;
27925367Sahrens 	if (*cp != '/') {
27935367Sahrens 		assert(strchr(cp, '/') == NULL);
27945367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27955367Sahrens 	} else {
27965367Sahrens 		*cp = '\0';
27975367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27985367Sahrens 		*cp = '/';
27995367Sahrens 	}
28005367Sahrens 	if (h == NULL)
28015367Sahrens 		return (-1);
28025367Sahrens 	zfs_close(h);
28035367Sahrens 
28045367Sahrens 	/*
28055367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28065367Sahrens 	 * up to the prefixlen-long one.
28075367Sahrens 	 */
28085367Sahrens 	for (cp = target + prefixlen + 1;
28095367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28105367Sahrens 		char *logstr;
28115367Sahrens 
28125367Sahrens 		*cp = '\0';
28135367Sahrens 
28145367Sahrens 		h = make_dataset_handle(hdl, target);
28155367Sahrens 		if (h) {
28165367Sahrens 			/* it already exists, nothing to do here */
28175367Sahrens 			zfs_close(h);
28185367Sahrens 			continue;
28195367Sahrens 		}
28205367Sahrens 
28215367Sahrens 		logstr = hdl->libzfs_log_str;
28225367Sahrens 		hdl->libzfs_log_str = NULL;
28235367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28245367Sahrens 		    NULL) != 0) {
28255367Sahrens 			hdl->libzfs_log_str = logstr;
28265367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28275367Sahrens 			goto ancestorerr;
28285367Sahrens 		}
28295367Sahrens 
28305367Sahrens 		hdl->libzfs_log_str = logstr;
28315367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28325367Sahrens 		if (h == NULL) {
28335367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28345367Sahrens 			goto ancestorerr;
28355367Sahrens 		}
28365367Sahrens 
28375367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28385367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28395367Sahrens 			goto ancestorerr;
28405367Sahrens 		}
28415367Sahrens 
28425367Sahrens 		if (zfs_share(h) != 0) {
28435367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28445367Sahrens 			goto ancestorerr;
28455367Sahrens 		}
28465367Sahrens 
28475367Sahrens 		zfs_close(h);
28485367Sahrens 	}
28495367Sahrens 
28505367Sahrens 	return (0);
28515367Sahrens 
28525367Sahrens ancestorerr:
28535367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28545367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28555367Sahrens 	return (-1);
28565367Sahrens }
28575367Sahrens 
28585367Sahrens /*
28594490Svb160487  * Creates non-existing ancestors of the given path.
28604490Svb160487  */
28614490Svb160487 int
28624490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28634490Svb160487 {
28644490Svb160487 	int prefix;
28654490Svb160487 	uint64_t zoned;
28664490Svb160487 	char *path_copy;
28674490Svb160487 	int rc;
28684490Svb160487 
28694490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28704490Svb160487 		return (-1);
28714490Svb160487 
28724490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28734490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28744490Svb160487 		free(path_copy);
28754490Svb160487 	}
28764490Svb160487 	if (path_copy == NULL || rc != 0)
28774490Svb160487 		return (-1);
28784490Svb160487 
2879789Sahrens 	return (0);
2880789Sahrens }
2881789Sahrens 
2882789Sahrens /*
28832676Seschrock  * Create a new filesystem or volume.
2884789Sahrens  */
2885789Sahrens int
28862082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28872676Seschrock     nvlist_t *props)
2888789Sahrens {
2889789Sahrens 	zfs_cmd_t zc = { 0 };
2890789Sahrens 	int ret;
2891789Sahrens 	uint64_t size = 0;
2892789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
28932082Seschrock 	char errbuf[1024];
28942676Seschrock 	uint64_t zoned;
28952082Seschrock 
28962082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28972082Seschrock 	    "cannot create '%s'"), path);
2898789Sahrens 
2899789Sahrens 	/* validate the path, taking care to note the extended error message */
29005326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
29012082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2902789Sahrens 
2903789Sahrens 	/* validate parents exist */
29044490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2905789Sahrens 		return (-1);
2906789Sahrens 
2907789Sahrens 	/*
2908789Sahrens 	 * The failure modes when creating a dataset of a different type over
2909789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2910789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2911789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2912789Sahrens 	 * first try to see if the dataset exists.
2913789Sahrens 	 */
2914789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
29155094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
29162082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29172082Seschrock 		    "dataset already exists"));
29182082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2919789Sahrens 	}
2920789Sahrens 
2921789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2922789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2923789Sahrens 	else
2924789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2925789Sahrens 
29265094Slling 	if (props && (props = zfs_validate_properties(hdl, type, props,
29273912Slling 	    zoned, NULL, errbuf)) == 0)
29282676Seschrock 		return (-1);
29292676Seschrock 
2930789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29311133Seschrock 		/*
29321133Seschrock 		 * If we are creating a volume, the size and block size must
29331133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29341133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29351133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29361133Seschrock 		 * zero.
29371133Seschrock 		 */
29382676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
29392676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
29402676Seschrock 			nvlist_free(props);
29412082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29422676Seschrock 			    "missing volume size"));
29432676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2944789Sahrens 		}
2945789Sahrens 
29462676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
29472676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29482676Seschrock 		    &blocksize)) != 0) {
29492676Seschrock 			if (ret == ENOENT) {
29502676Seschrock 				blocksize = zfs_prop_default_numeric(
29512676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29522676Seschrock 			} else {
29532676Seschrock 				nvlist_free(props);
29542676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29552676Seschrock 				    "missing volume block size"));
29562676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29572676Seschrock 			}
29582676Seschrock 		}
29592676Seschrock 
29602676Seschrock 		if (size == 0) {
29612676Seschrock 			nvlist_free(props);
29622082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29632676Seschrock 			    "volume size cannot be zero"));
29642676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29651133Seschrock 		}
29661133Seschrock 
29671133Seschrock 		if (size % blocksize != 0) {
29682676Seschrock 			nvlist_free(props);
29692082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29702676Seschrock 			    "volume size must be a multiple of volume block "
29712676Seschrock 			    "size"));
29722676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29731133Seschrock 		}
2974789Sahrens 	}
2975789Sahrens 
29765094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
29772676Seschrock 		return (-1);
29782676Seschrock 	nvlist_free(props);
29792676Seschrock 
2980789Sahrens 	/* create the dataset */
29814543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2982789Sahrens 
29833912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29842082Seschrock 		ret = zvol_create_link(hdl, path);
29853912Slling 		if (ret) {
29863912Slling 			(void) zfs_standard_error(hdl, errno,
29873912Slling 			    dgettext(TEXT_DOMAIN,
29883912Slling 			    "Volume successfully created, but device links "
29893912Slling 			    "were not created"));
29903912Slling 			zcmd_free_nvlists(&zc);
29913912Slling 			return (-1);
29923912Slling 		}
29933912Slling 	}
2994789Sahrens 
29952676Seschrock 	zcmd_free_nvlists(&zc);
29962676Seschrock 
2997789Sahrens 	/* check for failure */
2998789Sahrens 	if (ret != 0) {
2999789Sahrens 		char parent[ZFS_MAXNAMELEN];
3000789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3001789Sahrens 
3002789Sahrens 		switch (errno) {
3003789Sahrens 		case ENOENT:
30042082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30052082Seschrock 			    "no such parent '%s'"), parent);
30062082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3007789Sahrens 
3008789Sahrens 		case EINVAL:
30092082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30103413Smmusante 			    "parent '%s' is not a filesystem"), parent);
30112082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3012789Sahrens 
3013789Sahrens 		case EDOM:
30142082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30152676Seschrock 			    "volume block size must be power of 2 from "
30162676Seschrock 			    "%u to %uk"),
3017789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3018789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
30192082Seschrock 
30202676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30212082Seschrock 
30224603Sahrens 		case ENOTSUP:
30234603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30244603Sahrens 			    "pool must be upgraded to set this "
30254603Sahrens 			    "property or value"));
30264603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3027789Sahrens #ifdef _ILP32
3028789Sahrens 		case EOVERFLOW:
3029789Sahrens 			/*
3030789Sahrens 			 * This platform can't address a volume this big.
3031789Sahrens 			 */
30322082Seschrock 			if (type == ZFS_TYPE_VOLUME)
30332082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
30342082Seschrock 				    errbuf));
3035789Sahrens #endif
30362082Seschrock 			/* FALLTHROUGH */
3037789Sahrens 		default:
30382082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3039789Sahrens 		}
3040789Sahrens 	}
3041789Sahrens 
3042789Sahrens 	return (0);
3043789Sahrens }
3044789Sahrens 
3045789Sahrens /*
3046789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3047789Sahrens  * isn't mounted, and that there are no active dependents.
3048789Sahrens  */
3049789Sahrens int
3050789Sahrens zfs_destroy(zfs_handle_t *zhp)
3051789Sahrens {
3052789Sahrens 	zfs_cmd_t zc = { 0 };
3053789Sahrens 
3054789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3055789Sahrens 
30562676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30573126Sahl 		/*
30584543Smarks 		 * If user doesn't have permissions to unshare volume, then
30594543Smarks 		 * abort the request.  This would only happen for a
30604543Smarks 		 * non-privileged user.
30613126Sahl 		 */
30624543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30634543Smarks 			return (-1);
30644543Smarks 		}
30653126Sahl 
30662082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3067789Sahrens 			return (-1);
3068789Sahrens 
3069789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3070789Sahrens 	} else {
3071789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3072789Sahrens 	}
3073789Sahrens 
30744543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30753237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30762082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30772082Seschrock 		    zhp->zfs_name));
30782199Sahrens 	}
3079789Sahrens 
3080789Sahrens 	remove_mountpoint(zhp);
3081789Sahrens 
3082789Sahrens 	return (0);
3083789Sahrens }
3084789Sahrens 
30852199Sahrens struct destroydata {
30862199Sahrens 	char *snapname;
30872199Sahrens 	boolean_t gotone;
30883265Sahrens 	boolean_t closezhp;
30892199Sahrens };
30902199Sahrens 
30912199Sahrens static int
30922199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
30932199Sahrens {
30942199Sahrens 	struct destroydata *dd = arg;
30952199Sahrens 	zfs_handle_t *szhp;
30962199Sahrens 	char name[ZFS_MAXNAMELEN];
30973265Sahrens 	boolean_t closezhp = dd->closezhp;
30983265Sahrens 	int rv;
30992199Sahrens 
31002676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31012676Seschrock 	(void) strlcat(name, "@", sizeof (name));
31022676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
31032199Sahrens 
31042199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31052199Sahrens 	if (szhp) {
31062199Sahrens 		dd->gotone = B_TRUE;
31072199Sahrens 		zfs_close(szhp);
31082199Sahrens 	}
31092199Sahrens 
31102199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31112199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
31122199Sahrens 		/*
31132199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31142199Sahrens 		 * return an error, because then we wouldn't visit all
31152199Sahrens 		 * the volumes.
31162199Sahrens 		 */
31172199Sahrens 	}
31182199Sahrens 
31193265Sahrens 	dd->closezhp = B_TRUE;
31203265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
31213265Sahrens 	if (closezhp)
31223265Sahrens 		zfs_close(zhp);
31233265Sahrens 	return (rv);
31242199Sahrens }
31252199Sahrens 
31262199Sahrens /*
31272199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31282199Sahrens  */
31292199Sahrens int
31302199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
31312199Sahrens {
31322199Sahrens 	zfs_cmd_t zc = { 0 };
31332199Sahrens 	int ret;
31342199Sahrens 	struct destroydata dd = { 0 };
31352199Sahrens 
31362199Sahrens 	dd.snapname = snapname;
31372199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
31382199Sahrens 
31392199Sahrens 	if (!dd.gotone) {
31403237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31412199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
31422199Sahrens 		    zhp->zfs_name, snapname));
31432199Sahrens 	}
31442199Sahrens 
31452199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31462676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
31472199Sahrens 
31484543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
31492199Sahrens 	if (ret != 0) {
31502199Sahrens 		char errbuf[1024];
31512199Sahrens 
31522199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31532199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31542199Sahrens 
31552199Sahrens 		switch (errno) {
31562199Sahrens 		case EEXIST:
31572199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31582199Sahrens 			    "snapshot is cloned"));
31592199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31602199Sahrens 
31612199Sahrens 		default:
31622199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31632199Sahrens 			    errbuf));
31642199Sahrens 		}
31652199Sahrens 	}
31662199Sahrens 
31672199Sahrens 	return (0);
31682199Sahrens }
31692199Sahrens 
3170789Sahrens /*
3171789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3172789Sahrens  */
3173789Sahrens int
31742676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3175789Sahrens {
3176789Sahrens 	zfs_cmd_t zc = { 0 };
3177789Sahrens 	char parent[ZFS_MAXNAMELEN];
3178789Sahrens 	int ret;
31792082Seschrock 	char errbuf[1024];
31802082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31812676Seschrock 	zfs_type_t type;
31822676Seschrock 	uint64_t zoned;
3183789Sahrens 
3184789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3185789Sahrens 
31862082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31872082Seschrock 	    "cannot create '%s'"), target);
31882082Seschrock 
3189789Sahrens 	/* validate the target name */
31905326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
31912082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3192789Sahrens 
3193789Sahrens 	/* validate parents exist */
31944490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3195789Sahrens 		return (-1);
3196789Sahrens 
3197789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3198789Sahrens 
3199789Sahrens 	/* do the clone */
32002676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3201789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32022744Snn35248 		type = ZFS_TYPE_VOLUME;
32032676Seschrock 	} else {
3204789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32052744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
32062676Seschrock 	}
32072676Seschrock 
32082676Seschrock 	if (props) {
32095094Slling 		if ((props = zfs_validate_properties(hdl, type, props,
32103912Slling 		    zoned, zhp, errbuf)) == NULL)
32112676Seschrock 			return (-1);
32122676Seschrock 
32135094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
32142676Seschrock 			nvlist_free(props);
32152676Seschrock 			return (-1);
32162676Seschrock 		}
32172676Seschrock 
32182676Seschrock 		nvlist_free(props);
32192676Seschrock 	}
3220789Sahrens 
3221789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
32222676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
32234543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3224789Sahrens 
32252676Seschrock 	zcmd_free_nvlists(&zc);
32262676Seschrock 
3227789Sahrens 	if (ret != 0) {
3228789Sahrens 		switch (errno) {
3229789Sahrens 
3230789Sahrens 		case ENOENT:
3231789Sahrens 			/*
3232789Sahrens 			 * The parent doesn't exist.  We should have caught this
3233789Sahrens 			 * above, but there may a race condition that has since
3234789Sahrens 			 * destroyed the parent.
3235789Sahrens 			 *
3236789Sahrens 			 * At this point, we don't know whether it's the source
3237789Sahrens 			 * that doesn't exist anymore, or whether the target
3238789Sahrens 			 * dataset doesn't exist.
3239789Sahrens 			 */
32402082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32412082Seschrock 			    "no such parent '%s'"), parent);
32422082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
32432082Seschrock 
32442082Seschrock 		case EXDEV:
32452082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32462082Seschrock 			    "source and target pools differ"));
32472082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
32482082Seschrock 			    errbuf));
32492082Seschrock 
32502082Seschrock 		default:
32512082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32522082Seschrock 			    errbuf));
32532082Seschrock 		}
32542676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32552082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32562082Seschrock 	}
32572082Seschrock 
32582082Seschrock 	return (ret);
32592082Seschrock }
32602082Seschrock 
32612082Seschrock typedef struct promote_data {
32622082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32632082Seschrock 	const char *cb_target;
32642082Seschrock 	const char *cb_errbuf;
32652082Seschrock 	uint64_t cb_pivot_txg;
32662082Seschrock } promote_data_t;
32672082Seschrock 
32682082Seschrock static int
32692082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32702082Seschrock {
32712082Seschrock 	promote_data_t *pd = data;
32722082Seschrock 	zfs_handle_t *szhp;
32732082Seschrock 	char snapname[MAXPATHLEN];
32743265Sahrens 	int rv = 0;
32752082Seschrock 
32762082Seschrock 	/* We don't care about snapshots after the pivot point */
32773265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32783265Sahrens 		zfs_close(zhp);
32792082Seschrock 		return (0);
32803265Sahrens 	}
32812082Seschrock 
32822417Sahrens 	/* Remove the device link if it's a zvol. */
32832676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32842417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32852082Seschrock 
32862082Seschrock 	/* Check for conflicting names */
32872676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32882676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
32892082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
32902082Seschrock 	if (szhp != NULL) {
32912082Seschrock 		zfs_close(szhp);
32922082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32932082Seschrock 		    "snapshot name '%s' from origin \n"
32942082Seschrock 		    "conflicts with '%s' from target"),
32952082Seschrock 		    zhp->zfs_name, snapname);
32963265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
32972082Seschrock 	}
32983265Sahrens 	zfs_close(zhp);
32993265Sahrens 	return (rv);
33002082Seschrock }
33012082Seschrock 
33022417Sahrens static int
33032417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
33042417Sahrens {
33052417Sahrens 	promote_data_t *pd = data;
33062417Sahrens 
33072417Sahrens 	/* We don't care about snapshots after the pivot point */
33083265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
33093265Sahrens 		/* Create the device link if it's a zvol. */
33103265Sahrens 		if (ZFS_IS_VOLUME(zhp))
33113265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
33123265Sahrens 	}
33133265Sahrens 
33143265Sahrens 	zfs_close(zhp);
33152417Sahrens 	return (0);
33162417Sahrens }
33172417Sahrens 
33182082Seschrock /*
33192082Seschrock  * Promotes the given clone fs to be the clone parent.
33202082Seschrock  */
33212082Seschrock int
33222082Seschrock zfs_promote(zfs_handle_t *zhp)
33232082Seschrock {
33242082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33252082Seschrock 	zfs_cmd_t zc = { 0 };
33262082Seschrock 	char parent[MAXPATHLEN];
33272082Seschrock 	char *cp;
33282082Seschrock 	int ret;
33292082Seschrock 	zfs_handle_t *pzhp;
33302082Seschrock 	promote_data_t pd;
33312082Seschrock 	char errbuf[1024];
33322082Seschrock 
33332082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33342082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
33352082Seschrock 
33362082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
33372082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33382082Seschrock 		    "snapshots can not be promoted"));
33392082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33402082Seschrock 	}
33412082Seschrock 
33425367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
33432082Seschrock 	if (parent[0] == '\0') {
33442082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33452082Seschrock 		    "not a cloned filesystem"));
33462082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33472082Seschrock 	}
33482082Seschrock 	cp = strchr(parent, '@');
33492082Seschrock 	*cp = '\0';
33502082Seschrock 
33512082Seschrock 	/* Walk the snapshots we will be moving */
33525367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
33532082Seschrock 	if (pzhp == NULL)
33542082Seschrock 		return (-1);
33552082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33562082Seschrock 	zfs_close(pzhp);
33572082Seschrock 	pd.cb_target = zhp->zfs_name;
33582082Seschrock 	pd.cb_errbuf = errbuf;
33595094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
33602082Seschrock 	if (pzhp == NULL)
33612082Seschrock 		return (-1);
33622082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33632082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33642082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33652417Sahrens 	if (ret != 0) {
33662417Sahrens 		zfs_close(pzhp);
33672082Seschrock 		return (-1);
33682417Sahrens 	}
33692082Seschrock 
33702082Seschrock 	/* issue the ioctl */
33715367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
33722676Seschrock 	    sizeof (zc.zc_value));
33732082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33744543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33752082Seschrock 
33762082Seschrock 	if (ret != 0) {
33772417Sahrens 		int save_errno = errno;
33782417Sahrens 
33792417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33802417Sahrens 		zfs_close(pzhp);
33812417Sahrens 
33822417Sahrens 		switch (save_errno) {
3383789Sahrens 		case EEXIST:
3384789Sahrens 			/*
33852082Seschrock 			 * There is a conflicting snapshot name.  We
33862082Seschrock 			 * should have caught this above, but they could
33872082Seschrock 			 * have renamed something in the mean time.
3388789Sahrens 			 */
33892082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33902082Seschrock 			    "conflicting snapshot name from parent '%s'"),
33912082Seschrock 			    parent);
33922082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3393789Sahrens 
3394789Sahrens 		default:
33952417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3396789Sahrens 		}
33972417Sahrens 	} else {
33982417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3399789Sahrens 	}
3400789Sahrens 
34012417Sahrens 	zfs_close(pzhp);
3402789Sahrens 	return (ret);
3403789Sahrens }
3404789Sahrens 
34054007Smmusante struct createdata {
34064007Smmusante 	const char *cd_snapname;
34074007Smmusante 	int cd_ifexists;
34084007Smmusante };
34094007Smmusante 
34102199Sahrens static int
34112199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
34122199Sahrens {
34134007Smmusante 	struct createdata *cd = arg;
34142676Seschrock 	int ret;
34152199Sahrens 
34162199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
34172199Sahrens 		char name[MAXPATHLEN];
34182199Sahrens 
34192676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
34202676Seschrock 		(void) strlcat(name, "@", sizeof (name));
34214007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
34224007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
34234007Smmusante 		    cd->cd_ifexists);
34242199Sahrens 		/*
34252199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
34262199Sahrens 		 * return an error, because then we wouldn't visit all
34272199Sahrens 		 * the volumes.
34282199Sahrens 		 */
34292199Sahrens 	}
34302676Seschrock 
34314007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
34322676Seschrock 
34332676Seschrock 	zfs_close(zhp);
34342676Seschrock 
34352676Seschrock 	return (ret);
34362199Sahrens }
34372199Sahrens 
3438789Sahrens /*
34393504Sahl  * Takes a snapshot of the given dataset.
3440789Sahrens  */
3441789Sahrens int
34422199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3443789Sahrens {
3444789Sahrens 	const char *delim;
3445789Sahrens 	char *parent;
3446789Sahrens 	zfs_handle_t *zhp;
3447789Sahrens 	zfs_cmd_t zc = { 0 };
3448789Sahrens 	int ret;
34492082Seschrock 	char errbuf[1024];
34502082Seschrock 
34512082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34522082Seschrock 	    "cannot snapshot '%s'"), path);
34532082Seschrock 
34542082Seschrock 	/* validate the target name */
34555326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
34562082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3457789Sahrens 
3458789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34592199Sahrens 	delim = strchr(path, '@');
34602082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
34612082Seschrock 		return (-1);
3462789Sahrens 	(void) strncpy(parent, path, delim - path);
3463789Sahrens 	parent[delim - path] = '\0';
3464789Sahrens 
34652082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3466789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3467789Sahrens 		free(parent);
3468789Sahrens 		return (-1);
3469789Sahrens 	}
3470789Sahrens 
34712199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34722676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34734543Smarks 	if (ZFS_IS_VOLUME(zhp))
34744543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34754543Smarks 	else
34764543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34772199Sahrens 	zc.zc_cookie = recursive;
34784543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
34792199Sahrens 
34802199Sahrens 	/*
34812199Sahrens 	 * if it was recursive, the one that actually failed will be in
34822199Sahrens 	 * zc.zc_name.
34832199Sahrens 	 */
34844543Smarks 	if (ret != 0)
34854543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34864543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
34874543Smarks 
34882199Sahrens 	if (ret == 0 && recursive) {
34894007Smmusante 		struct createdata cd;
34904007Smmusante 
34914007Smmusante 		cd.cd_snapname = delim + 1;
34924007Smmusante 		cd.cd_ifexists = B_FALSE;
34934007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
34942199Sahrens 	}
3495789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
34962082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
34972199Sahrens 		if (ret != 0) {
34984543Smarks 			(void) zfs_standard_error(hdl, errno,
34994543Smarks 			    dgettext(TEXT_DOMAIN,
35004543Smarks 			    "Volume successfully snapshotted, but device links "
35014543Smarks 			    "were not created"));
35024543Smarks 			free(parent);
35034543Smarks 			zfs_close(zhp);
35044543Smarks 			return (-1);
35052199Sahrens 		}
3506789Sahrens 	}
3507789Sahrens 
35082082Seschrock 	if (ret != 0)
35092082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3510789Sahrens 
3511789Sahrens 	free(parent);
3512789Sahrens 	zfs_close(zhp);
3513789Sahrens 
3514789Sahrens 	return (ret);
3515789Sahrens }
3516789Sahrens 
3517789Sahrens /*
35181294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
35191294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
35201294Slling  * is a dependent and we should just destroy it without checking the transaction
35211294Slling  * group.
3522789Sahrens  */
35231294Slling typedef struct rollback_data {
35241294Slling 	const char	*cb_target;		/* the snapshot */
35251294Slling 	uint64_t	cb_create;		/* creation time reference */
35265749Sahrens 	boolean_t	cb_error;
35272082Seschrock 	boolean_t	cb_dependent;
35285749Sahrens 	boolean_t	cb_force;
35291294Slling } rollback_data_t;
35301294Slling 
35311294Slling static int
35321294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
35331294Slling {
35341294Slling 	rollback_data_t *cbp = data;
35351294Slling 
35361294Slling 	if (!cbp->cb_dependent) {
35371294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
35381294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
35391294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
35401294Slling 		    cbp->cb_create) {
35414543Smarks 			char *logstr;
35421294Slling 
35432082Seschrock 			cbp->cb_dependent = B_TRUE;
35445446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
35455446Sahrens 			    rollback_destroy, cbp);
35462082Seschrock 			cbp->cb_dependent = B_FALSE;
35471294Slling 
35484543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
35494543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
35505446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
35514543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
35521294Slling 		}
35531294Slling 	} else {
35545749Sahrens 		/* We must destroy this clone; first unmount it */
35555749Sahrens 		prop_changelist_t *clp;
35565749Sahrens 
35575749Sahrens 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
35585749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
35595749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
35605749Sahrens 			cbp->cb_error = B_TRUE;
35615749Sahrens 			zfs_close(zhp);
35625749Sahrens 			return (0);
35635749Sahrens 		}
35645749Sahrens 		if (zfs_destroy(zhp) != 0)
35655749Sahrens 			cbp->cb_error = B_TRUE;
35665749Sahrens 		else
35675749Sahrens 			changelist_remove(clp, zhp->zfs_name);
35685751Sahrens 		(void) changelist_postfix(clp);
35695749Sahrens 		changelist_free(clp);
35701294Slling 	}
35711294Slling 
35721294Slling 	zfs_close(zhp);
35731294Slling 	return (0);
35741294Slling }
35751294Slling 
35761294Slling /*
35775446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35785446Sahrens  * data changes since then and making it the active dataset.
35795446Sahrens  *
35805446Sahrens  * Any snapshots more recent than the target are destroyed, along with
35815446Sahrens  * their dependents.
35821294Slling  */
35835446Sahrens int
35845749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3585789Sahrens {
35865446Sahrens 	rollback_data_t cb = { 0 };
35875446Sahrens 	int err;
3588789Sahrens 	zfs_cmd_t zc = { 0 };
35895713Srm160521 	boolean_t restore_resv = 0;
35905713Srm160521 	uint64_t old_volsize, new_volsize;
35915713Srm160521 	zfs_prop_t resv_prop;
3592789Sahrens 
3593789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3594789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3595789Sahrens 
35965446Sahrens 	/*
35975446Sahrens 	 * Destroy all recent snapshots and its dependends.
35985446Sahrens 	 */
35995749Sahrens 	cb.cb_force = force;
36005446Sahrens 	cb.cb_target = snap->zfs_name;
36015446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
36025446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
36035446Sahrens 
36045749Sahrens 	if (cb.cb_error)
36055749Sahrens 		return (-1);
36065446Sahrens 
36075446Sahrens 	/*
36085446Sahrens 	 * Now that we have verified that the snapshot is the latest,
36095446Sahrens 	 * rollback to the given snapshot.
36105446Sahrens 	 */
36115446Sahrens 
36125713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
36135713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
36145713Srm160521 			return (-1);
36155713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
36165713Srm160521 			return (-1);
36175713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36185713Srm160521 		restore_resv =
36195713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
36205713Srm160521 	}
3621789Sahrens 
3622789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3623789Sahrens 
36242676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3625789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3626789Sahrens 	else
3627789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3628789Sahrens 
3629789Sahrens 	/*
36305446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
36315446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
36325446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
36335446Sahrens 	 * an unlikely race condition where the user has taken a
36345446Sahrens 	 * snapshot since we verified that this was the most recent.
36355713Srm160521 	 *
3636789Sahrens 	 */
36375446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
36383237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
36392082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
36402082Seschrock 		    zhp->zfs_name);
36415717Srm160521 		return (err);
36425717Srm160521 	}
36435713Srm160521 
36445713Srm160521 	/*
36455713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
36465713Srm160521 	 * rollback reservation and the volsize has changed then set
36475713Srm160521 	 * the reservation property to the post-rollback volsize.
36485713Srm160521 	 * Make a new handle since the rollback closed the dataset.
36495713Srm160521 	 */
36505717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
36515717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
36525717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
36535717Srm160521 			zfs_close(zhp);
36545713Srm160521 			return (err);
36555717Srm160521 		}
36565713Srm160521 		if (restore_resv) {
36575713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36585713Srm160521 			if (old_volsize != new_volsize)
36595717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
36605717Srm160521 				    new_volsize);
36615713Srm160521 		}
36625713Srm160521 		zfs_close(zhp);
3663789Sahrens 	}
36645446Sahrens 	return (err);
36651294Slling }
36661294Slling 
36671294Slling /*
3668789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3669789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3670789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3671789Sahrens  * libzfs_graph.c.
3672789Sahrens  */
3673789Sahrens int
36742474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36752474Seschrock     zfs_iter_f func, void *data)
3676789Sahrens {
3677789Sahrens 	char **dependents;
3678789Sahrens 	size_t count;
3679789Sahrens 	int i;
3680789Sahrens 	zfs_handle_t *child;
3681789Sahrens 	int ret = 0;
3682789Sahrens 
36832474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
36842474Seschrock 	    &dependents, &count) != 0)
36852474Seschrock 		return (-1);
36862474Seschrock 
3687789Sahrens 	for (i = 0; i < count; i++) {
36882082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
36892082Seschrock 		    dependents[i])) == NULL)
3690789Sahrens 			continue;
3691789Sahrens 
3692789Sahrens 		if ((ret = func(child, data)) != 0)
3693789Sahrens 			break;
3694789Sahrens 	}
3695789Sahrens 
3696789Sahrens 	for (i = 0; i < count; i++)
3697789Sahrens 		free(dependents[i]);
3698789Sahrens 	free(dependents);
3699789Sahrens 
3700789Sahrens 	return (ret);
3701789Sahrens }
3702789Sahrens 
3703789Sahrens /*
3704789Sahrens  * Renames the given dataset.
3705789Sahrens  */
3706789Sahrens int
37074490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3708789Sahrens {
3709789Sahrens 	int ret;
3710789Sahrens 	zfs_cmd_t zc = { 0 };
3711789Sahrens 	char *delim;
37124007Smmusante 	prop_changelist_t *cl = NULL;
37134007Smmusante 	zfs_handle_t *zhrp = NULL;
37144007Smmusante 	char *parentname = NULL;
3715789Sahrens 	char parent[ZFS_MAXNAMELEN];
37162082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37172082Seschrock 	char errbuf[1024];
3718789Sahrens 
3719789Sahrens 	/* if we have the same exact name, just return success */
3720789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3721789Sahrens 		return (0);
3722789Sahrens 
37232082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37242082Seschrock 	    "cannot rename to '%s'"), target);
37252082Seschrock 
3726789Sahrens 	/*
3727789Sahrens 	 * Make sure the target name is valid
3728789Sahrens 	 */
3729789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
37302665Snd150628 		if ((strchr(target, '@') == NULL) ||
37312665Snd150628 		    *target == '@') {
37322665Snd150628 			/*
37332665Snd150628 			 * Snapshot target name is abbreviated,
37342665Snd150628 			 * reconstruct full dataset name
37352665Snd150628 			 */
37362665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
37372665Snd150628 			    sizeof (parent));
37382665Snd150628 			delim = strchr(parent, '@');
37392665Snd150628 			if (strchr(target, '@') == NULL)
37402665Snd150628 				*(++delim) = '\0';
37412665Snd150628 			else
37422665Snd150628 				*delim = '\0';
37432665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
37442665Snd150628 			target = parent;
37452665Snd150628 		} else {
37462665Snd150628 			/*
37472665Snd150628 			 * Make sure we're renaming within the same dataset.
37482665Snd150628 			 */
37492665Snd150628 			delim = strchr(target, '@');
37502665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
37512665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
37522665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37532665Snd150628 				    "snapshots must be part of same "
37542665Snd150628 				    "dataset"));
37552665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
37563912Slling 				    errbuf));
37572665Snd150628 			}
3758789Sahrens 		}
37595326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37602665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3761789Sahrens 	} else {
37624007Smmusante 		if (recursive) {
37634007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37644007Smmusante 			    "recursive rename must be a snapshot"));
37654007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
37664007Smmusante 		}
37674007Smmusante 
37685326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37692665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37702676Seschrock 		uint64_t unused;
37712676Seschrock 
3772789Sahrens 		/* validate parents */
37734490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3774789Sahrens 			return (-1);
3775789Sahrens 
3776789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3777789Sahrens 
3778789Sahrens 		/* make sure we're in the same pool */
3779789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3780789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3781789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
37822082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37832082Seschrock 			    "datasets must be within same pool"));
37842082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3785789Sahrens 		}
37862440Snd150628 
37872440Snd150628 		/* new name cannot be a child of the current dataset name */
37882440Snd150628 		if (strncmp(parent, zhp->zfs_name,
37893912Slling 		    strlen(zhp->zfs_name)) == 0) {
37902440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37912440Snd150628 			    "New dataset name cannot be a descendent of "
37922440Snd150628 			    "current dataset name"));
37932440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37942440Snd150628 		}
3795789Sahrens 	}
3796789Sahrens 
37972082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
37982082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
37992082Seschrock 
3800789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3801789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
38022082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38032082Seschrock 		    "dataset is used in a non-global zone"));
38042082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3805789Sahrens 	}
3806789Sahrens 
38074007Smmusante 	if (recursive) {
38084007Smmusante 		struct destroydata dd;
38094007Smmusante 
38104183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
38114183Smmusante 		if (parentname == NULL) {
38124183Smmusante 			ret = -1;
38134183Smmusante 			goto error;
38144183Smmusante 		}
38154007Smmusante 		delim = strchr(parentname, '@');
38164007Smmusante 		*delim = '\0';
38175094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
38184007Smmusante 		if (zhrp == NULL) {
38194183Smmusante 			ret = -1;
38204183Smmusante 			goto error;
38214007Smmusante 		}
38224007Smmusante 
38234007Smmusante 		dd.snapname = delim + 1;
38244007Smmusante 		dd.gotone = B_FALSE;
38254183Smmusante 		dd.closezhp = B_TRUE;
38264007Smmusante 
38274007Smmusante 		/* We remove any zvol links prior to renaming them */
38284007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
38294007Smmusante 		if (ret) {
38304007Smmusante 			goto error;
38314007Smmusante 		}
38324007Smmusante 	} else {
38334007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
38344007Smmusante 			return (-1);
38354007Smmusante 
38364007Smmusante 		if (changelist_haszonedchild(cl)) {
38374007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38384007Smmusante 			    "child dataset with inherited mountpoint is used "
38394007Smmusante 			    "in a non-global zone"));
38404007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
38414007Smmusante 			goto error;
38424007Smmusante 		}
38434007Smmusante 
38444007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
38454007Smmusante 			goto error;
3846789Sahrens 	}
3847789Sahrens 
38482676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3849789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3850789Sahrens 	else
3851789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3852789Sahrens 
38532665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
38542676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
38552665Snd150628 
38564007Smmusante 	zc.zc_cookie = recursive;
38574007Smmusante 
38584543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
38594007Smmusante 		/*
38604007Smmusante 		 * if it was recursive, the one that actually failed will
38614007Smmusante 		 * be in zc.zc_name
38624007Smmusante 		 */
38634007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38645367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
38654007Smmusante 
38664007Smmusante 		if (recursive && errno == EEXIST) {
38674007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38684007Smmusante 			    "a child dataset already has a snapshot "
38694007Smmusante 			    "with the new name"));
38704801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38714007Smmusante 		} else {
38724007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
38734007Smmusante 		}
3874789Sahrens 
3875789Sahrens 		/*
3876789Sahrens 		 * On failure, we still want to remount any filesystems that
3877789Sahrens 		 * were previously mounted, so we don't alter the system state.
3878789Sahrens 		 */
38794007Smmusante 		if (recursive) {
38804007Smmusante 			struct createdata cd;
38814007Smmusante 
38824007Smmusante 			/* only create links for datasets that had existed */
38834007Smmusante 			cd.cd_snapname = delim + 1;
38844007Smmusante 			cd.cd_ifexists = B_TRUE;
38854007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38864007Smmusante 			    &cd);
38874007Smmusante 		} else {
38884007Smmusante 			(void) changelist_postfix(cl);
38894007Smmusante 		}
3890789Sahrens 	} else {
38914007Smmusante 		if (recursive) {
38924007Smmusante 			struct createdata cd;
38934007Smmusante 
38944007Smmusante 			/* only create links for datasets that had existed */
38954007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
38964007Smmusante 			cd.cd_ifexists = B_TRUE;
38974007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38984007Smmusante 			    &cd);
38994007Smmusante 		} else {
39004007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
39014007Smmusante 			ret = changelist_postfix(cl);
39024007Smmusante 		}
3903789Sahrens 	}
3904789Sahrens 
3905789Sahrens error:
39064007Smmusante 	if (parentname) {
39074007Smmusante 		free(parentname);
39084007Smmusante 	}
39094007Smmusante 	if (zhrp) {
39104007Smmusante 		zfs_close(zhrp);
39114007Smmusante 	}
39124007Smmusante 	if (cl) {
39134007Smmusante 		changelist_free(cl);
39144007Smmusante 	}
3915789Sahrens 	return (ret);
3916789Sahrens }
3917789Sahrens 
3918789Sahrens /*
3919789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3920789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3921789Sahrens  */
3922789Sahrens int
39232082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3924789Sahrens {
39254007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
39264007Smmusante }
39274007Smmusante 
39284007Smmusante static int
39294007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
39304007Smmusante {
3931789Sahrens 	zfs_cmd_t zc = { 0 };
39322082Seschrock 	di_devlink_handle_t dhdl;
39334543Smarks 	priv_set_t *priv_effective;
39344543Smarks 	int privileged;
3935789Sahrens 
3936789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3937789Sahrens 
3938789Sahrens 	/*
3939789Sahrens 	 * Issue the appropriate ioctl.
3940789Sahrens 	 */
39412082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3942789Sahrens 		switch (errno) {
3943789Sahrens 		case EEXIST:
3944789Sahrens 			/*
3945789Sahrens 			 * Silently ignore the case where the link already
3946789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3947789Sahrens 			 * times without errors.
3948789Sahrens 			 */
3949789Sahrens 			return (0);
3950789Sahrens 
39514007Smmusante 		case ENOENT:
39524007Smmusante 			/*
39534007Smmusante 			 * Dataset does not exist in the kernel.  If we
39544007Smmusante 			 * don't care (see zfs_rename), then ignore the
39554007Smmusante 			 * error quietly.
39564007Smmusante 			 */
39574007Smmusante 			if (ifexists) {
39584007Smmusante 				return (0);
39594007Smmusante 			}
39604007Smmusante 
39614007Smmusante 			/* FALLTHROUGH */
39624007Smmusante 
3963789Sahrens 		default:
39643237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39652082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39662082Seschrock 			    "for '%s'"), dataset));
3967789Sahrens 		}
3968789Sahrens 	}
3969789Sahrens 
3970789Sahrens 	/*
39714543Smarks 	 * If privileged call devfsadm and wait for the links to
39724543Smarks 	 * magically appear.
39734543Smarks 	 * Otherwise, print out an informational message.
3974789Sahrens 	 */
39754543Smarks 
39764543Smarks 	priv_effective = priv_allocset();
39774543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
39784543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
39794543Smarks 	priv_freeset(priv_effective);
39804543Smarks 
39814543Smarks 	if (privileged) {
39824543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
39834543Smarks 		    DI_MAKE_LINK)) == NULL) {
39844543Smarks 			zfs_error_aux(hdl, strerror(errno));
39854543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
39864543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39874543Smarks 			    "for '%s'"), dataset);
39884543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
39894543Smarks 			return (-1);
39904543Smarks 		} else {
39914543Smarks 			(void) di_devlink_fini(&dhdl);
39924543Smarks 		}
3993789Sahrens 	} else {
39944543Smarks 		char pathname[MAXPATHLEN];
39954543Smarks 		struct stat64 statbuf;
39964543Smarks 		int i;
39974543Smarks 
39984543Smarks #define	MAX_WAIT	10
39994543Smarks 
40004543Smarks 		/*
40014543Smarks 		 * This is the poor mans way of waiting for the link
40024543Smarks 		 * to show up.  If after 10 seconds we still don't
40034543Smarks 		 * have it, then print out a message.
40044543Smarks 		 */
40054543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
40064543Smarks 		    dataset);
40074543Smarks 
40084543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
40094543Smarks 			if (stat64(pathname, &statbuf) == 0)
40104543Smarks 				break;
40114543Smarks 			(void) sleep(1);
40124543Smarks 		}
40134543Smarks 		if (i == MAX_WAIT)
40144543Smarks 			(void) printf(gettext("%s may not be immediately "
40154543Smarks 			    "available\n"), pathname);
4016789Sahrens 	}
4017789Sahrens 
4018789Sahrens 	return (0);
4019789Sahrens }
4020789Sahrens 
4021789Sahrens /*
4022789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4023789Sahrens  */
4024789Sahrens int
40252082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4026789Sahrens {
4027789Sahrens 	zfs_cmd_t zc = { 0 };
4028789Sahrens 
4029789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4030789Sahrens 
40312082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4032789Sahrens 		switch (errno) {
4033789Sahrens 		case ENXIO:
4034789Sahrens 			/*
4035789Sahrens 			 * Silently ignore the case where the link no longer
4036789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4037789Sahrens 			 * times without errors.
4038789Sahrens 			 */
4039789Sahrens 			return (0);
4040789Sahrens 
4041789Sahrens 		default:
40423237Slling 			return (zfs_standard_error_fmt(hdl, errno,
40432082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
40442082Seschrock 			    "links for '%s'"), dataset));
4045789Sahrens 		}
4046789Sahrens 	}
4047789Sahrens 
4048789Sahrens 	return (0);
4049789Sahrens }
40502676Seschrock 
40512676Seschrock nvlist_t *
40522676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
40532676Seschrock {
40542676Seschrock 	return (zhp->zfs_user_props);
40552676Seschrock }
40562676Seschrock 
40572676Seschrock /*
40583912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
40593912Slling  * display, and their maximum widths.  This does two main things:
40603912Slling  *
40613912Slling  *      - If this is a list of all properties, then expand the list to include
40623912Slling  *        all native properties, and set a flag so that for each dataset we look
40633912Slling  *        for new unique user properties and add them to the list.
40643912Slling  *
40653912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
40663912Slling  *        so that we can size the column appropriately.
40673912Slling  */
40683912Slling int
40695094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
40703912Slling {
40713912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40725094Slling 	zprop_list_t *entry;
40735094Slling 	zprop_list_t **last, **start;
40743912Slling 	nvlist_t *userprops, *propval;
40753912Slling 	nvpair_t *elem;
40763912Slling 	char *strval;
40773912Slling 	char buf[ZFS_MAXPROPLEN];
40783912Slling 
40795094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
40803912Slling 		return (-1);
40812676Seschrock 
40822676Seschrock 	userprops = zfs_get_user_props(zhp);
40832676Seschrock 
40842676Seschrock 	entry = *plp;
40852676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
40862676Seschrock 		/*
40872676Seschrock 		 * Go through and add any user properties as necessary.  We
40882676Seschrock 		 * start by incrementing our list pointer to the first
40892676Seschrock 		 * non-native property.
40902676Seschrock 		 */
40912676Seschrock 		start = plp;
40922676Seschrock 		while (*start != NULL) {
40935094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
40942676Seschrock 				break;
40952676Seschrock 			start = &(*start)->pl_next;
40962676Seschrock 		}
40972676Seschrock 
40982676Seschrock 		elem = NULL;
40992676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
41002676Seschrock 			/*
41012676Seschrock 			 * See if we've already found this property in our list.
41022676Seschrock 			 */
41032676Seschrock 			for (last = start; *last != NULL;
41042676Seschrock 			    last = &(*last)->pl_next) {
41052676Seschrock 				if (strcmp((*last)->pl_user_prop,
41062676Seschrock 				    nvpair_name(elem)) == 0)
41072676Seschrock 					break;
41082676Seschrock 			}
41092676Seschrock 
41102676Seschrock 			if (*last == NULL) {
41112676Seschrock 				if ((entry = zfs_alloc(hdl,
41125094Slling 				    sizeof (zprop_list_t))) == NULL ||
41132676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
41142676Seschrock 				    nvpair_name(elem)))) == NULL) {
41152676Seschrock 					free(entry);
41162676Seschrock 					return (-1);
41172676Seschrock 				}
41182676Seschrock 
41195094Slling 				entry->pl_prop = ZPROP_INVAL;
41202676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
41212676Seschrock 				entry->pl_all = B_TRUE;
41222676Seschrock 				*last = entry;
41232676Seschrock 			}
41242676Seschrock 		}
41252676Seschrock 	}
41262676Seschrock 
41272676Seschrock 	/*
41282676Seschrock 	 * Now go through and check the width of any non-fixed columns
41292676Seschrock 	 */
41302676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
41312676Seschrock 		if (entry->pl_fixed)
41322676Seschrock 			continue;
41332676Seschrock 
41345094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
41352676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
41362676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
41372676Seschrock 				if (strlen(buf) > entry->pl_width)
41382676Seschrock 					entry->pl_width = strlen(buf);
41392676Seschrock 			}
41402676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
41412676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
41422676Seschrock 			verify(nvlist_lookup_string(propval,
41435094Slling 			    ZPROP_VALUE, &strval) == 0);
41442676Seschrock 			if (strlen(strval) > entry->pl_width)
41452676Seschrock 				entry->pl_width = strlen(strval);
41462676Seschrock 		}
41472676Seschrock 	}
41482676Seschrock 
41492676Seschrock 	return (0);
41502676Seschrock }
41514543Smarks 
41524543Smarks int
41534543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
41544543Smarks {
41554543Smarks 	zfs_cmd_t zc = { 0 };
41564543Smarks 	nvlist_t *nvp;
41574543Smarks 	gid_t gid;
41584543Smarks 	uid_t uid;
41594543Smarks 	const gid_t *groups;
41604543Smarks 	int group_cnt;
41614543Smarks 	int error;
41624543Smarks 
41634543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
41644543Smarks 		return (no_memory(hdl));
41654543Smarks 
41664543Smarks 	uid = ucred_geteuid(cred);
41674543Smarks 	gid = ucred_getegid(cred);
41684543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
41694543Smarks 
41704543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
41714543Smarks 		return (1);
41724543Smarks 
41734543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
41744543Smarks 		nvlist_free(nvp);
41754543Smarks 		return (1);
41764543Smarks 	}
41774543Smarks 
41784543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
41794543Smarks 		nvlist_free(nvp);
41804543Smarks 		return (1);
41814543Smarks 	}
41824543Smarks 
41834543Smarks 	if (nvlist_add_uint32_array(nvp,
41844543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
41854543Smarks 		nvlist_free(nvp);
41864543Smarks 		return (1);
41874543Smarks 	}
41884543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
41894543Smarks 
41905094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
41914543Smarks 		return (-1);
41924543Smarks 
41934543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
41944543Smarks 	nvlist_free(nvp);
41954543Smarks 	return (error);
41964543Smarks }
41974543Smarks 
41984543Smarks int
41994543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
42005331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
42014543Smarks {
42024543Smarks 	zfs_cmd_t zc = { 0 };
42034543Smarks 	int error;
42044543Smarks 
42054543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42064543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
42074543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
42084543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
42095331Samw 	zc.zc_share.z_sharetype = operation;
42104543Smarks 	zc.zc_share.z_sharemax = sharemax;
42114543Smarks 
42124543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
42134543Smarks 	return (error);
42144543Smarks }
4215