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>
395367Sahrens #include <stddef.h>
40789Sahrens #include <zone.h>
412082Seschrock #include <fcntl.h>
42789Sahrens #include <sys/mntent.h>
43789Sahrens #include <sys/mnttab.h>
441294Slling #include <sys/mount.h>
454543Smarks #include <sys/avl.h>
464543Smarks #include <priv.h>
474543Smarks #include <pwd.h>
484543Smarks #include <grp.h>
494543Smarks #include <stddef.h>
504543Smarks #include <ucred.h>
51789Sahrens 
52789Sahrens #include <sys/spa.h>
532676Seschrock #include <sys/zap.h>
54789Sahrens #include <libzfs.h>
55789Sahrens 
56789Sahrens #include "zfs_namecheck.h"
57789Sahrens #include "zfs_prop.h"
58789Sahrens #include "libzfs_impl.h"
594543Smarks #include "zfs_deleg.h"
60789Sahrens 
614007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
624007Smmusante 
63789Sahrens /*
64789Sahrens  * Given a single type (not a mask of types), return the type in a human
65789Sahrens  * readable form.
66789Sahrens  */
67789Sahrens const char *
68789Sahrens zfs_type_to_name(zfs_type_t type)
69789Sahrens {
70789Sahrens 	switch (type) {
71789Sahrens 	case ZFS_TYPE_FILESYSTEM:
72789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73789Sahrens 	case ZFS_TYPE_SNAPSHOT:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75789Sahrens 	case ZFS_TYPE_VOLUME:
76789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
77789Sahrens 	}
78789Sahrens 
79789Sahrens 	return (NULL);
80789Sahrens }
81789Sahrens 
82789Sahrens /*
83789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
84789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
85789Sahrens  * We guess what the type would have been based on the path and the mask of
86789Sahrens  * acceptable types.
87789Sahrens  */
88789Sahrens static const char *
89789Sahrens path_to_str(const char *path, int types)
90789Sahrens {
91789Sahrens 	/*
92789Sahrens 	 * When given a single type, always report the exact type.
93789Sahrens 	 */
94789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
95789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98789Sahrens 	if (types == ZFS_TYPE_VOLUME)
99789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
100789Sahrens 
101789Sahrens 	/*
102789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
103789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
104789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105789Sahrens 	 * snapshot attribute and try again.
106789Sahrens 	 */
107789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
108789Sahrens 		if (strchr(path, '@') != NULL)
109789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111789Sahrens 	}
112789Sahrens 
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * The user has requested either filesystems or volumes.
116789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118789Sahrens 	 */
119789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121789Sahrens 
122789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124789Sahrens }
125789Sahrens 
126789Sahrens /*
127789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128789Sahrens  * provide a more meaningful error message.  We place a more useful message in
129789Sahrens  * 'buf' detailing exactly why the name was not valid.
130789Sahrens  */
131789Sahrens static int
1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1335326Sek110237     boolean_t modifying)
134789Sahrens {
135789Sahrens 	namecheck_err_t why;
136789Sahrens 	char what;
137789Sahrens 
138789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1392082Seschrock 		if (hdl != NULL) {
140789Sahrens 			switch (why) {
1411003Slling 			case NAME_ERR_TOOLONG:
1422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432082Seschrock 				    "name is too long"));
1441003Slling 				break;
1451003Slling 
146789Sahrens 			case NAME_ERR_LEADING_SLASH:
1472082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482082Seschrock 				    "leading slash in name"));
149789Sahrens 				break;
150789Sahrens 
151789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532082Seschrock 				    "empty component in name"));
154789Sahrens 				break;
155789Sahrens 
156789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1572082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582082Seschrock 				    "trailing slash in name"));
159789Sahrens 				break;
160789Sahrens 
161789Sahrens 			case NAME_ERR_INVALCHAR:
1622082Seschrock 				zfs_error_aux(hdl,
163789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1642082Seschrock 				    "'%c' in name"), what);
165789Sahrens 				break;
166789Sahrens 
167789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1682082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692082Seschrock 				    "multiple '@' delimiters in name"));
170789Sahrens 				break;
1712856Snd150628 
1722856Snd150628 			case NAME_ERR_NOLETTER:
1732856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1742856Snd150628 				    "pool doesn't begin with a letter"));
1752856Snd150628 				break;
1762856Snd150628 
1772856Snd150628 			case NAME_ERR_RESERVED:
1782856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1792856Snd150628 				    "name is reserved"));
1802856Snd150628 				break;
1812856Snd150628 
1822856Snd150628 			case NAME_ERR_DISKLIKE:
1832856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1842856Snd150628 				    "reserved disk name"));
1852856Snd150628 				break;
186789Sahrens 			}
187789Sahrens 		}
188789Sahrens 
189789Sahrens 		return (0);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1932082Seschrock 		if (hdl != NULL)
1942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1952082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
196789Sahrens 		return (0);
197789Sahrens 	}
198789Sahrens 
1992199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2002199Sahrens 		if (hdl != NULL)
2012199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2023413Smmusante 			    "missing '@' delimiter in snapshot name"));
2032199Sahrens 		return (0);
2042199Sahrens 	}
2052199Sahrens 
2065326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2075326Sek110237 		if (hdl != NULL)
2085326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2095326Sek110237 			    "invalid character %c in name"), '%');
2105326Sek110237 		return (0);
2115326Sek110237 	}
2125326Sek110237 
2132082Seschrock 	return (-1);
214789Sahrens }
215789Sahrens 
216789Sahrens int
217789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218789Sahrens {
2195326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
220789Sahrens }
221789Sahrens 
222789Sahrens /*
2232676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2242676Seschrock  * properties into a separate nvlist.
2252676Seschrock  */
2264217Seschrock static nvlist_t *
2274217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2282676Seschrock {
2292676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2302676Seschrock 	nvpair_t *elem;
2312676Seschrock 	nvlist_t *propval;
2324217Seschrock 	nvlist_t *nvl;
2334217Seschrock 
2344217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2354217Seschrock 		(void) no_memory(hdl);
2364217Seschrock 		return (NULL);
2374217Seschrock 	}
2382676Seschrock 
2392676Seschrock 	elem = NULL;
2404217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2412676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2422676Seschrock 			continue;
2432676Seschrock 
2442676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2454217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2464217Seschrock 			nvlist_free(nvl);
2474217Seschrock 			(void) no_memory(hdl);
2484217Seschrock 			return (NULL);
2494217Seschrock 		}
2502676Seschrock 	}
2512676Seschrock 
2524217Seschrock 	return (nvl);
2532676Seschrock }
2542676Seschrock 
2552676Seschrock /*
256789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
257789Sahrens  */
258789Sahrens static int
259789Sahrens get_stats(zfs_handle_t *zhp)
260789Sahrens {
261789Sahrens 	zfs_cmd_t zc = { 0 };
2622676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2634217Seschrock 	nvlist_t *allprops, *userprops;
264789Sahrens 
265789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
266789Sahrens 
2672676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
2682082Seschrock 		return (-1);
2691356Seschrock 
2702082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
2711356Seschrock 		if (errno == ENOMEM) {
2722676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2732676Seschrock 				zcmd_free_nvlists(&zc);
2742082Seschrock 				return (-1);
2752676Seschrock 			}
2761356Seschrock 		} else {
2772676Seschrock 			zcmd_free_nvlists(&zc);
2781356Seschrock 			return (-1);
2791356Seschrock 		}
2801356Seschrock 	}
281789Sahrens 
2822885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
283789Sahrens 
2842676Seschrock 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
2851544Seschrock 
2864217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
2872676Seschrock 		zcmd_free_nvlists(&zc);
2882082Seschrock 		return (-1);
2892082Seschrock 	}
290789Sahrens 
2912676Seschrock 	zcmd_free_nvlists(&zc);
2922676Seschrock 
2934217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
2944217Seschrock 		nvlist_free(allprops);
2952676Seschrock 		return (-1);
2964217Seschrock 	}
2974217Seschrock 
2984217Seschrock 	nvlist_free(zhp->zfs_props);
2994217Seschrock 	nvlist_free(zhp->zfs_user_props);
3004217Seschrock 
3014217Seschrock 	zhp->zfs_props = allprops;
3024217Seschrock 	zhp->zfs_user_props = userprops;
3032082Seschrock 
304789Sahrens 	return (0);
305789Sahrens }
306789Sahrens 
307789Sahrens /*
308789Sahrens  * Refresh the properties currently stored in the handle.
309789Sahrens  */
310789Sahrens void
311789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
312789Sahrens {
313789Sahrens 	(void) get_stats(zhp);
314789Sahrens }
315789Sahrens 
316789Sahrens /*
317789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
318789Sahrens  * zfs_iter_* to create child handles on the fly.
319789Sahrens  */
320789Sahrens zfs_handle_t *
3212082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
322789Sahrens {
3232082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3244543Smarks 	char *logstr;
3252082Seschrock 
3262082Seschrock 	if (zhp == NULL)
3272082Seschrock 		return (NULL);
3282082Seschrock 
3292082Seschrock 	zhp->zfs_hdl = hdl;
330789Sahrens 
3314543Smarks 	/*
3324543Smarks 	 * Preserve history log string.
3334543Smarks 	 * any changes performed here will be
3344543Smarks 	 * logged as an internal event.
3354543Smarks 	 */
3364543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3374543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3381758Sahrens top:
339789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
340789Sahrens 
341789Sahrens 	if (get_stats(zhp) != 0) {
3424543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
343789Sahrens 		free(zhp);
344789Sahrens 		return (NULL);
345789Sahrens 	}
346789Sahrens 
3471758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
3481758Sahrens 		zfs_cmd_t zc = { 0 };
3491758Sahrens 
3501758Sahrens 		/*
3511758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
3521758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
3531758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
3541758Sahrens 		 * can't be mounted.  However, it could also be that we
3551758Sahrens 		 * have crashed in the middle of one of those
3561758Sahrens 		 * operations, in which case we need to get rid of the
3571758Sahrens 		 * inconsistent state.  We do that by either rolling
3581758Sahrens 		 * back to the previous snapshot (which will fail if
3591758Sahrens 		 * there is none), or destroying the filesystem.  Note
3601758Sahrens 		 * that if we are still in the middle of an active
3611758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
3621758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
3631758Sahrens 		 */
3641758Sahrens 
3651758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3661758Sahrens 
3672885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
3682082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
3691758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
3701758Sahrens 		} else {
3711758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
3721758Sahrens 		}
3731758Sahrens 
3741758Sahrens 		/*
3755331Samw 		 * If we can successfully destroy it, pretend that it
3761758Sahrens 		 * never existed.
3771758Sahrens 		 */
3782082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
3794543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3801758Sahrens 			free(zhp);
3811758Sahrens 			errno = ENOENT;
3821758Sahrens 			return (NULL);
3831758Sahrens 		}
3845367Sahrens 		/* If we can successfully roll it back, reget the stats */
3855367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
3865367Sahrens 			goto top;
3871758Sahrens 	}
3881758Sahrens 
389789Sahrens 	/*
390789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
391789Sahrens 	 * the high-level type.
392789Sahrens 	 */
3932885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
3942885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
3952885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
3962885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
3972885Sahrens 	else
3982885Sahrens 		abort();
3992885Sahrens 
400789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
401789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
402789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
403789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
404789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
405789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
406789Sahrens 	else
4072082Seschrock 		abort();	/* we should never see any other types */
408789Sahrens 
4094543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
410789Sahrens 	return (zhp);
411789Sahrens }
412789Sahrens 
413789Sahrens /*
414789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
415789Sahrens  * argument is a mask of acceptable types.  The function will print an
416789Sahrens  * appropriate error message and return NULL if it can't be opened.
417789Sahrens  */
418789Sahrens zfs_handle_t *
4192082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
420789Sahrens {
421789Sahrens 	zfs_handle_t *zhp;
4222082Seschrock 	char errbuf[1024];
4232082Seschrock 
4242082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4252082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
426789Sahrens 
427789Sahrens 	/*
4282082Seschrock 	 * Validate the name before we even try to open it.
429789Sahrens 	 */
4305326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4312082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4322082Seschrock 		    "invalid dataset name"));
4332082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
434789Sahrens 		return (NULL);
435789Sahrens 	}
436789Sahrens 
437789Sahrens 	/*
438789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
439789Sahrens 	 */
440789Sahrens 	errno = 0;
4412082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
4423237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
443789Sahrens 		return (NULL);
444789Sahrens 	}
445789Sahrens 
446789Sahrens 	if (!(types & zhp->zfs_type)) {
4472082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4482142Seschrock 		zfs_close(zhp);
449789Sahrens 		return (NULL);
450789Sahrens 	}
451789Sahrens 
452789Sahrens 	return (zhp);
453789Sahrens }
454789Sahrens 
455789Sahrens /*
456789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
457789Sahrens  */
458789Sahrens void
459789Sahrens zfs_close(zfs_handle_t *zhp)
460789Sahrens {
461789Sahrens 	if (zhp->zfs_mntopts)
462789Sahrens 		free(zhp->zfs_mntopts);
4632676Seschrock 	nvlist_free(zhp->zfs_props);
4642676Seschrock 	nvlist_free(zhp->zfs_user_props);
465789Sahrens 	free(zhp);
466789Sahrens }
467789Sahrens 
4683912Slling /*
4692676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
4702676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
4712676Seschrock  * strings.
472789Sahrens  */
4735094Slling static nvlist_t *
4745094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
4755094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
476789Sahrens {
4772676Seschrock 	nvpair_t *elem;
4782676Seschrock 	uint64_t intval;
4792676Seschrock 	char *strval;
4805094Slling 	zfs_prop_t prop;
4812676Seschrock 	nvlist_t *ret;
4825331Samw 	int chosen_normal = -1;
4835331Samw 	int chosen_utf = -1;
4842676Seschrock 
4852676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
4862676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4873413Smmusante 		    "snapshot properties cannot be modified"));
4882676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
4895094Slling 		return (NULL);
4905094Slling 	}
4915094Slling 
4925094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
4935094Slling 		(void) no_memory(hdl);
4945094Slling 		return (NULL);
495789Sahrens 	}
496789Sahrens 
4972676Seschrock 	elem = NULL;
4982676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
4995094Slling 		const char *propname = nvpair_name(elem);
5002676Seschrock 
5012676Seschrock 		/*
5022676Seschrock 		 * Make sure this property is valid and applies to this type.
5032676Seschrock 		 */
5045094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
5055094Slling 			if (!zfs_prop_user(propname)) {
5062676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5075094Slling 				    "invalid property '%s'"), propname);
5085094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5095094Slling 				goto error;
5105094Slling 			}
5115094Slling 
5125094Slling 			/*
5135094Slling 			 * If this is a user property, make sure it's a
5145094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
5155094Slling 			 */
5165094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
5175094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5185094Slling 				    "'%s' must be a string"), propname);
5195094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5205094Slling 				goto error;
5215094Slling 			}
5225094Slling 
5235094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
5245094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5255094Slling 				    "property name '%s' is too long"),
5262676Seschrock 				    propname);
5272676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5282676Seschrock 				goto error;
5292676Seschrock 			}
5302676Seschrock 
5312676Seschrock 			(void) nvpair_value_string(elem, &strval);
5322676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
5332676Seschrock 				(void) no_memory(hdl);
5342676Seschrock 				goto error;
5352676Seschrock 			}
5362676Seschrock 			continue;
537789Sahrens 		}
5382676Seschrock 
5392676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
5402676Seschrock 			zfs_error_aux(hdl,
5412676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
5422676Seschrock 			    "apply to datasets of this type"), propname);
5432676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5442676Seschrock 			goto error;
5452676Seschrock 		}
5462676Seschrock 
5472676Seschrock 		if (zfs_prop_readonly(prop) &&
5485331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
5492676Seschrock 			zfs_error_aux(hdl,
5502676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
5512676Seschrock 			    propname);
5522676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
5532676Seschrock 			goto error;
5542676Seschrock 		}
5552676Seschrock 
5565094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
5575094Slling 		    &strval, &intval, errbuf) != 0)
5582676Seschrock 			goto error;
5592676Seschrock 
5602676Seschrock 		/*
5612676Seschrock 		 * Perform some additional checks for specific properties.
5622676Seschrock 		 */
5632676Seschrock 		switch (prop) {
5644577Sahrens 		case ZFS_PROP_VERSION:
5654577Sahrens 		{
5664577Sahrens 			int version;
5674577Sahrens 
5684577Sahrens 			if (zhp == NULL)
5694577Sahrens 				break;
5704577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
5714577Sahrens 			if (intval < version) {
5724577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5734577Sahrens 				    "Can not downgrade; already at version %u"),
5744577Sahrens 				    version);
5754577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5764577Sahrens 				goto error;
5774577Sahrens 			}
5784577Sahrens 			break;
5794577Sahrens 		}
5804577Sahrens 
5812676Seschrock 		case ZFS_PROP_RECORDSIZE:
5822676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
5832676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
5842676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
5852676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
5862082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5872676Seschrock 				    "'%s' must be power of 2 from %u "
5882676Seschrock 				    "to %uk"), propname,
5892676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
5902676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
5912676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5922676Seschrock 				goto error;
593789Sahrens 			}
594789Sahrens 			break;
595789Sahrens 
5963126Sahl 		case ZFS_PROP_SHAREISCSI:
5973126Sahl 			if (strcmp(strval, "off") != 0 &&
5983126Sahl 			    strcmp(strval, "on") != 0 &&
5993126Sahl 			    strcmp(strval, "type=disk") != 0) {
6003126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6013126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6023126Sahl 				    propname);
6033126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6043126Sahl 				goto error;
6053126Sahl 			}
6063126Sahl 
6073126Sahl 			break;
6083126Sahl 
6092676Seschrock 		case ZFS_PROP_MOUNTPOINT:
6104778Srm160521 		{
6114778Srm160521 			namecheck_err_t why;
6124778Srm160521 
6132676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
6142676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
6152676Seschrock 				break;
6162676Seschrock 
6174778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
6184778Srm160521 				switch (why) {
6194778Srm160521 				case NAME_ERR_LEADING_SLASH:
6204778Srm160521 					zfs_error_aux(hdl,
6214778Srm160521 					    dgettext(TEXT_DOMAIN,
6224778Srm160521 					    "'%s' must be an absolute path, "
6234778Srm160521 					    "'none', or 'legacy'"), propname);
6244778Srm160521 					break;
6254778Srm160521 				case NAME_ERR_TOOLONG:
6264778Srm160521 					zfs_error_aux(hdl,
6274778Srm160521 					    dgettext(TEXT_DOMAIN,
6284778Srm160521 					    "component of '%s' is too long"),
6294778Srm160521 					    propname);
6304778Srm160521 					break;
6314778Srm160521 				}
6322676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6332676Seschrock 				goto error;
634789Sahrens 			}
6354778Srm160521 		}
6364778Srm160521 
6373126Sahl 			/*FALLTHRU*/
6383126Sahl 
6395331Samw 		case ZFS_PROP_SHARESMB:
6403126Sahl 		case ZFS_PROP_SHARENFS:
6413126Sahl 			/*
6425331Samw 			 * For the mountpoint and sharenfs or sharesmb
6435331Samw 			 * properties, check if it can be set in a
6445331Samw 			 * global/non-global zone based on
6453126Sahl 			 * the zoned property value:
6463126Sahl 			 *
6473126Sahl 			 *		global zone	    non-global zone
6483126Sahl 			 * --------------------------------------------------
6493126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
6503126Sahl 			 *		sharenfs (no)	    sharenfs (no)
6515331Samw 			 *		sharesmb (no)	    sharesmb (no)
6523126Sahl 			 *
6533126Sahl 			 * zoned=off	mountpoint (yes)	N/A
6543126Sahl 			 *		sharenfs (yes)
6555331Samw 			 *		sharesmb (yes)
6563126Sahl 			 */
6572676Seschrock 			if (zoned) {
6582676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
6592676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6602676Seschrock 					    "'%s' cannot be set on "
6612676Seschrock 					    "dataset in a non-global zone"),
6622676Seschrock 					    propname);
6632676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
6642676Seschrock 					    errbuf);
6652676Seschrock 					goto error;
6665331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
6675331Samw 				    prop == ZFS_PROP_SHARESMB) {
6682676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6692676Seschrock 					    "'%s' cannot be set in "
6702676Seschrock 					    "a non-global zone"), propname);
6712676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
6722676Seschrock 					    errbuf);
6732676Seschrock 					goto error;
6742676Seschrock 				}
6752676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
6762676Seschrock 				/*
6772676Seschrock 				 * If zoned property is 'off', this must be in
6782676Seschrock 				 * a globle zone. If not, something is wrong.
6792676Seschrock 				 */
6802676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6812676Seschrock 				    "'%s' cannot be set while dataset "
6822676Seschrock 				    "'zoned' property is set"), propname);
6832676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
6842676Seschrock 				goto error;
6852676Seschrock 			}
6863126Sahl 
6874180Sdougm 			/*
6884180Sdougm 			 * At this point, it is legitimate to set the
6894180Sdougm 			 * property. Now we want to make sure that the
6904180Sdougm 			 * property value is valid if it is sharenfs.
6914180Sdougm 			 */
6925331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
6935331Samw 			    prop == ZFS_PROP_SHARESMB) &&
6944217Seschrock 			    strcmp(strval, "on") != 0 &&
6954217Seschrock 			    strcmp(strval, "off") != 0) {
6965331Samw 				zfs_share_proto_t proto;
6975331Samw 
6985331Samw 				if (prop == ZFS_PROP_SHARESMB)
6995331Samw 					proto = PROTO_SMB;
7005331Samw 				else
7015331Samw 					proto = PROTO_NFS;
7024180Sdougm 
7034180Sdougm 				/*
7045331Samw 				 * Must be an valid sharing protocol
7055331Samw 				 * option string so init the libshare
7065331Samw 				 * in order to enable the parser and
7075331Samw 				 * then parse the options. We use the
7085331Samw 				 * control API since we don't care about
7095331Samw 				 * the current configuration and don't
7104180Sdougm 				 * want the overhead of loading it
7114180Sdougm 				 * until we actually do something.
7124180Sdougm 				 */
7134180Sdougm 
7144217Seschrock 				if (zfs_init_libshare(hdl,
7154217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
7164217Seschrock 					/*
7174217Seschrock 					 * An error occurred so we can't do
7184217Seschrock 					 * anything
7194217Seschrock 					 */
7204217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7214217Seschrock 					    "'%s' cannot be set: problem "
7224217Seschrock 					    "in share initialization"),
7234217Seschrock 					    propname);
7244217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7254217Seschrock 					    errbuf);
7264217Seschrock 					goto error;
7274217Seschrock 				}
7284217Seschrock 
7295331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
7304217Seschrock 					/*
7314217Seschrock 					 * There was an error in parsing so
7324217Seschrock 					 * deal with it by issuing an error
7334217Seschrock 					 * message and leaving after
7344217Seschrock 					 * uninitializing the the libshare
7354217Seschrock 					 * interface.
7364217Seschrock 					 */
7374217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7384217Seschrock 					    "'%s' cannot be set to invalid "
7394217Seschrock 					    "options"), propname);
7404217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7414217Seschrock 					    errbuf);
7424217Seschrock 					zfs_uninit_libshare(hdl);
7434217Seschrock 					goto error;
7444217Seschrock 				}
7454180Sdougm 				zfs_uninit_libshare(hdl);
7464180Sdougm 			}
7474180Sdougm 
7483126Sahl 			break;
7495331Samw 		case ZFS_PROP_UTF8ONLY:
7505331Samw 			chosen_utf = (int)intval;
7515331Samw 			break;
7525331Samw 		case ZFS_PROP_NORMALIZE:
7535331Samw 			chosen_normal = (int)intval;
7545331Samw 			break;
7552676Seschrock 		}
7562676Seschrock 
7572676Seschrock 		/*
7582676Seschrock 		 * For changes to existing volumes, we have some additional
7592676Seschrock 		 * checks to enforce.
7602676Seschrock 		 */
7612676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
7622676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
7632676Seschrock 			    ZFS_PROP_VOLSIZE);
7642676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
7652676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
7662676Seschrock 			char buf[64];
7672676Seschrock 
7682676Seschrock 			switch (prop) {
7692676Seschrock 			case ZFS_PROP_RESERVATION:
7705378Sck153898 			case ZFS_PROP_REFRESERVATION:
7712676Seschrock 				if (intval > volsize) {
7722676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7732676Seschrock 					    "'%s' is greater than current "
7742676Seschrock 					    "volume size"), propname);
7752676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7762676Seschrock 					    errbuf);
7772676Seschrock 					goto error;
7782676Seschrock 				}
7792676Seschrock 				break;
7802676Seschrock 
7812676Seschrock 			case ZFS_PROP_VOLSIZE:
7822676Seschrock 				if (intval % blocksize != 0) {
7832676Seschrock 					zfs_nicenum(blocksize, buf,
7842676Seschrock 					    sizeof (buf));
7852676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7862676Seschrock 					    "'%s' must be a multiple of "
7872676Seschrock 					    "volume block size (%s)"),
7882676Seschrock 					    propname, buf);
7892676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7902676Seschrock 					    errbuf);
7912676Seschrock 					goto error;
7922676Seschrock 				}
7932676Seschrock 
7942676Seschrock 				if (intval == 0) {
7952676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7962676Seschrock 					    "'%s' cannot be zero"),
7972676Seschrock 					    propname);
7982676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7992676Seschrock 					    errbuf);
8002676Seschrock 					goto error;
801789Sahrens 				}
8023126Sahl 				break;
803789Sahrens 			}
804789Sahrens 		}
805789Sahrens 	}
806789Sahrens 
8072676Seschrock 	/*
8085331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
8095331Samw 	 * enforce rejection of non-UTF8 names.
8105331Samw 	 *
8115331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
8125331Samw 	 * was explicitly not chosen, it is an error.
8135331Samw 	 */
8145498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
8155331Samw 		if (nvlist_add_uint64(ret,
8165331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
8175331Samw 			(void) no_memory(hdl);
8185331Samw 			goto error;
8195331Samw 		}
8205498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
8215331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8225331Samw 		    "'%s' must be set 'on' if normalization chosen"),
8235331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
8245331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8255331Samw 		goto error;
8265331Samw 	}
8275331Samw 
8285331Samw 	/*
8292676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
8302676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
8312676Seschrock 	 */
8322676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
8332676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
8342676Seschrock 	    &intval) == 0) {
8352676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
8362676Seschrock 		    ZFS_PROP_VOLSIZE);
8375481Sck153898 		uint64_t old_reservation;
8382676Seschrock 		uint64_t new_reservation;
8395481Sck153898 		char *pool_name;
8405481Sck153898 		zpool_handle_t *zpool_handle;
8415481Sck153898 		char *p;
8425481Sck153898 		zfs_prop_t resv_prop;
8435481Sck153898 		uint64_t spa_version;
8445481Sck153898 
8455481Sck153898 		pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN);
8465481Sck153898 		if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name,
8475481Sck153898 		    MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) {
8485481Sck153898 			free(pool_name);
8495481Sck153898 			goto error;
8505481Sck153898 		}
8515481Sck153898 
8525481Sck153898 		if (p = strchr(pool_name, '/'))
8535481Sck153898 			*p = '\0';
8545481Sck153898 		zpool_handle = zpool_open(hdl, pool_name);
8555481Sck153898 		free(pool_name);
8565481Sck153898 		if (zpool_handle == NULL)
8575481Sck153898 			goto error;
8585481Sck153898 
8595481Sck153898 		spa_version = zpool_get_prop_int(zpool_handle,
8605481Sck153898 		    ZPOOL_PROP_VERSION, NULL);
8615481Sck153898 		zpool_close(zpool_handle);
8625481Sck153898 		if (spa_version >= SPA_VERSION_REFRESERVATION)
8635481Sck153898 			resv_prop = ZFS_PROP_REFRESERVATION;
8645481Sck153898 		else
8655481Sck153898 			resv_prop = ZFS_PROP_RESERVATION;
8665481Sck153898 
8675481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
8682676Seschrock 
8692676Seschrock 		if (old_volsize == old_reservation &&
8705481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
8712676Seschrock 		    &new_reservation) != 0) {
8722676Seschrock 			if (nvlist_add_uint64(ret,
8735481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
8742676Seschrock 				(void) no_memory(hdl);
8752676Seschrock 				goto error;
8762676Seschrock 			}
8772676Seschrock 		}
8782676Seschrock 	}
8792676Seschrock 
8802676Seschrock 	return (ret);
8812676Seschrock 
8822676Seschrock error:
8832676Seschrock 	nvlist_free(ret);
8842676Seschrock 	return (NULL);
885789Sahrens }
886789Sahrens 
8874543Smarks static int
8884543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
8894543Smarks     uint64_t *ret_who)
8904543Smarks {
8914543Smarks 	struct passwd *pwd;
8924543Smarks 	struct group *grp;
8934543Smarks 	uid_t id;
8944543Smarks 
8954543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
8964543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
8974543Smarks 		*ret_who = -1;
8984543Smarks 		return (0);
8994543Smarks 	}
9004543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9014543Smarks 		return (EZFS_BADWHO);
9024543Smarks 
9034543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9044543Smarks 	    strcmp(who, "everyone") == 0) {
9054543Smarks 		*ret_who = -1;
9064543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9074543Smarks 		return (0);
9084543Smarks 	}
9094543Smarks 
9104543Smarks 	pwd = getpwnam(who);
9114543Smarks 	grp = getgrnam(who);
9124543Smarks 
9134543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9144543Smarks 		*ret_who = pwd->pw_uid;
9154543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9164543Smarks 		*ret_who = grp->gr_gid;
9174543Smarks 	} else if (pwd) {
9184543Smarks 		*ret_who = pwd->pw_uid;
9194543Smarks 		*who_type = ZFS_DELEG_USER;
9204543Smarks 	} else if (grp) {
9214543Smarks 		*ret_who = grp->gr_gid;
9224543Smarks 		*who_type = ZFS_DELEG_GROUP;
9234543Smarks 	} else {
9244543Smarks 		char *end;
9254543Smarks 
9264543Smarks 		id = strtol(who, &end, 10);
9274543Smarks 		if (errno != 0 || *end != '\0') {
9284543Smarks 			return (EZFS_BADWHO);
9294543Smarks 		} else {
9304543Smarks 			*ret_who = id;
9314543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
9324543Smarks 				*who_type = ZFS_DELEG_USER;
9334543Smarks 		}
9344543Smarks 	}
9354543Smarks 
9364543Smarks 	return (0);
9374543Smarks }
9384543Smarks 
9394543Smarks static void
9404543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
9414543Smarks {
9424543Smarks 	if (perms_nvp != NULL) {
9434543Smarks 		verify(nvlist_add_nvlist(who_nvp,
9444543Smarks 		    name, perms_nvp) == 0);
9454543Smarks 	} else {
9464543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
9474543Smarks 	}
9484543Smarks }
9494543Smarks 
9504543Smarks static void
9514543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
9524543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
9534543Smarks     nvlist_t *sets_nvp)
9544543Smarks {
9554543Smarks 	boolean_t do_perms, do_sets;
9564543Smarks 	char name[ZFS_MAX_DELEG_NAME];
9574543Smarks 
9584543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
9594543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
9604543Smarks 
9614543Smarks 	if (!do_perms && !do_sets)
9624543Smarks 		do_perms = do_sets = B_TRUE;
9634543Smarks 
9644543Smarks 	if (do_perms) {
9654543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
9664543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9674543Smarks 		    whostr : (void *)&whoid);
9684543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
9694543Smarks 	}
9704543Smarks 	if (do_sets) {
9714543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
9724543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9734543Smarks 		    whostr : (void *)&whoid);
9744543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
9754543Smarks 	}
9764543Smarks }
9774543Smarks 
9784543Smarks static void
9794543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
9804543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
9814543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
9824543Smarks {
9834543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
9844543Smarks 		helper(who_type, whoid, whostr, 0,
9854543Smarks 		    who_nvp, perms_nvp, sets_nvp);
9864543Smarks 	} else {
9874543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
9884543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
9894543Smarks 			    who_nvp, perms_nvp, sets_nvp);
9904543Smarks 		}
9914543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
9924543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
9934543Smarks 			    who_nvp, perms_nvp, sets_nvp);
9944543Smarks 		}
9954543Smarks 	}
9964543Smarks }
9974543Smarks 
9984543Smarks /*
9994543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10004543Smarks  *
10014543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10024543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10034543Smarks  * base attribute named stored in the dsl.
10044543Smarks  * Arguments:
10054543Smarks  *
10064543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10074543Smarks  *           whostr may be null for everyone or create perms.
10084543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10094543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10105331Samw  * perms:    common separated list of permissions.  May be null if user
10114543Smarks  *           is requested to remove permissions by who.
10124543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10134543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10144543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10154543Smarks  *           The output nvp will look something like this.
10164543Smarks  *              ul$1234 -> {create ; destroy }
10174543Smarks  *              Ul$1234 -> { @myset }
10184543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10194543Smarks  */
10204543Smarks int
10214543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10224543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10234543Smarks {
10244543Smarks 	nvlist_t *who_nvp;
10254543Smarks 	nvlist_t *perms_nvp = NULL;
10264543Smarks 	nvlist_t *sets_nvp = NULL;
10274543Smarks 	char errbuf[1024];
10284787Sahrens 	char *who_tok, *perm;
10294543Smarks 	int error;
10304543Smarks 
10314543Smarks 	*nvp = NULL;
10324543Smarks 
10334543Smarks 	if (perms) {
10344543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
10354543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10364543Smarks 			return (1);
10374543Smarks 		}
10384543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
10394543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10404543Smarks 			nvlist_free(perms_nvp);
10414543Smarks 			return (1);
10424543Smarks 		}
10434543Smarks 	}
10444543Smarks 
10454543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
10464543Smarks 		if (perms_nvp)
10474543Smarks 			nvlist_free(perms_nvp);
10484543Smarks 		if (sets_nvp)
10494543Smarks 			nvlist_free(sets_nvp);
10504543Smarks 		return (1);
10514543Smarks 	}
10524543Smarks 
10534543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
10544543Smarks 		namecheck_err_t why;
10554543Smarks 		char what;
10564543Smarks 
10574543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
10584787Sahrens 			nvlist_free(who_nvp);
10594787Sahrens 			if (perms_nvp)
10604787Sahrens 				nvlist_free(perms_nvp);
10614787Sahrens 			if (sets_nvp)
10624787Sahrens 				nvlist_free(sets_nvp);
10634787Sahrens 
10644543Smarks 			switch (why) {
10654543Smarks 			case NAME_ERR_NO_AT:
10664543Smarks 				zfs_error_aux(zhp->zfs_hdl,
10674543Smarks 				    dgettext(TEXT_DOMAIN,
10684543Smarks 				    "set definition must begin with an '@' "
10694543Smarks 				    "character"));
10704543Smarks 			}
10714543Smarks 			return (zfs_error(zhp->zfs_hdl,
10724543Smarks 			    EZFS_BADPERMSET, whostr));
10734543Smarks 		}
10744543Smarks 	}
10754543Smarks 
10764543Smarks 	/*
10774543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
10784543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
10794543Smarks 	 * other sets_nvp will have only permssion set names in it.
10804543Smarks 	 */
10814787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
10824787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
10834787Sahrens 
10844787Sahrens 		if (perm_canonical) {
10854787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
10864787Sahrens 			    perm_canonical) == 0);
10874787Sahrens 		} else if (perm[0] == '@') {
10884787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
10894787Sahrens 		} else {
10904787Sahrens 			nvlist_free(who_nvp);
10914787Sahrens 			nvlist_free(perms_nvp);
10924787Sahrens 			nvlist_free(sets_nvp);
10934787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
10944543Smarks 		}
10954543Smarks 	}
10964543Smarks 
10974543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
10984543Smarks 		who_tok = strtok(whostr, ",");
10994543Smarks 		if (who_tok == NULL) {
11004543Smarks 			nvlist_free(who_nvp);
11014787Sahrens 			if (perms_nvp)
11024787Sahrens 				nvlist_free(perms_nvp);
11034543Smarks 			if (sets_nvp)
11044543Smarks 				nvlist_free(sets_nvp);
11054543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11064543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11074543Smarks 			    whostr);
11084543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11094543Smarks 		}
11104543Smarks 	}
11114543Smarks 
11124543Smarks 	/*
11134543Smarks 	 * Now create the nvlist(s)
11144543Smarks 	 */
11154543Smarks 	do {
11164543Smarks 		uint64_t who_id;
11174543Smarks 
11184543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11194543Smarks 		    &who_id);
11204543Smarks 		if (error) {
11214543Smarks 			nvlist_free(who_nvp);
11224787Sahrens 			if (perms_nvp)
11234787Sahrens 				nvlist_free(perms_nvp);
11244543Smarks 			if (sets_nvp)
11254543Smarks 				nvlist_free(sets_nvp);
11264543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11274543Smarks 			    dgettext(TEXT_DOMAIN,
11284543Smarks 			    "Unable to determine uid/gid for "
11294543Smarks 			    "%s "), who_tok);
11304543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11314543Smarks 		}
11324543Smarks 
11334543Smarks 		/*
11344543Smarks 		 * add entries for both local and descendent when required
11354543Smarks 		 */
11364543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
11374543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
11384543Smarks 
11394543Smarks 	} while (who_tok = strtok(NULL, ","));
11404543Smarks 	*nvp = who_nvp;
11414543Smarks 	return (0);
11424543Smarks }
11434543Smarks 
11444543Smarks static int
11454543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
11464543Smarks {
11474543Smarks 	zfs_cmd_t zc = { 0 };
11484543Smarks 	int error;
11494543Smarks 	char errbuf[1024];
11504543Smarks 
11514543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
11524543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
11534543Smarks 	    zhp->zfs_name);
11544543Smarks 
11555094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
11564543Smarks 		return (-1);
11574543Smarks 
11584543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
11594543Smarks 	zc.zc_perm_action = unset;
11604543Smarks 
11614543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
11624543Smarks 	if (error && errno == ENOTSUP) {
11634543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
11644543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
11654543Smarks 		zcmd_free_nvlists(&zc);
11664543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
11674543Smarks 	} else if (error) {
11684543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
11694543Smarks 	}
11704543Smarks 	zcmd_free_nvlists(&zc);
11714543Smarks 
11724543Smarks 	return (error);
11734543Smarks }
11744543Smarks 
11754543Smarks int
11764543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
11774543Smarks {
11784543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
11794543Smarks }
11804543Smarks 
11814543Smarks int
11824543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
11834543Smarks {
11844543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
11854543Smarks }
11864543Smarks 
11874543Smarks static int
11884543Smarks perm_compare(const void *arg1, const void *arg2)
11894543Smarks {
11904543Smarks 	const zfs_perm_node_t *node1 = arg1;
11914543Smarks 	const zfs_perm_node_t *node2 = arg2;
11924543Smarks 	int ret;
11934543Smarks 
11944543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
11954543Smarks 
11964543Smarks 	if (ret > 0)
11974543Smarks 		return (1);
11984543Smarks 	if (ret < 0)
11994543Smarks 		return (-1);
12004543Smarks 	else
12014543Smarks 		return (0);
12024543Smarks }
12034543Smarks 
12044543Smarks static void
12054543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12064543Smarks {
12074543Smarks 	zfs_perm_node_t *permnode;
12085367Sahrens 	void *cookie = NULL;
12095367Sahrens 
12105367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12114543Smarks 		free(permnode);
12125367Sahrens 	avl_destroy(tree);
12134543Smarks }
12144543Smarks 
12154543Smarks static void
12164543Smarks zfs_destroy_tree(avl_tree_t *tree)
12174543Smarks {
12184543Smarks 	zfs_allow_node_t *allownode;
12195367Sahrens 	void *cookie = NULL;
12205367Sahrens 
12214543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12224543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12234543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12244543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12254543Smarks 		free(allownode);
12264543Smarks 	}
12275367Sahrens 	avl_destroy(tree);
12284543Smarks }
12294543Smarks 
12304543Smarks void
12314543Smarks zfs_free_allows(zfs_allow_t *allow)
12324543Smarks {
12334543Smarks 	zfs_allow_t *allownext;
12344543Smarks 	zfs_allow_t *freeallow;
12354543Smarks 
12364543Smarks 	allownext = allow;
12374543Smarks 	while (allownext) {
12384543Smarks 		zfs_destroy_tree(&allownext->z_sets);
12394543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
12404543Smarks 		zfs_destroy_tree(&allownext->z_user);
12414543Smarks 		zfs_destroy_tree(&allownext->z_group);
12424543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
12434543Smarks 		freeallow = allownext;
12444543Smarks 		allownext = allownext->z_next;
12454543Smarks 		free(freeallow);
12464543Smarks 	}
12474543Smarks }
12484543Smarks 
12494543Smarks static zfs_allow_t *
12504543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
12514543Smarks {
12524543Smarks 	zfs_allow_t *ptree;
12534543Smarks 
12544543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
12554543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
12564543Smarks 		return (NULL);
12574543Smarks 	}
12584543Smarks 
12594543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
12604543Smarks 	avl_create(&ptree->z_sets,
12614543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12624543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12634543Smarks 	avl_create(&ptree->z_crperms,
12644543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12654543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12664543Smarks 	avl_create(&ptree->z_user,
12674543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12684543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12694543Smarks 	avl_create(&ptree->z_group,
12704543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12714543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12724543Smarks 	avl_create(&ptree->z_everyone,
12734543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12744543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12754543Smarks 
12764543Smarks 	if (prev)
12774543Smarks 		prev->z_next = ptree;
12784543Smarks 	ptree->z_next = NULL;
12794543Smarks 	return (ptree);
12804543Smarks }
12814543Smarks 
12824543Smarks /*
12834543Smarks  * Add permissions to the appropriate AVL permission tree.
12844543Smarks  * The appropriate tree may not be the requested tree.
12854543Smarks  * For example if ld indicates a local permission, but
12864543Smarks  * same permission also exists as a descendent permission
12874543Smarks  * then the permission will be removed from the descendent
12884543Smarks  * tree and add the the local+descendent tree.
12894543Smarks  */
12904543Smarks static int
12914543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
12924543Smarks     char *perm, char ld)
12934543Smarks {
12944543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
12954543Smarks 	zfs_perm_node_t *newnode;
12964543Smarks 	avl_index_t where, where2;
12974543Smarks 	avl_tree_t *tree, *altree;
12984543Smarks 
12994543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13004543Smarks 
13014543Smarks 	if (ld == ZFS_DELEG_NA) {
13024543Smarks 		tree =  &allownode->z_localdescend;
13034543Smarks 		altree = &allownode->z_descend;
13044543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13054543Smarks 		tree = &allownode->z_local;
13064543Smarks 		altree = &allownode->z_descend;
13074543Smarks 	} else {
13084543Smarks 		tree = &allownode->z_descend;
13094543Smarks 		altree = &allownode->z_local;
13104543Smarks 	}
13114543Smarks 	permnode = avl_find(tree, &pnode, &where);
13124543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13134543Smarks 
13144543Smarks 	if (permnode2) {
13154543Smarks 		avl_remove(altree, permnode2);
13164543Smarks 		free(permnode2);
13174543Smarks 		if (permnode == NULL) {
13184543Smarks 			tree =  &allownode->z_localdescend;
13194543Smarks 		}
13204543Smarks 	}
13214543Smarks 
13224543Smarks 	/*
13234543Smarks 	 * Now insert new permission in either requested location
13244543Smarks 	 * local/descendent or into ld when perm will exist in both.
13254543Smarks 	 */
13264543Smarks 	if (permnode == NULL) {
13274543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13284543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
13294543Smarks 			return (-1);
13304543Smarks 		}
13314543Smarks 		*newnode = pnode;
13324543Smarks 		avl_add(tree, newnode);
13334543Smarks 	}
13344543Smarks 	return (0);
13354543Smarks }
13364577Sahrens 
13374543Smarks /*
13384543Smarks  * Uggh, this is going to be a bit complicated.
13394543Smarks  * we have an nvlist coming out of the kernel that
13404543Smarks  * will indicate where the permission is set and then
13414543Smarks  * it will contain allow of the various "who's", and what
13424543Smarks  * their permissions are.  To further complicate this
13434543Smarks  * we will then have to coalesce the local,descendent
13444543Smarks  * and local+descendent permissions where appropriate.
13454543Smarks  * The kernel only knows about a permission as being local
13464543Smarks  * or descendent, but not both.
13474543Smarks  *
13484543Smarks  * In order to make this easier for zfs_main to deal with
13494543Smarks  * a series of AVL trees will be used to maintain
13504543Smarks  * all of this, primarily for sorting purposes as well
13514543Smarks  * as the ability to quickly locate a specific entry.
13524543Smarks  *
13534543Smarks  * What we end up with are tree's for sets, create perms,
13544543Smarks  * user, groups and everyone.  With each of those trees
13554543Smarks  * we have subtrees for local, descendent and local+descendent
13564543Smarks  * permissions.
13574543Smarks  */
13584543Smarks int
13594543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
13604543Smarks {
13614543Smarks 	zfs_cmd_t zc = { 0 };
13624543Smarks 	int error;
13634543Smarks 	nvlist_t *nvlist;
13644543Smarks 	nvlist_t *permnv, *sourcenv;
13654543Smarks 	nvpair_t *who_pair, *source_pair;
13664543Smarks 	nvpair_t *perm_pair;
13674543Smarks 	char errbuf[1024];
13684543Smarks 	zfs_allow_t *zallowp, *newallowp;
13694543Smarks 	char  ld;
13704543Smarks 	char *nvpname;
13714543Smarks 	uid_t	uid;
13724543Smarks 	gid_t	gid;
13734543Smarks 	avl_tree_t *tree;
13744543Smarks 	avl_index_t where;
13754543Smarks 
13764543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13774543Smarks 
13784543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
13794543Smarks 		return (-1);
13804543Smarks 
13814543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
13824543Smarks 		if (errno == ENOMEM) {
13834543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
13844543Smarks 				zcmd_free_nvlists(&zc);
13854543Smarks 				return (-1);
13864543Smarks 			}
13874543Smarks 		} else if (errno == ENOTSUP) {
13884543Smarks 			zcmd_free_nvlists(&zc);
13894543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
13904543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
13914543Smarks 			return (zfs_error(zhp->zfs_hdl,
13924543Smarks 			    EZFS_BADVERSION, errbuf));
13934543Smarks 		} else {
13944543Smarks 			zcmd_free_nvlists(&zc);
13954543Smarks 			return (-1);
13964543Smarks 		}
13974543Smarks 	}
13984543Smarks 
13994543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14004543Smarks 		zcmd_free_nvlists(&zc);
14014543Smarks 		return (-1);
14024543Smarks 	}
14034543Smarks 
14044543Smarks 	zcmd_free_nvlists(&zc);
14054543Smarks 
14064543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14074543Smarks 
14084543Smarks 	if (source_pair == NULL) {
14094543Smarks 		*zfs_perms = NULL;
14104543Smarks 		return (0);
14114543Smarks 	}
14124543Smarks 
14134543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14144543Smarks 	if (*zfs_perms == NULL) {
14154543Smarks 		return (0);
14164543Smarks 	}
14174543Smarks 
14184543Smarks 	zallowp = *zfs_perms;
14194543Smarks 
14204543Smarks 	for (;;) {
14214543Smarks 		struct passwd *pwd;
14224543Smarks 		struct group *grp;
14234543Smarks 		zfs_allow_node_t *allownode;
14244543Smarks 		zfs_allow_node_t  findallownode;
14254543Smarks 		zfs_allow_node_t *newallownode;
14264543Smarks 
14274543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14284543Smarks 		    nvpair_name(source_pair),
14294543Smarks 		    sizeof (zallowp->z_setpoint));
14304543Smarks 
14314543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
14324543Smarks 			goto abort;
14334543Smarks 
14344543Smarks 		/*
14354543Smarks 		 * Make sure nvlist is composed correctly
14364543Smarks 		 */
14374543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
14384543Smarks 			goto abort;
14394543Smarks 		}
14404543Smarks 
14414543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
14424543Smarks 		if (who_pair == NULL) {
14434543Smarks 			goto abort;
14444543Smarks 		}
14454543Smarks 
14464543Smarks 		do {
14474543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
14484543Smarks 			if (error) {
14494543Smarks 				goto abort;
14504543Smarks 			}
14514543Smarks 
14524543Smarks 			/*
14534543Smarks 			 * First build up the key to use
14544543Smarks 			 * for looking up in the various
14554543Smarks 			 * who trees.
14564543Smarks 			 */
14574543Smarks 			ld = nvpair_name(who_pair)[1];
14584543Smarks 			nvpname = nvpair_name(who_pair);
14594543Smarks 			switch (nvpair_name(who_pair)[0]) {
14604543Smarks 			case ZFS_DELEG_USER:
14614543Smarks 			case ZFS_DELEG_USER_SETS:
14624543Smarks 				tree = &zallowp->z_user;
14634543Smarks 				uid = atol(&nvpname[3]);
14644543Smarks 				pwd = getpwuid(uid);
14654543Smarks 				(void) snprintf(findallownode.z_key,
14664543Smarks 				    sizeof (findallownode.z_key), "user %s",
14674543Smarks 				    (pwd) ? pwd->pw_name :
14684543Smarks 				    &nvpair_name(who_pair)[3]);
14694543Smarks 				break;
14704543Smarks 			case ZFS_DELEG_GROUP:
14714543Smarks 			case ZFS_DELEG_GROUP_SETS:
14724543Smarks 				tree = &zallowp->z_group;
14734543Smarks 				gid = atol(&nvpname[3]);
14744543Smarks 				grp = getgrgid(gid);
14754543Smarks 				(void) snprintf(findallownode.z_key,
14764543Smarks 				    sizeof (findallownode.z_key), "group %s",
14774543Smarks 				    (grp) ? grp->gr_name :
14784543Smarks 				    &nvpair_name(who_pair)[3]);
14794543Smarks 				break;
14804543Smarks 			case ZFS_DELEG_CREATE:
14814543Smarks 			case ZFS_DELEG_CREATE_SETS:
14824543Smarks 				tree = &zallowp->z_crperms;
14834543Smarks 				(void) strlcpy(findallownode.z_key, "",
14844543Smarks 				    sizeof (findallownode.z_key));
14854543Smarks 				break;
14864543Smarks 			case ZFS_DELEG_EVERYONE:
14874543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
14884543Smarks 				(void) snprintf(findallownode.z_key,
14894543Smarks 				    sizeof (findallownode.z_key), "everyone");
14904543Smarks 				tree = &zallowp->z_everyone;
14914543Smarks 				break;
14924543Smarks 			case ZFS_DELEG_NAMED_SET:
14934543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
14944543Smarks 				(void) snprintf(findallownode.z_key,
14954543Smarks 				    sizeof (findallownode.z_key), "%s",
14964543Smarks 				    &nvpair_name(who_pair)[3]);
14974543Smarks 				tree = &zallowp->z_sets;
14984543Smarks 				break;
14994543Smarks 			}
15004543Smarks 
15014543Smarks 			/*
15024543Smarks 			 * Place who in tree
15034543Smarks 			 */
15044543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15054543Smarks 			if (allownode == NULL) {
15064543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15074543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15084543Smarks 					goto abort;
15094543Smarks 				}
15104543Smarks 				avl_create(&newallownode->z_localdescend,
15114543Smarks 				    perm_compare,
15124543Smarks 				    sizeof (zfs_perm_node_t),
15134543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15144543Smarks 				avl_create(&newallownode->z_local,
15154543Smarks 				    perm_compare,
15164543Smarks 				    sizeof (zfs_perm_node_t),
15174543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15184543Smarks 				avl_create(&newallownode->z_descend,
15194543Smarks 				    perm_compare,
15204543Smarks 				    sizeof (zfs_perm_node_t),
15214543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15224543Smarks 				(void) strlcpy(newallownode->z_key,
15234543Smarks 				    findallownode.z_key,
15244543Smarks 				    sizeof (findallownode.z_key));
15254543Smarks 				avl_insert(tree, newallownode, where);
15264543Smarks 				allownode = newallownode;
15274543Smarks 			}
15284543Smarks 
15294543Smarks 			/*
15304543Smarks 			 * Now iterate over the permissions and
15314543Smarks 			 * place them in the appropriate local,
15324543Smarks 			 * descendent or local+descendent tree.
15334543Smarks 			 *
15344543Smarks 			 * The permissions are added to the tree
15354543Smarks 			 * via zfs_coalesce_perm().
15364543Smarks 			 */
15374543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
15384543Smarks 			if (perm_pair == NULL)
15394543Smarks 				goto abort;
15404543Smarks 			do {
15414543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
15424543Smarks 				    nvpair_name(perm_pair), ld) != 0)
15434543Smarks 					goto abort;
15444543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
15454543Smarks 			    perm_pair));
15464543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
15474543Smarks 
15484543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
15494543Smarks 		if (source_pair == NULL)
15504543Smarks 			break;
15514543Smarks 
15524543Smarks 		/*
15534543Smarks 		 * allocate another node from the link list of
15544543Smarks 		 * zfs_allow_t structures
15554543Smarks 		 */
15564543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
15574543Smarks 		    nvpair_name(source_pair));
15584543Smarks 		if (newallowp == NULL) {
15594543Smarks 			goto abort;
15604543Smarks 		}
15614543Smarks 		zallowp = newallowp;
15624543Smarks 	}
15634543Smarks 	nvlist_free(nvlist);
15644543Smarks 	return (0);
15654543Smarks abort:
15664543Smarks 	zfs_free_allows(*zfs_perms);
15674543Smarks 	nvlist_free(nvlist);
15684543Smarks 	return (-1);
15694543Smarks }
15704543Smarks 
1571789Sahrens /*
1572789Sahrens  * Given a property name and value, set the property for the given dataset.
1573789Sahrens  */
1574789Sahrens int
15752676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1576789Sahrens {
1577789Sahrens 	zfs_cmd_t zc = { 0 };
15782676Seschrock 	int ret = -1;
15792676Seschrock 	prop_changelist_t *cl = NULL;
15802082Seschrock 	char errbuf[1024];
15812082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
15822676Seschrock 	nvlist_t *nvl = NULL, *realprops;
15832676Seschrock 	zfs_prop_t prop;
15842082Seschrock 
15852082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
15862676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
15872082Seschrock 	    zhp->zfs_name);
15882082Seschrock 
15892676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
15902676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
15912676Seschrock 		(void) no_memory(hdl);
15922676Seschrock 		goto error;
1593789Sahrens 	}
1594789Sahrens 
15955094Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
15962676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
15972676Seschrock 		goto error;
15985094Slling 
15992676Seschrock 	nvlist_free(nvl);
16002676Seschrock 	nvl = realprops;
16012676Seschrock 
16022676Seschrock 	prop = zfs_name_to_prop(propname);
16032676Seschrock 
1604789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
16052676Seschrock 		goto error;
1606789Sahrens 
1607789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
16082082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16092082Seschrock 		    "child dataset with inherited mountpoint is used "
16102082Seschrock 		    "in a non-global zone"));
16112082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1612789Sahrens 		goto error;
1613789Sahrens 	}
1614789Sahrens 
1615789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1616789Sahrens 		goto error;
1617789Sahrens 
1618789Sahrens 	/*
1619789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1620789Sahrens 	 */
1621789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1622789Sahrens 
16235094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
16242676Seschrock 		goto error;
16252676Seschrock 
16264543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1627789Sahrens 
1628789Sahrens 	if (ret != 0) {
1629789Sahrens 		switch (errno) {
1630789Sahrens 
1631789Sahrens 		case ENOSPC:
1632789Sahrens 			/*
1633789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1634789Sahrens 			 * something different; setting a quota or reservation
1635789Sahrens 			 * doesn't use any disk space.
1636789Sahrens 			 */
1637789Sahrens 			switch (prop) {
1638789Sahrens 			case ZFS_PROP_QUOTA:
16395378Sck153898 			case ZFS_PROP_REFQUOTA:
16402082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16412082Seschrock 				    "size is less than current used or "
16422082Seschrock 				    "reserved space"));
16432082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1644789Sahrens 				break;
1645789Sahrens 
1646789Sahrens 			case ZFS_PROP_RESERVATION:
16475378Sck153898 			case ZFS_PROP_REFRESERVATION:
16482082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16492082Seschrock 				    "size is greater than available space"));
16502082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1651789Sahrens 				break;
1652789Sahrens 
1653789Sahrens 			default:
16542082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1655789Sahrens 				break;
1656789Sahrens 			}
1657789Sahrens 			break;
1658789Sahrens 
1659789Sahrens 		case EBUSY:
16602082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
16612082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
16622082Seschrock 			else
16632676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1664789Sahrens 			break;
1665789Sahrens 
16661175Slling 		case EROFS:
16672082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
16681175Slling 			break;
16691175Slling 
16703886Sahl 		case ENOTSUP:
16713886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16724603Sahrens 			    "pool must be upgraded to set this "
16734603Sahrens 			    "property or value"));
16743886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
16753886Sahl 			break;
16763886Sahl 
1677789Sahrens 		case EOVERFLOW:
1678789Sahrens 			/*
1679789Sahrens 			 * This platform can't address a volume this big.
1680789Sahrens 			 */
1681789Sahrens #ifdef _ILP32
1682789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
16832082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1684789Sahrens 				break;
1685789Sahrens 			}
1686789Sahrens #endif
16872082Seschrock 			/* FALLTHROUGH */
1688789Sahrens 		default:
16892082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1690789Sahrens 		}
1691789Sahrens 	} else {
1692789Sahrens 		/*
1693789Sahrens 		 * Refresh the statistics so the new property value
1694789Sahrens 		 * is reflected.
1695789Sahrens 		 */
16962676Seschrock 		if ((ret = changelist_postfix(cl)) == 0)
16972676Seschrock 			(void) get_stats(zhp);
1698789Sahrens 	}
1699789Sahrens 
1700789Sahrens error:
17012676Seschrock 	nvlist_free(nvl);
17022676Seschrock 	zcmd_free_nvlists(&zc);
17032676Seschrock 	if (cl)
17042676Seschrock 		changelist_free(cl);
1705789Sahrens 	return (ret);
1706789Sahrens }
1707789Sahrens 
1708789Sahrens /*
1709789Sahrens  * Given a property, inherit the value from the parent dataset.
1710789Sahrens  */
1711789Sahrens int
17122676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1713789Sahrens {
1714789Sahrens 	zfs_cmd_t zc = { 0 };
1715789Sahrens 	int ret;
1716789Sahrens 	prop_changelist_t *cl;
17172082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17182082Seschrock 	char errbuf[1024];
17192676Seschrock 	zfs_prop_t prop;
17202082Seschrock 
17212082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
17222082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1723789Sahrens 
17245094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
17252676Seschrock 		/*
17262676Seschrock 		 * For user properties, the amount of work we have to do is very
17272676Seschrock 		 * small, so just do it here.
17282676Seschrock 		 */
17292676Seschrock 		if (!zfs_prop_user(propname)) {
17302676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17312676Seschrock 			    "invalid property"));
17322676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
17332676Seschrock 		}
17342676Seschrock 
17352676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
17362676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
17372676Seschrock 
17384849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
17392676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
17402676Seschrock 
17412676Seschrock 		return (0);
17422676Seschrock 	}
17432676Seschrock 
1744789Sahrens 	/*
1745789Sahrens 	 * Verify that this property is inheritable.
1746789Sahrens 	 */
17472082Seschrock 	if (zfs_prop_readonly(prop))
17482082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
17492082Seschrock 
17502082Seschrock 	if (!zfs_prop_inheritable(prop))
17512082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1752789Sahrens 
1753789Sahrens 	/*
1754789Sahrens 	 * Check to see if the value applies to this type
1755789Sahrens 	 */
17562082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
17572082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1758789Sahrens 
17593443Srm160521 	/*
17603443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
17613443Srm160521 	 */
17623443Srm160521 	propname = zfs_prop_to_name(prop);
1763789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
17642676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1765789Sahrens 
1766789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1767789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
17682082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17692082Seschrock 		    "dataset is used in a non-global zone"));
17702082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1771789Sahrens 	}
1772789Sahrens 
1773789Sahrens 	/*
1774789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1775789Sahrens 	 */
1776789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1777789Sahrens 		return (-1);
1778789Sahrens 
1779789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17802082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17812082Seschrock 		    "child dataset with inherited mountpoint is used "
17822082Seschrock 		    "in a non-global zone"));
17832082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1784789Sahrens 		goto error;
1785789Sahrens 	}
1786789Sahrens 
1787789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1788789Sahrens 		goto error;
1789789Sahrens 
17904849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
17912082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1792789Sahrens 	} else {
1793789Sahrens 
17942169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1795789Sahrens 			goto error;
1796789Sahrens 
1797789Sahrens 		/*
1798789Sahrens 		 * Refresh the statistics so the new property is reflected.
1799789Sahrens 		 */
1800789Sahrens 		(void) get_stats(zhp);
1801789Sahrens 	}
1802789Sahrens 
1803789Sahrens error:
1804789Sahrens 	changelist_free(cl);
1805789Sahrens 	return (ret);
1806789Sahrens }
1807789Sahrens 
1808789Sahrens /*
18091356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
18101356Seschrock  * extract them appropriately.
18111356Seschrock  */
18121356Seschrock static uint64_t
18131356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18141356Seschrock {
18151356Seschrock 	nvlist_t *nv;
18161356Seschrock 	uint64_t value;
18171356Seschrock 
18182885Sahrens 	*source = NULL;
18191356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
18201356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
18215094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
18225094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
18231356Seschrock 	} else {
18241356Seschrock 		value = zfs_prop_default_numeric(prop);
18251356Seschrock 		*source = "";
18261356Seschrock 	}
18271356Seschrock 
18281356Seschrock 	return (value);
18291356Seschrock }
18301356Seschrock 
18311356Seschrock static char *
18321356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18331356Seschrock {
18341356Seschrock 	nvlist_t *nv;
18351356Seschrock 	char *value;
18361356Seschrock 
18372885Sahrens 	*source = NULL;
18381356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
18391356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
18405094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
18415094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
18421356Seschrock 	} else {
18431356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
18441356Seschrock 			value = "";
18451356Seschrock 		*source = "";
18461356Seschrock 	}
18471356Seschrock 
18481356Seschrock 	return (value);
18491356Seschrock }
18501356Seschrock 
18511356Seschrock /*
1852789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1853789Sahrens  * zfs_prop_get_int() are built using this interface.
1854789Sahrens  *
1855789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1856789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1857789Sahrens  * If they differ from the on-disk values, report the current values and mark
1858789Sahrens  * the source "temporary".
1859789Sahrens  */
18602082Seschrock static int
18615094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
18622082Seschrock     char **source, uint64_t *val)
1863789Sahrens {
18645147Srm160521 	zfs_cmd_t zc = { 0 };
1865*5592Stimh 	nvlist_t *zplprops = NULL;
1866789Sahrens 	struct mnttab mnt;
18673265Sahrens 	char *mntopt_on = NULL;
18683265Sahrens 	char *mntopt_off = NULL;
1869789Sahrens 
1870789Sahrens 	*source = NULL;
1871789Sahrens 
18723265Sahrens 	switch (prop) {
18733265Sahrens 	case ZFS_PROP_ATIME:
18743265Sahrens 		mntopt_on = MNTOPT_ATIME;
18753265Sahrens 		mntopt_off = MNTOPT_NOATIME;
18763265Sahrens 		break;
18773265Sahrens 
18783265Sahrens 	case ZFS_PROP_DEVICES:
18793265Sahrens 		mntopt_on = MNTOPT_DEVICES;
18803265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
18813265Sahrens 		break;
18823265Sahrens 
18833265Sahrens 	case ZFS_PROP_EXEC:
18843265Sahrens 		mntopt_on = MNTOPT_EXEC;
18853265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
18863265Sahrens 		break;
18873265Sahrens 
18883265Sahrens 	case ZFS_PROP_READONLY:
18893265Sahrens 		mntopt_on = MNTOPT_RO;
18903265Sahrens 		mntopt_off = MNTOPT_RW;
18913265Sahrens 		break;
18923265Sahrens 
18933265Sahrens 	case ZFS_PROP_SETUID:
18943265Sahrens 		mntopt_on = MNTOPT_SETUID;
18953265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
18963265Sahrens 		break;
18973265Sahrens 
18983265Sahrens 	case ZFS_PROP_XATTR:
18993265Sahrens 		mntopt_on = MNTOPT_XATTR;
19003265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
19013265Sahrens 		break;
19025331Samw 
19035331Samw 	case ZFS_PROP_NBMAND:
19045331Samw 		mntopt_on = MNTOPT_NBMAND;
19055331Samw 		mntopt_off = MNTOPT_NONBMAND;
19065331Samw 		break;
19073265Sahrens 	}
19083265Sahrens 
19092474Seschrock 	/*
19102474Seschrock 	 * Because looking up the mount options is potentially expensive
19112474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
19122474Seschrock 	 * we're looking up a property which requires its presence.
19132474Seschrock 	 */
19142474Seschrock 	if (!zhp->zfs_mntcheck &&
19153265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
19163265Sahrens 		struct mnttab entry, search = { 0 };
19173265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
19182474Seschrock 
19192474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
19202474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
19213265Sahrens 		rewind(mnttab);
19223265Sahrens 
19233265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
19243265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
19253265Sahrens 			    entry.mnt_mntopts);
19263265Sahrens 			if (zhp->zfs_mntopts == NULL)
19273265Sahrens 				return (-1);
19283265Sahrens 		}
19292474Seschrock 
19302474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
19312474Seschrock 	}
19322474Seschrock 
1933789Sahrens 	if (zhp->zfs_mntopts == NULL)
1934789Sahrens 		mnt.mnt_mntopts = "";
1935789Sahrens 	else
1936789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1937789Sahrens 
1938789Sahrens 	switch (prop) {
1939789Sahrens 	case ZFS_PROP_ATIME:
19403265Sahrens 	case ZFS_PROP_DEVICES:
19413265Sahrens 	case ZFS_PROP_EXEC:
19423265Sahrens 	case ZFS_PROP_READONLY:
19433265Sahrens 	case ZFS_PROP_SETUID:
19443265Sahrens 	case ZFS_PROP_XATTR:
19455331Samw 	case ZFS_PROP_NBMAND:
19462082Seschrock 		*val = getprop_uint64(zhp, prop, source);
19472082Seschrock 
19483265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
19492082Seschrock 			*val = B_TRUE;
1950789Sahrens 			if (src)
19515094Slling 				*src = ZPROP_SRC_TEMPORARY;
19523265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
19532082Seschrock 			*val = B_FALSE;
1954789Sahrens 			if (src)
19555094Slling 				*src = ZPROP_SRC_TEMPORARY;
1956789Sahrens 		}
19572082Seschrock 		break;
1958789Sahrens 
19593265Sahrens 	case ZFS_PROP_CANMOUNT:
19602082Seschrock 		*val = getprop_uint64(zhp, prop, source);
19613417Srm160521 		if (*val == 0)
19623417Srm160521 			*source = zhp->zfs_name;
19633417Srm160521 		else
19643417Srm160521 			*source = "";	/* default */
19652082Seschrock 		break;
1966789Sahrens 
1967789Sahrens 	case ZFS_PROP_QUOTA:
19685378Sck153898 	case ZFS_PROP_REFQUOTA:
1969789Sahrens 	case ZFS_PROP_RESERVATION:
19705378Sck153898 	case ZFS_PROP_REFRESERVATION:
19712885Sahrens 		*val = getprop_uint64(zhp, prop, source);
19722885Sahrens 		if (*val == 0)
1973789Sahrens 			*source = "";	/* default */
1974789Sahrens 		else
1975789Sahrens 			*source = zhp->zfs_name;
19762082Seschrock 		break;
1977789Sahrens 
1978789Sahrens 	case ZFS_PROP_MOUNTED:
19792082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
19802082Seschrock 		break;
1981789Sahrens 
19823377Seschrock 	case ZFS_PROP_NUMCLONES:
19833377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
19843377Seschrock 		break;
19853377Seschrock 
19865147Srm160521 	case ZFS_PROP_VERSION:
19875498Stimh 	case ZFS_PROP_NORMALIZE:
19885498Stimh 	case ZFS_PROP_UTF8ONLY:
19895498Stimh 	case ZFS_PROP_CASE:
19905498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
19915498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
19925473Srm160521 			return (-1);
19935147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19945498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
19955498Stimh 			zcmd_free_nvlists(&zc);
19965147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
19975498Stimh 			    "unable to get %s property"),
19985498Stimh 			    zfs_prop_to_name(prop));
19995147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
20005147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
20015147Srm160521 		}
20025498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
20035498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
20045498Stimh 		    val) != 0) {
20055498Stimh 			zcmd_free_nvlists(&zc);
20065498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
20075498Stimh 			    "unable to get %s property"),
20085498Stimh 			    zfs_prop_to_name(prop));
20095498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
20105498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
20115498Stimh 		}
2012*5592Stimh 		if (zplprops)
2013*5592Stimh 			nvlist_free(zplprops);
20145498Stimh 		zcmd_free_nvlists(&zc);
20155147Srm160521 		break;
20165147Srm160521 
2017789Sahrens 	default:
20184577Sahrens 		switch (zfs_prop_get_type(prop)) {
20194787Sahrens 		case PROP_TYPE_NUMBER:
20204787Sahrens 		case PROP_TYPE_INDEX:
20214577Sahrens 			*val = getprop_uint64(zhp, prop, source);
20224577Sahrens 			break;
20234577Sahrens 
20244787Sahrens 		case PROP_TYPE_STRING:
20254577Sahrens 		default:
20264577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
20274577Sahrens 			    "cannot get non-numeric property"));
20284577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
20294577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
20304577Sahrens 		}
2031789Sahrens 	}
2032789Sahrens 
2033789Sahrens 	return (0);
2034789Sahrens }
2035789Sahrens 
2036789Sahrens /*
2037789Sahrens  * Calculate the source type, given the raw source string.
2038789Sahrens  */
2039789Sahrens static void
20405094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2041789Sahrens     char *statbuf, size_t statlen)
2042789Sahrens {
20435094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2044789Sahrens 		return;
2045789Sahrens 
2046789Sahrens 	if (source == NULL) {
20475094Slling 		*srctype = ZPROP_SRC_NONE;
2048789Sahrens 	} else if (source[0] == '\0') {
20495094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2050789Sahrens 	} else {
2051789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
20525094Slling 			*srctype = ZPROP_SRC_LOCAL;
2053789Sahrens 		} else {
2054789Sahrens 			(void) strlcpy(statbuf, source, statlen);
20555094Slling 			*srctype = ZPROP_SRC_INHERITED;
2056789Sahrens 		}
2057789Sahrens 	}
2058789Sahrens 
2059789Sahrens }
2060789Sahrens 
2061789Sahrens /*
2062789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2063789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2064789Sahrens  * human-readable form.
2065789Sahrens  *
2066789Sahrens  * Returns 0 on success, or -1 on error.
2067789Sahrens  */
2068789Sahrens int
2069789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
20705094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2071789Sahrens {
2072789Sahrens 	char *source = NULL;
2073789Sahrens 	uint64_t val;
2074789Sahrens 	char *str;
2075789Sahrens 	const char *root;
20762676Seschrock 	const char *strval;
2077789Sahrens 
2078789Sahrens 	/*
2079789Sahrens 	 * Check to see if this property applies to our object
2080789Sahrens 	 */
2081789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2082789Sahrens 		return (-1);
2083789Sahrens 
2084789Sahrens 	if (src)
20855094Slling 		*src = ZPROP_SRC_NONE;
2086789Sahrens 
2087789Sahrens 	switch (prop) {
2088789Sahrens 	case ZFS_PROP_CREATION:
2089789Sahrens 		/*
2090789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2091789Sahrens 		 * this into a string unless 'literal' is specified.
2092789Sahrens 		 */
2093789Sahrens 		{
20942885Sahrens 			val = getprop_uint64(zhp, prop, &source);
20952885Sahrens 			time_t time = (time_t)val;
2096789Sahrens 			struct tm t;
2097789Sahrens 
2098789Sahrens 			if (literal ||
2099789Sahrens 			    localtime_r(&time, &t) == NULL ||
2100789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2101789Sahrens 			    &t) == 0)
21022885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2103789Sahrens 		}
2104789Sahrens 		break;
2105789Sahrens 
2106789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2107789Sahrens 		/*
2108789Sahrens 		 * Getting the precise mountpoint can be tricky.
2109789Sahrens 		 *
2110789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2111789Sahrens 		 *  - for default mountpoints, construct it as /zfs/<dataset>
2112789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2113789Sahrens 		 *    after our ancestor and append it to the inherited value.
2114789Sahrens 		 *
2115789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2116789Sahrens 		 * root to any values we return.
2117789Sahrens 		 */
21181544Seschrock 		root = zhp->zfs_root;
21191356Seschrock 		str = getprop_string(zhp, prop, &source);
21201356Seschrock 
21211356Seschrock 		if (str[0] == '\0') {
2122789Sahrens 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
2123789Sahrens 			    root, zhp->zfs_name);
21241356Seschrock 		} else if (str[0] == '/') {
21251356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2126789Sahrens 
2127789Sahrens 			if (relpath[0] == '/')
2128789Sahrens 				relpath++;
21291356Seschrock 			if (str[1] == '\0')
21301356Seschrock 				str++;
2131789Sahrens 
2132789Sahrens 			if (relpath[0] == '\0')
2133789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
21341356Seschrock 				    root, str);
2135789Sahrens 			else
2136789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
21371356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2138789Sahrens 				    relpath);
2139789Sahrens 		} else {
2140789Sahrens 			/* 'legacy' or 'none' */
21411356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2142789Sahrens 		}
2143789Sahrens 
2144789Sahrens 		break;
2145789Sahrens 
2146789Sahrens 	case ZFS_PROP_ORIGIN:
21472885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2148789Sahrens 		    proplen);
2149789Sahrens 		/*
2150789Sahrens 		 * If there is no parent at all, return failure to indicate that
2151789Sahrens 		 * it doesn't apply to this dataset.
2152789Sahrens 		 */
2153789Sahrens 		if (propbuf[0] == '\0')
2154789Sahrens 			return (-1);
2155789Sahrens 		break;
2156789Sahrens 
2157789Sahrens 	case ZFS_PROP_QUOTA:
21585378Sck153898 	case ZFS_PROP_REFQUOTA:
2159789Sahrens 	case ZFS_PROP_RESERVATION:
21605378Sck153898 	case ZFS_PROP_REFRESERVATION:
21615378Sck153898 
21622082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
21632082Seschrock 			return (-1);
2164789Sahrens 
2165789Sahrens 		/*
2166789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2167789Sahrens 		 * (unless literal is set), and indicate that it's the default
2168789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2169789Sahrens 		 * that its set locally.
2170789Sahrens 		 */
2171789Sahrens 		if (val == 0) {
2172789Sahrens 			if (literal)
2173789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2174789Sahrens 			else
2175789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2176789Sahrens 		} else {
2177789Sahrens 			if (literal)
21782856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
21793912Slling 				    (u_longlong_t)val);
2180789Sahrens 			else
2181789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2182789Sahrens 		}
2183789Sahrens 		break;
2184789Sahrens 
2185789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
21862082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
21872082Seschrock 			return (-1);
21882856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
21892856Snd150628 		    val / 100, (longlong_t)val % 100);
2190789Sahrens 		break;
2191789Sahrens 
2192789Sahrens 	case ZFS_PROP_TYPE:
2193789Sahrens 		switch (zhp->zfs_type) {
2194789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2195789Sahrens 			str = "filesystem";
2196789Sahrens 			break;
2197789Sahrens 		case ZFS_TYPE_VOLUME:
2198789Sahrens 			str = "volume";
2199789Sahrens 			break;
2200789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2201789Sahrens 			str = "snapshot";
2202789Sahrens 			break;
2203789Sahrens 		default:
22042082Seschrock 			abort();
2205789Sahrens 		}
2206789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2207789Sahrens 		break;
2208789Sahrens 
2209789Sahrens 	case ZFS_PROP_MOUNTED:
2210789Sahrens 		/*
2211789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2212789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2213789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2214789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2215789Sahrens 		 */
22162082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
22172082Seschrock 		    src, &source, &val) != 0)
22182082Seschrock 			return (-1);
22192082Seschrock 		if (val)
2220789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2221789Sahrens 		else
2222789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2223789Sahrens 		break;
2224789Sahrens 
2225789Sahrens 	case ZFS_PROP_NAME:
2226789Sahrens 		/*
2227789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2228789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2229789Sahrens 		 * consumers.
2230789Sahrens 		 */
2231789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2232789Sahrens 		break;
2233789Sahrens 
2234789Sahrens 	default:
22354577Sahrens 		switch (zfs_prop_get_type(prop)) {
22364787Sahrens 		case PROP_TYPE_NUMBER:
22374577Sahrens 			if (get_numeric_property(zhp, prop, src,
22384577Sahrens 			    &source, &val) != 0)
22394577Sahrens 				return (-1);
22404577Sahrens 			if (literal)
22414577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
22424577Sahrens 				    (u_longlong_t)val);
22434577Sahrens 			else
22444577Sahrens 				zfs_nicenum(val, propbuf, proplen);
22454577Sahrens 			break;
22464577Sahrens 
22474787Sahrens 		case PROP_TYPE_STRING:
22484577Sahrens 			(void) strlcpy(propbuf,
22494577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
22504577Sahrens 			break;
22514577Sahrens 
22524787Sahrens 		case PROP_TYPE_INDEX:
22534861Sahrens 			if (get_numeric_property(zhp, prop, src,
22544861Sahrens 			    &source, &val) != 0)
22554861Sahrens 				return (-1);
22564861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
22574577Sahrens 				return (-1);
22584577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
22594577Sahrens 			break;
22604577Sahrens 
22614577Sahrens 		default:
22624577Sahrens 			abort();
22634577Sahrens 		}
2264789Sahrens 	}
2265789Sahrens 
2266789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2267789Sahrens 
2268789Sahrens 	return (0);
2269789Sahrens }
2270789Sahrens 
2271789Sahrens /*
2272789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2273789Sahrens  * the given property is the appropriate type; should only be used with
2274789Sahrens  * hard-coded property types.
2275789Sahrens  */
2276789Sahrens uint64_t
2277789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2278789Sahrens {
2279789Sahrens 	char *source;
22802082Seschrock 	uint64_t val;
22812082Seschrock 
22825367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
22832082Seschrock 
22842082Seschrock 	return (val);
2285789Sahrens }
2286789Sahrens 
2287789Sahrens /*
2288789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2289789Sahrens  */
2290789Sahrens int
2291789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
22925094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2293789Sahrens {
2294789Sahrens 	char *source;
2295789Sahrens 
2296789Sahrens 	/*
2297789Sahrens 	 * Check to see if this property applies to our object
2298789Sahrens 	 */
22994849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
23003237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
23012082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
23022082Seschrock 		    zfs_prop_to_name(prop)));
23034849Sahrens 	}
2304789Sahrens 
2305789Sahrens 	if (src)
23065094Slling 		*src = ZPROP_SRC_NONE;
2307789Sahrens 
23082082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
23092082Seschrock 		return (-1);
2310789Sahrens 
2311789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2312789Sahrens 
2313789Sahrens 	return (0);
2314789Sahrens }
2315789Sahrens 
2316789Sahrens /*
2317789Sahrens  * Returns the name of the given zfs handle.
2318789Sahrens  */
2319789Sahrens const char *
2320789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2321789Sahrens {
2322789Sahrens 	return (zhp->zfs_name);
2323789Sahrens }
2324789Sahrens 
2325789Sahrens /*
2326789Sahrens  * Returns the type of the given zfs handle.
2327789Sahrens  */
2328789Sahrens zfs_type_t
2329789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2330789Sahrens {
2331789Sahrens 	return (zhp->zfs_type);
2332789Sahrens }
2333789Sahrens 
2334789Sahrens /*
23351356Seschrock  * Iterate over all child filesystems
2336789Sahrens  */
2337789Sahrens int
23381356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2339789Sahrens {
2340789Sahrens 	zfs_cmd_t zc = { 0 };
2341789Sahrens 	zfs_handle_t *nzhp;
2342789Sahrens 	int ret;
2343789Sahrens 
23445367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
23455367Sahrens 		return (0);
23465367Sahrens 
2347789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
23482082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2349789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2350789Sahrens 		/*
2351789Sahrens 		 * Ignore private dataset names.
2352789Sahrens 		 */
2353789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2354789Sahrens 			continue;
2355789Sahrens 
2356789Sahrens 		/*
2357789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2358789Sahrens 		 * that the pool has since been removed.
2359789Sahrens 		 */
23602082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
23612082Seschrock 		    zc.zc_name)) == NULL)
2362789Sahrens 			continue;
2363789Sahrens 
2364789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2365789Sahrens 			return (ret);
2366789Sahrens 	}
2367789Sahrens 
2368789Sahrens 	/*
2369789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2370789Sahrens 	 * returned, then the underlying dataset has been removed since we
2371789Sahrens 	 * obtained the handle.
2372789Sahrens 	 */
2373789Sahrens 	if (errno != ESRCH && errno != ENOENT)
23742082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
23752082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2376789Sahrens 
23771356Seschrock 	return (0);
23781356Seschrock }
23791356Seschrock 
23801356Seschrock /*
23811356Seschrock  * Iterate over all snapshots
23821356Seschrock  */
23831356Seschrock int
23841356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23851356Seschrock {
23861356Seschrock 	zfs_cmd_t zc = { 0 };
23871356Seschrock 	zfs_handle_t *nzhp;
23881356Seschrock 	int ret;
2389789Sahrens 
23905367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
23915367Sahrens 		return (0);
23925367Sahrens 
2393789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
23942082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
23952082Seschrock 	    &zc) == 0;
2396789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2397789Sahrens 
23982082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
23992082Seschrock 		    zc.zc_name)) == NULL)
2400789Sahrens 			continue;
2401789Sahrens 
2402789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2403789Sahrens 			return (ret);
2404789Sahrens 	}
2405789Sahrens 
2406789Sahrens 	/*
2407789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2408789Sahrens 	 * returned, then the underlying dataset has been removed since we
2409789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2410789Sahrens 	 */
2411789Sahrens 	if (errno != ESRCH && errno != ENOENT)
24122082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
24132082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2414789Sahrens 
2415789Sahrens 	return (0);
2416789Sahrens }
2417789Sahrens 
2418789Sahrens /*
24191356Seschrock  * Iterate over all children, snapshots and filesystems
24201356Seschrock  */
24211356Seschrock int
24221356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
24231356Seschrock {
24241356Seschrock 	int ret;
24251356Seschrock 
24261356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
24271356Seschrock 		return (ret);
24281356Seschrock 
24291356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
24301356Seschrock }
24311356Seschrock 
24321356Seschrock /*
2433789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2434789Sahrens  * Can return NULL if this is a pool.
2435789Sahrens  */
2436789Sahrens static int
2437789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2438789Sahrens {
2439789Sahrens 	char *loc;
2440789Sahrens 
2441789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2442789Sahrens 		return (-1);
2443789Sahrens 
2444789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2445789Sahrens 	buf[loc - path] = '\0';
2446789Sahrens 
2447789Sahrens 	return (0);
2448789Sahrens }
2449789Sahrens 
2450789Sahrens /*
24514490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
24524490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
24534490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
24544490Svb160487  * length of already existing prefix of the given path.  We also fetch the
24554490Svb160487  * 'zoned' property, which is used to validate property settings when creating
24564490Svb160487  * new datasets.
2457789Sahrens  */
2458789Sahrens static int
24594490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
24604490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2461789Sahrens {
2462789Sahrens 	zfs_cmd_t zc = { 0 };
2463789Sahrens 	char parent[ZFS_MAXNAMELEN];
2464789Sahrens 	char *slash;
24651356Seschrock 	zfs_handle_t *zhp;
24662082Seschrock 	char errbuf[1024];
24672082Seschrock 
24682082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
24692082Seschrock 	    path);
2470789Sahrens 
2471789Sahrens 	/* get parent, and check to see if this is just a pool */
2472789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
24732082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24742082Seschrock 		    "missing dataset name"));
24752082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2476789Sahrens 	}
2477789Sahrens 
2478789Sahrens 	/* check to see if the pool exists */
2479789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2480789Sahrens 		slash = parent + strlen(parent);
2481789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2482789Sahrens 	zc.zc_name[slash - parent] = '\0';
24832082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2484789Sahrens 	    errno == ENOENT) {
24852082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24862082Seschrock 		    "no such pool '%s'"), zc.zc_name);
24872082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2488789Sahrens 	}
2489789Sahrens 
2490789Sahrens 	/* check to see if the parent dataset exists */
24914490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
24924490Svb160487 		if (errno == ENOENT && accept_ancestor) {
24934490Svb160487 			/*
24944490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
24954490Svb160487 			 */
24964490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
24974490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24984490Svb160487 				    "no such pool '%s'"), zc.zc_name);
24994490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
25004490Svb160487 			}
25014490Svb160487 		} else if (errno == ENOENT) {
25022082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25032082Seschrock 			    "parent does not exist"));
25042082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
25054490Svb160487 		} else
25062082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2507789Sahrens 	}
2508789Sahrens 
25092676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2510789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
25112676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
25122082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
25131356Seschrock 		zfs_close(zhp);
2514789Sahrens 		return (-1);
2515789Sahrens 	}
2516789Sahrens 
2517789Sahrens 	/* make sure parent is a filesystem */
25181356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
25192082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25202082Seschrock 		    "parent is not a filesystem"));
25212082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
25221356Seschrock 		zfs_close(zhp);
2523789Sahrens 		return (-1);
2524789Sahrens 	}
2525789Sahrens 
25261356Seschrock 	zfs_close(zhp);
25274490Svb160487 	if (prefixlen != NULL)
25284490Svb160487 		*prefixlen = strlen(parent);
25294490Svb160487 	return (0);
25304490Svb160487 }
25314490Svb160487 
25324490Svb160487 /*
25334490Svb160487  * Finds whether the dataset of the given type(s) exists.
25344490Svb160487  */
25354490Svb160487 boolean_t
25364490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
25374490Svb160487 {
25384490Svb160487 	zfs_handle_t *zhp;
25394490Svb160487 
25405326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
25414490Svb160487 		return (B_FALSE);
25424490Svb160487 
25434490Svb160487 	/*
25444490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
25454490Svb160487 	 */
25464490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
25474490Svb160487 		int ds_type = zhp->zfs_type;
25484490Svb160487 
25494490Svb160487 		zfs_close(zhp);
25504490Svb160487 		if (types & ds_type)
25514490Svb160487 			return (B_TRUE);
25524490Svb160487 	}
25534490Svb160487 	return (B_FALSE);
25544490Svb160487 }
25554490Svb160487 
25564490Svb160487 /*
25575367Sahrens  * Given a path to 'target', create all the ancestors between
25585367Sahrens  * the prefixlen portion of the path, and the target itself.
25595367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
25605367Sahrens  */
25615367Sahrens int
25625367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
25635367Sahrens {
25645367Sahrens 	zfs_handle_t *h;
25655367Sahrens 	char *cp;
25665367Sahrens 	const char *opname;
25675367Sahrens 
25685367Sahrens 	/* make sure prefix exists */
25695367Sahrens 	cp = target + prefixlen;
25705367Sahrens 	if (*cp != '/') {
25715367Sahrens 		assert(strchr(cp, '/') == NULL);
25725367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
25735367Sahrens 	} else {
25745367Sahrens 		*cp = '\0';
25755367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
25765367Sahrens 		*cp = '/';
25775367Sahrens 	}
25785367Sahrens 	if (h == NULL)
25795367Sahrens 		return (-1);
25805367Sahrens 	zfs_close(h);
25815367Sahrens 
25825367Sahrens 	/*
25835367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
25845367Sahrens 	 * up to the prefixlen-long one.
25855367Sahrens 	 */
25865367Sahrens 	for (cp = target + prefixlen + 1;
25875367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
25885367Sahrens 		char *logstr;
25895367Sahrens 
25905367Sahrens 		*cp = '\0';
25915367Sahrens 
25925367Sahrens 		h = make_dataset_handle(hdl, target);
25935367Sahrens 		if (h) {
25945367Sahrens 			/* it already exists, nothing to do here */
25955367Sahrens 			zfs_close(h);
25965367Sahrens 			continue;
25975367Sahrens 		}
25985367Sahrens 
25995367Sahrens 		logstr = hdl->libzfs_log_str;
26005367Sahrens 		hdl->libzfs_log_str = NULL;
26015367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
26025367Sahrens 		    NULL) != 0) {
26035367Sahrens 			hdl->libzfs_log_str = logstr;
26045367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
26055367Sahrens 			goto ancestorerr;
26065367Sahrens 		}
26075367Sahrens 
26085367Sahrens 		hdl->libzfs_log_str = logstr;
26095367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
26105367Sahrens 		if (h == NULL) {
26115367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
26125367Sahrens 			goto ancestorerr;
26135367Sahrens 		}
26145367Sahrens 
26155367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
26165367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
26175367Sahrens 			goto ancestorerr;
26185367Sahrens 		}
26195367Sahrens 
26205367Sahrens 		if (zfs_share(h) != 0) {
26215367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
26225367Sahrens 			goto ancestorerr;
26235367Sahrens 		}
26245367Sahrens 
26255367Sahrens 		zfs_close(h);
26265367Sahrens 	}
26275367Sahrens 
26285367Sahrens 	return (0);
26295367Sahrens 
26305367Sahrens ancestorerr:
26315367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26325367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
26335367Sahrens 	return (-1);
26345367Sahrens }
26355367Sahrens 
26365367Sahrens /*
26374490Svb160487  * Creates non-existing ancestors of the given path.
26384490Svb160487  */
26394490Svb160487 int
26404490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
26414490Svb160487 {
26424490Svb160487 	int prefix;
26434490Svb160487 	uint64_t zoned;
26444490Svb160487 	char *path_copy;
26454490Svb160487 	int rc;
26464490Svb160487 
26474490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
26484490Svb160487 		return (-1);
26494490Svb160487 
26504490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
26514490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
26524490Svb160487 		free(path_copy);
26534490Svb160487 	}
26544490Svb160487 	if (path_copy == NULL || rc != 0)
26554490Svb160487 		return (-1);
26564490Svb160487 
2657789Sahrens 	return (0);
2658789Sahrens }
2659789Sahrens 
2660789Sahrens /*
26612676Seschrock  * Create a new filesystem or volume.
2662789Sahrens  */
2663789Sahrens int
26642082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
26652676Seschrock     nvlist_t *props)
2666789Sahrens {
2667789Sahrens 	zfs_cmd_t zc = { 0 };
2668789Sahrens 	int ret;
2669789Sahrens 	uint64_t size = 0;
2670789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
26712082Seschrock 	char errbuf[1024];
26722676Seschrock 	uint64_t zoned;
26732082Seschrock 
26742082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
26752082Seschrock 	    "cannot create '%s'"), path);
2676789Sahrens 
2677789Sahrens 	/* validate the path, taking care to note the extended error message */
26785326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
26792082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2680789Sahrens 
2681789Sahrens 	/* validate parents exist */
26824490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2683789Sahrens 		return (-1);
2684789Sahrens 
2685789Sahrens 	/*
2686789Sahrens 	 * The failure modes when creating a dataset of a different type over
2687789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2688789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2689789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2690789Sahrens 	 * first try to see if the dataset exists.
2691789Sahrens 	 */
2692789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
26935094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
26942082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26952082Seschrock 		    "dataset already exists"));
26962082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2697789Sahrens 	}
2698789Sahrens 
2699789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2700789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2701789Sahrens 	else
2702789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2703789Sahrens 
27045094Slling 	if (props && (props = zfs_validate_properties(hdl, type, props,
27053912Slling 	    zoned, NULL, errbuf)) == 0)
27062676Seschrock 		return (-1);
27072676Seschrock 
2708789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
27091133Seschrock 		/*
27101133Seschrock 		 * If we are creating a volume, the size and block size must
27111133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
27121133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
27131133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
27141133Seschrock 		 * zero.
27151133Seschrock 		 */
27162676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
27172676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
27182676Seschrock 			nvlist_free(props);
27192082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27202676Seschrock 			    "missing volume size"));
27212676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2722789Sahrens 		}
2723789Sahrens 
27242676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
27252676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
27262676Seschrock 		    &blocksize)) != 0) {
27272676Seschrock 			if (ret == ENOENT) {
27282676Seschrock 				blocksize = zfs_prop_default_numeric(
27292676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
27302676Seschrock 			} else {
27312676Seschrock 				nvlist_free(props);
27322676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27332676Seschrock 				    "missing volume block size"));
27342676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
27352676Seschrock 			}
27362676Seschrock 		}
27372676Seschrock 
27382676Seschrock 		if (size == 0) {
27392676Seschrock 			nvlist_free(props);
27402082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27412676Seschrock 			    "volume size cannot be zero"));
27422676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
27431133Seschrock 		}
27441133Seschrock 
27451133Seschrock 		if (size % blocksize != 0) {
27462676Seschrock 			nvlist_free(props);
27472082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27482676Seschrock 			    "volume size must be a multiple of volume block "
27492676Seschrock 			    "size"));
27502676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
27511133Seschrock 		}
2752789Sahrens 	}
2753789Sahrens 
27545094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
27552676Seschrock 		return (-1);
27562676Seschrock 	nvlist_free(props);
27572676Seschrock 
2758789Sahrens 	/* create the dataset */
27594543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2760789Sahrens 
27613912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
27622082Seschrock 		ret = zvol_create_link(hdl, path);
27633912Slling 		if (ret) {
27643912Slling 			(void) zfs_standard_error(hdl, errno,
27653912Slling 			    dgettext(TEXT_DOMAIN,
27663912Slling 			    "Volume successfully created, but device links "
27673912Slling 			    "were not created"));
27683912Slling 			zcmd_free_nvlists(&zc);
27693912Slling 			return (-1);
27703912Slling 		}
27713912Slling 	}
2772789Sahrens 
27732676Seschrock 	zcmd_free_nvlists(&zc);
27742676Seschrock 
2775789Sahrens 	/* check for failure */
2776789Sahrens 	if (ret != 0) {
2777789Sahrens 		char parent[ZFS_MAXNAMELEN];
2778789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2779789Sahrens 
2780789Sahrens 		switch (errno) {
2781789Sahrens 		case ENOENT:
27822082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27832082Seschrock 			    "no such parent '%s'"), parent);
27842082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2785789Sahrens 
2786789Sahrens 		case EINVAL:
27872082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27883413Smmusante 			    "parent '%s' is not a filesystem"), parent);
27892082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2790789Sahrens 
2791789Sahrens 		case EDOM:
27922082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27932676Seschrock 			    "volume block size must be power of 2 from "
27942676Seschrock 			    "%u to %uk"),
2795789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2796789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
27972082Seschrock 
27982676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
27992082Seschrock 
28004603Sahrens 		case ENOTSUP:
28014603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28024603Sahrens 			    "pool must be upgraded to set this "
28034603Sahrens 			    "property or value"));
28044603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
28054603Sahrens 
2806789Sahrens #ifdef _ILP32
2807789Sahrens 		case EOVERFLOW:
2808789Sahrens 			/*
2809789Sahrens 			 * This platform can't address a volume this big.
2810789Sahrens 			 */
28112082Seschrock 			if (type == ZFS_TYPE_VOLUME)
28122082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
28132082Seschrock 				    errbuf));
2814789Sahrens #endif
28152082Seschrock 			/* FALLTHROUGH */
2816789Sahrens 		default:
28172082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2818789Sahrens 		}
2819789Sahrens 	}
2820789Sahrens 
2821789Sahrens 	return (0);
2822789Sahrens }
2823789Sahrens 
2824789Sahrens /*
2825789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2826789Sahrens  * isn't mounted, and that there are no active dependents.
2827789Sahrens  */
2828789Sahrens int
2829789Sahrens zfs_destroy(zfs_handle_t *zhp)
2830789Sahrens {
2831789Sahrens 	zfs_cmd_t zc = { 0 };
2832789Sahrens 
2833789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2834789Sahrens 
28352676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
28363126Sahl 		/*
28374543Smarks 		 * If user doesn't have permissions to unshare volume, then
28384543Smarks 		 * abort the request.  This would only happen for a
28394543Smarks 		 * non-privileged user.
28403126Sahl 		 */
28414543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
28424543Smarks 			return (-1);
28434543Smarks 		}
28443126Sahl 
28452082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2846789Sahrens 			return (-1);
2847789Sahrens 
2848789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2849789Sahrens 	} else {
2850789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2851789Sahrens 	}
2852789Sahrens 
28534543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
28543237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
28552082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
28562082Seschrock 		    zhp->zfs_name));
28572199Sahrens 	}
2858789Sahrens 
2859789Sahrens 	remove_mountpoint(zhp);
2860789Sahrens 
2861789Sahrens 	return (0);
2862789Sahrens }
2863789Sahrens 
28642199Sahrens struct destroydata {
28652199Sahrens 	char *snapname;
28662199Sahrens 	boolean_t gotone;
28673265Sahrens 	boolean_t closezhp;
28682199Sahrens };
28692199Sahrens 
28702199Sahrens static int
28712199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
28722199Sahrens {
28732199Sahrens 	struct destroydata *dd = arg;
28742199Sahrens 	zfs_handle_t *szhp;
28752199Sahrens 	char name[ZFS_MAXNAMELEN];
28763265Sahrens 	boolean_t closezhp = dd->closezhp;
28773265Sahrens 	int rv;
28782199Sahrens 
28792676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
28802676Seschrock 	(void) strlcat(name, "@", sizeof (name));
28812676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
28822199Sahrens 
28832199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
28842199Sahrens 	if (szhp) {
28852199Sahrens 		dd->gotone = B_TRUE;
28862199Sahrens 		zfs_close(szhp);
28872199Sahrens 	}
28882199Sahrens 
28892199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
28902199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
28912199Sahrens 		/*
28922199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
28932199Sahrens 		 * return an error, because then we wouldn't visit all
28942199Sahrens 		 * the volumes.
28952199Sahrens 		 */
28962199Sahrens 	}
28972199Sahrens 
28983265Sahrens 	dd->closezhp = B_TRUE;
28993265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
29003265Sahrens 	if (closezhp)
29013265Sahrens 		zfs_close(zhp);
29023265Sahrens 	return (rv);
29032199Sahrens }
29042199Sahrens 
29052199Sahrens /*
29062199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
29072199Sahrens  */
29082199Sahrens int
29092199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
29102199Sahrens {
29112199Sahrens 	zfs_cmd_t zc = { 0 };
29122199Sahrens 	int ret;
29132199Sahrens 	struct destroydata dd = { 0 };
29142199Sahrens 
29152199Sahrens 	dd.snapname = snapname;
29162199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
29172199Sahrens 
29182199Sahrens 	if (!dd.gotone) {
29193237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
29202199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
29212199Sahrens 		    zhp->zfs_name, snapname));
29222199Sahrens 	}
29232199Sahrens 
29242199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
29252676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
29262199Sahrens 
29274543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
29282199Sahrens 	if (ret != 0) {
29292199Sahrens 		char errbuf[1024];
29302199Sahrens 
29312199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29322199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
29332199Sahrens 
29342199Sahrens 		switch (errno) {
29352199Sahrens 		case EEXIST:
29362199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29372199Sahrens 			    "snapshot is cloned"));
29382199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
29392199Sahrens 
29402199Sahrens 		default:
29412199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
29422199Sahrens 			    errbuf));
29432199Sahrens 		}
29442199Sahrens 	}
29452199Sahrens 
29462199Sahrens 	return (0);
29472199Sahrens }
29482199Sahrens 
2949789Sahrens /*
2950789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
2951789Sahrens  */
2952789Sahrens int
29532676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2954789Sahrens {
2955789Sahrens 	zfs_cmd_t zc = { 0 };
2956789Sahrens 	char parent[ZFS_MAXNAMELEN];
2957789Sahrens 	int ret;
29582082Seschrock 	char errbuf[1024];
29592082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
29602676Seschrock 	zfs_type_t type;
29612676Seschrock 	uint64_t zoned;
2962789Sahrens 
2963789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2964789Sahrens 
29652082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29662082Seschrock 	    "cannot create '%s'"), target);
29672082Seschrock 
2968789Sahrens 	/* validate the target name */
29695326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
29702082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2971789Sahrens 
2972789Sahrens 	/* validate parents exist */
29734490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2974789Sahrens 		return (-1);
2975789Sahrens 
2976789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
2977789Sahrens 
2978789Sahrens 	/* do the clone */
29792676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
2980789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
29812744Snn35248 		type = ZFS_TYPE_VOLUME;
29822676Seschrock 	} else {
2983789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
29842744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
29852676Seschrock 	}
29862676Seschrock 
29872676Seschrock 	if (props) {
29885094Slling 		if ((props = zfs_validate_properties(hdl, type, props,
29893912Slling 		    zoned, zhp, errbuf)) == NULL)
29902676Seschrock 			return (-1);
29912676Seschrock 
29925094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
29932676Seschrock 			nvlist_free(props);
29942676Seschrock 			return (-1);
29952676Seschrock 		}
29962676Seschrock 
29972676Seschrock 		nvlist_free(props);
29982676Seschrock 	}
2999789Sahrens 
3000789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
30012676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
30024543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3003789Sahrens 
30042676Seschrock 	zcmd_free_nvlists(&zc);
30052676Seschrock 
3006789Sahrens 	if (ret != 0) {
3007789Sahrens 		switch (errno) {
3008789Sahrens 
3009789Sahrens 		case ENOENT:
3010789Sahrens 			/*
3011789Sahrens 			 * The parent doesn't exist.  We should have caught this
3012789Sahrens 			 * above, but there may a race condition that has since
3013789Sahrens 			 * destroyed the parent.
3014789Sahrens 			 *
3015789Sahrens 			 * At this point, we don't know whether it's the source
3016789Sahrens 			 * that doesn't exist anymore, or whether the target
3017789Sahrens 			 * dataset doesn't exist.
3018789Sahrens 			 */
30192082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
30202082Seschrock 			    "no such parent '%s'"), parent);
30212082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
30222082Seschrock 
30232082Seschrock 		case EXDEV:
30242082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
30252082Seschrock 			    "source and target pools differ"));
30262082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
30272082Seschrock 			    errbuf));
30282082Seschrock 
30292082Seschrock 		default:
30302082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
30312082Seschrock 			    errbuf));
30322082Seschrock 		}
30332676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
30342082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
30352082Seschrock 	}
30362082Seschrock 
30372082Seschrock 	return (ret);
30382082Seschrock }
30392082Seschrock 
30402082Seschrock typedef struct promote_data {
30412082Seschrock 	char cb_mountpoint[MAXPATHLEN];
30422082Seschrock 	const char *cb_target;
30432082Seschrock 	const char *cb_errbuf;
30442082Seschrock 	uint64_t cb_pivot_txg;
30452082Seschrock } promote_data_t;
30462082Seschrock 
30472082Seschrock static int
30482082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
30492082Seschrock {
30502082Seschrock 	promote_data_t *pd = data;
30512082Seschrock 	zfs_handle_t *szhp;
30522082Seschrock 	char snapname[MAXPATHLEN];
30533265Sahrens 	int rv = 0;
30542082Seschrock 
30552082Seschrock 	/* We don't care about snapshots after the pivot point */
30563265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
30573265Sahrens 		zfs_close(zhp);
30582082Seschrock 		return (0);
30593265Sahrens 	}
30602082Seschrock 
30612417Sahrens 	/* Remove the device link if it's a zvol. */
30622676Seschrock 	if (ZFS_IS_VOLUME(zhp))
30632417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
30642082Seschrock 
30652082Seschrock 	/* Check for conflicting names */
30662676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
30672676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
30682082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
30692082Seschrock 	if (szhp != NULL) {
30702082Seschrock 		zfs_close(szhp);
30712082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
30722082Seschrock 		    "snapshot name '%s' from origin \n"
30732082Seschrock 		    "conflicts with '%s' from target"),
30742082Seschrock 		    zhp->zfs_name, snapname);
30753265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
30762082Seschrock 	}
30773265Sahrens 	zfs_close(zhp);
30783265Sahrens 	return (rv);
30792082Seschrock }
30802082Seschrock 
30812417Sahrens static int
30822417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
30832417Sahrens {
30842417Sahrens 	promote_data_t *pd = data;
30852417Sahrens 
30862417Sahrens 	/* We don't care about snapshots after the pivot point */
30873265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
30883265Sahrens 		/* Create the device link if it's a zvol. */
30893265Sahrens 		if (ZFS_IS_VOLUME(zhp))
30903265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
30913265Sahrens 	}
30923265Sahrens 
30933265Sahrens 	zfs_close(zhp);
30942417Sahrens 	return (0);
30952417Sahrens }
30962417Sahrens 
30972082Seschrock /*
30982082Seschrock  * Promotes the given clone fs to be the clone parent.
30992082Seschrock  */
31002082Seschrock int
31012082Seschrock zfs_promote(zfs_handle_t *zhp)
31022082Seschrock {
31032082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31042082Seschrock 	zfs_cmd_t zc = { 0 };
31052082Seschrock 	char parent[MAXPATHLEN];
31062082Seschrock 	char *cp;
31072082Seschrock 	int ret;
31082082Seschrock 	zfs_handle_t *pzhp;
31092082Seschrock 	promote_data_t pd;
31102082Seschrock 	char errbuf[1024];
31112082Seschrock 
31122082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31132082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
31142082Seschrock 
31152082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
31162082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31172082Seschrock 		    "snapshots can not be promoted"));
31182082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
31192082Seschrock 	}
31202082Seschrock 
31215367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
31222082Seschrock 	if (parent[0] == '\0') {
31232082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31242082Seschrock 		    "not a cloned filesystem"));
31252082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
31262082Seschrock 	}
31272082Seschrock 	cp = strchr(parent, '@');
31282082Seschrock 	*cp = '\0';
31292082Seschrock 
31302082Seschrock 	/* Walk the snapshots we will be moving */
31315367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
31322082Seschrock 	if (pzhp == NULL)
31332082Seschrock 		return (-1);
31342082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
31352082Seschrock 	zfs_close(pzhp);
31362082Seschrock 	pd.cb_target = zhp->zfs_name;
31372082Seschrock 	pd.cb_errbuf = errbuf;
31385094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
31392082Seschrock 	if (pzhp == NULL)
31402082Seschrock 		return (-1);
31412082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
31422082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
31432082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
31442417Sahrens 	if (ret != 0) {
31452417Sahrens 		zfs_close(pzhp);
31462082Seschrock 		return (-1);
31472417Sahrens 	}
31482082Seschrock 
31492082Seschrock 	/* issue the ioctl */
31505367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
31512676Seschrock 	    sizeof (zc.zc_value));
31522082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31534543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
31542082Seschrock 
31552082Seschrock 	if (ret != 0) {
31562417Sahrens 		int save_errno = errno;
31572417Sahrens 
31582417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
31592417Sahrens 		zfs_close(pzhp);
31602417Sahrens 
31612417Sahrens 		switch (save_errno) {
3162789Sahrens 		case EEXIST:
3163789Sahrens 			/*
31642082Seschrock 			 * There is a conflicting snapshot name.  We
31652082Seschrock 			 * should have caught this above, but they could
31662082Seschrock 			 * have renamed something in the mean time.
3167789Sahrens 			 */
31682082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31692082Seschrock 			    "conflicting snapshot name from parent '%s'"),
31702082Seschrock 			    parent);
31712082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3172789Sahrens 
3173789Sahrens 		default:
31742417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3175789Sahrens 		}
31762417Sahrens 	} else {
31772417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3178789Sahrens 	}
3179789Sahrens 
31802417Sahrens 	zfs_close(pzhp);
3181789Sahrens 	return (ret);
3182789Sahrens }
3183789Sahrens 
31844007Smmusante struct createdata {
31854007Smmusante 	const char *cd_snapname;
31864007Smmusante 	int cd_ifexists;
31874007Smmusante };
31884007Smmusante 
31892199Sahrens static int
31902199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
31912199Sahrens {
31924007Smmusante 	struct createdata *cd = arg;
31932676Seschrock 	int ret;
31942199Sahrens 
31952199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31962199Sahrens 		char name[MAXPATHLEN];
31972199Sahrens 
31982676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31992676Seschrock 		(void) strlcat(name, "@", sizeof (name));
32004007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
32014007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
32024007Smmusante 		    cd->cd_ifexists);
32032199Sahrens 		/*
32042199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
32052199Sahrens 		 * return an error, because then we wouldn't visit all
32062199Sahrens 		 * the volumes.
32072199Sahrens 		 */
32082199Sahrens 	}
32092676Seschrock 
32104007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
32112676Seschrock 
32122676Seschrock 	zfs_close(zhp);
32132676Seschrock 
32142676Seschrock 	return (ret);
32152199Sahrens }
32162199Sahrens 
3217789Sahrens /*
32183504Sahl  * Takes a snapshot of the given dataset.
3219789Sahrens  */
3220789Sahrens int
32212199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3222789Sahrens {
3223789Sahrens 	const char *delim;
3224789Sahrens 	char *parent;
3225789Sahrens 	zfs_handle_t *zhp;
3226789Sahrens 	zfs_cmd_t zc = { 0 };
3227789Sahrens 	int ret;
32282082Seschrock 	char errbuf[1024];
32292082Seschrock 
32302082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32312082Seschrock 	    "cannot snapshot '%s'"), path);
32322082Seschrock 
32332082Seschrock 	/* validate the target name */
32345326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
32352082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3236789Sahrens 
3237789Sahrens 	/* make sure the parent exists and is of the appropriate type */
32382199Sahrens 	delim = strchr(path, '@');
32392082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
32402082Seschrock 		return (-1);
3241789Sahrens 	(void) strncpy(parent, path, delim - path);
3242789Sahrens 	parent[delim - path] = '\0';
3243789Sahrens 
32442082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3245789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3246789Sahrens 		free(parent);
3247789Sahrens 		return (-1);
3248789Sahrens 	}
3249789Sahrens 
32502199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
32512676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
32524543Smarks 	if (ZFS_IS_VOLUME(zhp))
32534543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
32544543Smarks 	else
32554543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
32562199Sahrens 	zc.zc_cookie = recursive;
32574543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
32582199Sahrens 
32592199Sahrens 	/*
32602199Sahrens 	 * if it was recursive, the one that actually failed will be in
32612199Sahrens 	 * zc.zc_name.
32622199Sahrens 	 */
32634543Smarks 	if (ret != 0)
32644543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32654543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
32664543Smarks 
32672199Sahrens 	if (ret == 0 && recursive) {
32684007Smmusante 		struct createdata cd;
32694007Smmusante 
32704007Smmusante 		cd.cd_snapname = delim + 1;
32714007Smmusante 		cd.cd_ifexists = B_FALSE;
32724007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
32732199Sahrens 	}
3274789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
32752082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
32762199Sahrens 		if (ret != 0) {
32774543Smarks 			(void) zfs_standard_error(hdl, errno,
32784543Smarks 			    dgettext(TEXT_DOMAIN,
32794543Smarks 			    "Volume successfully snapshotted, but device links "
32804543Smarks 			    "were not created"));
32814543Smarks 			free(parent);
32824543Smarks 			zfs_close(zhp);
32834543Smarks 			return (-1);
32842199Sahrens 		}
3285789Sahrens 	}
3286789Sahrens 
32872082Seschrock 	if (ret != 0)
32882082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3289789Sahrens 
3290789Sahrens 	free(parent);
3291789Sahrens 	zfs_close(zhp);
3292789Sahrens 
3293789Sahrens 	return (ret);
3294789Sahrens }
3295789Sahrens 
3296789Sahrens /*
32971294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
32981294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
32991294Slling  * is a dependent and we should just destroy it without checking the transaction
33001294Slling  * group.
3301789Sahrens  */
33021294Slling typedef struct rollback_data {
33031294Slling 	const char	*cb_target;		/* the snapshot */
33041294Slling 	uint64_t	cb_create;		/* creation time reference */
33051294Slling 	int		cb_error;
33062082Seschrock 	boolean_t	cb_dependent;
33071294Slling } rollback_data_t;
33081294Slling 
33091294Slling static int
33101294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
33111294Slling {
33121294Slling 	rollback_data_t *cbp = data;
33131294Slling 
33141294Slling 	if (!cbp->cb_dependent) {
33151294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
33161294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
33171294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
33181294Slling 		    cbp->cb_create) {
33194543Smarks 			char *logstr;
33201294Slling 
33212082Seschrock 			cbp->cb_dependent = B_TRUE;
33225446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
33235446Sahrens 			    rollback_destroy, cbp);
33242082Seschrock 			cbp->cb_dependent = B_FALSE;
33251294Slling 
33264543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
33274543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
33285446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
33294543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
33301294Slling 		}
33311294Slling 	} else {
33325446Sahrens 		cbp->cb_error |= zfs_destroy(zhp);
33331294Slling 	}
33341294Slling 
33351294Slling 	zfs_close(zhp);
33361294Slling 	return (0);
33371294Slling }
33381294Slling 
33391294Slling /*
33405446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
33415446Sahrens  * data changes since then and making it the active dataset.
33425446Sahrens  *
33435446Sahrens  * Any snapshots more recent than the target are destroyed, along with
33445446Sahrens  * their dependents.
33451294Slling  */
33465446Sahrens int
33475446Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap)
3348789Sahrens {
33495446Sahrens 	rollback_data_t cb = { 0 };
33505446Sahrens 	int err;
3351789Sahrens 	zfs_cmd_t zc = { 0 };
3352789Sahrens 
3353789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3354789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3355789Sahrens 
33565446Sahrens 	/*
33575446Sahrens 	 * Destroy all recent snapshots and its dependends.
33585446Sahrens 	 */
33595446Sahrens 	cb.cb_target = snap->zfs_name;
33605446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
33615446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
33625446Sahrens 
33635446Sahrens 	if (cb.cb_error != 0)
33645446Sahrens 		return (cb.cb_error);
33655446Sahrens 
33665446Sahrens 	/*
33675446Sahrens 	 * Now that we have verified that the snapshot is the latest,
33685446Sahrens 	 * rollback to the given snapshot.
33695446Sahrens 	 */
33705446Sahrens 
3371789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME &&
33722082Seschrock 	    zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3373789Sahrens 		return (-1);
3374789Sahrens 
3375789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3376789Sahrens 
33772676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3378789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3379789Sahrens 	else
3380789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3381789Sahrens 
3382789Sahrens 	/*
33835446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
33845446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
33855446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
33865446Sahrens 	 * an unlikely race condition where the user has taken a
33875446Sahrens 	 * snapshot since we verified that this was the most recent.
3388789Sahrens 	 */
33895446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
33903237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
33912082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
33922082Seschrock 		    zhp->zfs_name);
3393789Sahrens 	} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33945446Sahrens 		err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3395789Sahrens 	}
3396789Sahrens 
33975446Sahrens 	return (err);
33981294Slling }
33991294Slling 
34001294Slling /*
3401789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3402789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3403789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3404789Sahrens  * libzfs_graph.c.
3405789Sahrens  */
3406789Sahrens int
34072474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
34082474Seschrock     zfs_iter_f func, void *data)
3409789Sahrens {
3410789Sahrens 	char **dependents;
3411789Sahrens 	size_t count;
3412789Sahrens 	int i;
3413789Sahrens 	zfs_handle_t *child;
3414789Sahrens 	int ret = 0;
3415789Sahrens 
34162474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
34172474Seschrock 	    &dependents, &count) != 0)
34182474Seschrock 		return (-1);
34192474Seschrock 
3420789Sahrens 	for (i = 0; i < count; i++) {
34212082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
34222082Seschrock 		    dependents[i])) == NULL)
3423789Sahrens 			continue;
3424789Sahrens 
3425789Sahrens 		if ((ret = func(child, data)) != 0)
3426789Sahrens 			break;
3427789Sahrens 	}
3428789Sahrens 
3429789Sahrens 	for (i = 0; i < count; i++)
3430789Sahrens 		free(dependents[i]);
3431789Sahrens 	free(dependents);
3432789Sahrens 
3433789Sahrens 	return (ret);
3434789Sahrens }
3435789Sahrens 
3436789Sahrens /*
3437789Sahrens  * Renames the given dataset.
3438789Sahrens  */
3439789Sahrens int
34404490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3441789Sahrens {
3442789Sahrens 	int ret;
3443789Sahrens 	zfs_cmd_t zc = { 0 };
3444789Sahrens 	char *delim;
34454007Smmusante 	prop_changelist_t *cl = NULL;
34464007Smmusante 	zfs_handle_t *zhrp = NULL;
34474007Smmusante 	char *parentname = NULL;
3448789Sahrens 	char parent[ZFS_MAXNAMELEN];
34492082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34502082Seschrock 	char errbuf[1024];
3451789Sahrens 
3452789Sahrens 	/* if we have the same exact name, just return success */
3453789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3454789Sahrens 		return (0);
3455789Sahrens 
34562082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34572082Seschrock 	    "cannot rename to '%s'"), target);
34582082Seschrock 
3459789Sahrens 	/*
3460789Sahrens 	 * Make sure the target name is valid
3461789Sahrens 	 */
3462789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
34632665Snd150628 		if ((strchr(target, '@') == NULL) ||
34642665Snd150628 		    *target == '@') {
34652665Snd150628 			/*
34662665Snd150628 			 * Snapshot target name is abbreviated,
34672665Snd150628 			 * reconstruct full dataset name
34682665Snd150628 			 */
34692665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
34702665Snd150628 			    sizeof (parent));
34712665Snd150628 			delim = strchr(parent, '@');
34722665Snd150628 			if (strchr(target, '@') == NULL)
34732665Snd150628 				*(++delim) = '\0';
34742665Snd150628 			else
34752665Snd150628 				*delim = '\0';
34762665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
34772665Snd150628 			target = parent;
34782665Snd150628 		} else {
34792665Snd150628 			/*
34802665Snd150628 			 * Make sure we're renaming within the same dataset.
34812665Snd150628 			 */
34822665Snd150628 			delim = strchr(target, '@');
34832665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
34842665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
34852665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34862665Snd150628 				    "snapshots must be part of same "
34872665Snd150628 				    "dataset"));
34882665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
34893912Slling 				    errbuf));
34902665Snd150628 			}
3491789Sahrens 		}
34925326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
34932665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3494789Sahrens 	} else {
34954007Smmusante 		if (recursive) {
34964007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34974007Smmusante 			    "recursive rename must be a snapshot"));
34984007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
34994007Smmusante 		}
35004007Smmusante 
35015326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
35022665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35032676Seschrock 		uint64_t unused;
35042676Seschrock 
3505789Sahrens 		/* validate parents */
35064490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3507789Sahrens 			return (-1);
3508789Sahrens 
3509789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3510789Sahrens 
3511789Sahrens 		/* make sure we're in the same pool */
3512789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3513789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3514789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
35152082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35162082Seschrock 			    "datasets must be within same pool"));
35172082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3518789Sahrens 		}
35192440Snd150628 
35202440Snd150628 		/* new name cannot be a child of the current dataset name */
35212440Snd150628 		if (strncmp(parent, zhp->zfs_name,
35223912Slling 		    strlen(zhp->zfs_name)) == 0) {
35232440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35242440Snd150628 			    "New dataset name cannot be a descendent of "
35252440Snd150628 			    "current dataset name"));
35262440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35272440Snd150628 		}
3528789Sahrens 	}
3529789Sahrens 
35302082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
35312082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
35322082Seschrock 
3533789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3534789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
35352082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35362082Seschrock 		    "dataset is used in a non-global zone"));
35372082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3538789Sahrens 	}
3539789Sahrens 
35404007Smmusante 	if (recursive) {
35414007Smmusante 		struct destroydata dd;
35424007Smmusante 
35434183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
35444183Smmusante 		if (parentname == NULL) {
35454183Smmusante 			ret = -1;
35464183Smmusante 			goto error;
35474183Smmusante 		}
35484007Smmusante 		delim = strchr(parentname, '@');
35494007Smmusante 		*delim = '\0';
35505094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
35514007Smmusante 		if (zhrp == NULL) {
35524183Smmusante 			ret = -1;
35534183Smmusante 			goto error;
35544007Smmusante 		}
35554007Smmusante 
35564007Smmusante 		dd.snapname = delim + 1;
35574007Smmusante 		dd.gotone = B_FALSE;
35584183Smmusante 		dd.closezhp = B_TRUE;
35594007Smmusante 
35604007Smmusante 		/* We remove any zvol links prior to renaming them */
35614007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
35624007Smmusante 		if (ret) {
35634007Smmusante 			goto error;
35644007Smmusante 		}
35654007Smmusante 	} else {
35664007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
35674007Smmusante 			return (-1);
35684007Smmusante 
35694007Smmusante 		if (changelist_haszonedchild(cl)) {
35704007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35714007Smmusante 			    "child dataset with inherited mountpoint is used "
35724007Smmusante 			    "in a non-global zone"));
35734007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
35744007Smmusante 			goto error;
35754007Smmusante 		}
35764007Smmusante 
35774007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
35784007Smmusante 			goto error;
3579789Sahrens 	}
3580789Sahrens 
35812676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3582789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3583789Sahrens 	else
3584789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3585789Sahrens 
35862665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35872676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
35882665Snd150628 
35894007Smmusante 	zc.zc_cookie = recursive;
35904007Smmusante 
35914543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
35924007Smmusante 		/*
35934007Smmusante 		 * if it was recursive, the one that actually failed will
35944007Smmusante 		 * be in zc.zc_name
35954007Smmusante 		 */
35964007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35975367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
35984007Smmusante 
35994007Smmusante 		if (recursive && errno == EEXIST) {
36004007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36014007Smmusante 			    "a child dataset already has a snapshot "
36024007Smmusante 			    "with the new name"));
36034801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
36044007Smmusante 		} else {
36054007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
36064007Smmusante 		}
3607789Sahrens 
3608789Sahrens 		/*
3609789Sahrens 		 * On failure, we still want to remount any filesystems that
3610789Sahrens 		 * were previously mounted, so we don't alter the system state.
3611789Sahrens 		 */
36124007Smmusante 		if (recursive) {
36134007Smmusante 			struct createdata cd;
36144007Smmusante 
36154007Smmusante 			/* only create links for datasets that had existed */
36164007Smmusante 			cd.cd_snapname = delim + 1;
36174007Smmusante 			cd.cd_ifexists = B_TRUE;
36184007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
36194007Smmusante 			    &cd);
36204007Smmusante 		} else {
36214007Smmusante 			(void) changelist_postfix(cl);
36224007Smmusante 		}
3623789Sahrens 	} else {
36244007Smmusante 		if (recursive) {
36254007Smmusante 			struct createdata cd;
36264007Smmusante 
36274007Smmusante 			/* only create links for datasets that had existed */
36284007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
36294007Smmusante 			cd.cd_ifexists = B_TRUE;
36304007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
36314007Smmusante 			    &cd);
36324007Smmusante 		} else {
36334007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
36344007Smmusante 			ret = changelist_postfix(cl);
36354007Smmusante 		}
3636789Sahrens 	}
3637789Sahrens 
3638789Sahrens error:
36394007Smmusante 	if (parentname) {
36404007Smmusante 		free(parentname);
36414007Smmusante 	}
36424007Smmusante 	if (zhrp) {
36434007Smmusante 		zfs_close(zhrp);
36444007Smmusante 	}
36454007Smmusante 	if (cl) {
36464007Smmusante 		changelist_free(cl);
36474007Smmusante 	}
3648789Sahrens 	return (ret);
3649789Sahrens }
3650789Sahrens 
3651789Sahrens /*
3652789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3653789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3654789Sahrens  */
3655789Sahrens int
36562082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3657789Sahrens {
36584007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
36594007Smmusante }
36604007Smmusante 
36614007Smmusante static int
36624007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
36634007Smmusante {
3664789Sahrens 	zfs_cmd_t zc = { 0 };
36652082Seschrock 	di_devlink_handle_t dhdl;
36664543Smarks 	priv_set_t *priv_effective;
36674543Smarks 	int privileged;
3668789Sahrens 
3669789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3670789Sahrens 
3671789Sahrens 	/*
3672789Sahrens 	 * Issue the appropriate ioctl.
3673789Sahrens 	 */
36742082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3675789Sahrens 		switch (errno) {
3676789Sahrens 		case EEXIST:
3677789Sahrens 			/*
3678789Sahrens 			 * Silently ignore the case where the link already
3679789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3680789Sahrens 			 * times without errors.
3681789Sahrens 			 */
3682789Sahrens 			return (0);
3683789Sahrens 
36844007Smmusante 		case ENOENT:
36854007Smmusante 			/*
36864007Smmusante 			 * Dataset does not exist in the kernel.  If we
36874007Smmusante 			 * don't care (see zfs_rename), then ignore the
36884007Smmusante 			 * error quietly.
36894007Smmusante 			 */
36904007Smmusante 			if (ifexists) {
36914007Smmusante 				return (0);
36924007Smmusante 			}
36934007Smmusante 
36944007Smmusante 			/* FALLTHROUGH */
36954007Smmusante 
3696789Sahrens 		default:
36973237Slling 			return (zfs_standard_error_fmt(hdl, errno,
36982082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
36992082Seschrock 			    "for '%s'"), dataset));
3700789Sahrens 		}
3701789Sahrens 	}
3702789Sahrens 
3703789Sahrens 	/*
37044543Smarks 	 * If privileged call devfsadm and wait for the links to
37054543Smarks 	 * magically appear.
37064543Smarks 	 * Otherwise, print out an informational message.
3707789Sahrens 	 */
37084543Smarks 
37094543Smarks 	priv_effective = priv_allocset();
37104543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
37114543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
37124543Smarks 	priv_freeset(priv_effective);
37134543Smarks 
37144543Smarks 	if (privileged) {
37154543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
37164543Smarks 		    DI_MAKE_LINK)) == NULL) {
37174543Smarks 			zfs_error_aux(hdl, strerror(errno));
37184543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
37194543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
37204543Smarks 			    "for '%s'"), dataset);
37214543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
37224543Smarks 			return (-1);
37234543Smarks 		} else {
37244543Smarks 			(void) di_devlink_fini(&dhdl);
37254543Smarks 		}
3726789Sahrens 	} else {
37274543Smarks 		char pathname[MAXPATHLEN];
37284543Smarks 		struct stat64 statbuf;
37294543Smarks 		int i;
37304543Smarks 
37314543Smarks #define	MAX_WAIT	10
37324543Smarks 
37334543Smarks 		/*
37344543Smarks 		 * This is the poor mans way of waiting for the link
37354543Smarks 		 * to show up.  If after 10 seconds we still don't
37364543Smarks 		 * have it, then print out a message.
37374543Smarks 		 */
37384543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
37394543Smarks 		    dataset);
37404543Smarks 
37414543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
37424543Smarks 			if (stat64(pathname, &statbuf) == 0)
37434543Smarks 				break;
37444543Smarks 			(void) sleep(1);
37454543Smarks 		}
37464543Smarks 		if (i == MAX_WAIT)
37474543Smarks 			(void) printf(gettext("%s may not be immediately "
37484543Smarks 			    "available\n"), pathname);
3749789Sahrens 	}
3750789Sahrens 
3751789Sahrens 	return (0);
3752789Sahrens }
3753789Sahrens 
3754789Sahrens /*
3755789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3756789Sahrens  */
3757789Sahrens int
37582082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3759789Sahrens {
3760789Sahrens 	zfs_cmd_t zc = { 0 };
3761789Sahrens 
3762789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3763789Sahrens 
37642082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3765789Sahrens 		switch (errno) {
3766789Sahrens 		case ENXIO:
3767789Sahrens 			/*
3768789Sahrens 			 * Silently ignore the case where the link no longer
3769789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3770789Sahrens 			 * times without errors.
3771789Sahrens 			 */
3772789Sahrens 			return (0);
3773789Sahrens 
3774789Sahrens 		default:
37753237Slling 			return (zfs_standard_error_fmt(hdl, errno,
37762082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
37772082Seschrock 			    "links for '%s'"), dataset));
3778789Sahrens 		}
3779789Sahrens 	}
3780789Sahrens 
3781789Sahrens 	return (0);
3782789Sahrens }
37832676Seschrock 
37842676Seschrock nvlist_t *
37852676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
37862676Seschrock {
37872676Seschrock 	return (zhp->zfs_user_props);
37882676Seschrock }
37892676Seschrock 
37902676Seschrock /*
37913912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
37923912Slling  * display, and their maximum widths.  This does two main things:
37933912Slling  *
37943912Slling  *      - If this is a list of all properties, then expand the list to include
37953912Slling  *        all native properties, and set a flag so that for each dataset we look
37963912Slling  *        for new unique user properties and add them to the list.
37973912Slling  *
37983912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
37993912Slling  *        so that we can size the column appropriately.
38003912Slling  */
38013912Slling int
38025094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
38033912Slling {
38043912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
38055094Slling 	zprop_list_t *entry;
38065094Slling 	zprop_list_t **last, **start;
38073912Slling 	nvlist_t *userprops, *propval;
38083912Slling 	nvpair_t *elem;
38093912Slling 	char *strval;
38103912Slling 	char buf[ZFS_MAXPROPLEN];
38113912Slling 
38125094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
38133912Slling 		return (-1);
38142676Seschrock 
38152676Seschrock 	userprops = zfs_get_user_props(zhp);
38162676Seschrock 
38172676Seschrock 	entry = *plp;
38182676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
38192676Seschrock 		/*
38202676Seschrock 		 * Go through and add any user properties as necessary.  We
38212676Seschrock 		 * start by incrementing our list pointer to the first
38222676Seschrock 		 * non-native property.
38232676Seschrock 		 */
38242676Seschrock 		start = plp;
38252676Seschrock 		while (*start != NULL) {
38265094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
38272676Seschrock 				break;
38282676Seschrock 			start = &(*start)->pl_next;
38292676Seschrock 		}
38302676Seschrock 
38312676Seschrock 		elem = NULL;
38322676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
38332676Seschrock 			/*
38342676Seschrock 			 * See if we've already found this property in our list.
38352676Seschrock 			 */
38362676Seschrock 			for (last = start; *last != NULL;
38372676Seschrock 			    last = &(*last)->pl_next) {
38382676Seschrock 				if (strcmp((*last)->pl_user_prop,
38392676Seschrock 				    nvpair_name(elem)) == 0)
38402676Seschrock 					break;
38412676Seschrock 			}
38422676Seschrock 
38432676Seschrock 			if (*last == NULL) {
38442676Seschrock 				if ((entry = zfs_alloc(hdl,
38455094Slling 				    sizeof (zprop_list_t))) == NULL ||
38462676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
38472676Seschrock 				    nvpair_name(elem)))) == NULL) {
38482676Seschrock 					free(entry);
38492676Seschrock 					return (-1);
38502676Seschrock 				}
38512676Seschrock 
38525094Slling 				entry->pl_prop = ZPROP_INVAL;
38532676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
38542676Seschrock 				entry->pl_all = B_TRUE;
38552676Seschrock 				*last = entry;
38562676Seschrock 			}
38572676Seschrock 		}
38582676Seschrock 	}
38592676Seschrock 
38602676Seschrock 	/*
38612676Seschrock 	 * Now go through and check the width of any non-fixed columns
38622676Seschrock 	 */
38632676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
38642676Seschrock 		if (entry->pl_fixed)
38652676Seschrock 			continue;
38662676Seschrock 
38675094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
38682676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
38692676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
38702676Seschrock 				if (strlen(buf) > entry->pl_width)
38712676Seschrock 					entry->pl_width = strlen(buf);
38722676Seschrock 			}
38732676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
38742676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
38752676Seschrock 			verify(nvlist_lookup_string(propval,
38765094Slling 			    ZPROP_VALUE, &strval) == 0);
38772676Seschrock 			if (strlen(strval) > entry->pl_width)
38782676Seschrock 				entry->pl_width = strlen(strval);
38792676Seschrock 		}
38802676Seschrock 	}
38812676Seschrock 
38822676Seschrock 	return (0);
38832676Seschrock }
38844543Smarks 
38854543Smarks int
38864543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
38874543Smarks {
38884543Smarks 	zfs_cmd_t zc = { 0 };
38894543Smarks 	nvlist_t *nvp;
38904543Smarks 	gid_t gid;
38914543Smarks 	uid_t uid;
38924543Smarks 	const gid_t *groups;
38934543Smarks 	int group_cnt;
38944543Smarks 	int error;
38954543Smarks 
38964543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
38974543Smarks 		return (no_memory(hdl));
38984543Smarks 
38994543Smarks 	uid = ucred_geteuid(cred);
39004543Smarks 	gid = ucred_getegid(cred);
39014543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
39024543Smarks 
39034543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
39044543Smarks 		return (1);
39054543Smarks 
39064543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
39074543Smarks 		nvlist_free(nvp);
39084543Smarks 		return (1);
39094543Smarks 	}
39104543Smarks 
39114543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
39124543Smarks 		nvlist_free(nvp);
39134543Smarks 		return (1);
39144543Smarks 	}
39154543Smarks 
39164543Smarks 	if (nvlist_add_uint32_array(nvp,
39174543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
39184543Smarks 		nvlist_free(nvp);
39194543Smarks 		return (1);
39204543Smarks 	}
39214543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39224543Smarks 
39235094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
39244543Smarks 		return (-1);
39254543Smarks 
39264543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
39274543Smarks 	nvlist_free(nvp);
39284543Smarks 	return (error);
39294543Smarks }
39304543Smarks 
39314543Smarks int
39324543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
39335331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
39344543Smarks {
39354543Smarks 	zfs_cmd_t zc = { 0 };
39364543Smarks 	int error;
39374543Smarks 
39384543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39394543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
39404543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
39414543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
39425331Samw 	zc.zc_share.z_sharetype = operation;
39434543Smarks 	zc.zc_share.z_sharemax = sharemax;
39444543Smarks 
39454543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
39464543Smarks 	return (error);
39474543Smarks }
3948