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 /*
233363Sgw25295  * Copyright 2007 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>
39789Sahrens #include <zone.h>
402082Seschrock #include <fcntl.h>
41789Sahrens #include <sys/mntent.h>
42789Sahrens #include <sys/mnttab.h>
431294Slling #include <sys/mount.h>
444543Smarks #include <sys/avl.h>
454543Smarks #include <priv.h>
464543Smarks #include <pwd.h>
474543Smarks #include <grp.h>
484543Smarks #include <stddef.h>
494543Smarks #include <ucred.h>
50789Sahrens 
51789Sahrens #include <sys/spa.h>
52789Sahrens #include <sys/zio.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 
614490Svb160487 static int create_parents(libzfs_handle_t *, char *, int);
624007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
634007Smmusante 
64789Sahrens /*
65789Sahrens  * Given a single type (not a mask of types), return the type in a human
66789Sahrens  * readable form.
67789Sahrens  */
68789Sahrens const char *
69789Sahrens zfs_type_to_name(zfs_type_t type)
70789Sahrens {
71789Sahrens 	switch (type) {
72789Sahrens 	case ZFS_TYPE_FILESYSTEM:
73789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
74789Sahrens 	case ZFS_TYPE_SNAPSHOT:
75789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
76789Sahrens 	case ZFS_TYPE_VOLUME:
77789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
78789Sahrens 	}
79789Sahrens 
80789Sahrens 	return (NULL);
81789Sahrens }
82789Sahrens 
83789Sahrens /*
84789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
85789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
86789Sahrens  * We guess what the type would have been based on the path and the mask of
87789Sahrens  * acceptable types.
88789Sahrens  */
89789Sahrens static const char *
90789Sahrens path_to_str(const char *path, int types)
91789Sahrens {
92789Sahrens 	/*
93789Sahrens 	 * When given a single type, always report the exact type.
94789Sahrens 	 */
95789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
96789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
97789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
98789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
99789Sahrens 	if (types == ZFS_TYPE_VOLUME)
100789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
101789Sahrens 
102789Sahrens 	/*
103789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
104789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
105789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
106789Sahrens 	 * snapshot attribute and try again.
107789Sahrens 	 */
108789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
109789Sahrens 		if (strchr(path, '@') != NULL)
110789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
111789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
112789Sahrens 	}
113789Sahrens 
114789Sahrens 
115789Sahrens 	/*
116789Sahrens 	 * The user has requested either filesystems or volumes.
117789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
118789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
119789Sahrens 	 */
120789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
121789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
122789Sahrens 
123789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
124789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
125789Sahrens }
126789Sahrens 
127789Sahrens /*
128789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
129789Sahrens  * provide a more meaningful error message.  We place a more useful message in
130789Sahrens  * 'buf' detailing exactly why the name was not valid.
131789Sahrens  */
132789Sahrens static int
1332082Seschrock zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type)
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 
2062082Seschrock 	return (-1);
207789Sahrens }
208789Sahrens 
209789Sahrens int
210789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
211789Sahrens {
2122082Seschrock 	return (zfs_validate_name(NULL, name, type));
213789Sahrens }
214789Sahrens 
215789Sahrens /*
2162676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2172676Seschrock  * properties into a separate nvlist.
2182676Seschrock  */
2194217Seschrock static nvlist_t *
2204217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2212676Seschrock {
2222676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2232676Seschrock 	nvpair_t *elem;
2242676Seschrock 	nvlist_t *propval;
2254217Seschrock 	nvlist_t *nvl;
2264217Seschrock 
2274217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2284217Seschrock 		(void) no_memory(hdl);
2294217Seschrock 		return (NULL);
2304217Seschrock 	}
2312676Seschrock 
2322676Seschrock 	elem = NULL;
2334217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2342676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2352676Seschrock 			continue;
2362676Seschrock 
2372676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2384217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2394217Seschrock 			nvlist_free(nvl);
2404217Seschrock 			(void) no_memory(hdl);
2414217Seschrock 			return (NULL);
2424217Seschrock 		}
2432676Seschrock 	}
2442676Seschrock 
2454217Seschrock 	return (nvl);
2462676Seschrock }
2472676Seschrock 
2482676Seschrock /*
249789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
250789Sahrens  */
251789Sahrens static int
252789Sahrens get_stats(zfs_handle_t *zhp)
253789Sahrens {
254789Sahrens 	zfs_cmd_t zc = { 0 };
2552676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2564217Seschrock 	nvlist_t *allprops, *userprops;
257789Sahrens 
258789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
259789Sahrens 
2602676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
2612082Seschrock 		return (-1);
2621356Seschrock 
2632082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
2641356Seschrock 		if (errno == ENOMEM) {
2652676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2662676Seschrock 				zcmd_free_nvlists(&zc);
2672082Seschrock 				return (-1);
2682676Seschrock 			}
2691356Seschrock 		} else {
2702676Seschrock 			zcmd_free_nvlists(&zc);
2711356Seschrock 			return (-1);
2721356Seschrock 		}
2731356Seschrock 	}
274789Sahrens 
2752885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
276789Sahrens 
2772676Seschrock 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
2781544Seschrock 
2794217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
2802676Seschrock 		zcmd_free_nvlists(&zc);
2812082Seschrock 		return (-1);
2822082Seschrock 	}
283789Sahrens 
2842676Seschrock 	zcmd_free_nvlists(&zc);
2852676Seschrock 
2864217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
2874217Seschrock 		nvlist_free(allprops);
2882676Seschrock 		return (-1);
2894217Seschrock 	}
2904217Seschrock 
2914217Seschrock 	nvlist_free(zhp->zfs_props);
2924217Seschrock 	nvlist_free(zhp->zfs_user_props);
2934217Seschrock 
2944217Seschrock 	zhp->zfs_props = allprops;
2954217Seschrock 	zhp->zfs_user_props = userprops;
2962082Seschrock 
297789Sahrens 	return (0);
298789Sahrens }
299789Sahrens 
300789Sahrens /*
301789Sahrens  * Refresh the properties currently stored in the handle.
302789Sahrens  */
303789Sahrens void
304789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
305789Sahrens {
306789Sahrens 	(void) get_stats(zhp);
307789Sahrens }
308789Sahrens 
309789Sahrens /*
310789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
311789Sahrens  * zfs_iter_* to create child handles on the fly.
312789Sahrens  */
313789Sahrens zfs_handle_t *
3142082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
315789Sahrens {
3162082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3174543Smarks 	char *logstr;
3182082Seschrock 
3192082Seschrock 	if (zhp == NULL)
3202082Seschrock 		return (NULL);
3212082Seschrock 
3222082Seschrock 	zhp->zfs_hdl = hdl;
323789Sahrens 
3244543Smarks 	/*
3254543Smarks 	 * Preserve history log string.
3264543Smarks 	 * any changes performed here will be
3274543Smarks 	 * logged as an internal event.
3284543Smarks 	 */
3294543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3304543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3311758Sahrens top:
332789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
333789Sahrens 
334789Sahrens 	if (get_stats(zhp) != 0) {
3354543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
336789Sahrens 		free(zhp);
337789Sahrens 		return (NULL);
338789Sahrens 	}
339789Sahrens 
3401758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
3411758Sahrens 		zfs_cmd_t zc = { 0 };
3421758Sahrens 
3431758Sahrens 		/*
3441758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
3451758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
3461758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
3471758Sahrens 		 * can't be mounted.  However, it could also be that we
3481758Sahrens 		 * have crashed in the middle of one of those
3491758Sahrens 		 * operations, in which case we need to get rid of the
3501758Sahrens 		 * inconsistent state.  We do that by either rolling
3511758Sahrens 		 * back to the previous snapshot (which will fail if
3521758Sahrens 		 * there is none), or destroying the filesystem.  Note
3531758Sahrens 		 * that if we are still in the middle of an active
3541758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
3551758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
3561758Sahrens 		 */
3571758Sahrens 
3581758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3591758Sahrens 
3602885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
3612082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
3621758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
3631758Sahrens 		} else {
3641758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
3651758Sahrens 		}
3661758Sahrens 
3671758Sahrens 		/* If we can successfully roll it back, reget the stats */
3682082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
3691758Sahrens 			goto top;
3701758Sahrens 		/*
3711758Sahrens 		 * If we can sucessfully destroy it, pretend that it
3721758Sahrens 		 * never existed.
3731758Sahrens 		 */
3742082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
3754543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3761758Sahrens 			free(zhp);
3771758Sahrens 			errno = ENOENT;
3781758Sahrens 			return (NULL);
3791758Sahrens 		}
3801758Sahrens 	}
3811758Sahrens 
382789Sahrens 	/*
383789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
384789Sahrens 	 * the high-level type.
385789Sahrens 	 */
3862885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
3872885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
3882885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
3892885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
3902885Sahrens 	else
3912885Sahrens 		abort();
3922885Sahrens 
393789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
394789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
395789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
396789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
397789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
398789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
399789Sahrens 	else
4002082Seschrock 		abort();	/* we should never see any other types */
401789Sahrens 
4024543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
403789Sahrens 	return (zhp);
404789Sahrens }
405789Sahrens 
406789Sahrens /*
407789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
408789Sahrens  * argument is a mask of acceptable types.  The function will print an
409789Sahrens  * appropriate error message and return NULL if it can't be opened.
410789Sahrens  */
411789Sahrens zfs_handle_t *
4122082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
413789Sahrens {
414789Sahrens 	zfs_handle_t *zhp;
4152082Seschrock 	char errbuf[1024];
4162082Seschrock 
4172082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4182082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
419789Sahrens 
420789Sahrens 	/*
4212082Seschrock 	 * Validate the name before we even try to open it.
422789Sahrens 	 */
4232082Seschrock 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_ANY)) {
4242082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4252082Seschrock 		    "invalid dataset name"));
4262082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
427789Sahrens 		return (NULL);
428789Sahrens 	}
429789Sahrens 
430789Sahrens 	/*
431789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
432789Sahrens 	 */
433789Sahrens 	errno = 0;
4342082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
4353237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
436789Sahrens 		return (NULL);
437789Sahrens 	}
438789Sahrens 
439789Sahrens 	if (!(types & zhp->zfs_type)) {
4402082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4412142Seschrock 		zfs_close(zhp);
442789Sahrens 		return (NULL);
443789Sahrens 	}
444789Sahrens 
445789Sahrens 	return (zhp);
446789Sahrens }
447789Sahrens 
448789Sahrens /*
449789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
450789Sahrens  */
451789Sahrens void
452789Sahrens zfs_close(zfs_handle_t *zhp)
453789Sahrens {
454789Sahrens 	if (zhp->zfs_mntopts)
455789Sahrens 		free(zhp->zfs_mntopts);
4562676Seschrock 	nvlist_free(zhp->zfs_props);
4572676Seschrock 	nvlist_free(zhp->zfs_user_props);
458789Sahrens 	free(zhp);
459789Sahrens }
460789Sahrens 
461789Sahrens /*
462789Sahrens  * Given a numeric suffix, convert the value into a number of bits that the
463789Sahrens  * resulting value must be shifted.
464789Sahrens  */
465789Sahrens static int
4662082Seschrock str2shift(libzfs_handle_t *hdl, const char *buf)
467789Sahrens {
468789Sahrens 	const char *ends = "BKMGTPEZ";
469789Sahrens 	int i;
470789Sahrens 
471789Sahrens 	if (buf[0] == '\0')
472789Sahrens 		return (0);
473789Sahrens 	for (i = 0; i < strlen(ends); i++) {
474789Sahrens 		if (toupper(buf[0]) == ends[i])
475789Sahrens 			break;
476789Sahrens 	}
477789Sahrens 	if (i == strlen(ends)) {
4782082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4792082Seschrock 		    "invalid numeric suffix '%s'"), buf);
480789Sahrens 		return (-1);
481789Sahrens 	}
482789Sahrens 
483789Sahrens 	/*
484789Sahrens 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
485789Sahrens 	 * allow 'BB' - that's just weird.
486789Sahrens 	 */
487789Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
4882082Seschrock 	    toupper(buf[0]) != 'B'))
489789Sahrens 		return (10*i);
4902082Seschrock 
4912082Seschrock 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4922082Seschrock 	    "invalid numeric suffix '%s'"), buf);
493789Sahrens 	return (-1);
494789Sahrens }
495789Sahrens 
496789Sahrens /*
497789Sahrens  * Convert a string of the form '100G' into a real number.  Used when setting
498789Sahrens  * properties or creating a volume.  'buf' is used to place an extended error
499789Sahrens  * message for the caller to use.
500789Sahrens  */
501789Sahrens static int
5022082Seschrock nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
503789Sahrens {
504789Sahrens 	char *end;
505789Sahrens 	int shift;
506789Sahrens 
507789Sahrens 	*num = 0;
508789Sahrens 
509789Sahrens 	/* Check to see if this looks like a number.  */
510789Sahrens 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
5112082Seschrock 		if (hdl)
5122082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5132082Seschrock 			    "bad numeric value '%s'"), value);
514789Sahrens 		return (-1);
515789Sahrens 	}
516789Sahrens 
517789Sahrens 	/* Rely on stroll() to process the numeric portion.  */
518789Sahrens 	errno = 0;
519789Sahrens 	*num = strtoll(value, &end, 10);
520789Sahrens 
521789Sahrens 	/*
522789Sahrens 	 * Check for ERANGE, which indicates that the value is too large to fit
523789Sahrens 	 * in a 64-bit value.
524789Sahrens 	 */
525789Sahrens 	if (errno == ERANGE) {
5262082Seschrock 		if (hdl)
5272082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5282082Seschrock 			    "numeric value is too large"));
529789Sahrens 		return (-1);
530789Sahrens 	}
531789Sahrens 
532789Sahrens 	/*
533789Sahrens 	 * If we have a decimal value, then do the computation with floating
534789Sahrens 	 * point arithmetic.  Otherwise, use standard arithmetic.
535789Sahrens 	 */
536789Sahrens 	if (*end == '.') {
537789Sahrens 		double fval = strtod(value, &end);
538789Sahrens 
5392082Seschrock 		if ((shift = str2shift(hdl, end)) == -1)
540789Sahrens 			return (-1);
541789Sahrens 
542789Sahrens 		fval *= pow(2, shift);
543789Sahrens 
544789Sahrens 		if (fval > UINT64_MAX) {
5452082Seschrock 			if (hdl)
5462082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5472082Seschrock 				    "numeric value is too large"));
548789Sahrens 			return (-1);
549789Sahrens 		}
550789Sahrens 
551789Sahrens 		*num = (uint64_t)fval;
552789Sahrens 	} else {
5532082Seschrock 		if ((shift = str2shift(hdl, end)) == -1)
554789Sahrens 			return (-1);
555789Sahrens 
556789Sahrens 		/* Check for overflow */
557789Sahrens 		if (shift >= 64 || (*num << shift) >> shift != *num) {
5582082Seschrock 			if (hdl)
5592082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5602082Seschrock 				    "numeric value is too large"));
561789Sahrens 			return (-1);
562789Sahrens 		}
563789Sahrens 
564789Sahrens 		*num <<= shift;
565789Sahrens 	}
566789Sahrens 
567789Sahrens 	return (0);
568789Sahrens }
569789Sahrens 
570789Sahrens int
5712676Seschrock zfs_nicestrtonum(libzfs_handle_t *hdl, const char *str, uint64_t *val)
5722676Seschrock {
5732676Seschrock 	return (nicestrtonum(hdl, str, val));
5742676Seschrock }
5752676Seschrock 
5762676Seschrock /*
5772676Seschrock  * The prop_parse_*() functions are designed to allow flexibility in callers
5782676Seschrock  * when setting properties.  At the DSL layer, all properties are either 64-bit
5792676Seschrock  * numbers or strings.  We want the user to be able to ignore this fact and
5802676Seschrock  * specify properties as native values (boolean, for example) or as strings (to
5812676Seschrock  * simplify command line utilities).  This also handles converting index types
5822676Seschrock  * (compression, checksum, etc) from strings to their on-disk index.
5832676Seschrock  */
5842676Seschrock 
5852676Seschrock static int
5862676Seschrock prop_parse_boolean(libzfs_handle_t *hdl, nvpair_t *elem, uint64_t *val)
587789Sahrens {
5882676Seschrock 	uint64_t ret;
5892676Seschrock 
5902676Seschrock 	switch (nvpair_type(elem)) {
5912676Seschrock 	case DATA_TYPE_STRING:
5922676Seschrock 		{
5932676Seschrock 			char *value;
5943363Sgw25295 			verify(nvpair_value_string(elem, &value) == 0);
5952676Seschrock 
5962676Seschrock 			if (strcmp(value, "on") == 0) {
5972676Seschrock 				ret = 1;
5982676Seschrock 			} else if (strcmp(value, "off") == 0) {
5992676Seschrock 				ret = 0;
6002676Seschrock 			} else {
6012676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6022676Seschrock 				    "property '%s' must be 'on' or 'off'"),
6032676Seschrock 				    nvpair_name(elem));
6042676Seschrock 				return (-1);
6052676Seschrock 			}
6062676Seschrock 			break;
6072676Seschrock 		}
6082676Seschrock 
6092676Seschrock 	case DATA_TYPE_UINT64:
6102676Seschrock 		{
6113363Sgw25295 			verify(nvpair_value_uint64(elem, &ret) == 0);
6122676Seschrock 			if (ret > 1) {
6132676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6142676Seschrock 				    "'%s' must be a boolean value"),
6152676Seschrock 				    nvpair_name(elem));
6162676Seschrock 				return (-1);
6172676Seschrock 			}
6182676Seschrock 			break;
6192676Seschrock 		}
6202676Seschrock 
6212676Seschrock 	case DATA_TYPE_BOOLEAN_VALUE:
6222676Seschrock 		{
6232676Seschrock 			boolean_t value;
6243363Sgw25295 			verify(nvpair_value_boolean_value(elem, &value) == 0);
6252676Seschrock 			ret = value;
6262676Seschrock 			break;
6272676Seschrock 		}
6282676Seschrock 
6292676Seschrock 	default:
6302676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6312676Seschrock 		    "'%s' must be a boolean value"),
6322676Seschrock 		    nvpair_name(elem));
6332676Seschrock 		return (-1);
6342676Seschrock 	}
6352676Seschrock 
6362676Seschrock 	*val = ret;
6372676Seschrock 	return (0);
6382676Seschrock }
6392676Seschrock 
6402676Seschrock static int
6412676Seschrock prop_parse_number(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop,
6422676Seschrock     uint64_t *val)
6432676Seschrock {
6442676Seschrock 	uint64_t ret;
6452676Seschrock 	boolean_t isnone = B_FALSE;
6462676Seschrock 
6472676Seschrock 	switch (nvpair_type(elem)) {
6482676Seschrock 	case DATA_TYPE_STRING:
6492676Seschrock 		{
6502676Seschrock 			char *value;
6512676Seschrock 			(void) nvpair_value_string(elem, &value);
6522676Seschrock 			if (strcmp(value, "none") == 0) {
6532676Seschrock 				isnone = B_TRUE;
6542676Seschrock 				ret = 0;
6552676Seschrock 			} else if (nicestrtonum(hdl, value, &ret) != 0) {
6562676Seschrock 				return (-1);
6572676Seschrock 			}
6582676Seschrock 			break;
6592676Seschrock 		}
6602676Seschrock 
6612676Seschrock 	case DATA_TYPE_UINT64:
6622676Seschrock 		(void) nvpair_value_uint64(elem, &ret);
6632676Seschrock 		break;
6642676Seschrock 
6652676Seschrock 	default:
6662676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6672676Seschrock 		    "'%s' must be a number"),
6682676Seschrock 		    nvpair_name(elem));
6692676Seschrock 		return (-1);
6702676Seschrock 	}
6712676Seschrock 
6722676Seschrock 	/*
6732676Seschrock 	 * Quota special: force 'none' and don't allow 0.
6742676Seschrock 	 */
6752676Seschrock 	if (ret == 0 && !isnone && prop == ZFS_PROP_QUOTA) {
6762676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6772676Seschrock 		    "use 'none' to disable quota"));
6782676Seschrock 		return (-1);
6792676Seschrock 	}
6802676Seschrock 
6812676Seschrock 	*val = ret;
6822676Seschrock 	return (0);
6832676Seschrock }
6842676Seschrock 
6852676Seschrock static int
6862676Seschrock prop_parse_index(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop,
6872676Seschrock     uint64_t *val)
6882676Seschrock {
6892676Seschrock 	char *propname = nvpair_name(elem);
6902676Seschrock 	char *value;
6912676Seschrock 
6922676Seschrock 	if (nvpair_type(elem) != DATA_TYPE_STRING) {
6932676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6942676Seschrock 		    "'%s' must be a string"), propname);
6952676Seschrock 		return (-1);
6962676Seschrock 	}
6972676Seschrock 
6982676Seschrock 	(void) nvpair_value_string(elem, &value);
6992676Seschrock 
7002676Seschrock 	if (zfs_prop_string_to_index(prop, value, val) != 0) {
7012676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7022676Seschrock 		    "'%s' must be one of '%s'"), propname,
7032676Seschrock 		    zfs_prop_values(prop));
7042676Seschrock 		return (-1);
7052676Seschrock 	}
7062676Seschrock 
7072676Seschrock 	return (0);
708789Sahrens }
709789Sahrens 
710789Sahrens /*
7113912Slling  * Check if the bootfs name has the same pool name as it is set to.
7123912Slling  * Assuming bootfs is a valid dataset name.
7133912Slling  */
7143912Slling static boolean_t
7153912Slling bootfs_poolname_valid(char *pool, char *bootfs)
7163912Slling {
7173912Slling 	char ch, *pname;
7183912Slling 
7193912Slling 	/* get the pool name from the bootfs name */
7203912Slling 	pname = bootfs;
7213912Slling 	while (*bootfs && !isspace(*bootfs) && *bootfs != '/')
7223912Slling 		bootfs++;
7233912Slling 
7243912Slling 	ch = *bootfs;
7253912Slling 	*bootfs = 0;
7263912Slling 
7273912Slling 	if (strcmp(pool, pname) == 0) {
7283912Slling 		*bootfs = ch;
7293912Slling 		return (B_TRUE);
7303912Slling 	}
7313912Slling 
7323912Slling 	*bootfs = ch;
7333912Slling 	return (B_FALSE);
7343912Slling }
7353912Slling 
7363912Slling /*
7372676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
7382676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
7392676Seschrock  * strings.
740789Sahrens  */
7413912Slling nvlist_t *
7423912Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, char *pool_name,
7433912Slling     nvlist_t *nvl, uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
744789Sahrens {
7452676Seschrock 	nvpair_t *elem;
7462676Seschrock 	const char *propname;
7472676Seschrock 	zfs_prop_t prop;
7482676Seschrock 	uint64_t intval;
7492676Seschrock 	char *strval;
7502676Seschrock 	nvlist_t *ret;
7513912Slling 	int isuser;
7522676Seschrock 
7532676Seschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
7542676Seschrock 		(void) no_memory(hdl);
7552676Seschrock 		return (NULL);
7562676Seschrock 	}
7572676Seschrock 
7582676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
7592676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7603413Smmusante 		    "snapshot properties cannot be modified"));
7612676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
7622676Seschrock 		goto error;
763789Sahrens 	}
764789Sahrens 
7652676Seschrock 	elem = NULL;
7662676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
7672676Seschrock 		propname = nvpair_name(elem);
7682676Seschrock 
7692676Seschrock 		/*
7702676Seschrock 		 * Make sure this property is valid and applies to this type.
7712676Seschrock 		 */
7723912Slling 		if ((prop = zfs_name_to_prop_common(propname, type))
7733912Slling 		    == ZFS_PROP_INVAL) {
7743912Slling 			isuser = zfs_prop_user(propname);
7753912Slling 			if (!isuser || (isuser && (type & ZFS_TYPE_POOL))) {
7762676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7772676Seschrock 				    "invalid property '%s'"),
7782676Seschrock 				    propname);
7792676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7802676Seschrock 				goto error;
7812676Seschrock 			} else {
7822676Seschrock 				/*
7832676Seschrock 				 * If this is a user property, make sure it's a
7842676Seschrock 				 * string, and that it's less than
7852676Seschrock 				 * ZAP_MAXNAMELEN.
7862676Seschrock 				 */
7872676Seschrock 				if (nvpair_type(elem) != DATA_TYPE_STRING) {
7882676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7892676Seschrock 					    "'%s' must be a string"),
7902676Seschrock 					    propname);
7912676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7922676Seschrock 					    errbuf);
7932676Seschrock 					goto error;
7942676Seschrock 				}
7952676Seschrock 
7962676Seschrock 				if (strlen(nvpair_name(elem)) >=
7972676Seschrock 				    ZAP_MAXNAMELEN) {
7982676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7992676Seschrock 					    "property name '%s' is too long"),
8002676Seschrock 					    propname);
8012676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8022676Seschrock 					    errbuf);
8032676Seschrock 					goto error;
8042676Seschrock 				}
8052676Seschrock 			}
8062676Seschrock 
8072676Seschrock 			(void) nvpair_value_string(elem, &strval);
8082676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
8092676Seschrock 				(void) no_memory(hdl);
8102676Seschrock 				goto error;
8112676Seschrock 			}
8122676Seschrock 			continue;
813789Sahrens 		}
8142676Seschrock 
8152676Seschrock 		/*
8162676Seschrock 		 * Normalize the name, to get rid of shorthand abbrevations.
8172676Seschrock 		 */
8182676Seschrock 		propname = zfs_prop_to_name(prop);
8192676Seschrock 
8202676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
8212676Seschrock 			zfs_error_aux(hdl,
8222676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
8232676Seschrock 			    "apply to datasets of this type"), propname);
8242676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8252676Seschrock 			goto error;
8262676Seschrock 		}
8272676Seschrock 
8282676Seschrock 		if (zfs_prop_readonly(prop) &&
8292676Seschrock 		    (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) {
8302676Seschrock 			zfs_error_aux(hdl,
8312676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
8322676Seschrock 			    propname);
8332676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
8342676Seschrock 			goto error;
8352676Seschrock 		}
8362676Seschrock 
8372676Seschrock 		/*
8382676Seschrock 		 * Convert any properties to the internal DSL value types.
8392676Seschrock 		 */
8402676Seschrock 		strval = NULL;
8412676Seschrock 		switch (zfs_prop_get_type(prop)) {
8422676Seschrock 		case prop_type_boolean:
8432676Seschrock 			if (prop_parse_boolean(hdl, elem, &intval) != 0) {
8442676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8452676Seschrock 				goto error;
8462676Seschrock 			}
847789Sahrens 			break;
8482676Seschrock 
8492676Seschrock 		case prop_type_string:
8502676Seschrock 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
8512082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8522676Seschrock 				    "'%s' must be a string"),
8532676Seschrock 				    propname);
8542676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8552676Seschrock 				goto error;
856789Sahrens 			}
8572676Seschrock 			(void) nvpair_value_string(elem, &strval);
8582676Seschrock 			if (strlen(strval) >= ZFS_MAXPROPLEN) {
8592082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8602676Seschrock 				    "'%s' is too long"), propname);
8612676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8622676Seschrock 				goto error;
8632676Seschrock 			}
8642676Seschrock 			break;
8652676Seschrock 
8662676Seschrock 		case prop_type_number:
8672676Seschrock 			if (prop_parse_number(hdl, elem, prop, &intval) != 0) {
8682676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8692676Seschrock 				goto error;
8702676Seschrock 			}
8712676Seschrock 			break;
8722676Seschrock 
8732676Seschrock 		case prop_type_index:
8742676Seschrock 			if (prop_parse_index(hdl, elem, prop, &intval) != 0) {
8752676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8762676Seschrock 				goto error;
877789Sahrens 			}
878789Sahrens 			break;
879789Sahrens 
8802676Seschrock 		default:
8812676Seschrock 			abort();
8822676Seschrock 		}
8832676Seschrock 
8842676Seschrock 		/*
8852676Seschrock 		 * Add the result to our return set of properties.
8862676Seschrock 		 */
8872676Seschrock 		if (strval) {
8882676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
8892676Seschrock 				(void) no_memory(hdl);
8902676Seschrock 				goto error;
891789Sahrens 			}
8922676Seschrock 		} else if (nvlist_add_uint64(ret, propname, intval) != 0) {
8932676Seschrock 			(void) no_memory(hdl);
8942676Seschrock 			goto error;
8952676Seschrock 		}
8962676Seschrock 
8972676Seschrock 		/*
8982676Seschrock 		 * Perform some additional checks for specific properties.
8992676Seschrock 		 */
9002676Seschrock 		switch (prop) {
9014577Sahrens 		case ZFS_PROP_VERSION:
9024577Sahrens 		{
9034577Sahrens 			int version;
9044577Sahrens 
9054577Sahrens 			if (zhp == NULL)
9064577Sahrens 				break;
9074577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
9084577Sahrens 			if (intval < version) {
9094577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9104577Sahrens 				    "Can not downgrade; already at version %u"),
9114577Sahrens 				    version);
9124577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9134577Sahrens 				goto error;
9144577Sahrens 			}
9154577Sahrens 			break;
9164577Sahrens 		}
9174577Sahrens 
9182676Seschrock 		case ZFS_PROP_RECORDSIZE:
9192676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
9202676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
9212676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
9222676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
9232082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9242676Seschrock 				    "'%s' must be power of 2 from %u "
9252676Seschrock 				    "to %uk"), propname,
9262676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
9272676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
9282676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9292676Seschrock 				goto error;
930789Sahrens 			}
931789Sahrens 			break;
932789Sahrens 
9333126Sahl 		case ZFS_PROP_SHAREISCSI:
9343126Sahl 			if (strcmp(strval, "off") != 0 &&
9353126Sahl 			    strcmp(strval, "on") != 0 &&
9363126Sahl 			    strcmp(strval, "type=disk") != 0) {
9373126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9383126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
9393126Sahl 				    propname);
9403126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9413126Sahl 				goto error;
9423126Sahl 			}
9433126Sahl 
9443126Sahl 			break;
9453126Sahl 
9462676Seschrock 		case ZFS_PROP_MOUNTPOINT:
947*4778Srm160521 		{
948*4778Srm160521 			namecheck_err_t why;
949*4778Srm160521 
9502676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
9512676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9522676Seschrock 				break;
9532676Seschrock 
954*4778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
955*4778Srm160521 				switch (why) {
956*4778Srm160521 				case NAME_ERR_LEADING_SLASH:
957*4778Srm160521 					zfs_error_aux(hdl,
958*4778Srm160521 					    dgettext(TEXT_DOMAIN,
959*4778Srm160521 					    "'%s' must be an absolute path, "
960*4778Srm160521 					    "'none', or 'legacy'"), propname);
961*4778Srm160521 					break;
962*4778Srm160521 				case NAME_ERR_TOOLONG:
963*4778Srm160521 					zfs_error_aux(hdl,
964*4778Srm160521 					    dgettext(TEXT_DOMAIN,
965*4778Srm160521 					    "component of '%s' is too long"),
966*4778Srm160521 					    propname);
967*4778Srm160521 					break;
968*4778Srm160521 				}
9692676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9702676Seschrock 				goto error;
971789Sahrens 			}
972*4778Srm160521 		}
973*4778Srm160521 
9743126Sahl 			/*FALLTHRU*/
9753126Sahl 
9763126Sahl 		case ZFS_PROP_SHARENFS:
9773126Sahl 			/*
9783126Sahl 			 * For the mountpoint and sharenfs properties, check if
9793126Sahl 			 * it can be set in a global/non-global zone based on
9803126Sahl 			 * the zoned property value:
9813126Sahl 			 *
9823126Sahl 			 *		global zone	    non-global zone
9833126Sahl 			 * --------------------------------------------------
9843126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
9853126Sahl 			 *		sharenfs (no)	    sharenfs (no)
9863126Sahl 			 *
9873126Sahl 			 * zoned=off	mountpoint (yes)	N/A
9883126Sahl 			 *		sharenfs (yes)
9893126Sahl 			 */
9902676Seschrock 			if (zoned) {
9912676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
9922676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9932676Seschrock 					    "'%s' cannot be set on "
9942676Seschrock 					    "dataset in a non-global zone"),
9952676Seschrock 					    propname);
9962676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9972676Seschrock 					    errbuf);
9982676Seschrock 					goto error;
9992676Seschrock 				} else if (prop == ZFS_PROP_SHARENFS) {
10002676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10012676Seschrock 					    "'%s' cannot be set in "
10022676Seschrock 					    "a non-global zone"), propname);
10032676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
10042676Seschrock 					    errbuf);
10052676Seschrock 					goto error;
10062676Seschrock 				}
10072676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
10082676Seschrock 				/*
10092676Seschrock 				 * If zoned property is 'off', this must be in
10102676Seschrock 				 * a globle zone. If not, something is wrong.
10112676Seschrock 				 */
10122676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10132676Seschrock 				    "'%s' cannot be set while dataset "
10142676Seschrock 				    "'zoned' property is set"), propname);
10152676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
10162676Seschrock 				goto error;
10172676Seschrock 			}
10183126Sahl 
10194180Sdougm 			/*
10204180Sdougm 			 * At this point, it is legitimate to set the
10214180Sdougm 			 * property. Now we want to make sure that the
10224180Sdougm 			 * property value is valid if it is sharenfs.
10234180Sdougm 			 */
10244180Sdougm 			if (prop == ZFS_PROP_SHARENFS &&
10254217Seschrock 			    strcmp(strval, "on") != 0 &&
10264217Seschrock 			    strcmp(strval, "off") != 0) {
10274180Sdougm 
10284180Sdougm 				/*
10294180Sdougm 				 * Must be an NFS option string so
10304180Sdougm 				 * init the libshare in order to
10314180Sdougm 				 * enable the parser and then parse
10324180Sdougm 				 * the options. We use the control API
10334180Sdougm 				 * since we don't care about the
10344180Sdougm 				 * current configuration and don't
10354180Sdougm 				 * want the overhead of loading it
10364180Sdougm 				 * until we actually do something.
10374180Sdougm 				 */
10384180Sdougm 
10394217Seschrock 				if (zfs_init_libshare(hdl,
10404217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
10414217Seschrock 					/*
10424217Seschrock 					 * An error occurred so we can't do
10434217Seschrock 					 * anything
10444217Seschrock 					 */
10454217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10464217Seschrock 					    "'%s' cannot be set: problem "
10474217Seschrock 					    "in share initialization"),
10484217Seschrock 					    propname);
10494217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10504217Seschrock 					    errbuf);
10514217Seschrock 					goto error;
10524217Seschrock 				}
10534217Seschrock 
10544217Seschrock 				if (zfs_parse_options(strval, "nfs") != SA_OK) {
10554217Seschrock 					/*
10564217Seschrock 					 * There was an error in parsing so
10574217Seschrock 					 * deal with it by issuing an error
10584217Seschrock 					 * message and leaving after
10594217Seschrock 					 * uninitializing the the libshare
10604217Seschrock 					 * interface.
10614217Seschrock 					 */
10624217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10634217Seschrock 					    "'%s' cannot be set to invalid "
10644217Seschrock 					    "options"), propname);
10654217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10664217Seschrock 					    errbuf);
10674217Seschrock 					zfs_uninit_libshare(hdl);
10684217Seschrock 					goto error;
10694217Seschrock 				}
10704180Sdougm 				zfs_uninit_libshare(hdl);
10714180Sdougm 			}
10724180Sdougm 
10733126Sahl 			break;
10743912Slling 
10754451Seschrock 		case ZPOOL_PROP_BOOTFS:
10763912Slling 			/*
10773912Slling 			 * bootfs property value has to be a dataset name and
10783912Slling 			 * the dataset has to be in the same pool as it sets to.
10793912Slling 			 */
10803912Slling 			if (strval[0] != '\0' && (!zfs_name_valid(strval,
10813912Slling 			    ZFS_TYPE_FILESYSTEM) || !bootfs_poolname_valid(
10823912Slling 			    pool_name, strval))) {
10833912Slling 
10843912Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
10853912Slling 				    "is an invalid name"), strval);
10863912Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
10873912Slling 				goto error;
10883912Slling 			}
10893912Slling 			break;
10902676Seschrock 		}
10912676Seschrock 
10922676Seschrock 		/*
10932676Seschrock 		 * For changes to existing volumes, we have some additional
10942676Seschrock 		 * checks to enforce.
10952676Seschrock 		 */
10962676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
10972676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
10982676Seschrock 			    ZFS_PROP_VOLSIZE);
10992676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
11002676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
11012676Seschrock 			char buf[64];
11022676Seschrock 
11032676Seschrock 			switch (prop) {
11042676Seschrock 			case ZFS_PROP_RESERVATION:
11052676Seschrock 				if (intval > volsize) {
11062676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11072676Seschrock 					    "'%s' is greater than current "
11082676Seschrock 					    "volume size"), propname);
11092676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11102676Seschrock 					    errbuf);
11112676Seschrock 					goto error;
11122676Seschrock 				}
11132676Seschrock 				break;
11142676Seschrock 
11152676Seschrock 			case ZFS_PROP_VOLSIZE:
11162676Seschrock 				if (intval % blocksize != 0) {
11172676Seschrock 					zfs_nicenum(blocksize, buf,
11182676Seschrock 					    sizeof (buf));
11192676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11202676Seschrock 					    "'%s' must be a multiple of "
11212676Seschrock 					    "volume block size (%s)"),
11222676Seschrock 					    propname, buf);
11232676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11242676Seschrock 					    errbuf);
11252676Seschrock 					goto error;
11262676Seschrock 				}
11272676Seschrock 
11282676Seschrock 				if (intval == 0) {
11292676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11302676Seschrock 					    "'%s' cannot be zero"),
11312676Seschrock 					    propname);
11322676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11332676Seschrock 					    errbuf);
11342676Seschrock 					goto error;
1135789Sahrens 				}
11363126Sahl 				break;
1137789Sahrens 			}
1138789Sahrens 		}
1139789Sahrens 	}
1140789Sahrens 
11412676Seschrock 	/*
11422676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
11432676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
11442676Seschrock 	 */
11452676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
11462676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
11472676Seschrock 	    &intval) == 0) {
11482676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
11492676Seschrock 		    ZFS_PROP_VOLSIZE);
11502676Seschrock 		uint64_t old_reservation = zfs_prop_get_int(zhp,
11512676Seschrock 		    ZFS_PROP_RESERVATION);
11522676Seschrock 		uint64_t new_reservation;
11532676Seschrock 
11542676Seschrock 		if (old_volsize == old_reservation &&
11552676Seschrock 		    nvlist_lookup_uint64(ret,
11562676Seschrock 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
11572676Seschrock 		    &new_reservation) != 0) {
11582676Seschrock 			if (nvlist_add_uint64(ret,
11592676Seschrock 			    zfs_prop_to_name(ZFS_PROP_RESERVATION),
11602676Seschrock 			    intval) != 0) {
11612676Seschrock 				(void) no_memory(hdl);
11622676Seschrock 				goto error;
11632676Seschrock 			}
11642676Seschrock 		}
11652676Seschrock 	}
11662676Seschrock 
11672676Seschrock 	return (ret);
11682676Seschrock 
11692676Seschrock error:
11702676Seschrock 	nvlist_free(ret);
11712676Seschrock 	return (NULL);
1172789Sahrens }
1173789Sahrens 
11744543Smarks static int
11754543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
11764543Smarks     uint64_t *ret_who)
11774543Smarks {
11784543Smarks 	struct passwd *pwd;
11794543Smarks 	struct group *grp;
11804543Smarks 	uid_t id;
11814543Smarks 
11824543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
11834543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
11844543Smarks 		*ret_who = -1;
11854543Smarks 		return (0);
11864543Smarks 	}
11874543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
11884543Smarks 		return (EZFS_BADWHO);
11894543Smarks 
11904543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
11914543Smarks 	    strcmp(who, "everyone") == 0) {
11924543Smarks 		*ret_who = -1;
11934543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
11944543Smarks 		return (0);
11954543Smarks 	}
11964543Smarks 
11974543Smarks 	pwd = getpwnam(who);
11984543Smarks 	grp = getgrnam(who);
11994543Smarks 
12004543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
12014543Smarks 		*ret_who = pwd->pw_uid;
12024543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
12034543Smarks 		*ret_who = grp->gr_gid;
12044543Smarks 	} else if (pwd) {
12054543Smarks 		*ret_who = pwd->pw_uid;
12064543Smarks 		*who_type = ZFS_DELEG_USER;
12074543Smarks 	} else if (grp) {
12084543Smarks 		*ret_who = grp->gr_gid;
12094543Smarks 		*who_type = ZFS_DELEG_GROUP;
12104543Smarks 	} else {
12114543Smarks 		char *end;
12124543Smarks 
12134543Smarks 		id = strtol(who, &end, 10);
12144543Smarks 		if (errno != 0 || *end != '\0') {
12154543Smarks 			return (EZFS_BADWHO);
12164543Smarks 		} else {
12174543Smarks 			*ret_who = id;
12184543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
12194543Smarks 				*who_type = ZFS_DELEG_USER;
12204543Smarks 		}
12214543Smarks 	}
12224543Smarks 
12234543Smarks 	return (0);
12244543Smarks }
12254543Smarks 
12264543Smarks static void
12274543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
12284543Smarks {
12294543Smarks 	if (perms_nvp != NULL) {
12304543Smarks 		verify(nvlist_add_nvlist(who_nvp,
12314543Smarks 		    name, perms_nvp) == 0);
12324543Smarks 	} else {
12334543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
12344543Smarks 	}
12354543Smarks }
12364543Smarks 
12374543Smarks static void
12384543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
12394543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
12404543Smarks     nvlist_t *sets_nvp)
12414543Smarks {
12424543Smarks 	boolean_t do_perms, do_sets;
12434543Smarks 	char name[ZFS_MAX_DELEG_NAME];
12444543Smarks 
12454543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
12464543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
12474543Smarks 
12484543Smarks 	if (!do_perms && !do_sets)
12494543Smarks 		do_perms = do_sets = B_TRUE;
12504543Smarks 
12514543Smarks 	if (do_perms) {
12524543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
12534543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
12544543Smarks 		    whostr : (void *)&whoid);
12554543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
12564543Smarks 	}
12574543Smarks 	if (do_sets) {
12584543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
12594543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
12604543Smarks 		    whostr : (void *)&whoid);
12614543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
12624543Smarks 	}
12634543Smarks }
12644543Smarks 
12654543Smarks static void
12664543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
12674543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
12684543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
12694543Smarks {
12704543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
12714543Smarks 		helper(who_type, whoid, whostr, 0,
12724543Smarks 		    who_nvp, perms_nvp, sets_nvp);
12734543Smarks 	} else {
12744543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
12754543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
12764543Smarks 			    who_nvp, perms_nvp, sets_nvp);
12774543Smarks 		}
12784543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
12794543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
12804543Smarks 			    who_nvp, perms_nvp, sets_nvp);
12814543Smarks 		}
12824543Smarks 	}
12834543Smarks }
12844543Smarks 
12854543Smarks /*
12864543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
12874543Smarks  *
12884543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
12894543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
12904543Smarks  * base attribute named stored in the dsl.
12914543Smarks  * Arguments:
12924543Smarks  *
12934543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
12944543Smarks  *           whostr may be null for everyone or create perms.
12954543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
12964543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
12974543Smarks  * perms:    comman separated list of permissions.  May be null if user
12984543Smarks  *           is requested to remove permissions by who.
12994543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
13004543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
13014543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
13024543Smarks  *           The output nvp will look something like this.
13034543Smarks  *              ul$1234 -> {create ; destroy }
13044543Smarks  *              Ul$1234 -> { @myset }
13054543Smarks  *              s-$@myset - { snapshot; checksum; compression }
13064543Smarks  */
13074543Smarks int
13084543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
13094543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
13104543Smarks {
13114543Smarks 	nvlist_t *who_nvp;
13124543Smarks 	nvlist_t *perms_nvp = NULL;
13134543Smarks 	nvlist_t *sets_nvp = NULL;
13144543Smarks 	char errbuf[1024];
13154543Smarks 	char *who_tok;
13164543Smarks 	int error;
13174543Smarks 
13184543Smarks 	*nvp = NULL;
13194543Smarks 
13204543Smarks 	if (perms) {
13214543Smarks 		/* Make sure permission string doesn't have an '=' sign in it */
13224543Smarks 		if (strchr(perms, '=') != NULL) {
13234543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
13244543Smarks 			    dgettext(TEXT_DOMAIN,
13254543Smarks 			    "permissions can't contain equal sign : '%s'"),
13264543Smarks 			    perms);
13274543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, errbuf));
13284543Smarks 		}
13294543Smarks 
13304543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
13314543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
13324543Smarks 			return (1);
13334543Smarks 		}
13344543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
13354543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
13364543Smarks 			nvlist_free(perms_nvp);
13374543Smarks 			return (1);
13384543Smarks 		}
13394543Smarks 	}
13404543Smarks 
13414543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
13424543Smarks 		if (perms_nvp)
13434543Smarks 			nvlist_free(perms_nvp);
13444543Smarks 		if (sets_nvp)
13454543Smarks 			nvlist_free(sets_nvp);
13464543Smarks 		return (1);
13474543Smarks 	}
13484543Smarks 
13494543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
13504543Smarks 		namecheck_err_t why;
13514543Smarks 		char what;
13524543Smarks 
13534543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
13544543Smarks 			switch (why) {
13554543Smarks 			case NAME_ERR_NO_AT:
13564543Smarks 				zfs_error_aux(zhp->zfs_hdl,
13574543Smarks 				    dgettext(TEXT_DOMAIN,
13584543Smarks 				    "set definition must begin with an '@' "
13594543Smarks 				    "character"));
13604543Smarks 			}
13614543Smarks 			return (zfs_error(zhp->zfs_hdl,
13624543Smarks 			    EZFS_BADPERMSET, whostr));
13634543Smarks 		}
13644543Smarks 	}
13654543Smarks 
13664543Smarks 	/*
13674543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
13684543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
13694543Smarks 	 * other sets_nvp will have only permssion set names in it.
13704543Smarks 	 */
13714543Smarks 
13724543Smarks 
13734543Smarks 	while (perms && *perms != '\0') {
13744543Smarks 		char *value;
13754543Smarks 		char *perm_name;
13764543Smarks 		nvlist_t *update_nvp;
13774543Smarks 		int  perm_num;
13784543Smarks 		char canonical_name[64];
13794543Smarks 		char *canonicalp = canonical_name;
13804543Smarks 
13814543Smarks 
13824543Smarks 		update_nvp = perms_nvp;
13834543Smarks 
13844543Smarks 		perm_num = getsubopt(&perms, zfs_deleg_perm_tab, &value);
13854543Smarks 		if (perm_num == -1) {
13864543Smarks 			zfs_prop_t prop;
13874543Smarks 
13884543Smarks 			prop = zfs_name_to_prop(value);
13894543Smarks 			if (prop != ZFS_PROP_INVAL) {
13904543Smarks 				(void) snprintf(canonical_name,
13914543Smarks 				    sizeof (canonical_name), "%s",
13924543Smarks 				    zfs_prop_to_name(prop));
13934543Smarks 				perm_num = getsubopt(&canonicalp,
13944543Smarks 				    zfs_deleg_perm_tab, &value);
13954543Smarks 			}
13964543Smarks 		}
13974543Smarks 		if (perm_num != -1) {
13984543Smarks 			perm_name = zfs_deleg_perm_tab[perm_num];
13994543Smarks 		} else {  /* check and see if permission is a named set */
14004543Smarks 			if (value[0] == '@') {
14014543Smarks 
14024543Smarks 				/*
14034543Smarks 				 * make sure permssion set isn't defined
14044543Smarks 				 * in terms of itself. ie.
14054543Smarks 				 * @set1 = create,destroy,@set1
14064543Smarks 				 */
14074543Smarks 				if (who_type == ZFS_DELEG_NAMED_SET &&
14084543Smarks 				    strcmp(value, whostr) == 0) {
14094543Smarks 					nvlist_free(who_nvp);
14104543Smarks 					nvlist_free(perms_nvp);
14114543Smarks 					if (sets_nvp)
14124543Smarks 						nvlist_free(sets_nvp);
14134543Smarks 					(void) snprintf(errbuf,
14144543Smarks 					    sizeof (errbuf),
14154543Smarks 					    dgettext(TEXT_DOMAIN,
14164543Smarks 					    "Invalid permission %s"), value);
14174543Smarks 					return (zfs_error(zhp->zfs_hdl,
14184543Smarks 					    EZFS_PERMSET_CIRCULAR, errbuf));
14194543Smarks 				}
14204543Smarks 				update_nvp = sets_nvp;
14214543Smarks 				perm_name = value;
14224543Smarks 			} else {
14234543Smarks 				nvlist_free(who_nvp);
14244543Smarks 				nvlist_free(perms_nvp);
14254543Smarks 				if (sets_nvp)
14264543Smarks 					nvlist_free(sets_nvp);
14274543Smarks 				return (zfs_error(zhp->zfs_hdl,
14284543Smarks 				    EZFS_BADPERM, value));
14294543Smarks 			}
14304543Smarks 		}
14314543Smarks 		verify(nvlist_add_boolean(update_nvp, perm_name) == 0);
14324543Smarks 	}
14334543Smarks 
14344543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
14354543Smarks 		who_tok = strtok(whostr, ",");
14364543Smarks 		if (who_tok == NULL) {
14374543Smarks 			nvlist_free(who_nvp);
14384543Smarks 			nvlist_free(perms_nvp);
14394543Smarks 			if (sets_nvp)
14404543Smarks 				nvlist_free(sets_nvp);
14414543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14424543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
14434543Smarks 			    whostr);
14444543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
14454543Smarks 		}
14464543Smarks 	}
14474543Smarks 
14484543Smarks 	/*
14494543Smarks 	 * Now create the nvlist(s)
14504543Smarks 	 */
14514543Smarks 	do {
14524543Smarks 		uint64_t who_id;
14534543Smarks 
14544543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
14554543Smarks 		    &who_id);
14564543Smarks 		if (error) {
14574543Smarks 			nvlist_free(who_nvp);
14584543Smarks 			nvlist_free(perms_nvp);
14594543Smarks 			if (sets_nvp)
14604543Smarks 				nvlist_free(sets_nvp);
14614543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14624543Smarks 			    dgettext(TEXT_DOMAIN,
14634543Smarks 			    "Unable to determine uid/gid for "
14644543Smarks 			    "%s "), who_tok);
14654543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
14664543Smarks 		}
14674543Smarks 
14684543Smarks 		/*
14694543Smarks 		 * add entries for both local and descendent when required
14704543Smarks 		 */
14714543Smarks 
14724543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
14734543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
14744543Smarks 
14754543Smarks 	} while (who_tok = strtok(NULL, ","));
14764543Smarks 	*nvp = who_nvp;
14774543Smarks 	return (0);
14784543Smarks }
14794543Smarks 
14804543Smarks static int
14814543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
14824543Smarks {
14834543Smarks 	zfs_cmd_t zc = { 0 };
14844543Smarks 	int error;
14854543Smarks 	size_t sz;
14864543Smarks 	char errbuf[1024];
14874543Smarks 
14884543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
14894543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
14904543Smarks 	    zhp->zfs_name);
14914543Smarks 
14924543Smarks 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz))
14934543Smarks 		return (-1);
14944543Smarks 
14954543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14964543Smarks 	zc.zc_perm_action = unset;
14974543Smarks 
14984543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
14994543Smarks 	if (error && errno == ENOTSUP) {
15004543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
15014543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
15024543Smarks 		zcmd_free_nvlists(&zc);
15034543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
15044543Smarks 	} else if (error) {
15054543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
15064543Smarks 	}
15074543Smarks 	zcmd_free_nvlists(&zc);
15084543Smarks 
15094543Smarks 	return (error);
15104543Smarks }
15114543Smarks 
15124543Smarks int
15134543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
15144543Smarks {
15154543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
15164543Smarks }
15174543Smarks 
15184543Smarks int
15194543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
15204543Smarks {
15214543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
15224543Smarks }
15234543Smarks 
15244543Smarks static int
15254543Smarks perm_compare(const void *arg1, const void *arg2)
15264543Smarks {
15274543Smarks 	const zfs_perm_node_t *node1 = arg1;
15284543Smarks 	const zfs_perm_node_t *node2 = arg2;
15294543Smarks 	int ret;
15304543Smarks 
15314543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
15324543Smarks 
15334543Smarks 	if (ret > 0)
15344543Smarks 		return (1);
15354543Smarks 	if (ret < 0)
15364543Smarks 		return (-1);
15374543Smarks 	else
15384543Smarks 		return (0);
15394543Smarks }
15404543Smarks 
15414543Smarks static void
15424543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
15434543Smarks {
15444543Smarks 	zfs_perm_node_t *permnode;
15454543Smarks 	void *cookie;
15464543Smarks 
15474543Smarks 	cookie = NULL;
15484543Smarks 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL) {
15494543Smarks 		avl_remove(tree, permnode);
15504543Smarks 		free(permnode);
15514543Smarks 	}
15524543Smarks }
15534543Smarks 
15544543Smarks static void
15554543Smarks zfs_destroy_tree(avl_tree_t *tree)
15564543Smarks {
15574543Smarks 	zfs_allow_node_t *allownode;
15584543Smarks 	void *cookie;
15594543Smarks 
15604543Smarks 	cookie = NULL;
15614543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
15624543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
15634543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
15644543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
15654543Smarks 		avl_remove(tree, allownode);
15664543Smarks 		free(allownode);
15674543Smarks 	}
15684543Smarks }
15694543Smarks 
15704543Smarks void
15714543Smarks zfs_free_allows(zfs_allow_t *allow)
15724543Smarks {
15734543Smarks 	zfs_allow_t *allownext;
15744543Smarks 	zfs_allow_t *freeallow;
15754543Smarks 
15764543Smarks 	allownext = allow;
15774543Smarks 	while (allownext) {
15784543Smarks 		zfs_destroy_tree(&allownext->z_sets);
15794543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
15804543Smarks 		zfs_destroy_tree(&allownext->z_user);
15814543Smarks 		zfs_destroy_tree(&allownext->z_group);
15824543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
15834543Smarks 		freeallow = allownext;
15844543Smarks 		allownext = allownext->z_next;
15854543Smarks 		free(freeallow);
15864543Smarks 	}
15874543Smarks }
15884543Smarks 
15894543Smarks static zfs_allow_t *
15904543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
15914543Smarks {
15924543Smarks 	zfs_allow_t *ptree;
15934543Smarks 
15944543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
15954543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
15964543Smarks 		return (NULL);
15974543Smarks 	}
15984543Smarks 
15994543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
16004543Smarks 	avl_create(&ptree->z_sets,
16014543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
16024543Smarks 	    offsetof(zfs_allow_node_t, z_node));
16034543Smarks 	avl_create(&ptree->z_crperms,
16044543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
16054543Smarks 	    offsetof(zfs_allow_node_t, z_node));
16064543Smarks 	avl_create(&ptree->z_user,
16074543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
16084543Smarks 	    offsetof(zfs_allow_node_t, z_node));
16094543Smarks 	avl_create(&ptree->z_group,
16104543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
16114543Smarks 	    offsetof(zfs_allow_node_t, z_node));
16124543Smarks 	avl_create(&ptree->z_everyone,
16134543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
16144543Smarks 	    offsetof(zfs_allow_node_t, z_node));
16154543Smarks 
16164543Smarks 	if (prev)
16174543Smarks 		prev->z_next = ptree;
16184543Smarks 	ptree->z_next = NULL;
16194543Smarks 	return (ptree);
16204543Smarks }
16214543Smarks 
16224543Smarks /*
16234543Smarks  * Add permissions to the appropriate AVL permission tree.
16244543Smarks  * The appropriate tree may not be the requested tree.
16254543Smarks  * For example if ld indicates a local permission, but
16264543Smarks  * same permission also exists as a descendent permission
16274543Smarks  * then the permission will be removed from the descendent
16284543Smarks  * tree and add the the local+descendent tree.
16294543Smarks  */
16304543Smarks static int
16314543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
16324543Smarks     char *perm, char ld)
16334543Smarks {
16344543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
16354543Smarks 	zfs_perm_node_t *newnode;
16364543Smarks 	avl_index_t where, where2;
16374543Smarks 	avl_tree_t *tree, *altree;
16384543Smarks 
16394543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
16404543Smarks 
16414543Smarks 	if (ld == ZFS_DELEG_NA) {
16424543Smarks 		tree =  &allownode->z_localdescend;
16434543Smarks 		altree = &allownode->z_descend;
16444543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
16454543Smarks 		tree = &allownode->z_local;
16464543Smarks 		altree = &allownode->z_descend;
16474543Smarks 	} else {
16484543Smarks 		tree = &allownode->z_descend;
16494543Smarks 		altree = &allownode->z_local;
16504543Smarks 	}
16514543Smarks 	permnode = avl_find(tree, &pnode, &where);
16524543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
16534543Smarks 
16544543Smarks 	if (permnode2) {
16554543Smarks 		avl_remove(altree, permnode2);
16564543Smarks 		free(permnode2);
16574543Smarks 		if (permnode == NULL) {
16584543Smarks 			tree =  &allownode->z_localdescend;
16594543Smarks 		}
16604543Smarks 	}
16614543Smarks 
16624543Smarks 	/*
16634543Smarks 	 * Now insert new permission in either requested location
16644543Smarks 	 * local/descendent or into ld when perm will exist in both.
16654543Smarks 	 */
16664543Smarks 	if (permnode == NULL) {
16674543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
16684543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
16694543Smarks 			return (-1);
16704543Smarks 		}
16714543Smarks 		*newnode = pnode;
16724543Smarks 		avl_add(tree, newnode);
16734543Smarks 	}
16744543Smarks 	return (0);
16754543Smarks }
16764577Sahrens 
16774543Smarks /*
16784543Smarks  * Uggh, this is going to be a bit complicated.
16794543Smarks  * we have an nvlist coming out of the kernel that
16804543Smarks  * will indicate where the permission is set and then
16814543Smarks  * it will contain allow of the various "who's", and what
16824543Smarks  * their permissions are.  To further complicate this
16834543Smarks  * we will then have to coalesce the local,descendent
16844543Smarks  * and local+descendent permissions where appropriate.
16854543Smarks  * The kernel only knows about a permission as being local
16864543Smarks  * or descendent, but not both.
16874543Smarks  *
16884543Smarks  * In order to make this easier for zfs_main to deal with
16894543Smarks  * a series of AVL trees will be used to maintain
16904543Smarks  * all of this, primarily for sorting purposes as well
16914543Smarks  * as the ability to quickly locate a specific entry.
16924543Smarks  *
16934543Smarks  * What we end up with are tree's for sets, create perms,
16944543Smarks  * user, groups and everyone.  With each of those trees
16954543Smarks  * we have subtrees for local, descendent and local+descendent
16964543Smarks  * permissions.
16974543Smarks  */
16984543Smarks int
16994543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
17004543Smarks {
17014543Smarks 	zfs_cmd_t zc = { 0 };
17024543Smarks 	int error;
17034543Smarks 	nvlist_t *nvlist;
17044543Smarks 	nvlist_t *permnv, *sourcenv;
17054543Smarks 	nvpair_t *who_pair, *source_pair;
17064543Smarks 	nvpair_t *perm_pair;
17074543Smarks 	char errbuf[1024];
17084543Smarks 	zfs_allow_t *zallowp, *newallowp;
17094543Smarks 	char  ld;
17104543Smarks 	char *nvpname;
17114543Smarks 	uid_t	uid;
17124543Smarks 	gid_t	gid;
17134543Smarks 	avl_tree_t *tree;
17144543Smarks 	avl_index_t where;
17154543Smarks 
17164543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
17174543Smarks 
17184543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
17194543Smarks 		return (-1);
17204543Smarks 
17214543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
17224543Smarks 		if (errno == ENOMEM) {
17234543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
17244543Smarks 				zcmd_free_nvlists(&zc);
17254543Smarks 				return (-1);
17264543Smarks 			}
17274543Smarks 		} else if (errno == ENOTSUP) {
17284543Smarks 			zcmd_free_nvlists(&zc);
17294543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
17304543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
17314543Smarks 			return (zfs_error(zhp->zfs_hdl,
17324543Smarks 			    EZFS_BADVERSION, errbuf));
17334543Smarks 		} else {
17344543Smarks 			zcmd_free_nvlists(&zc);
17354543Smarks 			return (-1);
17364543Smarks 		}
17374543Smarks 	}
17384543Smarks 
17394543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
17404543Smarks 		zcmd_free_nvlists(&zc);
17414543Smarks 		return (-1);
17424543Smarks 	}
17434543Smarks 
17444543Smarks 	zcmd_free_nvlists(&zc);
17454543Smarks 
17464543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
17474543Smarks 
17484543Smarks 	if (source_pair == NULL) {
17494543Smarks 		*zfs_perms = NULL;
17504543Smarks 		return (0);
17514543Smarks 	}
17524543Smarks 
17534543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
17544543Smarks 	if (*zfs_perms == NULL) {
17554543Smarks 		return (0);
17564543Smarks 	}
17574543Smarks 
17584543Smarks 	zallowp = *zfs_perms;
17594543Smarks 
17604543Smarks 	for (;;) {
17614543Smarks 		struct passwd *pwd;
17624543Smarks 		struct group *grp;
17634543Smarks 		zfs_allow_node_t *allownode;
17644543Smarks 		zfs_allow_node_t  findallownode;
17654543Smarks 		zfs_allow_node_t *newallownode;
17664543Smarks 
17674543Smarks 		(void) strlcpy(zallowp->z_setpoint,
17684543Smarks 		    nvpair_name(source_pair),
17694543Smarks 		    sizeof (zallowp->z_setpoint));
17704543Smarks 
17714543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
17724543Smarks 			goto abort;
17734543Smarks 
17744543Smarks 		/*
17754543Smarks 		 * Make sure nvlist is composed correctly
17764543Smarks 		 */
17774543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
17784543Smarks 			goto abort;
17794543Smarks 		}
17804543Smarks 
17814543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
17824543Smarks 		if (who_pair == NULL) {
17834543Smarks 			goto abort;
17844543Smarks 		}
17854543Smarks 
17864543Smarks 		do {
17874543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
17884543Smarks 			if (error) {
17894543Smarks 				goto abort;
17904543Smarks 			}
17914543Smarks 
17924543Smarks 			/*
17934543Smarks 			 * First build up the key to use
17944543Smarks 			 * for looking up in the various
17954543Smarks 			 * who trees.
17964543Smarks 			 */
17974543Smarks 			ld = nvpair_name(who_pair)[1];
17984543Smarks 			nvpname = nvpair_name(who_pair);
17994543Smarks 			switch (nvpair_name(who_pair)[0]) {
18004543Smarks 			case ZFS_DELEG_USER:
18014543Smarks 			case ZFS_DELEG_USER_SETS:
18024543Smarks 				tree = &zallowp->z_user;
18034543Smarks 				uid = atol(&nvpname[3]);
18044543Smarks 				pwd = getpwuid(uid);
18054543Smarks 				(void) snprintf(findallownode.z_key,
18064543Smarks 				    sizeof (findallownode.z_key), "user %s",
18074543Smarks 				    (pwd) ? pwd->pw_name :
18084543Smarks 				    &nvpair_name(who_pair)[3]);
18094543Smarks 				break;
18104543Smarks 			case ZFS_DELEG_GROUP:
18114543Smarks 			case ZFS_DELEG_GROUP_SETS:
18124543Smarks 				tree = &zallowp->z_group;
18134543Smarks 				gid = atol(&nvpname[3]);
18144543Smarks 				grp = getgrgid(gid);
18154543Smarks 				(void) snprintf(findallownode.z_key,
18164543Smarks 				    sizeof (findallownode.z_key), "group %s",
18174543Smarks 				    (grp) ? grp->gr_name :
18184543Smarks 				    &nvpair_name(who_pair)[3]);
18194543Smarks 				break;
18204543Smarks 			case ZFS_DELEG_CREATE:
18214543Smarks 			case ZFS_DELEG_CREATE_SETS:
18224543Smarks 				tree = &zallowp->z_crperms;
18234543Smarks 				(void) strlcpy(findallownode.z_key, "",
18244543Smarks 				    sizeof (findallownode.z_key));
18254543Smarks 				break;
18264543Smarks 			case ZFS_DELEG_EVERYONE:
18274543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
18284543Smarks 				(void) snprintf(findallownode.z_key,
18294543Smarks 				    sizeof (findallownode.z_key), "everyone");
18304543Smarks 				tree = &zallowp->z_everyone;
18314543Smarks 				break;
18324543Smarks 			case ZFS_DELEG_NAMED_SET:
18334543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
18344543Smarks 				(void) snprintf(findallownode.z_key,
18354543Smarks 				    sizeof (findallownode.z_key), "%s",
18364543Smarks 				    &nvpair_name(who_pair)[3]);
18374543Smarks 				tree = &zallowp->z_sets;
18384543Smarks 				break;
18394543Smarks 			}
18404543Smarks 
18414543Smarks 			/*
18424543Smarks 			 * Place who in tree
18434543Smarks 			 */
18444543Smarks 			allownode = avl_find(tree, &findallownode, &where);
18454543Smarks 			if (allownode == NULL) {
18464543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
18474543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
18484543Smarks 					goto abort;
18494543Smarks 				}
18504543Smarks 				avl_create(&newallownode->z_localdescend,
18514543Smarks 				    perm_compare,
18524543Smarks 				    sizeof (zfs_perm_node_t),
18534543Smarks 				    offsetof(zfs_perm_node_t, z_node));
18544543Smarks 				avl_create(&newallownode->z_local,
18554543Smarks 				    perm_compare,
18564543Smarks 				    sizeof (zfs_perm_node_t),
18574543Smarks 				    offsetof(zfs_perm_node_t, z_node));
18584543Smarks 				avl_create(&newallownode->z_descend,
18594543Smarks 				    perm_compare,
18604543Smarks 				    sizeof (zfs_perm_node_t),
18614543Smarks 				    offsetof(zfs_perm_node_t, z_node));
18624543Smarks 				(void) strlcpy(newallownode->z_key,
18634543Smarks 				    findallownode.z_key,
18644543Smarks 				    sizeof (findallownode.z_key));
18654543Smarks 				avl_insert(tree, newallownode, where);
18664543Smarks 				allownode = newallownode;
18674543Smarks 			}
18684543Smarks 
18694543Smarks 			/*
18704543Smarks 			 * Now iterate over the permissions and
18714543Smarks 			 * place them in the appropriate local,
18724543Smarks 			 * descendent or local+descendent tree.
18734543Smarks 			 *
18744543Smarks 			 * The permissions are added to the tree
18754543Smarks 			 * via zfs_coalesce_perm().
18764543Smarks 			 */
18774543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
18784543Smarks 			if (perm_pair == NULL)
18794543Smarks 				goto abort;
18804543Smarks 			do {
18814543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
18824543Smarks 				    nvpair_name(perm_pair), ld) != 0)
18834543Smarks 					goto abort;
18844543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
18854543Smarks 			    perm_pair));
18864543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
18874543Smarks 
18884543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
18894543Smarks 		if (source_pair == NULL)
18904543Smarks 			break;
18914543Smarks 
18924543Smarks 		/*
18934543Smarks 		 * allocate another node from the link list of
18944543Smarks 		 * zfs_allow_t structures
18954543Smarks 		 */
18964543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
18974543Smarks 		    nvpair_name(source_pair));
18984543Smarks 		if (newallowp == NULL) {
18994543Smarks 			goto abort;
19004543Smarks 		}
19014543Smarks 		zallowp = newallowp;
19024543Smarks 	}
19034543Smarks 	nvlist_free(nvlist);
19044543Smarks 	return (0);
19054543Smarks abort:
19064543Smarks 	zfs_free_allows(*zfs_perms);
19074543Smarks 	nvlist_free(nvlist);
19084543Smarks 	return (-1);
19094543Smarks }
19104543Smarks 
1911789Sahrens /*
1912789Sahrens  * Given a property name and value, set the property for the given dataset.
1913789Sahrens  */
1914789Sahrens int
19152676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1916789Sahrens {
1917789Sahrens 	zfs_cmd_t zc = { 0 };
19182676Seschrock 	int ret = -1;
19192676Seschrock 	prop_changelist_t *cl = NULL;
19202082Seschrock 	char errbuf[1024];
19212082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
19222676Seschrock 	nvlist_t *nvl = NULL, *realprops;
19232676Seschrock 	zfs_prop_t prop;
19242082Seschrock 
19252082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
19262676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
19272082Seschrock 	    zhp->zfs_name);
19282082Seschrock 
19292676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
19302676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
19312676Seschrock 		(void) no_memory(hdl);
19322676Seschrock 		goto error;
1933789Sahrens 	}
1934789Sahrens 
19353912Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl,
19362676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
19372676Seschrock 		goto error;
19382676Seschrock 	nvlist_free(nvl);
19392676Seschrock 	nvl = realprops;
19402676Seschrock 
19412676Seschrock 	prop = zfs_name_to_prop(propname);
19422676Seschrock 
1943789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
19442676Seschrock 		goto error;
1945789Sahrens 
1946789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19472082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19482082Seschrock 		    "child dataset with inherited mountpoint is used "
19492082Seschrock 		    "in a non-global zone"));
19502082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1951789Sahrens 		goto error;
1952789Sahrens 	}
1953789Sahrens 
1954789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1955789Sahrens 		goto error;
1956789Sahrens 
1957789Sahrens 	/*
1958789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1959789Sahrens 	 */
1960789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1961789Sahrens 
19622676Seschrock 	if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0)
19632676Seschrock 		goto error;
19642676Seschrock 
19654543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1966789Sahrens 
1967789Sahrens 	if (ret != 0) {
1968789Sahrens 		switch (errno) {
1969789Sahrens 
1970789Sahrens 		case ENOSPC:
1971789Sahrens 			/*
1972789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1973789Sahrens 			 * something different; setting a quota or reservation
1974789Sahrens 			 * doesn't use any disk space.
1975789Sahrens 			 */
1976789Sahrens 			switch (prop) {
1977789Sahrens 			case ZFS_PROP_QUOTA:
19782082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19792082Seschrock 				    "size is less than current used or "
19802082Seschrock 				    "reserved space"));
19812082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1982789Sahrens 				break;
1983789Sahrens 
1984789Sahrens 			case ZFS_PROP_RESERVATION:
19852082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19862082Seschrock 				    "size is greater than available space"));
19872082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1988789Sahrens 				break;
1989789Sahrens 
1990789Sahrens 			default:
19912082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1992789Sahrens 				break;
1993789Sahrens 			}
1994789Sahrens 			break;
1995789Sahrens 
1996789Sahrens 		case EBUSY:
19972082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
19982082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
19992082Seschrock 			else
20002676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
2001789Sahrens 			break;
2002789Sahrens 
20031175Slling 		case EROFS:
20042082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
20051175Slling 			break;
20061175Slling 
20073886Sahl 		case ENOTSUP:
20083886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20094603Sahrens 			    "pool must be upgraded to set this "
20104603Sahrens 			    "property or value"));
20113886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
20123886Sahl 			break;
20133886Sahl 
2014789Sahrens 		case EOVERFLOW:
2015789Sahrens 			/*
2016789Sahrens 			 * This platform can't address a volume this big.
2017789Sahrens 			 */
2018789Sahrens #ifdef _ILP32
2019789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
20202082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
2021789Sahrens 				break;
2022789Sahrens 			}
2023789Sahrens #endif
20242082Seschrock 			/* FALLTHROUGH */
2025789Sahrens 		default:
20262082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
2027789Sahrens 		}
2028789Sahrens 	} else {
2029789Sahrens 		/*
2030789Sahrens 		 * Refresh the statistics so the new property value
2031789Sahrens 		 * is reflected.
2032789Sahrens 		 */
20332676Seschrock 		if ((ret = changelist_postfix(cl)) == 0)
20342676Seschrock 			(void) get_stats(zhp);
2035789Sahrens 	}
2036789Sahrens 
2037789Sahrens error:
20382676Seschrock 	nvlist_free(nvl);
20392676Seschrock 	zcmd_free_nvlists(&zc);
20402676Seschrock 	if (cl)
20412676Seschrock 		changelist_free(cl);
2042789Sahrens 	return (ret);
2043789Sahrens }
2044789Sahrens 
2045789Sahrens /*
2046789Sahrens  * Given a property, inherit the value from the parent dataset.
2047789Sahrens  */
2048789Sahrens int
20492676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
2050789Sahrens {
2051789Sahrens 	zfs_cmd_t zc = { 0 };
2052789Sahrens 	int ret;
2053789Sahrens 	prop_changelist_t *cl;
20542082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
20552082Seschrock 	char errbuf[1024];
20562676Seschrock 	zfs_prop_t prop;
20572082Seschrock 
20582082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
20592082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2060789Sahrens 
20612676Seschrock 	if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) {
20622676Seschrock 		/*
20632676Seschrock 		 * For user properties, the amount of work we have to do is very
20642676Seschrock 		 * small, so just do it here.
20652676Seschrock 		 */
20662676Seschrock 		if (!zfs_prop_user(propname)) {
20672676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20682676Seschrock 			    "invalid property"));
20692676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
20702676Seschrock 		}
20712676Seschrock 
20722676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
20732676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
20742676Seschrock 
20754543Smarks 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0)
20762676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
20772676Seschrock 
20782676Seschrock 		return (0);
20792676Seschrock 	}
20802676Seschrock 
2081789Sahrens 	/*
2082789Sahrens 	 * Verify that this property is inheritable.
2083789Sahrens 	 */
20842082Seschrock 	if (zfs_prop_readonly(prop))
20852082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
20862082Seschrock 
20872082Seschrock 	if (!zfs_prop_inheritable(prop))
20882082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2089789Sahrens 
2090789Sahrens 	/*
2091789Sahrens 	 * Check to see if the value applies to this type
2092789Sahrens 	 */
20932082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
20942082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2095789Sahrens 
20963443Srm160521 	/*
20973443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
20983443Srm160521 	 */
20993443Srm160521 	propname = zfs_prop_to_name(prop);
2100789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21012676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2102789Sahrens 
2103789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2104789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
21052082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21062082Seschrock 		    "dataset is used in a non-global zone"));
21072082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2108789Sahrens 	}
2109789Sahrens 
2110789Sahrens 	/*
2111789Sahrens 	 * Determine datasets which will be affected by this change, if any.
2112789Sahrens 	 */
2113789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
2114789Sahrens 		return (-1);
2115789Sahrens 
2116789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
21172082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21182082Seschrock 		    "child dataset with inherited mountpoint is used "
21192082Seschrock 		    "in a non-global zone"));
21202082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2121789Sahrens 		goto error;
2122789Sahrens 	}
2123789Sahrens 
2124789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
2125789Sahrens 		goto error;
2126789Sahrens 
21274543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) {
21282082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2129789Sahrens 	} else {
2130789Sahrens 
21312169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
2132789Sahrens 			goto error;
2133789Sahrens 
2134789Sahrens 		/*
2135789Sahrens 		 * Refresh the statistics so the new property is reflected.
2136789Sahrens 		 */
2137789Sahrens 		(void) get_stats(zhp);
2138789Sahrens 	}
2139789Sahrens 
2140789Sahrens error:
2141789Sahrens 	changelist_free(cl);
2142789Sahrens 	return (ret);
2143789Sahrens }
2144789Sahrens 
21453912Slling void
2146789Sahrens nicebool(int value, char *buf, size_t buflen)
2147789Sahrens {
2148789Sahrens 	if (value)
2149789Sahrens 		(void) strlcpy(buf, "on", buflen);
2150789Sahrens 	else
2151789Sahrens 		(void) strlcpy(buf, "off", buflen);
2152789Sahrens }
2153789Sahrens 
2154789Sahrens /*
21551356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
21561356Seschrock  * extract them appropriately.
21571356Seschrock  */
21581356Seschrock static uint64_t
21591356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21601356Seschrock {
21611356Seschrock 	nvlist_t *nv;
21621356Seschrock 	uint64_t value;
21631356Seschrock 
21642885Sahrens 	*source = NULL;
21651356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21661356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21671356Seschrock 		verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0);
21682885Sahrens 		(void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source);
21691356Seschrock 	} else {
21701356Seschrock 		value = zfs_prop_default_numeric(prop);
21711356Seschrock 		*source = "";
21721356Seschrock 	}
21731356Seschrock 
21741356Seschrock 	return (value);
21751356Seschrock }
21761356Seschrock 
21771356Seschrock static char *
21781356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21791356Seschrock {
21801356Seschrock 	nvlist_t *nv;
21811356Seschrock 	char *value;
21821356Seschrock 
21832885Sahrens 	*source = NULL;
21841356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21851356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21861356Seschrock 		verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0);
21872885Sahrens 		(void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source);
21881356Seschrock 	} else {
21891356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
21901356Seschrock 			value = "";
21911356Seschrock 		*source = "";
21921356Seschrock 	}
21931356Seschrock 
21941356Seschrock 	return (value);
21951356Seschrock }
21961356Seschrock 
21971356Seschrock /*
2198789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2199789Sahrens  * zfs_prop_get_int() are built using this interface.
2200789Sahrens  *
2201789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2202789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2203789Sahrens  * If they differ from the on-disk values, report the current values and mark
2204789Sahrens  * the source "temporary".
2205789Sahrens  */
22062082Seschrock static int
2207789Sahrens get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src,
22082082Seschrock     char **source, uint64_t *val)
2209789Sahrens {
2210789Sahrens 	struct mnttab mnt;
22113265Sahrens 	char *mntopt_on = NULL;
22123265Sahrens 	char *mntopt_off = NULL;
2213789Sahrens 
2214789Sahrens 	*source = NULL;
2215789Sahrens 
22163265Sahrens 	switch (prop) {
22173265Sahrens 	case ZFS_PROP_ATIME:
22183265Sahrens 		mntopt_on = MNTOPT_ATIME;
22193265Sahrens 		mntopt_off = MNTOPT_NOATIME;
22203265Sahrens 		break;
22213265Sahrens 
22223265Sahrens 	case ZFS_PROP_DEVICES:
22233265Sahrens 		mntopt_on = MNTOPT_DEVICES;
22243265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
22253265Sahrens 		break;
22263265Sahrens 
22273265Sahrens 	case ZFS_PROP_EXEC:
22283265Sahrens 		mntopt_on = MNTOPT_EXEC;
22293265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
22303265Sahrens 		break;
22313265Sahrens 
22323265Sahrens 	case ZFS_PROP_READONLY:
22333265Sahrens 		mntopt_on = MNTOPT_RO;
22343265Sahrens 		mntopt_off = MNTOPT_RW;
22353265Sahrens 		break;
22363265Sahrens 
22373265Sahrens 	case ZFS_PROP_SETUID:
22383265Sahrens 		mntopt_on = MNTOPT_SETUID;
22393265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
22403265Sahrens 		break;
22413265Sahrens 
22423265Sahrens 	case ZFS_PROP_XATTR:
22433265Sahrens 		mntopt_on = MNTOPT_XATTR;
22443265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
22453265Sahrens 		break;
22463265Sahrens 	}
22473265Sahrens 
22482474Seschrock 	/*
22492474Seschrock 	 * Because looking up the mount options is potentially expensive
22502474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
22512474Seschrock 	 * we're looking up a property which requires its presence.
22522474Seschrock 	 */
22532474Seschrock 	if (!zhp->zfs_mntcheck &&
22543265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
22553265Sahrens 		struct mnttab entry, search = { 0 };
22563265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
22572474Seschrock 
22582474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
22592474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
22603265Sahrens 		rewind(mnttab);
22613265Sahrens 
22623265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
22633265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
22643265Sahrens 			    entry.mnt_mntopts);
22653265Sahrens 			if (zhp->zfs_mntopts == NULL)
22663265Sahrens 				return (-1);
22673265Sahrens 		}
22682474Seschrock 
22692474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
22702474Seschrock 	}
22712474Seschrock 
2272789Sahrens 	if (zhp->zfs_mntopts == NULL)
2273789Sahrens 		mnt.mnt_mntopts = "";
2274789Sahrens 	else
2275789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2276789Sahrens 
2277789Sahrens 	switch (prop) {
2278789Sahrens 	case ZFS_PROP_ATIME:
22793265Sahrens 	case ZFS_PROP_DEVICES:
22803265Sahrens 	case ZFS_PROP_EXEC:
22813265Sahrens 	case ZFS_PROP_READONLY:
22823265Sahrens 	case ZFS_PROP_SETUID:
22833265Sahrens 	case ZFS_PROP_XATTR:
22842082Seschrock 		*val = getprop_uint64(zhp, prop, source);
22852082Seschrock 
22863265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
22872082Seschrock 			*val = B_TRUE;
2288789Sahrens 			if (src)
2289789Sahrens 				*src = ZFS_SRC_TEMPORARY;
22903265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
22912082Seschrock 			*val = B_FALSE;
2292789Sahrens 			if (src)
2293789Sahrens 				*src = ZFS_SRC_TEMPORARY;
2294789Sahrens 		}
22952082Seschrock 		break;
2296789Sahrens 
22973265Sahrens 	case ZFS_PROP_CANMOUNT:
22982082Seschrock 		*val = getprop_uint64(zhp, prop, source);
22993417Srm160521 		if (*val == 0)
23003417Srm160521 			*source = zhp->zfs_name;
23013417Srm160521 		else
23023417Srm160521 			*source = "";	/* default */
23032082Seschrock 		break;
2304789Sahrens 
2305789Sahrens 	case ZFS_PROP_QUOTA:
2306789Sahrens 	case ZFS_PROP_RESERVATION:
23072885Sahrens 		*val = getprop_uint64(zhp, prop, source);
23082885Sahrens 		if (*val == 0)
2309789Sahrens 			*source = "";	/* default */
2310789Sahrens 		else
2311789Sahrens 			*source = zhp->zfs_name;
23122082Seschrock 		break;
2313789Sahrens 
2314789Sahrens 	case ZFS_PROP_MOUNTED:
23152082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
23162082Seschrock 		break;
2317789Sahrens 
23183377Seschrock 	case ZFS_PROP_NUMCLONES:
23193377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
23203377Seschrock 		break;
23213377Seschrock 
2322789Sahrens 	default:
23234577Sahrens 		switch (zfs_prop_get_type(prop)) {
23244577Sahrens 		case prop_type_number:
23254577Sahrens 		case prop_type_boolean:
23264577Sahrens 		case prop_type_index:
23274577Sahrens 			*val = getprop_uint64(zhp, prop, source);
23284577Sahrens 			break;
23294577Sahrens 
23304577Sahrens 		case prop_type_string:
23314577Sahrens 		default:
23324577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
23334577Sahrens 			    "cannot get non-numeric property"));
23344577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
23354577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
23364577Sahrens 		}
2337789Sahrens 	}
2338789Sahrens 
2339789Sahrens 	return (0);
2340789Sahrens }
2341789Sahrens 
2342789Sahrens /*
2343789Sahrens  * Calculate the source type, given the raw source string.
2344789Sahrens  */
2345789Sahrens static void
2346789Sahrens get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source,
2347789Sahrens     char *statbuf, size_t statlen)
2348789Sahrens {
2349789Sahrens 	if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY)
2350789Sahrens 		return;
2351789Sahrens 
2352789Sahrens 	if (source == NULL) {
2353789Sahrens 		*srctype = ZFS_SRC_NONE;
2354789Sahrens 	} else if (source[0] == '\0') {
2355789Sahrens 		*srctype = ZFS_SRC_DEFAULT;
2356789Sahrens 	} else {
2357789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2358789Sahrens 			*srctype = ZFS_SRC_LOCAL;
2359789Sahrens 		} else {
2360789Sahrens 			(void) strlcpy(statbuf, source, statlen);
2361789Sahrens 			*srctype = ZFS_SRC_INHERITED;
2362789Sahrens 		}
2363789Sahrens 	}
2364789Sahrens 
2365789Sahrens }
2366789Sahrens 
2367789Sahrens /*
2368789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2369789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2370789Sahrens  * human-readable form.
2371789Sahrens  *
2372789Sahrens  * Returns 0 on success, or -1 on error.
2373789Sahrens  */
2374789Sahrens int
2375789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
23762082Seschrock     zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2377789Sahrens {
2378789Sahrens 	char *source = NULL;
2379789Sahrens 	uint64_t val;
2380789Sahrens 	char *str;
2381789Sahrens 	const char *root;
23822676Seschrock 	const char *strval;
2383789Sahrens 
2384789Sahrens 	/*
2385789Sahrens 	 * Check to see if this property applies to our object
2386789Sahrens 	 */
2387789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2388789Sahrens 		return (-1);
2389789Sahrens 
2390789Sahrens 	if (src)
2391789Sahrens 		*src = ZFS_SRC_NONE;
2392789Sahrens 
2393789Sahrens 	switch (prop) {
2394789Sahrens 	case ZFS_PROP_CREATION:
2395789Sahrens 		/*
2396789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2397789Sahrens 		 * this into a string unless 'literal' is specified.
2398789Sahrens 		 */
2399789Sahrens 		{
24002885Sahrens 			val = getprop_uint64(zhp, prop, &source);
24012885Sahrens 			time_t time = (time_t)val;
2402789Sahrens 			struct tm t;
2403789Sahrens 
2404789Sahrens 			if (literal ||
2405789Sahrens 			    localtime_r(&time, &t) == NULL ||
2406789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2407789Sahrens 			    &t) == 0)
24082885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2409789Sahrens 		}
2410789Sahrens 		break;
2411789Sahrens 
2412789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2413789Sahrens 		/*
2414789Sahrens 		 * Getting the precise mountpoint can be tricky.
2415789Sahrens 		 *
2416789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2417789Sahrens 		 *  - for default mountpoints, construct it as /zfs/<dataset>
2418789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2419789Sahrens 		 *    after our ancestor and append it to the inherited value.
2420789Sahrens 		 *
2421789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2422789Sahrens 		 * root to any values we return.
2423789Sahrens 		 */
24241544Seschrock 		root = zhp->zfs_root;
24251356Seschrock 		str = getprop_string(zhp, prop, &source);
24261356Seschrock 
24271356Seschrock 		if (str[0] == '\0') {
2428789Sahrens 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
2429789Sahrens 			    root, zhp->zfs_name);
24301356Seschrock 		} else if (str[0] == '/') {
24311356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2432789Sahrens 
2433789Sahrens 			if (relpath[0] == '/')
2434789Sahrens 				relpath++;
24351356Seschrock 			if (str[1] == '\0')
24361356Seschrock 				str++;
2437789Sahrens 
2438789Sahrens 			if (relpath[0] == '\0')
2439789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
24401356Seschrock 				    root, str);
2441789Sahrens 			else
2442789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
24431356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2444789Sahrens 				    relpath);
2445789Sahrens 		} else {
2446789Sahrens 			/* 'legacy' or 'none' */
24471356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2448789Sahrens 		}
2449789Sahrens 
2450789Sahrens 		break;
2451789Sahrens 
2452789Sahrens 	case ZFS_PROP_ORIGIN:
24532885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2454789Sahrens 		    proplen);
2455789Sahrens 		/*
2456789Sahrens 		 * If there is no parent at all, return failure to indicate that
2457789Sahrens 		 * it doesn't apply to this dataset.
2458789Sahrens 		 */
2459789Sahrens 		if (propbuf[0] == '\0')
2460789Sahrens 			return (-1);
2461789Sahrens 		break;
2462789Sahrens 
2463789Sahrens 	case ZFS_PROP_QUOTA:
2464789Sahrens 	case ZFS_PROP_RESERVATION:
24652082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24662082Seschrock 			return (-1);
2467789Sahrens 
2468789Sahrens 		/*
2469789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2470789Sahrens 		 * (unless literal is set), and indicate that it's the default
2471789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2472789Sahrens 		 * that its set locally.
2473789Sahrens 		 */
2474789Sahrens 		if (val == 0) {
2475789Sahrens 			if (literal)
2476789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2477789Sahrens 			else
2478789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2479789Sahrens 		} else {
2480789Sahrens 			if (literal)
24812856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24823912Slling 				    (u_longlong_t)val);
2483789Sahrens 			else
2484789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2485789Sahrens 		}
2486789Sahrens 		break;
2487789Sahrens 
2488789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
24892082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24902082Seschrock 			return (-1);
24912856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
24922856Snd150628 		    val / 100, (longlong_t)val % 100);
2493789Sahrens 		break;
2494789Sahrens 
2495789Sahrens 	case ZFS_PROP_TYPE:
2496789Sahrens 		switch (zhp->zfs_type) {
2497789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2498789Sahrens 			str = "filesystem";
2499789Sahrens 			break;
2500789Sahrens 		case ZFS_TYPE_VOLUME:
2501789Sahrens 			str = "volume";
2502789Sahrens 			break;
2503789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2504789Sahrens 			str = "snapshot";
2505789Sahrens 			break;
2506789Sahrens 		default:
25072082Seschrock 			abort();
2508789Sahrens 		}
2509789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2510789Sahrens 		break;
2511789Sahrens 
2512789Sahrens 	case ZFS_PROP_MOUNTED:
2513789Sahrens 		/*
2514789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2515789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2516789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2517789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2518789Sahrens 		 */
25192082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
25202082Seschrock 		    src, &source, &val) != 0)
25212082Seschrock 			return (-1);
25222082Seschrock 		if (val)
2523789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2524789Sahrens 		else
2525789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2526789Sahrens 		break;
2527789Sahrens 
2528789Sahrens 	case ZFS_PROP_NAME:
2529789Sahrens 		/*
2530789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2531789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2532789Sahrens 		 * consumers.
2533789Sahrens 		 */
2534789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2535789Sahrens 		break;
2536789Sahrens 
2537789Sahrens 	default:
25384577Sahrens 		switch (zfs_prop_get_type(prop)) {
25394577Sahrens 		case prop_type_number:
25404577Sahrens 			if (get_numeric_property(zhp, prop, src,
25414577Sahrens 			    &source, &val) != 0)
25424577Sahrens 				return (-1);
25434577Sahrens 			if (literal)
25444577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
25454577Sahrens 				    (u_longlong_t)val);
25464577Sahrens 			else
25474577Sahrens 				zfs_nicenum(val, propbuf, proplen);
25484577Sahrens 			break;
25494577Sahrens 
25504577Sahrens 		case prop_type_string:
25514577Sahrens 			(void) strlcpy(propbuf,
25524577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
25534577Sahrens 			break;
25544577Sahrens 
25554577Sahrens 		case prop_type_boolean:
25564577Sahrens 			if (get_numeric_property(zhp, prop, src,
25574577Sahrens 			    &source, &val) != 0)
25584577Sahrens 				return (-1);
25594577Sahrens 			nicebool(val, propbuf, proplen);
25604577Sahrens 
25614577Sahrens 			break;
25624577Sahrens 
25634577Sahrens 		case prop_type_index:
25644577Sahrens 			val = getprop_uint64(zhp, prop, &source);
25654577Sahrens 			if (zfs_prop_index_to_string(prop, val,
25664577Sahrens 			    &strval) != 0)
25674577Sahrens 				return (-1);
25684577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
25694577Sahrens 			break;
25704577Sahrens 
25714577Sahrens 		default:
25724577Sahrens 			abort();
25734577Sahrens 		}
2574789Sahrens 	}
2575789Sahrens 
2576789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2577789Sahrens 
2578789Sahrens 	return (0);
2579789Sahrens }
2580789Sahrens 
2581789Sahrens /*
2582789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2583789Sahrens  * the given property is the appropriate type; should only be used with
2584789Sahrens  * hard-coded property types.
2585789Sahrens  */
2586789Sahrens uint64_t
2587789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2588789Sahrens {
2589789Sahrens 	char *source;
2590789Sahrens 	zfs_source_t sourcetype = ZFS_SRC_NONE;
25912082Seschrock 	uint64_t val;
25922082Seschrock 
25932082Seschrock 	(void) get_numeric_property(zhp, prop, &sourcetype, &source, &val);
25942082Seschrock 
25952082Seschrock 	return (val);
2596789Sahrens }
2597789Sahrens 
2598789Sahrens /*
2599789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2600789Sahrens  */
2601789Sahrens int
2602789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2603789Sahrens     zfs_source_t *src, char *statbuf, size_t statlen)
2604789Sahrens {
2605789Sahrens 	char *source;
2606789Sahrens 
2607789Sahrens 	/*
2608789Sahrens 	 * Check to see if this property applies to our object
2609789Sahrens 	 */
2610789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
26113237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
26122082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
26132082Seschrock 		    zfs_prop_to_name(prop)));
2614789Sahrens 
2615789Sahrens 	if (src)
2616789Sahrens 		*src = ZFS_SRC_NONE;
2617789Sahrens 
26182082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
26192082Seschrock 		return (-1);
2620789Sahrens 
2621789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2622789Sahrens 
2623789Sahrens 	return (0);
2624789Sahrens }
2625789Sahrens 
2626789Sahrens /*
2627789Sahrens  * Returns the name of the given zfs handle.
2628789Sahrens  */
2629789Sahrens const char *
2630789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2631789Sahrens {
2632789Sahrens 	return (zhp->zfs_name);
2633789Sahrens }
2634789Sahrens 
2635789Sahrens /*
2636789Sahrens  * Returns the type of the given zfs handle.
2637789Sahrens  */
2638789Sahrens zfs_type_t
2639789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2640789Sahrens {
2641789Sahrens 	return (zhp->zfs_type);
2642789Sahrens }
2643789Sahrens 
2644789Sahrens /*
26451356Seschrock  * Iterate over all child filesystems
2646789Sahrens  */
2647789Sahrens int
26481356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2649789Sahrens {
2650789Sahrens 	zfs_cmd_t zc = { 0 };
2651789Sahrens 	zfs_handle_t *nzhp;
2652789Sahrens 	int ret;
2653789Sahrens 
2654789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26552082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2656789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2657789Sahrens 		/*
2658789Sahrens 		 * Ignore private dataset names.
2659789Sahrens 		 */
2660789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2661789Sahrens 			continue;
2662789Sahrens 
2663789Sahrens 		/*
2664789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2665789Sahrens 		 * that the pool has since been removed.
2666789Sahrens 		 */
26672082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26682082Seschrock 		    zc.zc_name)) == NULL)
2669789Sahrens 			continue;
2670789Sahrens 
2671789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2672789Sahrens 			return (ret);
2673789Sahrens 	}
2674789Sahrens 
2675789Sahrens 	/*
2676789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2677789Sahrens 	 * returned, then the underlying dataset has been removed since we
2678789Sahrens 	 * obtained the handle.
2679789Sahrens 	 */
2680789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26812082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26822082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2683789Sahrens 
26841356Seschrock 	return (0);
26851356Seschrock }
26861356Seschrock 
26871356Seschrock /*
26881356Seschrock  * Iterate over all snapshots
26891356Seschrock  */
26901356Seschrock int
26911356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26921356Seschrock {
26931356Seschrock 	zfs_cmd_t zc = { 0 };
26941356Seschrock 	zfs_handle_t *nzhp;
26951356Seschrock 	int ret;
2696789Sahrens 
2697789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26982082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
26992082Seschrock 	    &zc) == 0;
2700789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2701789Sahrens 
27022082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
27032082Seschrock 		    zc.zc_name)) == NULL)
2704789Sahrens 			continue;
2705789Sahrens 
2706789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2707789Sahrens 			return (ret);
2708789Sahrens 	}
2709789Sahrens 
2710789Sahrens 	/*
2711789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2712789Sahrens 	 * returned, then the underlying dataset has been removed since we
2713789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2714789Sahrens 	 */
2715789Sahrens 	if (errno != ESRCH && errno != ENOENT)
27162082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
27172082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2718789Sahrens 
2719789Sahrens 	return (0);
2720789Sahrens }
2721789Sahrens 
2722789Sahrens /*
27231356Seschrock  * Iterate over all children, snapshots and filesystems
27241356Seschrock  */
27251356Seschrock int
27261356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
27271356Seschrock {
27281356Seschrock 	int ret;
27291356Seschrock 
27301356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
27311356Seschrock 		return (ret);
27321356Seschrock 
27331356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
27341356Seschrock }
27351356Seschrock 
27361356Seschrock /*
2737789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2738789Sahrens  * Can return NULL if this is a pool.
2739789Sahrens  */
2740789Sahrens static int
2741789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2742789Sahrens {
2743789Sahrens 	char *loc;
2744789Sahrens 
2745789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2746789Sahrens 		return (-1);
2747789Sahrens 
2748789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2749789Sahrens 	buf[loc - path] = '\0';
2750789Sahrens 
2751789Sahrens 	return (0);
2752789Sahrens }
2753789Sahrens 
2754789Sahrens /*
27554490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
27564490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
27574490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
27584490Svb160487  * length of already existing prefix of the given path.  We also fetch the
27594490Svb160487  * 'zoned' property, which is used to validate property settings when creating
27604490Svb160487  * new datasets.
2761789Sahrens  */
2762789Sahrens static int
27634490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27644490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2765789Sahrens {
2766789Sahrens 	zfs_cmd_t zc = { 0 };
2767789Sahrens 	char parent[ZFS_MAXNAMELEN];
2768789Sahrens 	char *slash;
27691356Seschrock 	zfs_handle_t *zhp;
27702082Seschrock 	char errbuf[1024];
27712082Seschrock 
27722082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
27732082Seschrock 	    path);
2774789Sahrens 
2775789Sahrens 	/* get parent, and check to see if this is just a pool */
2776789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
27772082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27782082Seschrock 		    "missing dataset name"));
27792082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2780789Sahrens 	}
2781789Sahrens 
2782789Sahrens 	/* check to see if the pool exists */
2783789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2784789Sahrens 		slash = parent + strlen(parent);
2785789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2786789Sahrens 	zc.zc_name[slash - parent] = '\0';
27872082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2788789Sahrens 	    errno == ENOENT) {
27892082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27902082Seschrock 		    "no such pool '%s'"), zc.zc_name);
27912082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2792789Sahrens 	}
2793789Sahrens 
2794789Sahrens 	/* check to see if the parent dataset exists */
27954490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27964490Svb160487 		if (errno == ENOENT && accept_ancestor) {
27974490Svb160487 			/*
27984490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
27994490Svb160487 			 */
28004490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
28014490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28024490Svb160487 				    "no such pool '%s'"), zc.zc_name);
28034490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
28044490Svb160487 			}
28054490Svb160487 		} else if (errno == ENOENT) {
28062082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28072082Seschrock 			    "parent does not exist"));
28082082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
28094490Svb160487 		} else
28102082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2811789Sahrens 	}
2812789Sahrens 
28132676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2814789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
28152676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
28162082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
28171356Seschrock 		zfs_close(zhp);
2818789Sahrens 		return (-1);
2819789Sahrens 	}
2820789Sahrens 
2821789Sahrens 	/* make sure parent is a filesystem */
28221356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
28232082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28242082Seschrock 		    "parent is not a filesystem"));
28252082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
28261356Seschrock 		zfs_close(zhp);
2827789Sahrens 		return (-1);
2828789Sahrens 	}
2829789Sahrens 
28301356Seschrock 	zfs_close(zhp);
28314490Svb160487 	if (prefixlen != NULL)
28324490Svb160487 		*prefixlen = strlen(parent);
28334490Svb160487 	return (0);
28344490Svb160487 }
28354490Svb160487 
28364490Svb160487 /*
28374490Svb160487  * Finds whether the dataset of the given type(s) exists.
28384490Svb160487  */
28394490Svb160487 boolean_t
28404490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
28414490Svb160487 {
28424490Svb160487 	zfs_handle_t *zhp;
28434490Svb160487 
28444490Svb160487 	if (!zfs_validate_name(hdl, path, types))
28454490Svb160487 		return (B_FALSE);
28464490Svb160487 
28474490Svb160487 	/*
28484490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
28494490Svb160487 	 */
28504490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
28514490Svb160487 		int ds_type = zhp->zfs_type;
28524490Svb160487 
28534490Svb160487 		zfs_close(zhp);
28544490Svb160487 		if (types & ds_type)
28554490Svb160487 			return (B_TRUE);
28564490Svb160487 	}
28574490Svb160487 	return (B_FALSE);
28584490Svb160487 }
28594490Svb160487 
28604490Svb160487 /*
28614490Svb160487  * Creates non-existing ancestors of the given path.
28624490Svb160487  */
28634490Svb160487 int
28644490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28654490Svb160487 {
28664490Svb160487 	int prefix;
28674490Svb160487 	uint64_t zoned;
28684490Svb160487 	char *path_copy;
28694490Svb160487 	int rc;
28704490Svb160487 
28714490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28724490Svb160487 		return (-1);
28734490Svb160487 
28744490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28754490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28764490Svb160487 		free(path_copy);
28774490Svb160487 	}
28784490Svb160487 	if (path_copy == NULL || rc != 0)
28794490Svb160487 		return (-1);
28804490Svb160487 
2881789Sahrens 	return (0);
2882789Sahrens }
2883789Sahrens 
2884789Sahrens /*
28852676Seschrock  * Create a new filesystem or volume.
2886789Sahrens  */
2887789Sahrens int
28882082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28892676Seschrock     nvlist_t *props)
2890789Sahrens {
2891789Sahrens 	zfs_cmd_t zc = { 0 };
2892789Sahrens 	int ret;
2893789Sahrens 	uint64_t size = 0;
2894789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
28952082Seschrock 	char errbuf[1024];
28962676Seschrock 	uint64_t zoned;
28972082Seschrock 
28982082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28992082Seschrock 	    "cannot create '%s'"), path);
2900789Sahrens 
2901789Sahrens 	/* validate the path, taking care to note the extended error message */
29022082Seschrock 	if (!zfs_validate_name(hdl, path, type))
29032082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2904789Sahrens 
2905789Sahrens 	/* validate parents exist */
29064490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2907789Sahrens 		return (-1);
2908789Sahrens 
2909789Sahrens 	/*
2910789Sahrens 	 * The failure modes when creating a dataset of a different type over
2911789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2912789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2913789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2914789Sahrens 	 * first try to see if the dataset exists.
2915789Sahrens 	 */
2916789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
29174490Svb160487 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) {
29182082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29192082Seschrock 		    "dataset already exists"));
29202082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2921789Sahrens 	}
2922789Sahrens 
2923789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2924789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2925789Sahrens 	else
2926789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2927789Sahrens 
29283912Slling 	if (props && (props = zfs_validate_properties(hdl, type, NULL, props,
29293912Slling 	    zoned, NULL, errbuf)) == 0)
29302676Seschrock 		return (-1);
29312676Seschrock 
2932789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29331133Seschrock 		/*
29341133Seschrock 		 * If we are creating a volume, the size and block size must
29351133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29361133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29371133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29381133Seschrock 		 * zero.
29391133Seschrock 		 */
29402676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
29412676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
29422676Seschrock 			nvlist_free(props);
29432082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29442676Seschrock 			    "missing volume size"));
29452676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2946789Sahrens 		}
2947789Sahrens 
29482676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
29492676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29502676Seschrock 		    &blocksize)) != 0) {
29512676Seschrock 			if (ret == ENOENT) {
29522676Seschrock 				blocksize = zfs_prop_default_numeric(
29532676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29542676Seschrock 			} else {
29552676Seschrock 				nvlist_free(props);
29562676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29572676Seschrock 				    "missing volume block size"));
29582676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29592676Seschrock 			}
29602676Seschrock 		}
29612676Seschrock 
29622676Seschrock 		if (size == 0) {
29632676Seschrock 			nvlist_free(props);
29642082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29652676Seschrock 			    "volume size cannot be zero"));
29662676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29671133Seschrock 		}
29681133Seschrock 
29691133Seschrock 		if (size % blocksize != 0) {
29702676Seschrock 			nvlist_free(props);
29712082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29722676Seschrock 			    "volume size must be a multiple of volume block "
29732676Seschrock 			    "size"));
29742676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29751133Seschrock 		}
2976789Sahrens 	}
2977789Sahrens 
29782676Seschrock 	if (props &&
29792676Seschrock 	    zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0)
29802676Seschrock 		return (-1);
29812676Seschrock 	nvlist_free(props);
29822676Seschrock 
2983789Sahrens 	/* create the dataset */
29844543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2985789Sahrens 
29863912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29872082Seschrock 		ret = zvol_create_link(hdl, path);
29883912Slling 		if (ret) {
29893912Slling 			(void) zfs_standard_error(hdl, errno,
29903912Slling 			    dgettext(TEXT_DOMAIN,
29913912Slling 			    "Volume successfully created, but device links "
29923912Slling 			    "were not created"));
29933912Slling 			zcmd_free_nvlists(&zc);
29943912Slling 			return (-1);
29953912Slling 		}
29963912Slling 	}
2997789Sahrens 
29982676Seschrock 	zcmd_free_nvlists(&zc);
29992676Seschrock 
3000789Sahrens 	/* check for failure */
3001789Sahrens 	if (ret != 0) {
3002789Sahrens 		char parent[ZFS_MAXNAMELEN];
3003789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3004789Sahrens 
3005789Sahrens 		switch (errno) {
3006789Sahrens 		case ENOENT:
30072082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30082082Seschrock 			    "no such parent '%s'"), parent);
30092082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3010789Sahrens 
3011789Sahrens 		case EINVAL:
30122082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30133413Smmusante 			    "parent '%s' is not a filesystem"), parent);
30142082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3015789Sahrens 
3016789Sahrens 		case EDOM:
30172082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30182676Seschrock 			    "volume block size must be power of 2 from "
30192676Seschrock 			    "%u to %uk"),
3020789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3021789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
30222082Seschrock 
30232676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30242082Seschrock 
30254603Sahrens 		case ENOTSUP:
30264603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30274603Sahrens 			    "pool must be upgraded to set this "
30284603Sahrens 			    "property or value"));
30294603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
30304603Sahrens 
3031789Sahrens #ifdef _ILP32
3032789Sahrens 		case EOVERFLOW:
3033789Sahrens 			/*
3034789Sahrens 			 * This platform can't address a volume this big.
3035789Sahrens 			 */
30362082Seschrock 			if (type == ZFS_TYPE_VOLUME)
30372082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
30382082Seschrock 				    errbuf));
3039789Sahrens #endif
30402082Seschrock 			/* FALLTHROUGH */
3041789Sahrens 		default:
30422082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3043789Sahrens 		}
3044789Sahrens 	}
3045789Sahrens 
3046789Sahrens 	return (0);
3047789Sahrens }
3048789Sahrens 
3049789Sahrens /*
3050789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3051789Sahrens  * isn't mounted, and that there are no active dependents.
3052789Sahrens  */
3053789Sahrens int
3054789Sahrens zfs_destroy(zfs_handle_t *zhp)
3055789Sahrens {
3056789Sahrens 	zfs_cmd_t zc = { 0 };
3057789Sahrens 
3058789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3059789Sahrens 
30602676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30613126Sahl 		/*
30624543Smarks 		 * If user doesn't have permissions to unshare volume, then
30634543Smarks 		 * abort the request.  This would only happen for a
30644543Smarks 		 * non-privileged user.
30653126Sahl 		 */
30664543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30674543Smarks 			return (-1);
30684543Smarks 		}
30693126Sahl 
30702082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3071789Sahrens 			return (-1);
3072789Sahrens 
3073789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3074789Sahrens 	} else {
3075789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3076789Sahrens 	}
3077789Sahrens 
30784543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30793237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30802082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30812082Seschrock 		    zhp->zfs_name));
30822199Sahrens 	}
3083789Sahrens 
3084789Sahrens 	remove_mountpoint(zhp);
3085789Sahrens 
3086789Sahrens 	return (0);
3087789Sahrens }
3088789Sahrens 
30892199Sahrens struct destroydata {
30902199Sahrens 	char *snapname;
30912199Sahrens 	boolean_t gotone;
30923265Sahrens 	boolean_t closezhp;
30932199Sahrens };
30942199Sahrens 
30952199Sahrens static int
30962199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
30972199Sahrens {
30982199Sahrens 	struct destroydata *dd = arg;
30992199Sahrens 	zfs_handle_t *szhp;
31002199Sahrens 	char name[ZFS_MAXNAMELEN];
31013265Sahrens 	boolean_t closezhp = dd->closezhp;
31023265Sahrens 	int rv;
31032199Sahrens 
31042676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31052676Seschrock 	(void) strlcat(name, "@", sizeof (name));
31062676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
31072199Sahrens 
31082199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31092199Sahrens 	if (szhp) {
31102199Sahrens 		dd->gotone = B_TRUE;
31112199Sahrens 		zfs_close(szhp);
31122199Sahrens 	}
31132199Sahrens 
31142199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31152199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
31162199Sahrens 		/*
31172199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31182199Sahrens 		 * return an error, because then we wouldn't visit all
31192199Sahrens 		 * the volumes.
31202199Sahrens 		 */
31212199Sahrens 	}
31222199Sahrens 
31233265Sahrens 	dd->closezhp = B_TRUE;
31243265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
31253265Sahrens 	if (closezhp)
31263265Sahrens 		zfs_close(zhp);
31273265Sahrens 	return (rv);
31282199Sahrens }
31292199Sahrens 
31302199Sahrens /*
31312199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31322199Sahrens  */
31332199Sahrens int
31342199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
31352199Sahrens {
31362199Sahrens 	zfs_cmd_t zc = { 0 };
31372199Sahrens 	int ret;
31382199Sahrens 	struct destroydata dd = { 0 };
31392199Sahrens 
31402199Sahrens 	dd.snapname = snapname;
31412199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
31422199Sahrens 
31432199Sahrens 	if (!dd.gotone) {
31443237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31452199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
31462199Sahrens 		    zhp->zfs_name, snapname));
31472199Sahrens 	}
31482199Sahrens 
31492199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31502676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
31512199Sahrens 
31524543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
31532199Sahrens 	if (ret != 0) {
31542199Sahrens 		char errbuf[1024];
31552199Sahrens 
31562199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31572199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31582199Sahrens 
31592199Sahrens 		switch (errno) {
31602199Sahrens 		case EEXIST:
31612199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31622199Sahrens 			    "snapshot is cloned"));
31632199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31642199Sahrens 
31652199Sahrens 		default:
31662199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31672199Sahrens 			    errbuf));
31682199Sahrens 		}
31692199Sahrens 	}
31702199Sahrens 
31712199Sahrens 	return (0);
31722199Sahrens }
31732199Sahrens 
3174789Sahrens /*
3175789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3176789Sahrens  */
3177789Sahrens int
31782676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3179789Sahrens {
3180789Sahrens 	zfs_cmd_t zc = { 0 };
3181789Sahrens 	char parent[ZFS_MAXNAMELEN];
3182789Sahrens 	int ret;
31832082Seschrock 	char errbuf[1024];
31842082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31852676Seschrock 	zfs_type_t type;
31862676Seschrock 	uint64_t zoned;
3187789Sahrens 
3188789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3189789Sahrens 
31902082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31912082Seschrock 	    "cannot create '%s'"), target);
31922082Seschrock 
3193789Sahrens 	/* validate the target name */
31942082Seschrock 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM))
31952082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3196789Sahrens 
3197789Sahrens 	/* validate parents exist */
31984490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3199789Sahrens 		return (-1);
3200789Sahrens 
3201789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3202789Sahrens 
3203789Sahrens 	/* do the clone */
32042676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3205789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32062744Snn35248 		type = ZFS_TYPE_VOLUME;
32072676Seschrock 	} else {
3208789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32092744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
32102676Seschrock 	}
32112676Seschrock 
32122676Seschrock 	if (props) {
32133912Slling 		if ((props = zfs_validate_properties(hdl, type, NULL, props,
32143912Slling 		    zoned, zhp, errbuf)) == NULL)
32152676Seschrock 			return (-1);
32162676Seschrock 
32172676Seschrock 		if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) {
32182676Seschrock 			nvlist_free(props);
32192676Seschrock 			return (-1);
32202676Seschrock 		}
32212676Seschrock 
32222676Seschrock 		nvlist_free(props);
32232676Seschrock 	}
3224789Sahrens 
3225789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
32262676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
32274543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3228789Sahrens 
32292676Seschrock 	zcmd_free_nvlists(&zc);
32302676Seschrock 
3231789Sahrens 	if (ret != 0) {
3232789Sahrens 		switch (errno) {
3233789Sahrens 
3234789Sahrens 		case ENOENT:
3235789Sahrens 			/*
3236789Sahrens 			 * The parent doesn't exist.  We should have caught this
3237789Sahrens 			 * above, but there may a race condition that has since
3238789Sahrens 			 * destroyed the parent.
3239789Sahrens 			 *
3240789Sahrens 			 * At this point, we don't know whether it's the source
3241789Sahrens 			 * that doesn't exist anymore, or whether the target
3242789Sahrens 			 * dataset doesn't exist.
3243789Sahrens 			 */
32442082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32452082Seschrock 			    "no such parent '%s'"), parent);
32462082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
32472082Seschrock 
32482082Seschrock 		case EXDEV:
32492082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32502082Seschrock 			    "source and target pools differ"));
32512082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
32522082Seschrock 			    errbuf));
32532082Seschrock 
32542082Seschrock 		default:
32552082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32562082Seschrock 			    errbuf));
32572082Seschrock 		}
32582676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32592082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32602082Seschrock 	}
32612082Seschrock 
32622082Seschrock 	return (ret);
32632082Seschrock }
32642082Seschrock 
32652082Seschrock typedef struct promote_data {
32662082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32672082Seschrock 	const char *cb_target;
32682082Seschrock 	const char *cb_errbuf;
32692082Seschrock 	uint64_t cb_pivot_txg;
32702082Seschrock } promote_data_t;
32712082Seschrock 
32722082Seschrock static int
32732082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32742082Seschrock {
32752082Seschrock 	promote_data_t *pd = data;
32762082Seschrock 	zfs_handle_t *szhp;
32772082Seschrock 	char snapname[MAXPATHLEN];
32783265Sahrens 	int rv = 0;
32792082Seschrock 
32802082Seschrock 	/* We don't care about snapshots after the pivot point */
32813265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32823265Sahrens 		zfs_close(zhp);
32832082Seschrock 		return (0);
32843265Sahrens 	}
32852082Seschrock 
32862417Sahrens 	/* Remove the device link if it's a zvol. */
32872676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32882417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32892082Seschrock 
32902082Seschrock 	/* Check for conflicting names */
32912676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32922676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
32932082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
32942082Seschrock 	if (szhp != NULL) {
32952082Seschrock 		zfs_close(szhp);
32962082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32972082Seschrock 		    "snapshot name '%s' from origin \n"
32982082Seschrock 		    "conflicts with '%s' from target"),
32992082Seschrock 		    zhp->zfs_name, snapname);
33003265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
33012082Seschrock 	}
33023265Sahrens 	zfs_close(zhp);
33033265Sahrens 	return (rv);
33042082Seschrock }
33052082Seschrock 
33062417Sahrens static int
33072417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
33082417Sahrens {
33092417Sahrens 	promote_data_t *pd = data;
33102417Sahrens 
33112417Sahrens 	/* We don't care about snapshots after the pivot point */
33123265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
33133265Sahrens 		/* Create the device link if it's a zvol. */
33143265Sahrens 		if (ZFS_IS_VOLUME(zhp))
33153265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
33163265Sahrens 	}
33173265Sahrens 
33183265Sahrens 	zfs_close(zhp);
33192417Sahrens 	return (0);
33202417Sahrens }
33212417Sahrens 
33222082Seschrock /*
33232082Seschrock  * Promotes the given clone fs to be the clone parent.
33242082Seschrock  */
33252082Seschrock int
33262082Seschrock zfs_promote(zfs_handle_t *zhp)
33272082Seschrock {
33282082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33292082Seschrock 	zfs_cmd_t zc = { 0 };
33302082Seschrock 	char parent[MAXPATHLEN];
33312082Seschrock 	char *cp;
33322082Seschrock 	int ret;
33332082Seschrock 	zfs_handle_t *pzhp;
33342082Seschrock 	promote_data_t pd;
33352082Seschrock 	char errbuf[1024];
33362082Seschrock 
33372082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33382082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
33392082Seschrock 
33402082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
33412082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33422082Seschrock 		    "snapshots can not be promoted"));
33432082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33442082Seschrock 	}
33452082Seschrock 
33462676Seschrock 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent));
33472082Seschrock 	if (parent[0] == '\0') {
33482082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33492082Seschrock 		    "not a cloned filesystem"));
33502082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33512082Seschrock 	}
33522082Seschrock 	cp = strchr(parent, '@');
33532082Seschrock 	*cp = '\0';
33542082Seschrock 
33552082Seschrock 	/* Walk the snapshots we will be moving */
33562082Seschrock 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT);
33572082Seschrock 	if (pzhp == NULL)
33582082Seschrock 		return (-1);
33592082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33602082Seschrock 	zfs_close(pzhp);
33612082Seschrock 	pd.cb_target = zhp->zfs_name;
33622082Seschrock 	pd.cb_errbuf = errbuf;
33632082Seschrock 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY);
33642082Seschrock 	if (pzhp == NULL)
33652082Seschrock 		return (-1);
33662082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33672082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33682082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33692417Sahrens 	if (ret != 0) {
33702417Sahrens 		zfs_close(pzhp);
33712082Seschrock 		return (-1);
33722417Sahrens 	}
33732082Seschrock 
33742082Seschrock 	/* issue the ioctl */
33752676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of,
33762676Seschrock 	    sizeof (zc.zc_value));
33772082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33784543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33792082Seschrock 
33802082Seschrock 	if (ret != 0) {
33812417Sahrens 		int save_errno = errno;
33822417Sahrens 
33832417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33842417Sahrens 		zfs_close(pzhp);
33852417Sahrens 
33862417Sahrens 		switch (save_errno) {
3387789Sahrens 		case EEXIST:
3388789Sahrens 			/*
33892082Seschrock 			 * There is a conflicting snapshot name.  We
33902082Seschrock 			 * should have caught this above, but they could
33912082Seschrock 			 * have renamed something in the mean time.
3392789Sahrens 			 */
33932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33942082Seschrock 			    "conflicting snapshot name from parent '%s'"),
33952082Seschrock 			    parent);
33962082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3397789Sahrens 
3398789Sahrens 		default:
33992417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3400789Sahrens 		}
34012417Sahrens 	} else {
34022417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3403789Sahrens 	}
3404789Sahrens 
34052417Sahrens 	zfs_close(pzhp);
3406789Sahrens 	return (ret);
3407789Sahrens }
3408789Sahrens 
34094007Smmusante struct createdata {
34104007Smmusante 	const char *cd_snapname;
34114007Smmusante 	int cd_ifexists;
34124007Smmusante };
34134007Smmusante 
34142199Sahrens static int
34152199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
34162199Sahrens {
34174007Smmusante 	struct createdata *cd = arg;
34182676Seschrock 	int ret;
34192199Sahrens 
34202199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
34212199Sahrens 		char name[MAXPATHLEN];
34222199Sahrens 
34232676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
34242676Seschrock 		(void) strlcat(name, "@", sizeof (name));
34254007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
34264007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
34274007Smmusante 		    cd->cd_ifexists);
34282199Sahrens 		/*
34292199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
34302199Sahrens 		 * return an error, because then we wouldn't visit all
34312199Sahrens 		 * the volumes.
34322199Sahrens 		 */
34332199Sahrens 	}
34342676Seschrock 
34354007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
34362676Seschrock 
34372676Seschrock 	zfs_close(zhp);
34382676Seschrock 
34392676Seschrock 	return (ret);
34402199Sahrens }
34412199Sahrens 
3442789Sahrens /*
34433504Sahl  * Takes a snapshot of the given dataset.
3444789Sahrens  */
3445789Sahrens int
34462199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3447789Sahrens {
3448789Sahrens 	const char *delim;
3449789Sahrens 	char *parent;
3450789Sahrens 	zfs_handle_t *zhp;
3451789Sahrens 	zfs_cmd_t zc = { 0 };
3452789Sahrens 	int ret;
34532082Seschrock 	char errbuf[1024];
34542082Seschrock 
34552082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34562082Seschrock 	    "cannot snapshot '%s'"), path);
34572082Seschrock 
34582082Seschrock 	/* validate the target name */
34592082Seschrock 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT))
34602082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3461789Sahrens 
3462789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34632199Sahrens 	delim = strchr(path, '@');
34642082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
34652082Seschrock 		return (-1);
3466789Sahrens 	(void) strncpy(parent, path, delim - path);
3467789Sahrens 	parent[delim - path] = '\0';
3468789Sahrens 
34692082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3470789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3471789Sahrens 		free(parent);
3472789Sahrens 		return (-1);
3473789Sahrens 	}
3474789Sahrens 
34752199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34762676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34774543Smarks 	if (ZFS_IS_VOLUME(zhp))
34784543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34794543Smarks 	else
34804543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34812199Sahrens 	zc.zc_cookie = recursive;
34824543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
34832199Sahrens 
34842199Sahrens 	/*
34852199Sahrens 	 * if it was recursive, the one that actually failed will be in
34862199Sahrens 	 * zc.zc_name.
34872199Sahrens 	 */
34884543Smarks 	if (ret != 0)
34894543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34904543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
34914543Smarks 
34922199Sahrens 	if (ret == 0 && recursive) {
34934007Smmusante 		struct createdata cd;
34944007Smmusante 
34954007Smmusante 		cd.cd_snapname = delim + 1;
34964007Smmusante 		cd.cd_ifexists = B_FALSE;
34974007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
34982199Sahrens 	}
3499789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
35002082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
35012199Sahrens 		if (ret != 0) {
35024543Smarks 			(void) zfs_standard_error(hdl, errno,
35034543Smarks 			    dgettext(TEXT_DOMAIN,
35044543Smarks 			    "Volume successfully snapshotted, but device links "
35054543Smarks 			    "were not created"));
35064543Smarks 			free(parent);
35074543Smarks 			zfs_close(zhp);
35084543Smarks 			return (-1);
35092199Sahrens 		}
3510789Sahrens 	}
3511789Sahrens 
35122082Seschrock 	if (ret != 0)
35132082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3514789Sahrens 
3515789Sahrens 	free(parent);
3516789Sahrens 	zfs_close(zhp);
3517789Sahrens 
3518789Sahrens 	return (ret);
3519789Sahrens }
3520789Sahrens 
3521789Sahrens /*
35223504Sahl  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
35233504Sahl  * NULL) to the file descriptor specified by outfd.
3524789Sahrens  */
3525789Sahrens int
35263504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd)
3527789Sahrens {
3528789Sahrens 	zfs_cmd_t zc = { 0 };
35292082Seschrock 	char errbuf[1024];
35302885Sahrens 	libzfs_handle_t *hdl = zhp->zfs_hdl;
35312082Seschrock 
35323504Sahl 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
35333504Sahl 
35342885Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35352885Sahrens 	if (fromsnap)
35362885Sahrens 		(void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name));
35373504Sahl 	zc.zc_cookie = outfd;
35383504Sahl 
35393504Sahl 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) {
35403504Sahl 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35413504Sahl 		    "cannot send '%s'"), zhp->zfs_name);
35423504Sahl 
3543789Sahrens 		switch (errno) {
3544789Sahrens 
3545789Sahrens 		case EXDEV:
35462082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35473413Smmusante 			    "not an earlier snapshot from the same fs"));
35482082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3549789Sahrens 
3550789Sahrens 		case EDQUOT:
3551789Sahrens 		case EFBIG:
3552789Sahrens 		case EIO:
3553789Sahrens 		case ENOLINK:
3554789Sahrens 		case ENOSPC:
3555789Sahrens 		case ENOSTR:
3556789Sahrens 		case ENXIO:
3557789Sahrens 		case EPIPE:
3558789Sahrens 		case ERANGE:
3559789Sahrens 		case EFAULT:
3560789Sahrens 		case EROFS:
35612082Seschrock 			zfs_error_aux(hdl, strerror(errno));
35622082Seschrock 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
3563789Sahrens 
3564789Sahrens 		default:
35652082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3566789Sahrens 		}
3567789Sahrens 	}
3568789Sahrens 
35693504Sahl 	return (0);
3570789Sahrens }
3571789Sahrens 
3572789Sahrens /*
35732885Sahrens  * Create ancestors of 'target', but not target itself, and not
35742885Sahrens  * ancestors whose names are shorter than prefixlen.  Die if
35752885Sahrens  * prefixlen-ancestor does not exist.
35762885Sahrens  */
35772885Sahrens static int
35782885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
35792885Sahrens {
35802885Sahrens 	zfs_handle_t *h;
35812885Sahrens 	char *cp;
35822885Sahrens 
35832885Sahrens 	/* make sure prefix exists */
35842885Sahrens 	cp = strchr(target + prefixlen, '/');
35854603Sahrens 	if (cp == NULL) {
35864603Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
35874603Sahrens 	} else {
35884603Sahrens 		*cp = '\0';
35894603Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
35904603Sahrens 		*cp = '/';
35914603Sahrens 	}
35922885Sahrens 	if (h == NULL)
35932885Sahrens 		return (-1);
35942885Sahrens 	zfs_close(h);
35952885Sahrens 
35962885Sahrens 	/*
35972885Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
35982885Sahrens 	 * up to the prefixlen-long one.
35992885Sahrens 	 */
36002885Sahrens 	for (cp = target + prefixlen + 1;
36012885Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
36022885Sahrens 		const char *opname;
36034543Smarks 		char *logstr;
36042885Sahrens 
36052885Sahrens 		*cp = '\0';
36062885Sahrens 
36072885Sahrens 		h = make_dataset_handle(hdl, target);
36082885Sahrens 		if (h) {
36092885Sahrens 			/* it already exists, nothing to do here */
36102885Sahrens 			zfs_close(h);
36112885Sahrens 			continue;
36122885Sahrens 		}
36132885Sahrens 
36142885Sahrens 		opname = dgettext(TEXT_DOMAIN, "create");
36154543Smarks 		logstr = hdl->libzfs_log_str;
36164543Smarks 		hdl->libzfs_log_str = NULL;
36172885Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
36184543Smarks 		    NULL) != 0) {
36194543Smarks 			hdl->libzfs_log_str = logstr;
36202885Sahrens 			goto ancestorerr;
36214543Smarks 		}
36224543Smarks 
36234543Smarks 		hdl->libzfs_log_str = logstr;
36242885Sahrens 		opname = dgettext(TEXT_DOMAIN, "open");
36252885Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
36262885Sahrens 		if (h == NULL)
36272885Sahrens 			goto ancestorerr;
36282885Sahrens 
36292885Sahrens 		opname = dgettext(TEXT_DOMAIN, "mount");
36302885Sahrens 		if (zfs_mount(h, NULL, 0) != 0)
36312885Sahrens 			goto ancestorerr;
36322885Sahrens 
36332885Sahrens 		opname = dgettext(TEXT_DOMAIN, "share");
36342885Sahrens 		if (zfs_share(h) != 0)
36352885Sahrens 			goto ancestorerr;
36362885Sahrens 
36372885Sahrens 		zfs_close(h);
36382885Sahrens 
36392885Sahrens 		continue;
36402885Sahrens ancestorerr:
36412885Sahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36422885Sahrens 		    "failed to %s ancestor '%s'"), opname, target);
36432885Sahrens 		return (-1);
36442885Sahrens 	}
36452885Sahrens 
36462885Sahrens 	return (0);
36472885Sahrens }
36482885Sahrens 
36492885Sahrens /*
36503504Sahl  * Restores a backup of tosnap from the file descriptor specified by infd.
3651789Sahrens  */
3652789Sahrens int
36532082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix,
36543504Sahl     int verbose, int dryrun, boolean_t force, int infd)
3655789Sahrens {
3656789Sahrens 	zfs_cmd_t zc = { 0 };
3657789Sahrens 	time_t begin_time;
36582885Sahrens 	int ioctl_err, err, bytes, size, choplen;
3659789Sahrens 	char *cp;
3660789Sahrens 	dmu_replay_record_t drr;
3661789Sahrens 	struct drr_begin *drrb = &zc.zc_begin_record;
36622082Seschrock 	char errbuf[1024];
36632665Snd150628 	prop_changelist_t *clp;
36642885Sahrens 	char chopprefix[ZFS_MAXNAMELEN];
3665789Sahrens 
3666789Sahrens 	begin_time = time(NULL);
3667789Sahrens 
36682082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36692082Seschrock 	    "cannot receive"));
36702082Seschrock 
3671789Sahrens 	/* read in the BEGIN record */
3672789Sahrens 	cp = (char *)&drr;
3673789Sahrens 	bytes = 0;
3674789Sahrens 	do {
36753504Sahl 		size = read(infd, cp, sizeof (drr) - bytes);
3676868Sahrens 		cp += size;
3677868Sahrens 		bytes += size;
3678868Sahrens 	} while (size > 0);
3679868Sahrens 
3680868Sahrens 	if (size < 0 || bytes != sizeof (drr)) {
36812082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
36822082Seschrock 		    "stream (failed to read first record)"));
36832082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3684789Sahrens 	}
3685789Sahrens 
3686789Sahrens 	zc.zc_begin_record = drr.drr_u.drr_begin;
3687789Sahrens 
3688789Sahrens 	if (drrb->drr_magic != DMU_BACKUP_MAGIC &&
3689789Sahrens 	    drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) {
36902082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
36912082Seschrock 		    "stream (bad magic number)"));
36922082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3693789Sahrens 	}
3694789Sahrens 
3695789Sahrens 	if (drrb->drr_version != DMU_BACKUP_VERSION &&
3696789Sahrens 	    drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) {
36972082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version "
36982082Seschrock 		    "0x%llx is supported (stream is version 0x%llx)"),
3699789Sahrens 		    DMU_BACKUP_VERSION, drrb->drr_version);
37002082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3701789Sahrens 	}
3702789Sahrens 
37032885Sahrens 	if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) {
37042885Sahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
37053912Slling 		    "stream (bad snapshot name)"));
37062885Sahrens 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
37072885Sahrens 	}
3708789Sahrens 	/*
37092885Sahrens 	 * Determine how much of the snapshot name stored in the stream
37102885Sahrens 	 * we are going to tack on to the name they specified on the
37112885Sahrens 	 * command line, and how much we are going to chop off.
37122885Sahrens 	 *
37132885Sahrens 	 * If they specified a snapshot, chop the entire name stored in
37142885Sahrens 	 * the stream.
3715789Sahrens 	 */
37162885Sahrens 	(void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname);
3717789Sahrens 	if (isprefix) {
37182885Sahrens 		/*
37192885Sahrens 		 * They specified a fs with -d, we want to tack on
37202885Sahrens 		 * everything but the pool name stored in the stream
37212885Sahrens 		 */
37222885Sahrens 		if (strchr(tosnap, '@')) {
37232885Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
37242885Sahrens 			    "argument - snapshot not allowed with -d"));
37252885Sahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3726789Sahrens 		}
37272885Sahrens 		cp = strchr(chopprefix, '/');
3728789Sahrens 		if (cp == NULL)
37292885Sahrens 			cp = strchr(chopprefix, '@');
37302885Sahrens 		*cp = '\0';
3731789Sahrens 	} else if (strchr(tosnap, '@') == NULL) {
3732789Sahrens 		/*
37332885Sahrens 		 * If they specified a filesystem without -d, we want to
37342885Sahrens 		 * tack on everything after the fs specified in the
37352885Sahrens 		 * first name from the stream.
3736789Sahrens 		 */
37372885Sahrens 		cp = strchr(chopprefix, '@');
37382885Sahrens 		*cp = '\0';
3739789Sahrens 	}
37402885Sahrens 	choplen = strlen(chopprefix);
37412885Sahrens 
37422885Sahrens 	/*
37432885Sahrens 	 * Determine name of destination snapshot, store in zc_value.
37442885Sahrens 	 */
37452885Sahrens 	(void) strcpy(zc.zc_value, tosnap);
37462885Sahrens 	(void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen,
37472885Sahrens 	    sizeof (zc.zc_value));
37483265Sahrens 	if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT))
37493265Sahrens 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37502885Sahrens 
37512885Sahrens 	(void) strcpy(zc.zc_name, zc.zc_value);
3752789Sahrens 	if (drrb->drr_fromguid) {
3753789Sahrens 		/* incremental backup stream */
37542885Sahrens 		zfs_handle_t *h;
37552885Sahrens 
37562885Sahrens 		/* do the recvbackup ioctl to the containing fs */
37572885Sahrens 		*strchr(zc.zc_name, '@') = '\0';
3758789Sahrens 
3759789Sahrens 		/* make sure destination fs exists */
37602082Seschrock 		h = zfs_open(hdl, zc.zc_name,
37612082Seschrock 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
37622082Seschrock 		if (h == NULL)
3763789Sahrens 			return (-1);
3764868Sahrens 		if (!dryrun) {
37652665Snd150628 			/*
37662665Snd150628 			 * We need to unmount all the dependents of the dataset
37672665Snd150628 			 * and the dataset itself. If it's a volume
37682665Snd150628 			 * then remove device link.
37692665Snd150628 			 */
3770868Sahrens 			if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
37712665Snd150628 				clp = changelist_gather(h, ZFS_PROP_NAME, 0);
37722665Snd150628 				if (clp == NULL)
37732665Snd150628 					return (-1);
37742665Snd150628 				if (changelist_prefix(clp) != 0) {
37752665Snd150628 					changelist_free(clp);
37762665Snd150628 					return (-1);
37772665Snd150628 				}
3778868Sahrens 			} else {
37794543Smarks 				if (zvol_remove_link(hdl, h->zfs_name) != 0) {
37804543Smarks 					zfs_close(h);
37814543Smarks 					return (-1);
37824543Smarks 				}
37834543Smarks 
3784868Sahrens 			}
3785868Sahrens 		}
3786789Sahrens 		zfs_close(h);
3787789Sahrens 	} else {
3788789Sahrens 		/* full backup stream */
3789789Sahrens 
3790868Sahrens 		/* Make sure destination fs does not exist */
37912885Sahrens 		*strchr(zc.zc_name, '@') = '\0';
37924490Svb160487 		if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) {
37932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37942082Seschrock 			    "destination '%s' exists"), zc.zc_name);
37952082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3796868Sahrens 		}
3797868Sahrens 
37982885Sahrens 		if (strchr(zc.zc_name, '/') == NULL) {
37992885Sahrens 			/*
38002885Sahrens 			 * they're trying to do a recv into a
38012885Sahrens 			 * nonexistant topmost filesystem.
38022885Sahrens 			 */
38032885Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38042885Sahrens 			    "destination does not exist"), zc.zc_name);
38052885Sahrens 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
38062885Sahrens 		}
38072885Sahrens 
3808868Sahrens 		/* Do the recvbackup ioctl to the fs's parent. */
38092885Sahrens 		*strrchr(zc.zc_name, '/') = '\0';
38102885Sahrens 
38112885Sahrens 		if (isprefix && (err = create_parents(hdl,
38122885Sahrens 		    zc.zc_value, strlen(tosnap))) != 0) {
38132885Sahrens 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
38142885Sahrens 		}
38152885Sahrens 
3816789Sahrens 	}
3817789Sahrens 
38183504Sahl 	zc.zc_cookie = infd;
38192676Seschrock 	zc.zc_guid = force;
3820789Sahrens 	if (verbose) {
38211749Sahrens 		(void) printf("%s %s stream of %s into %s\n",
38221749Sahrens 		    dryrun ? "would receive" : "receiving",
3823789Sahrens 		    drrb->drr_fromguid ? "incremental" : "full",
3824789Sahrens 		    drr.drr_u.drr_begin.drr_toname,
38252676Seschrock 		    zc.zc_value);
3826789Sahrens 		(void) fflush(stdout);
3827789Sahrens 	}
3828789Sahrens 	if (dryrun)
3829789Sahrens 		return (0);
38304543Smarks 	err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc);
3831868Sahrens 	if (ioctl_err != 0) {
3832789Sahrens 		switch (errno) {
3833789Sahrens 		case ENODEV:
38342082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38352082Seschrock 			    "most recent snapshot does not match incremental "
38362082Seschrock 			    "source"));
38372082Seschrock 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3838789Sahrens 			break;
3839789Sahrens 		case ETXTBSY:
38402082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38412082Seschrock 			    "destination has been modified since most recent "
38422082Seschrock 			    "snapshot"));
38432082Seschrock 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3844789Sahrens 			break;
3845789Sahrens 		case EEXIST:
3846789Sahrens 			if (drrb->drr_fromguid == 0) {
3847789Sahrens 				/* it's the containing fs that exists */
38482676Seschrock 				cp = strchr(zc.zc_value, '@');
3849789Sahrens 				*cp = '\0';
3850789Sahrens 			}
38512082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38522082Seschrock 			    "destination already exists"));
38533237Slling 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
38543237Slling 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
38553237Slling 			    zc.zc_value);
3856789Sahrens 			break;
3857789Sahrens 		case EINVAL:
38582082Seschrock 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3859868Sahrens 			break;
38601544Seschrock 		case ECKSUM:
38612082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38622082Seschrock 			    "invalid stream (checksum mismatch)"));
38632082Seschrock 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3864789Sahrens 			break;
3865789Sahrens 		default:
38662082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
3867789Sahrens 		}
3868789Sahrens 	}
3869789Sahrens 
3870789Sahrens 	/*
3871868Sahrens 	 * Mount or recreate the /dev links for the target filesystem
3872868Sahrens 	 * (if created, or if we tore them down to do an incremental
3873868Sahrens 	 * restore), and the /dev links for the new snapshot (if
38742665Snd150628 	 * created). Also mount any children of the target filesystem
38752665Snd150628 	 * if we did an incremental receive.
3876789Sahrens 	 */
38772676Seschrock 	cp = strchr(zc.zc_value, '@');
3878868Sahrens 	if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) {
3879789Sahrens 		zfs_handle_t *h;
3880789Sahrens 
3881789Sahrens 		*cp = '\0';
38822676Seschrock 		h = zfs_open(hdl, zc.zc_value,
3883789Sahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3884868Sahrens 		*cp = '@';
3885789Sahrens 		if (h) {
38862665Snd150628 			if (h->zfs_type == ZFS_TYPE_VOLUME) {
38872082Seschrock 				err = zvol_create_link(hdl, h->zfs_name);
38881544Seschrock 				if (err == 0 && ioctl_err == 0)
38892082Seschrock 					err = zvol_create_link(hdl,
38902676Seschrock 					    zc.zc_value);
38912665Snd150628 			} else {
38922665Snd150628 				if (drrb->drr_fromguid) {
38932665Snd150628 					err = changelist_postfix(clp);
38942665Snd150628 					changelist_free(clp);
38952665Snd150628 				} else {
38962665Snd150628 					err = zfs_mount(h, NULL, 0);
38972665Snd150628 				}
3898868Sahrens 			}
38992665Snd150628 		zfs_close(h);
3900789Sahrens 		}
3901789Sahrens 	}
3902789Sahrens 
3903868Sahrens 	if (err || ioctl_err)
3904868Sahrens 		return (-1);
3905789Sahrens 
3906789Sahrens 	if (verbose) {
3907789Sahrens 		char buf1[64];
3908789Sahrens 		char buf2[64];
3909789Sahrens 		uint64_t bytes = zc.zc_cookie;
3910789Sahrens 		time_t delta = time(NULL) - begin_time;
3911789Sahrens 		if (delta == 0)
3912789Sahrens 			delta = 1;
3913789Sahrens 		zfs_nicenum(bytes, buf1, sizeof (buf1));
3914789Sahrens 		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3915789Sahrens 
39164603Sahrens 		(void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3917789Sahrens 		    buf1, delta, buf2);
3918789Sahrens 	}
39192665Snd150628 
3920789Sahrens 	return (0);
3921789Sahrens }
3922789Sahrens 
3923789Sahrens /*
39241294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
39251294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
39261294Slling  * is a dependent and we should just destroy it without checking the transaction
39271294Slling  * group.
3928789Sahrens  */
39291294Slling typedef struct rollback_data {
39301294Slling 	const char	*cb_target;		/* the snapshot */
39311294Slling 	uint64_t	cb_create;		/* creation time reference */
39321294Slling 	prop_changelist_t *cb_clp;		/* changelist pointer */
39331294Slling 	int		cb_error;
39342082Seschrock 	boolean_t	cb_dependent;
39351294Slling } rollback_data_t;
39361294Slling 
39371294Slling static int
39381294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
39391294Slling {
39401294Slling 	rollback_data_t *cbp = data;
39411294Slling 
39421294Slling 	if (!cbp->cb_dependent) {
39431294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
39441294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
39451294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
39461294Slling 		    cbp->cb_create) {
39474543Smarks 			char *logstr;
39481294Slling 
39492082Seschrock 			cbp->cb_dependent = B_TRUE;
39502474Seschrock 			if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy,
39512474Seschrock 			    cbp) != 0)
39522474Seschrock 				cbp->cb_error = 1;
39532082Seschrock 			cbp->cb_dependent = B_FALSE;
39541294Slling 
39554543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
39564543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
39571294Slling 			if (zfs_destroy(zhp) != 0)
39581294Slling 				cbp->cb_error = 1;
39591294Slling 			else
39601294Slling 				changelist_remove(zhp, cbp->cb_clp);
39614543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
39621294Slling 		}
39631294Slling 	} else {
39641294Slling 		if (zfs_destroy(zhp) != 0)
39651294Slling 			cbp->cb_error = 1;
39661294Slling 		else
39671294Slling 			changelist_remove(zhp, cbp->cb_clp);
39681294Slling 	}
39691294Slling 
39701294Slling 	zfs_close(zhp);
39711294Slling 	return (0);
39721294Slling }
39731294Slling 
39741294Slling /*
39751294Slling  * Rollback the dataset to its latest snapshot.
39761294Slling  */
39771294Slling static int
39781294Slling do_rollback(zfs_handle_t *zhp)
3979789Sahrens {
3980789Sahrens 	int ret;
3981789Sahrens 	zfs_cmd_t zc = { 0 };
3982789Sahrens 
3983789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3984789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3985789Sahrens 
3986789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME &&
39872082Seschrock 	    zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3988789Sahrens 		return (-1);
3989789Sahrens 
3990789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3991789Sahrens 
39922676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3993789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3994789Sahrens 	else
3995789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3996789Sahrens 
3997789Sahrens 	/*
3998789Sahrens 	 * We rely on the consumer to verify that there are no newer snapshots
3999789Sahrens 	 * for the given dataset.  Given these constraints, we can simply pass
4000789Sahrens 	 * the name on to the ioctl() call.  There is still an unlikely race
4001789Sahrens 	 * condition where the user has taken a snapshot since we verified that
4002789Sahrens 	 * this was the most recent.
4003789Sahrens 	 */
40044543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
40053237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
40062082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
40072082Seschrock 		    zhp->zfs_name);
4008789Sahrens 	} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
40092082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
4010789Sahrens 	}
4011789Sahrens 
4012789Sahrens 	return (ret);
4013789Sahrens }
4014789Sahrens 
4015789Sahrens /*
40161294Slling  * Given a dataset, rollback to a specific snapshot, discarding any
40171294Slling  * data changes since then and making it the active dataset.
40181294Slling  *
40191294Slling  * Any snapshots more recent than the target are destroyed, along with
40201294Slling  * their dependents.
40211294Slling  */
40221294Slling int
40231294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag)
40241294Slling {
40251294Slling 	int ret;
40261294Slling 	rollback_data_t cb = { 0 };
40271294Slling 	prop_changelist_t *clp;
40281294Slling 
40291294Slling 	/*
40301294Slling 	 * Unmount all dependendents of the dataset and the dataset itself.
40311294Slling 	 * The list we need to gather is the same as for doing rename
40321294Slling 	 */
40331294Slling 	clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0);
40341294Slling 	if (clp == NULL)
40351294Slling 		return (-1);
40361294Slling 
40371294Slling 	if ((ret = changelist_prefix(clp)) != 0)
40381294Slling 		goto out;
40391294Slling 
40401294Slling 	/*
40411294Slling 	 * Destroy all recent snapshots and its dependends.
40421294Slling 	 */
40431294Slling 	cb.cb_target = snap->zfs_name;
40441294Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
40451294Slling 	cb.cb_clp = clp;
40461294Slling 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
40471294Slling 
40481294Slling 	if ((ret = cb.cb_error) != 0) {
40491294Slling 		(void) changelist_postfix(clp);
40501294Slling 		goto out;
40511294Slling 	}
40521294Slling 
40531294Slling 	/*
40541294Slling 	 * Now that we have verified that the snapshot is the latest,
40551294Slling 	 * rollback to the given snapshot.
40561294Slling 	 */
40571294Slling 	ret = do_rollback(zhp);
40581294Slling 
40591294Slling 	if (ret != 0) {
40601294Slling 		(void) changelist_postfix(clp);
40611294Slling 		goto out;
40621294Slling 	}
40631294Slling 
40641294Slling 	/*
40651294Slling 	 * We only want to re-mount the filesystem if it was mounted in the
40661294Slling 	 * first place.
40671294Slling 	 */
40681294Slling 	ret = changelist_postfix(clp);
40691294Slling 
40701294Slling out:
40711294Slling 	changelist_free(clp);
40721294Slling 	return (ret);
40731294Slling }
40741294Slling 
40751294Slling /*
4076789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
4077789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
4078789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
4079789Sahrens  * libzfs_graph.c.
4080789Sahrens  */
4081789Sahrens int
40822474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
40832474Seschrock     zfs_iter_f func, void *data)
4084789Sahrens {
4085789Sahrens 	char **dependents;
4086789Sahrens 	size_t count;
4087789Sahrens 	int i;
4088789Sahrens 	zfs_handle_t *child;
4089789Sahrens 	int ret = 0;
4090789Sahrens 
40912474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
40922474Seschrock 	    &dependents, &count) != 0)
40932474Seschrock 		return (-1);
40942474Seschrock 
4095789Sahrens 	for (i = 0; i < count; i++) {
40962082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
40972082Seschrock 		    dependents[i])) == NULL)
4098789Sahrens 			continue;
4099789Sahrens 
4100789Sahrens 		if ((ret = func(child, data)) != 0)
4101789Sahrens 			break;
4102789Sahrens 	}
4103789Sahrens 
4104789Sahrens 	for (i = 0; i < count; i++)
4105789Sahrens 		free(dependents[i]);
4106789Sahrens 	free(dependents);
4107789Sahrens 
4108789Sahrens 	return (ret);
4109789Sahrens }
4110789Sahrens 
4111789Sahrens /*
4112789Sahrens  * Renames the given dataset.
4113789Sahrens  */
4114789Sahrens int
41154490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
4116789Sahrens {
4117789Sahrens 	int ret;
4118789Sahrens 	zfs_cmd_t zc = { 0 };
4119789Sahrens 	char *delim;
41204007Smmusante 	prop_changelist_t *cl = NULL;
41214007Smmusante 	zfs_handle_t *zhrp = NULL;
41224007Smmusante 	char *parentname = NULL;
4123789Sahrens 	char parent[ZFS_MAXNAMELEN];
41242082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
41252082Seschrock 	char errbuf[1024];
4126789Sahrens 
4127789Sahrens 	/* if we have the same exact name, just return success */
4128789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
4129789Sahrens 		return (0);
4130789Sahrens 
41312082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
41322082Seschrock 	    "cannot rename to '%s'"), target);
41332082Seschrock 
4134789Sahrens 	/*
4135789Sahrens 	 * Make sure the target name is valid
4136789Sahrens 	 */
4137789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
41382665Snd150628 		if ((strchr(target, '@') == NULL) ||
41392665Snd150628 		    *target == '@') {
41402665Snd150628 			/*
41412665Snd150628 			 * Snapshot target name is abbreviated,
41422665Snd150628 			 * reconstruct full dataset name
41432665Snd150628 			 */
41442665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
41452665Snd150628 			    sizeof (parent));
41462665Snd150628 			delim = strchr(parent, '@');
41472665Snd150628 			if (strchr(target, '@') == NULL)
41482665Snd150628 				*(++delim) = '\0';
41492665Snd150628 			else
41502665Snd150628 				*delim = '\0';
41512665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
41522665Snd150628 			target = parent;
41532665Snd150628 		} else {
41542665Snd150628 			/*
41552665Snd150628 			 * Make sure we're renaming within the same dataset.
41562665Snd150628 			 */
41572665Snd150628 			delim = strchr(target, '@');
41582665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
41592665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
41602665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41612665Snd150628 				    "snapshots must be part of same "
41622665Snd150628 				    "dataset"));
41632665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
41643912Slling 				    errbuf));
41652665Snd150628 			}
4166789Sahrens 		}
41672665Snd150628 		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
41682665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4169789Sahrens 	} else {
41704007Smmusante 		if (recursive) {
41714007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41724007Smmusante 			    "recursive rename must be a snapshot"));
41734007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
41744007Smmusante 		}
41754007Smmusante 
41762665Snd150628 		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
41772665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41782676Seschrock 		uint64_t unused;
41792676Seschrock 
4180789Sahrens 		/* validate parents */
41814490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
4182789Sahrens 			return (-1);
4183789Sahrens 
4184789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
4185789Sahrens 
4186789Sahrens 		/* make sure we're in the same pool */
4187789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4188789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4189789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
41902082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41912082Seschrock 			    "datasets must be within same pool"));
41922082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4193789Sahrens 		}
41942440Snd150628 
41952440Snd150628 		/* new name cannot be a child of the current dataset name */
41962440Snd150628 		if (strncmp(parent, zhp->zfs_name,
41973912Slling 		    strlen(zhp->zfs_name)) == 0) {
41982440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41992440Snd150628 			    "New dataset name cannot be a descendent of "
42002440Snd150628 			    "current dataset name"));
42012440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
42022440Snd150628 		}
4203789Sahrens 	}
4204789Sahrens 
42052082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
42062082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
42072082Seschrock 
4208789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4209789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
42102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42112082Seschrock 		    "dataset is used in a non-global zone"));
42122082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4213789Sahrens 	}
4214789Sahrens 
42154007Smmusante 	if (recursive) {
42164007Smmusante 		struct destroydata dd;
42174007Smmusante 
42184183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
42194183Smmusante 		if (parentname == NULL) {
42204183Smmusante 			ret = -1;
42214183Smmusante 			goto error;
42224183Smmusante 		}
42234007Smmusante 		delim = strchr(parentname, '@');
42244007Smmusante 		*delim = '\0';
42254007Smmusante 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY);
42264007Smmusante 		if (zhrp == NULL) {
42274183Smmusante 			ret = -1;
42284183Smmusante 			goto error;
42294007Smmusante 		}
42304007Smmusante 
42314007Smmusante 		dd.snapname = delim + 1;
42324007Smmusante 		dd.gotone = B_FALSE;
42334183Smmusante 		dd.closezhp = B_TRUE;
42344007Smmusante 
42354007Smmusante 		/* We remove any zvol links prior to renaming them */
42364007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
42374007Smmusante 		if (ret) {
42384007Smmusante 			goto error;
42394007Smmusante 		}
42404007Smmusante 	} else {
42414007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
42424007Smmusante 			return (-1);
42434007Smmusante 
42444007Smmusante 		if (changelist_haszonedchild(cl)) {
42454007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42464007Smmusante 			    "child dataset with inherited mountpoint is used "
42474007Smmusante 			    "in a non-global zone"));
42484007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
42494007Smmusante 			goto error;
42504007Smmusante 		}
42514007Smmusante 
42524007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
42534007Smmusante 			goto error;
4254789Sahrens 	}
4255789Sahrens 
42562676Seschrock 	if (ZFS_IS_VOLUME(zhp))
4257789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4258789Sahrens 	else
4259789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4260789Sahrens 
42612665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
42622676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
42632665Snd150628 
42644007Smmusante 	zc.zc_cookie = recursive;
42654007Smmusante 
42664543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
42674007Smmusante 		/*
42684007Smmusante 		 * if it was recursive, the one that actually failed will
42694007Smmusante 		 * be in zc.zc_name
42704007Smmusante 		 */
42714007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
42724007Smmusante 		    "cannot rename to '%s'"), zc.zc_name);
42734007Smmusante 
42744007Smmusante 		if (recursive && errno == EEXIST) {
42754007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42764007Smmusante 			    "a child dataset already has a snapshot "
42774007Smmusante 			    "with the new name"));
42784007Smmusante 			(void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf);
42794007Smmusante 		} else {
42804007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
42814007Smmusante 		}
4282789Sahrens 
4283789Sahrens 		/*
4284789Sahrens 		 * On failure, we still want to remount any filesystems that
4285789Sahrens 		 * were previously mounted, so we don't alter the system state.
4286789Sahrens 		 */
42874007Smmusante 		if (recursive) {
42884007Smmusante 			struct createdata cd;
42894007Smmusante 
42904007Smmusante 			/* only create links for datasets that had existed */
42914007Smmusante 			cd.cd_snapname = delim + 1;
42924007Smmusante 			cd.cd_ifexists = B_TRUE;
42934007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
42944007Smmusante 			    &cd);
42954007Smmusante 		} else {
42964007Smmusante 			(void) changelist_postfix(cl);
42974007Smmusante 		}
4298789Sahrens 	} else {
42994007Smmusante 		if (recursive) {
43004007Smmusante 			struct createdata cd;
43014007Smmusante 
43024007Smmusante 			/* only create links for datasets that had existed */
43034007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
43044007Smmusante 			cd.cd_ifexists = B_TRUE;
43054007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
43064007Smmusante 			    &cd);
43074007Smmusante 		} else {
43084007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
43094007Smmusante 			ret = changelist_postfix(cl);
43104007Smmusante 		}
4311789Sahrens 	}
4312789Sahrens 
4313789Sahrens error:
43144007Smmusante 	if (parentname) {
43154007Smmusante 		free(parentname);
43164007Smmusante 	}
43174007Smmusante 	if (zhrp) {
43184007Smmusante 		zfs_close(zhrp);
43194007Smmusante 	}
43204007Smmusante 	if (cl) {
43214007Smmusante 		changelist_free(cl);
43224007Smmusante 	}
4323789Sahrens 	return (ret);
4324789Sahrens }
4325789Sahrens 
4326789Sahrens /*
4327789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
4328789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
4329789Sahrens  */
4330789Sahrens int
43312082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
4332789Sahrens {
43334007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
43344007Smmusante }
43354007Smmusante 
43364007Smmusante static int
43374007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
43384007Smmusante {
4339789Sahrens 	zfs_cmd_t zc = { 0 };
43402082Seschrock 	di_devlink_handle_t dhdl;
43414543Smarks 	priv_set_t *priv_effective;
43424543Smarks 	int privileged;
4343789Sahrens 
4344789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4345789Sahrens 
4346789Sahrens 	/*
4347789Sahrens 	 * Issue the appropriate ioctl.
4348789Sahrens 	 */
43492082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4350789Sahrens 		switch (errno) {
4351789Sahrens 		case EEXIST:
4352789Sahrens 			/*
4353789Sahrens 			 * Silently ignore the case where the link already
4354789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
4355789Sahrens 			 * times without errors.
4356789Sahrens 			 */
4357789Sahrens 			return (0);
4358789Sahrens 
43594007Smmusante 		case ENOENT:
43604007Smmusante 			/*
43614007Smmusante 			 * Dataset does not exist in the kernel.  If we
43624007Smmusante 			 * don't care (see zfs_rename), then ignore the
43634007Smmusante 			 * error quietly.
43644007Smmusante 			 */
43654007Smmusante 			if (ifexists) {
43664007Smmusante 				return (0);
43674007Smmusante 			}
43684007Smmusante 
43694007Smmusante 			/* FALLTHROUGH */
43704007Smmusante 
4371789Sahrens 		default:
43723237Slling 			return (zfs_standard_error_fmt(hdl, errno,
43732082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
43742082Seschrock 			    "for '%s'"), dataset));
4375789Sahrens 		}
4376789Sahrens 	}
4377789Sahrens 
4378789Sahrens 	/*
43794543Smarks 	 * If privileged call devfsadm and wait for the links to
43804543Smarks 	 * magically appear.
43814543Smarks 	 * Otherwise, print out an informational message.
4382789Sahrens 	 */
43834543Smarks 
43844543Smarks 	priv_effective = priv_allocset();
43854543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
43864543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
43874543Smarks 	priv_freeset(priv_effective);
43884543Smarks 
43894543Smarks 	if (privileged) {
43904543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
43914543Smarks 		    DI_MAKE_LINK)) == NULL) {
43924543Smarks 			zfs_error_aux(hdl, strerror(errno));
43934543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
43944543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
43954543Smarks 			    "for '%s'"), dataset);
43964543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
43974543Smarks 			return (-1);
43984543Smarks 		} else {
43994543Smarks 			(void) di_devlink_fini(&dhdl);
44004543Smarks 		}
4401789Sahrens 	} else {
44024543Smarks 		char pathname[MAXPATHLEN];
44034543Smarks 		struct stat64 statbuf;
44044543Smarks 		int i;
44054543Smarks 
44064543Smarks #define	MAX_WAIT	10
44074543Smarks 
44084543Smarks 		/*
44094543Smarks 		 * This is the poor mans way of waiting for the link
44104543Smarks 		 * to show up.  If after 10 seconds we still don't
44114543Smarks 		 * have it, then print out a message.
44124543Smarks 		 */
44134543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
44144543Smarks 		    dataset);
44154543Smarks 
44164543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
44174543Smarks 			if (stat64(pathname, &statbuf) == 0)
44184543Smarks 				break;
44194543Smarks 			(void) sleep(1);
44204543Smarks 		}
44214543Smarks 		if (i == MAX_WAIT)
44224543Smarks 			(void) printf(gettext("%s may not be immediately "
44234543Smarks 			    "available\n"), pathname);
4424789Sahrens 	}
4425789Sahrens 
4426789Sahrens 	return (0);
4427789Sahrens }
4428789Sahrens 
4429789Sahrens /*
4430789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4431789Sahrens  */
4432789Sahrens int
44332082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4434789Sahrens {
4435789Sahrens 	zfs_cmd_t zc = { 0 };
4436789Sahrens 
4437789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4438789Sahrens 
44392082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4440789Sahrens 		switch (errno) {
4441789Sahrens 		case ENXIO:
4442789Sahrens 			/*
4443789Sahrens 			 * Silently ignore the case where the link no longer
4444789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4445789Sahrens 			 * times without errors.
4446789Sahrens 			 */
4447789Sahrens 			return (0);
4448789Sahrens 
4449789Sahrens 		default:
44503237Slling 			return (zfs_standard_error_fmt(hdl, errno,
44512082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
44522082Seschrock 			    "links for '%s'"), dataset));
4453789Sahrens 		}
4454789Sahrens 	}
4455789Sahrens 
4456789Sahrens 	return (0);
4457789Sahrens }
44582676Seschrock 
44592676Seschrock nvlist_t *
44602676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
44612676Seschrock {
44622676Seschrock 	return (zhp->zfs_user_props);
44632676Seschrock }
44642676Seschrock 
44652676Seschrock /*
44664451Seschrock  * Given a comma-separated list of properties, construct a property list
44672676Seschrock  * containing both user-defined and native properties.  This function will
44682676Seschrock  * return a NULL list if 'all' is specified, which can later be expanded on a
44692676Seschrock  * per-dataset basis by zfs_expand_proplist().
44702676Seschrock  */
44712676Seschrock int
44723912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields,
44733912Slling     zfs_proplist_t **listp, zfs_type_t type)
44742676Seschrock {
44752676Seschrock 	size_t len;
44762676Seschrock 	char *s, *p;
44772676Seschrock 	char c;
44782676Seschrock 	zfs_prop_t prop;
44792676Seschrock 	zfs_proplist_t *entry;
44802676Seschrock 	zfs_proplist_t **last;
44812676Seschrock 
44822676Seschrock 	*listp = NULL;
44832676Seschrock 	last = listp;
44842676Seschrock 
44852676Seschrock 	/*
44862676Seschrock 	 * If 'all' is specified, return a NULL list.
44872676Seschrock 	 */
44882676Seschrock 	if (strcmp(fields, "all") == 0)
44892676Seschrock 		return (0);
44902676Seschrock 
44912676Seschrock 	/*
44922676Seschrock 	 * If no fields were specified, return an error.
44932676Seschrock 	 */
44942676Seschrock 	if (fields[0] == '\0') {
44952676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44962676Seschrock 		    "no properties specified"));
44972676Seschrock 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
44982676Seschrock 		    "bad property list")));
44992676Seschrock 	}
45002676Seschrock 
45012676Seschrock 	/*
45022676Seschrock 	 * It would be nice to use getsubopt() here, but the inclusion of column
45032676Seschrock 	 * aliases makes this more effort than it's worth.
45042676Seschrock 	 */
45052676Seschrock 	s = fields;
45062676Seschrock 	while (*s != '\0') {
45072676Seschrock 		if ((p = strchr(s, ',')) == NULL) {
45082676Seschrock 			len = strlen(s);
45092676Seschrock 			p = s + len;
45102676Seschrock 		} else {
45112676Seschrock 			len = p - s;
45122676Seschrock 		}
45132676Seschrock 
45142676Seschrock 		/*
45152676Seschrock 		 * Check for empty options.
45162676Seschrock 		 */
45172676Seschrock 		if (len == 0) {
45182676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45192676Seschrock 			    "empty property name"));
45202676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP,
45212676Seschrock 			    dgettext(TEXT_DOMAIN, "bad property list")));
45222676Seschrock 		}
45232676Seschrock 
45242676Seschrock 		/*
45252676Seschrock 		 * Check all regular property names.
45262676Seschrock 		 */
45272676Seschrock 		c = s[len];
45282676Seschrock 		s[len] = '\0';
45294451Seschrock 		prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) :
45304451Seschrock 		    zfs_name_to_prop(s);
45313912Slling 
45323912Slling 		if (prop != ZFS_PROP_INVAL &&
45333912Slling 		    !zfs_prop_valid_for_type(prop, type))
45343912Slling 			prop = ZFS_PROP_INVAL;
45352676Seschrock 
45362676Seschrock 		/*
45373912Slling 		 * When no property table entry can be found, return failure if
45383912Slling 		 * this is a pool property or if this isn't a user-defined
45393912Slling 		 * dataset property,
45402676Seschrock 		 */
45413912Slling 		if (prop == ZFS_PROP_INVAL &&
45423912Slling 		    (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) {
45432676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45442676Seschrock 			    "invalid property '%s'"), s);
45452676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP,
45462676Seschrock 			    dgettext(TEXT_DOMAIN, "bad property list")));
45472676Seschrock 		}
45482676Seschrock 
45492676Seschrock 		if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL)
45502676Seschrock 			return (-1);
45512676Seschrock 
45522676Seschrock 		entry->pl_prop = prop;
45532676Seschrock 		if (prop == ZFS_PROP_INVAL) {
45542676Seschrock 			if ((entry->pl_user_prop =
45552676Seschrock 			    zfs_strdup(hdl, s)) == NULL) {
45562676Seschrock 				free(entry);
45572676Seschrock 				return (-1);
45582676Seschrock 			}
45592676Seschrock 			entry->pl_width = strlen(s);
45602676Seschrock 		} else {
45612676Seschrock 			entry->pl_width = zfs_prop_width(prop,
45622676Seschrock 			    &entry->pl_fixed);
45632676Seschrock 		}
45642676Seschrock 
45652676Seschrock 		*last = entry;
45662676Seschrock 		last = &entry->pl_next;
45672676Seschrock 
45682676Seschrock 		s = p;
45692676Seschrock 		if (c == ',')
45702676Seschrock 			s++;
45712676Seschrock 	}
45722676Seschrock 
45732676Seschrock 	return (0);
45742676Seschrock }
45752676Seschrock 
45763912Slling int
45773912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp)
45783912Slling {
45793912Slling 	return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY));
45803912Slling }
45813912Slling 
45822676Seschrock void
45832676Seschrock zfs_free_proplist(zfs_proplist_t *pl)
45842676Seschrock {
45852676Seschrock 	zfs_proplist_t *next;
45862676Seschrock 
45872676Seschrock 	while (pl != NULL) {
45882676Seschrock 		next = pl->pl_next;
45892676Seschrock 		free(pl->pl_user_prop);
45902676Seschrock 		free(pl);
45912676Seschrock 		pl = next;
45922676Seschrock 	}
45932676Seschrock }
45942676Seschrock 
45953654Sgw25295 typedef struct expand_data {
45963654Sgw25295 	zfs_proplist_t	**last;
45973654Sgw25295 	libzfs_handle_t	*hdl;
45983654Sgw25295 } expand_data_t;
45993654Sgw25295 
46003654Sgw25295 static zfs_prop_t
46013654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb)
46023654Sgw25295 {
46033654Sgw25295 	zfs_proplist_t *entry;
46043654Sgw25295 	expand_data_t *edp = cb;
46053654Sgw25295 
46063654Sgw25295 	if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL)
46073654Sgw25295 		return (ZFS_PROP_INVAL);
46083654Sgw25295 
46093654Sgw25295 	entry->pl_prop = prop;
46103654Sgw25295 	entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed);
46113654Sgw25295 	entry->pl_all = B_TRUE;
46123654Sgw25295 
46133654Sgw25295 	*(edp->last) = entry;
46143654Sgw25295 	edp->last = &entry->pl_next;
46153654Sgw25295 
46163654Sgw25295 	return (ZFS_PROP_CONT);
46173654Sgw25295 }
46183654Sgw25295 
46192676Seschrock int
46203912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp,
46213912Slling 	zfs_type_t type)
46222676Seschrock {
46232676Seschrock 	zfs_proplist_t *entry;
46243912Slling 	zfs_proplist_t **last;
46253654Sgw25295 	expand_data_t exp;
46262676Seschrock 
46272676Seschrock 	if (*plp == NULL) {
46282676Seschrock 		/*
46292676Seschrock 		 * If this is the very first time we've been called for an 'all'
46302676Seschrock 		 * specification, expand the list to include all native
46312676Seschrock 		 * properties.
46322676Seschrock 		 */
46332676Seschrock 		last = plp;
46343654Sgw25295 
46353654Sgw25295 		exp.last = last;
46363654Sgw25295 		exp.hdl = hdl;
46373654Sgw25295 
46383912Slling 		if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type,
46394597Stimf 		    B_FALSE, B_FALSE) == ZFS_PROP_INVAL)
46403654Sgw25295 			return (-1);
46412676Seschrock 
46422676Seschrock 		/*
46432676Seschrock 		 * Add 'name' to the beginning of the list, which is handled
46442676Seschrock 		 * specially.
46452676Seschrock 		 */
46462676Seschrock 		if ((entry = zfs_alloc(hdl,
46472676Seschrock 		    sizeof (zfs_proplist_t))) == NULL)
46482676Seschrock 			return (-1);
46492676Seschrock 
46502676Seschrock 		entry->pl_prop = ZFS_PROP_NAME;
46512676Seschrock 		entry->pl_width = zfs_prop_width(ZFS_PROP_NAME,
46522676Seschrock 		    &entry->pl_fixed);
46532676Seschrock 		entry->pl_all = B_TRUE;
46542676Seschrock 		entry->pl_next = *plp;
46552676Seschrock 		*plp = entry;
46562676Seschrock 	}
46573912Slling 	return (0);
46583912Slling }
46593912Slling 
46603912Slling /*
46613912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
46623912Slling  * display, and their maximum widths.  This does two main things:
46633912Slling  *
46643912Slling  *      - If this is a list of all properties, then expand the list to include
46653912Slling  *        all native properties, and set a flag so that for each dataset we look
46663912Slling  *        for new unique user properties and add them to the list.
46673912Slling  *
46683912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
46693912Slling  *        so that we can size the column appropriately.
46703912Slling  */
46713912Slling int
46723912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp)
46733912Slling {
46743912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
46753912Slling 	zfs_proplist_t *entry;
46763912Slling 	zfs_proplist_t **last, **start;
46773912Slling 	nvlist_t *userprops, *propval;
46783912Slling 	nvpair_t *elem;
46793912Slling 	char *strval;
46803912Slling 	char buf[ZFS_MAXPROPLEN];
46813912Slling 
46823912Slling 	if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0)
46833912Slling 		return (-1);
46842676Seschrock 
46852676Seschrock 	userprops = zfs_get_user_props(zhp);
46862676Seschrock 
46872676Seschrock 	entry = *plp;
46882676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
46892676Seschrock 		/*
46902676Seschrock 		 * Go through and add any user properties as necessary.  We
46912676Seschrock 		 * start by incrementing our list pointer to the first
46922676Seschrock 		 * non-native property.
46932676Seschrock 		 */
46942676Seschrock 		start = plp;
46952676Seschrock 		while (*start != NULL) {
46962676Seschrock 			if ((*start)->pl_prop == ZFS_PROP_INVAL)
46972676Seschrock 				break;
46982676Seschrock 			start = &(*start)->pl_next;
46992676Seschrock 		}
47002676Seschrock 
47012676Seschrock 		elem = NULL;
47022676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
47032676Seschrock 			/*
47042676Seschrock 			 * See if we've already found this property in our list.
47052676Seschrock 			 */
47062676Seschrock 			for (last = start; *last != NULL;
47072676Seschrock 			    last = &(*last)->pl_next) {
47082676Seschrock 				if (strcmp((*last)->pl_user_prop,
47092676Seschrock 				    nvpair_name(elem)) == 0)
47102676Seschrock 					break;
47112676Seschrock 			}
47122676Seschrock 
47132676Seschrock 			if (*last == NULL) {
47142676Seschrock 				if ((entry = zfs_alloc(hdl,
47152676Seschrock 				    sizeof (zfs_proplist_t))) == NULL ||
47162676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
47172676Seschrock 				    nvpair_name(elem)))) == NULL) {
47182676Seschrock 					free(entry);
47192676Seschrock 					return (-1);
47202676Seschrock 				}
47212676Seschrock 
47222676Seschrock 				entry->pl_prop = ZFS_PROP_INVAL;
47232676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
47242676Seschrock 				entry->pl_all = B_TRUE;
47252676Seschrock 				*last = entry;
47262676Seschrock 			}
47272676Seschrock 		}
47282676Seschrock 	}
47292676Seschrock 
47302676Seschrock 	/*
47312676Seschrock 	 * Now go through and check the width of any non-fixed columns
47322676Seschrock 	 */
47332676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
47342676Seschrock 		if (entry->pl_fixed)
47352676Seschrock 			continue;
47362676Seschrock 
47372676Seschrock 		if (entry->pl_prop != ZFS_PROP_INVAL) {
47382676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
47392676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
47402676Seschrock 				if (strlen(buf) > entry->pl_width)
47412676Seschrock 					entry->pl_width = strlen(buf);
47422676Seschrock 			}
47432676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
47442676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
47452676Seschrock 			verify(nvlist_lookup_string(propval,
47462676Seschrock 			    ZFS_PROP_VALUE, &strval) == 0);
47472676Seschrock 			if (strlen(strval) > entry->pl_width)
47482676Seschrock 				entry->pl_width = strlen(strval);
47492676Seschrock 		}
47502676Seschrock 	}
47512676Seschrock 
47522676Seschrock 	return (0);
47532676Seschrock }
47544543Smarks 
47554543Smarks int
47564543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
47574543Smarks {
47584543Smarks 	zfs_cmd_t zc = { 0 };
47594543Smarks 	nvlist_t *nvp;
47604543Smarks 	size_t sz;
47614543Smarks 	gid_t gid;
47624543Smarks 	uid_t uid;
47634543Smarks 	const gid_t *groups;
47644543Smarks 	int group_cnt;
47654543Smarks 	int error;
47664543Smarks 
47674543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
47684543Smarks 		return (no_memory(hdl));
47694543Smarks 
47704543Smarks 	uid = ucred_geteuid(cred);
47714543Smarks 	gid = ucred_getegid(cred);
47724543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
47734543Smarks 
47744543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
47754543Smarks 		return (1);
47764543Smarks 
47774543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
47784543Smarks 		nvlist_free(nvp);
47794543Smarks 		return (1);
47804543Smarks 	}
47814543Smarks 
47824543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
47834543Smarks 		nvlist_free(nvp);
47844543Smarks 		return (1);
47854543Smarks 	}
47864543Smarks 
47874543Smarks 	if (nvlist_add_uint32_array(nvp,
47884543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
47894543Smarks 		nvlist_free(nvp);
47904543Smarks 		return (1);
47914543Smarks 	}
47924543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
47934543Smarks 
47944543Smarks 	if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz))
47954543Smarks 		return (-1);
47964543Smarks 
47974543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
47984543Smarks 	nvlist_free(nvp);
47994543Smarks 	return (error);
48004543Smarks }
48014543Smarks 
48024543Smarks int
48034543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
48044543Smarks     void *export, void *sharetab, int sharemax, boolean_t share_on)
48054543Smarks {
48064543Smarks 	zfs_cmd_t zc = { 0 };
48074543Smarks 	int error;
48084543Smarks 
48094543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
48104543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
48114543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
48124543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
48134543Smarks 	zc.zc_share.z_sharetype = share_on;
48144543Smarks 	zc.zc_share.z_sharemax = sharemax;
48154543Smarks 
48164543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
48174543Smarks 	return (error);
48184543Smarks }
4819