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>
545331Samw #include <sys/zfs_i18n.h>
55789Sahrens #include <libzfs.h>
56789Sahrens 
57789Sahrens #include "zfs_namecheck.h"
58789Sahrens #include "zfs_prop.h"
59789Sahrens #include "libzfs_impl.h"
604543Smarks #include "zfs_deleg.h"
61789Sahrens 
624007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
634007Smmusante 
64789Sahrens /*
65789Sahrens  * Given a single type (not a mask of types), return the type in a human
66789Sahrens  * readable form.
67789Sahrens  */
68789Sahrens const char *
69789Sahrens zfs_type_to_name(zfs_type_t type)
70789Sahrens {
71789Sahrens 	switch (type) {
72789Sahrens 	case ZFS_TYPE_FILESYSTEM:
73789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
74789Sahrens 	case ZFS_TYPE_SNAPSHOT:
75789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
76789Sahrens 	case ZFS_TYPE_VOLUME:
77789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
78789Sahrens 	}
79789Sahrens 
80789Sahrens 	return (NULL);
81789Sahrens }
82789Sahrens 
83789Sahrens /*
84789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
85789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
86789Sahrens  * We guess what the type would have been based on the path and the mask of
87789Sahrens  * acceptable types.
88789Sahrens  */
89789Sahrens static const char *
90789Sahrens path_to_str(const char *path, int types)
91789Sahrens {
92789Sahrens 	/*
93789Sahrens 	 * When given a single type, always report the exact type.
94789Sahrens 	 */
95789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
96789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
97789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
98789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
99789Sahrens 	if (types == ZFS_TYPE_VOLUME)
100789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
101789Sahrens 
102789Sahrens 	/*
103789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
104789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
105789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
106789Sahrens 	 * snapshot attribute and try again.
107789Sahrens 	 */
108789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
109789Sahrens 		if (strchr(path, '@') != NULL)
110789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
111789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
112789Sahrens 	}
113789Sahrens 
114789Sahrens 
115789Sahrens 	/*
116789Sahrens 	 * The user has requested either filesystems or volumes.
117789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
118789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
119789Sahrens 	 */
120789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
121789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
122789Sahrens 
123789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
124789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
125789Sahrens }
126789Sahrens 
127789Sahrens /*
128789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
129789Sahrens  * provide a more meaningful error message.  We place a more useful message in
130789Sahrens  * 'buf' detailing exactly why the name was not valid.
131789Sahrens  */
132789Sahrens static int
1335326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1345326Sek110237     boolean_t modifying)
135789Sahrens {
136789Sahrens 	namecheck_err_t why;
137789Sahrens 	char what;
138789Sahrens 
139789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1402082Seschrock 		if (hdl != NULL) {
141789Sahrens 			switch (why) {
1421003Slling 			case NAME_ERR_TOOLONG:
1432082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442082Seschrock 				    "name is too long"));
1451003Slling 				break;
1461003Slling 
147789Sahrens 			case NAME_ERR_LEADING_SLASH:
1482082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492082Seschrock 				    "leading slash in name"));
150789Sahrens 				break;
151789Sahrens 
152789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1532082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542082Seschrock 				    "empty component in name"));
155789Sahrens 				break;
156789Sahrens 
157789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1582082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1592082Seschrock 				    "trailing slash in name"));
160789Sahrens 				break;
161789Sahrens 
162789Sahrens 			case NAME_ERR_INVALCHAR:
1632082Seschrock 				zfs_error_aux(hdl,
164789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1652082Seschrock 				    "'%c' in name"), what);
166789Sahrens 				break;
167789Sahrens 
168789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1692082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1702082Seschrock 				    "multiple '@' delimiters in name"));
171789Sahrens 				break;
1722856Snd150628 
1732856Snd150628 			case NAME_ERR_NOLETTER:
1742856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1752856Snd150628 				    "pool doesn't begin with a letter"));
1762856Snd150628 				break;
1772856Snd150628 
1782856Snd150628 			case NAME_ERR_RESERVED:
1792856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1802856Snd150628 				    "name is reserved"));
1812856Snd150628 				break;
1822856Snd150628 
1832856Snd150628 			case NAME_ERR_DISKLIKE:
1842856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1852856Snd150628 				    "reserved disk name"));
1862856Snd150628 				break;
187789Sahrens 			}
188789Sahrens 		}
189789Sahrens 
190789Sahrens 		return (0);
191789Sahrens 	}
192789Sahrens 
193789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1942082Seschrock 		if (hdl != NULL)
1952082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1962082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
197789Sahrens 		return (0);
198789Sahrens 	}
199789Sahrens 
2002199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2012199Sahrens 		if (hdl != NULL)
2022199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2033413Smmusante 			    "missing '@' delimiter in snapshot name"));
2042199Sahrens 		return (0);
2052199Sahrens 	}
2062199Sahrens 
2075326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2085326Sek110237 		if (hdl != NULL)
2095326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2105326Sek110237 			    "invalid character %c in name"), '%');
2115326Sek110237 		return (0);
2125326Sek110237 	}
2135326Sek110237 
2142082Seschrock 	return (-1);
215789Sahrens }
216789Sahrens 
217789Sahrens int
218789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
219789Sahrens {
2205326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
221789Sahrens }
222789Sahrens 
223789Sahrens /*
2242676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2252676Seschrock  * properties into a separate nvlist.
2262676Seschrock  */
2274217Seschrock static nvlist_t *
2284217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2292676Seschrock {
2302676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2312676Seschrock 	nvpair_t *elem;
2322676Seschrock 	nvlist_t *propval;
2334217Seschrock 	nvlist_t *nvl;
2344217Seschrock 
2354217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2364217Seschrock 		(void) no_memory(hdl);
2374217Seschrock 		return (NULL);
2384217Seschrock 	}
2392676Seschrock 
2402676Seschrock 	elem = NULL;
2414217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2422676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2432676Seschrock 			continue;
2442676Seschrock 
2452676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2464217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2474217Seschrock 			nvlist_free(nvl);
2484217Seschrock 			(void) no_memory(hdl);
2494217Seschrock 			return (NULL);
2504217Seschrock 		}
2512676Seschrock 	}
2522676Seschrock 
2534217Seschrock 	return (nvl);
2542676Seschrock }
2552676Seschrock 
2562676Seschrock /*
257789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
258789Sahrens  */
259789Sahrens static int
260789Sahrens get_stats(zfs_handle_t *zhp)
261789Sahrens {
262789Sahrens 	zfs_cmd_t zc = { 0 };
2632676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2644217Seschrock 	nvlist_t *allprops, *userprops;
265789Sahrens 
266789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
267789Sahrens 
2682676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
2692082Seschrock 		return (-1);
2701356Seschrock 
2712082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
2721356Seschrock 		if (errno == ENOMEM) {
2732676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2742676Seschrock 				zcmd_free_nvlists(&zc);
2752082Seschrock 				return (-1);
2762676Seschrock 			}
2771356Seschrock 		} else {
2782676Seschrock 			zcmd_free_nvlists(&zc);
2791356Seschrock 			return (-1);
2801356Seschrock 		}
2811356Seschrock 	}
282789Sahrens 
2832885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
284789Sahrens 
2852676Seschrock 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
2861544Seschrock 
2874217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
2882676Seschrock 		zcmd_free_nvlists(&zc);
2892082Seschrock 		return (-1);
2902082Seschrock 	}
291789Sahrens 
2922676Seschrock 	zcmd_free_nvlists(&zc);
2932676Seschrock 
2944217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
2954217Seschrock 		nvlist_free(allprops);
2962676Seschrock 		return (-1);
2974217Seschrock 	}
2984217Seschrock 
2994217Seschrock 	nvlist_free(zhp->zfs_props);
3004217Seschrock 	nvlist_free(zhp->zfs_user_props);
3014217Seschrock 
3024217Seschrock 	zhp->zfs_props = allprops;
3034217Seschrock 	zhp->zfs_user_props = userprops;
3042082Seschrock 
305789Sahrens 	return (0);
306789Sahrens }
307789Sahrens 
308789Sahrens /*
309789Sahrens  * Refresh the properties currently stored in the handle.
310789Sahrens  */
311789Sahrens void
312789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
313789Sahrens {
314789Sahrens 	(void) get_stats(zhp);
315789Sahrens }
316789Sahrens 
317789Sahrens /*
318789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
319789Sahrens  * zfs_iter_* to create child handles on the fly.
320789Sahrens  */
321789Sahrens zfs_handle_t *
3222082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
323789Sahrens {
3242082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3254543Smarks 	char *logstr;
3262082Seschrock 
3272082Seschrock 	if (zhp == NULL)
3282082Seschrock 		return (NULL);
3292082Seschrock 
3302082Seschrock 	zhp->zfs_hdl = hdl;
331789Sahrens 
3324543Smarks 	/*
3334543Smarks 	 * Preserve history log string.
3344543Smarks 	 * any changes performed here will be
3354543Smarks 	 * logged as an internal event.
3364543Smarks 	 */
3374543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3384543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3391758Sahrens top:
340789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
341789Sahrens 
342789Sahrens 	if (get_stats(zhp) != 0) {
3434543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
344789Sahrens 		free(zhp);
345789Sahrens 		return (NULL);
346789Sahrens 	}
347789Sahrens 
3481758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
3491758Sahrens 		zfs_cmd_t zc = { 0 };
3501758Sahrens 
3511758Sahrens 		/*
3521758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
3531758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
3541758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
3551758Sahrens 		 * can't be mounted.  However, it could also be that we
3561758Sahrens 		 * have crashed in the middle of one of those
3571758Sahrens 		 * operations, in which case we need to get rid of the
3581758Sahrens 		 * inconsistent state.  We do that by either rolling
3591758Sahrens 		 * back to the previous snapshot (which will fail if
3601758Sahrens 		 * there is none), or destroying the filesystem.  Note
3611758Sahrens 		 * that if we are still in the middle of an active
3621758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
3631758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
3641758Sahrens 		 */
3651758Sahrens 
3661758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3671758Sahrens 
3682885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
3692082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
3701758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
3711758Sahrens 		} else {
3721758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
3731758Sahrens 		}
3741758Sahrens 
3751758Sahrens 		/*
3765331Samw 		 * If we can successfully destroy it, pretend that it
3771758Sahrens 		 * never existed.
3781758Sahrens 		 */
3792082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
3804543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3811758Sahrens 			free(zhp);
3821758Sahrens 			errno = ENOENT;
3831758Sahrens 			return (NULL);
3841758Sahrens 		}
3855367Sahrens 		/* If we can successfully roll it back, reget the stats */
3865367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
3875367Sahrens 			goto top;
3881758Sahrens 	}
3891758Sahrens 
390789Sahrens 	/*
391789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
392789Sahrens 	 * the high-level type.
393789Sahrens 	 */
3942885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
3952885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
3962885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
3972885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
3982885Sahrens 	else
3992885Sahrens 		abort();
4002885Sahrens 
401789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
402789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
403789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
404789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
405789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
406789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
407789Sahrens 	else
4082082Seschrock 		abort();	/* we should never see any other types */
409789Sahrens 
4104543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
411789Sahrens 	return (zhp);
412789Sahrens }
413789Sahrens 
414789Sahrens /*
415789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
416789Sahrens  * argument is a mask of acceptable types.  The function will print an
417789Sahrens  * appropriate error message and return NULL if it can't be opened.
418789Sahrens  */
419789Sahrens zfs_handle_t *
4202082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
421789Sahrens {
422789Sahrens 	zfs_handle_t *zhp;
4232082Seschrock 	char errbuf[1024];
4242082Seschrock 
4252082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4262082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
427789Sahrens 
428789Sahrens 	/*
4292082Seschrock 	 * Validate the name before we even try to open it.
430789Sahrens 	 */
4315326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4322082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4332082Seschrock 		    "invalid dataset name"));
4342082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
435789Sahrens 		return (NULL);
436789Sahrens 	}
437789Sahrens 
438789Sahrens 	/*
439789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
440789Sahrens 	 */
441789Sahrens 	errno = 0;
4422082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
4433237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
444789Sahrens 		return (NULL);
445789Sahrens 	}
446789Sahrens 
447789Sahrens 	if (!(types & zhp->zfs_type)) {
4482082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4492142Seschrock 		zfs_close(zhp);
450789Sahrens 		return (NULL);
451789Sahrens 	}
452789Sahrens 
453789Sahrens 	return (zhp);
454789Sahrens }
455789Sahrens 
456789Sahrens /*
457789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
458789Sahrens  */
459789Sahrens void
460789Sahrens zfs_close(zfs_handle_t *zhp)
461789Sahrens {
462789Sahrens 	if (zhp->zfs_mntopts)
463789Sahrens 		free(zhp->zfs_mntopts);
4642676Seschrock 	nvlist_free(zhp->zfs_props);
4652676Seschrock 	nvlist_free(zhp->zfs_user_props);
466789Sahrens 	free(zhp);
467789Sahrens }
468789Sahrens 
4693912Slling /*
4702676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
4712676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
4722676Seschrock  * strings.
473789Sahrens  */
4745094Slling static nvlist_t *
4755094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
4765094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
477789Sahrens {
4782676Seschrock 	nvpair_t *elem;
4792676Seschrock 	uint64_t intval;
4802676Seschrock 	char *strval;
4815094Slling 	zfs_prop_t prop;
4822676Seschrock 	nvlist_t *ret;
4835375Stimh 	int chosen_sense = -1;
4845331Samw 	int chosen_normal = -1;
4855331Samw 	int chosen_utf = -1;
4862676Seschrock 
4872676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
4882676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4893413Smmusante 		    "snapshot properties cannot be modified"));
4902676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
4915094Slling 		return (NULL);
4925094Slling 	}
4935094Slling 
4945094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
4955094Slling 		(void) no_memory(hdl);
4965094Slling 		return (NULL);
497789Sahrens 	}
498789Sahrens 
4992676Seschrock 	elem = NULL;
5002676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5015094Slling 		const char *propname = nvpair_name(elem);
5022676Seschrock 
5032676Seschrock 		/*
5042676Seschrock 		 * Make sure this property is valid and applies to this type.
5052676Seschrock 		 */
5065094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
5075094Slling 			if (!zfs_prop_user(propname)) {
5082676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5095094Slling 				    "invalid property '%s'"), propname);
5105094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5115094Slling 				goto error;
5125094Slling 			}
5135094Slling 
5145094Slling 			/*
5155094Slling 			 * If this is a user property, make sure it's a
5165094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
5175094Slling 			 */
5185094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
5195094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5205094Slling 				    "'%s' must be a string"), propname);
5215094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5225094Slling 				goto error;
5235094Slling 			}
5245094Slling 
5255094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
5265094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5275094Slling 				    "property name '%s' is too long"),
5282676Seschrock 				    propname);
5292676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5302676Seschrock 				goto error;
5312676Seschrock 			}
5322676Seschrock 
5332676Seschrock 			(void) nvpair_value_string(elem, &strval);
5342676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
5352676Seschrock 				(void) no_memory(hdl);
5362676Seschrock 				goto error;
5372676Seschrock 			}
5382676Seschrock 			continue;
539789Sahrens 		}
5402676Seschrock 
5412676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
5422676Seschrock 			zfs_error_aux(hdl,
5432676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
5442676Seschrock 			    "apply to datasets of this type"), propname);
5452676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5462676Seschrock 			goto error;
5472676Seschrock 		}
5482676Seschrock 
5492676Seschrock 		if (zfs_prop_readonly(prop) &&
5505331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
5512676Seschrock 			zfs_error_aux(hdl,
5522676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
5532676Seschrock 			    propname);
5542676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
5552676Seschrock 			goto error;
5562676Seschrock 		}
5572676Seschrock 
5585094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
5595094Slling 		    &strval, &intval, errbuf) != 0)
5602676Seschrock 			goto error;
5612676Seschrock 
5622676Seschrock 		/*
5632676Seschrock 		 * Perform some additional checks for specific properties.
5642676Seschrock 		 */
5652676Seschrock 		switch (prop) {
5664577Sahrens 		case ZFS_PROP_VERSION:
5674577Sahrens 		{
5684577Sahrens 			int version;
5694577Sahrens 
5704577Sahrens 			if (zhp == NULL)
5714577Sahrens 				break;
5724577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
5734577Sahrens 			if (intval < version) {
5744577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5754577Sahrens 				    "Can not downgrade; already at version %u"),
5764577Sahrens 				    version);
5774577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5784577Sahrens 				goto error;
5794577Sahrens 			}
5804577Sahrens 			break;
5814577Sahrens 		}
5824577Sahrens 
5832676Seschrock 		case ZFS_PROP_RECORDSIZE:
5842676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
5852676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
5862676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
5872676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
5882082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5892676Seschrock 				    "'%s' must be power of 2 from %u "
5902676Seschrock 				    "to %uk"), propname,
5912676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
5922676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
5932676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5942676Seschrock 				goto error;
595789Sahrens 			}
596789Sahrens 			break;
597789Sahrens 
5983126Sahl 		case ZFS_PROP_SHAREISCSI:
5993126Sahl 			if (strcmp(strval, "off") != 0 &&
6003126Sahl 			    strcmp(strval, "on") != 0 &&
6013126Sahl 			    strcmp(strval, "type=disk") != 0) {
6023126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6033126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6043126Sahl 				    propname);
6053126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6063126Sahl 				goto error;
6073126Sahl 			}
6083126Sahl 
6093126Sahl 			break;
6103126Sahl 
6112676Seschrock 		case ZFS_PROP_MOUNTPOINT:
6124778Srm160521 		{
6134778Srm160521 			namecheck_err_t why;
6144778Srm160521 
6152676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
6162676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
6172676Seschrock 				break;
6182676Seschrock 
6194778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
6204778Srm160521 				switch (why) {
6214778Srm160521 				case NAME_ERR_LEADING_SLASH:
6224778Srm160521 					zfs_error_aux(hdl,
6234778Srm160521 					    dgettext(TEXT_DOMAIN,
6244778Srm160521 					    "'%s' must be an absolute path, "
6254778Srm160521 					    "'none', or 'legacy'"), propname);
6264778Srm160521 					break;
6274778Srm160521 				case NAME_ERR_TOOLONG:
6284778Srm160521 					zfs_error_aux(hdl,
6294778Srm160521 					    dgettext(TEXT_DOMAIN,
6304778Srm160521 					    "component of '%s' is too long"),
6314778Srm160521 					    propname);
6324778Srm160521 					break;
6334778Srm160521 				}
6342676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6352676Seschrock 				goto error;
636789Sahrens 			}
6374778Srm160521 		}
6384778Srm160521 
6393126Sahl 			/*FALLTHRU*/
6403126Sahl 
6415331Samw 		case ZFS_PROP_SHARESMB:
6423126Sahl 		case ZFS_PROP_SHARENFS:
6433126Sahl 			/*
6445331Samw 			 * For the mountpoint and sharenfs or sharesmb
6455331Samw 			 * properties, check if it can be set in a
6465331Samw 			 * global/non-global zone based on
6473126Sahl 			 * the zoned property value:
6483126Sahl 			 *
6493126Sahl 			 *		global zone	    non-global zone
6503126Sahl 			 * --------------------------------------------------
6513126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
6523126Sahl 			 *		sharenfs (no)	    sharenfs (no)
6535331Samw 			 *		sharesmb (no)	    sharesmb (no)
6543126Sahl 			 *
6553126Sahl 			 * zoned=off	mountpoint (yes)	N/A
6563126Sahl 			 *		sharenfs (yes)
6575331Samw 			 *		sharesmb (yes)
6583126Sahl 			 */
6592676Seschrock 			if (zoned) {
6602676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
6612676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6622676Seschrock 					    "'%s' cannot be set on "
6632676Seschrock 					    "dataset in a non-global zone"),
6642676Seschrock 					    propname);
6652676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
6662676Seschrock 					    errbuf);
6672676Seschrock 					goto error;
6685331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
6695331Samw 				    prop == ZFS_PROP_SHARESMB) {
6702676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6712676Seschrock 					    "'%s' cannot be set in "
6722676Seschrock 					    "a non-global zone"), propname);
6732676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
6742676Seschrock 					    errbuf);
6752676Seschrock 					goto error;
6762676Seschrock 				}
6772676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
6782676Seschrock 				/*
6792676Seschrock 				 * If zoned property is 'off', this must be in
6802676Seschrock 				 * a globle zone. If not, something is wrong.
6812676Seschrock 				 */
6822676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6832676Seschrock 				    "'%s' cannot be set while dataset "
6842676Seschrock 				    "'zoned' property is set"), propname);
6852676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
6862676Seschrock 				goto error;
6872676Seschrock 			}
6883126Sahl 
6894180Sdougm 			/*
6904180Sdougm 			 * At this point, it is legitimate to set the
6914180Sdougm 			 * property. Now we want to make sure that the
6924180Sdougm 			 * property value is valid if it is sharenfs.
6934180Sdougm 			 */
6945331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
6955331Samw 			    prop == ZFS_PROP_SHARESMB) &&
6964217Seschrock 			    strcmp(strval, "on") != 0 &&
6974217Seschrock 			    strcmp(strval, "off") != 0) {
6985331Samw 				zfs_share_proto_t proto;
6995331Samw 
7005331Samw 				if (prop == ZFS_PROP_SHARESMB)
7015331Samw 					proto = PROTO_SMB;
7025331Samw 				else
7035331Samw 					proto = PROTO_NFS;
7044180Sdougm 
7054180Sdougm 				/*
7065331Samw 				 * Must be an valid sharing protocol
7075331Samw 				 * option string so init the libshare
7085331Samw 				 * in order to enable the parser and
7095331Samw 				 * then parse the options. We use the
7105331Samw 				 * control API since we don't care about
7115331Samw 				 * the current configuration and don't
7124180Sdougm 				 * want the overhead of loading it
7134180Sdougm 				 * until we actually do something.
7144180Sdougm 				 */
7154180Sdougm 
7164217Seschrock 				if (zfs_init_libshare(hdl,
7174217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
7184217Seschrock 					/*
7194217Seschrock 					 * An error occurred so we can't do
7204217Seschrock 					 * anything
7214217Seschrock 					 */
7224217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7234217Seschrock 					    "'%s' cannot be set: problem "
7244217Seschrock 					    "in share initialization"),
7254217Seschrock 					    propname);
7264217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7274217Seschrock 					    errbuf);
7284217Seschrock 					goto error;
7294217Seschrock 				}
7304217Seschrock 
7315331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
7324217Seschrock 					/*
7334217Seschrock 					 * There was an error in parsing so
7344217Seschrock 					 * deal with it by issuing an error
7354217Seschrock 					 * message and leaving after
7364217Seschrock 					 * uninitializing the the libshare
7374217Seschrock 					 * interface.
7384217Seschrock 					 */
7394217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7404217Seschrock 					    "'%s' cannot be set to invalid "
7414217Seschrock 					    "options"), propname);
7424217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7434217Seschrock 					    errbuf);
7444217Seschrock 					zfs_uninit_libshare(hdl);
7454217Seschrock 					goto error;
7464217Seschrock 				}
7474180Sdougm 				zfs_uninit_libshare(hdl);
7484180Sdougm 			}
7494180Sdougm 
7503126Sahl 			break;
7515375Stimh 		case ZFS_PROP_CASE:
7525375Stimh 			chosen_sense = (int)intval;
7535375Stimh 			break;
7545331Samw 		case ZFS_PROP_UTF8ONLY:
7555331Samw 			chosen_utf = (int)intval;
7565331Samw 			break;
7575331Samw 		case ZFS_PROP_NORMALIZE:
7585331Samw 			chosen_normal = (int)intval;
7595331Samw 			break;
7602676Seschrock 		}
7612676Seschrock 
7622676Seschrock 		/*
7632676Seschrock 		 * For changes to existing volumes, we have some additional
7642676Seschrock 		 * checks to enforce.
7652676Seschrock 		 */
7662676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
7672676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
7682676Seschrock 			    ZFS_PROP_VOLSIZE);
7692676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
7702676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
7712676Seschrock 			char buf[64];
7722676Seschrock 
7732676Seschrock 			switch (prop) {
7742676Seschrock 			case ZFS_PROP_RESERVATION:
7755378Sck153898 			case ZFS_PROP_REFRESERVATION:
7762676Seschrock 				if (intval > volsize) {
7772676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7782676Seschrock 					    "'%s' is greater than current "
7792676Seschrock 					    "volume size"), propname);
7802676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7812676Seschrock 					    errbuf);
7822676Seschrock 					goto error;
7832676Seschrock 				}
7842676Seschrock 				break;
7852676Seschrock 
7862676Seschrock 			case ZFS_PROP_VOLSIZE:
7872676Seschrock 				if (intval % blocksize != 0) {
7882676Seschrock 					zfs_nicenum(blocksize, buf,
7892676Seschrock 					    sizeof (buf));
7902676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7912676Seschrock 					    "'%s' must be a multiple of "
7922676Seschrock 					    "volume block size (%s)"),
7932676Seschrock 					    propname, buf);
7942676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7952676Seschrock 					    errbuf);
7962676Seschrock 					goto error;
7972676Seschrock 				}
7982676Seschrock 
7992676Seschrock 				if (intval == 0) {
8002676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8012676Seschrock 					    "'%s' cannot be zero"),
8022676Seschrock 					    propname);
8032676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8042676Seschrock 					    errbuf);
8052676Seschrock 					goto error;
806789Sahrens 				}
8073126Sahl 				break;
808789Sahrens 			}
809789Sahrens 		}
810789Sahrens 	}
811789Sahrens 
8122676Seschrock 	/*
8135375Stimh 	 * Temporarily disallow any non-default settings for
8145375Stimh 	 * casesensitivity, normalization, and/or utf8only.
8155375Stimh 	 */
8165375Stimh 	if (chosen_sense > ZFS_CASE_SENSITIVE || chosen_utf > 0 ||
8175375Stimh 	    chosen_normal > ZFS_NORMALIZE_NONE) {
8185375Stimh 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8195375Stimh 		    "Non-default values for casesensitivity, utf8only, and "
8205375Stimh 		    "normalization are (temporarily) disabled"));
8215375Stimh 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8225375Stimh 		goto error;
8235375Stimh 	}
8245375Stimh 
8255375Stimh 	/*
8265331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
8275331Samw 	 * enforce rejection of non-UTF8 names.
8285331Samw 	 *
8295331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
8305331Samw 	 * was explicitly not chosen, it is an error.
8315331Samw 	 */
8325331Samw 	if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf < 0) {
8335331Samw 		if (nvlist_add_uint64(ret,
8345331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
8355331Samw 			(void) no_memory(hdl);
8365331Samw 			goto error;
8375331Samw 		}
8385331Samw 	} else if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf == 0) {
8395331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8405331Samw 		    "'%s' must be set 'on' if normalization chosen"),
8415331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
8425331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8435331Samw 		goto error;
8445331Samw 	}
8455331Samw 
8465331Samw 	/*
8472676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
8482676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
8492676Seschrock 	 */
8502676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
8512676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
8522676Seschrock 	    &intval) == 0) {
8532676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
8542676Seschrock 		    ZFS_PROP_VOLSIZE);
855*5481Sck153898 		uint64_t old_reservation;
8562676Seschrock 		uint64_t new_reservation;
857*5481Sck153898 		char *pool_name;
858*5481Sck153898 		zpool_handle_t *zpool_handle;
859*5481Sck153898 		char *p;
860*5481Sck153898 		zfs_prop_t resv_prop;
861*5481Sck153898 		uint64_t spa_version;
862*5481Sck153898 
863*5481Sck153898 		pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN);
864*5481Sck153898 		if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name,
865*5481Sck153898 		    MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) {
866*5481Sck153898 			free(pool_name);
867*5481Sck153898 			goto error;
868*5481Sck153898 		}
869*5481Sck153898 
870*5481Sck153898 		if (p = strchr(pool_name, '/'))
871*5481Sck153898 			*p = '\0';
872*5481Sck153898 		zpool_handle = zpool_open(hdl, pool_name);
873*5481Sck153898 		free(pool_name);
874*5481Sck153898 		if (zpool_handle == NULL)
875*5481Sck153898 			goto error;
876*5481Sck153898 
877*5481Sck153898 		spa_version = zpool_get_prop_int(zpool_handle,
878*5481Sck153898 		    ZPOOL_PROP_VERSION, NULL);
879*5481Sck153898 		zpool_close(zpool_handle);
880*5481Sck153898 		if (spa_version >= SPA_VERSION_REFRESERVATION)
881*5481Sck153898 			resv_prop = ZFS_PROP_REFRESERVATION;
882*5481Sck153898 		else
883*5481Sck153898 			resv_prop = ZFS_PROP_RESERVATION;
884*5481Sck153898 
885*5481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
8862676Seschrock 
8872676Seschrock 		if (old_volsize == old_reservation &&
888*5481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
8892676Seschrock 		    &new_reservation) != 0) {
8902676Seschrock 			if (nvlist_add_uint64(ret,
891*5481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
8922676Seschrock 				(void) no_memory(hdl);
8932676Seschrock 				goto error;
8942676Seschrock 			}
8952676Seschrock 		}
8962676Seschrock 	}
8972676Seschrock 
8982676Seschrock 	return (ret);
8992676Seschrock 
9002676Seschrock error:
9012676Seschrock 	nvlist_free(ret);
9022676Seschrock 	return (NULL);
903789Sahrens }
904789Sahrens 
9054543Smarks static int
9064543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9074543Smarks     uint64_t *ret_who)
9084543Smarks {
9094543Smarks 	struct passwd *pwd;
9104543Smarks 	struct group *grp;
9114543Smarks 	uid_t id;
9124543Smarks 
9134543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9144543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9154543Smarks 		*ret_who = -1;
9164543Smarks 		return (0);
9174543Smarks 	}
9184543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9194543Smarks 		return (EZFS_BADWHO);
9204543Smarks 
9214543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9224543Smarks 	    strcmp(who, "everyone") == 0) {
9234543Smarks 		*ret_who = -1;
9244543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9254543Smarks 		return (0);
9264543Smarks 	}
9274543Smarks 
9284543Smarks 	pwd = getpwnam(who);
9294543Smarks 	grp = getgrnam(who);
9304543Smarks 
9314543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9324543Smarks 		*ret_who = pwd->pw_uid;
9334543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9344543Smarks 		*ret_who = grp->gr_gid;
9354543Smarks 	} else if (pwd) {
9364543Smarks 		*ret_who = pwd->pw_uid;
9374543Smarks 		*who_type = ZFS_DELEG_USER;
9384543Smarks 	} else if (grp) {
9394543Smarks 		*ret_who = grp->gr_gid;
9404543Smarks 		*who_type = ZFS_DELEG_GROUP;
9414543Smarks 	} else {
9424543Smarks 		char *end;
9434543Smarks 
9444543Smarks 		id = strtol(who, &end, 10);
9454543Smarks 		if (errno != 0 || *end != '\0') {
9464543Smarks 			return (EZFS_BADWHO);
9474543Smarks 		} else {
9484543Smarks 			*ret_who = id;
9494543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
9504543Smarks 				*who_type = ZFS_DELEG_USER;
9514543Smarks 		}
9524543Smarks 	}
9534543Smarks 
9544543Smarks 	return (0);
9554543Smarks }
9564543Smarks 
9574543Smarks static void
9584543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
9594543Smarks {
9604543Smarks 	if (perms_nvp != NULL) {
9614543Smarks 		verify(nvlist_add_nvlist(who_nvp,
9624543Smarks 		    name, perms_nvp) == 0);
9634543Smarks 	} else {
9644543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
9654543Smarks 	}
9664543Smarks }
9674543Smarks 
9684543Smarks static void
9694543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
9704543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
9714543Smarks     nvlist_t *sets_nvp)
9724543Smarks {
9734543Smarks 	boolean_t do_perms, do_sets;
9744543Smarks 	char name[ZFS_MAX_DELEG_NAME];
9754543Smarks 
9764543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
9774543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
9784543Smarks 
9794543Smarks 	if (!do_perms && !do_sets)
9804543Smarks 		do_perms = do_sets = B_TRUE;
9814543Smarks 
9824543Smarks 	if (do_perms) {
9834543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
9844543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9854543Smarks 		    whostr : (void *)&whoid);
9864543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
9874543Smarks 	}
9884543Smarks 	if (do_sets) {
9894543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
9904543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9914543Smarks 		    whostr : (void *)&whoid);
9924543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
9934543Smarks 	}
9944543Smarks }
9954543Smarks 
9964543Smarks static void
9974543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
9984543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
9994543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10004543Smarks {
10014543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10024543Smarks 		helper(who_type, whoid, whostr, 0,
10034543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10044543Smarks 	} else {
10054543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10064543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10074543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10084543Smarks 		}
10094543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10104543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10114543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10124543Smarks 		}
10134543Smarks 	}
10144543Smarks }
10154543Smarks 
10164543Smarks /*
10174543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10184543Smarks  *
10194543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10204543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10214543Smarks  * base attribute named stored in the dsl.
10224543Smarks  * Arguments:
10234543Smarks  *
10244543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10254543Smarks  *           whostr may be null for everyone or create perms.
10264543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10274543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10285331Samw  * perms:    common separated list of permissions.  May be null if user
10294543Smarks  *           is requested to remove permissions by who.
10304543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10314543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10324543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10334543Smarks  *           The output nvp will look something like this.
10344543Smarks  *              ul$1234 -> {create ; destroy }
10354543Smarks  *              Ul$1234 -> { @myset }
10364543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10374543Smarks  */
10384543Smarks int
10394543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10404543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10414543Smarks {
10424543Smarks 	nvlist_t *who_nvp;
10434543Smarks 	nvlist_t *perms_nvp = NULL;
10444543Smarks 	nvlist_t *sets_nvp = NULL;
10454543Smarks 	char errbuf[1024];
10464787Sahrens 	char *who_tok, *perm;
10474543Smarks 	int error;
10484543Smarks 
10494543Smarks 	*nvp = NULL;
10504543Smarks 
10514543Smarks 	if (perms) {
10524543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
10534543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10544543Smarks 			return (1);
10554543Smarks 		}
10564543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
10574543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10584543Smarks 			nvlist_free(perms_nvp);
10594543Smarks 			return (1);
10604543Smarks 		}
10614543Smarks 	}
10624543Smarks 
10634543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
10644543Smarks 		if (perms_nvp)
10654543Smarks 			nvlist_free(perms_nvp);
10664543Smarks 		if (sets_nvp)
10674543Smarks 			nvlist_free(sets_nvp);
10684543Smarks 		return (1);
10694543Smarks 	}
10704543Smarks 
10714543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
10724543Smarks 		namecheck_err_t why;
10734543Smarks 		char what;
10744543Smarks 
10754543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
10764787Sahrens 			nvlist_free(who_nvp);
10774787Sahrens 			if (perms_nvp)
10784787Sahrens 				nvlist_free(perms_nvp);
10794787Sahrens 			if (sets_nvp)
10804787Sahrens 				nvlist_free(sets_nvp);
10814787Sahrens 
10824543Smarks 			switch (why) {
10834543Smarks 			case NAME_ERR_NO_AT:
10844543Smarks 				zfs_error_aux(zhp->zfs_hdl,
10854543Smarks 				    dgettext(TEXT_DOMAIN,
10864543Smarks 				    "set definition must begin with an '@' "
10874543Smarks 				    "character"));
10884543Smarks 			}
10894543Smarks 			return (zfs_error(zhp->zfs_hdl,
10904543Smarks 			    EZFS_BADPERMSET, whostr));
10914543Smarks 		}
10924543Smarks 	}
10934543Smarks 
10944543Smarks 	/*
10954543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
10964543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
10974543Smarks 	 * other sets_nvp will have only permssion set names in it.
10984543Smarks 	 */
10994787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11004787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11014787Sahrens 
11024787Sahrens 		if (perm_canonical) {
11034787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11044787Sahrens 			    perm_canonical) == 0);
11054787Sahrens 		} else if (perm[0] == '@') {
11064787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11074787Sahrens 		} else {
11084787Sahrens 			nvlist_free(who_nvp);
11094787Sahrens 			nvlist_free(perms_nvp);
11104787Sahrens 			nvlist_free(sets_nvp);
11114787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11124543Smarks 		}
11134543Smarks 	}
11144543Smarks 
11154543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11164543Smarks 		who_tok = strtok(whostr, ",");
11174543Smarks 		if (who_tok == NULL) {
11184543Smarks 			nvlist_free(who_nvp);
11194787Sahrens 			if (perms_nvp)
11204787Sahrens 				nvlist_free(perms_nvp);
11214543Smarks 			if (sets_nvp)
11224543Smarks 				nvlist_free(sets_nvp);
11234543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11244543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11254543Smarks 			    whostr);
11264543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11274543Smarks 		}
11284543Smarks 	}
11294543Smarks 
11304543Smarks 	/*
11314543Smarks 	 * Now create the nvlist(s)
11324543Smarks 	 */
11334543Smarks 	do {
11344543Smarks 		uint64_t who_id;
11354543Smarks 
11364543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11374543Smarks 		    &who_id);
11384543Smarks 		if (error) {
11394543Smarks 			nvlist_free(who_nvp);
11404787Sahrens 			if (perms_nvp)
11414787Sahrens 				nvlist_free(perms_nvp);
11424543Smarks 			if (sets_nvp)
11434543Smarks 				nvlist_free(sets_nvp);
11444543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11454543Smarks 			    dgettext(TEXT_DOMAIN,
11464543Smarks 			    "Unable to determine uid/gid for "
11474543Smarks 			    "%s "), who_tok);
11484543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11494543Smarks 		}
11504543Smarks 
11514543Smarks 		/*
11524543Smarks 		 * add entries for both local and descendent when required
11534543Smarks 		 */
11544543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
11554543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
11564543Smarks 
11574543Smarks 	} while (who_tok = strtok(NULL, ","));
11584543Smarks 	*nvp = who_nvp;
11594543Smarks 	return (0);
11604543Smarks }
11614543Smarks 
11624543Smarks static int
11634543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
11644543Smarks {
11654543Smarks 	zfs_cmd_t zc = { 0 };
11664543Smarks 	int error;
11674543Smarks 	char errbuf[1024];
11684543Smarks 
11694543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
11704543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
11714543Smarks 	    zhp->zfs_name);
11724543Smarks 
11735094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
11744543Smarks 		return (-1);
11754543Smarks 
11764543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
11774543Smarks 	zc.zc_perm_action = unset;
11784543Smarks 
11794543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
11804543Smarks 	if (error && errno == ENOTSUP) {
11814543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
11824543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
11834543Smarks 		zcmd_free_nvlists(&zc);
11844543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
11854543Smarks 	} else if (error) {
11864543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
11874543Smarks 	}
11884543Smarks 	zcmd_free_nvlists(&zc);
11894543Smarks 
11904543Smarks 	return (error);
11914543Smarks }
11924543Smarks 
11934543Smarks int
11944543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
11954543Smarks {
11964543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
11974543Smarks }
11984543Smarks 
11994543Smarks int
12004543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12014543Smarks {
12024543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12034543Smarks }
12044543Smarks 
12054543Smarks static int
12064543Smarks perm_compare(const void *arg1, const void *arg2)
12074543Smarks {
12084543Smarks 	const zfs_perm_node_t *node1 = arg1;
12094543Smarks 	const zfs_perm_node_t *node2 = arg2;
12104543Smarks 	int ret;
12114543Smarks 
12124543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12134543Smarks 
12144543Smarks 	if (ret > 0)
12154543Smarks 		return (1);
12164543Smarks 	if (ret < 0)
12174543Smarks 		return (-1);
12184543Smarks 	else
12194543Smarks 		return (0);
12204543Smarks }
12214543Smarks 
12224543Smarks static void
12234543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12244543Smarks {
12254543Smarks 	zfs_perm_node_t *permnode;
12265367Sahrens 	void *cookie = NULL;
12275367Sahrens 
12285367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12294543Smarks 		free(permnode);
12305367Sahrens 	avl_destroy(tree);
12314543Smarks }
12324543Smarks 
12334543Smarks static void
12344543Smarks zfs_destroy_tree(avl_tree_t *tree)
12354543Smarks {
12364543Smarks 	zfs_allow_node_t *allownode;
12375367Sahrens 	void *cookie = NULL;
12385367Sahrens 
12394543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12404543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12414543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12424543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12434543Smarks 		free(allownode);
12444543Smarks 	}
12455367Sahrens 	avl_destroy(tree);
12464543Smarks }
12474543Smarks 
12484543Smarks void
12494543Smarks zfs_free_allows(zfs_allow_t *allow)
12504543Smarks {
12514543Smarks 	zfs_allow_t *allownext;
12524543Smarks 	zfs_allow_t *freeallow;
12534543Smarks 
12544543Smarks 	allownext = allow;
12554543Smarks 	while (allownext) {
12564543Smarks 		zfs_destroy_tree(&allownext->z_sets);
12574543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
12584543Smarks 		zfs_destroy_tree(&allownext->z_user);
12594543Smarks 		zfs_destroy_tree(&allownext->z_group);
12604543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
12614543Smarks 		freeallow = allownext;
12624543Smarks 		allownext = allownext->z_next;
12634543Smarks 		free(freeallow);
12644543Smarks 	}
12654543Smarks }
12664543Smarks 
12674543Smarks static zfs_allow_t *
12684543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
12694543Smarks {
12704543Smarks 	zfs_allow_t *ptree;
12714543Smarks 
12724543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
12734543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
12744543Smarks 		return (NULL);
12754543Smarks 	}
12764543Smarks 
12774543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
12784543Smarks 	avl_create(&ptree->z_sets,
12794543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12804543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12814543Smarks 	avl_create(&ptree->z_crperms,
12824543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12834543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12844543Smarks 	avl_create(&ptree->z_user,
12854543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12864543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12874543Smarks 	avl_create(&ptree->z_group,
12884543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12894543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12904543Smarks 	avl_create(&ptree->z_everyone,
12914543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12924543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12934543Smarks 
12944543Smarks 	if (prev)
12954543Smarks 		prev->z_next = ptree;
12964543Smarks 	ptree->z_next = NULL;
12974543Smarks 	return (ptree);
12984543Smarks }
12994543Smarks 
13004543Smarks /*
13014543Smarks  * Add permissions to the appropriate AVL permission tree.
13024543Smarks  * The appropriate tree may not be the requested tree.
13034543Smarks  * For example if ld indicates a local permission, but
13044543Smarks  * same permission also exists as a descendent permission
13054543Smarks  * then the permission will be removed from the descendent
13064543Smarks  * tree and add the the local+descendent tree.
13074543Smarks  */
13084543Smarks static int
13094543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13104543Smarks     char *perm, char ld)
13114543Smarks {
13124543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13134543Smarks 	zfs_perm_node_t *newnode;
13144543Smarks 	avl_index_t where, where2;
13154543Smarks 	avl_tree_t *tree, *altree;
13164543Smarks 
13174543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13184543Smarks 
13194543Smarks 	if (ld == ZFS_DELEG_NA) {
13204543Smarks 		tree =  &allownode->z_localdescend;
13214543Smarks 		altree = &allownode->z_descend;
13224543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13234543Smarks 		tree = &allownode->z_local;
13244543Smarks 		altree = &allownode->z_descend;
13254543Smarks 	} else {
13264543Smarks 		tree = &allownode->z_descend;
13274543Smarks 		altree = &allownode->z_local;
13284543Smarks 	}
13294543Smarks 	permnode = avl_find(tree, &pnode, &where);
13304543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13314543Smarks 
13324543Smarks 	if (permnode2) {
13334543Smarks 		avl_remove(altree, permnode2);
13344543Smarks 		free(permnode2);
13354543Smarks 		if (permnode == NULL) {
13364543Smarks 			tree =  &allownode->z_localdescend;
13374543Smarks 		}
13384543Smarks 	}
13394543Smarks 
13404543Smarks 	/*
13414543Smarks 	 * Now insert new permission in either requested location
13424543Smarks 	 * local/descendent or into ld when perm will exist in both.
13434543Smarks 	 */
13444543Smarks 	if (permnode == NULL) {
13454543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13464543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
13474543Smarks 			return (-1);
13484543Smarks 		}
13494543Smarks 		*newnode = pnode;
13504543Smarks 		avl_add(tree, newnode);
13514543Smarks 	}
13524543Smarks 	return (0);
13534543Smarks }
13544577Sahrens 
13554543Smarks /*
13564543Smarks  * Uggh, this is going to be a bit complicated.
13574543Smarks  * we have an nvlist coming out of the kernel that
13584543Smarks  * will indicate where the permission is set and then
13594543Smarks  * it will contain allow of the various "who's", and what
13604543Smarks  * their permissions are.  To further complicate this
13614543Smarks  * we will then have to coalesce the local,descendent
13624543Smarks  * and local+descendent permissions where appropriate.
13634543Smarks  * The kernel only knows about a permission as being local
13644543Smarks  * or descendent, but not both.
13654543Smarks  *
13664543Smarks  * In order to make this easier for zfs_main to deal with
13674543Smarks  * a series of AVL trees will be used to maintain
13684543Smarks  * all of this, primarily for sorting purposes as well
13694543Smarks  * as the ability to quickly locate a specific entry.
13704543Smarks  *
13714543Smarks  * What we end up with are tree's for sets, create perms,
13724543Smarks  * user, groups and everyone.  With each of those trees
13734543Smarks  * we have subtrees for local, descendent and local+descendent
13744543Smarks  * permissions.
13754543Smarks  */
13764543Smarks int
13774543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
13784543Smarks {
13794543Smarks 	zfs_cmd_t zc = { 0 };
13804543Smarks 	int error;
13814543Smarks 	nvlist_t *nvlist;
13824543Smarks 	nvlist_t *permnv, *sourcenv;
13834543Smarks 	nvpair_t *who_pair, *source_pair;
13844543Smarks 	nvpair_t *perm_pair;
13854543Smarks 	char errbuf[1024];
13864543Smarks 	zfs_allow_t *zallowp, *newallowp;
13874543Smarks 	char  ld;
13884543Smarks 	char *nvpname;
13894543Smarks 	uid_t	uid;
13904543Smarks 	gid_t	gid;
13914543Smarks 	avl_tree_t *tree;
13924543Smarks 	avl_index_t where;
13934543Smarks 
13944543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13954543Smarks 
13964543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
13974543Smarks 		return (-1);
13984543Smarks 
13994543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14004543Smarks 		if (errno == ENOMEM) {
14014543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14024543Smarks 				zcmd_free_nvlists(&zc);
14034543Smarks 				return (-1);
14044543Smarks 			}
14054543Smarks 		} else if (errno == ENOTSUP) {
14064543Smarks 			zcmd_free_nvlists(&zc);
14074543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14084543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14094543Smarks 			return (zfs_error(zhp->zfs_hdl,
14104543Smarks 			    EZFS_BADVERSION, errbuf));
14114543Smarks 		} else {
14124543Smarks 			zcmd_free_nvlists(&zc);
14134543Smarks 			return (-1);
14144543Smarks 		}
14154543Smarks 	}
14164543Smarks 
14174543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14184543Smarks 		zcmd_free_nvlists(&zc);
14194543Smarks 		return (-1);
14204543Smarks 	}
14214543Smarks 
14224543Smarks 	zcmd_free_nvlists(&zc);
14234543Smarks 
14244543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14254543Smarks 
14264543Smarks 	if (source_pair == NULL) {
14274543Smarks 		*zfs_perms = NULL;
14284543Smarks 		return (0);
14294543Smarks 	}
14304543Smarks 
14314543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14324543Smarks 	if (*zfs_perms == NULL) {
14334543Smarks 		return (0);
14344543Smarks 	}
14354543Smarks 
14364543Smarks 	zallowp = *zfs_perms;
14374543Smarks 
14384543Smarks 	for (;;) {
14394543Smarks 		struct passwd *pwd;
14404543Smarks 		struct group *grp;
14414543Smarks 		zfs_allow_node_t *allownode;
14424543Smarks 		zfs_allow_node_t  findallownode;
14434543Smarks 		zfs_allow_node_t *newallownode;
14444543Smarks 
14454543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14464543Smarks 		    nvpair_name(source_pair),
14474543Smarks 		    sizeof (zallowp->z_setpoint));
14484543Smarks 
14494543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
14504543Smarks 			goto abort;
14514543Smarks 
14524543Smarks 		/*
14534543Smarks 		 * Make sure nvlist is composed correctly
14544543Smarks 		 */
14554543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
14564543Smarks 			goto abort;
14574543Smarks 		}
14584543Smarks 
14594543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
14604543Smarks 		if (who_pair == NULL) {
14614543Smarks 			goto abort;
14624543Smarks 		}
14634543Smarks 
14644543Smarks 		do {
14654543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
14664543Smarks 			if (error) {
14674543Smarks 				goto abort;
14684543Smarks 			}
14694543Smarks 
14704543Smarks 			/*
14714543Smarks 			 * First build up the key to use
14724543Smarks 			 * for looking up in the various
14734543Smarks 			 * who trees.
14744543Smarks 			 */
14754543Smarks 			ld = nvpair_name(who_pair)[1];
14764543Smarks 			nvpname = nvpair_name(who_pair);
14774543Smarks 			switch (nvpair_name(who_pair)[0]) {
14784543Smarks 			case ZFS_DELEG_USER:
14794543Smarks 			case ZFS_DELEG_USER_SETS:
14804543Smarks 				tree = &zallowp->z_user;
14814543Smarks 				uid = atol(&nvpname[3]);
14824543Smarks 				pwd = getpwuid(uid);
14834543Smarks 				(void) snprintf(findallownode.z_key,
14844543Smarks 				    sizeof (findallownode.z_key), "user %s",
14854543Smarks 				    (pwd) ? pwd->pw_name :
14864543Smarks 				    &nvpair_name(who_pair)[3]);
14874543Smarks 				break;
14884543Smarks 			case ZFS_DELEG_GROUP:
14894543Smarks 			case ZFS_DELEG_GROUP_SETS:
14904543Smarks 				tree = &zallowp->z_group;
14914543Smarks 				gid = atol(&nvpname[3]);
14924543Smarks 				grp = getgrgid(gid);
14934543Smarks 				(void) snprintf(findallownode.z_key,
14944543Smarks 				    sizeof (findallownode.z_key), "group %s",
14954543Smarks 				    (grp) ? grp->gr_name :
14964543Smarks 				    &nvpair_name(who_pair)[3]);
14974543Smarks 				break;
14984543Smarks 			case ZFS_DELEG_CREATE:
14994543Smarks 			case ZFS_DELEG_CREATE_SETS:
15004543Smarks 				tree = &zallowp->z_crperms;
15014543Smarks 				(void) strlcpy(findallownode.z_key, "",
15024543Smarks 				    sizeof (findallownode.z_key));
15034543Smarks 				break;
15044543Smarks 			case ZFS_DELEG_EVERYONE:
15054543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15064543Smarks 				(void) snprintf(findallownode.z_key,
15074543Smarks 				    sizeof (findallownode.z_key), "everyone");
15084543Smarks 				tree = &zallowp->z_everyone;
15094543Smarks 				break;
15104543Smarks 			case ZFS_DELEG_NAMED_SET:
15114543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15124543Smarks 				(void) snprintf(findallownode.z_key,
15134543Smarks 				    sizeof (findallownode.z_key), "%s",
15144543Smarks 				    &nvpair_name(who_pair)[3]);
15154543Smarks 				tree = &zallowp->z_sets;
15164543Smarks 				break;
15174543Smarks 			}
15184543Smarks 
15194543Smarks 			/*
15204543Smarks 			 * Place who in tree
15214543Smarks 			 */
15224543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15234543Smarks 			if (allownode == NULL) {
15244543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15254543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15264543Smarks 					goto abort;
15274543Smarks 				}
15284543Smarks 				avl_create(&newallownode->z_localdescend,
15294543Smarks 				    perm_compare,
15304543Smarks 				    sizeof (zfs_perm_node_t),
15314543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15324543Smarks 				avl_create(&newallownode->z_local,
15334543Smarks 				    perm_compare,
15344543Smarks 				    sizeof (zfs_perm_node_t),
15354543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15364543Smarks 				avl_create(&newallownode->z_descend,
15374543Smarks 				    perm_compare,
15384543Smarks 				    sizeof (zfs_perm_node_t),
15394543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15404543Smarks 				(void) strlcpy(newallownode->z_key,
15414543Smarks 				    findallownode.z_key,
15424543Smarks 				    sizeof (findallownode.z_key));
15434543Smarks 				avl_insert(tree, newallownode, where);
15444543Smarks 				allownode = newallownode;
15454543Smarks 			}
15464543Smarks 
15474543Smarks 			/*
15484543Smarks 			 * Now iterate over the permissions and
15494543Smarks 			 * place them in the appropriate local,
15504543Smarks 			 * descendent or local+descendent tree.
15514543Smarks 			 *
15524543Smarks 			 * The permissions are added to the tree
15534543Smarks 			 * via zfs_coalesce_perm().
15544543Smarks 			 */
15554543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
15564543Smarks 			if (perm_pair == NULL)
15574543Smarks 				goto abort;
15584543Smarks 			do {
15594543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
15604543Smarks 				    nvpair_name(perm_pair), ld) != 0)
15614543Smarks 					goto abort;
15624543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
15634543Smarks 			    perm_pair));
15644543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
15654543Smarks 
15664543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
15674543Smarks 		if (source_pair == NULL)
15684543Smarks 			break;
15694543Smarks 
15704543Smarks 		/*
15714543Smarks 		 * allocate another node from the link list of
15724543Smarks 		 * zfs_allow_t structures
15734543Smarks 		 */
15744543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
15754543Smarks 		    nvpair_name(source_pair));
15764543Smarks 		if (newallowp == NULL) {
15774543Smarks 			goto abort;
15784543Smarks 		}
15794543Smarks 		zallowp = newallowp;
15804543Smarks 	}
15814543Smarks 	nvlist_free(nvlist);
15824543Smarks 	return (0);
15834543Smarks abort:
15844543Smarks 	zfs_free_allows(*zfs_perms);
15854543Smarks 	nvlist_free(nvlist);
15864543Smarks 	return (-1);
15874543Smarks }
15884543Smarks 
1589789Sahrens /*
1590789Sahrens  * Given a property name and value, set the property for the given dataset.
1591789Sahrens  */
1592789Sahrens int
15932676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1594789Sahrens {
1595789Sahrens 	zfs_cmd_t zc = { 0 };
15962676Seschrock 	int ret = -1;
15972676Seschrock 	prop_changelist_t *cl = NULL;
15982082Seschrock 	char errbuf[1024];
15992082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
16002676Seschrock 	nvlist_t *nvl = NULL, *realprops;
16012676Seschrock 	zfs_prop_t prop;
16022082Seschrock 
16032082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
16042676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
16052082Seschrock 	    zhp->zfs_name);
16062082Seschrock 
16072676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
16082676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
16092676Seschrock 		(void) no_memory(hdl);
16102676Seschrock 		goto error;
1611789Sahrens 	}
1612789Sahrens 
16135094Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
16142676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
16152676Seschrock 		goto error;
16165094Slling 
16172676Seschrock 	nvlist_free(nvl);
16182676Seschrock 	nvl = realprops;
16192676Seschrock 
16202676Seschrock 	prop = zfs_name_to_prop(propname);
16212676Seschrock 
1622789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
16232676Seschrock 		goto error;
1624789Sahrens 
1625789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
16262082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16272082Seschrock 		    "child dataset with inherited mountpoint is used "
16282082Seschrock 		    "in a non-global zone"));
16292082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1630789Sahrens 		goto error;
1631789Sahrens 	}
1632789Sahrens 
1633789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1634789Sahrens 		goto error;
1635789Sahrens 
1636789Sahrens 	/*
1637789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1638789Sahrens 	 */
1639789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1640789Sahrens 
16415094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
16422676Seschrock 		goto error;
16432676Seschrock 
16444543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1645789Sahrens 
1646789Sahrens 	if (ret != 0) {
1647789Sahrens 		switch (errno) {
1648789Sahrens 
1649789Sahrens 		case ENOSPC:
1650789Sahrens 			/*
1651789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1652789Sahrens 			 * something different; setting a quota or reservation
1653789Sahrens 			 * doesn't use any disk space.
1654789Sahrens 			 */
1655789Sahrens 			switch (prop) {
1656789Sahrens 			case ZFS_PROP_QUOTA:
16575378Sck153898 			case ZFS_PROP_REFQUOTA:
16582082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16592082Seschrock 				    "size is less than current used or "
16602082Seschrock 				    "reserved space"));
16612082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1662789Sahrens 				break;
1663789Sahrens 
1664789Sahrens 			case ZFS_PROP_RESERVATION:
16655378Sck153898 			case ZFS_PROP_REFRESERVATION:
16662082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16672082Seschrock 				    "size is greater than available space"));
16682082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1669789Sahrens 				break;
1670789Sahrens 
1671789Sahrens 			default:
16722082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1673789Sahrens 				break;
1674789Sahrens 			}
1675789Sahrens 			break;
1676789Sahrens 
1677789Sahrens 		case EBUSY:
16782082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
16792082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
16802082Seschrock 			else
16812676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1682789Sahrens 			break;
1683789Sahrens 
16841175Slling 		case EROFS:
16852082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
16861175Slling 			break;
16871175Slling 
16883886Sahl 		case ENOTSUP:
16893886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16904603Sahrens 			    "pool must be upgraded to set this "
16914603Sahrens 			    "property or value"));
16923886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
16933886Sahl 			break;
16943886Sahl 
1695789Sahrens 		case EOVERFLOW:
1696789Sahrens 			/*
1697789Sahrens 			 * This platform can't address a volume this big.
1698789Sahrens 			 */
1699789Sahrens #ifdef _ILP32
1700789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
17012082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1702789Sahrens 				break;
1703789Sahrens 			}
1704789Sahrens #endif
17052082Seschrock 			/* FALLTHROUGH */
1706789Sahrens 		default:
17072082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1708789Sahrens 		}
1709789Sahrens 	} else {
1710789Sahrens 		/*
1711789Sahrens 		 * Refresh the statistics so the new property value
1712789Sahrens 		 * is reflected.
1713789Sahrens 		 */
17142676Seschrock 		if ((ret = changelist_postfix(cl)) == 0)
17152676Seschrock 			(void) get_stats(zhp);
1716789Sahrens 	}
1717789Sahrens 
1718789Sahrens error:
17192676Seschrock 	nvlist_free(nvl);
17202676Seschrock 	zcmd_free_nvlists(&zc);
17212676Seschrock 	if (cl)
17222676Seschrock 		changelist_free(cl);
1723789Sahrens 	return (ret);
1724789Sahrens }
1725789Sahrens 
1726789Sahrens /*
1727789Sahrens  * Given a property, inherit the value from the parent dataset.
1728789Sahrens  */
1729789Sahrens int
17302676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1731789Sahrens {
1732789Sahrens 	zfs_cmd_t zc = { 0 };
1733789Sahrens 	int ret;
1734789Sahrens 	prop_changelist_t *cl;
17352082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17362082Seschrock 	char errbuf[1024];
17372676Seschrock 	zfs_prop_t prop;
17382082Seschrock 
17392082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
17402082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1741789Sahrens 
17425094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
17432676Seschrock 		/*
17442676Seschrock 		 * For user properties, the amount of work we have to do is very
17452676Seschrock 		 * small, so just do it here.
17462676Seschrock 		 */
17472676Seschrock 		if (!zfs_prop_user(propname)) {
17482676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17492676Seschrock 			    "invalid property"));
17502676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
17512676Seschrock 		}
17522676Seschrock 
17532676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
17542676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
17552676Seschrock 
17564849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
17572676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
17582676Seschrock 
17592676Seschrock 		return (0);
17602676Seschrock 	}
17612676Seschrock 
1762789Sahrens 	/*
1763789Sahrens 	 * Verify that this property is inheritable.
1764789Sahrens 	 */
17652082Seschrock 	if (zfs_prop_readonly(prop))
17662082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
17672082Seschrock 
17682082Seschrock 	if (!zfs_prop_inheritable(prop))
17692082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1770789Sahrens 
1771789Sahrens 	/*
1772789Sahrens 	 * Check to see if the value applies to this type
1773789Sahrens 	 */
17742082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
17752082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1776789Sahrens 
17773443Srm160521 	/*
17783443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
17793443Srm160521 	 */
17803443Srm160521 	propname = zfs_prop_to_name(prop);
1781789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
17822676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1783789Sahrens 
1784789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1785789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
17862082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17872082Seschrock 		    "dataset is used in a non-global zone"));
17882082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1789789Sahrens 	}
1790789Sahrens 
1791789Sahrens 	/*
1792789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1793789Sahrens 	 */
1794789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1795789Sahrens 		return (-1);
1796789Sahrens 
1797789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17982082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17992082Seschrock 		    "child dataset with inherited mountpoint is used "
18002082Seschrock 		    "in a non-global zone"));
18012082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1802789Sahrens 		goto error;
1803789Sahrens 	}
1804789Sahrens 
1805789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1806789Sahrens 		goto error;
1807789Sahrens 
18084849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
18092082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1810789Sahrens 	} else {
1811789Sahrens 
18122169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1813789Sahrens 			goto error;
1814789Sahrens 
1815789Sahrens 		/*
1816789Sahrens 		 * Refresh the statistics so the new property is reflected.
1817789Sahrens 		 */
1818789Sahrens 		(void) get_stats(zhp);
1819789Sahrens 	}
1820789Sahrens 
1821789Sahrens error:
1822789Sahrens 	changelist_free(cl);
1823789Sahrens 	return (ret);
1824789Sahrens }
1825789Sahrens 
1826789Sahrens /*
18271356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
18281356Seschrock  * extract them appropriately.
18291356Seschrock  */
18301356Seschrock static uint64_t
18311356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18321356Seschrock {
18331356Seschrock 	nvlist_t *nv;
18341356Seschrock 	uint64_t value;
18351356Seschrock 
18362885Sahrens 	*source = NULL;
18371356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
18381356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
18395094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
18405094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
18411356Seschrock 	} else {
18421356Seschrock 		value = zfs_prop_default_numeric(prop);
18431356Seschrock 		*source = "";
18441356Seschrock 	}
18451356Seschrock 
18461356Seschrock 	return (value);
18471356Seschrock }
18481356Seschrock 
18491356Seschrock static char *
18501356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18511356Seschrock {
18521356Seschrock 	nvlist_t *nv;
18531356Seschrock 	char *value;
18541356Seschrock 
18552885Sahrens 	*source = NULL;
18561356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
18571356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
18585094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
18595094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
18601356Seschrock 	} else {
18611356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
18621356Seschrock 			value = "";
18631356Seschrock 		*source = "";
18641356Seschrock 	}
18651356Seschrock 
18661356Seschrock 	return (value);
18671356Seschrock }
18681356Seschrock 
18691356Seschrock /*
1870789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1871789Sahrens  * zfs_prop_get_int() are built using this interface.
1872789Sahrens  *
1873789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1874789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1875789Sahrens  * If they differ from the on-disk values, report the current values and mark
1876789Sahrens  * the source "temporary".
1877789Sahrens  */
18782082Seschrock static int
18795094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
18802082Seschrock     char **source, uint64_t *val)
1881789Sahrens {
18825147Srm160521 	zfs_cmd_t zc = { 0 };
1883789Sahrens 	struct mnttab mnt;
18843265Sahrens 	char *mntopt_on = NULL;
18853265Sahrens 	char *mntopt_off = NULL;
1886789Sahrens 
1887789Sahrens 	*source = NULL;
1888789Sahrens 
18893265Sahrens 	switch (prop) {
18903265Sahrens 	case ZFS_PROP_ATIME:
18913265Sahrens 		mntopt_on = MNTOPT_ATIME;
18923265Sahrens 		mntopt_off = MNTOPT_NOATIME;
18933265Sahrens 		break;
18943265Sahrens 
18953265Sahrens 	case ZFS_PROP_DEVICES:
18963265Sahrens 		mntopt_on = MNTOPT_DEVICES;
18973265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
18983265Sahrens 		break;
18993265Sahrens 
19003265Sahrens 	case ZFS_PROP_EXEC:
19013265Sahrens 		mntopt_on = MNTOPT_EXEC;
19023265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
19033265Sahrens 		break;
19043265Sahrens 
19053265Sahrens 	case ZFS_PROP_READONLY:
19063265Sahrens 		mntopt_on = MNTOPT_RO;
19073265Sahrens 		mntopt_off = MNTOPT_RW;
19083265Sahrens 		break;
19093265Sahrens 
19103265Sahrens 	case ZFS_PROP_SETUID:
19113265Sahrens 		mntopt_on = MNTOPT_SETUID;
19123265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
19133265Sahrens 		break;
19143265Sahrens 
19153265Sahrens 	case ZFS_PROP_XATTR:
19163265Sahrens 		mntopt_on = MNTOPT_XATTR;
19173265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
19183265Sahrens 		break;
19195331Samw 
19205331Samw 	case ZFS_PROP_NBMAND:
19215331Samw 		mntopt_on = MNTOPT_NBMAND;
19225331Samw 		mntopt_off = MNTOPT_NONBMAND;
19235331Samw 		break;
19243265Sahrens 	}
19253265Sahrens 
19262474Seschrock 	/*
19272474Seschrock 	 * Because looking up the mount options is potentially expensive
19282474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
19292474Seschrock 	 * we're looking up a property which requires its presence.
19302474Seschrock 	 */
19312474Seschrock 	if (!zhp->zfs_mntcheck &&
19323265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
19333265Sahrens 		struct mnttab entry, search = { 0 };
19343265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
19352474Seschrock 
19362474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
19372474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
19383265Sahrens 		rewind(mnttab);
19393265Sahrens 
19403265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
19413265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
19423265Sahrens 			    entry.mnt_mntopts);
19433265Sahrens 			if (zhp->zfs_mntopts == NULL)
19443265Sahrens 				return (-1);
19453265Sahrens 		}
19462474Seschrock 
19472474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
19482474Seschrock 	}
19492474Seschrock 
1950789Sahrens 	if (zhp->zfs_mntopts == NULL)
1951789Sahrens 		mnt.mnt_mntopts = "";
1952789Sahrens 	else
1953789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1954789Sahrens 
1955789Sahrens 	switch (prop) {
1956789Sahrens 	case ZFS_PROP_ATIME:
19573265Sahrens 	case ZFS_PROP_DEVICES:
19583265Sahrens 	case ZFS_PROP_EXEC:
19593265Sahrens 	case ZFS_PROP_READONLY:
19603265Sahrens 	case ZFS_PROP_SETUID:
19613265Sahrens 	case ZFS_PROP_XATTR:
19625331Samw 	case ZFS_PROP_NBMAND:
19632082Seschrock 		*val = getprop_uint64(zhp, prop, source);
19642082Seschrock 
19653265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
19662082Seschrock 			*val = B_TRUE;
1967789Sahrens 			if (src)
19685094Slling 				*src = ZPROP_SRC_TEMPORARY;
19693265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
19702082Seschrock 			*val = B_FALSE;
1971789Sahrens 			if (src)
19725094Slling 				*src = ZPROP_SRC_TEMPORARY;
1973789Sahrens 		}
19742082Seschrock 		break;
1975789Sahrens 
19763265Sahrens 	case ZFS_PROP_CANMOUNT:
19772082Seschrock 		*val = getprop_uint64(zhp, prop, source);
19783417Srm160521 		if (*val == 0)
19793417Srm160521 			*source = zhp->zfs_name;
19803417Srm160521 		else
19813417Srm160521 			*source = "";	/* default */
19822082Seschrock 		break;
1983789Sahrens 
1984789Sahrens 	case ZFS_PROP_QUOTA:
19855378Sck153898 	case ZFS_PROP_REFQUOTA:
1986789Sahrens 	case ZFS_PROP_RESERVATION:
19875378Sck153898 	case ZFS_PROP_REFRESERVATION:
19882885Sahrens 		*val = getprop_uint64(zhp, prop, source);
19892885Sahrens 		if (*val == 0)
1990789Sahrens 			*source = "";	/* default */
1991789Sahrens 		else
1992789Sahrens 			*source = zhp->zfs_name;
19932082Seschrock 		break;
1994789Sahrens 
1995789Sahrens 	case ZFS_PROP_MOUNTED:
19962082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
19972082Seschrock 		break;
1998789Sahrens 
19993377Seschrock 	case ZFS_PROP_NUMCLONES:
20003377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
20013377Seschrock 		break;
20023377Seschrock 
20035147Srm160521 	case ZFS_PROP_VERSION:
20045473Srm160521 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type))
20055473Srm160521 			return (-1);
20065147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
20075147Srm160521 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) ||
20085147Srm160521 		    (zc.zc_cookie == 0)) {
20095147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
20105147Srm160521 			    "unable to get version property"));
20115147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
20125147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
20135147Srm160521 		}
20145147Srm160521 		*val = zc.zc_cookie;
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