xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21f3861e1aSahl 
22fa9e4066Sahrens /*
239e6eda55Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <assert.h>
30fa9e4066Sahrens #include <ctype.h>
31fa9e4066Sahrens #include <errno.h>
32fa9e4066Sahrens #include <libdevinfo.h>
33fa9e4066Sahrens #include <libintl.h>
34fa9e4066Sahrens #include <math.h>
35fa9e4066Sahrens #include <stdio.h>
36fa9e4066Sahrens #include <stdlib.h>
37fa9e4066Sahrens #include <strings.h>
38fa9e4066Sahrens #include <unistd.h>
393cb34c60Sahrens #include <stddef.h>
40fa9e4066Sahrens #include <zone.h>
4199653d4eSeschrock #include <fcntl.h>
42fa9e4066Sahrens #include <sys/mntent.h>
43fa9e4066Sahrens #include <sys/mnttab.h>
44b12a1c38Slling #include <sys/mount.h>
45ecd6cf80Smarks #include <sys/avl.h>
46ecd6cf80Smarks #include <priv.h>
47ecd6cf80Smarks #include <pwd.h>
48ecd6cf80Smarks #include <grp.h>
49ecd6cf80Smarks #include <stddef.h>
50ecd6cf80Smarks #include <ucred.h>
51fa9e4066Sahrens 
52fa9e4066Sahrens #include <sys/spa.h>
53e9dbad6fSeschrock #include <sys/zap.h>
54fa9e4066Sahrens #include <libzfs.h>
55fa9e4066Sahrens 
56fa9e4066Sahrens #include "zfs_namecheck.h"
57fa9e4066Sahrens #include "zfs_prop.h"
58fa9e4066Sahrens #include "libzfs_impl.h"
59ecd6cf80Smarks #include "zfs_deleg.h"
60fa9e4066Sahrens 
61cdf5b4caSmmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
62cdf5b4caSmmusante 
63fa9e4066Sahrens /*
64fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
65fa9e4066Sahrens  * readable form.
66fa9e4066Sahrens  */
67fa9e4066Sahrens const char *
68fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
69fa9e4066Sahrens {
70fa9e4066Sahrens 	switch (type) {
71fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
72fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
74fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
76fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
77fa9e4066Sahrens 	}
78fa9e4066Sahrens 
79fa9e4066Sahrens 	return (NULL);
80fa9e4066Sahrens }
81fa9e4066Sahrens 
82fa9e4066Sahrens /*
83fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
84fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
85fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
86fa9e4066Sahrens  * acceptable types.
87fa9e4066Sahrens  */
88fa9e4066Sahrens static const char *
89fa9e4066Sahrens path_to_str(const char *path, int types)
90fa9e4066Sahrens {
91fa9e4066Sahrens 	/*
92fa9e4066Sahrens 	 * When given a single type, always report the exact type.
93fa9e4066Sahrens 	 */
94fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
95fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
97fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
99fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
100fa9e4066Sahrens 
101fa9e4066Sahrens 	/*
102fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
103fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
104fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105fa9e4066Sahrens 	 * snapshot attribute and try again.
106fa9e4066Sahrens 	 */
107fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
108fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
109fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111fa9e4066Sahrens 	}
112fa9e4066Sahrens 
113fa9e4066Sahrens 
114fa9e4066Sahrens 	/*
115fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
116fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118fa9e4066Sahrens 	 */
119fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121fa9e4066Sahrens 
122fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124fa9e4066Sahrens }
125fa9e4066Sahrens 
126fa9e4066Sahrens /*
127fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128fa9e4066Sahrens  * provide a more meaningful error message.  We place a more useful message in
129fa9e4066Sahrens  * 'buf' detailing exactly why the name was not valid.
130fa9e4066Sahrens  */
131fa9e4066Sahrens static int
132f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
133f18faf3fSek110237     boolean_t modifying)
134fa9e4066Sahrens {
135fa9e4066Sahrens 	namecheck_err_t why;
136fa9e4066Sahrens 	char what;
137fa9e4066Sahrens 
138fa9e4066Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
13999653d4eSeschrock 		if (hdl != NULL) {
140fa9e4066Sahrens 			switch (why) {
141b81d61a6Slling 			case NAME_ERR_TOOLONG:
14299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14399653d4eSeschrock 				    "name is too long"));
144b81d61a6Slling 				break;
145b81d61a6Slling 
146fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
14799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14899653d4eSeschrock 				    "leading slash in name"));
149fa9e4066Sahrens 				break;
150fa9e4066Sahrens 
151fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15399653d4eSeschrock 				    "empty component in name"));
154fa9e4066Sahrens 				break;
155fa9e4066Sahrens 
156fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
15799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15899653d4eSeschrock 				    "trailing slash in name"));
159fa9e4066Sahrens 				break;
160fa9e4066Sahrens 
161fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16299653d4eSeschrock 				zfs_error_aux(hdl,
163fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16499653d4eSeschrock 				    "'%c' in name"), what);
165fa9e4066Sahrens 				break;
166fa9e4066Sahrens 
167fa9e4066Sahrens 			case NAME_ERR_MULTIPLE_AT:
16899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16999653d4eSeschrock 				    "multiple '@' delimiters in name"));
170fa9e4066Sahrens 				break;
1715ad82045Snd150628 
1725ad82045Snd150628 			case NAME_ERR_NOLETTER:
1735ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1745ad82045Snd150628 				    "pool doesn't begin with a letter"));
1755ad82045Snd150628 				break;
1765ad82045Snd150628 
1775ad82045Snd150628 			case NAME_ERR_RESERVED:
1785ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1795ad82045Snd150628 				    "name is reserved"));
1805ad82045Snd150628 				break;
1815ad82045Snd150628 
1825ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1835ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1845ad82045Snd150628 				    "reserved disk name"));
1855ad82045Snd150628 				break;
186fa9e4066Sahrens 			}
187fa9e4066Sahrens 		}
188fa9e4066Sahrens 
189fa9e4066Sahrens 		return (0);
190fa9e4066Sahrens 	}
191fa9e4066Sahrens 
192fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19399653d4eSeschrock 		if (hdl != NULL)
19499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19599653d4eSeschrock 			    "snapshot delimiter '@' in filesystem name"));
196fa9e4066Sahrens 		return (0);
197fa9e4066Sahrens 	}
198fa9e4066Sahrens 
1991d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2001d452cf5Sahrens 		if (hdl != NULL)
2011d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
202d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2031d452cf5Sahrens 		return (0);
2041d452cf5Sahrens 	}
2051d452cf5Sahrens 
206f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
207f18faf3fSek110237 		if (hdl != NULL)
208f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
209f18faf3fSek110237 			    "invalid character %c in name"), '%');
210f18faf3fSek110237 		return (0);
211f18faf3fSek110237 	}
212f18faf3fSek110237 
21399653d4eSeschrock 	return (-1);
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216fa9e4066Sahrens int
217fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218fa9e4066Sahrens {
219*e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
220*e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
221f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222fa9e4066Sahrens }
223fa9e4066Sahrens 
224fa9e4066Sahrens /*
225e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
226e9dbad6fSeschrock  * properties into a separate nvlist.
227e9dbad6fSeschrock  */
228fac3008cSeschrock static nvlist_t *
229fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
230e9dbad6fSeschrock {
231e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
232e9dbad6fSeschrock 	nvpair_t *elem;
233e9dbad6fSeschrock 	nvlist_t *propval;
234fac3008cSeschrock 	nvlist_t *nvl;
235e9dbad6fSeschrock 
236fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
237fac3008cSeschrock 		(void) no_memory(hdl);
238fac3008cSeschrock 		return (NULL);
239fac3008cSeschrock 	}
240e9dbad6fSeschrock 
241e9dbad6fSeschrock 	elem = NULL;
242fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
243e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
244e9dbad6fSeschrock 			continue;
245e9dbad6fSeschrock 
246e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
247fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
248fac3008cSeschrock 			nvlist_free(nvl);
249fac3008cSeschrock 			(void) no_memory(hdl);
250fac3008cSeschrock 			return (NULL);
251fac3008cSeschrock 		}
252e9dbad6fSeschrock 	}
253e9dbad6fSeschrock 
254fac3008cSeschrock 	return (nvl);
255e9dbad6fSeschrock }
256e9dbad6fSeschrock 
257e9dbad6fSeschrock /*
258fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
259fa9e4066Sahrens  */
260fa9e4066Sahrens static int
261fa9e4066Sahrens get_stats(zfs_handle_t *zhp)
262fa9e4066Sahrens {
263fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
264e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
265fac3008cSeschrock 	nvlist_t *allprops, *userprops;
266fa9e4066Sahrens 
267fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
268fa9e4066Sahrens 
269e9dbad6fSeschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
27099653d4eSeschrock 		return (-1);
2717f7322feSeschrock 
27299653d4eSeschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
2737f7322feSeschrock 		if (errno == ENOMEM) {
274e9dbad6fSeschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
275e9dbad6fSeschrock 				zcmd_free_nvlists(&zc);
27699653d4eSeschrock 				return (-1);
277e9dbad6fSeschrock 			}
2787f7322feSeschrock 		} else {
279e9dbad6fSeschrock 			zcmd_free_nvlists(&zc);
280fa9e4066Sahrens 			return (-1);
2817f7322feSeschrock 		}
2827f7322feSeschrock 	}
283fa9e4066Sahrens 
284a2eea2e1Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
285fa9e4066Sahrens 
286e9dbad6fSeschrock 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
287ea8dc4b6Seschrock 
288fac3008cSeschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
289fac3008cSeschrock 		zcmd_free_nvlists(&zc);
290fac3008cSeschrock 		return (-1);
291fac3008cSeschrock 	}
292fac3008cSeschrock 
293fac3008cSeschrock 	zcmd_free_nvlists(&zc);
294fac3008cSeschrock 
295fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
296fac3008cSeschrock 		nvlist_free(allprops);
297fac3008cSeschrock 		return (-1);
298fac3008cSeschrock 	}
299fac3008cSeschrock 
30099653d4eSeschrock 	nvlist_free(zhp->zfs_props);
301fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
30299653d4eSeschrock 
303fac3008cSeschrock 	zhp->zfs_props = allprops;
304fac3008cSeschrock 	zhp->zfs_user_props = userprops;
30599653d4eSeschrock 
306fa9e4066Sahrens 	return (0);
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309fa9e4066Sahrens /*
310fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
311fa9e4066Sahrens  */
312fa9e4066Sahrens void
313fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
314fa9e4066Sahrens {
315fa9e4066Sahrens 	(void) get_stats(zhp);
316fa9e4066Sahrens }
317fa9e4066Sahrens 
318fa9e4066Sahrens /*
319fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
320fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
321fa9e4066Sahrens  */
322fa9e4066Sahrens zfs_handle_t *
32399653d4eSeschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
324fa9e4066Sahrens {
32599653d4eSeschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
326ecd6cf80Smarks 	char *logstr;
32799653d4eSeschrock 
32899653d4eSeschrock 	if (zhp == NULL)
32999653d4eSeschrock 		return (NULL);
33099653d4eSeschrock 
33199653d4eSeschrock 	zhp->zfs_hdl = hdl;
332fa9e4066Sahrens 
333ecd6cf80Smarks 	/*
334ecd6cf80Smarks 	 * Preserve history log string.
335ecd6cf80Smarks 	 * any changes performed here will be
336ecd6cf80Smarks 	 * logged as an internal event.
337ecd6cf80Smarks 	 */
338ecd6cf80Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
339ecd6cf80Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
34031fd60d3Sahrens top:
341fa9e4066Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
342fa9e4066Sahrens 
343fa9e4066Sahrens 	if (get_stats(zhp) != 0) {
344ecd6cf80Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
345fa9e4066Sahrens 		free(zhp);
346fa9e4066Sahrens 		return (NULL);
347fa9e4066Sahrens 	}
348fa9e4066Sahrens 
34931fd60d3Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
35031fd60d3Sahrens 		zfs_cmd_t zc = { 0 };
35131fd60d3Sahrens 
35231fd60d3Sahrens 		/*
35331fd60d3Sahrens 		 * If it is dds_inconsistent, then we've caught it in
35431fd60d3Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
35531fd60d3Sahrens 		 * it is inconsistent from the ZPL's point of view, so
35631fd60d3Sahrens 		 * can't be mounted.  However, it could also be that we
35731fd60d3Sahrens 		 * have crashed in the middle of one of those
35831fd60d3Sahrens 		 * operations, in which case we need to get rid of the
35931fd60d3Sahrens 		 * inconsistent state.  We do that by either rolling
36031fd60d3Sahrens 		 * back to the previous snapshot (which will fail if
36131fd60d3Sahrens 		 * there is none), or destroying the filesystem.  Note
36231fd60d3Sahrens 		 * that if we are still in the middle of an active
36331fd60d3Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
36431fd60d3Sahrens 		 * will fail with EBUSY and we will drive on as usual.
36531fd60d3Sahrens 		 */
36631fd60d3Sahrens 
36731fd60d3Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
36831fd60d3Sahrens 
369a2eea2e1Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
37099653d4eSeschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
37131fd60d3Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
37231fd60d3Sahrens 		} else {
37331fd60d3Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
37431fd60d3Sahrens 		}
37531fd60d3Sahrens 
37631fd60d3Sahrens 		/*
377da6c28aaSamw 		 * If we can successfully destroy it, pretend that it
37831fd60d3Sahrens 		 * never existed.
37931fd60d3Sahrens 		 */
38099653d4eSeschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
381ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
38231fd60d3Sahrens 			free(zhp);
38331fd60d3Sahrens 			errno = ENOENT;
38431fd60d3Sahrens 			return (NULL);
38531fd60d3Sahrens 		}
3863cb34c60Sahrens 		/* If we can successfully roll it back, reget the stats */
3873cb34c60Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
3883cb34c60Sahrens 			goto top;
38931fd60d3Sahrens 	}
39031fd60d3Sahrens 
391fa9e4066Sahrens 	/*
392fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
393fa9e4066Sahrens 	 * the high-level type.
394fa9e4066Sahrens 	 */
395a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
396a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
397a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
398a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
399a2eea2e1Sahrens 	else
400a2eea2e1Sahrens 		abort();
401a2eea2e1Sahrens 
402fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
403fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
404fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
405fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
406fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
407fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
408fa9e4066Sahrens 	else
40999653d4eSeschrock 		abort();	/* we should never see any other types */
410fa9e4066Sahrens 
411ecd6cf80Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
412fa9e4066Sahrens 	return (zhp);
413fa9e4066Sahrens }
414fa9e4066Sahrens 
415fa9e4066Sahrens /*
416fa9e4066Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
417fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
418fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
419fa9e4066Sahrens  */
420fa9e4066Sahrens zfs_handle_t *
42199653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
422fa9e4066Sahrens {
423fa9e4066Sahrens 	zfs_handle_t *zhp;
42499653d4eSeschrock 	char errbuf[1024];
42599653d4eSeschrock 
42699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
42799653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
428fa9e4066Sahrens 
429fa9e4066Sahrens 	/*
43099653d4eSeschrock 	 * Validate the name before we even try to open it.
431fa9e4066Sahrens 	 */
432f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
43399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43499653d4eSeschrock 		    "invalid dataset name"));
43599653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
436fa9e4066Sahrens 		return (NULL);
437fa9e4066Sahrens 	}
438fa9e4066Sahrens 
439fa9e4066Sahrens 	/*
440fa9e4066Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
441fa9e4066Sahrens 	 */
442fa9e4066Sahrens 	errno = 0;
44399653d4eSeschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
444ece3d9b3Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
445fa9e4066Sahrens 		return (NULL);
446fa9e4066Sahrens 	}
447fa9e4066Sahrens 
448fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
44999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
45094de1d4cSeschrock 		zfs_close(zhp);
451fa9e4066Sahrens 		return (NULL);
452fa9e4066Sahrens 	}
453fa9e4066Sahrens 
454fa9e4066Sahrens 	return (zhp);
455fa9e4066Sahrens }
456fa9e4066Sahrens 
457fa9e4066Sahrens /*
458fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
459fa9e4066Sahrens  */
460fa9e4066Sahrens void
461fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
462fa9e4066Sahrens {
463fa9e4066Sahrens 	if (zhp->zfs_mntopts)
464fa9e4066Sahrens 		free(zhp->zfs_mntopts);
46599653d4eSeschrock 	nvlist_free(zhp->zfs_props);
466e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
467fa9e4066Sahrens 	free(zhp);
468fa9e4066Sahrens }
469fa9e4066Sahrens 
4707b97dc1aSrm160521 int
4717b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
4727b97dc1aSrm160521 {
4737b97dc1aSrm160521 	char *pool_name;
4747b97dc1aSrm160521 	zpool_handle_t *zpool_handle;
4757b97dc1aSrm160521 	char *p;
4767b97dc1aSrm160521 
4777b97dc1aSrm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN);
4787b97dc1aSrm160521 	if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name,
4797b97dc1aSrm160521 	    MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) {
4807b97dc1aSrm160521 		free(pool_name);
4817b97dc1aSrm160521 		return (-1);
4827b97dc1aSrm160521 	}
4837b97dc1aSrm160521 
4847b97dc1aSrm160521 	if (p = strchr(pool_name, '/'))
4857b97dc1aSrm160521 		*p = '\0';
4867b97dc1aSrm160521 	zpool_handle = zpool_open(zhp->zfs_hdl, pool_name);
4877b97dc1aSrm160521 	free(pool_name);
4887b97dc1aSrm160521 	if (zpool_handle == NULL)
4897b97dc1aSrm160521 		return (-1);
4907b97dc1aSrm160521 
4917b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
4927b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
4937b97dc1aSrm160521 	zpool_close(zpool_handle);
4947b97dc1aSrm160521 	return (0);
4957b97dc1aSrm160521 }
4967b97dc1aSrm160521 
4977b97dc1aSrm160521 /*
4987b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
4997b97dc1aSrm160521  */
5007b97dc1aSrm160521 static int
5017b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5027b97dc1aSrm160521 {
5037b97dc1aSrm160521 	int spa_version;
5047b97dc1aSrm160521 
5057b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5067b97dc1aSrm160521 		return (-1);
5077b97dc1aSrm160521 
5087b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5097b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5107b97dc1aSrm160521 	else
5117b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5127b97dc1aSrm160521 
5137b97dc1aSrm160521 	return (0);
5147b97dc1aSrm160521 }
5157b97dc1aSrm160521 
516b1b8ab34Slling /*
517e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
518e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
519e9dbad6fSeschrock  * strings.
520fa9e4066Sahrens  */
521990b4856Slling static nvlist_t *
522990b4856Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
523990b4856Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
524fa9e4066Sahrens {
525e9dbad6fSeschrock 	nvpair_t *elem;
526e9dbad6fSeschrock 	uint64_t intval;
527e9dbad6fSeschrock 	char *strval;
528990b4856Slling 	zfs_prop_t prop;
529e9dbad6fSeschrock 	nvlist_t *ret;
530da6c28aaSamw 	int chosen_normal = -1;
531da6c28aaSamw 	int chosen_utf = -1;
532990b4856Slling 
533990b4856Slling 	if (type == ZFS_TYPE_SNAPSHOT) {
534990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
535990b4856Slling 		    "snapshot properties cannot be modified"));
536990b4856Slling 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
537990b4856Slling 		return (NULL);
538990b4856Slling 	}
539fa9e4066Sahrens 
540e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
541e9dbad6fSeschrock 		(void) no_memory(hdl);
542e9dbad6fSeschrock 		return (NULL);
543e9dbad6fSeschrock 	}
544fa9e4066Sahrens 
545e9dbad6fSeschrock 	elem = NULL;
546e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
547990b4856Slling 		const char *propname = nvpair_name(elem);
54899653d4eSeschrock 
549fa9e4066Sahrens 		/*
550e9dbad6fSeschrock 		 * Make sure this property is valid and applies to this type.
551fa9e4066Sahrens 		 */
552990b4856Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
553990b4856Slling 			if (!zfs_prop_user(propname)) {
554e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555990b4856Slling 				    "invalid property '%s'"), propname);
556e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
557e9dbad6fSeschrock 				goto error;
558e9dbad6fSeschrock 			}
559e9dbad6fSeschrock 
560990b4856Slling 			/*
561990b4856Slling 			 * If this is a user property, make sure it's a
562990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
563990b4856Slling 			 */
564990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
565990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
566990b4856Slling 				    "'%s' must be a string"), propname);
567990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
568990b4856Slling 				goto error;
569990b4856Slling 			}
570990b4856Slling 
571990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
572e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
573e9dbad6fSeschrock 				    "property name '%s' is too long"),
574e9dbad6fSeschrock 				    propname);
575990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
576e9dbad6fSeschrock 				goto error;
577e9dbad6fSeschrock 			}
578e9dbad6fSeschrock 
579e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
580e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
581e9dbad6fSeschrock 				(void) no_memory(hdl);
582e9dbad6fSeschrock 				goto error;
583e9dbad6fSeschrock 			}
584e9dbad6fSeschrock 			continue;
585e9dbad6fSeschrock 		}
586fa9e4066Sahrens 
587e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
588e9dbad6fSeschrock 			zfs_error_aux(hdl,
589e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
590e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
591e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
592e9dbad6fSeschrock 			goto error;
593e9dbad6fSeschrock 		}
594e9dbad6fSeschrock 
595e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
596da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
597e9dbad6fSeschrock 			zfs_error_aux(hdl,
598e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
599e9dbad6fSeschrock 			    propname);
600e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
601e9dbad6fSeschrock 			goto error;
602e9dbad6fSeschrock 		}
603e9dbad6fSeschrock 
604990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
605990b4856Slling 		    &strval, &intval, errbuf) != 0)
606e9dbad6fSeschrock 			goto error;
607e9dbad6fSeschrock 
608e9dbad6fSeschrock 		/*
609e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
610e9dbad6fSeschrock 		 */
611e9dbad6fSeschrock 		switch (prop) {
612e7437265Sahrens 		case ZFS_PROP_VERSION:
613e7437265Sahrens 		{
614e7437265Sahrens 			int version;
615e7437265Sahrens 
616e7437265Sahrens 			if (zhp == NULL)
617e7437265Sahrens 				break;
618e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
619e7437265Sahrens 			if (intval < version) {
620e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
621e7437265Sahrens 				    "Can not downgrade; already at version %u"),
622e7437265Sahrens 				    version);
623e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
624e7437265Sahrens 				goto error;
625e7437265Sahrens 			}
626e7437265Sahrens 			break;
627e7437265Sahrens 		}
628e7437265Sahrens 
629e9dbad6fSeschrock 		case ZFS_PROP_RECORDSIZE:
630e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
631e9dbad6fSeschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
632e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
633e9dbad6fSeschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
634e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
635e9dbad6fSeschrock 				    "'%s' must be power of 2 from %u "
636e9dbad6fSeschrock 				    "to %uk"), propname,
637e9dbad6fSeschrock 				    (uint_t)SPA_MINBLOCKSIZE,
638e9dbad6fSeschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
639e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
640e9dbad6fSeschrock 				goto error;
641e9dbad6fSeschrock 			}
642e9dbad6fSeschrock 			break;
643e9dbad6fSeschrock 
644f3861e1aSahl 		case ZFS_PROP_SHAREISCSI:
645f3861e1aSahl 			if (strcmp(strval, "off") != 0 &&
646f3861e1aSahl 			    strcmp(strval, "on") != 0 &&
647f3861e1aSahl 			    strcmp(strval, "type=disk") != 0) {
648f3861e1aSahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
649f3861e1aSahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
650f3861e1aSahl 				    propname);
651f3861e1aSahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
652f3861e1aSahl 				goto error;
653f3861e1aSahl 			}
654f3861e1aSahl 
655f3861e1aSahl 			break;
656f3861e1aSahl 
657e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
65889eef05eSrm160521 		{
65989eef05eSrm160521 			namecheck_err_t why;
66089eef05eSrm160521 
661e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
662e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
663e9dbad6fSeschrock 				break;
664e9dbad6fSeschrock 
66589eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
66689eef05eSrm160521 				switch (why) {
66789eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
66889eef05eSrm160521 					zfs_error_aux(hdl,
66989eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
670e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
671e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
67289eef05eSrm160521 					break;
67389eef05eSrm160521 				case NAME_ERR_TOOLONG:
67489eef05eSrm160521 					zfs_error_aux(hdl,
67589eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
67689eef05eSrm160521 					    "component of '%s' is too long"),
67789eef05eSrm160521 					    propname);
67889eef05eSrm160521 					break;
67989eef05eSrm160521 				}
680e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
681e9dbad6fSeschrock 				goto error;
682e9dbad6fSeschrock 			}
68389eef05eSrm160521 		}
68489eef05eSrm160521 
685f3861e1aSahl 			/*FALLTHRU*/
686e9dbad6fSeschrock 
687da6c28aaSamw 		case ZFS_PROP_SHARESMB:
688f3861e1aSahl 		case ZFS_PROP_SHARENFS:
689e9dbad6fSeschrock 			/*
690da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
691da6c28aaSamw 			 * properties, check if it can be set in a
692da6c28aaSamw 			 * global/non-global zone based on
693f3861e1aSahl 			 * the zoned property value:
694fa9e4066Sahrens 			 *
695fa9e4066Sahrens 			 *		global zone	    non-global zone
696f3861e1aSahl 			 * --------------------------------------------------
697fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
698fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
699da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
700fa9e4066Sahrens 			 *
701fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
702fa9e4066Sahrens 			 *		sharenfs (yes)
703da6c28aaSamw 			 *		sharesmb (yes)
704fa9e4066Sahrens 			 */
705e9dbad6fSeschrock 			if (zoned) {
706fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
70799653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
708e9dbad6fSeschrock 					    "'%s' cannot be set on "
709e9dbad6fSeschrock 					    "dataset in a non-global zone"),
710e9dbad6fSeschrock 					    propname);
711e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
712e9dbad6fSeschrock 					    errbuf);
713e9dbad6fSeschrock 					goto error;
714da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
715da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
71699653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
717e9dbad6fSeschrock 					    "'%s' cannot be set in "
718e9dbad6fSeschrock 					    "a non-global zone"), propname);
719e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
720e9dbad6fSeschrock 					    errbuf);
721e9dbad6fSeschrock 					goto error;
722fa9e4066Sahrens 				}
723fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
724fa9e4066Sahrens 				/*
725fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
726fa9e4066Sahrens 				 * a globle zone. If not, something is wrong.
727fa9e4066Sahrens 				 */
72899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
729e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
730e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
731e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
732e9dbad6fSeschrock 				goto error;
733fa9e4066Sahrens 			}
734f3861e1aSahl 
73567331909Sdougm 			/*
73667331909Sdougm 			 * At this point, it is legitimate to set the
73767331909Sdougm 			 * property. Now we want to make sure that the
73867331909Sdougm 			 * property value is valid if it is sharenfs.
73967331909Sdougm 			 */
740da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
741da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
74267331909Sdougm 			    strcmp(strval, "on") != 0 &&
74367331909Sdougm 			    strcmp(strval, "off") != 0) {
744da6c28aaSamw 				zfs_share_proto_t proto;
745da6c28aaSamw 
746da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
747da6c28aaSamw 					proto = PROTO_SMB;
748da6c28aaSamw 				else
749da6c28aaSamw 					proto = PROTO_NFS;
75067331909Sdougm 
75167331909Sdougm 				/*
752da6c28aaSamw 				 * Must be an valid sharing protocol
753da6c28aaSamw 				 * option string so init the libshare
754da6c28aaSamw 				 * in order to enable the parser and
755da6c28aaSamw 				 * then parse the options. We use the
756da6c28aaSamw 				 * control API since we don't care about
757da6c28aaSamw 				 * the current configuration and don't
75867331909Sdougm 				 * want the overhead of loading it
75967331909Sdougm 				 * until we actually do something.
76067331909Sdougm 				 */
76167331909Sdougm 
76267331909Sdougm 				if (zfs_init_libshare(hdl,
76367331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
764fac3008cSeschrock 					/*
765fac3008cSeschrock 					 * An error occurred so we can't do
766fac3008cSeschrock 					 * anything
767fac3008cSeschrock 					 */
76867331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76967331909Sdougm 					    "'%s' cannot be set: problem "
77067331909Sdougm 					    "in share initialization"),
77167331909Sdougm 					    propname);
772fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
773fac3008cSeschrock 					    errbuf);
77467331909Sdougm 					goto error;
77567331909Sdougm 				}
77667331909Sdougm 
777da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
77867331909Sdougm 					/*
77967331909Sdougm 					 * There was an error in parsing so
78067331909Sdougm 					 * deal with it by issuing an error
78167331909Sdougm 					 * message and leaving after
78267331909Sdougm 					 * uninitializing the the libshare
78367331909Sdougm 					 * interface.
78467331909Sdougm 					 */
78567331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
78667331909Sdougm 					    "'%s' cannot be set to invalid "
78767331909Sdougm 					    "options"), propname);
788fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
789fac3008cSeschrock 					    errbuf);
79067331909Sdougm 					zfs_uninit_libshare(hdl);
79167331909Sdougm 					goto error;
79267331909Sdougm 				}
79367331909Sdougm 				zfs_uninit_libshare(hdl);
79467331909Sdougm 			}
79567331909Sdougm 
796f3861e1aSahl 			break;
797da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
798da6c28aaSamw 			chosen_utf = (int)intval;
799da6c28aaSamw 			break;
800da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
801da6c28aaSamw 			chosen_normal = (int)intval;
802da6c28aaSamw 			break;
803fa9e4066Sahrens 		}
804fa9e4066Sahrens 
805e9dbad6fSeschrock 		/*
806e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
807e9dbad6fSeschrock 		 * checks to enforce.
808e9dbad6fSeschrock 		 */
809e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
810e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
811e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
812e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
813e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
814e9dbad6fSeschrock 			char buf[64];
815e9dbad6fSeschrock 
816e9dbad6fSeschrock 			switch (prop) {
817e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
818a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
819e9dbad6fSeschrock 				if (intval > volsize) {
820e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
821e9dbad6fSeschrock 					    "'%s' is greater than current "
822e9dbad6fSeschrock 					    "volume size"), propname);
823e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
824e9dbad6fSeschrock 					    errbuf);
825e9dbad6fSeschrock 					goto error;
826e9dbad6fSeschrock 				}
827e9dbad6fSeschrock 				break;
828e9dbad6fSeschrock 
829e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
830e9dbad6fSeschrock 				if (intval % blocksize != 0) {
831e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
832e9dbad6fSeschrock 					    sizeof (buf));
833e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
834e9dbad6fSeschrock 					    "'%s' must be a multiple of "
835e9dbad6fSeschrock 					    "volume block size (%s)"),
836e9dbad6fSeschrock 					    propname, buf);
837e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
838e9dbad6fSeschrock 					    errbuf);
839e9dbad6fSeschrock 					goto error;
840e9dbad6fSeschrock 				}
841e9dbad6fSeschrock 
842e9dbad6fSeschrock 				if (intval == 0) {
843e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
844e9dbad6fSeschrock 					    "'%s' cannot be zero"),
845e9dbad6fSeschrock 					    propname);
846e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
847e9dbad6fSeschrock 					    errbuf);
848e9dbad6fSeschrock 					goto error;
849e9dbad6fSeschrock 				}
850f3861e1aSahl 				break;
851e9dbad6fSeschrock 			}
852e9dbad6fSeschrock 		}
853e9dbad6fSeschrock 	}
854e9dbad6fSeschrock 
855e9dbad6fSeschrock 	/*
856da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
857da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
858da6c28aaSamw 	 *
859da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
860da6c28aaSamw 	 * was explicitly not chosen, it is an error.
861da6c28aaSamw 	 */
862de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
863da6c28aaSamw 		if (nvlist_add_uint64(ret,
864da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
865da6c28aaSamw 			(void) no_memory(hdl);
866da6c28aaSamw 			goto error;
867da6c28aaSamw 		}
868de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
869da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
870da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
871da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
872da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
873da6c28aaSamw 		goto error;
874da6c28aaSamw 	}
875da6c28aaSamw 
876da6c28aaSamw 	/*
877e9dbad6fSeschrock 	 * If this is an existing volume, and someone is setting the volsize,
878e9dbad6fSeschrock 	 * make sure that it matches the reservation, or add it if necessary.
879e9dbad6fSeschrock 	 */
880e9dbad6fSeschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
881e9dbad6fSeschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
882e9dbad6fSeschrock 	    &intval) == 0) {
883e9dbad6fSeschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
884e9dbad6fSeschrock 		    ZFS_PROP_VOLSIZE);
885a9b821a0Sck153898 		uint64_t old_reservation;
886e9dbad6fSeschrock 		uint64_t new_reservation;
887a9b821a0Sck153898 		zfs_prop_t resv_prop;
888a9b821a0Sck153898 
8897b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
890a9b821a0Sck153898 			goto error;
891a9b821a0Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
892e9dbad6fSeschrock 
893e9dbad6fSeschrock 		if (old_volsize == old_reservation &&
894a9b821a0Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
895e9dbad6fSeschrock 		    &new_reservation) != 0) {
896e9dbad6fSeschrock 			if (nvlist_add_uint64(ret,
897a9b821a0Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
898e9dbad6fSeschrock 				(void) no_memory(hdl);
899e9dbad6fSeschrock 				goto error;
900e9dbad6fSeschrock 			}
901e9dbad6fSeschrock 		}
902e9dbad6fSeschrock 	}
903e9dbad6fSeschrock 	return (ret);
904e9dbad6fSeschrock 
905e9dbad6fSeschrock error:
906e9dbad6fSeschrock 	nvlist_free(ret);
907e9dbad6fSeschrock 	return (NULL);
908e9dbad6fSeschrock }
909e9dbad6fSeschrock 
910ecd6cf80Smarks static int
911ecd6cf80Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
912ecd6cf80Smarks     uint64_t *ret_who)
913ecd6cf80Smarks {
914ecd6cf80Smarks 	struct passwd *pwd;
915ecd6cf80Smarks 	struct group *grp;
916ecd6cf80Smarks 	uid_t id;
917ecd6cf80Smarks 
918ecd6cf80Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
919ecd6cf80Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
920ecd6cf80Smarks 		*ret_who = -1;
921ecd6cf80Smarks 		return (0);
922ecd6cf80Smarks 	}
923ecd6cf80Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
924ecd6cf80Smarks 		return (EZFS_BADWHO);
925ecd6cf80Smarks 
926ecd6cf80Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
927ecd6cf80Smarks 	    strcmp(who, "everyone") == 0) {
928ecd6cf80Smarks 		*ret_who = -1;
929ecd6cf80Smarks 		*who_type = ZFS_DELEG_EVERYONE;
930ecd6cf80Smarks 		return (0);
931ecd6cf80Smarks 	}
932ecd6cf80Smarks 
933ecd6cf80Smarks 	pwd = getpwnam(who);
934ecd6cf80Smarks 	grp = getgrnam(who);
935ecd6cf80Smarks 
936ecd6cf80Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
937ecd6cf80Smarks 		*ret_who = pwd->pw_uid;
938ecd6cf80Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
939ecd6cf80Smarks 		*ret_who = grp->gr_gid;
940ecd6cf80Smarks 	} else if (pwd) {
941ecd6cf80Smarks 		*ret_who = pwd->pw_uid;
942ecd6cf80Smarks 		*who_type = ZFS_DELEG_USER;
943ecd6cf80Smarks 	} else if (grp) {
944ecd6cf80Smarks 		*ret_who = grp->gr_gid;
945ecd6cf80Smarks 		*who_type = ZFS_DELEG_GROUP;
946ecd6cf80Smarks 	} else {
947ecd6cf80Smarks 		char *end;
948ecd6cf80Smarks 
949ecd6cf80Smarks 		id = strtol(who, &end, 10);
950ecd6cf80Smarks 		if (errno != 0 || *end != '\0') {
951ecd6cf80Smarks 			return (EZFS_BADWHO);
952ecd6cf80Smarks 		} else {
953ecd6cf80Smarks 			*ret_who = id;
954ecd6cf80Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
955ecd6cf80Smarks 				*who_type = ZFS_DELEG_USER;
956ecd6cf80Smarks 		}
957ecd6cf80Smarks 	}
958ecd6cf80Smarks 
959ecd6cf80Smarks 	return (0);
960ecd6cf80Smarks }
961ecd6cf80Smarks 
962ecd6cf80Smarks static void
963ecd6cf80Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
964ecd6cf80Smarks {
965ecd6cf80Smarks 	if (perms_nvp != NULL) {
966ecd6cf80Smarks 		verify(nvlist_add_nvlist(who_nvp,
967ecd6cf80Smarks 		    name, perms_nvp) == 0);
968ecd6cf80Smarks 	} else {
969ecd6cf80Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
970ecd6cf80Smarks 	}
971ecd6cf80Smarks }
972ecd6cf80Smarks 
973ecd6cf80Smarks static void
974ecd6cf80Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
975ecd6cf80Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
976ecd6cf80Smarks     nvlist_t *sets_nvp)
977ecd6cf80Smarks {
978ecd6cf80Smarks 	boolean_t do_perms, do_sets;
979ecd6cf80Smarks 	char name[ZFS_MAX_DELEG_NAME];
980ecd6cf80Smarks 
981ecd6cf80Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
982ecd6cf80Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
983ecd6cf80Smarks 
984ecd6cf80Smarks 	if (!do_perms && !do_sets)
985ecd6cf80Smarks 		do_perms = do_sets = B_TRUE;
986ecd6cf80Smarks 
987ecd6cf80Smarks 	if (do_perms) {
988ecd6cf80Smarks 		zfs_deleg_whokey(name, who_type, inherit,
989ecd6cf80Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
990ecd6cf80Smarks 		    whostr : (void *)&whoid);
991ecd6cf80Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
992ecd6cf80Smarks 	}
993ecd6cf80Smarks 	if (do_sets) {
994ecd6cf80Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
995ecd6cf80Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
996ecd6cf80Smarks 		    whostr : (void *)&whoid);
997ecd6cf80Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
998ecd6cf80Smarks 	}
999ecd6cf80Smarks }
1000ecd6cf80Smarks 
1001ecd6cf80Smarks static void
1002ecd6cf80Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
1003ecd6cf80Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
1004ecd6cf80Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
1005ecd6cf80Smarks {
1006ecd6cf80Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
1007ecd6cf80Smarks 		helper(who_type, whoid, whostr, 0,
1008ecd6cf80Smarks 		    who_nvp, perms_nvp, sets_nvp);
1009ecd6cf80Smarks 	} else {
1010ecd6cf80Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
1011ecd6cf80Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
1012ecd6cf80Smarks 			    who_nvp, perms_nvp, sets_nvp);
1013ecd6cf80Smarks 		}
1014ecd6cf80Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
1015ecd6cf80Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
1016ecd6cf80Smarks 			    who_nvp, perms_nvp, sets_nvp);
1017ecd6cf80Smarks 		}
1018ecd6cf80Smarks 	}
1019ecd6cf80Smarks }
1020ecd6cf80Smarks 
1021ecd6cf80Smarks /*
1022ecd6cf80Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
1023ecd6cf80Smarks  *
1024ecd6cf80Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
1025ecd6cf80Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
1026ecd6cf80Smarks  * base attribute named stored in the dsl.
1027ecd6cf80Smarks  * Arguments:
1028ecd6cf80Smarks  *
1029ecd6cf80Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
1030ecd6cf80Smarks  *           whostr may be null for everyone or create perms.
1031ecd6cf80Smarks  * who_type: is the type of entry in whostr.  Typically this will be
1032ecd6cf80Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
1033da6c28aaSamw  * perms:    common separated list of permissions.  May be null if user
1034ecd6cf80Smarks  *           is requested to remove permissions by who.
1035ecd6cf80Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
1036ecd6cf80Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
1037ecd6cf80Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
1038ecd6cf80Smarks  *           The output nvp will look something like this.
1039ecd6cf80Smarks  *              ul$1234 -> {create ; destroy }
1040ecd6cf80Smarks  *              Ul$1234 -> { @myset }
1041ecd6cf80Smarks  *              s-$@myset - { snapshot; checksum; compression }
1042ecd6cf80Smarks  */
1043ecd6cf80Smarks int
1044ecd6cf80Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
1045ecd6cf80Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
1046ecd6cf80Smarks {
1047ecd6cf80Smarks 	nvlist_t *who_nvp;
1048ecd6cf80Smarks 	nvlist_t *perms_nvp = NULL;
1049ecd6cf80Smarks 	nvlist_t *sets_nvp = NULL;
1050ecd6cf80Smarks 	char errbuf[1024];
105191ebeef5Sahrens 	char *who_tok, *perm;
1052ecd6cf80Smarks 	int error;
1053ecd6cf80Smarks 
1054ecd6cf80Smarks 	*nvp = NULL;
1055ecd6cf80Smarks 
1056ecd6cf80Smarks 	if (perms) {
1057ecd6cf80Smarks 		if ((error = nvlist_alloc(&perms_nvp,
1058ecd6cf80Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
1059ecd6cf80Smarks 			return (1);
1060ecd6cf80Smarks 		}
1061ecd6cf80Smarks 		if ((error = nvlist_alloc(&sets_nvp,
1062ecd6cf80Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
1063ecd6cf80Smarks 			nvlist_free(perms_nvp);
1064ecd6cf80Smarks 			return (1);
1065ecd6cf80Smarks 		}
1066ecd6cf80Smarks 	}
1067ecd6cf80Smarks 
1068ecd6cf80Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
1069ecd6cf80Smarks 		if (perms_nvp)
1070ecd6cf80Smarks 			nvlist_free(perms_nvp);
1071ecd6cf80Smarks 		if (sets_nvp)
1072ecd6cf80Smarks 			nvlist_free(sets_nvp);
1073ecd6cf80Smarks 		return (1);
1074ecd6cf80Smarks 	}
1075ecd6cf80Smarks 
1076ecd6cf80Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
1077ecd6cf80Smarks 		namecheck_err_t why;
1078ecd6cf80Smarks 		char what;
1079ecd6cf80Smarks 
1080ecd6cf80Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
108191ebeef5Sahrens 			nvlist_free(who_nvp);
108291ebeef5Sahrens 			if (perms_nvp)
108391ebeef5Sahrens 				nvlist_free(perms_nvp);
108491ebeef5Sahrens 			if (sets_nvp)
108591ebeef5Sahrens 				nvlist_free(sets_nvp);
108691ebeef5Sahrens 
1087ecd6cf80Smarks 			switch (why) {
1088ecd6cf80Smarks 			case NAME_ERR_NO_AT:
1089ecd6cf80Smarks 				zfs_error_aux(zhp->zfs_hdl,
1090ecd6cf80Smarks 				    dgettext(TEXT_DOMAIN,
1091ecd6cf80Smarks 				    "set definition must begin with an '@' "
1092ecd6cf80Smarks 				    "character"));
1093ecd6cf80Smarks 			}
1094ecd6cf80Smarks 			return (zfs_error(zhp->zfs_hdl,
1095ecd6cf80Smarks 			    EZFS_BADPERMSET, whostr));
1096ecd6cf80Smarks 		}
1097ecd6cf80Smarks 	}
1098ecd6cf80Smarks 
1099ecd6cf80Smarks 	/*
1100ecd6cf80Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
1101ecd6cf80Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
1102ecd6cf80Smarks 	 * other sets_nvp will have only permssion set names in it.
1103ecd6cf80Smarks 	 */
110491ebeef5Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
110591ebeef5Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
1106ecd6cf80Smarks 
110791ebeef5Sahrens 		if (perm_canonical) {
110891ebeef5Sahrens 			verify(nvlist_add_boolean(perms_nvp,
110991ebeef5Sahrens 			    perm_canonical) == 0);
111091ebeef5Sahrens 		} else if (perm[0] == '@') {
111191ebeef5Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
1112ecd6cf80Smarks 		} else {
1113ecd6cf80Smarks 			nvlist_free(who_nvp);
1114ecd6cf80Smarks 			nvlist_free(perms_nvp);
1115ecd6cf80Smarks 			nvlist_free(sets_nvp);
111691ebeef5Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
1117ecd6cf80Smarks 		}
1118ecd6cf80Smarks 	}
1119ecd6cf80Smarks 
1120ecd6cf80Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
1121ecd6cf80Smarks 		who_tok = strtok(whostr, ",");
1122ecd6cf80Smarks 		if (who_tok == NULL) {
1123ecd6cf80Smarks 			nvlist_free(who_nvp);
112491ebeef5Sahrens 			if (perms_nvp)
1125ecd6cf80Smarks 				nvlist_free(perms_nvp);
1126ecd6cf80Smarks 			if (sets_nvp)
1127ecd6cf80Smarks 				nvlist_free(sets_nvp);
1128ecd6cf80Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
1129ecd6cf80Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
1130ecd6cf80Smarks 			    whostr);
1131ecd6cf80Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
1132ecd6cf80Smarks 		}
1133ecd6cf80Smarks 	}
1134ecd6cf80Smarks 
1135ecd6cf80Smarks 	/*
1136ecd6cf80Smarks 	 * Now create the nvlist(s)
1137ecd6cf80Smarks 	 */
1138ecd6cf80Smarks 	do {
1139ecd6cf80Smarks 		uint64_t who_id;
1140ecd6cf80Smarks 
1141ecd6cf80Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
1142ecd6cf80Smarks 		    &who_id);
1143ecd6cf80Smarks 		if (error) {
1144ecd6cf80Smarks 			nvlist_free(who_nvp);
114591ebeef5Sahrens 			if (perms_nvp)
1146ecd6cf80Smarks 				nvlist_free(perms_nvp);
1147ecd6cf80Smarks 			if (sets_nvp)
1148ecd6cf80Smarks 				nvlist_free(sets_nvp);
1149ecd6cf80Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
1150ecd6cf80Smarks 			    dgettext(TEXT_DOMAIN,
1151ecd6cf80Smarks 			    "Unable to determine uid/gid for "
1152ecd6cf80Smarks 			    "%s "), who_tok);
1153ecd6cf80Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
1154ecd6cf80Smarks 		}
1155ecd6cf80Smarks 
1156ecd6cf80Smarks 		/*
1157ecd6cf80Smarks 		 * add entries for both local and descendent when required
1158ecd6cf80Smarks 		 */
1159ecd6cf80Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
1160ecd6cf80Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
1161ecd6cf80Smarks 
1162ecd6cf80Smarks 	} while (who_tok = strtok(NULL, ","));
1163ecd6cf80Smarks 	*nvp = who_nvp;
1164ecd6cf80Smarks 	return (0);
1165ecd6cf80Smarks }
1166ecd6cf80Smarks 
1167ecd6cf80Smarks static int
1168ecd6cf80Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
1169ecd6cf80Smarks {
1170ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
1171ecd6cf80Smarks 	int error;
1172ecd6cf80Smarks 	char errbuf[1024];
1173ecd6cf80Smarks 
1174ecd6cf80Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
1175ecd6cf80Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
1176ecd6cf80Smarks 	    zhp->zfs_name);
1177ecd6cf80Smarks 
1178990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
1179ecd6cf80Smarks 		return (-1);
1180ecd6cf80Smarks 
1181ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1182ecd6cf80Smarks 	zc.zc_perm_action = unset;
1183ecd6cf80Smarks 
1184ecd6cf80Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
1185ecd6cf80Smarks 	if (error && errno == ENOTSUP) {
1186ecd6cf80Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
1187ecd6cf80Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
1188ecd6cf80Smarks 		zcmd_free_nvlists(&zc);
1189ecd6cf80Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
1190ecd6cf80Smarks 	} else if (error) {
1191ecd6cf80Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
1192ecd6cf80Smarks 	}
1193ecd6cf80Smarks 	zcmd_free_nvlists(&zc);
1194ecd6cf80Smarks 
1195ecd6cf80Smarks 	return (error);
1196ecd6cf80Smarks }
1197ecd6cf80Smarks 
1198ecd6cf80Smarks int
1199ecd6cf80Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
1200ecd6cf80Smarks {
1201ecd6cf80Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
1202ecd6cf80Smarks }
1203ecd6cf80Smarks 
1204ecd6cf80Smarks int
1205ecd6cf80Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
1206ecd6cf80Smarks {
1207ecd6cf80Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
1208ecd6cf80Smarks }
1209ecd6cf80Smarks 
1210ecd6cf80Smarks static int
1211ecd6cf80Smarks perm_compare(const void *arg1, const void *arg2)
1212ecd6cf80Smarks {
1213ecd6cf80Smarks 	const zfs_perm_node_t *node1 = arg1;
1214ecd6cf80Smarks 	const zfs_perm_node_t *node2 = arg2;
1215ecd6cf80Smarks 	int ret;
1216ecd6cf80Smarks 
1217ecd6cf80Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
1218ecd6cf80Smarks 
1219ecd6cf80Smarks 	if (ret > 0)
1220ecd6cf80Smarks 		return (1);
1221ecd6cf80Smarks 	if (ret < 0)
1222ecd6cf80Smarks 		return (-1);
1223ecd6cf80Smarks 	else
1224ecd6cf80Smarks 		return (0);
1225ecd6cf80Smarks }
1226ecd6cf80Smarks 
1227ecd6cf80Smarks static void
1228ecd6cf80Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
1229ecd6cf80Smarks {
1230ecd6cf80Smarks 	zfs_perm_node_t *permnode;
12313cb34c60Sahrens 	void *cookie = NULL;
1232ecd6cf80Smarks 
12333cb34c60Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
1234ecd6cf80Smarks 		free(permnode);
12353cb34c60Sahrens 	avl_destroy(tree);
1236ecd6cf80Smarks }
1237ecd6cf80Smarks 
1238ecd6cf80Smarks static void
1239ecd6cf80Smarks zfs_destroy_tree(avl_tree_t *tree)
1240ecd6cf80Smarks {
1241ecd6cf80Smarks 	zfs_allow_node_t *allownode;
12423cb34c60Sahrens 	void *cookie = NULL;
1243ecd6cf80Smarks 
1244ecd6cf80Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
1245ecd6cf80Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
1246ecd6cf80Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
1247ecd6cf80Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
1248ecd6cf80Smarks 		free(allownode);
1249ecd6cf80Smarks 	}
12503cb34c60Sahrens 	avl_destroy(tree);
1251ecd6cf80Smarks }
1252ecd6cf80Smarks 
1253ecd6cf80Smarks void
1254ecd6cf80Smarks zfs_free_allows(zfs_allow_t *allow)
1255ecd6cf80Smarks {
1256ecd6cf80Smarks 	zfs_allow_t *allownext;
1257ecd6cf80Smarks 	zfs_allow_t *freeallow;
1258ecd6cf80Smarks 
1259ecd6cf80Smarks 	allownext = allow;
1260ecd6cf80Smarks 	while (allownext) {
1261ecd6cf80Smarks 		zfs_destroy_tree(&allownext->z_sets);
1262ecd6cf80Smarks 		zfs_destroy_tree(&allownext->z_crperms);
1263ecd6cf80Smarks 		zfs_destroy_tree(&allownext->z_user);
1264ecd6cf80Smarks 		zfs_destroy_tree(&allownext->z_group);
1265ecd6cf80Smarks 		zfs_destroy_tree(&allownext->z_everyone);
1266ecd6cf80Smarks 		freeallow = allownext;
1267ecd6cf80Smarks 		allownext = allownext->z_next;
1268ecd6cf80Smarks 		free(freeallow);
1269ecd6cf80Smarks 	}
1270ecd6cf80Smarks }
1271ecd6cf80Smarks 
1272ecd6cf80Smarks static zfs_allow_t *
1273ecd6cf80Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
1274ecd6cf80Smarks {
1275ecd6cf80Smarks 	zfs_allow_t *ptree;
1276ecd6cf80Smarks 
1277ecd6cf80Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
1278ecd6cf80Smarks 	    sizeof (zfs_allow_t))) == NULL) {
1279ecd6cf80Smarks 		return (NULL);
1280ecd6cf80Smarks 	}
1281ecd6cf80Smarks 
1282ecd6cf80Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
1283ecd6cf80Smarks 	avl_create(&ptree->z_sets,
1284ecd6cf80Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
1285ecd6cf80Smarks 	    offsetof(zfs_allow_node_t, z_node));
1286ecd6cf80Smarks 	avl_create(&ptree->z_crperms,
1287ecd6cf80Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
1288ecd6cf80Smarks 	    offsetof(zfs_allow_node_t, z_node));
1289ecd6cf80Smarks 	avl_create(&ptree->z_user,
1290ecd6cf80Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
1291ecd6cf80Smarks 	    offsetof(zfs_allow_node_t, z_node));
1292ecd6cf80Smarks 	avl_create(&ptree->z_group,
1293ecd6cf80Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
1294ecd6cf80Smarks 	    offsetof(zfs_allow_node_t, z_node));
1295ecd6cf80Smarks 	avl_create(&ptree->z_everyone,
1296ecd6cf80Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
1297ecd6cf80Smarks 	    offsetof(zfs_allow_node_t, z_node));
1298ecd6cf80Smarks 
1299ecd6cf80Smarks 	if (prev)
1300ecd6cf80Smarks 		prev->z_next = ptree;
1301ecd6cf80Smarks 	ptree->z_next = NULL;
1302ecd6cf80Smarks 	return (ptree);
1303ecd6cf80Smarks }
1304ecd6cf80Smarks 
1305ecd6cf80Smarks /*
1306ecd6cf80Smarks  * Add permissions to the appropriate AVL permission tree.
1307ecd6cf80Smarks  * The appropriate tree may not be the requested tree.
1308ecd6cf80Smarks  * For example if ld indicates a local permission, but
1309ecd6cf80Smarks  * same permission also exists as a descendent permission
1310ecd6cf80Smarks  * then the permission will be removed from the descendent
1311ecd6cf80Smarks  * tree and add the the local+descendent tree.
1312ecd6cf80Smarks  */
1313ecd6cf80Smarks static int
1314ecd6cf80Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
1315ecd6cf80Smarks     char *perm, char ld)
1316ecd6cf80Smarks {
1317ecd6cf80Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
1318ecd6cf80Smarks 	zfs_perm_node_t *newnode;
1319ecd6cf80Smarks 	avl_index_t where, where2;
1320ecd6cf80Smarks 	avl_tree_t *tree, *altree;
1321ecd6cf80Smarks 
1322ecd6cf80Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
1323ecd6cf80Smarks 
1324ecd6cf80Smarks 	if (ld == ZFS_DELEG_NA) {
1325ecd6cf80Smarks 		tree =  &allownode->z_localdescend;
1326ecd6cf80Smarks 		altree = &allownode->z_descend;
1327ecd6cf80Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
1328ecd6cf80Smarks 		tree = &allownode->z_local;
1329ecd6cf80Smarks 		altree = &allownode->z_descend;
1330ecd6cf80Smarks 	} else {
1331ecd6cf80Smarks 		tree = &allownode->z_descend;
1332ecd6cf80Smarks 		altree = &allownode->z_local;
1333ecd6cf80Smarks 	}
1334ecd6cf80Smarks 	permnode = avl_find(tree, &pnode, &where);
1335ecd6cf80Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
1336ecd6cf80Smarks 
1337ecd6cf80Smarks 	if (permnode2) {
1338ecd6cf80Smarks 		avl_remove(altree, permnode2);
1339ecd6cf80Smarks 		free(permnode2);
1340ecd6cf80Smarks 		if (permnode == NULL) {
1341ecd6cf80Smarks 			tree =  &allownode->z_localdescend;
1342ecd6cf80Smarks 		}
1343ecd6cf80Smarks 	}
1344ecd6cf80Smarks 
1345ecd6cf80Smarks 	/*
1346ecd6cf80Smarks 	 * Now insert new permission in either requested location
1347ecd6cf80Smarks 	 * local/descendent or into ld when perm will exist in both.
1348ecd6cf80Smarks 	 */
1349ecd6cf80Smarks 	if (permnode == NULL) {
1350ecd6cf80Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
1351ecd6cf80Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
1352ecd6cf80Smarks 			return (-1);
1353ecd6cf80Smarks 		}
1354ecd6cf80Smarks 		*newnode = pnode;
1355ecd6cf80Smarks 		avl_add(tree, newnode);
1356ecd6cf80Smarks 	}
1357ecd6cf80Smarks 	return (0);
1358ecd6cf80Smarks }
1359e7437265Sahrens 
1360ecd6cf80Smarks /*
1361ecd6cf80Smarks  * Uggh, this is going to be a bit complicated.
1362ecd6cf80Smarks  * we have an nvlist coming out of the kernel that
1363ecd6cf80Smarks  * will indicate where the permission is set and then
1364ecd6cf80Smarks  * it will contain allow of the various "who's", and what
1365ecd6cf80Smarks  * their permissions are.  To further complicate this
1366ecd6cf80Smarks  * we will then have to coalesce the local,descendent
1367ecd6cf80Smarks  * and local+descendent permissions where appropriate.
1368ecd6cf80Smarks  * The kernel only knows about a permission as being local
1369ecd6cf80Smarks  * or descendent, but not both.
1370ecd6cf80Smarks  *
1371ecd6cf80Smarks  * In order to make this easier for zfs_main to deal with
1372ecd6cf80Smarks  * a series of AVL trees will be used to maintain
1373ecd6cf80Smarks  * all of this, primarily for sorting purposes as well
1374ecd6cf80Smarks  * as the ability to quickly locate a specific entry.
1375ecd6cf80Smarks  *
1376ecd6cf80Smarks  * What we end up with are tree's for sets, create perms,
1377ecd6cf80Smarks  * user, groups and everyone.  With each of those trees
1378ecd6cf80Smarks  * we have subtrees for local, descendent and local+descendent
1379ecd6cf80Smarks  * permissions.
1380ecd6cf80Smarks  */
1381ecd6cf80Smarks int
1382ecd6cf80Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
1383ecd6cf80Smarks {
1384ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
1385ecd6cf80Smarks 	int error;
1386ecd6cf80Smarks 	nvlist_t *nvlist;
1387ecd6cf80Smarks 	nvlist_t *permnv, *sourcenv;
1388ecd6cf80Smarks 	nvpair_t *who_pair, *source_pair;
1389ecd6cf80Smarks 	nvpair_t *perm_pair;
1390ecd6cf80Smarks 	char errbuf[1024];
1391ecd6cf80Smarks 	zfs_allow_t *zallowp, *newallowp;
1392ecd6cf80Smarks 	char  ld;
1393ecd6cf80Smarks 	char *nvpname;
1394ecd6cf80Smarks 	uid_t	uid;
1395ecd6cf80Smarks 	gid_t	gid;
1396ecd6cf80Smarks 	avl_tree_t *tree;
1397ecd6cf80Smarks 	avl_index_t where;
1398ecd6cf80Smarks 
1399ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1400ecd6cf80Smarks 
1401ecd6cf80Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1402ecd6cf80Smarks 		return (-1);
1403ecd6cf80Smarks 
1404ecd6cf80Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
1405ecd6cf80Smarks 		if (errno == ENOMEM) {
1406ecd6cf80Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
1407ecd6cf80Smarks 				zcmd_free_nvlists(&zc);
1408ecd6cf80Smarks 				return (-1);
1409ecd6cf80Smarks 			}
1410ecd6cf80Smarks 		} else if (errno == ENOTSUP) {
1411ecd6cf80Smarks 			zcmd_free_nvlists(&zc);
1412ecd6cf80Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
1413ecd6cf80Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
1414ecd6cf80Smarks 			return (zfs_error(zhp->zfs_hdl,
1415ecd6cf80Smarks 			    EZFS_BADVERSION, errbuf));
1416ecd6cf80Smarks 		} else {
1417ecd6cf80Smarks 			zcmd_free_nvlists(&zc);
1418ecd6cf80Smarks 			return (-1);
1419ecd6cf80Smarks 		}
1420ecd6cf80Smarks 	}
1421ecd6cf80Smarks 
1422ecd6cf80Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
1423ecd6cf80Smarks 		zcmd_free_nvlists(&zc);
1424ecd6cf80Smarks 		return (-1);
1425ecd6cf80Smarks 	}
1426ecd6cf80Smarks 
1427ecd6cf80Smarks 	zcmd_free_nvlists(&zc);
1428ecd6cf80Smarks 
1429ecd6cf80Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
1430ecd6cf80Smarks 
1431ecd6cf80Smarks 	if (source_pair == NULL) {
1432ecd6cf80Smarks 		*zfs_perms = NULL;
1433ecd6cf80Smarks 		return (0);
1434ecd6cf80Smarks 	}
1435ecd6cf80Smarks 
1436ecd6cf80Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
1437ecd6cf80Smarks 	if (*zfs_perms == NULL) {
1438ecd6cf80Smarks 		return (0);
1439ecd6cf80Smarks 	}
1440ecd6cf80Smarks 
1441ecd6cf80Smarks 	zallowp = *zfs_perms;
1442ecd6cf80Smarks 
1443ecd6cf80Smarks 	for (;;) {
1444ecd6cf80Smarks 		struct passwd *pwd;
1445ecd6cf80Smarks 		struct group *grp;
1446ecd6cf80Smarks 		zfs_allow_node_t *allownode;
1447ecd6cf80Smarks 		zfs_allow_node_t  findallownode;
1448ecd6cf80Smarks 		zfs_allow_node_t *newallownode;
1449ecd6cf80Smarks 
1450ecd6cf80Smarks 		(void) strlcpy(zallowp->z_setpoint,
1451ecd6cf80Smarks 		    nvpair_name(source_pair),
1452ecd6cf80Smarks 		    sizeof (zallowp->z_setpoint));
1453ecd6cf80Smarks 
1454ecd6cf80Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
1455ecd6cf80Smarks 			goto abort;
1456ecd6cf80Smarks 
1457ecd6cf80Smarks 		/*
1458ecd6cf80Smarks 		 * Make sure nvlist is composed correctly
1459ecd6cf80Smarks 		 */
1460ecd6cf80Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
1461ecd6cf80Smarks 			goto abort;
1462ecd6cf80Smarks 		}
1463ecd6cf80Smarks 
1464ecd6cf80Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
1465ecd6cf80Smarks 		if (who_pair == NULL) {
1466ecd6cf80Smarks 			goto abort;
1467ecd6cf80Smarks 		}
1468ecd6cf80Smarks 
1469ecd6cf80Smarks 		do {
1470ecd6cf80Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
1471ecd6cf80Smarks 			if (error) {
1472ecd6cf80Smarks 				goto abort;
1473ecd6cf80Smarks 			}
1474ecd6cf80Smarks 
1475ecd6cf80Smarks 			/*
1476ecd6cf80Smarks 			 * First build up the key to use
1477ecd6cf80Smarks 			 * for looking up in the various
1478ecd6cf80Smarks 			 * who trees.
1479ecd6cf80Smarks 			 */
1480ecd6cf80Smarks 			ld = nvpair_name(who_pair)[1];
1481ecd6cf80Smarks 			nvpname = nvpair_name(who_pair);
1482ecd6cf80Smarks 			switch (nvpair_name(who_pair)[0]) {
1483ecd6cf80Smarks 			case ZFS_DELEG_USER:
1484ecd6cf80Smarks 			case ZFS_DELEG_USER_SETS:
1485ecd6cf80Smarks 				tree = &zallowp->z_user;
1486ecd6cf80Smarks 				uid = atol(&nvpname[3]);
1487ecd6cf80Smarks 				pwd = getpwuid(uid);
1488ecd6cf80Smarks 				(void) snprintf(findallownode.z_key,
1489ecd6cf80Smarks 				    sizeof (findallownode.z_key), "user %s",
1490ecd6cf80Smarks 				    (pwd) ? pwd->pw_name :
1491ecd6cf80Smarks 				    &nvpair_name(who_pair)[3]);
1492ecd6cf80Smarks 				break;
1493ecd6cf80Smarks 			case ZFS_DELEG_GROUP:
1494ecd6cf80Smarks 			case ZFS_DELEG_GROUP_SETS:
1495ecd6cf80Smarks 				tree = &zallowp->z_group;
1496ecd6cf80Smarks 				gid = atol(&nvpname[3]);
1497ecd6cf80Smarks 				grp = getgrgid(gid);
1498ecd6cf80Smarks 				(void) snprintf(findallownode.z_key,
1499ecd6cf80Smarks 				    sizeof (findallownode.z_key), "group %s",
1500ecd6cf80Smarks 				    (grp) ? grp->gr_name :
1501ecd6cf80Smarks 				    &nvpair_name(who_pair)[3]);
1502ecd6cf80Smarks 				break;
1503ecd6cf80Smarks 			case ZFS_DELEG_CREATE:
1504ecd6cf80Smarks 			case ZFS_DELEG_CREATE_SETS:
1505ecd6cf80Smarks 				tree = &zallowp->z_crperms;
1506ecd6cf80Smarks 				(void) strlcpy(findallownode.z_key, "",
1507ecd6cf80Smarks 				    sizeof (findallownode.z_key));
1508ecd6cf80Smarks 				break;
1509ecd6cf80Smarks 			case ZFS_DELEG_EVERYONE:
1510ecd6cf80Smarks 			case ZFS_DELEG_EVERYONE_SETS:
1511ecd6cf80Smarks 				(void) snprintf(findallownode.z_key,
1512ecd6cf80Smarks 				    sizeof (findallownode.z_key), "everyone");
1513ecd6cf80Smarks 				tree = &zallowp->z_everyone;
1514ecd6cf80Smarks 				break;
1515ecd6cf80Smarks 			case ZFS_DELEG_NAMED_SET:
1516ecd6cf80Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
1517ecd6cf80Smarks 				(void) snprintf(findallownode.z_key,
1518ecd6cf80Smarks 				    sizeof (findallownode.z_key), "%s",
1519ecd6cf80Smarks 				    &nvpair_name(who_pair)[3]);
1520ecd6cf80Smarks 				tree = &zallowp->z_sets;
1521ecd6cf80Smarks 				break;
1522ecd6cf80Smarks 			}
1523ecd6cf80Smarks 
1524ecd6cf80Smarks 			/*
1525ecd6cf80Smarks 			 * Place who in tree
1526ecd6cf80Smarks 			 */
1527ecd6cf80Smarks 			allownode = avl_find(tree, &findallownode, &where);
1528ecd6cf80Smarks 			if (allownode == NULL) {
1529ecd6cf80Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
1530ecd6cf80Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
1531ecd6cf80Smarks 					goto abort;
1532ecd6cf80Smarks 				}
1533ecd6cf80Smarks 				avl_create(&newallownode->z_localdescend,
1534ecd6cf80Smarks 				    perm_compare,
1535ecd6cf80Smarks 				    sizeof (zfs_perm_node_t),
1536ecd6cf80Smarks 				    offsetof(zfs_perm_node_t, z_node));
1537ecd6cf80Smarks 				avl_create(&newallownode->z_local,
1538ecd6cf80Smarks 				    perm_compare,
1539ecd6cf80Smarks 				    sizeof (zfs_perm_node_t),
1540ecd6cf80Smarks 				    offsetof(zfs_perm_node_t, z_node));
1541ecd6cf80Smarks 				avl_create(&newallownode->z_descend,
1542ecd6cf80Smarks 				    perm_compare,
1543ecd6cf80Smarks 				    sizeof (zfs_perm_node_t),
1544ecd6cf80Smarks 				    offsetof(zfs_perm_node_t, z_node));
1545ecd6cf80Smarks 				(void) strlcpy(newallownode->z_key,
1546ecd6cf80Smarks 				    findallownode.z_key,
1547ecd6cf80Smarks 				    sizeof (findallownode.z_key));
1548ecd6cf80Smarks 				avl_insert(tree, newallownode, where);
1549ecd6cf80Smarks 				allownode = newallownode;
1550ecd6cf80Smarks 			}
1551ecd6cf80Smarks 
1552ecd6cf80Smarks 			/*
1553ecd6cf80Smarks 			 * Now iterate over the permissions and
1554ecd6cf80Smarks 			 * place them in the appropriate local,
1555ecd6cf80Smarks 			 * descendent or local+descendent tree.
1556ecd6cf80Smarks 			 *
1557ecd6cf80Smarks 			 * The permissions are added to the tree
1558ecd6cf80Smarks 			 * via zfs_coalesce_perm().
1559ecd6cf80Smarks 			 */
1560ecd6cf80Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
1561ecd6cf80Smarks 			if (perm_pair == NULL)
1562ecd6cf80Smarks 				goto abort;
1563ecd6cf80Smarks 			do {
1564ecd6cf80Smarks 				if (zfs_coalesce_perm(zhp, allownode,
1565ecd6cf80Smarks 				    nvpair_name(perm_pair), ld) != 0)
1566ecd6cf80Smarks 					goto abort;
1567ecd6cf80Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
1568ecd6cf80Smarks 			    perm_pair));
1569ecd6cf80Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
1570ecd6cf80Smarks 
1571ecd6cf80Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
1572ecd6cf80Smarks 		if (source_pair == NULL)
1573ecd6cf80Smarks 			break;
1574ecd6cf80Smarks 
1575ecd6cf80Smarks 		/*
1576ecd6cf80Smarks 		 * allocate another node from the link list of
1577ecd6cf80Smarks 		 * zfs_allow_t structures
1578ecd6cf80Smarks 		 */
1579ecd6cf80Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
1580ecd6cf80Smarks 		    nvpair_name(source_pair));
1581ecd6cf80Smarks 		if (newallowp == NULL) {
1582ecd6cf80Smarks 			goto abort;
1583ecd6cf80Smarks 		}
1584ecd6cf80Smarks 		zallowp = newallowp;
1585ecd6cf80Smarks 	}
1586ecd6cf80Smarks 	nvlist_free(nvlist);
1587ecd6cf80Smarks 	return (0);
1588ecd6cf80Smarks abort:
1589ecd6cf80Smarks 	zfs_free_allows(*zfs_perms);
1590ecd6cf80Smarks 	nvlist_free(nvlist);
1591ecd6cf80Smarks 	return (-1);
1592ecd6cf80Smarks }
1593ecd6cf80Smarks 
15946949a980Smarks static char *
15956949a980Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
15966949a980Smarks {
15976949a980Smarks 	/*
15986949a980Smarks 	 * Don't put newlines on end of lines
15996949a980Smarks 	 */
16006949a980Smarks 	switch (note) {
16016949a980Smarks 	case ZFS_DELEG_NOTE_CREATE:
16026949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16036949a980Smarks 		    "Must also have the 'mount' ability"));
16046949a980Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16056949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16066949a980Smarks 		    "Must also have the 'mount' ability"));
16076949a980Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16086949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16096949a980Smarks 		    "Must also have the 'mount' ability"));
16106949a980Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16116949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16126949a980Smarks 		    "Must also have the 'mount' ability"));
16136949a980Smarks 	case ZFS_DELEG_NOTE_CLONE:
16146949a980Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16156949a980Smarks 		    "ability and 'mount'\n"
16166949a980Smarks 		    "\t\t\t\tability in the origin file system"));
16176949a980Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16186949a980Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16196949a980Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16206949a980Smarks 	case ZFS_DELEG_NOTE_RENAME:
16216949a980Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16226949a980Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16236949a980Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16246949a980Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16256949a980Smarks 		    " and 'create' ability"));
16266949a980Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16276949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16286949a980Smarks 		    "Allows changing any user property"));
16296949a980Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16306949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16316949a980Smarks 		    "Must also have the permission that is being\n"
16326949a980Smarks 		    "\t\t\t\tallowed"));
16336949a980Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16346949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16356949a980Smarks 		    "Allows mount/umount of ZFS datasets"));
16366949a980Smarks 	case ZFS_DELEG_NOTE_SHARE:
16376949a980Smarks 		return (dgettext(TEXT_DOMAIN,
16386949a980Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16396949a980Smarks 		    "\t\t\t\tprotocols"));
16406949a980Smarks 	case ZFS_DELEG_NOTE_NONE:
16416949a980Smarks 	default:
16426949a980Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16436949a980Smarks 	}
16446949a980Smarks }
16456949a980Smarks 
16466949a980Smarks typedef enum {
16476949a980Smarks 	ZFS_DELEG_SUBCOMMAND,
16486949a980Smarks 	ZFS_DELEG_PROP,
16496949a980Smarks 	ZFS_DELEG_OTHER
16506949a980Smarks } zfs_deleg_perm_type_t;
16516949a980Smarks 
16526949a980Smarks /*
16536949a980Smarks  * is the permission a subcommand or other?
16546949a980Smarks  */
16556949a980Smarks zfs_deleg_perm_type_t
16566949a980Smarks zfs_deleg_perm_type(const char *perm)
16576949a980Smarks {
16586949a980Smarks 	if (strcmp(perm, "userprop") == 0)
16596949a980Smarks 		return (ZFS_DELEG_OTHER);
16606949a980Smarks 	else
16616949a980Smarks 		return (ZFS_DELEG_SUBCOMMAND);
16626949a980Smarks }
16636949a980Smarks 
16646949a980Smarks static char *
16656949a980Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
16666949a980Smarks {
16676949a980Smarks 	switch (type) {
16686949a980Smarks 	case ZFS_DELEG_SUBCOMMAND:
16696949a980Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
16706949a980Smarks 	case ZFS_DELEG_PROP:
16716949a980Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
16726949a980Smarks 	case ZFS_DELEG_OTHER:
16736949a980Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
16746949a980Smarks 	}
16756949a980Smarks 	return ("");
16766949a980Smarks }
16776949a980Smarks 
16786949a980Smarks /*ARGSUSED*/
16796949a980Smarks static int
16806949a980Smarks zfs_deleg_prop_cb(int prop, void *cb)
16816949a980Smarks {
16826949a980Smarks 	if (zfs_prop_delegatable(prop))
16836949a980Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
16846949a980Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
16856949a980Smarks 
16866949a980Smarks 	return (ZPROP_CONT);
16876949a980Smarks }
16886949a980Smarks 
16896949a980Smarks void
16906949a980Smarks zfs_deleg_permissions(void)
16916949a980Smarks {
16926949a980Smarks 	int i;
16936949a980Smarks 
16946949a980Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
16956949a980Smarks 	    "TYPE", "NOTES");
16966949a980Smarks 
16976949a980Smarks 	/*
16986949a980Smarks 	 * First print out the subcommands
16996949a980Smarks 	 */
17006949a980Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
17016949a980Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17026949a980Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17036949a980Smarks 		    zfs_deleg_perm_type_str(
17046949a980Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17056949a980Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17066949a980Smarks 	}
17076949a980Smarks 
17086949a980Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17096949a980Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17106949a980Smarks }
17116949a980Smarks 
1712e9dbad6fSeschrock /*
1713e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1714e9dbad6fSeschrock  */
1715e9dbad6fSeschrock int
1716e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1717e9dbad6fSeschrock {
1718e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1719e9dbad6fSeschrock 	int ret = -1;
1720e9dbad6fSeschrock 	prop_changelist_t *cl = NULL;
1721e9dbad6fSeschrock 	char errbuf[1024];
1722e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1723e9dbad6fSeschrock 	nvlist_t *nvl = NULL, *realprops;
1724e9dbad6fSeschrock 	zfs_prop_t prop;
1725a227b7f4Shs24103 	int do_prefix = 1;
1726e9dbad6fSeschrock 
1727e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1728e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1729e9dbad6fSeschrock 	    zhp->zfs_name);
1730e9dbad6fSeschrock 
1731e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1732e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1733e9dbad6fSeschrock 		(void) no_memory(hdl);
1734e9dbad6fSeschrock 		goto error;
1735e9dbad6fSeschrock 	}
1736e9dbad6fSeschrock 
1737990b4856Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
1738e9dbad6fSeschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1739e9dbad6fSeschrock 		goto error;
1740990b4856Slling 
1741e9dbad6fSeschrock 	nvlist_free(nvl);
1742e9dbad6fSeschrock 	nvl = realprops;
1743e9dbad6fSeschrock 
1744e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
1745e9dbad6fSeschrock 
1746fa9e4066Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1747e9dbad6fSeschrock 		goto error;
1748fa9e4066Sahrens 
1749fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
175099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1751fa9e4066Sahrens 		    "child dataset with inherited mountpoint is used "
175299653d4eSeschrock 		    "in a non-global zone"));
175399653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1754fa9e4066Sahrens 		goto error;
1755fa9e4066Sahrens 	}
1756fa9e4066Sahrens 
1757a227b7f4Shs24103 
1758a227b7f4Shs24103 	/* do not unmount dataset if canmount is being set to noauto */
1759a227b7f4Shs24103 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
1760a227b7f4Shs24103 		do_prefix = 0;
1761a227b7f4Shs24103 
1762a227b7f4Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1763fa9e4066Sahrens 			goto error;
1764fa9e4066Sahrens 
1765fa9e4066Sahrens 	/*
1766fa9e4066Sahrens 	 * Execute the corresponding ioctl() to set this property.
1767fa9e4066Sahrens 	 */
1768fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1769fa9e4066Sahrens 
1770990b4856Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1771e9dbad6fSeschrock 		goto error;
1772e9dbad6fSeschrock 
1773ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1774fa9e4066Sahrens 	if (ret != 0) {
1775fa9e4066Sahrens 		switch (errno) {
1776fa9e4066Sahrens 
1777fa9e4066Sahrens 		case ENOSPC:
1778fa9e4066Sahrens 			/*
1779fa9e4066Sahrens 			 * For quotas and reservations, ENOSPC indicates
1780fa9e4066Sahrens 			 * something different; setting a quota or reservation
1781fa9e4066Sahrens 			 * doesn't use any disk space.
1782fa9e4066Sahrens 			 */
1783fa9e4066Sahrens 			switch (prop) {
1784fa9e4066Sahrens 			case ZFS_PROP_QUOTA:
1785a9799022Sck153898 			case ZFS_PROP_REFQUOTA:
178699653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178799653d4eSeschrock 				    "size is less than current used or "
178899653d4eSeschrock 				    "reserved space"));
178999653d4eSeschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1790fa9e4066Sahrens 				break;
1791fa9e4066Sahrens 
1792fa9e4066Sahrens 			case ZFS_PROP_RESERVATION:
1793a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
179499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179599653d4eSeschrock 				    "size is greater than available space"));
179699653d4eSeschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1797fa9e4066Sahrens 				break;
1798fa9e4066Sahrens 
1799fa9e4066Sahrens 			default:
180099653d4eSeschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1801fa9e4066Sahrens 				break;
1802fa9e4066Sahrens 			}
1803fa9e4066Sahrens 			break;
1804fa9e4066Sahrens 
1805fa9e4066Sahrens 		case EBUSY:
180699653d4eSeschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
180799653d4eSeschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
180899653d4eSeschrock 			else
1809e9dbad6fSeschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1810fa9e4066Sahrens 			break;
1811fa9e4066Sahrens 
18122a79c5feSlling 		case EROFS:
181399653d4eSeschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18142a79c5feSlling 			break;
18152a79c5feSlling 
1816c9431fa1Sahl 		case ENOTSUP:
1817c9431fa1Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18189e6eda55Smarks 			    "pool and or dataset must be upgraded to set this "
181940feaa91Sahrens 			    "property or value"));
1820c9431fa1Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1821c9431fa1Sahl 			break;
1822c9431fa1Sahl 
1823fa9e4066Sahrens 		case EOVERFLOW:
1824fa9e4066Sahrens 			/*
1825fa9e4066Sahrens 			 * This platform can't address a volume this big.
1826fa9e4066Sahrens 			 */
1827fa9e4066Sahrens #ifdef _ILP32
1828fa9e4066Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
182999653d4eSeschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1830fa9e4066Sahrens 				break;
1831fa9e4066Sahrens 			}
1832fa9e4066Sahrens #endif
183399653d4eSeschrock 			/* FALLTHROUGH */
1834fa9e4066Sahrens 		default:
183599653d4eSeschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1836fa9e4066Sahrens 		}
1837fa9e4066Sahrens 	} else {
1838a227b7f4Shs24103 		if (do_prefix)
1839a227b7f4Shs24103 			ret = changelist_postfix(cl);
1840a227b7f4Shs24103 
1841fa9e4066Sahrens 		/*
1842fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1843fa9e4066Sahrens 		 * is reflected.
1844fa9e4066Sahrens 		 */
1845a227b7f4Shs24103 		if (ret == 0)
1846fa9e4066Sahrens 			(void) get_stats(zhp);
1847fa9e4066Sahrens 	}
1848fa9e4066Sahrens 
1849fa9e4066Sahrens error:
1850e9dbad6fSeschrock 	nvlist_free(nvl);
1851e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1852e9dbad6fSeschrock 	if (cl)
1853fa9e4066Sahrens 		changelist_free(cl);
1854fa9e4066Sahrens 	return (ret);
1855fa9e4066Sahrens }
1856fa9e4066Sahrens 
1857fa9e4066Sahrens /*
1858fa9e4066Sahrens  * Given a property, inherit the value from the parent dataset.
1859fa9e4066Sahrens  */
1860fa9e4066Sahrens int
1861e9dbad6fSeschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1862fa9e4066Sahrens {
1863fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1864fa9e4066Sahrens 	int ret;
1865fa9e4066Sahrens 	prop_changelist_t *cl;
186699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
186799653d4eSeschrock 	char errbuf[1024];
1868e9dbad6fSeschrock 	zfs_prop_t prop;
186999653d4eSeschrock 
187099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
187199653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1872fa9e4066Sahrens 
1873990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1874e9dbad6fSeschrock 		/*
1875e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1876e9dbad6fSeschrock 		 * small, so just do it here.
1877e9dbad6fSeschrock 		 */
1878e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1879e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1880e9dbad6fSeschrock 			    "invalid property"));
1881e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1882e9dbad6fSeschrock 		}
1883e9dbad6fSeschrock 
1884e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1885e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1886e9dbad6fSeschrock 
1887e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1888e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1889e9dbad6fSeschrock 
1890e9dbad6fSeschrock 		return (0);
1891e9dbad6fSeschrock 	}
1892e9dbad6fSeschrock 
1893fa9e4066Sahrens 	/*
1894fa9e4066Sahrens 	 * Verify that this property is inheritable.
1895fa9e4066Sahrens 	 */
189699653d4eSeschrock 	if (zfs_prop_readonly(prop))
189799653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1898fa9e4066Sahrens 
189999653d4eSeschrock 	if (!zfs_prop_inheritable(prop))
190099653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1901fa9e4066Sahrens 
1902fa9e4066Sahrens 	/*
1903fa9e4066Sahrens 	 * Check to see if the value applies to this type
1904fa9e4066Sahrens 	 */
190599653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
190699653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1907fa9e4066Sahrens 
1908bf7c2d40Srm160521 	/*
1909bf7c2d40Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
1910bf7c2d40Srm160521 	 */
1911bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1912fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1913e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1914fa9e4066Sahrens 
1915fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1916fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
191799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
191899653d4eSeschrock 		    "dataset is used in a non-global zone"));
191999653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1920fa9e4066Sahrens 	}
1921fa9e4066Sahrens 
1922fa9e4066Sahrens 	/*
1923fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1924fa9e4066Sahrens 	 */
1925fa9e4066Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1926fa9e4066Sahrens 		return (-1);
1927fa9e4066Sahrens 
1928fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
192999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193099653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
193199653d4eSeschrock 		    "in a non-global zone"));
193299653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1933fa9e4066Sahrens 		goto error;
1934fa9e4066Sahrens 	}
1935fa9e4066Sahrens 
1936fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1937fa9e4066Sahrens 		goto error;
1938fa9e4066Sahrens 
1939e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
194099653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1941fa9e4066Sahrens 	} else {
1942fa9e4066Sahrens 
1943efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1944fa9e4066Sahrens 			goto error;
1945fa9e4066Sahrens 
1946fa9e4066Sahrens 		/*
1947fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1948fa9e4066Sahrens 		 */
1949fa9e4066Sahrens 		(void) get_stats(zhp);
1950fa9e4066Sahrens 	}
1951fa9e4066Sahrens 
1952fa9e4066Sahrens error:
1953fa9e4066Sahrens 	changelist_free(cl);
1954fa9e4066Sahrens 	return (ret);
1955fa9e4066Sahrens }
1956fa9e4066Sahrens 
1957fa9e4066Sahrens /*
19587f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
19597f7322feSeschrock  * extract them appropriately.
19607f7322feSeschrock  */
19617f7322feSeschrock static uint64_t
19627f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19637f7322feSeschrock {
19647f7322feSeschrock 	nvlist_t *nv;
19657f7322feSeschrock 	uint64_t value;
19667f7322feSeschrock 
1967a2eea2e1Sahrens 	*source = NULL;
19687f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19697f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1970990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1971990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19727f7322feSeschrock 	} else {
19737f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
19747f7322feSeschrock 		*source = "";
19757f7322feSeschrock 	}
19767f7322feSeschrock 
19777f7322feSeschrock 	return (value);
19787f7322feSeschrock }
19797f7322feSeschrock 
19807f7322feSeschrock static char *
19817f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19827f7322feSeschrock {
19837f7322feSeschrock 	nvlist_t *nv;
19847f7322feSeschrock 	char *value;
19857f7322feSeschrock 
1986a2eea2e1Sahrens 	*source = NULL;
19877f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19887f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1989990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1990990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19917f7322feSeschrock 	} else {
19927f7322feSeschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
19937f7322feSeschrock 			value = "";
19947f7322feSeschrock 		*source = "";
19957f7322feSeschrock 	}
19967f7322feSeschrock 
19977f7322feSeschrock 	return (value);
19987f7322feSeschrock }
19997f7322feSeschrock 
20007f7322feSeschrock /*
2001fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2002fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
2003fa9e4066Sahrens  *
2004fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2005fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2006fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
2007fa9e4066Sahrens  * the source "temporary".
2008fa9e4066Sahrens  */
200999653d4eSeschrock static int
2010990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
201199653d4eSeschrock     char **source, uint64_t *val)
2012fa9e4066Sahrens {
2013bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
201496510749Stimh 	nvlist_t *zplprops = NULL;
2015fa9e4066Sahrens 	struct mnttab mnt;
20163ccfa83cSahrens 	char *mntopt_on = NULL;
20173ccfa83cSahrens 	char *mntopt_off = NULL;
2018fa9e4066Sahrens 
2019fa9e4066Sahrens 	*source = NULL;
2020fa9e4066Sahrens 
20213ccfa83cSahrens 	switch (prop) {
20223ccfa83cSahrens 	case ZFS_PROP_ATIME:
20233ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
20243ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
20253ccfa83cSahrens 		break;
20263ccfa83cSahrens 
20273ccfa83cSahrens 	case ZFS_PROP_DEVICES:
20283ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
20293ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
20303ccfa83cSahrens 		break;
20313ccfa83cSahrens 
20323ccfa83cSahrens 	case ZFS_PROP_EXEC:
20333ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
20343ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
20353ccfa83cSahrens 		break;
20363ccfa83cSahrens 
20373ccfa83cSahrens 	case ZFS_PROP_READONLY:
20383ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
20393ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
20403ccfa83cSahrens 		break;
20413ccfa83cSahrens 
20423ccfa83cSahrens 	case ZFS_PROP_SETUID:
20433ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
20443ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
20453ccfa83cSahrens 		break;
20463ccfa83cSahrens 
20473ccfa83cSahrens 	case ZFS_PROP_XATTR:
20483ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
20493ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
20503ccfa83cSahrens 		break;
2051da6c28aaSamw 
2052da6c28aaSamw 	case ZFS_PROP_NBMAND:
2053da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
2054da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2055da6c28aaSamw 		break;
20563ccfa83cSahrens 	}
20573ccfa83cSahrens 
20583bb79becSeschrock 	/*
20593bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
20603bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
20613bb79becSeschrock 	 * we're looking up a property which requires its presence.
20623bb79becSeschrock 	 */
20633bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
20643ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
20653ccfa83cSahrens 		struct mnttab entry, search = { 0 };
20663ccfa83cSahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
20673bb79becSeschrock 
20683bb79becSeschrock 		search.mnt_special = (char *)zhp->zfs_name;
20693bb79becSeschrock 		search.mnt_fstype = MNTTYPE_ZFS;
20703ccfa83cSahrens 		rewind(mnttab);
20713bb79becSeschrock 
20723ccfa83cSahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
20733ccfa83cSahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
20743ccfa83cSahrens 			    entry.mnt_mntopts);
20753ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
20763bb79becSeschrock 				return (-1);
20773ccfa83cSahrens 		}
20783bb79becSeschrock 
20793bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
20803bb79becSeschrock 	}
20813bb79becSeschrock 
2082fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2083fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2084fa9e4066Sahrens 	else
2085fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2086fa9e4066Sahrens 
2087fa9e4066Sahrens 	switch (prop) {
2088fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2089fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2090fa9e4066Sahrens 	case ZFS_PROP_EXEC:
20913ccfa83cSahrens 	case ZFS_PROP_READONLY:
20923ccfa83cSahrens 	case ZFS_PROP_SETUID:
20933ccfa83cSahrens 	case ZFS_PROP_XATTR:
2094da6c28aaSamw 	case ZFS_PROP_NBMAND:
209599653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2096fa9e4066Sahrens 
20973ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
209899653d4eSeschrock 			*val = B_TRUE;
2099fa9e4066Sahrens 			if (src)
2100990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
21013ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
210299653d4eSeschrock 			*val = B_FALSE;
2103fa9e4066Sahrens 			if (src)
2104990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2105fa9e4066Sahrens 		}
210699653d4eSeschrock 		break;
2107fa9e4066Sahrens 
21083ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
210999653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2110a227b7f4Shs24103 		if (*val != ZFS_CANMOUNT_ON)
2111fda77a98Srm160521 			*source = zhp->zfs_name;
2112fda77a98Srm160521 		else
2113fda77a98Srm160521 			*source = "";	/* default */
211499653d4eSeschrock 		break;
2115fa9e4066Sahrens 
2116fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2117a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2118fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2119a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2120a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
2121a2eea2e1Sahrens 		if (*val == 0)
2122fa9e4066Sahrens 			*source = "";	/* default */
2123fa9e4066Sahrens 		else
2124fa9e4066Sahrens 			*source = zhp->zfs_name;
212599653d4eSeschrock 		break;
2126fa9e4066Sahrens 
2127fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
212899653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
212999653d4eSeschrock 		break;
2130fa9e4066Sahrens 
213139c23413Seschrock 	case ZFS_PROP_NUMCLONES:
213239c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
213339c23413Seschrock 		break;
213439c23413Seschrock 
2135bd00f61bSrm160521 	case ZFS_PROP_VERSION:
2136de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2137de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2138de8267e0Stimh 	case ZFS_PROP_CASE:
2139de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2140de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
21413d7934e1Srm160521 			return (-1);
2142bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2143de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2144de8267e0Stimh 			zcmd_free_nvlists(&zc);
2145bd00f61bSrm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2146de8267e0Stimh 			    "unable to get %s property"),
2147de8267e0Stimh 			    zfs_prop_to_name(prop));
2148bd00f61bSrm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
2149bd00f61bSrm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
2150bd00f61bSrm160521 		}
2151de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2152de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2153de8267e0Stimh 		    val) != 0) {
2154de8267e0Stimh 			zcmd_free_nvlists(&zc);
2155de8267e0Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2156de8267e0Stimh 			    "unable to get %s property"),
2157de8267e0Stimh 			    zfs_prop_to_name(prop));
2158de8267e0Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
2159de8267e0Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
2160de8267e0Stimh 		}
216196510749Stimh 		if (zplprops)
216296510749Stimh 			nvlist_free(zplprops);
2163de8267e0Stimh 		zcmd_free_nvlists(&zc);
2164bd00f61bSrm160521 		break;
2165bd00f61bSrm160521 
2166fa9e4066Sahrens 	default:
2167e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
216891ebeef5Sahrens 		case PROP_TYPE_NUMBER:
216991ebeef5Sahrens 		case PROP_TYPE_INDEX:
2170e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
2171e7437265Sahrens 			break;
2172e7437265Sahrens 
217391ebeef5Sahrens 		case PROP_TYPE_STRING:
2174e7437265Sahrens 		default:
217599653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
217699653d4eSeschrock 			    "cannot get non-numeric property"));
217799653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
217899653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
2179fa9e4066Sahrens 		}
2180e7437265Sahrens 	}
2181fa9e4066Sahrens 
2182fa9e4066Sahrens 	return (0);
2183fa9e4066Sahrens }
2184fa9e4066Sahrens 
2185fa9e4066Sahrens /*
2186fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2187fa9e4066Sahrens  */
2188fa9e4066Sahrens static void
2189990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2190fa9e4066Sahrens     char *statbuf, size_t statlen)
2191fa9e4066Sahrens {
2192990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2193fa9e4066Sahrens 		return;
2194fa9e4066Sahrens 
2195fa9e4066Sahrens 	if (source == NULL) {
2196990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2197fa9e4066Sahrens 	} else if (source[0] == '\0') {
2198990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
2199fa9e4066Sahrens 	} else {
2200fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2201990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2202fa9e4066Sahrens 		} else {
2203fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2204990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2205fa9e4066Sahrens 		}
2206fa9e4066Sahrens 	}
2207fa9e4066Sahrens 
2208fa9e4066Sahrens }
2209fa9e4066Sahrens 
2210fa9e4066Sahrens /*
2211fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2212fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2213fa9e4066Sahrens  * human-readable form.
2214fa9e4066Sahrens  *
2215fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2216fa9e4066Sahrens  */
2217fa9e4066Sahrens int
2218fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2219990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2220fa9e4066Sahrens {
2221fa9e4066Sahrens 	char *source = NULL;
2222fa9e4066Sahrens 	uint64_t val;
2223fa9e4066Sahrens 	char *str;
2224fa9e4066Sahrens 	const char *root;
2225e9dbad6fSeschrock 	const char *strval;
2226fa9e4066Sahrens 
2227fa9e4066Sahrens 	/*
2228fa9e4066Sahrens 	 * Check to see if this property applies to our object
2229fa9e4066Sahrens 	 */
2230fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2231fa9e4066Sahrens 		return (-1);
2232fa9e4066Sahrens 
2233fa9e4066Sahrens 	if (src)
2234990b4856Slling 		*src = ZPROP_SRC_NONE;
2235fa9e4066Sahrens 
2236fa9e4066Sahrens 	switch (prop) {
2237fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2238fa9e4066Sahrens 		/*
2239fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2240fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2241fa9e4066Sahrens 		 */
2242fa9e4066Sahrens 		{
2243a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2244a2eea2e1Sahrens 			time_t time = (time_t)val;
2245fa9e4066Sahrens 			struct tm t;
2246fa9e4066Sahrens 
2247fa9e4066Sahrens 			if (literal ||
2248fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2249fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2250fa9e4066Sahrens 			    &t) == 0)
2251a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2252fa9e4066Sahrens 		}
2253fa9e4066Sahrens 		break;
2254fa9e4066Sahrens 
2255fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2256fa9e4066Sahrens 		/*
2257fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2258fa9e4066Sahrens 		 *
2259fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2260fa9e4066Sahrens 		 *  - for default mountpoints, construct it as /zfs/<dataset>
2261fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2262fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2263fa9e4066Sahrens 		 *
2264fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2265fa9e4066Sahrens 		 * root to any values we return.
2266fa9e4066Sahrens 		 */
2267ea8dc4b6Seschrock 		root = zhp->zfs_root;
22687f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2269fa9e4066Sahrens 
22707f7322feSeschrock 		if (str[0] == '\0') {
2271fa9e4066Sahrens 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
2272fa9e4066Sahrens 			    root, zhp->zfs_name);
22737f7322feSeschrock 		} else if (str[0] == '/') {
22747f7322feSeschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2275fa9e4066Sahrens 
2276fa9e4066Sahrens 			if (relpath[0] == '/')
2277fa9e4066Sahrens 				relpath++;
22787f7322feSeschrock 			if (str[1] == '\0')
22797f7322feSeschrock 				str++;
2280fa9e4066Sahrens 
2281fa9e4066Sahrens 			if (relpath[0] == '\0')
2282fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
22837f7322feSeschrock 				    root, str);
2284fa9e4066Sahrens 			else
2285fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
22867f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2287fa9e4066Sahrens 				    relpath);
2288fa9e4066Sahrens 		} else {
2289fa9e4066Sahrens 			/* 'legacy' or 'none' */
22907f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2291fa9e4066Sahrens 		}
2292fa9e4066Sahrens 
2293fa9e4066Sahrens 		break;
2294fa9e4066Sahrens 
2295fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
2296a2eea2e1Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2297fa9e4066Sahrens 		    proplen);
2298fa9e4066Sahrens 		/*
2299fa9e4066Sahrens 		 * If there is no parent at all, return failure to indicate that
2300fa9e4066Sahrens 		 * it doesn't apply to this dataset.
2301fa9e4066Sahrens 		 */
2302fa9e4066Sahrens 		if (propbuf[0] == '\0')
2303fa9e4066Sahrens 			return (-1);
2304fa9e4066Sahrens 		break;
2305fa9e4066Sahrens 
2306fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2307a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2308fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2309a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2310a9799022Sck153898 
231199653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
231299653d4eSeschrock 			return (-1);
2313fa9e4066Sahrens 
2314fa9e4066Sahrens 		/*
2315fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2316fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2317fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2318fa9e4066Sahrens 		 * that its set locally.
2319fa9e4066Sahrens 		 */
2320fa9e4066Sahrens 		if (val == 0) {
2321fa9e4066Sahrens 			if (literal)
2322fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2323fa9e4066Sahrens 			else
2324fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2325fa9e4066Sahrens 		} else {
2326fa9e4066Sahrens 			if (literal)
23275ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
23285ad82045Snd150628 				    (u_longlong_t)val);
2329fa9e4066Sahrens 			else
2330fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2331fa9e4066Sahrens 		}
2332fa9e4066Sahrens 		break;
2333fa9e4066Sahrens 
2334fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
233599653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
233699653d4eSeschrock 			return (-1);
23375ad82045Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
23385ad82045Snd150628 		    val / 100, (longlong_t)val % 100);
2339fa9e4066Sahrens 		break;
2340fa9e4066Sahrens 
2341fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2342fa9e4066Sahrens 		switch (zhp->zfs_type) {
2343fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2344fa9e4066Sahrens 			str = "filesystem";
2345fa9e4066Sahrens 			break;
2346fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2347fa9e4066Sahrens 			str = "volume";
2348fa9e4066Sahrens 			break;
2349fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2350fa9e4066Sahrens 			str = "snapshot";
2351fa9e4066Sahrens 			break;
2352fa9e4066Sahrens 		default:
235399653d4eSeschrock 			abort();
2354fa9e4066Sahrens 		}
2355fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2356fa9e4066Sahrens 		break;
2357fa9e4066Sahrens 
2358fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2359fa9e4066Sahrens 		/*
2360fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2361fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2362fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2363fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2364fa9e4066Sahrens 		 */
236599653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
236699653d4eSeschrock 		    src, &source, &val) != 0)
236799653d4eSeschrock 			return (-1);
236899653d4eSeschrock 		if (val)
2369fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2370fa9e4066Sahrens 		else
2371fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2372fa9e4066Sahrens 		break;
2373fa9e4066Sahrens 
2374fa9e4066Sahrens 	case ZFS_PROP_NAME:
2375fa9e4066Sahrens 		/*
2376fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2377fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2378fa9e4066Sahrens 		 * consumers.
2379fa9e4066Sahrens 		 */
2380fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2381fa9e4066Sahrens 		break;
2382fa9e4066Sahrens 
2383fa9e4066Sahrens 	default:
2384e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
238591ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2386e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2387e7437265Sahrens 			    &source, &val) != 0)
2388e7437265Sahrens 				return (-1);
2389e7437265Sahrens 			if (literal)
2390e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2391e7437265Sahrens 				    (u_longlong_t)val);
2392e7437265Sahrens 			else
2393e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2394e7437265Sahrens 			break;
2395e7437265Sahrens 
239691ebeef5Sahrens 		case PROP_TYPE_STRING:
2397e7437265Sahrens 			(void) strlcpy(propbuf,
2398e7437265Sahrens 			    getprop_string(zhp, prop, &source), proplen);
2399e7437265Sahrens 			break;
2400e7437265Sahrens 
240191ebeef5Sahrens 		case PROP_TYPE_INDEX:
2402fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2403fe192f76Sahrens 			    &source, &val) != 0)
2404fe192f76Sahrens 				return (-1);
2405fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2406e7437265Sahrens 				return (-1);
2407e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2408e7437265Sahrens 			break;
2409e7437265Sahrens 
2410e7437265Sahrens 		default:
241199653d4eSeschrock 			abort();
2412fa9e4066Sahrens 		}
2413e7437265Sahrens 	}
2414fa9e4066Sahrens 
2415fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2416fa9e4066Sahrens 
2417fa9e4066Sahrens 	return (0);
2418fa9e4066Sahrens }
2419fa9e4066Sahrens 
2420fa9e4066Sahrens /*
2421fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2422fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2423fa9e4066Sahrens  * hard-coded property types.
2424fa9e4066Sahrens  */
2425fa9e4066Sahrens uint64_t
2426fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2427fa9e4066Sahrens {
2428fa9e4066Sahrens 	char *source;
242999653d4eSeschrock 	uint64_t val;
2430fa9e4066Sahrens 
24313cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
243299653d4eSeschrock 
243399653d4eSeschrock 	return (val);
2434fa9e4066Sahrens }
2435fa9e4066Sahrens 
24367b97dc1aSrm160521 int
24377b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
24387b97dc1aSrm160521 {
24397b97dc1aSrm160521 	char buf[64];
24407b97dc1aSrm160521 
24417b97dc1aSrm160521 	zfs_nicenum(val, buf, sizeof (buf));
24427b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
24437b97dc1aSrm160521 }
24447b97dc1aSrm160521 
2445fa9e4066Sahrens /*
2446fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2447fa9e4066Sahrens  */
2448fa9e4066Sahrens int
2449fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2450990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2451fa9e4066Sahrens {
2452fa9e4066Sahrens 	char *source;
2453fa9e4066Sahrens 
2454fa9e4066Sahrens 	/*
2455fa9e4066Sahrens 	 * Check to see if this property applies to our object
2456fa9e4066Sahrens 	 */
2457e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2458ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
245999653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
246099653d4eSeschrock 		    zfs_prop_to_name(prop)));
2461e45ce728Sahrens 	}
2462fa9e4066Sahrens 
2463fa9e4066Sahrens 	if (src)
2464990b4856Slling 		*src = ZPROP_SRC_NONE;
2465fa9e4066Sahrens 
246699653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
246799653d4eSeschrock 		return (-1);
2468fa9e4066Sahrens 
2469fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2470fa9e4066Sahrens 
2471fa9e4066Sahrens 	return (0);
2472fa9e4066Sahrens }
2473fa9e4066Sahrens 
2474fa9e4066Sahrens /*
2475fa9e4066Sahrens  * Returns the name of the given zfs handle.
2476fa9e4066Sahrens  */
2477fa9e4066Sahrens const char *
2478fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
2479fa9e4066Sahrens {
2480fa9e4066Sahrens 	return (zhp->zfs_name);
2481fa9e4066Sahrens }
2482fa9e4066Sahrens 
2483fa9e4066Sahrens /*
2484fa9e4066Sahrens  * Returns the type of the given zfs handle.
2485fa9e4066Sahrens  */
2486fa9e4066Sahrens zfs_type_t
2487fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
2488fa9e4066Sahrens {
2489fa9e4066Sahrens 	return (zhp->zfs_type);
2490fa9e4066Sahrens }
2491fa9e4066Sahrens 
2492fa9e4066Sahrens /*
24937f7322feSeschrock  * Iterate over all child filesystems
2494fa9e4066Sahrens  */
2495fa9e4066Sahrens int
24967f7322feSeschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2497fa9e4066Sahrens {
2498fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2499fa9e4066Sahrens 	zfs_handle_t *nzhp;
2500fa9e4066Sahrens 	int ret;
2501fa9e4066Sahrens 
25023cb34c60Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25033cb34c60Sahrens 		return (0);
25043cb34c60Sahrens 
2505fa9e4066Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
250699653d4eSeschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2507fa9e4066Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2508fa9e4066Sahrens 		/*
2509fa9e4066Sahrens 		 * Ignore private dataset names.
2510fa9e4066Sahrens 		 */
2511fa9e4066Sahrens 		if (dataset_name_hidden(zc.zc_name))
2512fa9e4066Sahrens 			continue;
2513fa9e4066Sahrens 
2514fa9e4066Sahrens 		/*
2515fa9e4066Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2516fa9e4066Sahrens 		 * that the pool has since been removed.
2517fa9e4066Sahrens 		 */
251899653d4eSeschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
251999653d4eSeschrock 		    zc.zc_name)) == NULL)
2520fa9e4066Sahrens 			continue;
2521fa9e4066Sahrens 
2522fa9e4066Sahrens 		if ((ret = func(nzhp, data)) != 0)
2523fa9e4066Sahrens 			return (ret);
2524fa9e4066Sahrens 	}
2525fa9e4066Sahrens 
2526fa9e4066Sahrens 	/*
2527fa9e4066Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2528fa9e4066Sahrens 	 * returned, then the underlying dataset has been removed since we
2529fa9e4066Sahrens 	 * obtained the handle.
2530fa9e4066Sahrens 	 */
2531fa9e4066Sahrens 	if (errno != ESRCH && errno != ENOENT)
253299653d4eSeschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
253399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2534fa9e4066Sahrens 
25357f7322feSeschrock 	return (0);
25367f7322feSeschrock }
25377f7322feSeschrock 
25387f7322feSeschrock /*
25397f7322feSeschrock  * Iterate over all snapshots
25407f7322feSeschrock  */
25417f7322feSeschrock int
25427f7322feSeschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25437f7322feSeschrock {
25447f7322feSeschrock 	zfs_cmd_t zc = { 0 };
25457f7322feSeschrock 	zfs_handle_t *nzhp;
25467f7322feSeschrock 	int ret;
2547fa9e4066Sahrens 
25483cb34c60Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
25493cb34c60Sahrens 		return (0);
25503cb34c60Sahrens 
2551fa9e4066Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
255299653d4eSeschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
255399653d4eSeschrock 	    &zc) == 0;
2554fa9e4066Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2555fa9e4066Sahrens 
255699653d4eSeschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
255799653d4eSeschrock 		    zc.zc_name)) == NULL)
2558fa9e4066Sahrens 			continue;
2559fa9e4066Sahrens 
2560fa9e4066Sahrens 		if ((ret = func(nzhp, data)) != 0)
2561fa9e4066Sahrens 			return (ret);
2562fa9e4066Sahrens 	}
2563fa9e4066Sahrens 
2564fa9e4066Sahrens 	/*
2565fa9e4066Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2566fa9e4066Sahrens 	 * returned, then the underlying dataset has been removed since we
2567fa9e4066Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2568fa9e4066Sahrens 	 */
2569fa9e4066Sahrens 	if (errno != ESRCH && errno != ENOENT)
257099653d4eSeschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
257199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2572fa9e4066Sahrens 
2573fa9e4066Sahrens 	return (0);
2574fa9e4066Sahrens }
2575fa9e4066Sahrens 
2576fa9e4066Sahrens /*
25777f7322feSeschrock  * Iterate over all children, snapshots and filesystems
25787f7322feSeschrock  */
25797f7322feSeschrock int
25807f7322feSeschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25817f7322feSeschrock {
25827f7322feSeschrock 	int ret;
25837f7322feSeschrock 
25847f7322feSeschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
25857f7322feSeschrock 		return (ret);
25867f7322feSeschrock 
25877f7322feSeschrock 	return (zfs_iter_snapshots(zhp, func, data));
25887f7322feSeschrock }
25897f7322feSeschrock 
25907f7322feSeschrock /*
2591fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
2592fa9e4066Sahrens  * Can return NULL if this is a pool.
2593fa9e4066Sahrens  */
2594fa9e4066Sahrens static int
2595fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
2596fa9e4066Sahrens {
2597fa9e4066Sahrens 	char *loc;
2598fa9e4066Sahrens 
2599fa9e4066Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2600fa9e4066Sahrens 		return (-1);
2601fa9e4066Sahrens 
2602fa9e4066Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2603fa9e4066Sahrens 	buf[loc - path] = '\0';
2604fa9e4066Sahrens 
2605fa9e4066Sahrens 	return (0);
2606fa9e4066Sahrens }
2607fa9e4066Sahrens 
2608fa9e4066Sahrens /*
26097f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
26107f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26117f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
26127f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
26137f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
26147f1f55eaSvb160487  * new datasets.
2615fa9e4066Sahrens  */
2616fa9e4066Sahrens static int
26177f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
26187f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
2619fa9e4066Sahrens {
2620fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2621fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
2622fa9e4066Sahrens 	char *slash;
26237f7322feSeschrock 	zfs_handle_t *zhp;
262499653d4eSeschrock 	char errbuf[1024];
262599653d4eSeschrock 
262699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
262799653d4eSeschrock 	    path);
2628fa9e4066Sahrens 
2629fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
2630fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
263199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263299653d4eSeschrock 		    "missing dataset name"));
263399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2634fa9e4066Sahrens 	}
2635fa9e4066Sahrens 
2636fa9e4066Sahrens 	/* check to see if the pool exists */
2637fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2638fa9e4066Sahrens 		slash = parent + strlen(parent);
2639fa9e4066Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2640fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
264199653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2642fa9e4066Sahrens 	    errno == ENOENT) {
264399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
264499653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
264599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2646fa9e4066Sahrens 	}
2647fa9e4066Sahrens 
2648fa9e4066Sahrens 	/* check to see if the parent dataset exists */
26497f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
26507f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
26517f1f55eaSvb160487 			/*
26527f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
26537f1f55eaSvb160487 			 */
26547f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
26557f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26567f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
26577f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
26587f1f55eaSvb160487 			}
26597f1f55eaSvb160487 		} else if (errno == ENOENT) {
266099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
266199653d4eSeschrock 			    "parent does not exist"));
266299653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
26637f1f55eaSvb160487 		} else
266499653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2665fa9e4066Sahrens 	}
2666fa9e4066Sahrens 
2667e9dbad6fSeschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2668fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
2669e9dbad6fSeschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
267099653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
26717f7322feSeschrock 		zfs_close(zhp);
2672fa9e4066Sahrens 		return (-1);
2673fa9e4066Sahrens 	}
2674fa9e4066Sahrens 
2675fa9e4066Sahrens 	/* make sure parent is a filesystem */
26767f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
267799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
267899653d4eSeschrock 		    "parent is not a filesystem"));
267999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
26807f7322feSeschrock 		zfs_close(zhp);
2681fa9e4066Sahrens 		return (-1);
2682fa9e4066Sahrens 	}
2683fa9e4066Sahrens 
26847f7322feSeschrock 	zfs_close(zhp);
26857f1f55eaSvb160487 	if (prefixlen != NULL)
26867f1f55eaSvb160487 		*prefixlen = strlen(parent);
26877f1f55eaSvb160487 	return (0);
26887f1f55eaSvb160487 }
26897f1f55eaSvb160487 
26907f1f55eaSvb160487 /*
26917f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
26927f1f55eaSvb160487  */
26937f1f55eaSvb160487 boolean_t
26947f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
26957f1f55eaSvb160487 {
26967f1f55eaSvb160487 	zfs_handle_t *zhp;
26977f1f55eaSvb160487 
2698f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
26997f1f55eaSvb160487 		return (B_FALSE);
27007f1f55eaSvb160487 
27017f1f55eaSvb160487 	/*
27027f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27037f1f55eaSvb160487 	 */
27047f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27057f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
27067f1f55eaSvb160487 
27077f1f55eaSvb160487 		zfs_close(zhp);
27087f1f55eaSvb160487 		if (types & ds_type)
27097f1f55eaSvb160487 			return (B_TRUE);
27107f1f55eaSvb160487 	}
27117f1f55eaSvb160487 	return (B_FALSE);
27127f1f55eaSvb160487 }
27137f1f55eaSvb160487 
27147f1f55eaSvb160487 /*
27153cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
27163cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
27173cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
27183cb34c60Sahrens  */
27193cb34c60Sahrens int
27203cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
27213cb34c60Sahrens {
27223cb34c60Sahrens 	zfs_handle_t *h;
27233cb34c60Sahrens 	char *cp;
27243cb34c60Sahrens 	const char *opname;
27253cb34c60Sahrens 
27263cb34c60Sahrens 	/* make sure prefix exists */
27273cb34c60Sahrens 	cp = target + prefixlen;
27283cb34c60Sahrens 	if (*cp != '/') {
27293cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
27303cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27313cb34c60Sahrens 	} else {
27323cb34c60Sahrens 		*cp = '\0';
27333cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27343cb34c60Sahrens 		*cp = '/';
27353cb34c60Sahrens 	}
27363cb34c60Sahrens 	if (h == NULL)
27373cb34c60Sahrens 		return (-1);
27383cb34c60Sahrens 	zfs_close(h);
27393cb34c60Sahrens 
27403cb34c60Sahrens 	/*
27413cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
27423cb34c60Sahrens 	 * up to the prefixlen-long one.
27433cb34c60Sahrens 	 */
27443cb34c60Sahrens 	for (cp = target + prefixlen + 1;
27453cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
27463cb34c60Sahrens 		char *logstr;
27473cb34c60Sahrens 
27483cb34c60Sahrens 		*cp = '\0';
27493cb34c60Sahrens 
27503cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
27513cb34c60Sahrens 		if (h) {
27523cb34c60Sahrens 			/* it already exists, nothing to do here */
27533cb34c60Sahrens 			zfs_close(h);
27543cb34c60Sahrens 			continue;
27553cb34c60Sahrens 		}
27563cb34c60Sahrens 
27573cb34c60Sahrens 		logstr = hdl->libzfs_log_str;
27583cb34c60Sahrens 		hdl->libzfs_log_str = NULL;
27593cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
27603cb34c60Sahrens 		    NULL) != 0) {
27613cb34c60Sahrens 			hdl->libzfs_log_str = logstr;
27623cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
27633cb34c60Sahrens 			goto ancestorerr;
27643cb34c60Sahrens 		}
27653cb34c60Sahrens 
27663cb34c60Sahrens 		hdl->libzfs_log_str = logstr;
27673cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27683cb34c60Sahrens 		if (h == NULL) {
27693cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
27703cb34c60Sahrens 			goto ancestorerr;
27713cb34c60Sahrens 		}
27723cb34c60Sahrens 
27733cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
27743cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
27753cb34c60Sahrens 			goto ancestorerr;
27763cb34c60Sahrens 		}
27773cb34c60Sahrens 
27783cb34c60Sahrens 		if (zfs_share(h) != 0) {
27793cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
27803cb34c60Sahrens 			goto ancestorerr;
27813cb34c60Sahrens 		}
27823cb34c60Sahrens 
27833cb34c60Sahrens 		zfs_close(h);
27843cb34c60Sahrens 	}
27853cb34c60Sahrens 
27863cb34c60Sahrens 	return (0);
27873cb34c60Sahrens 
27883cb34c60Sahrens ancestorerr:
27893cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27903cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
27913cb34c60Sahrens 	return (-1);
27923cb34c60Sahrens }
27933cb34c60Sahrens 
27943cb34c60Sahrens /*
27957f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
27967f1f55eaSvb160487  */
27977f1f55eaSvb160487 int
27987f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
27997f1f55eaSvb160487 {
28007f1f55eaSvb160487 	int prefix;
28017f1f55eaSvb160487 	uint64_t zoned;
28027f1f55eaSvb160487 	char *path_copy;
28037f1f55eaSvb160487 	int rc;
28047f1f55eaSvb160487 
28057f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28067f1f55eaSvb160487 		return (-1);
28077f1f55eaSvb160487 
28087f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
28097f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
28107f1f55eaSvb160487 		free(path_copy);
28117f1f55eaSvb160487 	}
28127f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
28137f1f55eaSvb160487 		return (-1);
28147f1f55eaSvb160487 
2815fa9e4066Sahrens 	return (0);
2816fa9e4066Sahrens }
2817fa9e4066Sahrens 
2818fa9e4066Sahrens /*
2819e9dbad6fSeschrock  * Create a new filesystem or volume.
2820fa9e4066Sahrens  */
2821fa9e4066Sahrens int
282299653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2823e9dbad6fSeschrock     nvlist_t *props)
2824fa9e4066Sahrens {
2825fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2826fa9e4066Sahrens 	int ret;
2827fa9e4066Sahrens 	uint64_t size = 0;
2828fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
282999653d4eSeschrock 	char errbuf[1024];
2830e9dbad6fSeschrock 	uint64_t zoned;
283199653d4eSeschrock 
283299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
283399653d4eSeschrock 	    "cannot create '%s'"), path);
2834fa9e4066Sahrens 
2835fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
2836f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
283799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2838fa9e4066Sahrens 
2839fa9e4066Sahrens 	/* validate parents exist */
28407f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2841fa9e4066Sahrens 		return (-1);
2842fa9e4066Sahrens 
2843fa9e4066Sahrens 	/*
2844fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
2845fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
2846fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2847fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2848fa9e4066Sahrens 	 * first try to see if the dataset exists.
2849fa9e4066Sahrens 	 */
2850fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2851990b4856Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
285299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
285399653d4eSeschrock 		    "dataset already exists"));
285499653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2855fa9e4066Sahrens 	}
2856fa9e4066Sahrens 
2857fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
2858fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2859fa9e4066Sahrens 	else
2860fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2861fa9e4066Sahrens 
2862990b4856Slling 	if (props && (props = zfs_validate_properties(hdl, type, props,
2863b1b8ab34Slling 	    zoned, NULL, errbuf)) == 0)
2864e9dbad6fSeschrock 		return (-1);
2865e9dbad6fSeschrock 
2866fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
28675c5460e9Seschrock 		/*
28685c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
28695c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
28705c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
28715c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
28725c5460e9Seschrock 		 * zero.
28735c5460e9Seschrock 		 */
2874e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
2875e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2876e9dbad6fSeschrock 			nvlist_free(props);
287799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2878e9dbad6fSeschrock 			    "missing volume size"));
2879e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2880fa9e4066Sahrens 		}
2881fa9e4066Sahrens 
2882e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
2883e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2884e9dbad6fSeschrock 		    &blocksize)) != 0) {
2885e9dbad6fSeschrock 			if (ret == ENOENT) {
2886e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
2887e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
2888e9dbad6fSeschrock 			} else {
2889e9dbad6fSeschrock 				nvlist_free(props);
289099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2891e9dbad6fSeschrock 				    "missing volume block size"));
2892e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2893e9dbad6fSeschrock 			}
2894e9dbad6fSeschrock 		}
2895e9dbad6fSeschrock 
2896e9dbad6fSeschrock 		if (size == 0) {
2897e9dbad6fSeschrock 			nvlist_free(props);
2898e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2899e9dbad6fSeschrock 			    "volume size cannot be zero"));
2900e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29015c5460e9Seschrock 		}
29025c5460e9Seschrock 
29035c5460e9Seschrock 		if (size % blocksize != 0) {
2904e9dbad6fSeschrock 			nvlist_free(props);
290599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2906e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
2907e9dbad6fSeschrock 			    "size"));
2908e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2909e9dbad6fSeschrock 		}
29105c5460e9Seschrock 	}
29115c5460e9Seschrock 
2912990b4856Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2913e9dbad6fSeschrock 		return (-1);
2914e9dbad6fSeschrock 	nvlist_free(props);
2915fa9e4066Sahrens 
2916fa9e4066Sahrens 	/* create the dataset */
2917ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2918fa9e4066Sahrens 
2919b1b8ab34Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
292099653d4eSeschrock 		ret = zvol_create_link(hdl, path);
2921b1b8ab34Slling 		if (ret) {
2922b1b8ab34Slling 			(void) zfs_standard_error(hdl, errno,
2923b1b8ab34Slling 			    dgettext(TEXT_DOMAIN,
2924b1b8ab34Slling 			    "Volume successfully created, but device links "
2925b1b8ab34Slling 			    "were not created"));
2926b1b8ab34Slling 			zcmd_free_nvlists(&zc);
2927b1b8ab34Slling 			return (-1);
2928b1b8ab34Slling 		}
2929b1b8ab34Slling 	}
2930fa9e4066Sahrens 
2931e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2932e9dbad6fSeschrock 
2933fa9e4066Sahrens 	/* check for failure */
2934fa9e4066Sahrens 	if (ret != 0) {
2935fa9e4066Sahrens 		char parent[ZFS_MAXNAMELEN];
2936fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2937fa9e4066Sahrens 
2938fa9e4066Sahrens 		switch (errno) {
2939fa9e4066Sahrens 		case ENOENT:
294099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
294199653d4eSeschrock 			    "no such parent '%s'"), parent);
294299653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2943fa9e4066Sahrens 
2944fa9e4066Sahrens 		case EINVAL:
294599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2946d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
294799653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2948fa9e4066Sahrens 
2949fa9e4066Sahrens 		case EDOM:
295099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2951e9dbad6fSeschrock 			    "volume block size must be power of 2 from "
2952e9dbad6fSeschrock 			    "%u to %uk"),
2953fa9e4066Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2954fa9e4066Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
295599653d4eSeschrock 
2956e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
295799653d4eSeschrock 
295840feaa91Sahrens 		case ENOTSUP:
295940feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
296040feaa91Sahrens 			    "pool must be upgraded to set this "
296140feaa91Sahrens 			    "property or value"));
296240feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2963fa9e4066Sahrens #ifdef _ILP32
2964fa9e4066Sahrens 		case EOVERFLOW:
2965fa9e4066Sahrens 			/*
2966fa9e4066Sahrens 			 * This platform can't address a volume this big.
2967fa9e4066Sahrens 			 */
296899653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
296999653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
297099653d4eSeschrock 				    errbuf));
2971fa9e4066Sahrens #endif
297299653d4eSeschrock 			/* FALLTHROUGH */
2973fa9e4066Sahrens 		default:
297499653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2975fa9e4066Sahrens 		}
2976fa9e4066Sahrens 	}
2977fa9e4066Sahrens 
2978fa9e4066Sahrens 	return (0);
2979fa9e4066Sahrens }
2980fa9e4066Sahrens 
2981fa9e4066Sahrens /*
2982fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2983fa9e4066Sahrens  * isn't mounted, and that there are no active dependents.
2984fa9e4066Sahrens  */
2985fa9e4066Sahrens int
2986fa9e4066Sahrens zfs_destroy(zfs_handle_t *zhp)
2987fa9e4066Sahrens {
2988fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2989fa9e4066Sahrens 
2990fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2991fa9e4066Sahrens 
2992e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
2993f3861e1aSahl 		/*
2994ecd6cf80Smarks 		 * If user doesn't have permissions to unshare volume, then
2995ecd6cf80Smarks 		 * abort the request.  This would only happen for a
2996ecd6cf80Smarks 		 * non-privileged user.
2997f3861e1aSahl 		 */
2998ecd6cf80Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
2999ecd6cf80Smarks 			return (-1);
3000ecd6cf80Smarks 		}
3001f3861e1aSahl 
300299653d4eSeschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3003fa9e4066Sahrens 			return (-1);
3004fa9e4066Sahrens 
3005fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3006fa9e4066Sahrens 	} else {
3007fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3008fa9e4066Sahrens 	}
3009fa9e4066Sahrens 
3010ecd6cf80Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3011ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
301299653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
301399653d4eSeschrock 		    zhp->zfs_name));
30141d452cf5Sahrens 	}
3015fa9e4066Sahrens 
3016fa9e4066Sahrens 	remove_mountpoint(zhp);
3017fa9e4066Sahrens 
3018fa9e4066Sahrens 	return (0);
3019fa9e4066Sahrens }
3020fa9e4066Sahrens 
30211d452cf5Sahrens struct destroydata {
30221d452cf5Sahrens 	char *snapname;
30231d452cf5Sahrens 	boolean_t gotone;
30243ccfa83cSahrens 	boolean_t closezhp;
30251d452cf5Sahrens };
30261d452cf5Sahrens 
30271d452cf5Sahrens static int
30281d452cf5Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
30291d452cf5Sahrens {
30301d452cf5Sahrens 	struct destroydata *dd = arg;
30311d452cf5Sahrens 	zfs_handle_t *szhp;
30321d452cf5Sahrens 	char name[ZFS_MAXNAMELEN];
30333ccfa83cSahrens 	boolean_t closezhp = dd->closezhp;
30343ccfa83cSahrens 	int rv;
30351d452cf5Sahrens 
3036e9dbad6fSeschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
3037e9dbad6fSeschrock 	(void) strlcat(name, "@", sizeof (name));
3038e9dbad6fSeschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
30391d452cf5Sahrens 
30401d452cf5Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
30411d452cf5Sahrens 	if (szhp) {
30421d452cf5Sahrens 		dd->gotone = B_TRUE;
30431d452cf5Sahrens 		zfs_close(szhp);
30441d452cf5Sahrens 	}
30451d452cf5Sahrens 
30461d452cf5Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
30471d452cf5Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
30481d452cf5Sahrens 		/*
30491d452cf5Sahrens 		 * NB: this is simply a best-effort.  We don't want to
30501d452cf5Sahrens 		 * return an error, because then we wouldn't visit all
30511d452cf5Sahrens 		 * the volumes.
30521d452cf5Sahrens 		 */
30531d452cf5Sahrens 	}
30541d452cf5Sahrens 
30553ccfa83cSahrens 	dd->closezhp = B_TRUE;
30563ccfa83cSahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
30573ccfa83cSahrens 	if (closezhp)
30583ccfa83cSahrens 		zfs_close(zhp);
30593ccfa83cSahrens 	return (rv);
30601d452cf5Sahrens }
30611d452cf5Sahrens 
30621d452cf5Sahrens /*
30631d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
30641d452cf5Sahrens  */
30651d452cf5Sahrens int
30661d452cf5Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
30671d452cf5Sahrens {
30681d452cf5Sahrens 	zfs_cmd_t zc = { 0 };
30691d452cf5Sahrens 	int ret;
30701d452cf5Sahrens 	struct destroydata dd = { 0 };
30711d452cf5Sahrens 
30721d452cf5Sahrens 	dd.snapname = snapname;
30731d452cf5Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
30741d452cf5Sahrens 
30751d452cf5Sahrens 	if (!dd.gotone) {
3076ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
30771d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
30781d452cf5Sahrens 		    zhp->zfs_name, snapname));
30791d452cf5Sahrens 	}
30801d452cf5Sahrens 
30811d452cf5Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3082e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
30831d452cf5Sahrens 
3084ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
30851d452cf5Sahrens 	if (ret != 0) {
30861d452cf5Sahrens 		char errbuf[1024];
30871d452cf5Sahrens 
30881d452cf5Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30891d452cf5Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
30901d452cf5Sahrens 
30911d452cf5Sahrens 		switch (errno) {
30921d452cf5Sahrens 		case EEXIST:
30931d452cf5Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
30941d452cf5Sahrens 			    "snapshot is cloned"));
30951d452cf5Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
30961d452cf5Sahrens 
30971d452cf5Sahrens 		default:
30981d452cf5Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
30991d452cf5Sahrens 			    errbuf));
31001d452cf5Sahrens 		}
31011d452cf5Sahrens 	}
31021d452cf5Sahrens 
31031d452cf5Sahrens 	return (0);
31041d452cf5Sahrens }
31051d452cf5Sahrens 
3106fa9e4066Sahrens /*
3107fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3108fa9e4066Sahrens  */
3109fa9e4066Sahrens int
3110e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3111fa9e4066Sahrens {
3112fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3113fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
3114fa9e4066Sahrens 	int ret;
311599653d4eSeschrock 	char errbuf[1024];
311699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3117e9dbad6fSeschrock 	zfs_type_t type;
3118e9dbad6fSeschrock 	uint64_t zoned;
3119fa9e4066Sahrens 
3120fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3121fa9e4066Sahrens 
312299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
312399653d4eSeschrock 	    "cannot create '%s'"), target);
312499653d4eSeschrock 
3125fa9e4066Sahrens 	/* validate the target name */
3126f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
312799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3128fa9e4066Sahrens 
3129fa9e4066Sahrens 	/* validate parents exist */
31307f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3131fa9e4066Sahrens 		return (-1);
3132fa9e4066Sahrens 
3133fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3134fa9e4066Sahrens 
3135fa9e4066Sahrens 	/* do the clone */
3136e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3137fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
31385f8e1617Snn35248 		type = ZFS_TYPE_VOLUME;
3139e9dbad6fSeschrock 	} else {
3140fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
31415f8e1617Snn35248 		type = ZFS_TYPE_FILESYSTEM;
3142e9dbad6fSeschrock 	}
3143e9dbad6fSeschrock 
3144e9dbad6fSeschrock 	if (props) {
3145990b4856Slling 		if ((props = zfs_validate_properties(hdl, type, props,
3146b1b8ab34Slling 		    zoned, zhp, errbuf)) == NULL)
3147e9dbad6fSeschrock 			return (-1);
3148e9dbad6fSeschrock 
3149990b4856Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3150e9dbad6fSeschrock 			nvlist_free(props);
3151e9dbad6fSeschrock 			return (-1);
3152e9dbad6fSeschrock 		}
3153e9dbad6fSeschrock 
3154e9dbad6fSeschrock 		nvlist_free(props);
3155e9dbad6fSeschrock 	}
3156fa9e4066Sahrens 
3157fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3158e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3159ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3160fa9e4066Sahrens 
3161e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
3162e9dbad6fSeschrock 
3163fa9e4066Sahrens 	if (ret != 0) {
3164fa9e4066Sahrens 		switch (errno) {
3165fa9e4066Sahrens 
3166fa9e4066Sahrens 		case ENOENT:
3167fa9e4066Sahrens 			/*
3168fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3169fa9e4066Sahrens 			 * above, but there may a race condition that has since
3170fa9e4066Sahrens 			 * destroyed the parent.
3171fa9e4066Sahrens 			 *
3172fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3173fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3174fa9e4066Sahrens 			 * dataset doesn't exist.
3175fa9e4066Sahrens 			 */
317699653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
317799653d4eSeschrock 			    "no such parent '%s'"), parent);
317899653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3179fa9e4066Sahrens 
318099653d4eSeschrock 		case EXDEV:
318199653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
318299653d4eSeschrock 			    "source and target pools differ"));
318399653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
318499653d4eSeschrock 			    errbuf));
318599653d4eSeschrock 
318699653d4eSeschrock 		default:
318799653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
318899653d4eSeschrock 			    errbuf));
318999653d4eSeschrock 		}
3190e9dbad6fSeschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
319199653d4eSeschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
319299653d4eSeschrock 	}
319399653d4eSeschrock 
319499653d4eSeschrock 	return (ret);
319599653d4eSeschrock }
319699653d4eSeschrock 
319799653d4eSeschrock typedef struct promote_data {
319899653d4eSeschrock 	char cb_mountpoint[MAXPATHLEN];
319999653d4eSeschrock 	const char *cb_target;
320099653d4eSeschrock 	const char *cb_errbuf;
320199653d4eSeschrock 	uint64_t cb_pivot_txg;
320299653d4eSeschrock } promote_data_t;
320399653d4eSeschrock 
320499653d4eSeschrock static int
320599653d4eSeschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
320699653d4eSeschrock {
320799653d4eSeschrock 	promote_data_t *pd = data;
320899653d4eSeschrock 	zfs_handle_t *szhp;
320999653d4eSeschrock 	char snapname[MAXPATHLEN];
32103ccfa83cSahrens 	int rv = 0;
321199653d4eSeschrock 
321299653d4eSeschrock 	/* We don't care about snapshots after the pivot point */
32133ccfa83cSahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32143ccfa83cSahrens 		zfs_close(zhp);
321599653d4eSeschrock 		return (0);
32163ccfa83cSahrens 	}
321799653d4eSeschrock 
32180b69c2f0Sahrens 	/* Remove the device link if it's a zvol. */
3219e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
32200b69c2f0Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
322199653d4eSeschrock 
322299653d4eSeschrock 	/* Check for conflicting names */
3223e9dbad6fSeschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3224e9dbad6fSeschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
322599653d4eSeschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
322699653d4eSeschrock 	if (szhp != NULL) {
322799653d4eSeschrock 		zfs_close(szhp);
322899653d4eSeschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
322999653d4eSeschrock 		    "snapshot name '%s' from origin \n"
323099653d4eSeschrock 		    "conflicts with '%s' from target"),
323199653d4eSeschrock 		    zhp->zfs_name, snapname);
32323ccfa83cSahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
323399653d4eSeschrock 	}
32343ccfa83cSahrens 	zfs_close(zhp);
32353ccfa83cSahrens 	return (rv);
323699653d4eSeschrock }
323799653d4eSeschrock 
32380b69c2f0Sahrens static int
32390b69c2f0Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
32400b69c2f0Sahrens {
32410b69c2f0Sahrens 	promote_data_t *pd = data;
32420b69c2f0Sahrens 
32430b69c2f0Sahrens 	/* We don't care about snapshots after the pivot point */
32443ccfa83cSahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
32450b69c2f0Sahrens 		/* Create the device link if it's a zvol. */
3246e9dbad6fSeschrock 		if (ZFS_IS_VOLUME(zhp))
32470b69c2f0Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
32483ccfa83cSahrens 	}
32490b69c2f0Sahrens 
32503ccfa83cSahrens 	zfs_close(zhp);
32510b69c2f0Sahrens 	return (0);
32520b69c2f0Sahrens }
32530b69c2f0Sahrens 
325499653d4eSeschrock /*
325599653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
325699653d4eSeschrock  */
325799653d4eSeschrock int
325899653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
325999653d4eSeschrock {
326099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
326199653d4eSeschrock 	zfs_cmd_t zc = { 0 };
326299653d4eSeschrock 	char parent[MAXPATHLEN];
326399653d4eSeschrock 	char *cp;
326499653d4eSeschrock 	int ret;
326599653d4eSeschrock 	zfs_handle_t *pzhp;
326699653d4eSeschrock 	promote_data_t pd;
326799653d4eSeschrock 	char errbuf[1024];
326899653d4eSeschrock 
326999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
327099653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
327199653d4eSeschrock 
327299653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
327399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
327499653d4eSeschrock 		    "snapshots can not be promoted"));
327599653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
327699653d4eSeschrock 	}
327799653d4eSeschrock 
32783cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
327999653d4eSeschrock 	if (parent[0] == '\0') {
328099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
328199653d4eSeschrock 		    "not a cloned filesystem"));
328299653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
328399653d4eSeschrock 	}
328499653d4eSeschrock 	cp = strchr(parent, '@');
328599653d4eSeschrock 	*cp = '\0';
328699653d4eSeschrock 
328799653d4eSeschrock 	/* Walk the snapshots we will be moving */
32883cb34c60Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
328999653d4eSeschrock 	if (pzhp == NULL)
329099653d4eSeschrock 		return (-1);
329199653d4eSeschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
329299653d4eSeschrock 	zfs_close(pzhp);
329399653d4eSeschrock 	pd.cb_target = zhp->zfs_name;
329499653d4eSeschrock 	pd.cb_errbuf = errbuf;
3295990b4856Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
329699653d4eSeschrock 	if (pzhp == NULL)
329799653d4eSeschrock 		return (-1);
329899653d4eSeschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
329999653d4eSeschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
330099653d4eSeschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33010b69c2f0Sahrens 	if (ret != 0) {
33020b69c2f0Sahrens 		zfs_close(pzhp);
330399653d4eSeschrock 		return (-1);
33040b69c2f0Sahrens 	}
330599653d4eSeschrock 
330699653d4eSeschrock 	/* issue the ioctl */
33073cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3308e9dbad6fSeschrock 	    sizeof (zc.zc_value));
330999653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3310ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
331199653d4eSeschrock 
331299653d4eSeschrock 	if (ret != 0) {
33130b69c2f0Sahrens 		int save_errno = errno;
3314fa9e4066Sahrens 
33150b69c2f0Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33160b69c2f0Sahrens 		zfs_close(pzhp);
33170b69c2f0Sahrens 
33180b69c2f0Sahrens 		switch (save_errno) {
3319fa9e4066Sahrens 		case EEXIST:
3320fa9e4066Sahrens 			/*
332199653d4eSeschrock 			 * There is a conflicting snapshot name.  We
332299653d4eSeschrock 			 * should have caught this above, but they could
332399653d4eSeschrock 			 * have renamed something in the mean time.
3324fa9e4066Sahrens 			 */
332599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
332699653d4eSeschrock 			    "conflicting snapshot name from parent '%s'"),
332799653d4eSeschrock 			    parent);
332899653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3329fa9e4066Sahrens 
3330fa9e4066Sahrens 		default:
33310b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3332fa9e4066Sahrens 		}
33330b69c2f0Sahrens 	} else {
33340b69c2f0Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3335fa9e4066Sahrens 	}
3336fa9e4066Sahrens 
33370b69c2f0Sahrens 	zfs_close(pzhp);
3338fa9e4066Sahrens 	return (ret);
3339fa9e4066Sahrens }
3340fa9e4066Sahrens 
3341cdf5b4caSmmusante struct createdata {
3342cdf5b4caSmmusante 	const char *cd_snapname;
3343cdf5b4caSmmusante 	int cd_ifexists;
3344cdf5b4caSmmusante };
3345cdf5b4caSmmusante 
33461d452cf5Sahrens static int
33471d452cf5Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
33481d452cf5Sahrens {
3349cdf5b4caSmmusante 	struct createdata *cd = arg;
3350e9dbad6fSeschrock 	int ret;
33511d452cf5Sahrens 
33521d452cf5Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33531d452cf5Sahrens 		char name[MAXPATHLEN];
33541d452cf5Sahrens 
3355e9dbad6fSeschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
3356e9dbad6fSeschrock 		(void) strlcat(name, "@", sizeof (name));
3357cdf5b4caSmmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
3358cdf5b4caSmmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
3359cdf5b4caSmmusante 		    cd->cd_ifexists);
33601d452cf5Sahrens 		/*
33611d452cf5Sahrens 		 * NB: this is simply a best-effort.  We don't want to
33621d452cf5Sahrens 		 * return an error, because then we wouldn't visit all
33631d452cf5Sahrens 		 * the volumes.
33641d452cf5Sahrens 		 */
33651d452cf5Sahrens 	}
3366e9dbad6fSeschrock 
3367cdf5b4caSmmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3368e9dbad6fSeschrock 
3369e9dbad6fSeschrock 	zfs_close(zhp);
3370e9dbad6fSeschrock 
3371e9dbad6fSeschrock 	return (ret);
33721d452cf5Sahrens }
33731d452cf5Sahrens 
3374fa9e4066Sahrens /*
337572bdce51Sahl  * Takes a snapshot of the given dataset.
3376fa9e4066Sahrens  */
3377fa9e4066Sahrens int
33781d452cf5Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3379fa9e4066Sahrens {
3380fa9e4066Sahrens 	const char *delim;
3381fa9e4066Sahrens 	char *parent;
3382fa9e4066Sahrens 	zfs_handle_t *zhp;
3383fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3384fa9e4066Sahrens 	int ret;
338599653d4eSeschrock 	char errbuf[1024];
3386fa9e4066Sahrens 
338799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
338899653d4eSeschrock 	    "cannot snapshot '%s'"), path);
338999653d4eSeschrock 
339099653d4eSeschrock 	/* validate the target name */
3391f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
339299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3393fa9e4066Sahrens 
3394fa9e4066Sahrens 	/* make sure the parent exists and is of the appropriate type */
33951d452cf5Sahrens 	delim = strchr(path, '@');
339699653d4eSeschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
339799653d4eSeschrock 		return (-1);
3398fa9e4066Sahrens 	(void) strncpy(parent, path, delim - path);
3399fa9e4066Sahrens 	parent[delim - path] = '\0';
3400fa9e4066Sahrens 
340199653d4eSeschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3402fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3403fa9e4066Sahrens 		free(parent);
3404fa9e4066Sahrens 		return (-1);
3405fa9e4066Sahrens 	}
3406fa9e4066Sahrens 
34071d452cf5Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3408e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3409ecd6cf80Smarks 	if (ZFS_IS_VOLUME(zhp))
3410ecd6cf80Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
3411ecd6cf80Smarks 	else
3412ecd6cf80Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34131d452cf5Sahrens 	zc.zc_cookie = recursive;
3414ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3415fa9e4066Sahrens 
34161d452cf5Sahrens 	/*
34171d452cf5Sahrens 	 * if it was recursive, the one that actually failed will be in
34181d452cf5Sahrens 	 * zc.zc_name.
34191d452cf5Sahrens 	 */
3420ecd6cf80Smarks 	if (ret != 0)
34211d452cf5Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3422e9dbad6fSeschrock 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3423ecd6cf80Smarks 
34241d452cf5Sahrens 	if (ret == 0 && recursive) {
3425cdf5b4caSmmusante 		struct createdata cd;
3426cdf5b4caSmmusante 
3427cdf5b4caSmmusante 		cd.cd_snapname = delim + 1;
3428cdf5b4caSmmusante 		cd.cd_ifexists = B_FALSE;
3429cdf5b4caSmmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
34301d452cf5Sahrens 	}
3431fa9e4066Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
343299653d4eSeschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
34331d452cf5Sahrens 		if (ret != 0) {
3434ecd6cf80Smarks 			(void) zfs_standard_error(hdl, errno,
3435ecd6cf80Smarks 			    dgettext(TEXT_DOMAIN,
3436ecd6cf80Smarks 			    "Volume successfully snapshotted, but device links "
3437ecd6cf80Smarks 			    "were not created"));
3438ecd6cf80Smarks 			free(parent);
3439ecd6cf80Smarks 			zfs_close(zhp);
3440ecd6cf80Smarks 			return (-1);
3441fa9e4066Sahrens 		}
34421d452cf5Sahrens 	}
3443fa9e4066Sahrens 
344499653d4eSeschrock 	if (ret != 0)
344599653d4eSeschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3446fa9e4066Sahrens 
3447fa9e4066Sahrens 	free(parent);
3448fa9e4066Sahrens 	zfs_close(zhp);
3449fa9e4066Sahrens 
3450fa9e4066Sahrens 	return (ret);
3451fa9e4066Sahrens }
3452fa9e4066Sahrens 
3453fa9e4066Sahrens /*
3454b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3455b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3456b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3457b12a1c38Slling  * group.
3458fa9e4066Sahrens  */
3459b12a1c38Slling typedef struct rollback_data {
3460b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3461b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3462c391e322Sahrens 	boolean_t	cb_error;
346399653d4eSeschrock 	boolean_t	cb_dependent;
3464c391e322Sahrens 	boolean_t	cb_force;
3465b12a1c38Slling } rollback_data_t;
3466b12a1c38Slling 
3467b12a1c38Slling static int
3468b12a1c38Slling rollback_destroy(zfs_handle_t *zhp, void *data)
3469b12a1c38Slling {
3470b12a1c38Slling 	rollback_data_t *cbp = data;
3471b12a1c38Slling 
3472b12a1c38Slling 	if (!cbp->cb_dependent) {
3473b12a1c38Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3474b12a1c38Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3475b12a1c38Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3476b12a1c38Slling 		    cbp->cb_create) {
3477ecd6cf80Smarks 			char *logstr;
3478b12a1c38Slling 
347999653d4eSeschrock 			cbp->cb_dependent = B_TRUE;
34804ccbb6e7Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
34814ccbb6e7Sahrens 			    rollback_destroy, cbp);
348299653d4eSeschrock 			cbp->cb_dependent = B_FALSE;
3483b12a1c38Slling 
3484ecd6cf80Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
3485ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
34864ccbb6e7Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
3487ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3488b12a1c38Slling 		}
3489b12a1c38Slling 	} else {
3490c391e322Sahrens 		/* We must destroy this clone; first unmount it */
3491c391e322Sahrens 		prop_changelist_t *clp;
3492c391e322Sahrens 
3493c391e322Sahrens 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
3494c391e322Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
3495c391e322Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
3496c391e322Sahrens 			cbp->cb_error = B_TRUE;
3497c391e322Sahrens 			zfs_close(zhp);
3498c391e322Sahrens 			return (0);
3499c391e322Sahrens 		}
3500c391e322Sahrens 		if (zfs_destroy(zhp) != 0)
3501c391e322Sahrens 			cbp->cb_error = B_TRUE;
3502c391e322Sahrens 		else
3503c391e322Sahrens 			changelist_remove(clp, zhp->zfs_name);
3504ba7b046eSahrens 		(void) changelist_postfix(clp);
3505c391e322Sahrens 		changelist_free(clp);
3506b12a1c38Slling 	}
3507b12a1c38Slling 
3508b12a1c38Slling 	zfs_close(zhp);
3509b12a1c38Slling 	return (0);
3510b12a1c38Slling }
3511b12a1c38Slling 
3512b12a1c38Slling /*
35134ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35144ccbb6e7Sahrens  * data changes since then and making it the active dataset.
35154ccbb6e7Sahrens  *
35164ccbb6e7Sahrens  * Any snapshots more recent than the target are destroyed, along with
35174ccbb6e7Sahrens  * their dependents.
3518b12a1c38Slling  */
35194ccbb6e7Sahrens int
3520c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3521fa9e4066Sahrens {
35224ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
35234ccbb6e7Sahrens 	int err;
3524fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
35257b97dc1aSrm160521 	boolean_t restore_resv = 0;
35267b97dc1aSrm160521 	uint64_t old_volsize, new_volsize;
35277b97dc1aSrm160521 	zfs_prop_t resv_prop;
3528fa9e4066Sahrens 
3529fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3530fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3531fa9e4066Sahrens 
35324ccbb6e7Sahrens 	/*
35334ccbb6e7Sahrens 	 * Destroy all recent snapshots and its dependends.
35344ccbb6e7Sahrens 	 */
3535c391e322Sahrens 	cb.cb_force = force;
35364ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
35374ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
35384ccbb6e7Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
35394ccbb6e7Sahrens 
3540c391e322Sahrens 	if (cb.cb_error)
3541c391e322Sahrens 		return (-1);
35424ccbb6e7Sahrens 
35434ccbb6e7Sahrens 	/*
35444ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
35454ccbb6e7Sahrens 	 * rollback to the given snapshot.
35464ccbb6e7Sahrens 	 */
35474ccbb6e7Sahrens 
35487b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
35497b97dc1aSrm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3550fa9e4066Sahrens 			return (-1);
35517b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
35527b97dc1aSrm160521 			return (-1);
35537b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35547b97dc1aSrm160521 		restore_resv =
35557b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
35567b97dc1aSrm160521 	}
3557fa9e4066Sahrens 
3558fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3559fa9e4066Sahrens 
3560e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3561fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3562fa9e4066Sahrens 	else
3563fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3564fa9e4066Sahrens 
3565fa9e4066Sahrens 	/*
35664ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
35674ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
35684ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
35694ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
35704ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
35717b97dc1aSrm160521 	 *
3572fa9e4066Sahrens 	 */
35734ccbb6e7Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3574ece3d9b3Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
357599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
357699653d4eSeschrock 		    zhp->zfs_name);
3577b9415e83Srm160521 		return (err);
3578b9415e83Srm160521 	}
3579fa9e4066Sahrens 
35807b97dc1aSrm160521 	/*
35817b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
35827b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
35837b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
35847b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
35857b97dc1aSrm160521 	 */
3586b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3587b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3588b9415e83Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
3589b9415e83Srm160521 			zfs_close(zhp);
35907b97dc1aSrm160521 			return (err);
3591b9415e83Srm160521 		}
35927b97dc1aSrm160521 		if (restore_resv) {
35937b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35947b97dc1aSrm160521 			if (old_volsize != new_volsize)
3595b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
3596b9415e83Srm160521 				    new_volsize);
35977b97dc1aSrm160521 		}
35987b97dc1aSrm160521 		zfs_close(zhp);
35997b97dc1aSrm160521 	}
36004ccbb6e7Sahrens 	return (err);
3601b12a1c38Slling }
3602b12a1c38Slling 
3603b12a1c38Slling /*
3604fa9e4066Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3605fa9e4066Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3606fa9e4066Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3607fa9e4066Sahrens  * libzfs_graph.c.
3608fa9e4066Sahrens  */
3609fa9e4066Sahrens int
36103bb79becSeschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36113bb79becSeschrock     zfs_iter_f func, void *data)
3612fa9e4066Sahrens {
3613fa9e4066Sahrens 	char **dependents;
3614fa9e4066Sahrens 	size_t count;
3615fa9e4066Sahrens 	int i;
3616fa9e4066Sahrens 	zfs_handle_t *child;
3617fa9e4066Sahrens 	int ret = 0;
3618fa9e4066Sahrens 
36193bb79becSeschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
36203bb79becSeschrock 	    &dependents, &count) != 0)
36213bb79becSeschrock 		return (-1);
36223bb79becSeschrock 
3623fa9e4066Sahrens 	for (i = 0; i < count; i++) {
362499653d4eSeschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
362599653d4eSeschrock 		    dependents[i])) == NULL)
3626fa9e4066Sahrens 			continue;
3627fa9e4066Sahrens 
3628fa9e4066Sahrens 		if ((ret = func(child, data)) != 0)
3629fa9e4066Sahrens 			break;
3630fa9e4066Sahrens 	}
3631fa9e4066Sahrens 
3632fa9e4066Sahrens 	for (i = 0; i < count; i++)
3633fa9e4066Sahrens 		free(dependents[i]);
3634fa9e4066Sahrens 	free(dependents);
3635fa9e4066Sahrens 
3636fa9e4066Sahrens 	return (ret);
3637fa9e4066Sahrens }
3638fa9e4066Sahrens 
3639fa9e4066Sahrens /*
3640fa9e4066Sahrens  * Renames the given dataset.
3641fa9e4066Sahrens  */
3642fa9e4066Sahrens int
36437f1f55eaSvb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3644fa9e4066Sahrens {
3645fa9e4066Sahrens 	int ret;
3646fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3647fa9e4066Sahrens 	char *delim;
3648cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3649cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3650cdf5b4caSmmusante 	char *parentname = NULL;
3651fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
365299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
365399653d4eSeschrock 	char errbuf[1024];
3654fa9e4066Sahrens 
3655fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3656fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3657fa9e4066Sahrens 		return (0);
3658fa9e4066Sahrens 
365999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
366099653d4eSeschrock 	    "cannot rename to '%s'"), target);
366199653d4eSeschrock 
3662fa9e4066Sahrens 	/*
3663fa9e4066Sahrens 	 * Make sure the target name is valid
3664fa9e4066Sahrens 	 */
3665fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
366698579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
366798579b20Snd150628 		    *target == '@') {
366898579b20Snd150628 			/*
366998579b20Snd150628 			 * Snapshot target name is abbreviated,
367098579b20Snd150628 			 * reconstruct full dataset name
367198579b20Snd150628 			 */
367298579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
367398579b20Snd150628 			    sizeof (parent));
367498579b20Snd150628 			delim = strchr(parent, '@');
367598579b20Snd150628 			if (strchr(target, '@') == NULL)
367698579b20Snd150628 				*(++delim) = '\0';
367798579b20Snd150628 			else
367898579b20Snd150628 				*delim = '\0';
367998579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
368098579b20Snd150628 			target = parent;
368198579b20Snd150628 		} else {
3682fa9e4066Sahrens 			/*
3683fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
3684fa9e4066Sahrens 			 */
368598579b20Snd150628 			delim = strchr(target, '@');
368698579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
368798579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
368899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
368998579b20Snd150628 				    "snapshots must be part of same "
369098579b20Snd150628 				    "dataset"));
369198579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
369298579b20Snd150628 				    errbuf));
3693fa9e4066Sahrens 			}
369498579b20Snd150628 		}
3695f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
369698579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3697fa9e4066Sahrens 	} else {
3698cdf5b4caSmmusante 		if (recursive) {
3699cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3700cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
3701cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3702cdf5b4caSmmusante 		}
3703cdf5b4caSmmusante 
3704f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
370598579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3706e9dbad6fSeschrock 		uint64_t unused;
3707e9dbad6fSeschrock 
3708fa9e4066Sahrens 		/* validate parents */
37097f1f55eaSvb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3710fa9e4066Sahrens 			return (-1);
3711fa9e4066Sahrens 
3712fa9e4066Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3713fa9e4066Sahrens 
3714fa9e4066Sahrens 		/* make sure we're in the same pool */
3715fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3716fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3717fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
371899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
371999653d4eSeschrock 			    "datasets must be within same pool"));
372099653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3721fa9e4066Sahrens 		}
3722f2fdf992Snd150628 
3723f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
3724f2fdf992Snd150628 		if (strncmp(parent, zhp->zfs_name,
3725f2fdf992Snd150628 		    strlen(zhp->zfs_name)) == 0) {
3726f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3727f2fdf992Snd150628 			    "New dataset name cannot be a descendent of "
3728f2fdf992Snd150628 			    "current dataset name"));
3729f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3730f2fdf992Snd150628 		}
3731fa9e4066Sahrens 	}
3732fa9e4066Sahrens 
373399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
373499653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
373599653d4eSeschrock 
3736fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3737fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
373899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
373999653d4eSeschrock 		    "dataset is used in a non-global zone"));
374099653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3741fa9e4066Sahrens 	}
3742fa9e4066Sahrens 
3743cdf5b4caSmmusante 	if (recursive) {
3744cdf5b4caSmmusante 		struct destroydata dd;
3745cdf5b4caSmmusante 
3746f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3747f0c5ee21Smmusante 		if (parentname == NULL) {
3748f0c5ee21Smmusante 			ret = -1;
3749f0c5ee21Smmusante 			goto error;
3750f0c5ee21Smmusante 		}
3751cdf5b4caSmmusante 		delim = strchr(parentname, '@');
3752cdf5b4caSmmusante 		*delim = '\0';
3753990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3754cdf5b4caSmmusante 		if (zhrp == NULL) {
3755f0c5ee21Smmusante 			ret = -1;
3756f0c5ee21Smmusante 			goto error;
3757cdf5b4caSmmusante 		}
3758cdf5b4caSmmusante 
3759cdf5b4caSmmusante 		dd.snapname = delim + 1;
3760cdf5b4caSmmusante 		dd.gotone = B_FALSE;
3761f0c5ee21Smmusante 		dd.closezhp = B_TRUE;
3762cdf5b4caSmmusante 
3763cdf5b4caSmmusante 		/* We remove any zvol links prior to renaming them */
3764cdf5b4caSmmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
3765cdf5b4caSmmusante 		if (ret) {
3766cdf5b4caSmmusante 			goto error;
3767cdf5b4caSmmusante 		}
3768cdf5b4caSmmusante 	} else {
3769fa9e4066Sahrens 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
377099653d4eSeschrock 			return (-1);
3771fa9e4066Sahrens 
3772fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
377399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
377499653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
377599653d4eSeschrock 			    "in a non-global zone"));
3776e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3777fa9e4066Sahrens 			goto error;
3778fa9e4066Sahrens 		}
3779fa9e4066Sahrens 
3780fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
3781fa9e4066Sahrens 			goto error;
3782cdf5b4caSmmusante 	}
3783fa9e4066Sahrens 
3784e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3785fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3786fa9e4066Sahrens 	else
3787fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3788fa9e4066Sahrens 
378998579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3790e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
379198579b20Snd150628 
3792cdf5b4caSmmusante 	zc.zc_cookie = recursive;
3793cdf5b4caSmmusante 
3794ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3795cdf5b4caSmmusante 		/*
3796cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
3797cdf5b4caSmmusante 		 * be in zc.zc_name
3798cdf5b4caSmmusante 		 */
3799cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38003cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
3801cdf5b4caSmmusante 
3802cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
3803cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3804cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
3805cdf5b4caSmmusante 			    "with the new name"));
3806a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3807cdf5b4caSmmusante 		} else {
380899653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3809cdf5b4caSmmusante 		}
3810fa9e4066Sahrens 
3811fa9e4066Sahrens 		/*
3812fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
3813fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
3814fa9e4066Sahrens 		 */
3815cdf5b4caSmmusante 		if (recursive) {
3816cdf5b4caSmmusante 			struct createdata cd;
3817cdf5b4caSmmusante 
3818cdf5b4caSmmusante 			/* only create links for datasets that had existed */
3819cdf5b4caSmmusante 			cd.cd_snapname = delim + 1;
3820cdf5b4caSmmusante 			cd.cd_ifexists = B_TRUE;
3821cdf5b4caSmmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3822cdf5b4caSmmusante 			    &cd);
3823cdf5b4caSmmusante 		} else {
3824fa9e4066Sahrens 			(void) changelist_postfix(cl);
3825cdf5b4caSmmusante 		}
3826cdf5b4caSmmusante 	} else {
3827cdf5b4caSmmusante 		if (recursive) {
3828cdf5b4caSmmusante 			struct createdata cd;
3829cdf5b4caSmmusante 
3830cdf5b4caSmmusante 			/* only create links for datasets that had existed */
3831cdf5b4caSmmusante 			cd.cd_snapname = strchr(target, '@') + 1;
3832cdf5b4caSmmusante 			cd.cd_ifexists = B_TRUE;
3833cdf5b4caSmmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3834cdf5b4caSmmusante 			    &cd);
3835fa9e4066Sahrens 		} else {
3836fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
3837fa9e4066Sahrens 			ret = changelist_postfix(cl);
3838fa9e4066Sahrens 		}
3839cdf5b4caSmmusante 	}
3840fa9e4066Sahrens 
3841fa9e4066Sahrens error:
3842cdf5b4caSmmusante 	if (parentname) {
3843cdf5b4caSmmusante 		free(parentname);
3844cdf5b4caSmmusante 	}
3845cdf5b4caSmmusante 	if (zhrp) {
3846cdf5b4caSmmusante 		zfs_close(zhrp);
3847cdf5b4caSmmusante 	}
3848cdf5b4caSmmusante 	if (cl) {
3849fa9e4066Sahrens 		changelist_free(cl);
3850cdf5b4caSmmusante 	}
3851fa9e4066Sahrens 	return (ret);
3852fa9e4066Sahrens }
3853fa9e4066Sahrens 
3854fa9e4066Sahrens /*
3855fa9e4066Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3856fa9e4066Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3857fa9e4066Sahrens  */
3858fa9e4066Sahrens int
385999653d4eSeschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3860fa9e4066Sahrens {
3861cdf5b4caSmmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
3862cdf5b4caSmmusante }
3863cdf5b4caSmmusante 
3864cdf5b4caSmmusante static int
3865cdf5b4caSmmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3866cdf5b4caSmmusante {
3867fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
386899653d4eSeschrock 	di_devlink_handle_t dhdl;
3869ecd6cf80Smarks 	priv_set_t *priv_effective;
3870ecd6cf80Smarks 	int privileged;
3871fa9e4066Sahrens 
3872fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3873fa9e4066Sahrens 
3874fa9e4066Sahrens 	/*
3875fa9e4066Sahrens 	 * Issue the appropriate ioctl.
3876fa9e4066Sahrens 	 */
387799653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3878fa9e4066Sahrens 		switch (errno) {
3879fa9e4066Sahrens 		case EEXIST:
3880fa9e4066Sahrens 			/*
3881fa9e4066Sahrens 			 * Silently ignore the case where the link already
3882fa9e4066Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3883fa9e4066Sahrens 			 * times without errors.
3884fa9e4066Sahrens 			 */
3885fa9e4066Sahrens 			return (0);
3886fa9e4066Sahrens 
3887cdf5b4caSmmusante 		case ENOENT:
3888cdf5b4caSmmusante 			/*
3889cdf5b4caSmmusante 			 * Dataset does not exist in the kernel.  If we
3890cdf5b4caSmmusante 			 * don't care (see zfs_rename), then ignore the
3891cdf5b4caSmmusante 			 * error quietly.
3892cdf5b4caSmmusante 			 */
3893cdf5b4caSmmusante 			if (ifexists) {
3894cdf5b4caSmmusante 				return (0);
3895cdf5b4caSmmusante 			}
3896cdf5b4caSmmusante 
3897cdf5b4caSmmusante 			/* FALLTHROUGH */
3898cdf5b4caSmmusante 
3899fa9e4066Sahrens 		default:
3900ece3d9b3Slling 			return (zfs_standard_error_fmt(hdl, errno,
390199653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
390299653d4eSeschrock 			    "for '%s'"), dataset));
3903fa9e4066Sahrens 		}
3904fa9e4066Sahrens 	}
3905fa9e4066Sahrens 
3906fa9e4066Sahrens 	/*
3907ecd6cf80Smarks 	 * If privileged call devfsadm and wait for the links to
3908ecd6cf80Smarks 	 * magically appear.
3909ecd6cf80Smarks 	 * Otherwise, print out an informational message.
3910fa9e4066Sahrens 	 */
3911ecd6cf80Smarks 
3912ecd6cf80Smarks 	priv_effective = priv_allocset();
3913ecd6cf80Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3914ecd6cf80Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3915ecd6cf80Smarks 	priv_freeset(priv_effective);
3916ecd6cf80Smarks 
3917ecd6cf80Smarks 	if (privileged) {
3918ecd6cf80Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
3919ecd6cf80Smarks 		    DI_MAKE_LINK)) == NULL) {
392099653d4eSeschrock 			zfs_error_aux(hdl, strerror(errno));
3921ecd6cf80Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
392299653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
392399653d4eSeschrock 			    "for '%s'"), dataset);
392499653d4eSeschrock 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3925fa9e4066Sahrens 			return (-1);
3926fa9e4066Sahrens 		} else {
392799653d4eSeschrock 			(void) di_devlink_fini(&dhdl);
3928fa9e4066Sahrens 		}
3929ecd6cf80Smarks 	} else {
3930ecd6cf80Smarks 		char pathname[MAXPATHLEN];
3931ecd6cf80Smarks 		struct stat64 statbuf;
3932ecd6cf80Smarks 		int i;
3933ecd6cf80Smarks 
3934ecd6cf80Smarks #define	MAX_WAIT	10
3935ecd6cf80Smarks 
3936ecd6cf80Smarks 		/*
3937ecd6cf80Smarks 		 * This is the poor mans way of waiting for the link
3938ecd6cf80Smarks 		 * to show up.  If after 10 seconds we still don't
3939ecd6cf80Smarks 		 * have it, then print out a message.
3940ecd6cf80Smarks 		 */
3941ecd6cf80Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
3942ecd6cf80Smarks 		    dataset);
3943ecd6cf80Smarks 
3944ecd6cf80Smarks 		for (i = 0; i != MAX_WAIT; i++) {
3945ecd6cf80Smarks 			if (stat64(pathname, &statbuf) == 0)
3946ecd6cf80Smarks 				break;
3947ecd6cf80Smarks 			(void) sleep(1);
3948ecd6cf80Smarks 		}
3949ecd6cf80Smarks 		if (i == MAX_WAIT)
3950ecd6cf80Smarks 			(void) printf(gettext("%s may not be immediately "
3951ecd6cf80Smarks 			    "available\n"), pathname);
3952ecd6cf80Smarks 	}
3953fa9e4066Sahrens 
3954fa9e4066Sahrens 	return (0);
3955fa9e4066Sahrens }
3956fa9e4066Sahrens 
3957fa9e4066Sahrens /*
3958fa9e4066Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3959fa9e4066Sahrens  */
3960fa9e4066Sahrens int
396199653d4eSeschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3962fa9e4066Sahrens {
3963fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3964fa9e4066Sahrens 
3965fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3966fa9e4066Sahrens 
396799653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3968fa9e4066Sahrens 		switch (errno) {
3969fa9e4066Sahrens 		case ENXIO:
3970fa9e4066Sahrens 			/*
3971fa9e4066Sahrens 			 * Silently ignore the case where the link no longer
3972fa9e4066Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3973fa9e4066Sahrens 			 * times without errors.
3974fa9e4066Sahrens 			 */
3975fa9e4066Sahrens 			return (0);
3976fa9e4066Sahrens 
3977fa9e4066Sahrens 		default:
3978ece3d9b3Slling 			return (zfs_standard_error_fmt(hdl, errno,
397999653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
398099653d4eSeschrock 			    "links for '%s'"), dataset));
3981fa9e4066Sahrens 		}
3982fa9e4066Sahrens 	}
3983fa9e4066Sahrens 
3984fa9e4066Sahrens 	return (0);
3985fa9e4066Sahrens }
3986e9dbad6fSeschrock 
3987e9dbad6fSeschrock nvlist_t *
3988e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
3989e9dbad6fSeschrock {
3990e9dbad6fSeschrock 	return (zhp->zfs_user_props);
3991e9dbad6fSeschrock }
3992e9dbad6fSeschrock 
3993e9dbad6fSeschrock /*
3994e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
3995e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
3996e9dbad6fSeschrock  *
3997e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
3998e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
3999e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
4000e9dbad6fSeschrock  *
4001e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
4002e9dbad6fSeschrock  *        so that we can size the column appropriately.
4003e9dbad6fSeschrock  */
4004e9dbad6fSeschrock int
4005990b4856Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
4006e9dbad6fSeschrock {
4007e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4008990b4856Slling 	zprop_list_t *entry;
4009990b4856Slling 	zprop_list_t **last, **start;
4010e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
4011e9dbad6fSeschrock 	nvpair_t *elem;
4012e9dbad6fSeschrock 	char *strval;
4013e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
4014e9dbad6fSeschrock 
4015990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4016e9dbad6fSeschrock 		return (-1);
4017e9dbad6fSeschrock 
4018e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4019e9dbad6fSeschrock 
4020e9dbad6fSeschrock 	entry = *plp;
4021e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4022e9dbad6fSeschrock 		/*
4023e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4024e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4025e9dbad6fSeschrock 		 * non-native property.
4026e9dbad6fSeschrock 		 */
4027e9dbad6fSeschrock 		start = plp;
4028e9dbad6fSeschrock 		while (*start != NULL) {
4029990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4030e9dbad6fSeschrock 				break;
4031e9dbad6fSeschrock 			start = &(*start)->pl_next;
4032e9dbad6fSeschrock 		}
4033e9dbad6fSeschrock 
4034e9dbad6fSeschrock 		elem = NULL;
4035e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4036e9dbad6fSeschrock 			/*
4037e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4038e9dbad6fSeschrock 			 */
4039e9dbad6fSeschrock 			for (last = start; *last != NULL;
4040e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4041e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4042e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4043e9dbad6fSeschrock 					break;
4044e9dbad6fSeschrock 			}
4045e9dbad6fSeschrock 
4046e9dbad6fSeschrock 			if (*last == NULL) {
4047e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4048990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4049e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4050e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4051e9dbad6fSeschrock 					free(entry);
4052e9dbad6fSeschrock 					return (-1);
4053e9dbad6fSeschrock 				}
4054e9dbad6fSeschrock 
4055990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4056e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4057e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4058e9dbad6fSeschrock 				*last = entry;
4059e9dbad6fSeschrock 			}
4060e9dbad6fSeschrock 		}
4061e9dbad6fSeschrock 	}
4062e9dbad6fSeschrock 
4063e9dbad6fSeschrock 	/*
4064e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4065e9dbad6fSeschrock 	 */
4066e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4067e9dbad6fSeschrock 		if (entry->pl_fixed)
4068e9dbad6fSeschrock 			continue;
4069e9dbad6fSeschrock 
4070990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4071e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
4072e9dbad6fSeschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4073e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4074e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4075e9dbad6fSeschrock 			}
4076e9dbad6fSeschrock 		} else if (nvlist_lookup_nvlist(userprops,
4077e9dbad6fSeschrock 		    entry->pl_user_prop, &propval)  == 0) {
4078e9dbad6fSeschrock 			verify(nvlist_lookup_string(propval,
4079990b4856Slling 			    ZPROP_VALUE, &strval) == 0);
4080e9dbad6fSeschrock 			if (strlen(strval) > entry->pl_width)
4081e9dbad6fSeschrock 				entry->pl_width = strlen(strval);
4082e9dbad6fSeschrock 		}
4083e9dbad6fSeschrock 	}
4084e9dbad6fSeschrock 
4085e9dbad6fSeschrock 	return (0);
4086e9dbad6fSeschrock }
4087ecd6cf80Smarks 
4088ecd6cf80Smarks int
4089ecd6cf80Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
4090ecd6cf80Smarks {
4091ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4092ecd6cf80Smarks 	nvlist_t *nvp;
4093ecd6cf80Smarks 	gid_t gid;
4094ecd6cf80Smarks 	uid_t uid;
4095ecd6cf80Smarks 	const gid_t *groups;
4096ecd6cf80Smarks 	int group_cnt;
4097ecd6cf80Smarks 	int error;
4098ecd6cf80Smarks 
4099ecd6cf80Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
4100ecd6cf80Smarks 		return (no_memory(hdl));
4101ecd6cf80Smarks 
4102ecd6cf80Smarks 	uid = ucred_geteuid(cred);
4103ecd6cf80Smarks 	gid = ucred_getegid(cred);
4104ecd6cf80Smarks 	group_cnt = ucred_getgroups(cred, &groups);
4105ecd6cf80Smarks 
4106ecd6cf80Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
4107ecd6cf80Smarks 		return (1);
4108ecd6cf80Smarks 
4109ecd6cf80Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
4110ecd6cf80Smarks 		nvlist_free(nvp);
4111ecd6cf80Smarks 		return (1);
4112ecd6cf80Smarks 	}
4113ecd6cf80Smarks 
4114ecd6cf80Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
4115ecd6cf80Smarks 		nvlist_free(nvp);
4116ecd6cf80Smarks 		return (1);
4117ecd6cf80Smarks 	}
4118ecd6cf80Smarks 
4119ecd6cf80Smarks 	if (nvlist_add_uint32_array(nvp,
4120ecd6cf80Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
4121ecd6cf80Smarks 		nvlist_free(nvp);
4122ecd6cf80Smarks 		return (1);
4123ecd6cf80Smarks 	}
4124ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4125ecd6cf80Smarks 
4126990b4856Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
4127ecd6cf80Smarks 		return (-1);
4128ecd6cf80Smarks 
4129ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
4130ecd6cf80Smarks 	nvlist_free(nvp);
4131ecd6cf80Smarks 	return (error);
4132ecd6cf80Smarks }
4133ecd6cf80Smarks 
4134ecd6cf80Smarks int
4135ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4136da6c28aaSamw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
4137ecd6cf80Smarks {
4138ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4139ecd6cf80Smarks 	int error;
4140ecd6cf80Smarks 
4141ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4142ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4143ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4144ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4145da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4146ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4147ecd6cf80Smarks 
4148ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4149ecd6cf80Smarks 	return (error);
4150ecd6cf80Smarks }
4151