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 	 */
4235094Slling 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET)) {
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 
4613912Slling 
4623912Slling /*
4632676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
4642676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
4652676Seschrock  * strings.
466789Sahrens  */
4675094Slling static nvlist_t *
4685094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
4695094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
470789Sahrens {
4712676Seschrock 	nvpair_t *elem;
4722676Seschrock 	uint64_t intval;
4732676Seschrock 	char *strval;
4745094Slling 	zfs_prop_t prop;
4752676Seschrock 	nvlist_t *ret;
4762676Seschrock 
4772676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
4782676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4793413Smmusante 		    "snapshot properties cannot be modified"));
4802676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
4815094Slling 		return (NULL);
4825094Slling 	}
4835094Slling 
4845094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
4855094Slling 		(void) no_memory(hdl);
4865094Slling 		return (NULL);
487789Sahrens 	}
488789Sahrens 
4892676Seschrock 	elem = NULL;
4902676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
4915094Slling 		const char *propname = nvpair_name(elem);
4922676Seschrock 
4932676Seschrock 		/*
4942676Seschrock 		 * Make sure this property is valid and applies to this type.
4952676Seschrock 		 */
4965094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
4975094Slling 			if (!zfs_prop_user(propname)) {
4982676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4995094Slling 				    "invalid property '%s'"), propname);
5005094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5015094Slling 				goto error;
5025094Slling 			}
5035094Slling 
5045094Slling 			/*
5055094Slling 			 * If this is a user property, make sure it's a
5065094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
5075094Slling 			 */
5085094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
5095094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5105094Slling 				    "'%s' must be a string"), propname);
5115094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5125094Slling 				goto error;
5135094Slling 			}
5145094Slling 
5155094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
5165094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5175094Slling 				    "property name '%s' is too long"),
5182676Seschrock 				    propname);
5192676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5202676Seschrock 				goto error;
5212676Seschrock 			}
5222676Seschrock 
5232676Seschrock 			(void) nvpair_value_string(elem, &strval);
5242676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
5252676Seschrock 				(void) no_memory(hdl);
5262676Seschrock 				goto error;
5272676Seschrock 			}
5282676Seschrock 			continue;
529789Sahrens 		}
5302676Seschrock 
5312676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
5322676Seschrock 			zfs_error_aux(hdl,
5332676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
5342676Seschrock 			    "apply to datasets of this type"), propname);
5352676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5362676Seschrock 			goto error;
5372676Seschrock 		}
5382676Seschrock 
5392676Seschrock 		if (zfs_prop_readonly(prop) &&
5402676Seschrock 		    (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) {
5412676Seschrock 			zfs_error_aux(hdl,
5422676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
5432676Seschrock 			    propname);
5442676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
5452676Seschrock 			goto error;
5462676Seschrock 		}
5472676Seschrock 
5485094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
5495094Slling 		    &strval, &intval, errbuf) != 0)
5502676Seschrock 			goto error;
5512676Seschrock 
5522676Seschrock 		/*
5532676Seschrock 		 * Perform some additional checks for specific properties.
5542676Seschrock 		 */
5552676Seschrock 		switch (prop) {
5564577Sahrens 		case ZFS_PROP_VERSION:
5574577Sahrens 		{
5584577Sahrens 			int version;
5594577Sahrens 
5604577Sahrens 			if (zhp == NULL)
5614577Sahrens 				break;
5624577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
5634577Sahrens 			if (intval < version) {
5644577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5654577Sahrens 				    "Can not downgrade; already at version %u"),
5664577Sahrens 				    version);
5674577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5684577Sahrens 				goto error;
5694577Sahrens 			}
5704577Sahrens 			break;
5714577Sahrens 		}
5724577Sahrens 
5732676Seschrock 		case ZFS_PROP_RECORDSIZE:
5742676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
5752676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
5762676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
5772676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
5782082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5792676Seschrock 				    "'%s' must be power of 2 from %u "
5802676Seschrock 				    "to %uk"), propname,
5812676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
5822676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
5832676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5842676Seschrock 				goto error;
585789Sahrens 			}
586789Sahrens 			break;
587789Sahrens 
5883126Sahl 		case ZFS_PROP_SHAREISCSI:
5893126Sahl 			if (strcmp(strval, "off") != 0 &&
5903126Sahl 			    strcmp(strval, "on") != 0 &&
5913126Sahl 			    strcmp(strval, "type=disk") != 0) {
5923126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5933126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
5943126Sahl 				    propname);
5953126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5963126Sahl 				goto error;
5973126Sahl 			}
5983126Sahl 
5993126Sahl 			break;
6003126Sahl 
6012676Seschrock 		case ZFS_PROP_MOUNTPOINT:
6024778Srm160521 		{
6034778Srm160521 			namecheck_err_t why;
6044778Srm160521 
6052676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
6062676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
6072676Seschrock 				break;
6082676Seschrock 
6094778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
6104778Srm160521 				switch (why) {
6114778Srm160521 				case NAME_ERR_LEADING_SLASH:
6124778Srm160521 					zfs_error_aux(hdl,
6134778Srm160521 					    dgettext(TEXT_DOMAIN,
6144778Srm160521 					    "'%s' must be an absolute path, "
6154778Srm160521 					    "'none', or 'legacy'"), propname);
6164778Srm160521 					break;
6174778Srm160521 				case NAME_ERR_TOOLONG:
6184778Srm160521 					zfs_error_aux(hdl,
6194778Srm160521 					    dgettext(TEXT_DOMAIN,
6204778Srm160521 					    "component of '%s' is too long"),
6214778Srm160521 					    propname);
6224778Srm160521 					break;
6234778Srm160521 				}
6242676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6252676Seschrock 				goto error;
626789Sahrens 			}
6274778Srm160521 		}
6284778Srm160521 
6293126Sahl 			/*FALLTHRU*/
6303126Sahl 
6313126Sahl 		case ZFS_PROP_SHARENFS:
6323126Sahl 			/*
6333126Sahl 			 * For the mountpoint and sharenfs properties, check if
6343126Sahl 			 * it can be set in a global/non-global zone based on
6353126Sahl 			 * the zoned property value:
6363126Sahl 			 *
6373126Sahl 			 *		global zone	    non-global zone
6383126Sahl 			 * --------------------------------------------------
6393126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
6403126Sahl 			 *		sharenfs (no)	    sharenfs (no)
6413126Sahl 			 *
6423126Sahl 			 * zoned=off	mountpoint (yes)	N/A
6433126Sahl 			 *		sharenfs (yes)
6443126Sahl 			 */
6452676Seschrock 			if (zoned) {
6462676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
6472676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6482676Seschrock 					    "'%s' cannot be set on "
6492676Seschrock 					    "dataset in a non-global zone"),
6502676Seschrock 					    propname);
6512676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
6522676Seschrock 					    errbuf);
6532676Seschrock 					goto error;
6542676Seschrock 				} else if (prop == ZFS_PROP_SHARENFS) {
6552676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6562676Seschrock 					    "'%s' cannot be set in "
6572676Seschrock 					    "a non-global zone"), propname);
6582676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
6592676Seschrock 					    errbuf);
6602676Seschrock 					goto error;
6612676Seschrock 				}
6622676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
6632676Seschrock 				/*
6642676Seschrock 				 * If zoned property is 'off', this must be in
6652676Seschrock 				 * a globle zone. If not, something is wrong.
6662676Seschrock 				 */
6672676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6682676Seschrock 				    "'%s' cannot be set while dataset "
6692676Seschrock 				    "'zoned' property is set"), propname);
6702676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
6712676Seschrock 				goto error;
6722676Seschrock 			}
6733126Sahl 
6744180Sdougm 			/*
6754180Sdougm 			 * At this point, it is legitimate to set the
6764180Sdougm 			 * property. Now we want to make sure that the
6774180Sdougm 			 * property value is valid if it is sharenfs.
6784180Sdougm 			 */
6794180Sdougm 			if (prop == ZFS_PROP_SHARENFS &&
6804217Seschrock 			    strcmp(strval, "on") != 0 &&
6814217Seschrock 			    strcmp(strval, "off") != 0) {
6824180Sdougm 
6834180Sdougm 				/*
6844180Sdougm 				 * Must be an NFS option string so
6854180Sdougm 				 * init the libshare in order to
6864180Sdougm 				 * enable the parser and then parse
6874180Sdougm 				 * the options. We use the control API
6884180Sdougm 				 * since we don't care about the
6894180Sdougm 				 * current configuration and don't
6904180Sdougm 				 * want the overhead of loading it
6914180Sdougm 				 * until we actually do something.
6924180Sdougm 				 */
6934180Sdougm 
6944217Seschrock 				if (zfs_init_libshare(hdl,
6954217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
6964217Seschrock 					/*
6974217Seschrock 					 * An error occurred so we can't do
6984217Seschrock 					 * anything
6994217Seschrock 					 */
7004217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7014217Seschrock 					    "'%s' cannot be set: problem "
7024217Seschrock 					    "in share initialization"),
7034217Seschrock 					    propname);
7044217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7054217Seschrock 					    errbuf);
7064217Seschrock 					goto error;
7074217Seschrock 				}
7084217Seschrock 
7094217Seschrock 				if (zfs_parse_options(strval, "nfs") != SA_OK) {
7104217Seschrock 					/*
7114217Seschrock 					 * There was an error in parsing so
7124217Seschrock 					 * deal with it by issuing an error
7134217Seschrock 					 * message and leaving after
7144217Seschrock 					 * uninitializing the the libshare
7154217Seschrock 					 * interface.
7164217Seschrock 					 */
7174217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7184217Seschrock 					    "'%s' cannot be set to invalid "
7194217Seschrock 					    "options"), propname);
7204217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7214217Seschrock 					    errbuf);
7224217Seschrock 					zfs_uninit_libshare(hdl);
7234217Seschrock 					goto error;
7244217Seschrock 				}
7254180Sdougm 				zfs_uninit_libshare(hdl);
7264180Sdougm 			}
7274180Sdougm 
7283126Sahl 			break;
7292676Seschrock 		}
7302676Seschrock 
7312676Seschrock 		/*
7322676Seschrock 		 * For changes to existing volumes, we have some additional
7332676Seschrock 		 * checks to enforce.
7342676Seschrock 		 */
7352676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
7362676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
7372676Seschrock 			    ZFS_PROP_VOLSIZE);
7382676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
7392676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
7402676Seschrock 			char buf[64];
7412676Seschrock 
7422676Seschrock 			switch (prop) {
7432676Seschrock 			case ZFS_PROP_RESERVATION:
7442676Seschrock 				if (intval > volsize) {
7452676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7462676Seschrock 					    "'%s' is greater than current "
7472676Seschrock 					    "volume size"), propname);
7482676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7492676Seschrock 					    errbuf);
7502676Seschrock 					goto error;
7512676Seschrock 				}
7522676Seschrock 				break;
7532676Seschrock 
7542676Seschrock 			case ZFS_PROP_VOLSIZE:
7552676Seschrock 				if (intval % blocksize != 0) {
7562676Seschrock 					zfs_nicenum(blocksize, buf,
7572676Seschrock 					    sizeof (buf));
7582676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7592676Seschrock 					    "'%s' must be a multiple of "
7602676Seschrock 					    "volume block size (%s)"),
7612676Seschrock 					    propname, buf);
7622676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7632676Seschrock 					    errbuf);
7642676Seschrock 					goto error;
7652676Seschrock 				}
7662676Seschrock 
7672676Seschrock 				if (intval == 0) {
7682676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7692676Seschrock 					    "'%s' cannot be zero"),
7702676Seschrock 					    propname);
7712676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7722676Seschrock 					    errbuf);
7732676Seschrock 					goto error;
774789Sahrens 				}
7753126Sahl 				break;
776789Sahrens 			}
777789Sahrens 		}
778789Sahrens 	}
779789Sahrens 
7802676Seschrock 	/*
7812676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
7822676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
7832676Seschrock 	 */
7842676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
7852676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
7862676Seschrock 	    &intval) == 0) {
7872676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
7882676Seschrock 		    ZFS_PROP_VOLSIZE);
7892676Seschrock 		uint64_t old_reservation = zfs_prop_get_int(zhp,
7902676Seschrock 		    ZFS_PROP_RESERVATION);
7912676Seschrock 		uint64_t new_reservation;
7922676Seschrock 
7932676Seschrock 		if (old_volsize == old_reservation &&
7942676Seschrock 		    nvlist_lookup_uint64(ret,
7952676Seschrock 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
7962676Seschrock 		    &new_reservation) != 0) {
7972676Seschrock 			if (nvlist_add_uint64(ret,
7982676Seschrock 			    zfs_prop_to_name(ZFS_PROP_RESERVATION),
7992676Seschrock 			    intval) != 0) {
8002676Seschrock 				(void) no_memory(hdl);
8012676Seschrock 				goto error;
8022676Seschrock 			}
8032676Seschrock 		}
8042676Seschrock 	}
8052676Seschrock 
8062676Seschrock 	return (ret);
8072676Seschrock 
8082676Seschrock error:
8092676Seschrock 	nvlist_free(ret);
8102676Seschrock 	return (NULL);
811789Sahrens }
812789Sahrens 
8134543Smarks static int
8144543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
8154543Smarks     uint64_t *ret_who)
8164543Smarks {
8174543Smarks 	struct passwd *pwd;
8184543Smarks 	struct group *grp;
8194543Smarks 	uid_t id;
8204543Smarks 
8214543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
8224543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
8234543Smarks 		*ret_who = -1;
8244543Smarks 		return (0);
8254543Smarks 	}
8264543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
8274543Smarks 		return (EZFS_BADWHO);
8284543Smarks 
8294543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
8304543Smarks 	    strcmp(who, "everyone") == 0) {
8314543Smarks 		*ret_who = -1;
8324543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
8334543Smarks 		return (0);
8344543Smarks 	}
8354543Smarks 
8364543Smarks 	pwd = getpwnam(who);
8374543Smarks 	grp = getgrnam(who);
8384543Smarks 
8394543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
8404543Smarks 		*ret_who = pwd->pw_uid;
8414543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
8424543Smarks 		*ret_who = grp->gr_gid;
8434543Smarks 	} else if (pwd) {
8444543Smarks 		*ret_who = pwd->pw_uid;
8454543Smarks 		*who_type = ZFS_DELEG_USER;
8464543Smarks 	} else if (grp) {
8474543Smarks 		*ret_who = grp->gr_gid;
8484543Smarks 		*who_type = ZFS_DELEG_GROUP;
8494543Smarks 	} else {
8504543Smarks 		char *end;
8514543Smarks 
8524543Smarks 		id = strtol(who, &end, 10);
8534543Smarks 		if (errno != 0 || *end != '\0') {
8544543Smarks 			return (EZFS_BADWHO);
8554543Smarks 		} else {
8564543Smarks 			*ret_who = id;
8574543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
8584543Smarks 				*who_type = ZFS_DELEG_USER;
8594543Smarks 		}
8604543Smarks 	}
8614543Smarks 
8624543Smarks 	return (0);
8634543Smarks }
8644543Smarks 
8654543Smarks static void
8664543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
8674543Smarks {
8684543Smarks 	if (perms_nvp != NULL) {
8694543Smarks 		verify(nvlist_add_nvlist(who_nvp,
8704543Smarks 		    name, perms_nvp) == 0);
8714543Smarks 	} else {
8724543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
8734543Smarks 	}
8744543Smarks }
8754543Smarks 
8764543Smarks static void
8774543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
8784543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
8794543Smarks     nvlist_t *sets_nvp)
8804543Smarks {
8814543Smarks 	boolean_t do_perms, do_sets;
8824543Smarks 	char name[ZFS_MAX_DELEG_NAME];
8834543Smarks 
8844543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
8854543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
8864543Smarks 
8874543Smarks 	if (!do_perms && !do_sets)
8884543Smarks 		do_perms = do_sets = B_TRUE;
8894543Smarks 
8904543Smarks 	if (do_perms) {
8914543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
8924543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
8934543Smarks 		    whostr : (void *)&whoid);
8944543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
8954543Smarks 	}
8964543Smarks 	if (do_sets) {
8974543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
8984543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
8994543Smarks 		    whostr : (void *)&whoid);
9004543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
9014543Smarks 	}
9024543Smarks }
9034543Smarks 
9044543Smarks static void
9054543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
9064543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
9074543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
9084543Smarks {
9094543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
9104543Smarks 		helper(who_type, whoid, whostr, 0,
9114543Smarks 		    who_nvp, perms_nvp, sets_nvp);
9124543Smarks 	} else {
9134543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
9144543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
9154543Smarks 			    who_nvp, perms_nvp, sets_nvp);
9164543Smarks 		}
9174543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
9184543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
9194543Smarks 			    who_nvp, perms_nvp, sets_nvp);
9204543Smarks 		}
9214543Smarks 	}
9224543Smarks }
9234543Smarks 
9244543Smarks /*
9254543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
9264543Smarks  *
9274543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
9284543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
9294543Smarks  * base attribute named stored in the dsl.
9304543Smarks  * Arguments:
9314543Smarks  *
9324543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
9334543Smarks  *           whostr may be null for everyone or create perms.
9344543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
9354543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
9364543Smarks  * perms:    comman separated list of permissions.  May be null if user
9374543Smarks  *           is requested to remove permissions by who.
9384543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
9394543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
9404543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
9414543Smarks  *           The output nvp will look something like this.
9424543Smarks  *              ul$1234 -> {create ; destroy }
9434543Smarks  *              Ul$1234 -> { @myset }
9444543Smarks  *              s-$@myset - { snapshot; checksum; compression }
9454543Smarks  */
9464543Smarks int
9474543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
9484543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
9494543Smarks {
9504543Smarks 	nvlist_t *who_nvp;
9514543Smarks 	nvlist_t *perms_nvp = NULL;
9524543Smarks 	nvlist_t *sets_nvp = NULL;
9534543Smarks 	char errbuf[1024];
9544787Sahrens 	char *who_tok, *perm;
9554543Smarks 	int error;
9564543Smarks 
9574543Smarks 	*nvp = NULL;
9584543Smarks 
9594543Smarks 	if (perms) {
9604543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
9614543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
9624543Smarks 			return (1);
9634543Smarks 		}
9644543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
9654543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
9664543Smarks 			nvlist_free(perms_nvp);
9674543Smarks 			return (1);
9684543Smarks 		}
9694543Smarks 	}
9704543Smarks 
9714543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
9724543Smarks 		if (perms_nvp)
9734543Smarks 			nvlist_free(perms_nvp);
9744543Smarks 		if (sets_nvp)
9754543Smarks 			nvlist_free(sets_nvp);
9764543Smarks 		return (1);
9774543Smarks 	}
9784543Smarks 
9794543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
9804543Smarks 		namecheck_err_t why;
9814543Smarks 		char what;
9824543Smarks 
9834543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
9844787Sahrens 			nvlist_free(who_nvp);
9854787Sahrens 			if (perms_nvp)
9864787Sahrens 				nvlist_free(perms_nvp);
9874787Sahrens 			if (sets_nvp)
9884787Sahrens 				nvlist_free(sets_nvp);
9894787Sahrens 
9904543Smarks 			switch (why) {
9914543Smarks 			case NAME_ERR_NO_AT:
9924543Smarks 				zfs_error_aux(zhp->zfs_hdl,
9934543Smarks 				    dgettext(TEXT_DOMAIN,
9944543Smarks 				    "set definition must begin with an '@' "
9954543Smarks 				    "character"));
9964543Smarks 			}
9974543Smarks 			return (zfs_error(zhp->zfs_hdl,
9984543Smarks 			    EZFS_BADPERMSET, whostr));
9994543Smarks 		}
10004543Smarks 	}
10014543Smarks 
10024543Smarks 	/*
10034543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
10044543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
10054543Smarks 	 * other sets_nvp will have only permssion set names in it.
10064543Smarks 	 */
10074787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
10084787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
10094787Sahrens 
10104787Sahrens 		if (perm_canonical) {
10114787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
10124787Sahrens 			    perm_canonical) == 0);
10134787Sahrens 		} else if (perm[0] == '@') {
10144787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
10154787Sahrens 		} else {
10164787Sahrens 			nvlist_free(who_nvp);
10174787Sahrens 			nvlist_free(perms_nvp);
10184787Sahrens 			nvlist_free(sets_nvp);
10194787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
10204543Smarks 		}
10214543Smarks 	}
10224543Smarks 
10234543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
10244543Smarks 		who_tok = strtok(whostr, ",");
10254543Smarks 		if (who_tok == NULL) {
10264543Smarks 			nvlist_free(who_nvp);
10274787Sahrens 			if (perms_nvp)
10284787Sahrens 				nvlist_free(perms_nvp);
10294543Smarks 			if (sets_nvp)
10304543Smarks 				nvlist_free(sets_nvp);
10314543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
10324543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
10334543Smarks 			    whostr);
10344543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
10354543Smarks 		}
10364543Smarks 	}
10374543Smarks 
10384543Smarks 	/*
10394543Smarks 	 * Now create the nvlist(s)
10404543Smarks 	 */
10414543Smarks 	do {
10424543Smarks 		uint64_t who_id;
10434543Smarks 
10444543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
10454543Smarks 		    &who_id);
10464543Smarks 		if (error) {
10474543Smarks 			nvlist_free(who_nvp);
10484787Sahrens 			if (perms_nvp)
10494787Sahrens 				nvlist_free(perms_nvp);
10504543Smarks 			if (sets_nvp)
10514543Smarks 				nvlist_free(sets_nvp);
10524543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
10534543Smarks 			    dgettext(TEXT_DOMAIN,
10544543Smarks 			    "Unable to determine uid/gid for "
10554543Smarks 			    "%s "), who_tok);
10564543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
10574543Smarks 		}
10584543Smarks 
10594543Smarks 		/*
10604543Smarks 		 * add entries for both local and descendent when required
10614543Smarks 		 */
10624543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
10634543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
10644543Smarks 
10654543Smarks 	} while (who_tok = strtok(NULL, ","));
10664543Smarks 	*nvp = who_nvp;
10674543Smarks 	return (0);
10684543Smarks }
10694543Smarks 
10704543Smarks static int
10714543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
10724543Smarks {
10734543Smarks 	zfs_cmd_t zc = { 0 };
10744543Smarks 	int error;
10754543Smarks 	char errbuf[1024];
10764543Smarks 
10774543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
10784543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
10794543Smarks 	    zhp->zfs_name);
10804543Smarks 
10815094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
10824543Smarks 		return (-1);
10834543Smarks 
10844543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
10854543Smarks 	zc.zc_perm_action = unset;
10864543Smarks 
10874543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
10884543Smarks 	if (error && errno == ENOTSUP) {
10894543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
10904543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
10914543Smarks 		zcmd_free_nvlists(&zc);
10924543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
10934543Smarks 	} else if (error) {
10944543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
10954543Smarks 	}
10964543Smarks 	zcmd_free_nvlists(&zc);
10974543Smarks 
10984543Smarks 	return (error);
10994543Smarks }
11004543Smarks 
11014543Smarks int
11024543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
11034543Smarks {
11044543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
11054543Smarks }
11064543Smarks 
11074543Smarks int
11084543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
11094543Smarks {
11104543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
11114543Smarks }
11124543Smarks 
11134543Smarks static int
11144543Smarks perm_compare(const void *arg1, const void *arg2)
11154543Smarks {
11164543Smarks 	const zfs_perm_node_t *node1 = arg1;
11174543Smarks 	const zfs_perm_node_t *node2 = arg2;
11184543Smarks 	int ret;
11194543Smarks 
11204543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
11214543Smarks 
11224543Smarks 	if (ret > 0)
11234543Smarks 		return (1);
11244543Smarks 	if (ret < 0)
11254543Smarks 		return (-1);
11264543Smarks 	else
11274543Smarks 		return (0);
11284543Smarks }
11294543Smarks 
11304543Smarks static void
11314543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
11324543Smarks {
11334543Smarks 	zfs_perm_node_t *permnode;
11344543Smarks 	void *cookie;
11354543Smarks 
11364543Smarks 	cookie = NULL;
11374543Smarks 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL) {
11384543Smarks 		avl_remove(tree, permnode);
11394543Smarks 		free(permnode);
11404543Smarks 	}
11414543Smarks }
11424543Smarks 
11434543Smarks static void
11444543Smarks zfs_destroy_tree(avl_tree_t *tree)
11454543Smarks {
11464543Smarks 	zfs_allow_node_t *allownode;
11474543Smarks 	void *cookie;
11484543Smarks 
11494543Smarks 	cookie = NULL;
11504543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
11514543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
11524543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
11534543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
11544543Smarks 		avl_remove(tree, allownode);
11554543Smarks 		free(allownode);
11564543Smarks 	}
11574543Smarks }
11584543Smarks 
11594543Smarks void
11604543Smarks zfs_free_allows(zfs_allow_t *allow)
11614543Smarks {
11624543Smarks 	zfs_allow_t *allownext;
11634543Smarks 	zfs_allow_t *freeallow;
11644543Smarks 
11654543Smarks 	allownext = allow;
11664543Smarks 	while (allownext) {
11674543Smarks 		zfs_destroy_tree(&allownext->z_sets);
11684543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
11694543Smarks 		zfs_destroy_tree(&allownext->z_user);
11704543Smarks 		zfs_destroy_tree(&allownext->z_group);
11714543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
11724543Smarks 		freeallow = allownext;
11734543Smarks 		allownext = allownext->z_next;
11744543Smarks 		free(freeallow);
11754543Smarks 	}
11764543Smarks }
11774543Smarks 
11784543Smarks static zfs_allow_t *
11794543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
11804543Smarks {
11814543Smarks 	zfs_allow_t *ptree;
11824543Smarks 
11834543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
11844543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
11854543Smarks 		return (NULL);
11864543Smarks 	}
11874543Smarks 
11884543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
11894543Smarks 	avl_create(&ptree->z_sets,
11904543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
11914543Smarks 	    offsetof(zfs_allow_node_t, z_node));
11924543Smarks 	avl_create(&ptree->z_crperms,
11934543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
11944543Smarks 	    offsetof(zfs_allow_node_t, z_node));
11954543Smarks 	avl_create(&ptree->z_user,
11964543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
11974543Smarks 	    offsetof(zfs_allow_node_t, z_node));
11984543Smarks 	avl_create(&ptree->z_group,
11994543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12004543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12014543Smarks 	avl_create(&ptree->z_everyone,
12024543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12034543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12044543Smarks 
12054543Smarks 	if (prev)
12064543Smarks 		prev->z_next = ptree;
12074543Smarks 	ptree->z_next = NULL;
12084543Smarks 	return (ptree);
12094543Smarks }
12104543Smarks 
12114543Smarks /*
12124543Smarks  * Add permissions to the appropriate AVL permission tree.
12134543Smarks  * The appropriate tree may not be the requested tree.
12144543Smarks  * For example if ld indicates a local permission, but
12154543Smarks  * same permission also exists as a descendent permission
12164543Smarks  * then the permission will be removed from the descendent
12174543Smarks  * tree and add the the local+descendent tree.
12184543Smarks  */
12194543Smarks static int
12204543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
12214543Smarks     char *perm, char ld)
12224543Smarks {
12234543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
12244543Smarks 	zfs_perm_node_t *newnode;
12254543Smarks 	avl_index_t where, where2;
12264543Smarks 	avl_tree_t *tree, *altree;
12274543Smarks 
12284543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
12294543Smarks 
12304543Smarks 	if (ld == ZFS_DELEG_NA) {
12314543Smarks 		tree =  &allownode->z_localdescend;
12324543Smarks 		altree = &allownode->z_descend;
12334543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
12344543Smarks 		tree = &allownode->z_local;
12354543Smarks 		altree = &allownode->z_descend;
12364543Smarks 	} else {
12374543Smarks 		tree = &allownode->z_descend;
12384543Smarks 		altree = &allownode->z_local;
12394543Smarks 	}
12404543Smarks 	permnode = avl_find(tree, &pnode, &where);
12414543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
12424543Smarks 
12434543Smarks 	if (permnode2) {
12444543Smarks 		avl_remove(altree, permnode2);
12454543Smarks 		free(permnode2);
12464543Smarks 		if (permnode == NULL) {
12474543Smarks 			tree =  &allownode->z_localdescend;
12484543Smarks 		}
12494543Smarks 	}
12504543Smarks 
12514543Smarks 	/*
12524543Smarks 	 * Now insert new permission in either requested location
12534543Smarks 	 * local/descendent or into ld when perm will exist in both.
12544543Smarks 	 */
12554543Smarks 	if (permnode == NULL) {
12564543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
12574543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
12584543Smarks 			return (-1);
12594543Smarks 		}
12604543Smarks 		*newnode = pnode;
12614543Smarks 		avl_add(tree, newnode);
12624543Smarks 	}
12634543Smarks 	return (0);
12644543Smarks }
12654577Sahrens 
12664543Smarks /*
12674543Smarks  * Uggh, this is going to be a bit complicated.
12684543Smarks  * we have an nvlist coming out of the kernel that
12694543Smarks  * will indicate where the permission is set and then
12704543Smarks  * it will contain allow of the various "who's", and what
12714543Smarks  * their permissions are.  To further complicate this
12724543Smarks  * we will then have to coalesce the local,descendent
12734543Smarks  * and local+descendent permissions where appropriate.
12744543Smarks  * The kernel only knows about a permission as being local
12754543Smarks  * or descendent, but not both.
12764543Smarks  *
12774543Smarks  * In order to make this easier for zfs_main to deal with
12784543Smarks  * a series of AVL trees will be used to maintain
12794543Smarks  * all of this, primarily for sorting purposes as well
12804543Smarks  * as the ability to quickly locate a specific entry.
12814543Smarks  *
12824543Smarks  * What we end up with are tree's for sets, create perms,
12834543Smarks  * user, groups and everyone.  With each of those trees
12844543Smarks  * we have subtrees for local, descendent and local+descendent
12854543Smarks  * permissions.
12864543Smarks  */
12874543Smarks int
12884543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
12894543Smarks {
12904543Smarks 	zfs_cmd_t zc = { 0 };
12914543Smarks 	int error;
12924543Smarks 	nvlist_t *nvlist;
12934543Smarks 	nvlist_t *permnv, *sourcenv;
12944543Smarks 	nvpair_t *who_pair, *source_pair;
12954543Smarks 	nvpair_t *perm_pair;
12964543Smarks 	char errbuf[1024];
12974543Smarks 	zfs_allow_t *zallowp, *newallowp;
12984543Smarks 	char  ld;
12994543Smarks 	char *nvpname;
13004543Smarks 	uid_t	uid;
13014543Smarks 	gid_t	gid;
13024543Smarks 	avl_tree_t *tree;
13034543Smarks 	avl_index_t where;
13044543Smarks 
13054543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13064543Smarks 
13074543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
13084543Smarks 		return (-1);
13094543Smarks 
13104543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
13114543Smarks 		if (errno == ENOMEM) {
13124543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
13134543Smarks 				zcmd_free_nvlists(&zc);
13144543Smarks 				return (-1);
13154543Smarks 			}
13164543Smarks 		} else if (errno == ENOTSUP) {
13174543Smarks 			zcmd_free_nvlists(&zc);
13184543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
13194543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
13204543Smarks 			return (zfs_error(zhp->zfs_hdl,
13214543Smarks 			    EZFS_BADVERSION, errbuf));
13224543Smarks 		} else {
13234543Smarks 			zcmd_free_nvlists(&zc);
13244543Smarks 			return (-1);
13254543Smarks 		}
13264543Smarks 	}
13274543Smarks 
13284543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
13294543Smarks 		zcmd_free_nvlists(&zc);
13304543Smarks 		return (-1);
13314543Smarks 	}
13324543Smarks 
13334543Smarks 	zcmd_free_nvlists(&zc);
13344543Smarks 
13354543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
13364543Smarks 
13374543Smarks 	if (source_pair == NULL) {
13384543Smarks 		*zfs_perms = NULL;
13394543Smarks 		return (0);
13404543Smarks 	}
13414543Smarks 
13424543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
13434543Smarks 	if (*zfs_perms == NULL) {
13444543Smarks 		return (0);
13454543Smarks 	}
13464543Smarks 
13474543Smarks 	zallowp = *zfs_perms;
13484543Smarks 
13494543Smarks 	for (;;) {
13504543Smarks 		struct passwd *pwd;
13514543Smarks 		struct group *grp;
13524543Smarks 		zfs_allow_node_t *allownode;
13534543Smarks 		zfs_allow_node_t  findallownode;
13544543Smarks 		zfs_allow_node_t *newallownode;
13554543Smarks 
13564543Smarks 		(void) strlcpy(zallowp->z_setpoint,
13574543Smarks 		    nvpair_name(source_pair),
13584543Smarks 		    sizeof (zallowp->z_setpoint));
13594543Smarks 
13604543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
13614543Smarks 			goto abort;
13624543Smarks 
13634543Smarks 		/*
13644543Smarks 		 * Make sure nvlist is composed correctly
13654543Smarks 		 */
13664543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
13674543Smarks 			goto abort;
13684543Smarks 		}
13694543Smarks 
13704543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
13714543Smarks 		if (who_pair == NULL) {
13724543Smarks 			goto abort;
13734543Smarks 		}
13744543Smarks 
13754543Smarks 		do {
13764543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
13774543Smarks 			if (error) {
13784543Smarks 				goto abort;
13794543Smarks 			}
13804543Smarks 
13814543Smarks 			/*
13824543Smarks 			 * First build up the key to use
13834543Smarks 			 * for looking up in the various
13844543Smarks 			 * who trees.
13854543Smarks 			 */
13864543Smarks 			ld = nvpair_name(who_pair)[1];
13874543Smarks 			nvpname = nvpair_name(who_pair);
13884543Smarks 			switch (nvpair_name(who_pair)[0]) {
13894543Smarks 			case ZFS_DELEG_USER:
13904543Smarks 			case ZFS_DELEG_USER_SETS:
13914543Smarks 				tree = &zallowp->z_user;
13924543Smarks 				uid = atol(&nvpname[3]);
13934543Smarks 				pwd = getpwuid(uid);
13944543Smarks 				(void) snprintf(findallownode.z_key,
13954543Smarks 				    sizeof (findallownode.z_key), "user %s",
13964543Smarks 				    (pwd) ? pwd->pw_name :
13974543Smarks 				    &nvpair_name(who_pair)[3]);
13984543Smarks 				break;
13994543Smarks 			case ZFS_DELEG_GROUP:
14004543Smarks 			case ZFS_DELEG_GROUP_SETS:
14014543Smarks 				tree = &zallowp->z_group;
14024543Smarks 				gid = atol(&nvpname[3]);
14034543Smarks 				grp = getgrgid(gid);
14044543Smarks 				(void) snprintf(findallownode.z_key,
14054543Smarks 				    sizeof (findallownode.z_key), "group %s",
14064543Smarks 				    (grp) ? grp->gr_name :
14074543Smarks 				    &nvpair_name(who_pair)[3]);
14084543Smarks 				break;
14094543Smarks 			case ZFS_DELEG_CREATE:
14104543Smarks 			case ZFS_DELEG_CREATE_SETS:
14114543Smarks 				tree = &zallowp->z_crperms;
14124543Smarks 				(void) strlcpy(findallownode.z_key, "",
14134543Smarks 				    sizeof (findallownode.z_key));
14144543Smarks 				break;
14154543Smarks 			case ZFS_DELEG_EVERYONE:
14164543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
14174543Smarks 				(void) snprintf(findallownode.z_key,
14184543Smarks 				    sizeof (findallownode.z_key), "everyone");
14194543Smarks 				tree = &zallowp->z_everyone;
14204543Smarks 				break;
14214543Smarks 			case ZFS_DELEG_NAMED_SET:
14224543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
14234543Smarks 				(void) snprintf(findallownode.z_key,
14244543Smarks 				    sizeof (findallownode.z_key), "%s",
14254543Smarks 				    &nvpair_name(who_pair)[3]);
14264543Smarks 				tree = &zallowp->z_sets;
14274543Smarks 				break;
14284543Smarks 			}
14294543Smarks 
14304543Smarks 			/*
14314543Smarks 			 * Place who in tree
14324543Smarks 			 */
14334543Smarks 			allownode = avl_find(tree, &findallownode, &where);
14344543Smarks 			if (allownode == NULL) {
14354543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
14364543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
14374543Smarks 					goto abort;
14384543Smarks 				}
14394543Smarks 				avl_create(&newallownode->z_localdescend,
14404543Smarks 				    perm_compare,
14414543Smarks 				    sizeof (zfs_perm_node_t),
14424543Smarks 				    offsetof(zfs_perm_node_t, z_node));
14434543Smarks 				avl_create(&newallownode->z_local,
14444543Smarks 				    perm_compare,
14454543Smarks 				    sizeof (zfs_perm_node_t),
14464543Smarks 				    offsetof(zfs_perm_node_t, z_node));
14474543Smarks 				avl_create(&newallownode->z_descend,
14484543Smarks 				    perm_compare,
14494543Smarks 				    sizeof (zfs_perm_node_t),
14504543Smarks 				    offsetof(zfs_perm_node_t, z_node));
14514543Smarks 				(void) strlcpy(newallownode->z_key,
14524543Smarks 				    findallownode.z_key,
14534543Smarks 				    sizeof (findallownode.z_key));
14544543Smarks 				avl_insert(tree, newallownode, where);
14554543Smarks 				allownode = newallownode;
14564543Smarks 			}
14574543Smarks 
14584543Smarks 			/*
14594543Smarks 			 * Now iterate over the permissions and
14604543Smarks 			 * place them in the appropriate local,
14614543Smarks 			 * descendent or local+descendent tree.
14624543Smarks 			 *
14634543Smarks 			 * The permissions are added to the tree
14644543Smarks 			 * via zfs_coalesce_perm().
14654543Smarks 			 */
14664543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
14674543Smarks 			if (perm_pair == NULL)
14684543Smarks 				goto abort;
14694543Smarks 			do {
14704543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
14714543Smarks 				    nvpair_name(perm_pair), ld) != 0)
14724543Smarks 					goto abort;
14734543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
14744543Smarks 			    perm_pair));
14754543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
14764543Smarks 
14774543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
14784543Smarks 		if (source_pair == NULL)
14794543Smarks 			break;
14804543Smarks 
14814543Smarks 		/*
14824543Smarks 		 * allocate another node from the link list of
14834543Smarks 		 * zfs_allow_t structures
14844543Smarks 		 */
14854543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
14864543Smarks 		    nvpair_name(source_pair));
14874543Smarks 		if (newallowp == NULL) {
14884543Smarks 			goto abort;
14894543Smarks 		}
14904543Smarks 		zallowp = newallowp;
14914543Smarks 	}
14924543Smarks 	nvlist_free(nvlist);
14934543Smarks 	return (0);
14944543Smarks abort:
14954543Smarks 	zfs_free_allows(*zfs_perms);
14964543Smarks 	nvlist_free(nvlist);
14974543Smarks 	return (-1);
14984543Smarks }
14994543Smarks 
1500789Sahrens /*
1501789Sahrens  * Given a property name and value, set the property for the given dataset.
1502789Sahrens  */
1503789Sahrens int
15042676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1505789Sahrens {
1506789Sahrens 	zfs_cmd_t zc = { 0 };
15072676Seschrock 	int ret = -1;
15082676Seschrock 	prop_changelist_t *cl = NULL;
15092082Seschrock 	char errbuf[1024];
15102082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
15112676Seschrock 	nvlist_t *nvl = NULL, *realprops;
15122676Seschrock 	zfs_prop_t prop;
15132082Seschrock 
15142082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
15152676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
15162082Seschrock 	    zhp->zfs_name);
15172082Seschrock 
15182676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
15192676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
15202676Seschrock 		(void) no_memory(hdl);
15212676Seschrock 		goto error;
1522789Sahrens 	}
1523789Sahrens 
15245094Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
15252676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
15262676Seschrock 		goto error;
15275094Slling 
15282676Seschrock 	nvlist_free(nvl);
15292676Seschrock 	nvl = realprops;
15302676Seschrock 
15312676Seschrock 	prop = zfs_name_to_prop(propname);
15322676Seschrock 
1533789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
15342676Seschrock 		goto error;
1535789Sahrens 
1536789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
15372082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15382082Seschrock 		    "child dataset with inherited mountpoint is used "
15392082Seschrock 		    "in a non-global zone"));
15402082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1541789Sahrens 		goto error;
1542789Sahrens 	}
1543789Sahrens 
1544789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1545789Sahrens 		goto error;
1546789Sahrens 
1547789Sahrens 	/*
1548789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1549789Sahrens 	 */
1550789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1551789Sahrens 
15525094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
15532676Seschrock 		goto error;
15542676Seschrock 
15554543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1556789Sahrens 
1557789Sahrens 	if (ret != 0) {
1558789Sahrens 		switch (errno) {
1559789Sahrens 
1560789Sahrens 		case ENOSPC:
1561789Sahrens 			/*
1562789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1563789Sahrens 			 * something different; setting a quota or reservation
1564789Sahrens 			 * doesn't use any disk space.
1565789Sahrens 			 */
1566789Sahrens 			switch (prop) {
1567789Sahrens 			case ZFS_PROP_QUOTA:
15682082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15692082Seschrock 				    "size is less than current used or "
15702082Seschrock 				    "reserved space"));
15712082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1572789Sahrens 				break;
1573789Sahrens 
1574789Sahrens 			case ZFS_PROP_RESERVATION:
15752082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15762082Seschrock 				    "size is greater than available space"));
15772082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1578789Sahrens 				break;
1579789Sahrens 
1580789Sahrens 			default:
15812082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1582789Sahrens 				break;
1583789Sahrens 			}
1584789Sahrens 			break;
1585789Sahrens 
1586789Sahrens 		case EBUSY:
15872082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
15882082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
15892082Seschrock 			else
15902676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1591789Sahrens 			break;
1592789Sahrens 
15931175Slling 		case EROFS:
15942082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
15951175Slling 			break;
15961175Slling 
15973886Sahl 		case ENOTSUP:
15983886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15994603Sahrens 			    "pool must be upgraded to set this "
16004603Sahrens 			    "property or value"));
16013886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
16023886Sahl 			break;
16033886Sahl 
1604789Sahrens 		case EOVERFLOW:
1605789Sahrens 			/*
1606789Sahrens 			 * This platform can't address a volume this big.
1607789Sahrens 			 */
1608789Sahrens #ifdef _ILP32
1609789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
16102082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1611789Sahrens 				break;
1612789Sahrens 			}
1613789Sahrens #endif
16142082Seschrock 			/* FALLTHROUGH */
1615789Sahrens 		default:
16162082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1617789Sahrens 		}
1618789Sahrens 	} else {
1619789Sahrens 		/*
1620789Sahrens 		 * Refresh the statistics so the new property value
1621789Sahrens 		 * is reflected.
1622789Sahrens 		 */
16232676Seschrock 		if ((ret = changelist_postfix(cl)) == 0)
16242676Seschrock 			(void) get_stats(zhp);
1625789Sahrens 	}
1626789Sahrens 
1627789Sahrens error:
16282676Seschrock 	nvlist_free(nvl);
16292676Seschrock 	zcmd_free_nvlists(&zc);
16302676Seschrock 	if (cl)
16312676Seschrock 		changelist_free(cl);
1632789Sahrens 	return (ret);
1633789Sahrens }
1634789Sahrens 
1635789Sahrens /*
1636789Sahrens  * Given a property, inherit the value from the parent dataset.
1637789Sahrens  */
1638789Sahrens int
16392676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1640789Sahrens {
1641789Sahrens 	zfs_cmd_t zc = { 0 };
1642789Sahrens 	int ret;
1643789Sahrens 	prop_changelist_t *cl;
16442082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
16452082Seschrock 	char errbuf[1024];
16462676Seschrock 	zfs_prop_t prop;
16472082Seschrock 
16482082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
16492082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1650789Sahrens 
16515094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
16522676Seschrock 		/*
16532676Seschrock 		 * For user properties, the amount of work we have to do is very
16542676Seschrock 		 * small, so just do it here.
16552676Seschrock 		 */
16562676Seschrock 		if (!zfs_prop_user(propname)) {
16572676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16582676Seschrock 			    "invalid property"));
16592676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
16602676Seschrock 		}
16612676Seschrock 
16622676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16632676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
16642676Seschrock 
16654849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
16662676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
16672676Seschrock 
16682676Seschrock 		return (0);
16692676Seschrock 	}
16702676Seschrock 
1671789Sahrens 	/*
1672789Sahrens 	 * Verify that this property is inheritable.
1673789Sahrens 	 */
16742082Seschrock 	if (zfs_prop_readonly(prop))
16752082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
16762082Seschrock 
16772082Seschrock 	if (!zfs_prop_inheritable(prop))
16782082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1679789Sahrens 
1680789Sahrens 	/*
1681789Sahrens 	 * Check to see if the value applies to this type
1682789Sahrens 	 */
16832082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
16842082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1685789Sahrens 
16863443Srm160521 	/*
16873443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
16883443Srm160521 	 */
16893443Srm160521 	propname = zfs_prop_to_name(prop);
1690789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16912676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1692789Sahrens 
1693789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1694789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
16952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16962082Seschrock 		    "dataset is used in a non-global zone"));
16972082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1698789Sahrens 	}
1699789Sahrens 
1700789Sahrens 	/*
1701789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1702789Sahrens 	 */
1703789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1704789Sahrens 		return (-1);
1705789Sahrens 
1706789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17072082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17082082Seschrock 		    "child dataset with inherited mountpoint is used "
17092082Seschrock 		    "in a non-global zone"));
17102082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1711789Sahrens 		goto error;
1712789Sahrens 	}
1713789Sahrens 
1714789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1715789Sahrens 		goto error;
1716789Sahrens 
17174849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
17182082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1719789Sahrens 	} else {
1720789Sahrens 
17212169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1722789Sahrens 			goto error;
1723789Sahrens 
1724789Sahrens 		/*
1725789Sahrens 		 * Refresh the statistics so the new property is reflected.
1726789Sahrens 		 */
1727789Sahrens 		(void) get_stats(zhp);
1728789Sahrens 	}
1729789Sahrens 
1730789Sahrens error:
1731789Sahrens 	changelist_free(cl);
1732789Sahrens 	return (ret);
1733789Sahrens }
1734789Sahrens 
1735789Sahrens /*
17361356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
17371356Seschrock  * extract them appropriately.
17381356Seschrock  */
17391356Seschrock static uint64_t
17401356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
17411356Seschrock {
17421356Seschrock 	nvlist_t *nv;
17431356Seschrock 	uint64_t value;
17441356Seschrock 
17452885Sahrens 	*source = NULL;
17461356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
17471356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
17485094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
17495094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
17501356Seschrock 	} else {
17511356Seschrock 		value = zfs_prop_default_numeric(prop);
17521356Seschrock 		*source = "";
17531356Seschrock 	}
17541356Seschrock 
17551356Seschrock 	return (value);
17561356Seschrock }
17571356Seschrock 
17581356Seschrock static char *
17591356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
17601356Seschrock {
17611356Seschrock 	nvlist_t *nv;
17621356Seschrock 	char *value;
17631356Seschrock 
17642885Sahrens 	*source = NULL;
17651356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
17661356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
17675094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
17685094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
17691356Seschrock 	} else {
17701356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
17711356Seschrock 			value = "";
17721356Seschrock 		*source = "";
17731356Seschrock 	}
17741356Seschrock 
17751356Seschrock 	return (value);
17761356Seschrock }
17771356Seschrock 
17781356Seschrock /*
1779789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1780789Sahrens  * zfs_prop_get_int() are built using this interface.
1781789Sahrens  *
1782789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1783789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1784789Sahrens  * If they differ from the on-disk values, report the current values and mark
1785789Sahrens  * the source "temporary".
1786789Sahrens  */
17872082Seschrock static int
17885094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
17892082Seschrock     char **source, uint64_t *val)
1790789Sahrens {
1791*5147Srm160521 	zfs_cmd_t zc = { 0 };
1792789Sahrens 	struct mnttab mnt;
17933265Sahrens 	char *mntopt_on = NULL;
17943265Sahrens 	char *mntopt_off = NULL;
1795789Sahrens 
1796789Sahrens 	*source = NULL;
1797789Sahrens 
17983265Sahrens 	switch (prop) {
17993265Sahrens 	case ZFS_PROP_ATIME:
18003265Sahrens 		mntopt_on = MNTOPT_ATIME;
18013265Sahrens 		mntopt_off = MNTOPT_NOATIME;
18023265Sahrens 		break;
18033265Sahrens 
18043265Sahrens 	case ZFS_PROP_DEVICES:
18053265Sahrens 		mntopt_on = MNTOPT_DEVICES;
18063265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
18073265Sahrens 		break;
18083265Sahrens 
18093265Sahrens 	case ZFS_PROP_EXEC:
18103265Sahrens 		mntopt_on = MNTOPT_EXEC;
18113265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
18123265Sahrens 		break;
18133265Sahrens 
18143265Sahrens 	case ZFS_PROP_READONLY:
18153265Sahrens 		mntopt_on = MNTOPT_RO;
18163265Sahrens 		mntopt_off = MNTOPT_RW;
18173265Sahrens 		break;
18183265Sahrens 
18193265Sahrens 	case ZFS_PROP_SETUID:
18203265Sahrens 		mntopt_on = MNTOPT_SETUID;
18213265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
18223265Sahrens 		break;
18233265Sahrens 
18243265Sahrens 	case ZFS_PROP_XATTR:
18253265Sahrens 		mntopt_on = MNTOPT_XATTR;
18263265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
18273265Sahrens 		break;
18283265Sahrens 	}
18293265Sahrens 
18302474Seschrock 	/*
18312474Seschrock 	 * Because looking up the mount options is potentially expensive
18322474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
18332474Seschrock 	 * we're looking up a property which requires its presence.
18342474Seschrock 	 */
18352474Seschrock 	if (!zhp->zfs_mntcheck &&
18363265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
18373265Sahrens 		struct mnttab entry, search = { 0 };
18383265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
18392474Seschrock 
18402474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
18412474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
18423265Sahrens 		rewind(mnttab);
18433265Sahrens 
18443265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
18453265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
18463265Sahrens 			    entry.mnt_mntopts);
18473265Sahrens 			if (zhp->zfs_mntopts == NULL)
18483265Sahrens 				return (-1);
18493265Sahrens 		}
18502474Seschrock 
18512474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
18522474Seschrock 	}
18532474Seschrock 
1854789Sahrens 	if (zhp->zfs_mntopts == NULL)
1855789Sahrens 		mnt.mnt_mntopts = "";
1856789Sahrens 	else
1857789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1858789Sahrens 
1859789Sahrens 	switch (prop) {
1860789Sahrens 	case ZFS_PROP_ATIME:
18613265Sahrens 	case ZFS_PROP_DEVICES:
18623265Sahrens 	case ZFS_PROP_EXEC:
18633265Sahrens 	case ZFS_PROP_READONLY:
18643265Sahrens 	case ZFS_PROP_SETUID:
18653265Sahrens 	case ZFS_PROP_XATTR:
18662082Seschrock 		*val = getprop_uint64(zhp, prop, source);
18672082Seschrock 
18683265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
18692082Seschrock 			*val = B_TRUE;
1870789Sahrens 			if (src)
18715094Slling 				*src = ZPROP_SRC_TEMPORARY;
18723265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
18732082Seschrock 			*val = B_FALSE;
1874789Sahrens 			if (src)
18755094Slling 				*src = ZPROP_SRC_TEMPORARY;
1876789Sahrens 		}
18772082Seschrock 		break;
1878789Sahrens 
18793265Sahrens 	case ZFS_PROP_CANMOUNT:
18802082Seschrock 		*val = getprop_uint64(zhp, prop, source);
18813417Srm160521 		if (*val == 0)
18823417Srm160521 			*source = zhp->zfs_name;
18833417Srm160521 		else
18843417Srm160521 			*source = "";	/* default */
18852082Seschrock 		break;
1886789Sahrens 
1887789Sahrens 	case ZFS_PROP_QUOTA:
1888789Sahrens 	case ZFS_PROP_RESERVATION:
18892885Sahrens 		*val = getprop_uint64(zhp, prop, source);
18902885Sahrens 		if (*val == 0)
1891789Sahrens 			*source = "";	/* default */
1892789Sahrens 		else
1893789Sahrens 			*source = zhp->zfs_name;
18942082Seschrock 		break;
1895789Sahrens 
1896789Sahrens 	case ZFS_PROP_MOUNTED:
18972082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
18982082Seschrock 		break;
1899789Sahrens 
19003377Seschrock 	case ZFS_PROP_NUMCLONES:
19013377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
19023377Seschrock 		break;
19033377Seschrock 
1904*5147Srm160521 	case ZFS_PROP_VERSION:
1905*5147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1906*5147Srm160521 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) ||
1907*5147Srm160521 		    (zc.zc_cookie == 0)) {
1908*5147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1909*5147Srm160521 			    "unable to get version property"));
1910*5147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
1911*5147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
1912*5147Srm160521 		}
1913*5147Srm160521 		*val = zc.zc_cookie;
1914*5147Srm160521 		break;
1915*5147Srm160521 
1916789Sahrens 	default:
19174577Sahrens 		switch (zfs_prop_get_type(prop)) {
19184787Sahrens 		case PROP_TYPE_NUMBER:
19194787Sahrens 		case PROP_TYPE_INDEX:
19204577Sahrens 			*val = getprop_uint64(zhp, prop, source);
19214577Sahrens 			break;
19224577Sahrens 
19234787Sahrens 		case PROP_TYPE_STRING:
19244577Sahrens 		default:
19254577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
19264577Sahrens 			    "cannot get non-numeric property"));
19274577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
19284577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
19294577Sahrens 		}
1930789Sahrens 	}
1931789Sahrens 
1932789Sahrens 	return (0);
1933789Sahrens }
1934789Sahrens 
1935789Sahrens /*
1936789Sahrens  * Calculate the source type, given the raw source string.
1937789Sahrens  */
1938789Sahrens static void
19395094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1940789Sahrens     char *statbuf, size_t statlen)
1941789Sahrens {
19425094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1943789Sahrens 		return;
1944789Sahrens 
1945789Sahrens 	if (source == NULL) {
19465094Slling 		*srctype = ZPROP_SRC_NONE;
1947789Sahrens 	} else if (source[0] == '\0') {
19485094Slling 		*srctype = ZPROP_SRC_DEFAULT;
1949789Sahrens 	} else {
1950789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
19515094Slling 			*srctype = ZPROP_SRC_LOCAL;
1952789Sahrens 		} else {
1953789Sahrens 			(void) strlcpy(statbuf, source, statlen);
19545094Slling 			*srctype = ZPROP_SRC_INHERITED;
1955789Sahrens 		}
1956789Sahrens 	}
1957789Sahrens 
1958789Sahrens }
1959789Sahrens 
1960789Sahrens /*
1961789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
1962789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
1963789Sahrens  * human-readable form.
1964789Sahrens  *
1965789Sahrens  * Returns 0 on success, or -1 on error.
1966789Sahrens  */
1967789Sahrens int
1968789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
19695094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1970789Sahrens {
1971789Sahrens 	char *source = NULL;
1972789Sahrens 	uint64_t val;
1973789Sahrens 	char *str;
1974789Sahrens 	const char *root;
19752676Seschrock 	const char *strval;
1976789Sahrens 
1977789Sahrens 	/*
1978789Sahrens 	 * Check to see if this property applies to our object
1979789Sahrens 	 */
1980789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1981789Sahrens 		return (-1);
1982789Sahrens 
1983789Sahrens 	if (src)
19845094Slling 		*src = ZPROP_SRC_NONE;
1985789Sahrens 
1986789Sahrens 	switch (prop) {
1987789Sahrens 	case ZFS_PROP_CREATION:
1988789Sahrens 		/*
1989789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
1990789Sahrens 		 * this into a string unless 'literal' is specified.
1991789Sahrens 		 */
1992789Sahrens 		{
19932885Sahrens 			val = getprop_uint64(zhp, prop, &source);
19942885Sahrens 			time_t time = (time_t)val;
1995789Sahrens 			struct tm t;
1996789Sahrens 
1997789Sahrens 			if (literal ||
1998789Sahrens 			    localtime_r(&time, &t) == NULL ||
1999789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2000789Sahrens 			    &t) == 0)
20012885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2002789Sahrens 		}
2003789Sahrens 		break;
2004789Sahrens 
2005789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2006789Sahrens 		/*
2007789Sahrens 		 * Getting the precise mountpoint can be tricky.
2008789Sahrens 		 *
2009789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2010789Sahrens 		 *  - for default mountpoints, construct it as /zfs/<dataset>
2011789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2012789Sahrens 		 *    after our ancestor and append it to the inherited value.
2013789Sahrens 		 *
2014789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2015789Sahrens 		 * root to any values we return.
2016789Sahrens 		 */
20171544Seschrock 		root = zhp->zfs_root;
20181356Seschrock 		str = getprop_string(zhp, prop, &source);
20191356Seschrock 
20201356Seschrock 		if (str[0] == '\0') {
2021789Sahrens 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
2022789Sahrens 			    root, zhp->zfs_name);
20231356Seschrock 		} else if (str[0] == '/') {
20241356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2025789Sahrens 
2026789Sahrens 			if (relpath[0] == '/')
2027789Sahrens 				relpath++;
20281356Seschrock 			if (str[1] == '\0')
20291356Seschrock 				str++;
2030789Sahrens 
2031789Sahrens 			if (relpath[0] == '\0')
2032789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
20331356Seschrock 				    root, str);
2034789Sahrens 			else
2035789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
20361356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2037789Sahrens 				    relpath);
2038789Sahrens 		} else {
2039789Sahrens 			/* 'legacy' or 'none' */
20401356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2041789Sahrens 		}
2042789Sahrens 
2043789Sahrens 		break;
2044789Sahrens 
2045789Sahrens 	case ZFS_PROP_ORIGIN:
20462885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2047789Sahrens 		    proplen);
2048789Sahrens 		/*
2049789Sahrens 		 * If there is no parent at all, return failure to indicate that
2050789Sahrens 		 * it doesn't apply to this dataset.
2051789Sahrens 		 */
2052789Sahrens 		if (propbuf[0] == '\0')
2053789Sahrens 			return (-1);
2054789Sahrens 		break;
2055789Sahrens 
2056789Sahrens 	case ZFS_PROP_QUOTA:
2057789Sahrens 	case ZFS_PROP_RESERVATION:
20582082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
20592082Seschrock 			return (-1);
2060789Sahrens 
2061789Sahrens 		/*
2062789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2063789Sahrens 		 * (unless literal is set), and indicate that it's the default
2064789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2065789Sahrens 		 * that its set locally.
2066789Sahrens 		 */
2067789Sahrens 		if (val == 0) {
2068789Sahrens 			if (literal)
2069789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2070789Sahrens 			else
2071789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2072789Sahrens 		} else {
2073789Sahrens 			if (literal)
20742856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
20753912Slling 				    (u_longlong_t)val);
2076789Sahrens 			else
2077789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2078789Sahrens 		}
2079789Sahrens 		break;
2080789Sahrens 
2081789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
20822082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
20832082Seschrock 			return (-1);
20842856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
20852856Snd150628 		    val / 100, (longlong_t)val % 100);
2086789Sahrens 		break;
2087789Sahrens 
2088789Sahrens 	case ZFS_PROP_TYPE:
2089789Sahrens 		switch (zhp->zfs_type) {
2090789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2091789Sahrens 			str = "filesystem";
2092789Sahrens 			break;
2093789Sahrens 		case ZFS_TYPE_VOLUME:
2094789Sahrens 			str = "volume";
2095789Sahrens 			break;
2096789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2097789Sahrens 			str = "snapshot";
2098789Sahrens 			break;
2099789Sahrens 		default:
21002082Seschrock 			abort();
2101789Sahrens 		}
2102789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2103789Sahrens 		break;
2104789Sahrens 
2105789Sahrens 	case ZFS_PROP_MOUNTED:
2106789Sahrens 		/*
2107789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2108789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2109789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2110789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2111789Sahrens 		 */
21122082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
21132082Seschrock 		    src, &source, &val) != 0)
21142082Seschrock 			return (-1);
21152082Seschrock 		if (val)
2116789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2117789Sahrens 		else
2118789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2119789Sahrens 		break;
2120789Sahrens 
2121789Sahrens 	case ZFS_PROP_NAME:
2122789Sahrens 		/*
2123789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2124789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2125789Sahrens 		 * consumers.
2126789Sahrens 		 */
2127789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2128789Sahrens 		break;
2129789Sahrens 
2130789Sahrens 	default:
21314577Sahrens 		switch (zfs_prop_get_type(prop)) {
21324787Sahrens 		case PROP_TYPE_NUMBER:
21334577Sahrens 			if (get_numeric_property(zhp, prop, src,
21344577Sahrens 			    &source, &val) != 0)
21354577Sahrens 				return (-1);
21364577Sahrens 			if (literal)
21374577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
21384577Sahrens 				    (u_longlong_t)val);
21394577Sahrens 			else
21404577Sahrens 				zfs_nicenum(val, propbuf, proplen);
21414577Sahrens 			break;
21424577Sahrens 
21434787Sahrens 		case PROP_TYPE_STRING:
21444577Sahrens 			(void) strlcpy(propbuf,
21454577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
21464577Sahrens 			break;
21474577Sahrens 
21484787Sahrens 		case PROP_TYPE_INDEX:
21494861Sahrens 			if (get_numeric_property(zhp, prop, src,
21504861Sahrens 			    &source, &val) != 0)
21514861Sahrens 				return (-1);
21524861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
21534577Sahrens 				return (-1);
21544577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
21554577Sahrens 			break;
21564577Sahrens 
21574577Sahrens 		default:
21584577Sahrens 			abort();
21594577Sahrens 		}
2160789Sahrens 	}
2161789Sahrens 
2162789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2163789Sahrens 
2164789Sahrens 	return (0);
2165789Sahrens }
2166789Sahrens 
2167789Sahrens /*
2168789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2169789Sahrens  * the given property is the appropriate type; should only be used with
2170789Sahrens  * hard-coded property types.
2171789Sahrens  */
2172789Sahrens uint64_t
2173789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2174789Sahrens {
2175789Sahrens 	char *source;
21765094Slling 	zprop_source_t sourcetype = ZPROP_SRC_NONE;
21772082Seschrock 	uint64_t val;
21782082Seschrock 
21792082Seschrock 	(void) get_numeric_property(zhp, prop, &sourcetype, &source, &val);
21802082Seschrock 
21812082Seschrock 	return (val);
2182789Sahrens }
2183789Sahrens 
2184789Sahrens /*
2185789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2186789Sahrens  */
2187789Sahrens int
2188789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
21895094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2190789Sahrens {
2191789Sahrens 	char *source;
2192789Sahrens 
2193789Sahrens 	/*
2194789Sahrens 	 * Check to see if this property applies to our object
2195789Sahrens 	 */
21964849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
21973237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
21982082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
21992082Seschrock 		    zfs_prop_to_name(prop)));
22004849Sahrens 	}
2201789Sahrens 
2202789Sahrens 	if (src)
22035094Slling 		*src = ZPROP_SRC_NONE;
2204789Sahrens 
22052082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
22062082Seschrock 		return (-1);
2207789Sahrens 
2208789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2209789Sahrens 
2210789Sahrens 	return (0);
2211789Sahrens }
2212789Sahrens 
2213789Sahrens /*
2214789Sahrens  * Returns the name of the given zfs handle.
2215789Sahrens  */
2216789Sahrens const char *
2217789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2218789Sahrens {
2219789Sahrens 	return (zhp->zfs_name);
2220789Sahrens }
2221789Sahrens 
2222789Sahrens /*
2223789Sahrens  * Returns the type of the given zfs handle.
2224789Sahrens  */
2225789Sahrens zfs_type_t
2226789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2227789Sahrens {
2228789Sahrens 	return (zhp->zfs_type);
2229789Sahrens }
2230789Sahrens 
2231789Sahrens /*
22321356Seschrock  * Iterate over all child filesystems
2233789Sahrens  */
2234789Sahrens int
22351356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2236789Sahrens {
2237789Sahrens 	zfs_cmd_t zc = { 0 };
2238789Sahrens 	zfs_handle_t *nzhp;
2239789Sahrens 	int ret;
2240789Sahrens 
2241789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
22422082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2243789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2244789Sahrens 		/*
2245789Sahrens 		 * Ignore private dataset names.
2246789Sahrens 		 */
2247789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2248789Sahrens 			continue;
2249789Sahrens 
2250789Sahrens 		/*
2251789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2252789Sahrens 		 * that the pool has since been removed.
2253789Sahrens 		 */
22542082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
22552082Seschrock 		    zc.zc_name)) == NULL)
2256789Sahrens 			continue;
2257789Sahrens 
2258789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2259789Sahrens 			return (ret);
2260789Sahrens 	}
2261789Sahrens 
2262789Sahrens 	/*
2263789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2264789Sahrens 	 * returned, then the underlying dataset has been removed since we
2265789Sahrens 	 * obtained the handle.
2266789Sahrens 	 */
2267789Sahrens 	if (errno != ESRCH && errno != ENOENT)
22682082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
22692082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2270789Sahrens 
22711356Seschrock 	return (0);
22721356Seschrock }
22731356Seschrock 
22741356Seschrock /*
22751356Seschrock  * Iterate over all snapshots
22761356Seschrock  */
22771356Seschrock int
22781356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
22791356Seschrock {
22801356Seschrock 	zfs_cmd_t zc = { 0 };
22811356Seschrock 	zfs_handle_t *nzhp;
22821356Seschrock 	int ret;
2283789Sahrens 
2284789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
22852082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
22862082Seschrock 	    &zc) == 0;
2287789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2288789Sahrens 
22892082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
22902082Seschrock 		    zc.zc_name)) == NULL)
2291789Sahrens 			continue;
2292789Sahrens 
2293789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2294789Sahrens 			return (ret);
2295789Sahrens 	}
2296789Sahrens 
2297789Sahrens 	/*
2298789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2299789Sahrens 	 * returned, then the underlying dataset has been removed since we
2300789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2301789Sahrens 	 */
2302789Sahrens 	if (errno != ESRCH && errno != ENOENT)
23032082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
23042082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2305789Sahrens 
2306789Sahrens 	return (0);
2307789Sahrens }
2308789Sahrens 
2309789Sahrens /*
23101356Seschrock  * Iterate over all children, snapshots and filesystems
23111356Seschrock  */
23121356Seschrock int
23131356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23141356Seschrock {
23151356Seschrock 	int ret;
23161356Seschrock 
23171356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
23181356Seschrock 		return (ret);
23191356Seschrock 
23201356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
23211356Seschrock }
23221356Seschrock 
23231356Seschrock /*
2324789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2325789Sahrens  * Can return NULL if this is a pool.
2326789Sahrens  */
2327789Sahrens static int
2328789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2329789Sahrens {
2330789Sahrens 	char *loc;
2331789Sahrens 
2332789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2333789Sahrens 		return (-1);
2334789Sahrens 
2335789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2336789Sahrens 	buf[loc - path] = '\0';
2337789Sahrens 
2338789Sahrens 	return (0);
2339789Sahrens }
2340789Sahrens 
2341789Sahrens /*
23424490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
23434490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
23444490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
23454490Svb160487  * length of already existing prefix of the given path.  We also fetch the
23464490Svb160487  * 'zoned' property, which is used to validate property settings when creating
23474490Svb160487  * new datasets.
2348789Sahrens  */
2349789Sahrens static int
23504490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
23514490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2352789Sahrens {
2353789Sahrens 	zfs_cmd_t zc = { 0 };
2354789Sahrens 	char parent[ZFS_MAXNAMELEN];
2355789Sahrens 	char *slash;
23561356Seschrock 	zfs_handle_t *zhp;
23572082Seschrock 	char errbuf[1024];
23582082Seschrock 
23592082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
23602082Seschrock 	    path);
2361789Sahrens 
2362789Sahrens 	/* get parent, and check to see if this is just a pool */
2363789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
23642082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23652082Seschrock 		    "missing dataset name"));
23662082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2367789Sahrens 	}
2368789Sahrens 
2369789Sahrens 	/* check to see if the pool exists */
2370789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2371789Sahrens 		slash = parent + strlen(parent);
2372789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2373789Sahrens 	zc.zc_name[slash - parent] = '\0';
23742082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2375789Sahrens 	    errno == ENOENT) {
23762082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23772082Seschrock 		    "no such pool '%s'"), zc.zc_name);
23782082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2379789Sahrens 	}
2380789Sahrens 
2381789Sahrens 	/* check to see if the parent dataset exists */
23824490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
23834490Svb160487 		if (errno == ENOENT && accept_ancestor) {
23844490Svb160487 			/*
23854490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
23864490Svb160487 			 */
23874490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
23884490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23894490Svb160487 				    "no such pool '%s'"), zc.zc_name);
23904490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
23914490Svb160487 			}
23924490Svb160487 		} else if (errno == ENOENT) {
23932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23942082Seschrock 			    "parent does not exist"));
23952082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
23964490Svb160487 		} else
23972082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2398789Sahrens 	}
2399789Sahrens 
24002676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2401789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
24022676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
24032082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
24041356Seschrock 		zfs_close(zhp);
2405789Sahrens 		return (-1);
2406789Sahrens 	}
2407789Sahrens 
2408789Sahrens 	/* make sure parent is a filesystem */
24091356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
24102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24112082Seschrock 		    "parent is not a filesystem"));
24122082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
24131356Seschrock 		zfs_close(zhp);
2414789Sahrens 		return (-1);
2415789Sahrens 	}
2416789Sahrens 
24171356Seschrock 	zfs_close(zhp);
24184490Svb160487 	if (prefixlen != NULL)
24194490Svb160487 		*prefixlen = strlen(parent);
24204490Svb160487 	return (0);
24214490Svb160487 }
24224490Svb160487 
24234490Svb160487 /*
24244490Svb160487  * Finds whether the dataset of the given type(s) exists.
24254490Svb160487  */
24264490Svb160487 boolean_t
24274490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
24284490Svb160487 {
24294490Svb160487 	zfs_handle_t *zhp;
24304490Svb160487 
24314490Svb160487 	if (!zfs_validate_name(hdl, path, types))
24324490Svb160487 		return (B_FALSE);
24334490Svb160487 
24344490Svb160487 	/*
24354490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
24364490Svb160487 	 */
24374490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
24384490Svb160487 		int ds_type = zhp->zfs_type;
24394490Svb160487 
24404490Svb160487 		zfs_close(zhp);
24414490Svb160487 		if (types & ds_type)
24424490Svb160487 			return (B_TRUE);
24434490Svb160487 	}
24444490Svb160487 	return (B_FALSE);
24454490Svb160487 }
24464490Svb160487 
24474490Svb160487 /*
24484490Svb160487  * Creates non-existing ancestors of the given path.
24494490Svb160487  */
24504490Svb160487 int
24514490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
24524490Svb160487 {
24534490Svb160487 	int prefix;
24544490Svb160487 	uint64_t zoned;
24554490Svb160487 	char *path_copy;
24564490Svb160487 	int rc;
24574490Svb160487 
24584490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
24594490Svb160487 		return (-1);
24604490Svb160487 
24614490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
24624490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
24634490Svb160487 		free(path_copy);
24644490Svb160487 	}
24654490Svb160487 	if (path_copy == NULL || rc != 0)
24664490Svb160487 		return (-1);
24674490Svb160487 
2468789Sahrens 	return (0);
2469789Sahrens }
2470789Sahrens 
2471789Sahrens /*
24722676Seschrock  * Create a new filesystem or volume.
2473789Sahrens  */
2474789Sahrens int
24752082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
24762676Seschrock     nvlist_t *props)
2477789Sahrens {
2478789Sahrens 	zfs_cmd_t zc = { 0 };
2479789Sahrens 	int ret;
2480789Sahrens 	uint64_t size = 0;
2481789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
24822082Seschrock 	char errbuf[1024];
24832676Seschrock 	uint64_t zoned;
24842082Seschrock 
24852082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
24862082Seschrock 	    "cannot create '%s'"), path);
2487789Sahrens 
2488789Sahrens 	/* validate the path, taking care to note the extended error message */
24892082Seschrock 	if (!zfs_validate_name(hdl, path, type))
24902082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2491789Sahrens 
2492789Sahrens 	/* validate parents exist */
24934490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2494789Sahrens 		return (-1);
2495789Sahrens 
2496789Sahrens 	/*
2497789Sahrens 	 * The failure modes when creating a dataset of a different type over
2498789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2499789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2500789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2501789Sahrens 	 * first try to see if the dataset exists.
2502789Sahrens 	 */
2503789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
25045094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
25052082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25062082Seschrock 		    "dataset already exists"));
25072082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2508789Sahrens 	}
2509789Sahrens 
2510789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2511789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2512789Sahrens 	else
2513789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2514789Sahrens 
25155094Slling 	if (props && (props = zfs_validate_properties(hdl, type, props,
25163912Slling 	    zoned, NULL, errbuf)) == 0)
25172676Seschrock 		return (-1);
25182676Seschrock 
2519789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
25201133Seschrock 		/*
25211133Seschrock 		 * If we are creating a volume, the size and block size must
25221133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
25231133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
25241133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
25251133Seschrock 		 * zero.
25261133Seschrock 		 */
25272676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
25282676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
25292676Seschrock 			nvlist_free(props);
25302082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25312676Seschrock 			    "missing volume size"));
25322676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2533789Sahrens 		}
2534789Sahrens 
25352676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
25362676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
25372676Seschrock 		    &blocksize)) != 0) {
25382676Seschrock 			if (ret == ENOENT) {
25392676Seschrock 				blocksize = zfs_prop_default_numeric(
25402676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
25412676Seschrock 			} else {
25422676Seschrock 				nvlist_free(props);
25432676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25442676Seschrock 				    "missing volume block size"));
25452676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
25462676Seschrock 			}
25472676Seschrock 		}
25482676Seschrock 
25492676Seschrock 		if (size == 0) {
25502676Seschrock 			nvlist_free(props);
25512082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25522676Seschrock 			    "volume size cannot be zero"));
25532676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
25541133Seschrock 		}
25551133Seschrock 
25561133Seschrock 		if (size % blocksize != 0) {
25572676Seschrock 			nvlist_free(props);
25582082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25592676Seschrock 			    "volume size must be a multiple of volume block "
25602676Seschrock 			    "size"));
25612676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
25621133Seschrock 		}
2563789Sahrens 	}
2564789Sahrens 
25655094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
25662676Seschrock 		return (-1);
25672676Seschrock 	nvlist_free(props);
25682676Seschrock 
2569789Sahrens 	/* create the dataset */
25704543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2571789Sahrens 
25723912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
25732082Seschrock 		ret = zvol_create_link(hdl, path);
25743912Slling 		if (ret) {
25753912Slling 			(void) zfs_standard_error(hdl, errno,
25763912Slling 			    dgettext(TEXT_DOMAIN,
25773912Slling 			    "Volume successfully created, but device links "
25783912Slling 			    "were not created"));
25793912Slling 			zcmd_free_nvlists(&zc);
25803912Slling 			return (-1);
25813912Slling 		}
25823912Slling 	}
2583789Sahrens 
25842676Seschrock 	zcmd_free_nvlists(&zc);
25852676Seschrock 
2586789Sahrens 	/* check for failure */
2587789Sahrens 	if (ret != 0) {
2588789Sahrens 		char parent[ZFS_MAXNAMELEN];
2589789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2590789Sahrens 
2591789Sahrens 		switch (errno) {
2592789Sahrens 		case ENOENT:
25932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25942082Seschrock 			    "no such parent '%s'"), parent);
25952082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2596789Sahrens 
2597789Sahrens 		case EINVAL:
25982082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25993413Smmusante 			    "parent '%s' is not a filesystem"), parent);
26002082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2601789Sahrens 
2602789Sahrens 		case EDOM:
26032082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26042676Seschrock 			    "volume block size must be power of 2 from "
26052676Seschrock 			    "%u to %uk"),
2606789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2607789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
26082082Seschrock 
26092676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26102082Seschrock 
26114603Sahrens 		case ENOTSUP:
26124603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26134603Sahrens 			    "pool must be upgraded to set this "
26144603Sahrens 			    "property or value"));
26154603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
26164603Sahrens 
2617789Sahrens #ifdef _ILP32
2618789Sahrens 		case EOVERFLOW:
2619789Sahrens 			/*
2620789Sahrens 			 * This platform can't address a volume this big.
2621789Sahrens 			 */
26222082Seschrock 			if (type == ZFS_TYPE_VOLUME)
26232082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
26242082Seschrock 				    errbuf));
2625789Sahrens #endif
26262082Seschrock 			/* FALLTHROUGH */
2627789Sahrens 		default:
26282082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2629789Sahrens 		}
2630789Sahrens 	}
2631789Sahrens 
2632789Sahrens 	return (0);
2633789Sahrens }
2634789Sahrens 
2635789Sahrens /*
2636789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2637789Sahrens  * isn't mounted, and that there are no active dependents.
2638789Sahrens  */
2639789Sahrens int
2640789Sahrens zfs_destroy(zfs_handle_t *zhp)
2641789Sahrens {
2642789Sahrens 	zfs_cmd_t zc = { 0 };
2643789Sahrens 
2644789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2645789Sahrens 
26462676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
26473126Sahl 		/*
26484543Smarks 		 * If user doesn't have permissions to unshare volume, then
26494543Smarks 		 * abort the request.  This would only happen for a
26504543Smarks 		 * non-privileged user.
26513126Sahl 		 */
26524543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
26534543Smarks 			return (-1);
26544543Smarks 		}
26553126Sahl 
26562082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2657789Sahrens 			return (-1);
2658789Sahrens 
2659789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2660789Sahrens 	} else {
2661789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2662789Sahrens 	}
2663789Sahrens 
26644543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
26653237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
26662082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
26672082Seschrock 		    zhp->zfs_name));
26682199Sahrens 	}
2669789Sahrens 
2670789Sahrens 	remove_mountpoint(zhp);
2671789Sahrens 
2672789Sahrens 	return (0);
2673789Sahrens }
2674789Sahrens 
26752199Sahrens struct destroydata {
26762199Sahrens 	char *snapname;
26772199Sahrens 	boolean_t gotone;
26783265Sahrens 	boolean_t closezhp;
26792199Sahrens };
26802199Sahrens 
26812199Sahrens static int
26822199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
26832199Sahrens {
26842199Sahrens 	struct destroydata *dd = arg;
26852199Sahrens 	zfs_handle_t *szhp;
26862199Sahrens 	char name[ZFS_MAXNAMELEN];
26873265Sahrens 	boolean_t closezhp = dd->closezhp;
26883265Sahrens 	int rv;
26892199Sahrens 
26902676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
26912676Seschrock 	(void) strlcat(name, "@", sizeof (name));
26922676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
26932199Sahrens 
26942199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
26952199Sahrens 	if (szhp) {
26962199Sahrens 		dd->gotone = B_TRUE;
26972199Sahrens 		zfs_close(szhp);
26982199Sahrens 	}
26992199Sahrens 
27002199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
27012199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
27022199Sahrens 		/*
27032199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
27042199Sahrens 		 * return an error, because then we wouldn't visit all
27052199Sahrens 		 * the volumes.
27062199Sahrens 		 */
27072199Sahrens 	}
27082199Sahrens 
27093265Sahrens 	dd->closezhp = B_TRUE;
27103265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
27113265Sahrens 	if (closezhp)
27123265Sahrens 		zfs_close(zhp);
27133265Sahrens 	return (rv);
27142199Sahrens }
27152199Sahrens 
27162199Sahrens /*
27172199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
27182199Sahrens  */
27192199Sahrens int
27202199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
27212199Sahrens {
27222199Sahrens 	zfs_cmd_t zc = { 0 };
27232199Sahrens 	int ret;
27242199Sahrens 	struct destroydata dd = { 0 };
27252199Sahrens 
27262199Sahrens 	dd.snapname = snapname;
27272199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
27282199Sahrens 
27292199Sahrens 	if (!dd.gotone) {
27303237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
27312199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
27322199Sahrens 		    zhp->zfs_name, snapname));
27332199Sahrens 	}
27342199Sahrens 
27352199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
27362676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
27372199Sahrens 
27384543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
27392199Sahrens 	if (ret != 0) {
27402199Sahrens 		char errbuf[1024];
27412199Sahrens 
27422199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
27432199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
27442199Sahrens 
27452199Sahrens 		switch (errno) {
27462199Sahrens 		case EEXIST:
27472199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
27482199Sahrens 			    "snapshot is cloned"));
27492199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
27502199Sahrens 
27512199Sahrens 		default:
27522199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
27532199Sahrens 			    errbuf));
27542199Sahrens 		}
27552199Sahrens 	}
27562199Sahrens 
27572199Sahrens 	return (0);
27582199Sahrens }
27592199Sahrens 
2760789Sahrens /*
2761789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
2762789Sahrens  */
2763789Sahrens int
27642676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2765789Sahrens {
2766789Sahrens 	zfs_cmd_t zc = { 0 };
2767789Sahrens 	char parent[ZFS_MAXNAMELEN];
2768789Sahrens 	int ret;
27692082Seschrock 	char errbuf[1024];
27702082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
27712676Seschrock 	zfs_type_t type;
27722676Seschrock 	uint64_t zoned;
2773789Sahrens 
2774789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2775789Sahrens 
27762082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
27772082Seschrock 	    "cannot create '%s'"), target);
27782082Seschrock 
2779789Sahrens 	/* validate the target name */
27802082Seschrock 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM))
27812082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2782789Sahrens 
2783789Sahrens 	/* validate parents exist */
27844490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2785789Sahrens 		return (-1);
2786789Sahrens 
2787789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
2788789Sahrens 
2789789Sahrens 	/* do the clone */
27902676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
2791789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
27922744Snn35248 		type = ZFS_TYPE_VOLUME;
27932676Seschrock 	} else {
2794789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
27952744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
27962676Seschrock 	}
27972676Seschrock 
27982676Seschrock 	if (props) {
27995094Slling 		if ((props = zfs_validate_properties(hdl, type, props,
28003912Slling 		    zoned, zhp, errbuf)) == NULL)
28012676Seschrock 			return (-1);
28022676Seschrock 
28035094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
28042676Seschrock 			nvlist_free(props);
28052676Seschrock 			return (-1);
28062676Seschrock 		}
28072676Seschrock 
28082676Seschrock 		nvlist_free(props);
28092676Seschrock 	}
2810789Sahrens 
2811789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
28122676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
28134543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2814789Sahrens 
28152676Seschrock 	zcmd_free_nvlists(&zc);
28162676Seschrock 
2817789Sahrens 	if (ret != 0) {
2818789Sahrens 		switch (errno) {
2819789Sahrens 
2820789Sahrens 		case ENOENT:
2821789Sahrens 			/*
2822789Sahrens 			 * The parent doesn't exist.  We should have caught this
2823789Sahrens 			 * above, but there may a race condition that has since
2824789Sahrens 			 * destroyed the parent.
2825789Sahrens 			 *
2826789Sahrens 			 * At this point, we don't know whether it's the source
2827789Sahrens 			 * that doesn't exist anymore, or whether the target
2828789Sahrens 			 * dataset doesn't exist.
2829789Sahrens 			 */
28302082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28312082Seschrock 			    "no such parent '%s'"), parent);
28322082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
28332082Seschrock 
28342082Seschrock 		case EXDEV:
28352082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28362082Seschrock 			    "source and target pools differ"));
28372082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
28382082Seschrock 			    errbuf));
28392082Seschrock 
28402082Seschrock 		default:
28412082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
28422082Seschrock 			    errbuf));
28432082Seschrock 		}
28442676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
28452082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
28462082Seschrock 	}
28472082Seschrock 
28482082Seschrock 	return (ret);
28492082Seschrock }
28502082Seschrock 
28512082Seschrock typedef struct promote_data {
28522082Seschrock 	char cb_mountpoint[MAXPATHLEN];
28532082Seschrock 	const char *cb_target;
28542082Seschrock 	const char *cb_errbuf;
28552082Seschrock 	uint64_t cb_pivot_txg;
28562082Seschrock } promote_data_t;
28572082Seschrock 
28582082Seschrock static int
28592082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
28602082Seschrock {
28612082Seschrock 	promote_data_t *pd = data;
28622082Seschrock 	zfs_handle_t *szhp;
28632082Seschrock 	char snapname[MAXPATHLEN];
28643265Sahrens 	int rv = 0;
28652082Seschrock 
28662082Seschrock 	/* We don't care about snapshots after the pivot point */
28673265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
28683265Sahrens 		zfs_close(zhp);
28692082Seschrock 		return (0);
28703265Sahrens 	}
28712082Seschrock 
28722417Sahrens 	/* Remove the device link if it's a zvol. */
28732676Seschrock 	if (ZFS_IS_VOLUME(zhp))
28742417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
28752082Seschrock 
28762082Seschrock 	/* Check for conflicting names */
28772676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
28782676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
28792082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
28802082Seschrock 	if (szhp != NULL) {
28812082Seschrock 		zfs_close(szhp);
28822082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28832082Seschrock 		    "snapshot name '%s' from origin \n"
28842082Seschrock 		    "conflicts with '%s' from target"),
28852082Seschrock 		    zhp->zfs_name, snapname);
28863265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
28872082Seschrock 	}
28883265Sahrens 	zfs_close(zhp);
28893265Sahrens 	return (rv);
28902082Seschrock }
28912082Seschrock 
28922417Sahrens static int
28932417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
28942417Sahrens {
28952417Sahrens 	promote_data_t *pd = data;
28962417Sahrens 
28972417Sahrens 	/* We don't care about snapshots after the pivot point */
28983265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
28993265Sahrens 		/* Create the device link if it's a zvol. */
29003265Sahrens 		if (ZFS_IS_VOLUME(zhp))
29013265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
29023265Sahrens 	}
29033265Sahrens 
29043265Sahrens 	zfs_close(zhp);
29052417Sahrens 	return (0);
29062417Sahrens }
29072417Sahrens 
29082082Seschrock /*
29092082Seschrock  * Promotes the given clone fs to be the clone parent.
29102082Seschrock  */
29112082Seschrock int
29122082Seschrock zfs_promote(zfs_handle_t *zhp)
29132082Seschrock {
29142082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
29152082Seschrock 	zfs_cmd_t zc = { 0 };
29162082Seschrock 	char parent[MAXPATHLEN];
29172082Seschrock 	char *cp;
29182082Seschrock 	int ret;
29192082Seschrock 	zfs_handle_t *pzhp;
29202082Seschrock 	promote_data_t pd;
29212082Seschrock 	char errbuf[1024];
29222082Seschrock 
29232082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29242082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
29252082Seschrock 
29262082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
29272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29282082Seschrock 		    "snapshots can not be promoted"));
29292082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
29302082Seschrock 	}
29312082Seschrock 
29322676Seschrock 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent));
29332082Seschrock 	if (parent[0] == '\0') {
29342082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29352082Seschrock 		    "not a cloned filesystem"));
29362082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
29372082Seschrock 	}
29382082Seschrock 	cp = strchr(parent, '@');
29392082Seschrock 	*cp = '\0';
29402082Seschrock 
29412082Seschrock 	/* Walk the snapshots we will be moving */
29422082Seschrock 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT);
29432082Seschrock 	if (pzhp == NULL)
29442082Seschrock 		return (-1);
29452082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
29462082Seschrock 	zfs_close(pzhp);
29472082Seschrock 	pd.cb_target = zhp->zfs_name;
29482082Seschrock 	pd.cb_errbuf = errbuf;
29495094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
29502082Seschrock 	if (pzhp == NULL)
29512082Seschrock 		return (-1);
29522082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
29532082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
29542082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
29552417Sahrens 	if (ret != 0) {
29562417Sahrens 		zfs_close(pzhp);
29572082Seschrock 		return (-1);
29582417Sahrens 	}
29592082Seschrock 
29602082Seschrock 	/* issue the ioctl */
29612676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of,
29622676Seschrock 	    sizeof (zc.zc_value));
29632082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
29644543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
29652082Seschrock 
29662082Seschrock 	if (ret != 0) {
29672417Sahrens 		int save_errno = errno;
29682417Sahrens 
29692417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
29702417Sahrens 		zfs_close(pzhp);
29712417Sahrens 
29722417Sahrens 		switch (save_errno) {
2973789Sahrens 		case EEXIST:
2974789Sahrens 			/*
29752082Seschrock 			 * There is a conflicting snapshot name.  We
29762082Seschrock 			 * should have caught this above, but they could
29772082Seschrock 			 * have renamed something in the mean time.
2978789Sahrens 			 */
29792082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29802082Seschrock 			    "conflicting snapshot name from parent '%s'"),
29812082Seschrock 			    parent);
29822082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2983789Sahrens 
2984789Sahrens 		default:
29852417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
2986789Sahrens 		}
29872417Sahrens 	} else {
29882417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
2989789Sahrens 	}
2990789Sahrens 
29912417Sahrens 	zfs_close(pzhp);
2992789Sahrens 	return (ret);
2993789Sahrens }
2994789Sahrens 
29954007Smmusante struct createdata {
29964007Smmusante 	const char *cd_snapname;
29974007Smmusante 	int cd_ifexists;
29984007Smmusante };
29994007Smmusante 
30002199Sahrens static int
30012199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
30022199Sahrens {
30034007Smmusante 	struct createdata *cd = arg;
30042676Seschrock 	int ret;
30052199Sahrens 
30062199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
30072199Sahrens 		char name[MAXPATHLEN];
30082199Sahrens 
30092676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
30102676Seschrock 		(void) strlcat(name, "@", sizeof (name));
30114007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
30124007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
30134007Smmusante 		    cd->cd_ifexists);
30142199Sahrens 		/*
30152199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
30162199Sahrens 		 * return an error, because then we wouldn't visit all
30172199Sahrens 		 * the volumes.
30182199Sahrens 		 */
30192199Sahrens 	}
30202676Seschrock 
30214007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
30222676Seschrock 
30232676Seschrock 	zfs_close(zhp);
30242676Seschrock 
30252676Seschrock 	return (ret);
30262199Sahrens }
30272199Sahrens 
3028789Sahrens /*
30293504Sahl  * Takes a snapshot of the given dataset.
3030789Sahrens  */
3031789Sahrens int
30322199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3033789Sahrens {
3034789Sahrens 	const char *delim;
3035789Sahrens 	char *parent;
3036789Sahrens 	zfs_handle_t *zhp;
3037789Sahrens 	zfs_cmd_t zc = { 0 };
3038789Sahrens 	int ret;
30392082Seschrock 	char errbuf[1024];
30402082Seschrock 
30412082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30422082Seschrock 	    "cannot snapshot '%s'"), path);
30432082Seschrock 
30442082Seschrock 	/* validate the target name */
30452082Seschrock 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT))
30462082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3047789Sahrens 
3048789Sahrens 	/* make sure the parent exists and is of the appropriate type */
30492199Sahrens 	delim = strchr(path, '@');
30502082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
30512082Seschrock 		return (-1);
3052789Sahrens 	(void) strncpy(parent, path, delim - path);
3053789Sahrens 	parent[delim - path] = '\0';
3054789Sahrens 
30552082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3056789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3057789Sahrens 		free(parent);
3058789Sahrens 		return (-1);
3059789Sahrens 	}
3060789Sahrens 
30612199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
30622676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
30634543Smarks 	if (ZFS_IS_VOLUME(zhp))
30644543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
30654543Smarks 	else
30664543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
30672199Sahrens 	zc.zc_cookie = recursive;
30684543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
30692199Sahrens 
30702199Sahrens 	/*
30712199Sahrens 	 * if it was recursive, the one that actually failed will be in
30722199Sahrens 	 * zc.zc_name.
30732199Sahrens 	 */
30744543Smarks 	if (ret != 0)
30754543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30764543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
30774543Smarks 
30782199Sahrens 	if (ret == 0 && recursive) {
30794007Smmusante 		struct createdata cd;
30804007Smmusante 
30814007Smmusante 		cd.cd_snapname = delim + 1;
30824007Smmusante 		cd.cd_ifexists = B_FALSE;
30834007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
30842199Sahrens 	}
3085789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
30862082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
30872199Sahrens 		if (ret != 0) {
30884543Smarks 			(void) zfs_standard_error(hdl, errno,
30894543Smarks 			    dgettext(TEXT_DOMAIN,
30904543Smarks 			    "Volume successfully snapshotted, but device links "
30914543Smarks 			    "were not created"));
30924543Smarks 			free(parent);
30934543Smarks 			zfs_close(zhp);
30944543Smarks 			return (-1);
30952199Sahrens 		}
3096789Sahrens 	}
3097789Sahrens 
30982082Seschrock 	if (ret != 0)
30992082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3100789Sahrens 
3101789Sahrens 	free(parent);
3102789Sahrens 	zfs_close(zhp);
3103789Sahrens 
3104789Sahrens 	return (ret);
3105789Sahrens }
3106789Sahrens 
3107789Sahrens /*
31083504Sahl  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
31093504Sahl  * NULL) to the file descriptor specified by outfd.
3110789Sahrens  */
3111789Sahrens int
31123504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd)
3113789Sahrens {
3114789Sahrens 	zfs_cmd_t zc = { 0 };
31152082Seschrock 	char errbuf[1024];
31162885Sahrens 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31172082Seschrock 
31183504Sahl 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
31193504Sahl 
31202885Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31212885Sahrens 	if (fromsnap)
31222885Sahrens 		(void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name));
31233504Sahl 	zc.zc_cookie = outfd;
31243504Sahl 
31253504Sahl 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) {
31263504Sahl 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31273504Sahl 		    "cannot send '%s'"), zhp->zfs_name);
31283504Sahl 
3129789Sahrens 		switch (errno) {
3130789Sahrens 
3131789Sahrens 		case EXDEV:
31322082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31333413Smmusante 			    "not an earlier snapshot from the same fs"));
31342082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3135789Sahrens 
3136789Sahrens 		case EDQUOT:
3137789Sahrens 		case EFBIG:
3138789Sahrens 		case EIO:
3139789Sahrens 		case ENOLINK:
3140789Sahrens 		case ENOSPC:
3141789Sahrens 		case ENOSTR:
3142789Sahrens 		case ENXIO:
3143789Sahrens 		case EPIPE:
3144789Sahrens 		case ERANGE:
3145789Sahrens 		case EFAULT:
3146789Sahrens 		case EROFS:
31472082Seschrock 			zfs_error_aux(hdl, strerror(errno));
31482082Seschrock 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
3149789Sahrens 
3150789Sahrens 		default:
31512082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3152789Sahrens 		}
3153789Sahrens 	}
3154789Sahrens 
31553504Sahl 	return (0);
3156789Sahrens }
3157789Sahrens 
3158789Sahrens /*
31592885Sahrens  * Create ancestors of 'target', but not target itself, and not
31602885Sahrens  * ancestors whose names are shorter than prefixlen.  Die if
31612885Sahrens  * prefixlen-ancestor does not exist.
31622885Sahrens  */
31632885Sahrens static int
31642885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
31652885Sahrens {
31662885Sahrens 	zfs_handle_t *h;
31672885Sahrens 	char *cp;
31682885Sahrens 
31692885Sahrens 	/* make sure prefix exists */
31702885Sahrens 	cp = strchr(target + prefixlen, '/');
31714603Sahrens 	if (cp == NULL) {
31724603Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31734603Sahrens 	} else {
31744603Sahrens 		*cp = '\0';
31754603Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31764603Sahrens 		*cp = '/';
31774603Sahrens 	}
31782885Sahrens 	if (h == NULL)
31792885Sahrens 		return (-1);
31802885Sahrens 	zfs_close(h);
31812885Sahrens 
31822885Sahrens 	/*
31832885Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
31842885Sahrens 	 * up to the prefixlen-long one.
31852885Sahrens 	 */
31862885Sahrens 	for (cp = target + prefixlen + 1;
31872885Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
31882885Sahrens 		const char *opname;
31894543Smarks 		char *logstr;
31902885Sahrens 
31912885Sahrens 		*cp = '\0';
31922885Sahrens 
31932885Sahrens 		h = make_dataset_handle(hdl, target);
31942885Sahrens 		if (h) {
31952885Sahrens 			/* it already exists, nothing to do here */
31962885Sahrens 			zfs_close(h);
31972885Sahrens 			continue;
31982885Sahrens 		}
31992885Sahrens 
32002885Sahrens 		opname = dgettext(TEXT_DOMAIN, "create");
32014543Smarks 		logstr = hdl->libzfs_log_str;
32024543Smarks 		hdl->libzfs_log_str = NULL;
32032885Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
32044543Smarks 		    NULL) != 0) {
32054543Smarks 			hdl->libzfs_log_str = logstr;
32062885Sahrens 			goto ancestorerr;
32074543Smarks 		}
32084543Smarks 
32094543Smarks 		hdl->libzfs_log_str = logstr;
32102885Sahrens 		opname = dgettext(TEXT_DOMAIN, "open");
32112885Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
32122885Sahrens 		if (h == NULL)
32132885Sahrens 			goto ancestorerr;
32142885Sahrens 
32152885Sahrens 		opname = dgettext(TEXT_DOMAIN, "mount");
32162885Sahrens 		if (zfs_mount(h, NULL, 0) != 0)
32172885Sahrens 			goto ancestorerr;
32182885Sahrens 
32192885Sahrens 		opname = dgettext(TEXT_DOMAIN, "share");
32202885Sahrens 		if (zfs_share(h) != 0)
32212885Sahrens 			goto ancestorerr;
32222885Sahrens 
32232885Sahrens 		zfs_close(h);
32242885Sahrens 
32252885Sahrens 		continue;
32262885Sahrens ancestorerr:
32272885Sahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32282885Sahrens 		    "failed to %s ancestor '%s'"), opname, target);
32292885Sahrens 		return (-1);
32302885Sahrens 	}
32312885Sahrens 
32322885Sahrens 	return (0);
32332885Sahrens }
32342885Sahrens 
32352885Sahrens /*
32363504Sahl  * Restores a backup of tosnap from the file descriptor specified by infd.
3237789Sahrens  */
3238789Sahrens int
32392082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix,
32403504Sahl     int verbose, int dryrun, boolean_t force, int infd)
3241789Sahrens {
3242789Sahrens 	zfs_cmd_t zc = { 0 };
3243789Sahrens 	time_t begin_time;
32442885Sahrens 	int ioctl_err, err, bytes, size, choplen;
3245789Sahrens 	char *cp;
3246789Sahrens 	dmu_replay_record_t drr;
3247789Sahrens 	struct drr_begin *drrb = &zc.zc_begin_record;
32482082Seschrock 	char errbuf[1024];
32492665Snd150628 	prop_changelist_t *clp;
32502885Sahrens 	char chopprefix[ZFS_MAXNAMELEN];
3251789Sahrens 
3252789Sahrens 	begin_time = time(NULL);
3253789Sahrens 
32542082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32552082Seschrock 	    "cannot receive"));
32562082Seschrock 
3257789Sahrens 	/* read in the BEGIN record */
3258789Sahrens 	cp = (char *)&drr;
3259789Sahrens 	bytes = 0;
3260789Sahrens 	do {
32613504Sahl 		size = read(infd, cp, sizeof (drr) - bytes);
3262868Sahrens 		cp += size;
3263868Sahrens 		bytes += size;
3264868Sahrens 	} while (size > 0);
3265868Sahrens 
3266868Sahrens 	if (size < 0 || bytes != sizeof (drr)) {
32672082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
32682082Seschrock 		    "stream (failed to read first record)"));
32692082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3270789Sahrens 	}
3271789Sahrens 
3272789Sahrens 	zc.zc_begin_record = drr.drr_u.drr_begin;
3273789Sahrens 
3274789Sahrens 	if (drrb->drr_magic != DMU_BACKUP_MAGIC &&
3275789Sahrens 	    drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) {
32762082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
32772082Seschrock 		    "stream (bad magic number)"));
32782082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3279789Sahrens 	}
3280789Sahrens 
3281789Sahrens 	if (drrb->drr_version != DMU_BACKUP_VERSION &&
3282789Sahrens 	    drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) {
32832082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version "
32842082Seschrock 		    "0x%llx is supported (stream is version 0x%llx)"),
3285789Sahrens 		    DMU_BACKUP_VERSION, drrb->drr_version);
32862082Seschrock 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3287789Sahrens 	}
3288789Sahrens 
32892885Sahrens 	if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) {
32902885Sahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
32913912Slling 		    "stream (bad snapshot name)"));
32922885Sahrens 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
32932885Sahrens 	}
3294789Sahrens 	/*
32952885Sahrens 	 * Determine how much of the snapshot name stored in the stream
32962885Sahrens 	 * we are going to tack on to the name they specified on the
32972885Sahrens 	 * command line, and how much we are going to chop off.
32982885Sahrens 	 *
32992885Sahrens 	 * If they specified a snapshot, chop the entire name stored in
33002885Sahrens 	 * the stream.
3301789Sahrens 	 */
33022885Sahrens 	(void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname);
3303789Sahrens 	if (isprefix) {
33042885Sahrens 		/*
33052885Sahrens 		 * They specified a fs with -d, we want to tack on
33062885Sahrens 		 * everything but the pool name stored in the stream
33072885Sahrens 		 */
33082885Sahrens 		if (strchr(tosnap, '@')) {
33092885Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
33102885Sahrens 			    "argument - snapshot not allowed with -d"));
33112885Sahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3312789Sahrens 		}
33132885Sahrens 		cp = strchr(chopprefix, '/');
3314789Sahrens 		if (cp == NULL)
33152885Sahrens 			cp = strchr(chopprefix, '@');
33162885Sahrens 		*cp = '\0';
3317789Sahrens 	} else if (strchr(tosnap, '@') == NULL) {
3318789Sahrens 		/*
33192885Sahrens 		 * If they specified a filesystem without -d, we want to
33202885Sahrens 		 * tack on everything after the fs specified in the
33212885Sahrens 		 * first name from the stream.
3322789Sahrens 		 */
33232885Sahrens 		cp = strchr(chopprefix, '@');
33242885Sahrens 		*cp = '\0';
3325789Sahrens 	}
33262885Sahrens 	choplen = strlen(chopprefix);
33272885Sahrens 
33282885Sahrens 	/*
33292885Sahrens 	 * Determine name of destination snapshot, store in zc_value.
33302885Sahrens 	 */
33312885Sahrens 	(void) strcpy(zc.zc_value, tosnap);
33322885Sahrens 	(void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen,
33332885Sahrens 	    sizeof (zc.zc_value));
33343265Sahrens 	if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT))
33353265Sahrens 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
33362885Sahrens 
33372885Sahrens 	(void) strcpy(zc.zc_name, zc.zc_value);
3338789Sahrens 	if (drrb->drr_fromguid) {
3339789Sahrens 		/* incremental backup stream */
33402885Sahrens 		zfs_handle_t *h;
33412885Sahrens 
33422885Sahrens 		/* do the recvbackup ioctl to the containing fs */
33432885Sahrens 		*strchr(zc.zc_name, '@') = '\0';
3344789Sahrens 
3345789Sahrens 		/* make sure destination fs exists */
33462082Seschrock 		h = zfs_open(hdl, zc.zc_name,
33472082Seschrock 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
33482082Seschrock 		if (h == NULL)
3349789Sahrens 			return (-1);
3350868Sahrens 		if (!dryrun) {
33512665Snd150628 			/*
33522665Snd150628 			 * We need to unmount all the dependents of the dataset
33532665Snd150628 			 * and the dataset itself. If it's a volume
33542665Snd150628 			 * then remove device link.
33552665Snd150628 			 */
3356868Sahrens 			if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
33572665Snd150628 				clp = changelist_gather(h, ZFS_PROP_NAME, 0);
33582665Snd150628 				if (clp == NULL)
33592665Snd150628 					return (-1);
33602665Snd150628 				if (changelist_prefix(clp) != 0) {
33612665Snd150628 					changelist_free(clp);
33622665Snd150628 					return (-1);
33632665Snd150628 				}
3364868Sahrens 			} else {
33654543Smarks 				if (zvol_remove_link(hdl, h->zfs_name) != 0) {
33664543Smarks 					zfs_close(h);
33674543Smarks 					return (-1);
33684543Smarks 				}
33694543Smarks 
3370868Sahrens 			}
3371868Sahrens 		}
3372789Sahrens 		zfs_close(h);
3373789Sahrens 	} else {
3374789Sahrens 		/* full backup stream */
3375789Sahrens 
3376868Sahrens 		/* Make sure destination fs does not exist */
33772885Sahrens 		*strchr(zc.zc_name, '@') = '\0';
33785094Slling 		if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
33792082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33802082Seschrock 			    "destination '%s' exists"), zc.zc_name);
33812082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3382868Sahrens 		}
3383868Sahrens 
33842885Sahrens 		if (strchr(zc.zc_name, '/') == NULL) {
33852885Sahrens 			/*
33862885Sahrens 			 * they're trying to do a recv into a
33872885Sahrens 			 * nonexistant topmost filesystem.
33882885Sahrens 			 */
33892885Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33902885Sahrens 			    "destination does not exist"), zc.zc_name);
33912885Sahrens 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
33922885Sahrens 		}
33932885Sahrens 
3394868Sahrens 		/* Do the recvbackup ioctl to the fs's parent. */
33952885Sahrens 		*strrchr(zc.zc_name, '/') = '\0';
33962885Sahrens 
33972885Sahrens 		if (isprefix && (err = create_parents(hdl,
33982885Sahrens 		    zc.zc_value, strlen(tosnap))) != 0) {
33992885Sahrens 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
34002885Sahrens 		}
34012885Sahrens 
3402789Sahrens 	}
3403789Sahrens 
34043504Sahl 	zc.zc_cookie = infd;
34052676Seschrock 	zc.zc_guid = force;
3406789Sahrens 	if (verbose) {
34071749Sahrens 		(void) printf("%s %s stream of %s into %s\n",
34081749Sahrens 		    dryrun ? "would receive" : "receiving",
3409789Sahrens 		    drrb->drr_fromguid ? "incremental" : "full",
3410789Sahrens 		    drr.drr_u.drr_begin.drr_toname,
34112676Seschrock 		    zc.zc_value);
3412789Sahrens 		(void) fflush(stdout);
3413789Sahrens 	}
3414789Sahrens 	if (dryrun)
3415789Sahrens 		return (0);
34164543Smarks 	err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc);
3417868Sahrens 	if (ioctl_err != 0) {
3418789Sahrens 		switch (errno) {
3419789Sahrens 		case ENODEV:
34202082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34212082Seschrock 			    "most recent snapshot does not match incremental "
34222082Seschrock 			    "source"));
34232082Seschrock 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3424789Sahrens 			break;
3425789Sahrens 		case ETXTBSY:
34262082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34272082Seschrock 			    "destination has been modified since most recent "
34282082Seschrock 			    "snapshot"));
34292082Seschrock 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3430789Sahrens 			break;
3431789Sahrens 		case EEXIST:
3432789Sahrens 			if (drrb->drr_fromguid == 0) {
3433789Sahrens 				/* it's the containing fs that exists */
34342676Seschrock 				cp = strchr(zc.zc_value, '@');
3435789Sahrens 				*cp = '\0';
3436789Sahrens 			}
34372082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34382082Seschrock 			    "destination already exists"));
34393237Slling 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
34403237Slling 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
34413237Slling 			    zc.zc_value);
3442789Sahrens 			break;
3443789Sahrens 		case EINVAL:
34442082Seschrock 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3445868Sahrens 			break;
34461544Seschrock 		case ECKSUM:
34472082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34482082Seschrock 			    "invalid stream (checksum mismatch)"));
34492082Seschrock 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3450789Sahrens 			break;
3451789Sahrens 		default:
34522082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
3453789Sahrens 		}
3454789Sahrens 	}
3455789Sahrens 
3456789Sahrens 	/*
3457868Sahrens 	 * Mount or recreate the /dev links for the target filesystem
3458868Sahrens 	 * (if created, or if we tore them down to do an incremental
3459868Sahrens 	 * restore), and the /dev links for the new snapshot (if
34602665Snd150628 	 * created). Also mount any children of the target filesystem
34612665Snd150628 	 * if we did an incremental receive.
3462789Sahrens 	 */
34632676Seschrock 	cp = strchr(zc.zc_value, '@');
3464868Sahrens 	if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) {
3465789Sahrens 		zfs_handle_t *h;
3466789Sahrens 
3467789Sahrens 		*cp = '\0';
34682676Seschrock 		h = zfs_open(hdl, zc.zc_value,
3469789Sahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3470868Sahrens 		*cp = '@';
3471789Sahrens 		if (h) {
34722665Snd150628 			if (h->zfs_type == ZFS_TYPE_VOLUME) {
34732082Seschrock 				err = zvol_create_link(hdl, h->zfs_name);
34741544Seschrock 				if (err == 0 && ioctl_err == 0)
34752082Seschrock 					err = zvol_create_link(hdl,
34762676Seschrock 					    zc.zc_value);
34772665Snd150628 			} else {
34782665Snd150628 				if (drrb->drr_fromguid) {
34792665Snd150628 					err = changelist_postfix(clp);
34802665Snd150628 					changelist_free(clp);
34812665Snd150628 				} else {
34822665Snd150628 					err = zfs_mount(h, NULL, 0);
34832665Snd150628 				}
3484868Sahrens 			}
34852665Snd150628 		zfs_close(h);
3486789Sahrens 		}
3487789Sahrens 	}
3488789Sahrens 
3489868Sahrens 	if (err || ioctl_err)
3490868Sahrens 		return (-1);
3491789Sahrens 
3492789Sahrens 	if (verbose) {
3493789Sahrens 		char buf1[64];
3494789Sahrens 		char buf2[64];
3495789Sahrens 		uint64_t bytes = zc.zc_cookie;
3496789Sahrens 		time_t delta = time(NULL) - begin_time;
3497789Sahrens 		if (delta == 0)
3498789Sahrens 			delta = 1;
3499789Sahrens 		zfs_nicenum(bytes, buf1, sizeof (buf1));
3500789Sahrens 		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3501789Sahrens 
35024603Sahrens 		(void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3503789Sahrens 		    buf1, delta, buf2);
3504789Sahrens 	}
35052665Snd150628 
3506789Sahrens 	return (0);
3507789Sahrens }
3508789Sahrens 
3509789Sahrens /*
35101294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
35111294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
35121294Slling  * is a dependent and we should just destroy it without checking the transaction
35131294Slling  * group.
3514789Sahrens  */
35151294Slling typedef struct rollback_data {
35161294Slling 	const char	*cb_target;		/* the snapshot */
35171294Slling 	uint64_t	cb_create;		/* creation time reference */
35181294Slling 	prop_changelist_t *cb_clp;		/* changelist pointer */
35191294Slling 	int		cb_error;
35202082Seschrock 	boolean_t	cb_dependent;
35211294Slling } rollback_data_t;
35221294Slling 
35231294Slling static int
35241294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
35251294Slling {
35261294Slling 	rollback_data_t *cbp = data;
35271294Slling 
35281294Slling 	if (!cbp->cb_dependent) {
35291294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
35301294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
35311294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
35321294Slling 		    cbp->cb_create) {
35334543Smarks 			char *logstr;
35341294Slling 
35352082Seschrock 			cbp->cb_dependent = B_TRUE;
35362474Seschrock 			if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy,
35372474Seschrock 			    cbp) != 0)
35382474Seschrock 				cbp->cb_error = 1;
35392082Seschrock 			cbp->cb_dependent = B_FALSE;
35401294Slling 
35414543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
35424543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
35431294Slling 			if (zfs_destroy(zhp) != 0)
35441294Slling 				cbp->cb_error = 1;
35451294Slling 			else
35461294Slling 				changelist_remove(zhp, cbp->cb_clp);
35474543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
35481294Slling 		}
35491294Slling 	} else {
35501294Slling 		if (zfs_destroy(zhp) != 0)
35511294Slling 			cbp->cb_error = 1;
35521294Slling 		else
35531294Slling 			changelist_remove(zhp, cbp->cb_clp);
35541294Slling 	}
35551294Slling 
35561294Slling 	zfs_close(zhp);
35571294Slling 	return (0);
35581294Slling }
35591294Slling 
35601294Slling /*
35611294Slling  * Rollback the dataset to its latest snapshot.
35621294Slling  */
35631294Slling static int
35641294Slling do_rollback(zfs_handle_t *zhp)
3565789Sahrens {
3566789Sahrens 	int ret;
3567789Sahrens 	zfs_cmd_t zc = { 0 };
3568789Sahrens 
3569789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3570789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3571789Sahrens 
3572789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME &&
35732082Seschrock 	    zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3574789Sahrens 		return (-1);
3575789Sahrens 
3576789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3577789Sahrens 
35782676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3579789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3580789Sahrens 	else
3581789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3582789Sahrens 
3583789Sahrens 	/*
3584789Sahrens 	 * We rely on the consumer to verify that there are no newer snapshots
3585789Sahrens 	 * for the given dataset.  Given these constraints, we can simply pass
3586789Sahrens 	 * the name on to the ioctl() call.  There is still an unlikely race
3587789Sahrens 	 * condition where the user has taken a snapshot since we verified that
3588789Sahrens 	 * this was the most recent.
3589789Sahrens 	 */
35904543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
35913237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
35922082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
35932082Seschrock 		    zhp->zfs_name);
3594789Sahrens 	} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
35952082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3596789Sahrens 	}
3597789Sahrens 
3598789Sahrens 	return (ret);
3599789Sahrens }
3600789Sahrens 
3601789Sahrens /*
36021294Slling  * Given a dataset, rollback to a specific snapshot, discarding any
36031294Slling  * data changes since then and making it the active dataset.
36041294Slling  *
36051294Slling  * Any snapshots more recent than the target are destroyed, along with
36061294Slling  * their dependents.
36071294Slling  */
36081294Slling int
36091294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag)
36101294Slling {
36111294Slling 	int ret;
36121294Slling 	rollback_data_t cb = { 0 };
36131294Slling 	prop_changelist_t *clp;
36141294Slling 
36151294Slling 	/*
36161294Slling 	 * Unmount all dependendents of the dataset and the dataset itself.
36171294Slling 	 * The list we need to gather is the same as for doing rename
36181294Slling 	 */
36191294Slling 	clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0);
36201294Slling 	if (clp == NULL)
36211294Slling 		return (-1);
36221294Slling 
36231294Slling 	if ((ret = changelist_prefix(clp)) != 0)
36241294Slling 		goto out;
36251294Slling 
36261294Slling 	/*
36271294Slling 	 * Destroy all recent snapshots and its dependends.
36281294Slling 	 */
36291294Slling 	cb.cb_target = snap->zfs_name;
36301294Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
36311294Slling 	cb.cb_clp = clp;
36321294Slling 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
36331294Slling 
36341294Slling 	if ((ret = cb.cb_error) != 0) {
36351294Slling 		(void) changelist_postfix(clp);
36361294Slling 		goto out;
36371294Slling 	}
36381294Slling 
36391294Slling 	/*
36401294Slling 	 * Now that we have verified that the snapshot is the latest,
36411294Slling 	 * rollback to the given snapshot.
36421294Slling 	 */
36431294Slling 	ret = do_rollback(zhp);
36441294Slling 
36451294Slling 	if (ret != 0) {
36461294Slling 		(void) changelist_postfix(clp);
36471294Slling 		goto out;
36481294Slling 	}
36491294Slling 
36501294Slling 	/*
36511294Slling 	 * We only want to re-mount the filesystem if it was mounted in the
36521294Slling 	 * first place.
36531294Slling 	 */
36541294Slling 	ret = changelist_postfix(clp);
36551294Slling 
36561294Slling out:
36571294Slling 	changelist_free(clp);
36581294Slling 	return (ret);
36591294Slling }
36601294Slling 
36611294Slling /*
3662789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3663789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3664789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3665789Sahrens  * libzfs_graph.c.
3666789Sahrens  */
3667789Sahrens int
36682474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36692474Seschrock     zfs_iter_f func, void *data)
3670789Sahrens {
3671789Sahrens 	char **dependents;
3672789Sahrens 	size_t count;
3673789Sahrens 	int i;
3674789Sahrens 	zfs_handle_t *child;
3675789Sahrens 	int ret = 0;
3676789Sahrens 
36772474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
36782474Seschrock 	    &dependents, &count) != 0)
36792474Seschrock 		return (-1);
36802474Seschrock 
3681789Sahrens 	for (i = 0; i < count; i++) {
36822082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
36832082Seschrock 		    dependents[i])) == NULL)
3684789Sahrens 			continue;
3685789Sahrens 
3686789Sahrens 		if ((ret = func(child, data)) != 0)
3687789Sahrens 			break;
3688789Sahrens 	}
3689789Sahrens 
3690789Sahrens 	for (i = 0; i < count; i++)
3691789Sahrens 		free(dependents[i]);
3692789Sahrens 	free(dependents);
3693789Sahrens 
3694789Sahrens 	return (ret);
3695789Sahrens }
3696789Sahrens 
3697789Sahrens /*
3698789Sahrens  * Renames the given dataset.
3699789Sahrens  */
3700789Sahrens int
37014490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3702789Sahrens {
3703789Sahrens 	int ret;
3704789Sahrens 	zfs_cmd_t zc = { 0 };
3705789Sahrens 	char *delim;
37064007Smmusante 	prop_changelist_t *cl = NULL;
37074007Smmusante 	zfs_handle_t *zhrp = NULL;
37084007Smmusante 	char *parentname = NULL;
3709789Sahrens 	char parent[ZFS_MAXNAMELEN];
37102082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37112082Seschrock 	char errbuf[1024];
3712789Sahrens 
3713789Sahrens 	/* if we have the same exact name, just return success */
3714789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3715789Sahrens 		return (0);
3716789Sahrens 
37172082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37182082Seschrock 	    "cannot rename to '%s'"), target);
37192082Seschrock 
3720789Sahrens 	/*
3721789Sahrens 	 * Make sure the target name is valid
3722789Sahrens 	 */
3723789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
37242665Snd150628 		if ((strchr(target, '@') == NULL) ||
37252665Snd150628 		    *target == '@') {
37262665Snd150628 			/*
37272665Snd150628 			 * Snapshot target name is abbreviated,
37282665Snd150628 			 * reconstruct full dataset name
37292665Snd150628 			 */
37302665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
37312665Snd150628 			    sizeof (parent));
37322665Snd150628 			delim = strchr(parent, '@');
37332665Snd150628 			if (strchr(target, '@') == NULL)
37342665Snd150628 				*(++delim) = '\0';
37352665Snd150628 			else
37362665Snd150628 				*delim = '\0';
37372665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
37382665Snd150628 			target = parent;
37392665Snd150628 		} else {
37402665Snd150628 			/*
37412665Snd150628 			 * Make sure we're renaming within the same dataset.
37422665Snd150628 			 */
37432665Snd150628 			delim = strchr(target, '@');
37442665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
37452665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
37462665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37472665Snd150628 				    "snapshots must be part of same "
37482665Snd150628 				    "dataset"));
37492665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
37503912Slling 				    errbuf));
37512665Snd150628 			}
3752789Sahrens 		}
37532665Snd150628 		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
37542665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3755789Sahrens 	} else {
37564007Smmusante 		if (recursive) {
37574007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37584007Smmusante 			    "recursive rename must be a snapshot"));
37594007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
37604007Smmusante 		}
37614007Smmusante 
37622665Snd150628 		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
37632665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37642676Seschrock 		uint64_t unused;
37652676Seschrock 
3766789Sahrens 		/* validate parents */
37674490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3768789Sahrens 			return (-1);
3769789Sahrens 
3770789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3771789Sahrens 
3772789Sahrens 		/* make sure we're in the same pool */
3773789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3774789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3775789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
37762082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37772082Seschrock 			    "datasets must be within same pool"));
37782082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3779789Sahrens 		}
37802440Snd150628 
37812440Snd150628 		/* new name cannot be a child of the current dataset name */
37822440Snd150628 		if (strncmp(parent, zhp->zfs_name,
37833912Slling 		    strlen(zhp->zfs_name)) == 0) {
37842440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37852440Snd150628 			    "New dataset name cannot be a descendent of "
37862440Snd150628 			    "current dataset name"));
37872440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37882440Snd150628 		}
3789789Sahrens 	}
3790789Sahrens 
37912082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
37922082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
37932082Seschrock 
3794789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3795789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
37962082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37972082Seschrock 		    "dataset is used in a non-global zone"));
37982082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3799789Sahrens 	}
3800789Sahrens 
38014007Smmusante 	if (recursive) {
38024007Smmusante 		struct destroydata dd;
38034007Smmusante 
38044183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
38054183Smmusante 		if (parentname == NULL) {
38064183Smmusante 			ret = -1;
38074183Smmusante 			goto error;
38084183Smmusante 		}
38094007Smmusante 		delim = strchr(parentname, '@');
38104007Smmusante 		*delim = '\0';
38115094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
38124007Smmusante 		if (zhrp == NULL) {
38134183Smmusante 			ret = -1;
38144183Smmusante 			goto error;
38154007Smmusante 		}
38164007Smmusante 
38174007Smmusante 		dd.snapname = delim + 1;
38184007Smmusante 		dd.gotone = B_FALSE;
38194183Smmusante 		dd.closezhp = B_TRUE;
38204007Smmusante 
38214007Smmusante 		/* We remove any zvol links prior to renaming them */
38224007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
38234007Smmusante 		if (ret) {
38244007Smmusante 			goto error;
38254007Smmusante 		}
38264007Smmusante 	} else {
38274007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
38284007Smmusante 			return (-1);
38294007Smmusante 
38304007Smmusante 		if (changelist_haszonedchild(cl)) {
38314007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38324007Smmusante 			    "child dataset with inherited mountpoint is used "
38334007Smmusante 			    "in a non-global zone"));
38344007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
38354007Smmusante 			goto error;
38364007Smmusante 		}
38374007Smmusante 
38384007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
38394007Smmusante 			goto error;
3840789Sahrens 	}
3841789Sahrens 
38422676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3843789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3844789Sahrens 	else
3845789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3846789Sahrens 
38472665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
38482676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
38492665Snd150628 
38504007Smmusante 	zc.zc_cookie = recursive;
38514007Smmusante 
38524543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
38534007Smmusante 		/*
38544007Smmusante 		 * if it was recursive, the one that actually failed will
38554007Smmusante 		 * be in zc.zc_name
38564007Smmusante 		 */
38574007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38584007Smmusante 		    "cannot rename to '%s'"), zc.zc_name);
38594007Smmusante 
38604007Smmusante 		if (recursive && errno == EEXIST) {
38614007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38624007Smmusante 			    "a child dataset already has a snapshot "
38634007Smmusante 			    "with the new name"));
38644801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38654007Smmusante 		} else {
38664007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
38674007Smmusante 		}
3868789Sahrens 
3869789Sahrens 		/*
3870789Sahrens 		 * On failure, we still want to remount any filesystems that
3871789Sahrens 		 * were previously mounted, so we don't alter the system state.
3872789Sahrens 		 */
38734007Smmusante 		if (recursive) {
38744007Smmusante 			struct createdata cd;
38754007Smmusante 
38764007Smmusante 			/* only create links for datasets that had existed */
38774007Smmusante 			cd.cd_snapname = delim + 1;
38784007Smmusante 			cd.cd_ifexists = B_TRUE;
38794007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38804007Smmusante 			    &cd);
38814007Smmusante 		} else {
38824007Smmusante 			(void) changelist_postfix(cl);
38834007Smmusante 		}
3884789Sahrens 	} else {
38854007Smmusante 		if (recursive) {
38864007Smmusante 			struct createdata cd;
38874007Smmusante 
38884007Smmusante 			/* only create links for datasets that had existed */
38894007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
38904007Smmusante 			cd.cd_ifexists = B_TRUE;
38914007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38924007Smmusante 			    &cd);
38934007Smmusante 		} else {
38944007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
38954007Smmusante 			ret = changelist_postfix(cl);
38964007Smmusante 		}
3897789Sahrens 	}
3898789Sahrens 
3899789Sahrens error:
39004007Smmusante 	if (parentname) {
39014007Smmusante 		free(parentname);
39024007Smmusante 	}
39034007Smmusante 	if (zhrp) {
39044007Smmusante 		zfs_close(zhrp);
39054007Smmusante 	}
39064007Smmusante 	if (cl) {
39074007Smmusante 		changelist_free(cl);
39084007Smmusante 	}
3909789Sahrens 	return (ret);
3910789Sahrens }
3911789Sahrens 
3912789Sahrens /*
3913789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3914789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3915789Sahrens  */
3916789Sahrens int
39172082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3918789Sahrens {
39194007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
39204007Smmusante }
39214007Smmusante 
39224007Smmusante static int
39234007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
39244007Smmusante {
3925789Sahrens 	zfs_cmd_t zc = { 0 };
39262082Seschrock 	di_devlink_handle_t dhdl;
39274543Smarks 	priv_set_t *priv_effective;
39284543Smarks 	int privileged;
3929789Sahrens 
3930789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3931789Sahrens 
3932789Sahrens 	/*
3933789Sahrens 	 * Issue the appropriate ioctl.
3934789Sahrens 	 */
39352082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3936789Sahrens 		switch (errno) {
3937789Sahrens 		case EEXIST:
3938789Sahrens 			/*
3939789Sahrens 			 * Silently ignore the case where the link already
3940789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3941789Sahrens 			 * times without errors.
3942789Sahrens 			 */
3943789Sahrens 			return (0);
3944789Sahrens 
39454007Smmusante 		case ENOENT:
39464007Smmusante 			/*
39474007Smmusante 			 * Dataset does not exist in the kernel.  If we
39484007Smmusante 			 * don't care (see zfs_rename), then ignore the
39494007Smmusante 			 * error quietly.
39504007Smmusante 			 */
39514007Smmusante 			if (ifexists) {
39524007Smmusante 				return (0);
39534007Smmusante 			}
39544007Smmusante 
39554007Smmusante 			/* FALLTHROUGH */
39564007Smmusante 
3957789Sahrens 		default:
39583237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39592082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39602082Seschrock 			    "for '%s'"), dataset));
3961789Sahrens 		}
3962789Sahrens 	}
3963789Sahrens 
3964789Sahrens 	/*
39654543Smarks 	 * If privileged call devfsadm and wait for the links to
39664543Smarks 	 * magically appear.
39674543Smarks 	 * Otherwise, print out an informational message.
3968789Sahrens 	 */
39694543Smarks 
39704543Smarks 	priv_effective = priv_allocset();
39714543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
39724543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
39734543Smarks 	priv_freeset(priv_effective);
39744543Smarks 
39754543Smarks 	if (privileged) {
39764543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
39774543Smarks 		    DI_MAKE_LINK)) == NULL) {
39784543Smarks 			zfs_error_aux(hdl, strerror(errno));
39794543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
39804543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39814543Smarks 			    "for '%s'"), dataset);
39824543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
39834543Smarks 			return (-1);
39844543Smarks 		} else {
39854543Smarks 			(void) di_devlink_fini(&dhdl);
39864543Smarks 		}
3987789Sahrens 	} else {
39884543Smarks 		char pathname[MAXPATHLEN];
39894543Smarks 		struct stat64 statbuf;
39904543Smarks 		int i;
39914543Smarks 
39924543Smarks #define	MAX_WAIT	10
39934543Smarks 
39944543Smarks 		/*
39954543Smarks 		 * This is the poor mans way of waiting for the link
39964543Smarks 		 * to show up.  If after 10 seconds we still don't
39974543Smarks 		 * have it, then print out a message.
39984543Smarks 		 */
39994543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
40004543Smarks 		    dataset);
40014543Smarks 
40024543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
40034543Smarks 			if (stat64(pathname, &statbuf) == 0)
40044543Smarks 				break;
40054543Smarks 			(void) sleep(1);
40064543Smarks 		}
40074543Smarks 		if (i == MAX_WAIT)
40084543Smarks 			(void) printf(gettext("%s may not be immediately "
40094543Smarks 			    "available\n"), pathname);
4010789Sahrens 	}
4011789Sahrens 
4012789Sahrens 	return (0);
4013789Sahrens }
4014789Sahrens 
4015789Sahrens /*
4016789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4017789Sahrens  */
4018789Sahrens int
40192082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4020789Sahrens {
4021789Sahrens 	zfs_cmd_t zc = { 0 };
4022789Sahrens 
4023789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4024789Sahrens 
40252082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4026789Sahrens 		switch (errno) {
4027789Sahrens 		case ENXIO:
4028789Sahrens 			/*
4029789Sahrens 			 * Silently ignore the case where the link no longer
4030789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4031789Sahrens 			 * times without errors.
4032789Sahrens 			 */
4033789Sahrens 			return (0);
4034789Sahrens 
4035789Sahrens 		default:
40363237Slling 			return (zfs_standard_error_fmt(hdl, errno,
40372082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
40382082Seschrock 			    "links for '%s'"), dataset));
4039789Sahrens 		}
4040789Sahrens 	}
4041789Sahrens 
4042789Sahrens 	return (0);
4043789Sahrens }
40442676Seschrock 
40452676Seschrock nvlist_t *
40462676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
40472676Seschrock {
40482676Seschrock 	return (zhp->zfs_user_props);
40492676Seschrock }
40502676Seschrock 
40512676Seschrock /*
40523912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
40533912Slling  * display, and their maximum widths.  This does two main things:
40543912Slling  *
40553912Slling  *      - If this is a list of all properties, then expand the list to include
40563912Slling  *        all native properties, and set a flag so that for each dataset we look
40573912Slling  *        for new unique user properties and add them to the list.
40583912Slling  *
40593912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
40603912Slling  *        so that we can size the column appropriately.
40613912Slling  */
40623912Slling int
40635094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
40643912Slling {
40653912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40665094Slling 	zprop_list_t *entry;
40675094Slling 	zprop_list_t **last, **start;
40683912Slling 	nvlist_t *userprops, *propval;
40693912Slling 	nvpair_t *elem;
40703912Slling 	char *strval;
40713912Slling 	char buf[ZFS_MAXPROPLEN];
40723912Slling 
40735094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
40743912Slling 		return (-1);
40752676Seschrock 
40762676Seschrock 	userprops = zfs_get_user_props(zhp);
40772676Seschrock 
40782676Seschrock 	entry = *plp;
40792676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
40802676Seschrock 		/*
40812676Seschrock 		 * Go through and add any user properties as necessary.  We
40822676Seschrock 		 * start by incrementing our list pointer to the first
40832676Seschrock 		 * non-native property.
40842676Seschrock 		 */
40852676Seschrock 		start = plp;
40862676Seschrock 		while (*start != NULL) {
40875094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
40882676Seschrock 				break;
40892676Seschrock 			start = &(*start)->pl_next;
40902676Seschrock 		}
40912676Seschrock 
40922676Seschrock 		elem = NULL;
40932676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
40942676Seschrock 			/*
40952676Seschrock 			 * See if we've already found this property in our list.
40962676Seschrock 			 */
40972676Seschrock 			for (last = start; *last != NULL;
40982676Seschrock 			    last = &(*last)->pl_next) {
40992676Seschrock 				if (strcmp((*last)->pl_user_prop,
41002676Seschrock 				    nvpair_name(elem)) == 0)
41012676Seschrock 					break;
41022676Seschrock 			}
41032676Seschrock 
41042676Seschrock 			if (*last == NULL) {
41052676Seschrock 				if ((entry = zfs_alloc(hdl,
41065094Slling 				    sizeof (zprop_list_t))) == NULL ||
41072676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
41082676Seschrock 				    nvpair_name(elem)))) == NULL) {
41092676Seschrock 					free(entry);
41102676Seschrock 					return (-1);
41112676Seschrock 				}
41122676Seschrock 
41135094Slling 				entry->pl_prop = ZPROP_INVAL;
41142676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
41152676Seschrock 				entry->pl_all = B_TRUE;
41162676Seschrock 				*last = entry;
41172676Seschrock 			}
41182676Seschrock 		}
41192676Seschrock 	}
41202676Seschrock 
41212676Seschrock 	/*
41222676Seschrock 	 * Now go through and check the width of any non-fixed columns
41232676Seschrock 	 */
41242676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
41252676Seschrock 		if (entry->pl_fixed)
41262676Seschrock 			continue;
41272676Seschrock 
41285094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
41292676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
41302676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
41312676Seschrock 				if (strlen(buf) > entry->pl_width)
41322676Seschrock 					entry->pl_width = strlen(buf);
41332676Seschrock 			}
41342676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
41352676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
41362676Seschrock 			verify(nvlist_lookup_string(propval,
41375094Slling 			    ZPROP_VALUE, &strval) == 0);
41382676Seschrock 			if (strlen(strval) > entry->pl_width)
41392676Seschrock 				entry->pl_width = strlen(strval);
41402676Seschrock 		}
41412676Seschrock 	}
41422676Seschrock 
41432676Seschrock 	return (0);
41442676Seschrock }
41454543Smarks 
41464543Smarks int
41474543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
41484543Smarks {
41494543Smarks 	zfs_cmd_t zc = { 0 };
41504543Smarks 	nvlist_t *nvp;
41514543Smarks 	gid_t gid;
41524543Smarks 	uid_t uid;
41534543Smarks 	const gid_t *groups;
41544543Smarks 	int group_cnt;
41554543Smarks 	int error;
41564543Smarks 
41574543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
41584543Smarks 		return (no_memory(hdl));
41594543Smarks 
41604543Smarks 	uid = ucred_geteuid(cred);
41614543Smarks 	gid = ucred_getegid(cred);
41624543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
41634543Smarks 
41644543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
41654543Smarks 		return (1);
41664543Smarks 
41674543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
41684543Smarks 		nvlist_free(nvp);
41694543Smarks 		return (1);
41704543Smarks 	}
41714543Smarks 
41724543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
41734543Smarks 		nvlist_free(nvp);
41744543Smarks 		return (1);
41754543Smarks 	}
41764543Smarks 
41774543Smarks 	if (nvlist_add_uint32_array(nvp,
41784543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
41794543Smarks 		nvlist_free(nvp);
41804543Smarks 		return (1);
41814543Smarks 	}
41824543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
41834543Smarks 
41845094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
41854543Smarks 		return (-1);
41864543Smarks 
41874543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
41884543Smarks 	nvlist_free(nvp);
41894543Smarks 	return (error);
41904543Smarks }
41914543Smarks 
41924543Smarks int
41934543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
41944543Smarks     void *export, void *sharetab, int sharemax, boolean_t share_on)
41954543Smarks {
41964543Smarks 	zfs_cmd_t zc = { 0 };
41974543Smarks 	int error;
41984543Smarks 
41994543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42004543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
42014543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
42024543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
42034543Smarks 	zc.zc_share.z_sharetype = share_on;
42044543Smarks 	zc.zc_share.z_sharemax = sharemax;
42054543Smarks 
42064543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
42074543Smarks 	return (error);
42084543Smarks }
4209