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)) {
8424787Sahrens 		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 
8494787Sahrens 		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 
8664787Sahrens 		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 
8734787Sahrens 		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:
9474778Srm160521 		{
9484778Srm160521 			namecheck_err_t why;
9494778Srm160521 
9502676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
9512676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9522676Seschrock 				break;
9532676Seschrock 
9544778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
9554778Srm160521 				switch (why) {
9564778Srm160521 				case NAME_ERR_LEADING_SLASH:
9574778Srm160521 					zfs_error_aux(hdl,
9584778Srm160521 					    dgettext(TEXT_DOMAIN,
9594778Srm160521 					    "'%s' must be an absolute path, "
9604778Srm160521 					    "'none', or 'legacy'"), propname);
9614778Srm160521 					break;
9624778Srm160521 				case NAME_ERR_TOOLONG:
9634778Srm160521 					zfs_error_aux(hdl,
9644778Srm160521 					    dgettext(TEXT_DOMAIN,
9654778Srm160521 					    "component of '%s' is too long"),
9664778Srm160521 					    propname);
9674778Srm160521 					break;
9684778Srm160521 				}
9692676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9702676Seschrock 				goto error;
971789Sahrens 			}
9724778Srm160521 		}
9734778Srm160521 
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];
13154787Sahrens 	char *who_tok, *perm;
13164543Smarks 	int error;
13174543Smarks 
13184543Smarks 	*nvp = NULL;
13194543Smarks 
13204543Smarks 	if (perms) {
13214543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
13224543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
13234543Smarks 			return (1);
13244543Smarks 		}
13254543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
13264543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
13274543Smarks 			nvlist_free(perms_nvp);
13284543Smarks 			return (1);
13294543Smarks 		}
13304543Smarks 	}
13314543Smarks 
13324543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
13334543Smarks 		if (perms_nvp)
13344543Smarks 			nvlist_free(perms_nvp);
13354543Smarks 		if (sets_nvp)
13364543Smarks 			nvlist_free(sets_nvp);
13374543Smarks 		return (1);
13384543Smarks 	}
13394543Smarks 
13404543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
13414543Smarks 		namecheck_err_t why;
13424543Smarks 		char what;
13434543Smarks 
13444543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
13454787Sahrens 			nvlist_free(who_nvp);
13464787Sahrens 			if (perms_nvp)
13474787Sahrens 				nvlist_free(perms_nvp);
13484787Sahrens 			if (sets_nvp)
13494787Sahrens 				nvlist_free(sets_nvp);
13504787Sahrens 
13514543Smarks 			switch (why) {
13524543Smarks 			case NAME_ERR_NO_AT:
13534543Smarks 				zfs_error_aux(zhp->zfs_hdl,
13544543Smarks 				    dgettext(TEXT_DOMAIN,
13554543Smarks 				    "set definition must begin with an '@' "
13564543Smarks 				    "character"));
13574543Smarks 			}
13584543Smarks 			return (zfs_error(zhp->zfs_hdl,
13594543Smarks 			    EZFS_BADPERMSET, whostr));
13604543Smarks 		}
13614543Smarks 	}
13624543Smarks 
13634543Smarks 	/*
13644543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
13654543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
13664543Smarks 	 * other sets_nvp will have only permssion set names in it.
13674543Smarks 	 */
13684787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
13694787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
13704787Sahrens 
13714787Sahrens 		if (perm_canonical) {
13724787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
13734787Sahrens 			    perm_canonical) == 0);
13744787Sahrens 		} else if (perm[0] == '@') {
13754787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
13764787Sahrens 		} else {
13774787Sahrens 			nvlist_free(who_nvp);
13784787Sahrens 			nvlist_free(perms_nvp);
13794787Sahrens 			nvlist_free(sets_nvp);
13804787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
13814543Smarks 		}
13824543Smarks 	}
13834543Smarks 
13844543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
13854543Smarks 		who_tok = strtok(whostr, ",");
13864543Smarks 		if (who_tok == NULL) {
13874543Smarks 			nvlist_free(who_nvp);
13884787Sahrens 			if (perms_nvp)
13894787Sahrens 				nvlist_free(perms_nvp);
13904543Smarks 			if (sets_nvp)
13914543Smarks 				nvlist_free(sets_nvp);
13924543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
13934543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
13944543Smarks 			    whostr);
13954543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
13964543Smarks 		}
13974543Smarks 	}
13984543Smarks 
13994543Smarks 	/*
14004543Smarks 	 * Now create the nvlist(s)
14014543Smarks 	 */
14024543Smarks 	do {
14034543Smarks 		uint64_t who_id;
14044543Smarks 
14054543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
14064543Smarks 		    &who_id);
14074543Smarks 		if (error) {
14084543Smarks 			nvlist_free(who_nvp);
14094787Sahrens 			if (perms_nvp)
14104787Sahrens 				nvlist_free(perms_nvp);
14114543Smarks 			if (sets_nvp)
14124543Smarks 				nvlist_free(sets_nvp);
14134543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14144543Smarks 			    dgettext(TEXT_DOMAIN,
14154543Smarks 			    "Unable to determine uid/gid for "
14164543Smarks 			    "%s "), who_tok);
14174543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
14184543Smarks 		}
14194543Smarks 
14204543Smarks 		/*
14214543Smarks 		 * add entries for both local and descendent when required
14224543Smarks 		 */
14234543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
14244543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
14254543Smarks 
14264543Smarks 	} while (who_tok = strtok(NULL, ","));
14274543Smarks 	*nvp = who_nvp;
14284543Smarks 	return (0);
14294543Smarks }
14304543Smarks 
14314543Smarks static int
14324543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
14334543Smarks {
14344543Smarks 	zfs_cmd_t zc = { 0 };
14354543Smarks 	int error;
14364543Smarks 	size_t sz;
14374543Smarks 	char errbuf[1024];
14384543Smarks 
14394543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
14404543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
14414543Smarks 	    zhp->zfs_name);
14424543Smarks 
14434543Smarks 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz))
14444543Smarks 		return (-1);
14454543Smarks 
14464543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14474543Smarks 	zc.zc_perm_action = unset;
14484543Smarks 
14494543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
14504543Smarks 	if (error && errno == ENOTSUP) {
14514543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
14524543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
14534543Smarks 		zcmd_free_nvlists(&zc);
14544543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
14554543Smarks 	} else if (error) {
14564543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
14574543Smarks 	}
14584543Smarks 	zcmd_free_nvlists(&zc);
14594543Smarks 
14604543Smarks 	return (error);
14614543Smarks }
14624543Smarks 
14634543Smarks int
14644543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
14654543Smarks {
14664543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
14674543Smarks }
14684543Smarks 
14694543Smarks int
14704543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
14714543Smarks {
14724543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
14734543Smarks }
14744543Smarks 
14754543Smarks static int
14764543Smarks perm_compare(const void *arg1, const void *arg2)
14774543Smarks {
14784543Smarks 	const zfs_perm_node_t *node1 = arg1;
14794543Smarks 	const zfs_perm_node_t *node2 = arg2;
14804543Smarks 	int ret;
14814543Smarks 
14824543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
14834543Smarks 
14844543Smarks 	if (ret > 0)
14854543Smarks 		return (1);
14864543Smarks 	if (ret < 0)
14874543Smarks 		return (-1);
14884543Smarks 	else
14894543Smarks 		return (0);
14904543Smarks }
14914543Smarks 
14924543Smarks static void
14934543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
14944543Smarks {
14954543Smarks 	zfs_perm_node_t *permnode;
14964543Smarks 	void *cookie;
14974543Smarks 
14984543Smarks 	cookie = NULL;
14994543Smarks 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL) {
15004543Smarks 		avl_remove(tree, permnode);
15014543Smarks 		free(permnode);
15024543Smarks 	}
15034543Smarks }
15044543Smarks 
15054543Smarks static void
15064543Smarks zfs_destroy_tree(avl_tree_t *tree)
15074543Smarks {
15084543Smarks 	zfs_allow_node_t *allownode;
15094543Smarks 	void *cookie;
15104543Smarks 
15114543Smarks 	cookie = NULL;
15124543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
15134543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
15144543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
15154543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
15164543Smarks 		avl_remove(tree, allownode);
15174543Smarks 		free(allownode);
15184543Smarks 	}
15194543Smarks }
15204543Smarks 
15214543Smarks void
15224543Smarks zfs_free_allows(zfs_allow_t *allow)
15234543Smarks {
15244543Smarks 	zfs_allow_t *allownext;
15254543Smarks 	zfs_allow_t *freeallow;
15264543Smarks 
15274543Smarks 	allownext = allow;
15284543Smarks 	while (allownext) {
15294543Smarks 		zfs_destroy_tree(&allownext->z_sets);
15304543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
15314543Smarks 		zfs_destroy_tree(&allownext->z_user);
15324543Smarks 		zfs_destroy_tree(&allownext->z_group);
15334543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
15344543Smarks 		freeallow = allownext;
15354543Smarks 		allownext = allownext->z_next;
15364543Smarks 		free(freeallow);
15374543Smarks 	}
15384543Smarks }
15394543Smarks 
15404543Smarks static zfs_allow_t *
15414543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
15424543Smarks {
15434543Smarks 	zfs_allow_t *ptree;
15444543Smarks 
15454543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
15464543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
15474543Smarks 		return (NULL);
15484543Smarks 	}
15494543Smarks 
15504543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
15514543Smarks 	avl_create(&ptree->z_sets,
15524543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15534543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15544543Smarks 	avl_create(&ptree->z_crperms,
15554543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15564543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15574543Smarks 	avl_create(&ptree->z_user,
15584543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15594543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15604543Smarks 	avl_create(&ptree->z_group,
15614543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15624543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15634543Smarks 	avl_create(&ptree->z_everyone,
15644543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15654543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15664543Smarks 
15674543Smarks 	if (prev)
15684543Smarks 		prev->z_next = ptree;
15694543Smarks 	ptree->z_next = NULL;
15704543Smarks 	return (ptree);
15714543Smarks }
15724543Smarks 
15734543Smarks /*
15744543Smarks  * Add permissions to the appropriate AVL permission tree.
15754543Smarks  * The appropriate tree may not be the requested tree.
15764543Smarks  * For example if ld indicates a local permission, but
15774543Smarks  * same permission also exists as a descendent permission
15784543Smarks  * then the permission will be removed from the descendent
15794543Smarks  * tree and add the the local+descendent tree.
15804543Smarks  */
15814543Smarks static int
15824543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
15834543Smarks     char *perm, char ld)
15844543Smarks {
15854543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
15864543Smarks 	zfs_perm_node_t *newnode;
15874543Smarks 	avl_index_t where, where2;
15884543Smarks 	avl_tree_t *tree, *altree;
15894543Smarks 
15904543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
15914543Smarks 
15924543Smarks 	if (ld == ZFS_DELEG_NA) {
15934543Smarks 		tree =  &allownode->z_localdescend;
15944543Smarks 		altree = &allownode->z_descend;
15954543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
15964543Smarks 		tree = &allownode->z_local;
15974543Smarks 		altree = &allownode->z_descend;
15984543Smarks 	} else {
15994543Smarks 		tree = &allownode->z_descend;
16004543Smarks 		altree = &allownode->z_local;
16014543Smarks 	}
16024543Smarks 	permnode = avl_find(tree, &pnode, &where);
16034543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
16044543Smarks 
16054543Smarks 	if (permnode2) {
16064543Smarks 		avl_remove(altree, permnode2);
16074543Smarks 		free(permnode2);
16084543Smarks 		if (permnode == NULL) {
16094543Smarks 			tree =  &allownode->z_localdescend;
16104543Smarks 		}
16114543Smarks 	}
16124543Smarks 
16134543Smarks 	/*
16144543Smarks 	 * Now insert new permission in either requested location
16154543Smarks 	 * local/descendent or into ld when perm will exist in both.
16164543Smarks 	 */
16174543Smarks 	if (permnode == NULL) {
16184543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
16194543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
16204543Smarks 			return (-1);
16214543Smarks 		}
16224543Smarks 		*newnode = pnode;
16234543Smarks 		avl_add(tree, newnode);
16244543Smarks 	}
16254543Smarks 	return (0);
16264543Smarks }
16274577Sahrens 
16284543Smarks /*
16294543Smarks  * Uggh, this is going to be a bit complicated.
16304543Smarks  * we have an nvlist coming out of the kernel that
16314543Smarks  * will indicate where the permission is set and then
16324543Smarks  * it will contain allow of the various "who's", and what
16334543Smarks  * their permissions are.  To further complicate this
16344543Smarks  * we will then have to coalesce the local,descendent
16354543Smarks  * and local+descendent permissions where appropriate.
16364543Smarks  * The kernel only knows about a permission as being local
16374543Smarks  * or descendent, but not both.
16384543Smarks  *
16394543Smarks  * In order to make this easier for zfs_main to deal with
16404543Smarks  * a series of AVL trees will be used to maintain
16414543Smarks  * all of this, primarily for sorting purposes as well
16424543Smarks  * as the ability to quickly locate a specific entry.
16434543Smarks  *
16444543Smarks  * What we end up with are tree's for sets, create perms,
16454543Smarks  * user, groups and everyone.  With each of those trees
16464543Smarks  * we have subtrees for local, descendent and local+descendent
16474543Smarks  * permissions.
16484543Smarks  */
16494543Smarks int
16504543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
16514543Smarks {
16524543Smarks 	zfs_cmd_t zc = { 0 };
16534543Smarks 	int error;
16544543Smarks 	nvlist_t *nvlist;
16554543Smarks 	nvlist_t *permnv, *sourcenv;
16564543Smarks 	nvpair_t *who_pair, *source_pair;
16574543Smarks 	nvpair_t *perm_pair;
16584543Smarks 	char errbuf[1024];
16594543Smarks 	zfs_allow_t *zallowp, *newallowp;
16604543Smarks 	char  ld;
16614543Smarks 	char *nvpname;
16624543Smarks 	uid_t	uid;
16634543Smarks 	gid_t	gid;
16644543Smarks 	avl_tree_t *tree;
16654543Smarks 	avl_index_t where;
16664543Smarks 
16674543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16684543Smarks 
16694543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
16704543Smarks 		return (-1);
16714543Smarks 
16724543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
16734543Smarks 		if (errno == ENOMEM) {
16744543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
16754543Smarks 				zcmd_free_nvlists(&zc);
16764543Smarks 				return (-1);
16774543Smarks 			}
16784543Smarks 		} else if (errno == ENOTSUP) {
16794543Smarks 			zcmd_free_nvlists(&zc);
16804543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
16814543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
16824543Smarks 			return (zfs_error(zhp->zfs_hdl,
16834543Smarks 			    EZFS_BADVERSION, errbuf));
16844543Smarks 		} else {
16854543Smarks 			zcmd_free_nvlists(&zc);
16864543Smarks 			return (-1);
16874543Smarks 		}
16884543Smarks 	}
16894543Smarks 
16904543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
16914543Smarks 		zcmd_free_nvlists(&zc);
16924543Smarks 		return (-1);
16934543Smarks 	}
16944543Smarks 
16954543Smarks 	zcmd_free_nvlists(&zc);
16964543Smarks 
16974543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
16984543Smarks 
16994543Smarks 	if (source_pair == NULL) {
17004543Smarks 		*zfs_perms = NULL;
17014543Smarks 		return (0);
17024543Smarks 	}
17034543Smarks 
17044543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
17054543Smarks 	if (*zfs_perms == NULL) {
17064543Smarks 		return (0);
17074543Smarks 	}
17084543Smarks 
17094543Smarks 	zallowp = *zfs_perms;
17104543Smarks 
17114543Smarks 	for (;;) {
17124543Smarks 		struct passwd *pwd;
17134543Smarks 		struct group *grp;
17144543Smarks 		zfs_allow_node_t *allownode;
17154543Smarks 		zfs_allow_node_t  findallownode;
17164543Smarks 		zfs_allow_node_t *newallownode;
17174543Smarks 
17184543Smarks 		(void) strlcpy(zallowp->z_setpoint,
17194543Smarks 		    nvpair_name(source_pair),
17204543Smarks 		    sizeof (zallowp->z_setpoint));
17214543Smarks 
17224543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
17234543Smarks 			goto abort;
17244543Smarks 
17254543Smarks 		/*
17264543Smarks 		 * Make sure nvlist is composed correctly
17274543Smarks 		 */
17284543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
17294543Smarks 			goto abort;
17304543Smarks 		}
17314543Smarks 
17324543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
17334543Smarks 		if (who_pair == NULL) {
17344543Smarks 			goto abort;
17354543Smarks 		}
17364543Smarks 
17374543Smarks 		do {
17384543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
17394543Smarks 			if (error) {
17404543Smarks 				goto abort;
17414543Smarks 			}
17424543Smarks 
17434543Smarks 			/*
17444543Smarks 			 * First build up the key to use
17454543Smarks 			 * for looking up in the various
17464543Smarks 			 * who trees.
17474543Smarks 			 */
17484543Smarks 			ld = nvpair_name(who_pair)[1];
17494543Smarks 			nvpname = nvpair_name(who_pair);
17504543Smarks 			switch (nvpair_name(who_pair)[0]) {
17514543Smarks 			case ZFS_DELEG_USER:
17524543Smarks 			case ZFS_DELEG_USER_SETS:
17534543Smarks 				tree = &zallowp->z_user;
17544543Smarks 				uid = atol(&nvpname[3]);
17554543Smarks 				pwd = getpwuid(uid);
17564543Smarks 				(void) snprintf(findallownode.z_key,
17574543Smarks 				    sizeof (findallownode.z_key), "user %s",
17584543Smarks 				    (pwd) ? pwd->pw_name :
17594543Smarks 				    &nvpair_name(who_pair)[3]);
17604543Smarks 				break;
17614543Smarks 			case ZFS_DELEG_GROUP:
17624543Smarks 			case ZFS_DELEG_GROUP_SETS:
17634543Smarks 				tree = &zallowp->z_group;
17644543Smarks 				gid = atol(&nvpname[3]);
17654543Smarks 				grp = getgrgid(gid);
17664543Smarks 				(void) snprintf(findallownode.z_key,
17674543Smarks 				    sizeof (findallownode.z_key), "group %s",
17684543Smarks 				    (grp) ? grp->gr_name :
17694543Smarks 				    &nvpair_name(who_pair)[3]);
17704543Smarks 				break;
17714543Smarks 			case ZFS_DELEG_CREATE:
17724543Smarks 			case ZFS_DELEG_CREATE_SETS:
17734543Smarks 				tree = &zallowp->z_crperms;
17744543Smarks 				(void) strlcpy(findallownode.z_key, "",
17754543Smarks 				    sizeof (findallownode.z_key));
17764543Smarks 				break;
17774543Smarks 			case ZFS_DELEG_EVERYONE:
17784543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
17794543Smarks 				(void) snprintf(findallownode.z_key,
17804543Smarks 				    sizeof (findallownode.z_key), "everyone");
17814543Smarks 				tree = &zallowp->z_everyone;
17824543Smarks 				break;
17834543Smarks 			case ZFS_DELEG_NAMED_SET:
17844543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
17854543Smarks 				(void) snprintf(findallownode.z_key,
17864543Smarks 				    sizeof (findallownode.z_key), "%s",
17874543Smarks 				    &nvpair_name(who_pair)[3]);
17884543Smarks 				tree = &zallowp->z_sets;
17894543Smarks 				break;
17904543Smarks 			}
17914543Smarks 
17924543Smarks 			/*
17934543Smarks 			 * Place who in tree
17944543Smarks 			 */
17954543Smarks 			allownode = avl_find(tree, &findallownode, &where);
17964543Smarks 			if (allownode == NULL) {
17974543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
17984543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
17994543Smarks 					goto abort;
18004543Smarks 				}
18014543Smarks 				avl_create(&newallownode->z_localdescend,
18024543Smarks 				    perm_compare,
18034543Smarks 				    sizeof (zfs_perm_node_t),
18044543Smarks 				    offsetof(zfs_perm_node_t, z_node));
18054543Smarks 				avl_create(&newallownode->z_local,
18064543Smarks 				    perm_compare,
18074543Smarks 				    sizeof (zfs_perm_node_t),
18084543Smarks 				    offsetof(zfs_perm_node_t, z_node));
18094543Smarks 				avl_create(&newallownode->z_descend,
18104543Smarks 				    perm_compare,
18114543Smarks 				    sizeof (zfs_perm_node_t),
18124543Smarks 				    offsetof(zfs_perm_node_t, z_node));
18134543Smarks 				(void) strlcpy(newallownode->z_key,
18144543Smarks 				    findallownode.z_key,
18154543Smarks 				    sizeof (findallownode.z_key));
18164543Smarks 				avl_insert(tree, newallownode, where);
18174543Smarks 				allownode = newallownode;
18184543Smarks 			}
18194543Smarks 
18204543Smarks 			/*
18214543Smarks 			 * Now iterate over the permissions and
18224543Smarks 			 * place them in the appropriate local,
18234543Smarks 			 * descendent or local+descendent tree.
18244543Smarks 			 *
18254543Smarks 			 * The permissions are added to the tree
18264543Smarks 			 * via zfs_coalesce_perm().
18274543Smarks 			 */
18284543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
18294543Smarks 			if (perm_pair == NULL)
18304543Smarks 				goto abort;
18314543Smarks 			do {
18324543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
18334543Smarks 				    nvpair_name(perm_pair), ld) != 0)
18344543Smarks 					goto abort;
18354543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
18364543Smarks 			    perm_pair));
18374543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
18384543Smarks 
18394543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
18404543Smarks 		if (source_pair == NULL)
18414543Smarks 			break;
18424543Smarks 
18434543Smarks 		/*
18444543Smarks 		 * allocate another node from the link list of
18454543Smarks 		 * zfs_allow_t structures
18464543Smarks 		 */
18474543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
18484543Smarks 		    nvpair_name(source_pair));
18494543Smarks 		if (newallowp == NULL) {
18504543Smarks 			goto abort;
18514543Smarks 		}
18524543Smarks 		zallowp = newallowp;
18534543Smarks 	}
18544543Smarks 	nvlist_free(nvlist);
18554543Smarks 	return (0);
18564543Smarks abort:
18574543Smarks 	zfs_free_allows(*zfs_perms);
18584543Smarks 	nvlist_free(nvlist);
18594543Smarks 	return (-1);
18604543Smarks }
18614543Smarks 
1862789Sahrens /*
1863789Sahrens  * Given a property name and value, set the property for the given dataset.
1864789Sahrens  */
1865789Sahrens int
18662676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1867789Sahrens {
1868789Sahrens 	zfs_cmd_t zc = { 0 };
18692676Seschrock 	int ret = -1;
18702676Seschrock 	prop_changelist_t *cl = NULL;
18712082Seschrock 	char errbuf[1024];
18722082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
18732676Seschrock 	nvlist_t *nvl = NULL, *realprops;
18742676Seschrock 	zfs_prop_t prop;
18752082Seschrock 
18762082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
18772676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
18782082Seschrock 	    zhp->zfs_name);
18792082Seschrock 
18802676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
18812676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
18822676Seschrock 		(void) no_memory(hdl);
18832676Seschrock 		goto error;
1884789Sahrens 	}
1885789Sahrens 
18863912Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl,
18872676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
18882676Seschrock 		goto error;
18892676Seschrock 	nvlist_free(nvl);
18902676Seschrock 	nvl = realprops;
18912676Seschrock 
18922676Seschrock 	prop = zfs_name_to_prop(propname);
18932676Seschrock 
1894789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
18952676Seschrock 		goto error;
1896789Sahrens 
1897789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
18982082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18992082Seschrock 		    "child dataset with inherited mountpoint is used "
19002082Seschrock 		    "in a non-global zone"));
19012082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1902789Sahrens 		goto error;
1903789Sahrens 	}
1904789Sahrens 
1905789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1906789Sahrens 		goto error;
1907789Sahrens 
1908789Sahrens 	/*
1909789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1910789Sahrens 	 */
1911789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1912789Sahrens 
19132676Seschrock 	if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0)
19142676Seschrock 		goto error;
19152676Seschrock 
19164543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1917789Sahrens 
1918789Sahrens 	if (ret != 0) {
1919789Sahrens 		switch (errno) {
1920789Sahrens 
1921789Sahrens 		case ENOSPC:
1922789Sahrens 			/*
1923789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1924789Sahrens 			 * something different; setting a quota or reservation
1925789Sahrens 			 * doesn't use any disk space.
1926789Sahrens 			 */
1927789Sahrens 			switch (prop) {
1928789Sahrens 			case ZFS_PROP_QUOTA:
19292082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19302082Seschrock 				    "size is less than current used or "
19312082Seschrock 				    "reserved space"));
19322082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1933789Sahrens 				break;
1934789Sahrens 
1935789Sahrens 			case ZFS_PROP_RESERVATION:
19362082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19372082Seschrock 				    "size is greater than available space"));
19382082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1939789Sahrens 				break;
1940789Sahrens 
1941789Sahrens 			default:
19422082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1943789Sahrens 				break;
1944789Sahrens 			}
1945789Sahrens 			break;
1946789Sahrens 
1947789Sahrens 		case EBUSY:
19482082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
19492082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
19502082Seschrock 			else
19512676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1952789Sahrens 			break;
1953789Sahrens 
19541175Slling 		case EROFS:
19552082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
19561175Slling 			break;
19571175Slling 
19583886Sahl 		case ENOTSUP:
19593886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19604603Sahrens 			    "pool must be upgraded to set this "
19614603Sahrens 			    "property or value"));
19623886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
19633886Sahl 			break;
19643886Sahl 
1965789Sahrens 		case EOVERFLOW:
1966789Sahrens 			/*
1967789Sahrens 			 * This platform can't address a volume this big.
1968789Sahrens 			 */
1969789Sahrens #ifdef _ILP32
1970789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
19712082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1972789Sahrens 				break;
1973789Sahrens 			}
1974789Sahrens #endif
19752082Seschrock 			/* FALLTHROUGH */
1976789Sahrens 		default:
19772082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1978789Sahrens 		}
1979789Sahrens 	} else {
1980789Sahrens 		/*
1981789Sahrens 		 * Refresh the statistics so the new property value
1982789Sahrens 		 * is reflected.
1983789Sahrens 		 */
19842676Seschrock 		if ((ret = changelist_postfix(cl)) == 0)
19852676Seschrock 			(void) get_stats(zhp);
1986789Sahrens 	}
1987789Sahrens 
1988789Sahrens error:
19892676Seschrock 	nvlist_free(nvl);
19902676Seschrock 	zcmd_free_nvlists(&zc);
19912676Seschrock 	if (cl)
19922676Seschrock 		changelist_free(cl);
1993789Sahrens 	return (ret);
1994789Sahrens }
1995789Sahrens 
1996789Sahrens /*
1997789Sahrens  * Given a property, inherit the value from the parent dataset.
1998789Sahrens  */
1999789Sahrens int
20002676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
2001789Sahrens {
2002789Sahrens 	zfs_cmd_t zc = { 0 };
2003789Sahrens 	int ret;
2004789Sahrens 	prop_changelist_t *cl;
20052082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
20062082Seschrock 	char errbuf[1024];
20072676Seschrock 	zfs_prop_t prop;
20082082Seschrock 
20092082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
20102082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2011789Sahrens 
20122676Seschrock 	if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) {
20132676Seschrock 		/*
20142676Seschrock 		 * For user properties, the amount of work we have to do is very
20152676Seschrock 		 * small, so just do it here.
20162676Seschrock 		 */
20172676Seschrock 		if (!zfs_prop_user(propname)) {
20182676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20192676Seschrock 			    "invalid property"));
20202676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
20212676Seschrock 		}
20222676Seschrock 
20232676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
20242676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
20252676Seschrock 
20264543Smarks 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0)
20272676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
20282676Seschrock 
20292676Seschrock 		return (0);
20302676Seschrock 	}
20312676Seschrock 
2032789Sahrens 	/*
2033789Sahrens 	 * Verify that this property is inheritable.
2034789Sahrens 	 */
20352082Seschrock 	if (zfs_prop_readonly(prop))
20362082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
20372082Seschrock 
20382082Seschrock 	if (!zfs_prop_inheritable(prop))
20392082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2040789Sahrens 
2041789Sahrens 	/*
2042789Sahrens 	 * Check to see if the value applies to this type
2043789Sahrens 	 */
20442082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
20452082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2046789Sahrens 
20473443Srm160521 	/*
20483443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
20493443Srm160521 	 */
20503443Srm160521 	propname = zfs_prop_to_name(prop);
2051789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
20522676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2053789Sahrens 
2054789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2055789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
20562082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20572082Seschrock 		    "dataset is used in a non-global zone"));
20582082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2059789Sahrens 	}
2060789Sahrens 
2061789Sahrens 	/*
2062789Sahrens 	 * Determine datasets which will be affected by this change, if any.
2063789Sahrens 	 */
2064789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
2065789Sahrens 		return (-1);
2066789Sahrens 
2067789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
20682082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20692082Seschrock 		    "child dataset with inherited mountpoint is used "
20702082Seschrock 		    "in a non-global zone"));
20712082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2072789Sahrens 		goto error;
2073789Sahrens 	}
2074789Sahrens 
2075789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
2076789Sahrens 		goto error;
2077789Sahrens 
20784543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) {
20792082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2080789Sahrens 	} else {
2081789Sahrens 
20822169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
2083789Sahrens 			goto error;
2084789Sahrens 
2085789Sahrens 		/*
2086789Sahrens 		 * Refresh the statistics so the new property is reflected.
2087789Sahrens 		 */
2088789Sahrens 		(void) get_stats(zhp);
2089789Sahrens 	}
2090789Sahrens 
2091789Sahrens error:
2092789Sahrens 	changelist_free(cl);
2093789Sahrens 	return (ret);
2094789Sahrens }
2095789Sahrens 
20963912Slling void
2097789Sahrens nicebool(int value, char *buf, size_t buflen)
2098789Sahrens {
2099789Sahrens 	if (value)
2100789Sahrens 		(void) strlcpy(buf, "on", buflen);
2101789Sahrens 	else
2102789Sahrens 		(void) strlcpy(buf, "off", buflen);
2103789Sahrens }
2104789Sahrens 
2105789Sahrens /*
21061356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
21071356Seschrock  * extract them appropriately.
21081356Seschrock  */
21091356Seschrock static uint64_t
21101356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21111356Seschrock {
21121356Seschrock 	nvlist_t *nv;
21131356Seschrock 	uint64_t value;
21141356Seschrock 
21152885Sahrens 	*source = NULL;
21161356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21171356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21181356Seschrock 		verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0);
21192885Sahrens 		(void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source);
21201356Seschrock 	} else {
21211356Seschrock 		value = zfs_prop_default_numeric(prop);
21221356Seschrock 		*source = "";
21231356Seschrock 	}
21241356Seschrock 
21251356Seschrock 	return (value);
21261356Seschrock }
21271356Seschrock 
21281356Seschrock static char *
21291356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21301356Seschrock {
21311356Seschrock 	nvlist_t *nv;
21321356Seschrock 	char *value;
21331356Seschrock 
21342885Sahrens 	*source = NULL;
21351356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21361356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21371356Seschrock 		verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0);
21382885Sahrens 		(void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source);
21391356Seschrock 	} else {
21401356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
21411356Seschrock 			value = "";
21421356Seschrock 		*source = "";
21431356Seschrock 	}
21441356Seschrock 
21451356Seschrock 	return (value);
21461356Seschrock }
21471356Seschrock 
21481356Seschrock /*
2149789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2150789Sahrens  * zfs_prop_get_int() are built using this interface.
2151789Sahrens  *
2152789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2153789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2154789Sahrens  * If they differ from the on-disk values, report the current values and mark
2155789Sahrens  * the source "temporary".
2156789Sahrens  */
21572082Seschrock static int
2158789Sahrens get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src,
21592082Seschrock     char **source, uint64_t *val)
2160789Sahrens {
2161789Sahrens 	struct mnttab mnt;
21623265Sahrens 	char *mntopt_on = NULL;
21633265Sahrens 	char *mntopt_off = NULL;
2164789Sahrens 
2165789Sahrens 	*source = NULL;
2166789Sahrens 
21673265Sahrens 	switch (prop) {
21683265Sahrens 	case ZFS_PROP_ATIME:
21693265Sahrens 		mntopt_on = MNTOPT_ATIME;
21703265Sahrens 		mntopt_off = MNTOPT_NOATIME;
21713265Sahrens 		break;
21723265Sahrens 
21733265Sahrens 	case ZFS_PROP_DEVICES:
21743265Sahrens 		mntopt_on = MNTOPT_DEVICES;
21753265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
21763265Sahrens 		break;
21773265Sahrens 
21783265Sahrens 	case ZFS_PROP_EXEC:
21793265Sahrens 		mntopt_on = MNTOPT_EXEC;
21803265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
21813265Sahrens 		break;
21823265Sahrens 
21833265Sahrens 	case ZFS_PROP_READONLY:
21843265Sahrens 		mntopt_on = MNTOPT_RO;
21853265Sahrens 		mntopt_off = MNTOPT_RW;
21863265Sahrens 		break;
21873265Sahrens 
21883265Sahrens 	case ZFS_PROP_SETUID:
21893265Sahrens 		mntopt_on = MNTOPT_SETUID;
21903265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
21913265Sahrens 		break;
21923265Sahrens 
21933265Sahrens 	case ZFS_PROP_XATTR:
21943265Sahrens 		mntopt_on = MNTOPT_XATTR;
21953265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
21963265Sahrens 		break;
21973265Sahrens 	}
21983265Sahrens 
21992474Seschrock 	/*
22002474Seschrock 	 * Because looking up the mount options is potentially expensive
22012474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
22022474Seschrock 	 * we're looking up a property which requires its presence.
22032474Seschrock 	 */
22042474Seschrock 	if (!zhp->zfs_mntcheck &&
22053265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
22063265Sahrens 		struct mnttab entry, search = { 0 };
22073265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
22082474Seschrock 
22092474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
22102474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
22113265Sahrens 		rewind(mnttab);
22123265Sahrens 
22133265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
22143265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
22153265Sahrens 			    entry.mnt_mntopts);
22163265Sahrens 			if (zhp->zfs_mntopts == NULL)
22173265Sahrens 				return (-1);
22183265Sahrens 		}
22192474Seschrock 
22202474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
22212474Seschrock 	}
22222474Seschrock 
2223789Sahrens 	if (zhp->zfs_mntopts == NULL)
2224789Sahrens 		mnt.mnt_mntopts = "";
2225789Sahrens 	else
2226789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2227789Sahrens 
2228789Sahrens 	switch (prop) {
2229789Sahrens 	case ZFS_PROP_ATIME:
22303265Sahrens 	case ZFS_PROP_DEVICES:
22313265Sahrens 	case ZFS_PROP_EXEC:
22323265Sahrens 	case ZFS_PROP_READONLY:
22333265Sahrens 	case ZFS_PROP_SETUID:
22343265Sahrens 	case ZFS_PROP_XATTR:
22352082Seschrock 		*val = getprop_uint64(zhp, prop, source);
22362082Seschrock 
22373265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
22382082Seschrock 			*val = B_TRUE;
2239789Sahrens 			if (src)
2240789Sahrens 				*src = ZFS_SRC_TEMPORARY;
22413265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
22422082Seschrock 			*val = B_FALSE;
2243789Sahrens 			if (src)
2244789Sahrens 				*src = ZFS_SRC_TEMPORARY;
2245789Sahrens 		}
22462082Seschrock 		break;
2247789Sahrens 
22483265Sahrens 	case ZFS_PROP_CANMOUNT:
22492082Seschrock 		*val = getprop_uint64(zhp, prop, source);
22503417Srm160521 		if (*val == 0)
22513417Srm160521 			*source = zhp->zfs_name;
22523417Srm160521 		else
22533417Srm160521 			*source = "";	/* default */
22542082Seschrock 		break;
2255789Sahrens 
2256789Sahrens 	case ZFS_PROP_QUOTA:
2257789Sahrens 	case ZFS_PROP_RESERVATION:
22582885Sahrens 		*val = getprop_uint64(zhp, prop, source);
22592885Sahrens 		if (*val == 0)
2260789Sahrens 			*source = "";	/* default */
2261789Sahrens 		else
2262789Sahrens 			*source = zhp->zfs_name;
22632082Seschrock 		break;
2264789Sahrens 
2265789Sahrens 	case ZFS_PROP_MOUNTED:
22662082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
22672082Seschrock 		break;
2268789Sahrens 
22693377Seschrock 	case ZFS_PROP_NUMCLONES:
22703377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
22713377Seschrock 		break;
22723377Seschrock 
2273789Sahrens 	default:
22744577Sahrens 		switch (zfs_prop_get_type(prop)) {
22754787Sahrens 		case PROP_TYPE_NUMBER:
22764787Sahrens 		case PROP_TYPE_BOOLEAN:
22774787Sahrens 		case PROP_TYPE_INDEX:
22784577Sahrens 			*val = getprop_uint64(zhp, prop, source);
22794577Sahrens 			break;
22804577Sahrens 
22814787Sahrens 		case PROP_TYPE_STRING:
22824577Sahrens 		default:
22834577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22844577Sahrens 			    "cannot get non-numeric property"));
22854577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
22864577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
22874577Sahrens 		}
2288789Sahrens 	}
2289789Sahrens 
2290789Sahrens 	return (0);
2291789Sahrens }
2292789Sahrens 
2293789Sahrens /*
2294789Sahrens  * Calculate the source type, given the raw source string.
2295789Sahrens  */
2296789Sahrens static void
2297789Sahrens get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source,
2298789Sahrens     char *statbuf, size_t statlen)
2299789Sahrens {
2300789Sahrens 	if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY)
2301789Sahrens 		return;
2302789Sahrens 
2303789Sahrens 	if (source == NULL) {
2304789Sahrens 		*srctype = ZFS_SRC_NONE;
2305789Sahrens 	} else if (source[0] == '\0') {
2306789Sahrens 		*srctype = ZFS_SRC_DEFAULT;
2307789Sahrens 	} else {
2308789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2309789Sahrens 			*srctype = ZFS_SRC_LOCAL;
2310789Sahrens 		} else {
2311789Sahrens 			(void) strlcpy(statbuf, source, statlen);
2312789Sahrens 			*srctype = ZFS_SRC_INHERITED;
2313789Sahrens 		}
2314789Sahrens 	}
2315789Sahrens 
2316789Sahrens }
2317789Sahrens 
2318789Sahrens /*
2319789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2320789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2321789Sahrens  * human-readable form.
2322789Sahrens  *
2323789Sahrens  * Returns 0 on success, or -1 on error.
2324789Sahrens  */
2325789Sahrens int
2326789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
23272082Seschrock     zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2328789Sahrens {
2329789Sahrens 	char *source = NULL;
2330789Sahrens 	uint64_t val;
2331789Sahrens 	char *str;
2332789Sahrens 	const char *root;
23332676Seschrock 	const char *strval;
2334789Sahrens 
2335789Sahrens 	/*
2336789Sahrens 	 * Check to see if this property applies to our object
2337789Sahrens 	 */
2338789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2339789Sahrens 		return (-1);
2340789Sahrens 
2341789Sahrens 	if (src)
2342789Sahrens 		*src = ZFS_SRC_NONE;
2343789Sahrens 
2344789Sahrens 	switch (prop) {
2345789Sahrens 	case ZFS_PROP_CREATION:
2346789Sahrens 		/*
2347789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2348789Sahrens 		 * this into a string unless 'literal' is specified.
2349789Sahrens 		 */
2350789Sahrens 		{
23512885Sahrens 			val = getprop_uint64(zhp, prop, &source);
23522885Sahrens 			time_t time = (time_t)val;
2353789Sahrens 			struct tm t;
2354789Sahrens 
2355789Sahrens 			if (literal ||
2356789Sahrens 			    localtime_r(&time, &t) == NULL ||
2357789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2358789Sahrens 			    &t) == 0)
23592885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2360789Sahrens 		}
2361789Sahrens 		break;
2362789Sahrens 
2363789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2364789Sahrens 		/*
2365789Sahrens 		 * Getting the precise mountpoint can be tricky.
2366789Sahrens 		 *
2367789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2368789Sahrens 		 *  - for default mountpoints, construct it as /zfs/<dataset>
2369789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2370789Sahrens 		 *    after our ancestor and append it to the inherited value.
2371789Sahrens 		 *
2372789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2373789Sahrens 		 * root to any values we return.
2374789Sahrens 		 */
23751544Seschrock 		root = zhp->zfs_root;
23761356Seschrock 		str = getprop_string(zhp, prop, &source);
23771356Seschrock 
23781356Seschrock 		if (str[0] == '\0') {
2379789Sahrens 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
2380789Sahrens 			    root, zhp->zfs_name);
23811356Seschrock 		} else if (str[0] == '/') {
23821356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2383789Sahrens 
2384789Sahrens 			if (relpath[0] == '/')
2385789Sahrens 				relpath++;
23861356Seschrock 			if (str[1] == '\0')
23871356Seschrock 				str++;
2388789Sahrens 
2389789Sahrens 			if (relpath[0] == '\0')
2390789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
23911356Seschrock 				    root, str);
2392789Sahrens 			else
2393789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
23941356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2395789Sahrens 				    relpath);
2396789Sahrens 		} else {
2397789Sahrens 			/* 'legacy' or 'none' */
23981356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2399789Sahrens 		}
2400789Sahrens 
2401789Sahrens 		break;
2402789Sahrens 
2403789Sahrens 	case ZFS_PROP_ORIGIN:
24042885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2405789Sahrens 		    proplen);
2406789Sahrens 		/*
2407789Sahrens 		 * If there is no parent at all, return failure to indicate that
2408789Sahrens 		 * it doesn't apply to this dataset.
2409789Sahrens 		 */
2410789Sahrens 		if (propbuf[0] == '\0')
2411789Sahrens 			return (-1);
2412789Sahrens 		break;
2413789Sahrens 
2414789Sahrens 	case ZFS_PROP_QUOTA:
2415789Sahrens 	case ZFS_PROP_RESERVATION:
24162082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24172082Seschrock 			return (-1);
2418789Sahrens 
2419789Sahrens 		/*
2420789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2421789Sahrens 		 * (unless literal is set), and indicate that it's the default
2422789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2423789Sahrens 		 * that its set locally.
2424789Sahrens 		 */
2425789Sahrens 		if (val == 0) {
2426789Sahrens 			if (literal)
2427789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2428789Sahrens 			else
2429789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2430789Sahrens 		} else {
2431789Sahrens 			if (literal)
24322856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24333912Slling 				    (u_longlong_t)val);
2434789Sahrens 			else
2435789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2436789Sahrens 		}
2437789Sahrens 		break;
2438789Sahrens 
2439789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
24402082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24412082Seschrock 			return (-1);
24422856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
24432856Snd150628 		    val / 100, (longlong_t)val % 100);
2444789Sahrens 		break;
2445789Sahrens 
2446789Sahrens 	case ZFS_PROP_TYPE:
2447789Sahrens 		switch (zhp->zfs_type) {
2448789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2449789Sahrens 			str = "filesystem";
2450789Sahrens 			break;
2451789Sahrens 		case ZFS_TYPE_VOLUME:
2452789Sahrens 			str = "volume";
2453789Sahrens 			break;
2454789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2455789Sahrens 			str = "snapshot";
2456789Sahrens 			break;
2457789Sahrens 		default:
24582082Seschrock 			abort();
2459789Sahrens 		}
2460789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2461789Sahrens 		break;
2462789Sahrens 
2463789Sahrens 	case ZFS_PROP_MOUNTED:
2464789Sahrens 		/*
2465789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2466789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2467789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2468789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2469789Sahrens 		 */
24702082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
24712082Seschrock 		    src, &source, &val) != 0)
24722082Seschrock 			return (-1);
24732082Seschrock 		if (val)
2474789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2475789Sahrens 		else
2476789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2477789Sahrens 		break;
2478789Sahrens 
2479789Sahrens 	case ZFS_PROP_NAME:
2480789Sahrens 		/*
2481789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2482789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2483789Sahrens 		 * consumers.
2484789Sahrens 		 */
2485789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2486789Sahrens 		break;
2487789Sahrens 
2488789Sahrens 	default:
24894577Sahrens 		switch (zfs_prop_get_type(prop)) {
24904787Sahrens 		case PROP_TYPE_NUMBER:
24914577Sahrens 			if (get_numeric_property(zhp, prop, src,
24924577Sahrens 			    &source, &val) != 0)
24934577Sahrens 				return (-1);
24944577Sahrens 			if (literal)
24954577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
24964577Sahrens 				    (u_longlong_t)val);
24974577Sahrens 			else
24984577Sahrens 				zfs_nicenum(val, propbuf, proplen);
24994577Sahrens 			break;
25004577Sahrens 
25014787Sahrens 		case PROP_TYPE_STRING:
25024577Sahrens 			(void) strlcpy(propbuf,
25034577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
25044577Sahrens 			break;
25054577Sahrens 
25064787Sahrens 		case PROP_TYPE_BOOLEAN:
25074577Sahrens 			if (get_numeric_property(zhp, prop, src,
25084577Sahrens 			    &source, &val) != 0)
25094577Sahrens 				return (-1);
25104577Sahrens 			nicebool(val, propbuf, proplen);
25114577Sahrens 
25124577Sahrens 			break;
25134577Sahrens 
25144787Sahrens 		case PROP_TYPE_INDEX:
25154577Sahrens 			val = getprop_uint64(zhp, prop, &source);
25164577Sahrens 			if (zfs_prop_index_to_string(prop, val,
25174577Sahrens 			    &strval) != 0)
25184577Sahrens 				return (-1);
25194577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
25204577Sahrens 			break;
25214577Sahrens 
25224577Sahrens 		default:
25234577Sahrens 			abort();
25244577Sahrens 		}
2525789Sahrens 	}
2526789Sahrens 
2527789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2528789Sahrens 
2529789Sahrens 	return (0);
2530789Sahrens }
2531789Sahrens 
2532789Sahrens /*
2533789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2534789Sahrens  * the given property is the appropriate type; should only be used with
2535789Sahrens  * hard-coded property types.
2536789Sahrens  */
2537789Sahrens uint64_t
2538789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2539789Sahrens {
2540789Sahrens 	char *source;
2541789Sahrens 	zfs_source_t sourcetype = ZFS_SRC_NONE;
25422082Seschrock 	uint64_t val;
25432082Seschrock 
25442082Seschrock 	(void) get_numeric_property(zhp, prop, &sourcetype, &source, &val);
25452082Seschrock 
25462082Seschrock 	return (val);
2547789Sahrens }
2548789Sahrens 
2549789Sahrens /*
2550789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2551789Sahrens  */
2552789Sahrens int
2553789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2554789Sahrens     zfs_source_t *src, char *statbuf, size_t statlen)
2555789Sahrens {
2556789Sahrens 	char *source;
2557789Sahrens 
2558789Sahrens 	/*
2559789Sahrens 	 * Check to see if this property applies to our object
2560789Sahrens 	 */
2561789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
25623237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
25632082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
25642082Seschrock 		    zfs_prop_to_name(prop)));
2565789Sahrens 
2566789Sahrens 	if (src)
2567789Sahrens 		*src = ZFS_SRC_NONE;
2568789Sahrens 
25692082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
25702082Seschrock 		return (-1);
2571789Sahrens 
2572789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2573789Sahrens 
2574789Sahrens 	return (0);
2575789Sahrens }
2576789Sahrens 
2577789Sahrens /*
2578789Sahrens  * Returns the name of the given zfs handle.
2579789Sahrens  */
2580789Sahrens const char *
2581789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2582789Sahrens {
2583789Sahrens 	return (zhp->zfs_name);
2584789Sahrens }
2585789Sahrens 
2586789Sahrens /*
2587789Sahrens  * Returns the type of the given zfs handle.
2588789Sahrens  */
2589789Sahrens zfs_type_t
2590789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2591789Sahrens {
2592789Sahrens 	return (zhp->zfs_type);
2593789Sahrens }
2594789Sahrens 
2595789Sahrens /*
25961356Seschrock  * Iterate over all child filesystems
2597789Sahrens  */
2598789Sahrens int
25991356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2600789Sahrens {
2601789Sahrens 	zfs_cmd_t zc = { 0 };
2602789Sahrens 	zfs_handle_t *nzhp;
2603789Sahrens 	int ret;
2604789Sahrens 
2605789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26062082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2607789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2608789Sahrens 		/*
2609789Sahrens 		 * Ignore private dataset names.
2610789Sahrens 		 */
2611789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2612789Sahrens 			continue;
2613789Sahrens 
2614789Sahrens 		/*
2615789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2616789Sahrens 		 * that the pool has since been removed.
2617789Sahrens 		 */
26182082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26192082Seschrock 		    zc.zc_name)) == NULL)
2620789Sahrens 			continue;
2621789Sahrens 
2622789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2623789Sahrens 			return (ret);
2624789Sahrens 	}
2625789Sahrens 
2626789Sahrens 	/*
2627789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2628789Sahrens 	 * returned, then the underlying dataset has been removed since we
2629789Sahrens 	 * obtained the handle.
2630789Sahrens 	 */
2631789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26322082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26332082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2634789Sahrens 
26351356Seschrock 	return (0);
26361356Seschrock }
26371356Seschrock 
26381356Seschrock /*
26391356Seschrock  * Iterate over all snapshots
26401356Seschrock  */
26411356Seschrock int
26421356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26431356Seschrock {
26441356Seschrock 	zfs_cmd_t zc = { 0 };
26451356Seschrock 	zfs_handle_t *nzhp;
26461356Seschrock 	int ret;
2647789Sahrens 
2648789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26492082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
26502082Seschrock 	    &zc) == 0;
2651789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2652789Sahrens 
26532082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26542082Seschrock 		    zc.zc_name)) == NULL)
2655789Sahrens 			continue;
2656789Sahrens 
2657789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2658789Sahrens 			return (ret);
2659789Sahrens 	}
2660789Sahrens 
2661789Sahrens 	/*
2662789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2663789Sahrens 	 * returned, then the underlying dataset has been removed since we
2664789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2665789Sahrens 	 */
2666789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26672082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26682082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2669789Sahrens 
2670789Sahrens 	return (0);
2671789Sahrens }
2672789Sahrens 
2673789Sahrens /*
26741356Seschrock  * Iterate over all children, snapshots and filesystems
26751356Seschrock  */
26761356Seschrock int
26771356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26781356Seschrock {
26791356Seschrock 	int ret;
26801356Seschrock 
26811356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
26821356Seschrock 		return (ret);
26831356Seschrock 
26841356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
26851356Seschrock }
26861356Seschrock 
26871356Seschrock /*
2688789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2689789Sahrens  * Can return NULL if this is a pool.
2690789Sahrens  */
2691789Sahrens static int
2692789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2693789Sahrens {
2694789Sahrens 	char *loc;
2695789Sahrens 
2696789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2697789Sahrens 		return (-1);
2698789Sahrens 
2699789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2700789Sahrens 	buf[loc - path] = '\0';
2701789Sahrens 
2702789Sahrens 	return (0);
2703789Sahrens }
2704789Sahrens 
2705789Sahrens /*
27064490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
27074490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
27084490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
27094490Svb160487  * length of already existing prefix of the given path.  We also fetch the
27104490Svb160487  * 'zoned' property, which is used to validate property settings when creating
27114490Svb160487  * new datasets.
2712789Sahrens  */
2713789Sahrens static int
27144490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27154490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2716789Sahrens {
2717789Sahrens 	zfs_cmd_t zc = { 0 };
2718789Sahrens 	char parent[ZFS_MAXNAMELEN];
2719789Sahrens 	char *slash;
27201356Seschrock 	zfs_handle_t *zhp;
27212082Seschrock 	char errbuf[1024];
27222082Seschrock 
27232082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
27242082Seschrock 	    path);
2725789Sahrens 
2726789Sahrens 	/* get parent, and check to see if this is just a pool */
2727789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
27282082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27292082Seschrock 		    "missing dataset name"));
27302082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2731789Sahrens 	}
2732789Sahrens 
2733789Sahrens 	/* check to see if the pool exists */
2734789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2735789Sahrens 		slash = parent + strlen(parent);
2736789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2737789Sahrens 	zc.zc_name[slash - parent] = '\0';
27382082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2739789Sahrens 	    errno == ENOENT) {
27402082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27412082Seschrock 		    "no such pool '%s'"), zc.zc_name);
27422082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2743789Sahrens 	}
2744789Sahrens 
2745789Sahrens 	/* check to see if the parent dataset exists */
27464490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27474490Svb160487 		if (errno == ENOENT && accept_ancestor) {
27484490Svb160487 			/*
27494490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
27504490Svb160487 			 */
27514490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27524490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27534490Svb160487 				    "no such pool '%s'"), zc.zc_name);
27544490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27554490Svb160487 			}
27564490Svb160487 		} else if (errno == ENOENT) {
27572082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27582082Seschrock 			    "parent does not exist"));
27592082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27604490Svb160487 		} else
27612082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2762789Sahrens 	}
2763789Sahrens 
27642676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2765789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
27662676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
27672082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27681356Seschrock 		zfs_close(zhp);
2769789Sahrens 		return (-1);
2770789Sahrens 	}
2771789Sahrens 
2772789Sahrens 	/* make sure parent is a filesystem */
27731356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
27742082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27752082Seschrock 		    "parent is not a filesystem"));
27762082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27771356Seschrock 		zfs_close(zhp);
2778789Sahrens 		return (-1);
2779789Sahrens 	}
2780789Sahrens 
27811356Seschrock 	zfs_close(zhp);
27824490Svb160487 	if (prefixlen != NULL)
27834490Svb160487 		*prefixlen = strlen(parent);
27844490Svb160487 	return (0);
27854490Svb160487 }
27864490Svb160487 
27874490Svb160487 /*
27884490Svb160487  * Finds whether the dataset of the given type(s) exists.
27894490Svb160487  */
27904490Svb160487 boolean_t
27914490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
27924490Svb160487 {
27934490Svb160487 	zfs_handle_t *zhp;
27944490Svb160487 
27954490Svb160487 	if (!zfs_validate_name(hdl, path, types))
27964490Svb160487 		return (B_FALSE);
27974490Svb160487 
27984490Svb160487 	/*
27994490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
28004490Svb160487 	 */
28014490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
28024490Svb160487 		int ds_type = zhp->zfs_type;
28034490Svb160487 
28044490Svb160487 		zfs_close(zhp);
28054490Svb160487 		if (types & ds_type)
28064490Svb160487 			return (B_TRUE);
28074490Svb160487 	}
28084490Svb160487 	return (B_FALSE);
28094490Svb160487 }
28104490Svb160487 
28114490Svb160487 /*
28124490Svb160487  * Creates non-existing ancestors of the given path.
28134490Svb160487  */
28144490Svb160487 int
28154490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28164490Svb160487 {
28174490Svb160487 	int prefix;
28184490Svb160487 	uint64_t zoned;
28194490Svb160487 	char *path_copy;
28204490Svb160487 	int rc;
28214490Svb160487 
28224490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28234490Svb160487 		return (-1);
28244490Svb160487 
28254490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28264490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28274490Svb160487 		free(path_copy);
28284490Svb160487 	}
28294490Svb160487 	if (path_copy == NULL || rc != 0)
28304490Svb160487 		return (-1);
28314490Svb160487 
2832789Sahrens 	return (0);
2833789Sahrens }
2834789Sahrens 
2835789Sahrens /*
28362676Seschrock  * Create a new filesystem or volume.
2837789Sahrens  */
2838789Sahrens int
28392082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28402676Seschrock     nvlist_t *props)
2841789Sahrens {
2842789Sahrens 	zfs_cmd_t zc = { 0 };
2843789Sahrens 	int ret;
2844789Sahrens 	uint64_t size = 0;
2845789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
28462082Seschrock 	char errbuf[1024];
28472676Seschrock 	uint64_t zoned;
28482082Seschrock 
28492082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28502082Seschrock 	    "cannot create '%s'"), path);
2851789Sahrens 
2852789Sahrens 	/* validate the path, taking care to note the extended error message */
28532082Seschrock 	if (!zfs_validate_name(hdl, path, type))
28542082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2855789Sahrens 
2856789Sahrens 	/* validate parents exist */
28574490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2858789Sahrens 		return (-1);
2859789Sahrens 
2860789Sahrens 	/*
2861789Sahrens 	 * The failure modes when creating a dataset of a different type over
2862789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2863789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2864789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2865789Sahrens 	 * first try to see if the dataset exists.
2866789Sahrens 	 */
2867789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
28684490Svb160487 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) {
28692082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28702082Seschrock 		    "dataset already exists"));
28712082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2872789Sahrens 	}
2873789Sahrens 
2874789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2875789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2876789Sahrens 	else
2877789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2878789Sahrens 
28793912Slling 	if (props && (props = zfs_validate_properties(hdl, type, NULL, props,
28803912Slling 	    zoned, NULL, errbuf)) == 0)
28812676Seschrock 		return (-1);
28822676Seschrock 
2883789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
28841133Seschrock 		/*
28851133Seschrock 		 * If we are creating a volume, the size and block size must
28861133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
28871133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
28881133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
28891133Seschrock 		 * zero.
28901133Seschrock 		 */
28912676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
28922676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
28932676Seschrock 			nvlist_free(props);
28942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28952676Seschrock 			    "missing volume size"));
28962676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2897789Sahrens 		}
2898789Sahrens 
28992676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
29002676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29012676Seschrock 		    &blocksize)) != 0) {
29022676Seschrock 			if (ret == ENOENT) {
29032676Seschrock 				blocksize = zfs_prop_default_numeric(
29042676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29052676Seschrock 			} else {
29062676Seschrock 				nvlist_free(props);
29072676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29082676Seschrock 				    "missing volume block size"));
29092676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29102676Seschrock 			}
29112676Seschrock 		}
29122676Seschrock 
29132676Seschrock 		if (size == 0) {
29142676Seschrock 			nvlist_free(props);
29152082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29162676Seschrock 			    "volume size cannot be zero"));
29172676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29181133Seschrock 		}
29191133Seschrock 
29201133Seschrock 		if (size % blocksize != 0) {
29212676Seschrock 			nvlist_free(props);
29222082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29232676Seschrock 			    "volume size must be a multiple of volume block "
29242676Seschrock 			    "size"));
29252676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29261133Seschrock 		}
2927789Sahrens 	}
2928789Sahrens 
29292676Seschrock 	if (props &&
29302676Seschrock 	    zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0)
29312676Seschrock 		return (-1);
29322676Seschrock 	nvlist_free(props);
29332676Seschrock 
2934789Sahrens 	/* create the dataset */
29354543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2936789Sahrens 
29373912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29382082Seschrock 		ret = zvol_create_link(hdl, path);
29393912Slling 		if (ret) {
29403912Slling 			(void) zfs_standard_error(hdl, errno,
29413912Slling 			    dgettext(TEXT_DOMAIN,
29423912Slling 			    "Volume successfully created, but device links "
29433912Slling 			    "were not created"));
29443912Slling 			zcmd_free_nvlists(&zc);
29453912Slling 			return (-1);
29463912Slling 		}
29473912Slling 	}
2948789Sahrens 
29492676Seschrock 	zcmd_free_nvlists(&zc);
29502676Seschrock 
2951789Sahrens 	/* check for failure */
2952789Sahrens 	if (ret != 0) {
2953789Sahrens 		char parent[ZFS_MAXNAMELEN];
2954789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2955789Sahrens 
2956789Sahrens 		switch (errno) {
2957789Sahrens 		case ENOENT:
29582082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29592082Seschrock 			    "no such parent '%s'"), parent);
29602082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2961789Sahrens 
2962789Sahrens 		case EINVAL:
29632082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29643413Smmusante 			    "parent '%s' is not a filesystem"), parent);
29652082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2966789Sahrens 
2967789Sahrens 		case EDOM:
29682082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29692676Seschrock 			    "volume block size must be power of 2 from "
29702676Seschrock 			    "%u to %uk"),
2971789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2972789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
29732082Seschrock 
29742676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29752082Seschrock 
29764603Sahrens 		case ENOTSUP:
29774603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29784603Sahrens 			    "pool must be upgraded to set this "
29794603Sahrens 			    "property or value"));
29804603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
29814603Sahrens 
2982789Sahrens #ifdef _ILP32
2983789Sahrens 		case EOVERFLOW:
2984789Sahrens 			/*
2985789Sahrens 			 * This platform can't address a volume this big.
2986789Sahrens 			 */
29872082Seschrock 			if (type == ZFS_TYPE_VOLUME)
29882082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
29892082Seschrock 				    errbuf));
2990789Sahrens #endif
29912082Seschrock 			/* FALLTHROUGH */
2992789Sahrens 		default:
29932082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2994789Sahrens 		}
2995789Sahrens 	}
2996789Sahrens 
2997789Sahrens 	return (0);
2998789Sahrens }
2999789Sahrens 
3000789Sahrens /*
3001789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3002789Sahrens  * isn't mounted, and that there are no active dependents.
3003789Sahrens  */
3004789Sahrens int
3005789Sahrens zfs_destroy(zfs_handle_t *zhp)
3006789Sahrens {
3007789Sahrens 	zfs_cmd_t zc = { 0 };
3008789Sahrens 
3009789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3010789Sahrens 
30112676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30123126Sahl 		/*
30134543Smarks 		 * If user doesn't have permissions to unshare volume, then
30144543Smarks 		 * abort the request.  This would only happen for a
30154543Smarks 		 * non-privileged user.
30163126Sahl 		 */
30174543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30184543Smarks 			return (-1);
30194543Smarks 		}
30203126Sahl 
30212082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3022789Sahrens 			return (-1);
3023789Sahrens 
3024789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3025789Sahrens 	} else {
3026789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3027789Sahrens 	}
3028789Sahrens 
30294543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30303237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30312082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30322082Seschrock 		    zhp->zfs_name));
30332199Sahrens 	}
3034789Sahrens 
3035789Sahrens 	remove_mountpoint(zhp);
3036789Sahrens 
3037789Sahrens 	return (0);
3038789Sahrens }
3039789Sahrens 
30402199Sahrens struct destroydata {
30412199Sahrens 	char *snapname;
30422199Sahrens 	boolean_t gotone;
30433265Sahrens 	boolean_t closezhp;
30442199Sahrens };
30452199Sahrens 
30462199Sahrens static int
30472199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
30482199Sahrens {
30492199Sahrens 	struct destroydata *dd = arg;
30502199Sahrens 	zfs_handle_t *szhp;
30512199Sahrens 	char name[ZFS_MAXNAMELEN];
30523265Sahrens 	boolean_t closezhp = dd->closezhp;
30533265Sahrens 	int rv;
30542199Sahrens 
30552676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
30562676Seschrock 	(void) strlcat(name, "@", sizeof (name));
30572676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
30582199Sahrens 
30592199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
30602199Sahrens 	if (szhp) {
30612199Sahrens 		dd->gotone = B_TRUE;
30622199Sahrens 		zfs_close(szhp);
30632199Sahrens 	}
30642199Sahrens 
30652199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
30662199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
30672199Sahrens 		/*
30682199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
30692199Sahrens 		 * return an error, because then we wouldn't visit all
30702199Sahrens 		 * the volumes.
30712199Sahrens 		 */
30722199Sahrens 	}
30732199Sahrens 
30743265Sahrens 	dd->closezhp = B_TRUE;
30753265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
30763265Sahrens 	if (closezhp)
30773265Sahrens 		zfs_close(zhp);
30783265Sahrens 	return (rv);
30792199Sahrens }
30802199Sahrens 
30812199Sahrens /*
30822199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
30832199Sahrens  */
30842199Sahrens int
30852199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
30862199Sahrens {
30872199Sahrens 	zfs_cmd_t zc = { 0 };
30882199Sahrens 	int ret;
30892199Sahrens 	struct destroydata dd = { 0 };
30902199Sahrens 
30912199Sahrens 	dd.snapname = snapname;
30922199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
30932199Sahrens 
30942199Sahrens 	if (!dd.gotone) {
30953237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
30962199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
30972199Sahrens 		    zhp->zfs_name, snapname));
30982199Sahrens 	}
30992199Sahrens 
31002199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31012676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
31022199Sahrens 
31034543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
31042199Sahrens 	if (ret != 0) {
31052199Sahrens 		char errbuf[1024];
31062199Sahrens 
31072199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31082199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31092199Sahrens 
31102199Sahrens 		switch (errno) {
31112199Sahrens 		case EEXIST:
31122199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31132199Sahrens 			    "snapshot is cloned"));
31142199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31152199Sahrens 
31162199Sahrens 		default:
31172199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31182199Sahrens 			    errbuf));
31192199Sahrens 		}
31202199Sahrens 	}
31212199Sahrens 
31222199Sahrens 	return (0);
31232199Sahrens }
31242199Sahrens 
3125789Sahrens /*
3126789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3127789Sahrens  */
3128789Sahrens int
31292676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3130789Sahrens {
3131789Sahrens 	zfs_cmd_t zc = { 0 };
3132789Sahrens 	char parent[ZFS_MAXNAMELEN];
3133789Sahrens 	int ret;
31342082Seschrock 	char errbuf[1024];
31352082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31362676Seschrock 	zfs_type_t type;
31372676Seschrock 	uint64_t zoned;
3138789Sahrens 
3139789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3140789Sahrens 
31412082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31422082Seschrock 	    "cannot create '%s'"), target);
31432082Seschrock 
3144789Sahrens 	/* validate the target name */
31452082Seschrock 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM))
31462082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3147789Sahrens 
3148789Sahrens 	/* validate parents exist */
31494490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3150789Sahrens 		return (-1);
3151789Sahrens 
3152789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3153789Sahrens 
3154789Sahrens 	/* do the clone */
31552676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3156789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
31572744Snn35248 		type = ZFS_TYPE_VOLUME;
31582676Seschrock 	} else {
3159789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
31602744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
31612676Seschrock 	}
31622676Seschrock 
31632676Seschrock 	if (props) {
31643912Slling 		if ((props = zfs_validate_properties(hdl, type, NULL, props,
31653912Slling 		    zoned, zhp, errbuf)) == NULL)
31662676Seschrock 			return (-1);
31672676Seschrock 
31682676Seschrock 		if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) {
31692676Seschrock 			nvlist_free(props);
31702676Seschrock 			return (-1);
31712676Seschrock 		}
31722676Seschrock 
31732676Seschrock 		nvlist_free(props);
31742676Seschrock 	}
3175789Sahrens 
3176789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
31772676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
31784543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3179789Sahrens 
31802676Seschrock 	zcmd_free_nvlists(&zc);
31812676Seschrock 
3182789Sahrens 	if (ret != 0) {
3183789Sahrens 		switch (errno) {
3184789Sahrens 
3185789Sahrens 		case ENOENT:
3186789Sahrens 			/*
3187789Sahrens 			 * The parent doesn't exist.  We should have caught this
3188789Sahrens 			 * above, but there may a race condition that has since
3189789Sahrens 			 * destroyed the parent.
3190789Sahrens 			 *
3191789Sahrens 			 * At this point, we don't know whether it's the source
3192789Sahrens 			 * that doesn't exist anymore, or whether the target
3193789Sahrens 			 * dataset doesn't exist.
3194789Sahrens 			 */
31952082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31962082Seschrock 			    "no such parent '%s'"), parent);
31972082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
31982082Seschrock 
31992082Seschrock 		case EXDEV:
32002082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32012082Seschrock 			    "source and target pools differ"));
32022082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
32032082Seschrock 			    errbuf));
32042082Seschrock 
32052082Seschrock 		default:
32062082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32072082Seschrock 			    errbuf));
32082082Seschrock 		}
32092676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32102082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32112082Seschrock 	}
32122082Seschrock 
32132082Seschrock 	return (ret);
32142082Seschrock }
32152082Seschrock 
32162082Seschrock typedef struct promote_data {
32172082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32182082Seschrock 	const char *cb_target;
32192082Seschrock 	const char *cb_errbuf;
32202082Seschrock 	uint64_t cb_pivot_txg;
32212082Seschrock } promote_data_t;
32222082Seschrock 
32232082Seschrock static int
32242082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32252082Seschrock {
32262082Seschrock 	promote_data_t *pd = data;
32272082Seschrock 	zfs_handle_t *szhp;
32282082Seschrock 	char snapname[MAXPATHLEN];
32293265Sahrens 	int rv = 0;
32302082Seschrock 
32312082Seschrock 	/* We don't care about snapshots after the pivot point */
32323265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32333265Sahrens 		zfs_close(zhp);
32342082Seschrock 		return (0);
32353265Sahrens 	}
32362082Seschrock 
32372417Sahrens 	/* Remove the device link if it's a zvol. */
32382676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32392417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32402082Seschrock 
32412082Seschrock 	/* Check for conflicting names */
32422676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32432676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
32442082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
32452082Seschrock 	if (szhp != NULL) {
32462082Seschrock 		zfs_close(szhp);
32472082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32482082Seschrock 		    "snapshot name '%s' from origin \n"
32492082Seschrock 		    "conflicts with '%s' from target"),
32502082Seschrock 		    zhp->zfs_name, snapname);
32513265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
32522082Seschrock 	}
32533265Sahrens 	zfs_close(zhp);
32543265Sahrens 	return (rv);
32552082Seschrock }
32562082Seschrock 
32572417Sahrens static int
32582417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
32592417Sahrens {
32602417Sahrens 	promote_data_t *pd = data;
32612417Sahrens 
32622417Sahrens 	/* We don't care about snapshots after the pivot point */
32633265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
32643265Sahrens 		/* Create the device link if it's a zvol. */
32653265Sahrens 		if (ZFS_IS_VOLUME(zhp))
32663265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
32673265Sahrens 	}
32683265Sahrens 
32693265Sahrens 	zfs_close(zhp);
32702417Sahrens 	return (0);
32712417Sahrens }
32722417Sahrens 
32732082Seschrock /*
32742082Seschrock  * Promotes the given clone fs to be the clone parent.
32752082Seschrock  */
32762082Seschrock int
32772082Seschrock zfs_promote(zfs_handle_t *zhp)
32782082Seschrock {
32792082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
32802082Seschrock 	zfs_cmd_t zc = { 0 };
32812082Seschrock 	char parent[MAXPATHLEN];
32822082Seschrock 	char *cp;
32832082Seschrock 	int ret;
32842082Seschrock 	zfs_handle_t *pzhp;
32852082Seschrock 	promote_data_t pd;
32862082Seschrock 	char errbuf[1024];
32872082Seschrock 
32882082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32892082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
32902082Seschrock 
32912082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
32922082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32932082Seschrock 		    "snapshots can not be promoted"));
32942082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
32952082Seschrock 	}
32962082Seschrock 
32972676Seschrock 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent));
32982082Seschrock 	if (parent[0] == '\0') {
32992082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33002082Seschrock 		    "not a cloned filesystem"));
33012082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33022082Seschrock 	}
33032082Seschrock 	cp = strchr(parent, '@');
33042082Seschrock 	*cp = '\0';
33052082Seschrock 
33062082Seschrock 	/* Walk the snapshots we will be moving */
33072082Seschrock 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT);
33082082Seschrock 	if (pzhp == NULL)
33092082Seschrock 		return (-1);
33102082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33112082Seschrock 	zfs_close(pzhp);
33122082Seschrock 	pd.cb_target = zhp->zfs_name;
33132082Seschrock 	pd.cb_errbuf = errbuf;
33142082Seschrock 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY);
33152082Seschrock 	if (pzhp == NULL)
33162082Seschrock 		return (-1);
33172082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33182082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33192082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33202417Sahrens 	if (ret != 0) {
33212417Sahrens 		zfs_close(pzhp);
33222082Seschrock 		return (-1);
33232417Sahrens 	}
33242082Seschrock 
33252082Seschrock 	/* issue the ioctl */
33262676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of,
33272676Seschrock 	    sizeof (zc.zc_value));
33282082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33294543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33302082Seschrock 
33312082Seschrock 	if (ret != 0) {
33322417Sahrens 		int save_errno = errno;
33332417Sahrens 
33342417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33352417Sahrens 		zfs_close(pzhp);
33362417Sahrens 
33372417Sahrens 		switch (save_errno) {
3338789Sahrens 		case EEXIST:
3339789Sahrens 			/*
33402082Seschrock 			 * There is a conflicting snapshot name.  We
33412082Seschrock 			 * should have caught this above, but they could
33422082Seschrock 			 * have renamed something in the mean time.
3343789Sahrens 			 */
33442082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33452082Seschrock 			    "conflicting snapshot name from parent '%s'"),
33462082Seschrock 			    parent);
33472082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3348789Sahrens 
3349789Sahrens 		default:
33502417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3351789Sahrens 		}
33522417Sahrens 	} else {
33532417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3354789Sahrens 	}
3355789Sahrens 
33562417Sahrens 	zfs_close(pzhp);
3357789Sahrens 	return (ret);
3358789Sahrens }
3359789Sahrens 
33604007Smmusante struct createdata {
33614007Smmusante 	const char *cd_snapname;
33624007Smmusante 	int cd_ifexists;
33634007Smmusante };
33644007Smmusante 
33652199Sahrens static int
33662199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
33672199Sahrens {
33684007Smmusante 	struct createdata *cd = arg;
33692676Seschrock 	int ret;
33702199Sahrens 
33712199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33722199Sahrens 		char name[MAXPATHLEN];
33732199Sahrens 
33742676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
33752676Seschrock 		(void) strlcat(name, "@", sizeof (name));
33764007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
33774007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
33784007Smmusante 		    cd->cd_ifexists);
33792199Sahrens 		/*
33802199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
33812199Sahrens 		 * return an error, because then we wouldn't visit all
33822199Sahrens 		 * the volumes.
33832199Sahrens 		 */
33842199Sahrens 	}
33852676Seschrock 
33864007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
33872676Seschrock 
33882676Seschrock 	zfs_close(zhp);
33892676Seschrock 
33902676Seschrock 	return (ret);
33912199Sahrens }
33922199Sahrens 
3393789Sahrens /*
33943504Sahl  * Takes a snapshot of the given dataset.
3395789Sahrens  */
3396789Sahrens int
33972199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3398789Sahrens {
3399789Sahrens 	const char *delim;
3400789Sahrens 	char *parent;
3401789Sahrens 	zfs_handle_t *zhp;
3402789Sahrens 	zfs_cmd_t zc = { 0 };
3403789Sahrens 	int ret;
34042082Seschrock 	char errbuf[1024];
34052082Seschrock 
34062082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34072082Seschrock 	    "cannot snapshot '%s'"), path);
34082082Seschrock 
34092082Seschrock 	/* validate the target name */
34102082Seschrock 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT))
34112082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3412789Sahrens 
3413789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34142199Sahrens 	delim = strchr(path, '@');
34152082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
34162082Seschrock 		return (-1);
3417789Sahrens 	(void) strncpy(parent, path, delim - path);
3418789Sahrens 	parent[delim - path] = '\0';
3419789Sahrens 
34202082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3421789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3422789Sahrens 		free(parent);
3423789Sahrens 		return (-1);
3424789Sahrens 	}
3425789Sahrens 
34262199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34272676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34284543Smarks 	if (ZFS_IS_VOLUME(zhp))
34294543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34304543Smarks 	else
34314543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34322199Sahrens 	zc.zc_cookie = recursive;
34334543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
34342199Sahrens 
34352199Sahrens 	/*
34362199Sahrens 	 * if it was recursive, the one that actually failed will be in
34372199Sahrens 	 * zc.zc_name.
34382199Sahrens 	 */
34394543Smarks 	if (ret != 0)
34404543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34414543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
34424543Smarks 
34432199Sahrens 	if (ret == 0 && recursive) {
34444007Smmusante 		struct createdata cd;
34454007Smmusante 
34464007Smmusante 		cd.cd_snapname = delim + 1;
34474007Smmusante 		cd.cd_ifexists = B_FALSE;
34484007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
34492199Sahrens 	}
3450789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
34512082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
34522199Sahrens 		if (ret != 0) {
34534543Smarks 			(void) zfs_standard_error(hdl, errno,
34544543Smarks 			    dgettext(TEXT_DOMAIN,
34554543Smarks 			    "Volume successfully snapshotted, but device links "
34564543Smarks 			    "were not created"));
34574543Smarks 			free(parent);
34584543Smarks 			zfs_close(zhp);
34594543Smarks 			return (-1);
34602199Sahrens 		}
3461789Sahrens 	}
3462789Sahrens 
34632082Seschrock 	if (ret != 0)
34642082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3465789Sahrens 
3466789Sahrens 	free(parent);
3467789Sahrens 	zfs_close(zhp);
3468789Sahrens 
3469789Sahrens 	return (ret);
3470789Sahrens }
3471789Sahrens 
3472789Sahrens /*
34733504Sahl  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
34743504Sahl  * NULL) to the file descriptor specified by outfd.
3475789Sahrens  */
3476789Sahrens int
34773504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd)
3478789Sahrens {
3479789Sahrens 	zfs_cmd_t zc = { 0 };
34802082Seschrock 	char errbuf[1024];
34812885Sahrens 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34822082Seschrock 
34833504Sahl 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
34843504Sahl 
34852885Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34862885Sahrens 	if (fromsnap)
34872885Sahrens 		(void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name));
34883504Sahl 	zc.zc_cookie = outfd;
34893504Sahl 
34903504Sahl 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) {
34913504Sahl 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34923504Sahl 		    "cannot send '%s'"), zhp->zfs_name);
34933504Sahl 
3494789Sahrens 		switch (errno) {
3495789Sahrens 
3496789Sahrens 		case EXDEV:
34972082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34983413Smmusante 			    "not an earlier snapshot from the same fs"));
34992082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3500789Sahrens 
3501789Sahrens 		case EDQUOT:
3502789Sahrens 		case EFBIG:
3503789Sahrens 		case EIO:
3504789Sahrens 		case ENOLINK:
3505789Sahrens 		case ENOSPC:
3506789Sahrens 		case ENOSTR:
3507789Sahrens 		case ENXIO:
3508789Sahrens 		case EPIPE:
3509789Sahrens 		case ERANGE:
3510789Sahrens 		case EFAULT:
3511789Sahrens 		case EROFS:
35122082Seschrock 			zfs_error_aux(hdl, strerror(errno));
35132082Seschrock 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
3514789Sahrens 
3515789Sahrens 		default:
35162082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3517789Sahrens 		}
3518789Sahrens 	}
3519789Sahrens 
35203504Sahl 	return (0);
3521789Sahrens }
3522789Sahrens 
3523789Sahrens /*
35242885Sahrens  * Create ancestors of 'target', but not target itself, and not
35252885Sahrens  * ancestors whose names are shorter than prefixlen.  Die if
35262885Sahrens  * prefixlen-ancestor does not exist.
35272885Sahrens  */
35282885Sahrens static int
35292885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
35302885Sahrens {
35312885Sahrens 	zfs_handle_t *h;
35322885Sahrens 	char *cp;
35332885Sahrens 
35342885Sahrens 	/* make sure prefix exists */
35352885Sahrens 	cp = strchr(target + prefixlen, '/');
35364603Sahrens 	if (cp == NULL) {
35374603Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
35384603Sahrens 	} else {
35394603Sahrens 		*cp = '\0';
35404603Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
35414603Sahrens 		*cp = '/';
35424603Sahrens 	}
35432885Sahrens 	if (h == NULL)
35442885Sahrens 		return (-1);
35452885Sahrens 	zfs_close(h);
35462885Sahrens 
35472885Sahrens 	/*
35482885Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
35492885Sahrens 	 * up to the prefixlen-long one.
35502885Sahrens 	 */
35512885Sahrens 	for (cp = target + prefixlen + 1;
35522885Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
35532885Sahrens 		const char *opname;
35544543Smarks 		char *logstr;
35552885Sahrens 
35562885Sahrens 		*cp = '\0';
35572885Sahrens 
35582885Sahrens 		h = make_dataset_handle(hdl, target);
35592885Sahrens 		if (h) {
35602885Sahrens 			/* it already exists, nothing to do here */
35612885Sahrens 			zfs_close(h);
35622885Sahrens 			continue;
35632885Sahrens 		}
35642885Sahrens 
35652885Sahrens 		opname = dgettext(TEXT_DOMAIN, "create");
35664543Smarks 		logstr = hdl->libzfs_log_str;
35674543Smarks 		hdl->libzfs_log_str = NULL;
35682885Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
35694543Smarks 		    NULL) != 0) {
35704543Smarks 			hdl->libzfs_log_str = logstr;
35712885Sahrens 			goto ancestorerr;
35724543Smarks 		}
35734543Smarks 
35744543Smarks 		hdl->libzfs_log_str = logstr;
35752885Sahrens 		opname = dgettext(TEXT_DOMAIN, "open");
35762885Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
35772885Sahrens 		if (h == NULL)
35782885Sahrens 			goto ancestorerr;
35792885Sahrens 
35802885Sahrens 		opname = dgettext(TEXT_DOMAIN, "mount");
35812885Sahrens 		if (zfs_mount(h, NULL, 0) != 0)
35822885Sahrens 			goto ancestorerr;
35832885Sahrens 
35842885Sahrens 		opname = dgettext(TEXT_DOMAIN, "share");
35852885Sahrens 		if (zfs_share(h) != 0)
35862885Sahrens 			goto ancestorerr;
35872885Sahrens 
35882885Sahrens 		zfs_close(h);
35892885Sahrens 
35902885Sahrens 		continue;
35912885Sahrens ancestorerr:
35922885Sahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35932885Sahrens 		    "failed to %s ancestor '%s'"), opname, target);
35942885Sahrens 		return (-1);
35952885Sahrens 	}
35962885Sahrens 
35972885Sahrens 	return (0);
35982885Sahrens }
35992885Sahrens 
36002885Sahrens /*
36013504Sahl  * Restores a backup of tosnap from the file descriptor specified by infd.
3602789Sahrens  */
3603789Sahrens int
36042082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix,
36053504Sahl     int verbose, int dryrun, boolean_t force, int infd)
3606789Sahrens {
3607789Sahrens 	zfs_cmd_t zc = { 0 };
3608789Sahrens 	time_t begin_time;
36092885Sahrens 	int ioctl_err, err, bytes, size, choplen;
3610789Sahrens 	char *cp;
3611789Sahrens 	dmu_replay_record_t drr;
3612789Sahrens 	struct drr_begin *drrb = &zc.zc_begin_record;
36132082Seschrock 	char errbuf[1024];
36142665Snd150628 	prop_changelist_t *clp;
36152885Sahrens 	char chopprefix[ZFS_MAXNAMELEN];
3616789Sahrens 
3617789Sahrens 	begin_time = time(NULL);
3618789Sahrens 
36192082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36202082Seschrock 	    "cannot receive"));
36212082Seschrock 
3622789Sahrens 	/* read in the BEGIN record */
3623789Sahrens 	cp = (char *)&drr;
3624789Sahrens 	bytes = 0;
3625789Sahrens 	do {
36263504Sahl 		size = read(infd, cp, sizeof (drr) - bytes);
3627868Sahrens 		cp += size;
3628868Sahrens 		bytes += size;
3629868Sahrens 	} while (size > 0);
3630868Sahrens 
3631868Sahrens 	if (size < 0 || bytes != sizeof (drr)) {
36322082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
36332082Seschrock 		    "stream (failed to read first record)"));
36342082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3635789Sahrens 	}
3636789Sahrens 
3637789Sahrens 	zc.zc_begin_record = drr.drr_u.drr_begin;
3638789Sahrens 
3639789Sahrens 	if (drrb->drr_magic != DMU_BACKUP_MAGIC &&
3640789Sahrens 	    drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) {
36412082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
36422082Seschrock 		    "stream (bad magic number)"));
36432082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3644789Sahrens 	}
3645789Sahrens 
3646789Sahrens 	if (drrb->drr_version != DMU_BACKUP_VERSION &&
3647789Sahrens 	    drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) {
36482082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version "
36492082Seschrock 		    "0x%llx is supported (stream is version 0x%llx)"),
3650789Sahrens 		    DMU_BACKUP_VERSION, drrb->drr_version);
36512082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3652789Sahrens 	}
3653789Sahrens 
36542885Sahrens 	if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) {
36552885Sahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
36563912Slling 		    "stream (bad snapshot name)"));
36572885Sahrens 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
36582885Sahrens 	}
3659789Sahrens 	/*
36602885Sahrens 	 * Determine how much of the snapshot name stored in the stream
36612885Sahrens 	 * we are going to tack on to the name they specified on the
36622885Sahrens 	 * command line, and how much we are going to chop off.
36632885Sahrens 	 *
36642885Sahrens 	 * If they specified a snapshot, chop the entire name stored in
36652885Sahrens 	 * the stream.
3666789Sahrens 	 */
36672885Sahrens 	(void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname);
3668789Sahrens 	if (isprefix) {
36692885Sahrens 		/*
36702885Sahrens 		 * They specified a fs with -d, we want to tack on
36712885Sahrens 		 * everything but the pool name stored in the stream
36722885Sahrens 		 */
36732885Sahrens 		if (strchr(tosnap, '@')) {
36742885Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
36752885Sahrens 			    "argument - snapshot not allowed with -d"));
36762885Sahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3677789Sahrens 		}
36782885Sahrens 		cp = strchr(chopprefix, '/');
3679789Sahrens 		if (cp == NULL)
36802885Sahrens 			cp = strchr(chopprefix, '@');
36812885Sahrens 		*cp = '\0';
3682789Sahrens 	} else if (strchr(tosnap, '@') == NULL) {
3683789Sahrens 		/*
36842885Sahrens 		 * If they specified a filesystem without -d, we want to
36852885Sahrens 		 * tack on everything after the fs specified in the
36862885Sahrens 		 * first name from the stream.
3687789Sahrens 		 */
36882885Sahrens 		cp = strchr(chopprefix, '@');
36892885Sahrens 		*cp = '\0';
3690789Sahrens 	}
36912885Sahrens 	choplen = strlen(chopprefix);
36922885Sahrens 
36932885Sahrens 	/*
36942885Sahrens 	 * Determine name of destination snapshot, store in zc_value.
36952885Sahrens 	 */
36962885Sahrens 	(void) strcpy(zc.zc_value, tosnap);
36972885Sahrens 	(void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen,
36982885Sahrens 	    sizeof (zc.zc_value));
36993265Sahrens 	if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT))
37003265Sahrens 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37012885Sahrens 
37022885Sahrens 	(void) strcpy(zc.zc_name, zc.zc_value);
3703789Sahrens 	if (drrb->drr_fromguid) {
3704789Sahrens 		/* incremental backup stream */
37052885Sahrens 		zfs_handle_t *h;
37062885Sahrens 
37072885Sahrens 		/* do the recvbackup ioctl to the containing fs */
37082885Sahrens 		*strchr(zc.zc_name, '@') = '\0';
3709789Sahrens 
3710789Sahrens 		/* make sure destination fs exists */
37112082Seschrock 		h = zfs_open(hdl, zc.zc_name,
37122082Seschrock 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
37132082Seschrock 		if (h == NULL)
3714789Sahrens 			return (-1);
3715868Sahrens 		if (!dryrun) {
37162665Snd150628 			/*
37172665Snd150628 			 * We need to unmount all the dependents of the dataset
37182665Snd150628 			 * and the dataset itself. If it's a volume
37192665Snd150628 			 * then remove device link.
37202665Snd150628 			 */
3721868Sahrens 			if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
37222665Snd150628 				clp = changelist_gather(h, ZFS_PROP_NAME, 0);
37232665Snd150628 				if (clp == NULL)
37242665Snd150628 					return (-1);
37252665Snd150628 				if (changelist_prefix(clp) != 0) {
37262665Snd150628 					changelist_free(clp);
37272665Snd150628 					return (-1);
37282665Snd150628 				}
3729868Sahrens 			} else {
37304543Smarks 				if (zvol_remove_link(hdl, h->zfs_name) != 0) {
37314543Smarks 					zfs_close(h);
37324543Smarks 					return (-1);
37334543Smarks 				}
37344543Smarks 
3735868Sahrens 			}
3736868Sahrens 		}
3737789Sahrens 		zfs_close(h);
3738789Sahrens 	} else {
3739789Sahrens 		/* full backup stream */
3740789Sahrens 
3741868Sahrens 		/* Make sure destination fs does not exist */
37422885Sahrens 		*strchr(zc.zc_name, '@') = '\0';
37434490Svb160487 		if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) {
37442082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37452082Seschrock 			    "destination '%s' exists"), zc.zc_name);
37462082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3747868Sahrens 		}
3748868Sahrens 
37492885Sahrens 		if (strchr(zc.zc_name, '/') == NULL) {
37502885Sahrens 			/*
37512885Sahrens 			 * they're trying to do a recv into a
37522885Sahrens 			 * nonexistant topmost filesystem.
37532885Sahrens 			 */
37542885Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37552885Sahrens 			    "destination does not exist"), zc.zc_name);
37562885Sahrens 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
37572885Sahrens 		}
37582885Sahrens 
3759868Sahrens 		/* Do the recvbackup ioctl to the fs's parent. */
37602885Sahrens 		*strrchr(zc.zc_name, '/') = '\0';
37612885Sahrens 
37622885Sahrens 		if (isprefix && (err = create_parents(hdl,
37632885Sahrens 		    zc.zc_value, strlen(tosnap))) != 0) {
37642885Sahrens 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
37652885Sahrens 		}
37662885Sahrens 
3767789Sahrens 	}
3768789Sahrens 
37693504Sahl 	zc.zc_cookie = infd;
37702676Seschrock 	zc.zc_guid = force;
3771789Sahrens 	if (verbose) {
37721749Sahrens 		(void) printf("%s %s stream of %s into %s\n",
37731749Sahrens 		    dryrun ? "would receive" : "receiving",
3774789Sahrens 		    drrb->drr_fromguid ? "incremental" : "full",
3775789Sahrens 		    drr.drr_u.drr_begin.drr_toname,
37762676Seschrock 		    zc.zc_value);
3777789Sahrens 		(void) fflush(stdout);
3778789Sahrens 	}
3779789Sahrens 	if (dryrun)
3780789Sahrens 		return (0);
37814543Smarks 	err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc);
3782868Sahrens 	if (ioctl_err != 0) {
3783789Sahrens 		switch (errno) {
3784789Sahrens 		case ENODEV:
37852082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37862082Seschrock 			    "most recent snapshot does not match incremental "
37872082Seschrock 			    "source"));
37882082Seschrock 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3789789Sahrens 			break;
3790789Sahrens 		case ETXTBSY:
37912082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37922082Seschrock 			    "destination has been modified since most recent "
37932082Seschrock 			    "snapshot"));
37942082Seschrock 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3795789Sahrens 			break;
3796789Sahrens 		case EEXIST:
3797789Sahrens 			if (drrb->drr_fromguid == 0) {
3798789Sahrens 				/* it's the containing fs that exists */
37992676Seschrock 				cp = strchr(zc.zc_value, '@');
3800789Sahrens 				*cp = '\0';
3801789Sahrens 			}
38022082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38032082Seschrock 			    "destination already exists"));
38043237Slling 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
38053237Slling 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
38063237Slling 			    zc.zc_value);
3807789Sahrens 			break;
3808789Sahrens 		case EINVAL:
38092082Seschrock 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3810868Sahrens 			break;
38111544Seschrock 		case ECKSUM:
38122082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38132082Seschrock 			    "invalid stream (checksum mismatch)"));
38142082Seschrock 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3815789Sahrens 			break;
3816789Sahrens 		default:
38172082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
3818789Sahrens 		}
3819789Sahrens 	}
3820789Sahrens 
3821789Sahrens 	/*
3822868Sahrens 	 * Mount or recreate the /dev links for the target filesystem
3823868Sahrens 	 * (if created, or if we tore them down to do an incremental
3824868Sahrens 	 * restore), and the /dev links for the new snapshot (if
38252665Snd150628 	 * created). Also mount any children of the target filesystem
38262665Snd150628 	 * if we did an incremental receive.
3827789Sahrens 	 */
38282676Seschrock 	cp = strchr(zc.zc_value, '@');
3829868Sahrens 	if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) {
3830789Sahrens 		zfs_handle_t *h;
3831789Sahrens 
3832789Sahrens 		*cp = '\0';
38332676Seschrock 		h = zfs_open(hdl, zc.zc_value,
3834789Sahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3835868Sahrens 		*cp = '@';
3836789Sahrens 		if (h) {
38372665Snd150628 			if (h->zfs_type == ZFS_TYPE_VOLUME) {
38382082Seschrock 				err = zvol_create_link(hdl, h->zfs_name);
38391544Seschrock 				if (err == 0 && ioctl_err == 0)
38402082Seschrock 					err = zvol_create_link(hdl,
38412676Seschrock 					    zc.zc_value);
38422665Snd150628 			} else {
38432665Snd150628 				if (drrb->drr_fromguid) {
38442665Snd150628 					err = changelist_postfix(clp);
38452665Snd150628 					changelist_free(clp);
38462665Snd150628 				} else {
38472665Snd150628 					err = zfs_mount(h, NULL, 0);
38482665Snd150628 				}
3849868Sahrens 			}
38502665Snd150628 		zfs_close(h);
3851789Sahrens 		}
3852789Sahrens 	}
3853789Sahrens 
3854868Sahrens 	if (err || ioctl_err)
3855868Sahrens 		return (-1);
3856789Sahrens 
3857789Sahrens 	if (verbose) {
3858789Sahrens 		char buf1[64];
3859789Sahrens 		char buf2[64];
3860789Sahrens 		uint64_t bytes = zc.zc_cookie;
3861789Sahrens 		time_t delta = time(NULL) - begin_time;
3862789Sahrens 		if (delta == 0)
3863789Sahrens 			delta = 1;
3864789Sahrens 		zfs_nicenum(bytes, buf1, sizeof (buf1));
3865789Sahrens 		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3866789Sahrens 
38674603Sahrens 		(void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3868789Sahrens 		    buf1, delta, buf2);
3869789Sahrens 	}
38702665Snd150628 
3871789Sahrens 	return (0);
3872789Sahrens }
3873789Sahrens 
3874789Sahrens /*
38751294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
38761294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
38771294Slling  * is a dependent and we should just destroy it without checking the transaction
38781294Slling  * group.
3879789Sahrens  */
38801294Slling typedef struct rollback_data {
38811294Slling 	const char	*cb_target;		/* the snapshot */
38821294Slling 	uint64_t	cb_create;		/* creation time reference */
38831294Slling 	prop_changelist_t *cb_clp;		/* changelist pointer */
38841294Slling 	int		cb_error;
38852082Seschrock 	boolean_t	cb_dependent;
38861294Slling } rollback_data_t;
38871294Slling 
38881294Slling static int
38891294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
38901294Slling {
38911294Slling 	rollback_data_t *cbp = data;
38921294Slling 
38931294Slling 	if (!cbp->cb_dependent) {
38941294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
38951294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
38961294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
38971294Slling 		    cbp->cb_create) {
38984543Smarks 			char *logstr;
38991294Slling 
39002082Seschrock 			cbp->cb_dependent = B_TRUE;
39012474Seschrock 			if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy,
39022474Seschrock 			    cbp) != 0)
39032474Seschrock 				cbp->cb_error = 1;
39042082Seschrock 			cbp->cb_dependent = B_FALSE;
39051294Slling 
39064543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
39074543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
39081294Slling 			if (zfs_destroy(zhp) != 0)
39091294Slling 				cbp->cb_error = 1;
39101294Slling 			else
39111294Slling 				changelist_remove(zhp, cbp->cb_clp);
39124543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
39131294Slling 		}
39141294Slling 	} else {
39151294Slling 		if (zfs_destroy(zhp) != 0)
39161294Slling 			cbp->cb_error = 1;
39171294Slling 		else
39181294Slling 			changelist_remove(zhp, cbp->cb_clp);
39191294Slling 	}
39201294Slling 
39211294Slling 	zfs_close(zhp);
39221294Slling 	return (0);
39231294Slling }
39241294Slling 
39251294Slling /*
39261294Slling  * Rollback the dataset to its latest snapshot.
39271294Slling  */
39281294Slling static int
39291294Slling do_rollback(zfs_handle_t *zhp)
3930789Sahrens {
3931789Sahrens 	int ret;
3932789Sahrens 	zfs_cmd_t zc = { 0 };
3933789Sahrens 
3934789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3935789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3936789Sahrens 
3937789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME &&
39382082Seschrock 	    zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3939789Sahrens 		return (-1);
3940789Sahrens 
3941789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3942789Sahrens 
39432676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3944789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3945789Sahrens 	else
3946789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3947789Sahrens 
3948789Sahrens 	/*
3949789Sahrens 	 * We rely on the consumer to verify that there are no newer snapshots
3950789Sahrens 	 * for the given dataset.  Given these constraints, we can simply pass
3951789Sahrens 	 * the name on to the ioctl() call.  There is still an unlikely race
3952789Sahrens 	 * condition where the user has taken a snapshot since we verified that
3953789Sahrens 	 * this was the most recent.
3954789Sahrens 	 */
39554543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
39563237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
39572082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
39582082Seschrock 		    zhp->zfs_name);
3959789Sahrens 	} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
39602082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3961789Sahrens 	}
3962789Sahrens 
3963789Sahrens 	return (ret);
3964789Sahrens }
3965789Sahrens 
3966789Sahrens /*
39671294Slling  * Given a dataset, rollback to a specific snapshot, discarding any
39681294Slling  * data changes since then and making it the active dataset.
39691294Slling  *
39701294Slling  * Any snapshots more recent than the target are destroyed, along with
39711294Slling  * their dependents.
39721294Slling  */
39731294Slling int
39741294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag)
39751294Slling {
39761294Slling 	int ret;
39771294Slling 	rollback_data_t cb = { 0 };
39781294Slling 	prop_changelist_t *clp;
39791294Slling 
39801294Slling 	/*
39811294Slling 	 * Unmount all dependendents of the dataset and the dataset itself.
39821294Slling 	 * The list we need to gather is the same as for doing rename
39831294Slling 	 */
39841294Slling 	clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0);
39851294Slling 	if (clp == NULL)
39861294Slling 		return (-1);
39871294Slling 
39881294Slling 	if ((ret = changelist_prefix(clp)) != 0)
39891294Slling 		goto out;
39901294Slling 
39911294Slling 	/*
39921294Slling 	 * Destroy all recent snapshots and its dependends.
39931294Slling 	 */
39941294Slling 	cb.cb_target = snap->zfs_name;
39951294Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
39961294Slling 	cb.cb_clp = clp;
39971294Slling 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
39981294Slling 
39991294Slling 	if ((ret = cb.cb_error) != 0) {
40001294Slling 		(void) changelist_postfix(clp);
40011294Slling 		goto out;
40021294Slling 	}
40031294Slling 
40041294Slling 	/*
40051294Slling 	 * Now that we have verified that the snapshot is the latest,
40061294Slling 	 * rollback to the given snapshot.
40071294Slling 	 */
40081294Slling 	ret = do_rollback(zhp);
40091294Slling 
40101294Slling 	if (ret != 0) {
40111294Slling 		(void) changelist_postfix(clp);
40121294Slling 		goto out;
40131294Slling 	}
40141294Slling 
40151294Slling 	/*
40161294Slling 	 * We only want to re-mount the filesystem if it was mounted in the
40171294Slling 	 * first place.
40181294Slling 	 */
40191294Slling 	ret = changelist_postfix(clp);
40201294Slling 
40211294Slling out:
40221294Slling 	changelist_free(clp);
40231294Slling 	return (ret);
40241294Slling }
40251294Slling 
40261294Slling /*
4027789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
4028789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
4029789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
4030789Sahrens  * libzfs_graph.c.
4031789Sahrens  */
4032789Sahrens int
40332474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
40342474Seschrock     zfs_iter_f func, void *data)
4035789Sahrens {
4036789Sahrens 	char **dependents;
4037789Sahrens 	size_t count;
4038789Sahrens 	int i;
4039789Sahrens 	zfs_handle_t *child;
4040789Sahrens 	int ret = 0;
4041789Sahrens 
40422474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
40432474Seschrock 	    &dependents, &count) != 0)
40442474Seschrock 		return (-1);
40452474Seschrock 
4046789Sahrens 	for (i = 0; i < count; i++) {
40472082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
40482082Seschrock 		    dependents[i])) == NULL)
4049789Sahrens 			continue;
4050789Sahrens 
4051789Sahrens 		if ((ret = func(child, data)) != 0)
4052789Sahrens 			break;
4053789Sahrens 	}
4054789Sahrens 
4055789Sahrens 	for (i = 0; i < count; i++)
4056789Sahrens 		free(dependents[i]);
4057789Sahrens 	free(dependents);
4058789Sahrens 
4059789Sahrens 	return (ret);
4060789Sahrens }
4061789Sahrens 
4062789Sahrens /*
4063789Sahrens  * Renames the given dataset.
4064789Sahrens  */
4065789Sahrens int
40664490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
4067789Sahrens {
4068789Sahrens 	int ret;
4069789Sahrens 	zfs_cmd_t zc = { 0 };
4070789Sahrens 	char *delim;
40714007Smmusante 	prop_changelist_t *cl = NULL;
40724007Smmusante 	zfs_handle_t *zhrp = NULL;
40734007Smmusante 	char *parentname = NULL;
4074789Sahrens 	char parent[ZFS_MAXNAMELEN];
40752082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40762082Seschrock 	char errbuf[1024];
4077789Sahrens 
4078789Sahrens 	/* if we have the same exact name, just return success */
4079789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
4080789Sahrens 		return (0);
4081789Sahrens 
40822082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40832082Seschrock 	    "cannot rename to '%s'"), target);
40842082Seschrock 
4085789Sahrens 	/*
4086789Sahrens 	 * Make sure the target name is valid
4087789Sahrens 	 */
4088789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
40892665Snd150628 		if ((strchr(target, '@') == NULL) ||
40902665Snd150628 		    *target == '@') {
40912665Snd150628 			/*
40922665Snd150628 			 * Snapshot target name is abbreviated,
40932665Snd150628 			 * reconstruct full dataset name
40942665Snd150628 			 */
40952665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
40962665Snd150628 			    sizeof (parent));
40972665Snd150628 			delim = strchr(parent, '@');
40982665Snd150628 			if (strchr(target, '@') == NULL)
40992665Snd150628 				*(++delim) = '\0';
41002665Snd150628 			else
41012665Snd150628 				*delim = '\0';
41022665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
41032665Snd150628 			target = parent;
41042665Snd150628 		} else {
41052665Snd150628 			/*
41062665Snd150628 			 * Make sure we're renaming within the same dataset.
41072665Snd150628 			 */
41082665Snd150628 			delim = strchr(target, '@');
41092665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
41102665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
41112665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41122665Snd150628 				    "snapshots must be part of same "
41132665Snd150628 				    "dataset"));
41142665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
41153912Slling 				    errbuf));
41162665Snd150628 			}
4117789Sahrens 		}
41182665Snd150628 		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
41192665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4120789Sahrens 	} else {
41214007Smmusante 		if (recursive) {
41224007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41234007Smmusante 			    "recursive rename must be a snapshot"));
41244007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
41254007Smmusante 		}
41264007Smmusante 
41272665Snd150628 		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
41282665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41292676Seschrock 		uint64_t unused;
41302676Seschrock 
4131789Sahrens 		/* validate parents */
41324490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
4133789Sahrens 			return (-1);
4134789Sahrens 
4135789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
4136789Sahrens 
4137789Sahrens 		/* make sure we're in the same pool */
4138789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4139789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4140789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
41412082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41422082Seschrock 			    "datasets must be within same pool"));
41432082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4144789Sahrens 		}
41452440Snd150628 
41462440Snd150628 		/* new name cannot be a child of the current dataset name */
41472440Snd150628 		if (strncmp(parent, zhp->zfs_name,
41483912Slling 		    strlen(zhp->zfs_name)) == 0) {
41492440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41502440Snd150628 			    "New dataset name cannot be a descendent of "
41512440Snd150628 			    "current dataset name"));
41522440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41532440Snd150628 		}
4154789Sahrens 	}
4155789Sahrens 
41562082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
41572082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
41582082Seschrock 
4159789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4160789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
41612082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41622082Seschrock 		    "dataset is used in a non-global zone"));
41632082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4164789Sahrens 	}
4165789Sahrens 
41664007Smmusante 	if (recursive) {
41674007Smmusante 		struct destroydata dd;
41684007Smmusante 
41694183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
41704183Smmusante 		if (parentname == NULL) {
41714183Smmusante 			ret = -1;
41724183Smmusante 			goto error;
41734183Smmusante 		}
41744007Smmusante 		delim = strchr(parentname, '@');
41754007Smmusante 		*delim = '\0';
41764007Smmusante 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY);
41774007Smmusante 		if (zhrp == NULL) {
41784183Smmusante 			ret = -1;
41794183Smmusante 			goto error;
41804007Smmusante 		}
41814007Smmusante 
41824007Smmusante 		dd.snapname = delim + 1;
41834007Smmusante 		dd.gotone = B_FALSE;
41844183Smmusante 		dd.closezhp = B_TRUE;
41854007Smmusante 
41864007Smmusante 		/* We remove any zvol links prior to renaming them */
41874007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
41884007Smmusante 		if (ret) {
41894007Smmusante 			goto error;
41904007Smmusante 		}
41914007Smmusante 	} else {
41924007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
41934007Smmusante 			return (-1);
41944007Smmusante 
41954007Smmusante 		if (changelist_haszonedchild(cl)) {
41964007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41974007Smmusante 			    "child dataset with inherited mountpoint is used "
41984007Smmusante 			    "in a non-global zone"));
41994007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
42004007Smmusante 			goto error;
42014007Smmusante 		}
42024007Smmusante 
42034007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
42044007Smmusante 			goto error;
4205789Sahrens 	}
4206789Sahrens 
42072676Seschrock 	if (ZFS_IS_VOLUME(zhp))
4208789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4209789Sahrens 	else
4210789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4211789Sahrens 
42122665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
42132676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
42142665Snd150628 
42154007Smmusante 	zc.zc_cookie = recursive;
42164007Smmusante 
42174543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
42184007Smmusante 		/*
42194007Smmusante 		 * if it was recursive, the one that actually failed will
42204007Smmusante 		 * be in zc.zc_name
42214007Smmusante 		 */
42224007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
42234007Smmusante 		    "cannot rename to '%s'"), zc.zc_name);
42244007Smmusante 
42254007Smmusante 		if (recursive && errno == EEXIST) {
42264007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42274007Smmusante 			    "a child dataset already has a snapshot "
42284007Smmusante 			    "with the new name"));
4229*4801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
42304007Smmusante 		} else {
42314007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
42324007Smmusante 		}
4233789Sahrens 
4234789Sahrens 		/*
4235789Sahrens 		 * On failure, we still want to remount any filesystems that
4236789Sahrens 		 * were previously mounted, so we don't alter the system state.
4237789Sahrens 		 */
42384007Smmusante 		if (recursive) {
42394007Smmusante 			struct createdata cd;
42404007Smmusante 
42414007Smmusante 			/* only create links for datasets that had existed */
42424007Smmusante 			cd.cd_snapname = delim + 1;
42434007Smmusante 			cd.cd_ifexists = B_TRUE;
42444007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
42454007Smmusante 			    &cd);
42464007Smmusante 		} else {
42474007Smmusante 			(void) changelist_postfix(cl);
42484007Smmusante 		}
4249789Sahrens 	} else {
42504007Smmusante 		if (recursive) {
42514007Smmusante 			struct createdata cd;
42524007Smmusante 
42534007Smmusante 			/* only create links for datasets that had existed */
42544007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
42554007Smmusante 			cd.cd_ifexists = B_TRUE;
42564007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
42574007Smmusante 			    &cd);
42584007Smmusante 		} else {
42594007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
42604007Smmusante 			ret = changelist_postfix(cl);
42614007Smmusante 		}
4262789Sahrens 	}
4263789Sahrens 
4264789Sahrens error:
42654007Smmusante 	if (parentname) {
42664007Smmusante 		free(parentname);
42674007Smmusante 	}
42684007Smmusante 	if (zhrp) {
42694007Smmusante 		zfs_close(zhrp);
42704007Smmusante 	}
42714007Smmusante 	if (cl) {
42724007Smmusante 		changelist_free(cl);
42734007Smmusante 	}
4274789Sahrens 	return (ret);
4275789Sahrens }
4276789Sahrens 
4277789Sahrens /*
4278789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
4279789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
4280789Sahrens  */
4281789Sahrens int
42822082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
4283789Sahrens {
42844007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
42854007Smmusante }
42864007Smmusante 
42874007Smmusante static int
42884007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
42894007Smmusante {
4290789Sahrens 	zfs_cmd_t zc = { 0 };
42912082Seschrock 	di_devlink_handle_t dhdl;
42924543Smarks 	priv_set_t *priv_effective;
42934543Smarks 	int privileged;
4294789Sahrens 
4295789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4296789Sahrens 
4297789Sahrens 	/*
4298789Sahrens 	 * Issue the appropriate ioctl.
4299789Sahrens 	 */
43002082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4301789Sahrens 		switch (errno) {
4302789Sahrens 		case EEXIST:
4303789Sahrens 			/*
4304789Sahrens 			 * Silently ignore the case where the link already
4305789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
4306789Sahrens 			 * times without errors.
4307789Sahrens 			 */
4308789Sahrens 			return (0);
4309789Sahrens 
43104007Smmusante 		case ENOENT:
43114007Smmusante 			/*
43124007Smmusante 			 * Dataset does not exist in the kernel.  If we
43134007Smmusante 			 * don't care (see zfs_rename), then ignore the
43144007Smmusante 			 * error quietly.
43154007Smmusante 			 */
43164007Smmusante 			if (ifexists) {
43174007Smmusante 				return (0);
43184007Smmusante 			}
43194007Smmusante 
43204007Smmusante 			/* FALLTHROUGH */
43214007Smmusante 
4322789Sahrens 		default:
43233237Slling 			return (zfs_standard_error_fmt(hdl, errno,
43242082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
43252082Seschrock 			    "for '%s'"), dataset));
4326789Sahrens 		}
4327789Sahrens 	}
4328789Sahrens 
4329789Sahrens 	/*
43304543Smarks 	 * If privileged call devfsadm and wait for the links to
43314543Smarks 	 * magically appear.
43324543Smarks 	 * Otherwise, print out an informational message.
4333789Sahrens 	 */
43344543Smarks 
43354543Smarks 	priv_effective = priv_allocset();
43364543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
43374543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
43384543Smarks 	priv_freeset(priv_effective);
43394543Smarks 
43404543Smarks 	if (privileged) {
43414543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
43424543Smarks 		    DI_MAKE_LINK)) == NULL) {
43434543Smarks 			zfs_error_aux(hdl, strerror(errno));
43444543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
43454543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
43464543Smarks 			    "for '%s'"), dataset);
43474543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
43484543Smarks 			return (-1);
43494543Smarks 		} else {
43504543Smarks 			(void) di_devlink_fini(&dhdl);
43514543Smarks 		}
4352789Sahrens 	} else {
43534543Smarks 		char pathname[MAXPATHLEN];
43544543Smarks 		struct stat64 statbuf;
43554543Smarks 		int i;
43564543Smarks 
43574543Smarks #define	MAX_WAIT	10
43584543Smarks 
43594543Smarks 		/*
43604543Smarks 		 * This is the poor mans way of waiting for the link
43614543Smarks 		 * to show up.  If after 10 seconds we still don't
43624543Smarks 		 * have it, then print out a message.
43634543Smarks 		 */
43644543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
43654543Smarks 		    dataset);
43664543Smarks 
43674543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
43684543Smarks 			if (stat64(pathname, &statbuf) == 0)
43694543Smarks 				break;
43704543Smarks 			(void) sleep(1);
43714543Smarks 		}
43724543Smarks 		if (i == MAX_WAIT)
43734543Smarks 			(void) printf(gettext("%s may not be immediately "
43744543Smarks 			    "available\n"), pathname);
4375789Sahrens 	}
4376789Sahrens 
4377789Sahrens 	return (0);
4378789Sahrens }
4379789Sahrens 
4380789Sahrens /*
4381789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4382789Sahrens  */
4383789Sahrens int
43842082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4385789Sahrens {
4386789Sahrens 	zfs_cmd_t zc = { 0 };
4387789Sahrens 
4388789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4389789Sahrens 
43902082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4391789Sahrens 		switch (errno) {
4392789Sahrens 		case ENXIO:
4393789Sahrens 			/*
4394789Sahrens 			 * Silently ignore the case where the link no longer
4395789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4396789Sahrens 			 * times without errors.
4397789Sahrens 			 */
4398789Sahrens 			return (0);
4399789Sahrens 
4400789Sahrens 		default:
44013237Slling 			return (zfs_standard_error_fmt(hdl, errno,
44022082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
44032082Seschrock 			    "links for '%s'"), dataset));
4404789Sahrens 		}
4405789Sahrens 	}
4406789Sahrens 
4407789Sahrens 	return (0);
4408789Sahrens }
44092676Seschrock 
44102676Seschrock nvlist_t *
44112676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
44122676Seschrock {
44132676Seschrock 	return (zhp->zfs_user_props);
44142676Seschrock }
44152676Seschrock 
44162676Seschrock /*
44174451Seschrock  * Given a comma-separated list of properties, construct a property list
44182676Seschrock  * containing both user-defined and native properties.  This function will
44192676Seschrock  * return a NULL list if 'all' is specified, which can later be expanded on a
44202676Seschrock  * per-dataset basis by zfs_expand_proplist().
44212676Seschrock  */
44222676Seschrock int
44233912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields,
44243912Slling     zfs_proplist_t **listp, zfs_type_t type)
44252676Seschrock {
44262676Seschrock 	size_t len;
44272676Seschrock 	char *s, *p;
44282676Seschrock 	char c;
44292676Seschrock 	zfs_prop_t prop;
44302676Seschrock 	zfs_proplist_t *entry;
44312676Seschrock 	zfs_proplist_t **last;
44322676Seschrock 
44332676Seschrock 	*listp = NULL;
44342676Seschrock 	last = listp;
44352676Seschrock 
44362676Seschrock 	/*
44372676Seschrock 	 * If 'all' is specified, return a NULL list.
44382676Seschrock 	 */
44392676Seschrock 	if (strcmp(fields, "all") == 0)
44402676Seschrock 		return (0);
44412676Seschrock 
44422676Seschrock 	/*
44432676Seschrock 	 * If no fields were specified, return an error.
44442676Seschrock 	 */
44452676Seschrock 	if (fields[0] == '\0') {
44462676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44472676Seschrock 		    "no properties specified"));
44482676Seschrock 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
44492676Seschrock 		    "bad property list")));
44502676Seschrock 	}
44512676Seschrock 
44522676Seschrock 	/*
44532676Seschrock 	 * It would be nice to use getsubopt() here, but the inclusion of column
44542676Seschrock 	 * aliases makes this more effort than it's worth.
44552676Seschrock 	 */
44562676Seschrock 	s = fields;
44572676Seschrock 	while (*s != '\0') {
44582676Seschrock 		if ((p = strchr(s, ',')) == NULL) {
44592676Seschrock 			len = strlen(s);
44602676Seschrock 			p = s + len;
44612676Seschrock 		} else {
44622676Seschrock 			len = p - s;
44632676Seschrock 		}
44642676Seschrock 
44652676Seschrock 		/*
44662676Seschrock 		 * Check for empty options.
44672676Seschrock 		 */
44682676Seschrock 		if (len == 0) {
44692676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44702676Seschrock 			    "empty property name"));
44712676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP,
44722676Seschrock 			    dgettext(TEXT_DOMAIN, "bad property list")));
44732676Seschrock 		}
44742676Seschrock 
44752676Seschrock 		/*
44762676Seschrock 		 * Check all regular property names.
44772676Seschrock 		 */
44782676Seschrock 		c = s[len];
44792676Seschrock 		s[len] = '\0';
44804451Seschrock 		prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) :
44814451Seschrock 		    zfs_name_to_prop(s);
44823912Slling 
44833912Slling 		if (prop != ZFS_PROP_INVAL &&
44843912Slling 		    !zfs_prop_valid_for_type(prop, type))
44853912Slling 			prop = ZFS_PROP_INVAL;
44862676Seschrock 
44872676Seschrock 		/*
44883912Slling 		 * When no property table entry can be found, return failure if
44893912Slling 		 * this is a pool property or if this isn't a user-defined
44903912Slling 		 * dataset property,
44912676Seschrock 		 */
44923912Slling 		if (prop == ZFS_PROP_INVAL &&
44933912Slling 		    (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) {
44942676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44952676Seschrock 			    "invalid property '%s'"), s);
44962676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP,
44972676Seschrock 			    dgettext(TEXT_DOMAIN, "bad property list")));
44982676Seschrock 		}
44992676Seschrock 
45002676Seschrock 		if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL)
45012676Seschrock 			return (-1);
45022676Seschrock 
45032676Seschrock 		entry->pl_prop = prop;
45042676Seschrock 		if (prop == ZFS_PROP_INVAL) {
45052676Seschrock 			if ((entry->pl_user_prop =
45062676Seschrock 			    zfs_strdup(hdl, s)) == NULL) {
45072676Seschrock 				free(entry);
45082676Seschrock 				return (-1);
45092676Seschrock 			}
45102676Seschrock 			entry->pl_width = strlen(s);
45112676Seschrock 		} else {
45122676Seschrock 			entry->pl_width = zfs_prop_width(prop,
45132676Seschrock 			    &entry->pl_fixed);
45142676Seschrock 		}
45152676Seschrock 
45162676Seschrock 		*last = entry;
45172676Seschrock 		last = &entry->pl_next;
45182676Seschrock 
45192676Seschrock 		s = p;
45202676Seschrock 		if (c == ',')
45212676Seschrock 			s++;
45222676Seschrock 	}
45232676Seschrock 
45242676Seschrock 	return (0);
45252676Seschrock }
45262676Seschrock 
45273912Slling int
45283912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp)
45293912Slling {
45303912Slling 	return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY));
45313912Slling }
45323912Slling 
45332676Seschrock void
45342676Seschrock zfs_free_proplist(zfs_proplist_t *pl)
45352676Seschrock {
45362676Seschrock 	zfs_proplist_t *next;
45372676Seschrock 
45382676Seschrock 	while (pl != NULL) {
45392676Seschrock 		next = pl->pl_next;
45402676Seschrock 		free(pl->pl_user_prop);
45412676Seschrock 		free(pl);
45422676Seschrock 		pl = next;
45432676Seschrock 	}
45442676Seschrock }
45452676Seschrock 
45463654Sgw25295 typedef struct expand_data {
45473654Sgw25295 	zfs_proplist_t	**last;
45483654Sgw25295 	libzfs_handle_t	*hdl;
45493654Sgw25295 } expand_data_t;
45503654Sgw25295 
45513654Sgw25295 static zfs_prop_t
45523654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb)
45533654Sgw25295 {
45543654Sgw25295 	zfs_proplist_t *entry;
45553654Sgw25295 	expand_data_t *edp = cb;
45563654Sgw25295 
45573654Sgw25295 	if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL)
45583654Sgw25295 		return (ZFS_PROP_INVAL);
45593654Sgw25295 
45603654Sgw25295 	entry->pl_prop = prop;
45613654Sgw25295 	entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed);
45623654Sgw25295 	entry->pl_all = B_TRUE;
45633654Sgw25295 
45643654Sgw25295 	*(edp->last) = entry;
45653654Sgw25295 	edp->last = &entry->pl_next;
45663654Sgw25295 
45673654Sgw25295 	return (ZFS_PROP_CONT);
45683654Sgw25295 }
45693654Sgw25295 
45702676Seschrock int
45713912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp,
45723912Slling 	zfs_type_t type)
45732676Seschrock {
45742676Seschrock 	zfs_proplist_t *entry;
45753912Slling 	zfs_proplist_t **last;
45763654Sgw25295 	expand_data_t exp;
45772676Seschrock 
45782676Seschrock 	if (*plp == NULL) {
45792676Seschrock 		/*
45802676Seschrock 		 * If this is the very first time we've been called for an 'all'
45812676Seschrock 		 * specification, expand the list to include all native
45822676Seschrock 		 * properties.
45832676Seschrock 		 */
45842676Seschrock 		last = plp;
45853654Sgw25295 
45863654Sgw25295 		exp.last = last;
45873654Sgw25295 		exp.hdl = hdl;
45883654Sgw25295 
45893912Slling 		if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type,
45904597Stimf 		    B_FALSE, B_FALSE) == ZFS_PROP_INVAL)
45913654Sgw25295 			return (-1);
45922676Seschrock 
45932676Seschrock 		/*
45942676Seschrock 		 * Add 'name' to the beginning of the list, which is handled
45952676Seschrock 		 * specially.
45962676Seschrock 		 */
45972676Seschrock 		if ((entry = zfs_alloc(hdl,
45982676Seschrock 		    sizeof (zfs_proplist_t))) == NULL)
45992676Seschrock 			return (-1);
46002676Seschrock 
46012676Seschrock 		entry->pl_prop = ZFS_PROP_NAME;
46022676Seschrock 		entry->pl_width = zfs_prop_width(ZFS_PROP_NAME,
46032676Seschrock 		    &entry->pl_fixed);
46042676Seschrock 		entry->pl_all = B_TRUE;
46052676Seschrock 		entry->pl_next = *plp;
46062676Seschrock 		*plp = entry;
46072676Seschrock 	}
46083912Slling 	return (0);
46093912Slling }
46103912Slling 
46113912Slling /*
46123912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
46133912Slling  * display, and their maximum widths.  This does two main things:
46143912Slling  *
46153912Slling  *      - If this is a list of all properties, then expand the list to include
46163912Slling  *        all native properties, and set a flag so that for each dataset we look
46173912Slling  *        for new unique user properties and add them to the list.
46183912Slling  *
46193912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
46203912Slling  *        so that we can size the column appropriately.
46213912Slling  */
46223912Slling int
46233912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp)
46243912Slling {
46253912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
46263912Slling 	zfs_proplist_t *entry;
46273912Slling 	zfs_proplist_t **last, **start;
46283912Slling 	nvlist_t *userprops, *propval;
46293912Slling 	nvpair_t *elem;
46303912Slling 	char *strval;
46313912Slling 	char buf[ZFS_MAXPROPLEN];
46323912Slling 
46333912Slling 	if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0)
46343912Slling 		return (-1);
46352676Seschrock 
46362676Seschrock 	userprops = zfs_get_user_props(zhp);
46372676Seschrock 
46382676Seschrock 	entry = *plp;
46392676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
46402676Seschrock 		/*
46412676Seschrock 		 * Go through and add any user properties as necessary.  We
46422676Seschrock 		 * start by incrementing our list pointer to the first
46432676Seschrock 		 * non-native property.
46442676Seschrock 		 */
46452676Seschrock 		start = plp;
46462676Seschrock 		while (*start != NULL) {
46472676Seschrock 			if ((*start)->pl_prop == ZFS_PROP_INVAL)
46482676Seschrock 				break;
46492676Seschrock 			start = &(*start)->pl_next;
46502676Seschrock 		}
46512676Seschrock 
46522676Seschrock 		elem = NULL;
46532676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
46542676Seschrock 			/*
46552676Seschrock 			 * See if we've already found this property in our list.
46562676Seschrock 			 */
46572676Seschrock 			for (last = start; *last != NULL;
46582676Seschrock 			    last = &(*last)->pl_next) {
46592676Seschrock 				if (strcmp((*last)->pl_user_prop,
46602676Seschrock 				    nvpair_name(elem)) == 0)
46612676Seschrock 					break;
46622676Seschrock 			}
46632676Seschrock 
46642676Seschrock 			if (*last == NULL) {
46652676Seschrock 				if ((entry = zfs_alloc(hdl,
46662676Seschrock 				    sizeof (zfs_proplist_t))) == NULL ||
46672676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
46682676Seschrock 				    nvpair_name(elem)))) == NULL) {
46692676Seschrock 					free(entry);
46702676Seschrock 					return (-1);
46712676Seschrock 				}
46722676Seschrock 
46732676Seschrock 				entry->pl_prop = ZFS_PROP_INVAL;
46742676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
46752676Seschrock 				entry->pl_all = B_TRUE;
46762676Seschrock 				*last = entry;
46772676Seschrock 			}
46782676Seschrock 		}
46792676Seschrock 	}
46802676Seschrock 
46812676Seschrock 	/*
46822676Seschrock 	 * Now go through and check the width of any non-fixed columns
46832676Seschrock 	 */
46842676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
46852676Seschrock 		if (entry->pl_fixed)
46862676Seschrock 			continue;
46872676Seschrock 
46882676Seschrock 		if (entry->pl_prop != ZFS_PROP_INVAL) {
46892676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
46902676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
46912676Seschrock 				if (strlen(buf) > entry->pl_width)
46922676Seschrock 					entry->pl_width = strlen(buf);
46932676Seschrock 			}
46942676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
46952676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
46962676Seschrock 			verify(nvlist_lookup_string(propval,
46972676Seschrock 			    ZFS_PROP_VALUE, &strval) == 0);
46982676Seschrock 			if (strlen(strval) > entry->pl_width)
46992676Seschrock 				entry->pl_width = strlen(strval);
47002676Seschrock 		}
47012676Seschrock 	}
47022676Seschrock 
47032676Seschrock 	return (0);
47042676Seschrock }
47054543Smarks 
47064543Smarks int
47074543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
47084543Smarks {
47094543Smarks 	zfs_cmd_t zc = { 0 };
47104543Smarks 	nvlist_t *nvp;
47114543Smarks 	size_t sz;
47124543Smarks 	gid_t gid;
47134543Smarks 	uid_t uid;
47144543Smarks 	const gid_t *groups;
47154543Smarks 	int group_cnt;
47164543Smarks 	int error;
47174543Smarks 
47184543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
47194543Smarks 		return (no_memory(hdl));
47204543Smarks 
47214543Smarks 	uid = ucred_geteuid(cred);
47224543Smarks 	gid = ucred_getegid(cred);
47234543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
47244543Smarks 
47254543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
47264543Smarks 		return (1);
47274543Smarks 
47284543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
47294543Smarks 		nvlist_free(nvp);
47304543Smarks 		return (1);
47314543Smarks 	}
47324543Smarks 
47334543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
47344543Smarks 		nvlist_free(nvp);
47354543Smarks 		return (1);
47364543Smarks 	}
47374543Smarks 
47384543Smarks 	if (nvlist_add_uint32_array(nvp,
47394543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
47404543Smarks 		nvlist_free(nvp);
47414543Smarks 		return (1);
47424543Smarks 	}
47434543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
47444543Smarks 
47454543Smarks 	if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz))
47464543Smarks 		return (-1);
47474543Smarks 
47484543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
47494543Smarks 	nvlist_free(nvp);
47504543Smarks 	return (error);
47514543Smarks }
47524543Smarks 
47534543Smarks int
47544543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
47554543Smarks     void *export, void *sharetab, int sharemax, boolean_t share_on)
47564543Smarks {
47574543Smarks 	zfs_cmd_t zc = { 0 };
47584543Smarks 	int error;
47594543Smarks 
47604543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
47614543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
47624543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
47634543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
47644543Smarks 	zc.zc_share.z_sharetype = share_on;
47654543Smarks 	zc.zc_share.z_sharemax = sharemax;
47664543Smarks 
47674543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
47684543Smarks 	return (error);
47694543Smarks }
4770