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 /*
2336db6475SEric Taylor  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24bb6e7075SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
25f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
26013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
27a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
2843d68d68SYuri Pankov  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <ctype.h>
32fa9e4066Sahrens #include <errno.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>
43b12a1c38Slling #include <sys/mount.h>
44ecd6cf80Smarks #include <priv.h>
45ecd6cf80Smarks #include <pwd.h>
46ecd6cf80Smarks #include <grp.h>
47ecd6cf80Smarks #include <stddef.h>
48ecd6cf80Smarks #include <ucred.h>
4914843421SMatthew Ahrens #include <idmap.h>
5014843421SMatthew Ahrens #include <aclutils.h>
513b12c289SMatthew Ahrens #include <directory.h>
52fa9e4066Sahrens 
53c1449561SEric Taylor #include <sys/dnode.h>
54fa9e4066Sahrens #include <sys/spa.h>
55e9dbad6fSeschrock #include <sys/zap.h>
56fa9e4066Sahrens #include <libzfs.h>
57fa9e4066Sahrens 
58fa9e4066Sahrens #include "zfs_namecheck.h"
59fa9e4066Sahrens #include "zfs_prop.h"
60fa9e4066Sahrens #include "libzfs_impl.h"
61ecd6cf80Smarks #include "zfs_deleg.h"
62fa9e4066Sahrens 
6314843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6414843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
65cdf5b4caSmmusante 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
68fa9e4066Sahrens  * readable form.
69fa9e4066Sahrens  */
70fa9e4066Sahrens const char *
71fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
72fa9e4066Sahrens {
73fa9e4066Sahrens 	switch (type) {
74fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
75fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
76fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
77fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
78fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
79fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
80fa9e4066Sahrens 	}
81fa9e4066Sahrens 
82fa9e4066Sahrens 	return (NULL);
83fa9e4066Sahrens }
84fa9e4066Sahrens 
85fa9e4066Sahrens /*
86fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
87fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
88fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
89fa9e4066Sahrens  * acceptable types.
90fa9e4066Sahrens  */
91fa9e4066Sahrens static const char *
92fa9e4066Sahrens path_to_str(const char *path, int types)
93fa9e4066Sahrens {
94fa9e4066Sahrens 	/*
95fa9e4066Sahrens 	 * When given a single type, always report the exact type.
96fa9e4066Sahrens 	 */
97fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
98fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
99fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
100fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
101fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
102fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
103fa9e4066Sahrens 
104fa9e4066Sahrens 	/*
105fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
106fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
107fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
108fa9e4066Sahrens 	 * snapshot attribute and try again.
109fa9e4066Sahrens 	 */
110fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
111fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
112fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
113fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
114fa9e4066Sahrens 	}
115fa9e4066Sahrens 
116fa9e4066Sahrens 	/*
117fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
118fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
119fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
120fa9e4066Sahrens 	 */
121fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
122fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
123fa9e4066Sahrens 
124fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
125fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
126fa9e4066Sahrens }
127fa9e4066Sahrens 
128fa9e4066Sahrens /*
129fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
13014843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
13114843421SMatthew Ahrens  * explain exactly why the name was not valid.
132fa9e4066Sahrens  */
13399d5e173STim Haley int
134f18faf3fSek zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
135f18faf3fSek     boolean_t modifying)
136fa9e4066Sahrens {
137fa9e4066Sahrens 	namecheck_err_t why;
138fa9e4066Sahrens 	char what;
139fa9e4066Sahrens 
1401af68beaSAlexander Stetsenko 	(void) zfs_prop_get_table();
141fa9e4066Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
14299653d4eSeschrock 		if (hdl != NULL) {
143fa9e4066Sahrens 			switch (why) {
144b81d61a6Slling 			case NAME_ERR_TOOLONG:
14599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14699653d4eSeschrock 				    "name is too long"));
147b81d61a6Slling 				break;
148b81d61a6Slling 
149fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
15099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15199653d4eSeschrock 				    "leading slash in name"));
152fa9e4066Sahrens 				break;
153fa9e4066Sahrens 
154fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15699653d4eSeschrock 				    "empty component in name"));
157fa9e4066Sahrens 				break;
158fa9e4066Sahrens 
159fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
16099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16199653d4eSeschrock 				    "trailing slash in name"));
162fa9e4066Sahrens 				break;
163fa9e4066Sahrens 
164fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16599653d4eSeschrock 				zfs_error_aux(hdl,
166fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16799653d4eSeschrock 				    "'%c' in name"), what);
168fa9e4066Sahrens 				break;
169fa9e4066Sahrens 
170fa9e4066Sahrens 			case NAME_ERR_MULTIPLE_AT:
17199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17299653d4eSeschrock 				    "multiple '@' delimiters in name"));
173fa9e4066Sahrens 				break;
1745ad82045Snd 
1755ad82045Snd 			case NAME_ERR_NOLETTER:
1765ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1775ad82045Snd 				    "pool doesn't begin with a letter"));
1785ad82045Snd 				break;
1795ad82045Snd 
1805ad82045Snd 			case NAME_ERR_RESERVED:
1815ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825ad82045Snd 				    "name is reserved"));
1835ad82045Snd 				break;
1845ad82045Snd 
1855ad82045Snd 			case NAME_ERR_DISKLIKE:
1865ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1875ad82045Snd 				    "reserved disk name"));
1885ad82045Snd 				break;
189fa9e4066Sahrens 			}
190fa9e4066Sahrens 		}
191fa9e4066Sahrens 
192fa9e4066Sahrens 		return (0);
193fa9e4066Sahrens 	}
194fa9e4066Sahrens 
195fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19699653d4eSeschrock 		if (hdl != NULL)
19799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19899653d4eSeschrock 			    "snapshot delimiter '@' in filesystem name"));
199fa9e4066Sahrens 		return (0);
200fa9e4066Sahrens 	}
201fa9e4066Sahrens 
2021d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2031d452cf5Sahrens 		if (hdl != NULL)
2041d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2061d452cf5Sahrens 		return (0);
2071d452cf5Sahrens 	}
2081d452cf5Sahrens 
209f18faf3fSek 	if (modifying && strchr(path, '%') != NULL) {
210f18faf3fSek 		if (hdl != NULL)
211f18faf3fSek 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
212f18faf3fSek 			    "invalid character %c in name"), '%');
213f18faf3fSek 		return (0);
214f18faf3fSek 	}
215f18faf3fSek 
21699653d4eSeschrock 	return (-1);
217fa9e4066Sahrens }
218fa9e4066Sahrens 
219fa9e4066Sahrens int
220fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
221fa9e4066Sahrens {
222e7cbe64fSgw 	if (type == ZFS_TYPE_POOL)
223e7cbe64fSgw 		return (zpool_name_valid(NULL, B_FALSE, name));
224f18faf3fSek 	return (zfs_validate_name(NULL, name, type, B_FALSE));
225fa9e4066Sahrens }
226fa9e4066Sahrens 
227e9dbad6fSeschrock /*
228e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
229e9dbad6fSeschrock  * properties into a separate nvlist.
230e9dbad6fSeschrock  */
231fac3008cSeschrock static nvlist_t *
232fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
233e9dbad6fSeschrock {
234e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
235e9dbad6fSeschrock 	nvpair_t *elem;
236e9dbad6fSeschrock 	nvlist_t *propval;
237fac3008cSeschrock 	nvlist_t *nvl;
238e9dbad6fSeschrock 
239fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
240fac3008cSeschrock 		(void) no_memory(hdl);
241fac3008cSeschrock 		return (NULL);
242fac3008cSeschrock 	}
243e9dbad6fSeschrock 
244e9dbad6fSeschrock 	elem = NULL;
245fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
246e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
247e9dbad6fSeschrock 			continue;
248e9dbad6fSeschrock 
249e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
250fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
251fac3008cSeschrock 			nvlist_free(nvl);
252fac3008cSeschrock 			(void) no_memory(hdl);
253fac3008cSeschrock 			return (NULL);
254fac3008cSeschrock 		}
255e9dbad6fSeschrock 	}
256e9dbad6fSeschrock 
257fac3008cSeschrock 	return (nvl);
258e9dbad6fSeschrock }
259e9dbad6fSeschrock 
26029ab75c9Srm static zpool_handle_t *
26129ab75c9Srm zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
26229ab75c9Srm {
26329ab75c9Srm 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26429ab75c9Srm 	zpool_handle_t *zph;
26529ab75c9Srm 
26629ab75c9Srm 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
26729ab75c9Srm 		if (hdl->libzfs_pool_handles != NULL)
26829ab75c9Srm 			zph->zpool_next = hdl->libzfs_pool_handles;
26929ab75c9Srm 		hdl->libzfs_pool_handles = zph;
27029ab75c9Srm 	}
27129ab75c9Srm 	return (zph);
27229ab75c9Srm }
27329ab75c9Srm 
27429ab75c9Srm static zpool_handle_t *
27529ab75c9Srm zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
27629ab75c9Srm {
27729ab75c9Srm 	libzfs_handle_t *hdl = zhp->zfs_hdl;
27829ab75c9Srm 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
27929ab75c9Srm 
28029ab75c9Srm 	while ((zph != NULL) &&
28129ab75c9Srm 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
28229ab75c9Srm 		zph = zph->zpool_next;
28329ab75c9Srm 	return (zph);
28429ab75c9Srm }
28529ab75c9Srm 
28629ab75c9Srm /*
28729ab75c9Srm  * Returns a handle to the pool that contains the provided dataset.
28829ab75c9Srm  * If a handle to that pool already exists then that handle is returned.
28929ab75c9Srm  * Otherwise, a new handle is created and added to the list of handles.
29029ab75c9Srm  */
29129ab75c9Srm static zpool_handle_t *
29229ab75c9Srm zpool_handle(zfs_handle_t *zhp)
29329ab75c9Srm {
29429ab75c9Srm 	char *pool_name;
29529ab75c9Srm 	int len;
29629ab75c9Srm 	zpool_handle_t *zph;
29729ab75c9Srm 
298*78f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
29929ab75c9Srm 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
30029ab75c9Srm 	(void) strlcpy(pool_name, zhp->zfs_name, len);
30129ab75c9Srm 
30229ab75c9Srm 	zph = zpool_find_handle(zhp, pool_name, len);
30329ab75c9Srm 	if (zph == NULL)
30429ab75c9Srm 		zph = zpool_add_handle(zhp, pool_name);
30529ab75c9Srm 
30629ab75c9Srm 	free(pool_name);
30729ab75c9Srm 	return (zph);
30829ab75c9Srm }
30929ab75c9Srm 
31029ab75c9Srm void
31129ab75c9Srm zpool_free_handles(libzfs_handle_t *hdl)
31229ab75c9Srm {
31329ab75c9Srm 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
31429ab75c9Srm 
31529ab75c9Srm 	while (zph != NULL) {
31629ab75c9Srm 		next = zph->zpool_next;
31729ab75c9Srm 		zpool_close(zph);
31829ab75c9Srm 		zph = next;
31929ab75c9Srm 	}
32029ab75c9Srm 	hdl->libzfs_pool_handles = NULL;
32129ab75c9Srm }
32229ab75c9Srm 
323fa9e4066Sahrens /*
324fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
325fa9e4066Sahrens  */
326fa9e4066Sahrens static int
327ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
328fa9e4066Sahrens {
329e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
330fa9e4066Sahrens 
331ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
332fa9e4066Sahrens 
333ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3347f7322feSeschrock 		if (errno == ENOMEM) {
335ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
33699653d4eSeschrock 				return (-1);
337e9dbad6fSeschrock 			}
3387f7322feSeschrock 		} else {
3397f7322feSeschrock 			return (-1);
3407f7322feSeschrock 		}
3417f7322feSeschrock 	}
342ebedde84SEric Taylor 	return (0);
343ebedde84SEric Taylor }
344fa9e4066Sahrens 
34592241e0bSTom Erickson /*
34692241e0bSTom Erickson  * Utility function to get the received properties of the given object.
34792241e0bSTom Erickson  */
34892241e0bSTom Erickson static int
34992241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
35092241e0bSTom Erickson {
35192241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
35292241e0bSTom Erickson 	nvlist_t *recvdprops;
35392241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
35492241e0bSTom Erickson 	int err;
35592241e0bSTom Erickson 
35692241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
35792241e0bSTom Erickson 		return (-1);
35892241e0bSTom Erickson 
35992241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
36092241e0bSTom Erickson 
36192241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
36292241e0bSTom Erickson 		if (errno == ENOMEM) {
36392241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
36492241e0bSTom Erickson 				return (-1);
36592241e0bSTom Erickson 			}
36692241e0bSTom Erickson 		} else {
36792241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
36892241e0bSTom Erickson 			return (-1);
36992241e0bSTom Erickson 		}
37092241e0bSTom Erickson 	}
37192241e0bSTom Erickson 
37292241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
37392241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
37492241e0bSTom Erickson 	if (err != 0)
37592241e0bSTom Erickson 		return (-1);
37692241e0bSTom Erickson 
37792241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
37892241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
37992241e0bSTom Erickson 
38092241e0bSTom Erickson 	return (0);
38192241e0bSTom Erickson }
38292241e0bSTom Erickson 
383ebedde84SEric Taylor static int
384ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
385ebedde84SEric Taylor {
386ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
387fa9e4066Sahrens 
388ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
389ebedde84SEric Taylor 
390ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
39199653d4eSeschrock 		return (-1);
39299653d4eSeschrock 	}
393fa9e4066Sahrens 
39414843421SMatthew Ahrens 	/*
39514843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
39614843421SMatthew Ahrens 	 * storing them in zfs_props?
39714843421SMatthew Ahrens 	 */
398fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
399fac3008cSeschrock 		nvlist_free(allprops);
400e9dbad6fSeschrock 		return (-1);
401fac3008cSeschrock 	}
402fac3008cSeschrock 
403fac3008cSeschrock 	nvlist_free(zhp->zfs_props);
404fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
405fac3008cSeschrock 
406fac3008cSeschrock 	zhp->zfs_props = allprops;
407fac3008cSeschrock 	zhp->zfs_user_props = userprops;
40899653d4eSeschrock 
409fa9e4066Sahrens 	return (0);
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412ebedde84SEric Taylor static int
413ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
414ebedde84SEric Taylor {
415ebedde84SEric Taylor 	int rc = 0;
416ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
417ebedde84SEric Taylor 
418ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
419ebedde84SEric Taylor 		return (-1);
420ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
421ebedde84SEric Taylor 		rc = -1;
422ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
423ebedde84SEric Taylor 		rc = -1;
424ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
425ebedde84SEric Taylor 	return (rc);
426ebedde84SEric Taylor }
427ebedde84SEric Taylor 
428fa9e4066Sahrens /*
429fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
430fa9e4066Sahrens  */
431fa9e4066Sahrens void
432fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
433fa9e4066Sahrens {
434fa9e4066Sahrens 	(void) get_stats(zhp);
435fa9e4066Sahrens }
436fa9e4066Sahrens 
437fa9e4066Sahrens /*
438fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
439fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
440fa9e4066Sahrens  */
441ebedde84SEric Taylor static int
442ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
443fa9e4066Sahrens {
444503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
445ebedde84SEric Taylor 		return (-1);
44631fd60d3Sahrens 
447fa9e4066Sahrens 	/*
448fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
449fa9e4066Sahrens 	 * the high-level type.
450fa9e4066Sahrens 	 */
451a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
452a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
453a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
454a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
455a2eea2e1Sahrens 	else
456a2eea2e1Sahrens 		abort();
457a2eea2e1Sahrens 
458fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
459fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
460fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
461fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
462fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
463fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
464fa9e4066Sahrens 	else
46599653d4eSeschrock 		abort();	/* we should never see any other types */
466fa9e4066Sahrens 
4679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
470ebedde84SEric Taylor 	return (0);
471ebedde84SEric Taylor }
472ebedde84SEric Taylor 
473ebedde84SEric Taylor zfs_handle_t *
474ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
475ebedde84SEric Taylor {
476ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
477ebedde84SEric Taylor 
478ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
479ebedde84SEric Taylor 
480ebedde84SEric Taylor 	if (zhp == NULL)
481ebedde84SEric Taylor 		return (NULL);
482ebedde84SEric Taylor 
483ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
484ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
485ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
486ebedde84SEric Taylor 		free(zhp);
487ebedde84SEric Taylor 		return (NULL);
488ebedde84SEric Taylor 	}
489ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
490ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
491ebedde84SEric Taylor 		free(zhp);
492ebedde84SEric Taylor 		return (NULL);
493ebedde84SEric Taylor 	}
494ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
495ebedde84SEric Taylor 		free(zhp);
496ebedde84SEric Taylor 		zhp = NULL;
497ebedde84SEric Taylor 	}
498ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
499ebedde84SEric Taylor 	return (zhp);
500ebedde84SEric Taylor }
501ebedde84SEric Taylor 
50219b94df9SMatthew Ahrens zfs_handle_t *
503ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
504ebedde84SEric Taylor {
505ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
506ebedde84SEric Taylor 
507ebedde84SEric Taylor 	if (zhp == NULL)
508ebedde84SEric Taylor 		return (NULL);
509ebedde84SEric Taylor 
510ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
511ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
512ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
513ebedde84SEric Taylor 		free(zhp);
514ebedde84SEric Taylor 		return (NULL);
515ebedde84SEric Taylor 	}
516fa9e4066Sahrens 	return (zhp);
517fa9e4066Sahrens }
518fa9e4066Sahrens 
51919b94df9SMatthew Ahrens zfs_handle_t *
52019b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
52119b94df9SMatthew Ahrens {
52219b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
52319b94df9SMatthew Ahrens 
52419b94df9SMatthew Ahrens 	if (zhp == NULL)
52519b94df9SMatthew Ahrens 		return (NULL);
52619b94df9SMatthew Ahrens 
52719b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
52819b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
52919b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
53019b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
53119b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
53219b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
53319b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
53419b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
53519b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
53619b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
53719b94df9SMatthew Ahrens 			zfs_close(zhp);
53819b94df9SMatthew Ahrens 			return (NULL);
53919b94df9SMatthew Ahrens 		}
54019b94df9SMatthew Ahrens 	}
54119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
54219b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
54319b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
54419b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54519b94df9SMatthew Ahrens 			zfs_close(zhp);
54619b94df9SMatthew Ahrens 			return (NULL);
54719b94df9SMatthew Ahrens 		}
54819b94df9SMatthew Ahrens 	}
54919b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
55019b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
55119b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
55219b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55319b94df9SMatthew Ahrens 			zfs_close(zhp);
55419b94df9SMatthew Ahrens 			return (NULL);
55519b94df9SMatthew Ahrens 		}
55619b94df9SMatthew Ahrens 	}
55719b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
55819b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
55919b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
56019b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
56119b94df9SMatthew Ahrens 	}
56219b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
56319b94df9SMatthew Ahrens 	return (zhp);
56419b94df9SMatthew Ahrens }
56519b94df9SMatthew Ahrens 
566*78f17100SMatthew Ahrens boolean_t
567*78f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
568*78f17100SMatthew Ahrens {
569*78f17100SMatthew Ahrens 	nvlist_t *bmarks;
570*78f17100SMatthew Ahrens 	nvlist_t *props;
571*78f17100SMatthew Ahrens 	char fsname[ZFS_MAXNAMELEN];
572*78f17100SMatthew Ahrens 	char *bmark_name;
573*78f17100SMatthew Ahrens 	char *pound;
574*78f17100SMatthew Ahrens 	int err;
575*78f17100SMatthew Ahrens 	boolean_t rv;
576*78f17100SMatthew Ahrens 
577*78f17100SMatthew Ahrens 
578*78f17100SMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
579*78f17100SMatthew Ahrens 	pound = strchr(fsname, '#');
580*78f17100SMatthew Ahrens 	if (pound == NULL)
581*78f17100SMatthew Ahrens 		return (B_FALSE);
582*78f17100SMatthew Ahrens 
583*78f17100SMatthew Ahrens 	*pound = '\0';
584*78f17100SMatthew Ahrens 	bmark_name = pound + 1;
585*78f17100SMatthew Ahrens 	props = fnvlist_alloc();
586*78f17100SMatthew Ahrens 	err = lzc_get_bookmarks(fsname, props, &bmarks);
587*78f17100SMatthew Ahrens 	nvlist_free(props);
588*78f17100SMatthew Ahrens 	if (err != 0) {
589*78f17100SMatthew Ahrens 		nvlist_free(bmarks);
590*78f17100SMatthew Ahrens 		return (B_FALSE);
591*78f17100SMatthew Ahrens 	}
592*78f17100SMatthew Ahrens 
593*78f17100SMatthew Ahrens 	rv = nvlist_exists(bmarks, bmark_name);
594*78f17100SMatthew Ahrens 	nvlist_free(bmarks);
595*78f17100SMatthew Ahrens 	return (rv);
596*78f17100SMatthew Ahrens }
597*78f17100SMatthew Ahrens 
598*78f17100SMatthew Ahrens zfs_handle_t *
599*78f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
600*78f17100SMatthew Ahrens     nvlist_t *bmark_props)
601*78f17100SMatthew Ahrens {
602*78f17100SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
603*78f17100SMatthew Ahrens 
604*78f17100SMatthew Ahrens 	if (zhp == NULL)
605*78f17100SMatthew Ahrens 		return (NULL);
606*78f17100SMatthew Ahrens 
607*78f17100SMatthew Ahrens 	/* Fill in the name. */
608*78f17100SMatthew Ahrens 	zhp->zfs_hdl = parent->zfs_hdl;
609*78f17100SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
610*78f17100SMatthew Ahrens 
611*78f17100SMatthew Ahrens 	/* Set the property lists. */
612*78f17100SMatthew Ahrens 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
613*78f17100SMatthew Ahrens 		free(zhp);
614*78f17100SMatthew Ahrens 		return (NULL);
615*78f17100SMatthew Ahrens 	}
616*78f17100SMatthew Ahrens 
617*78f17100SMatthew Ahrens 	/* Set the types. */
618*78f17100SMatthew Ahrens 	zhp->zfs_head_type = parent->zfs_head_type;
619*78f17100SMatthew Ahrens 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
620*78f17100SMatthew Ahrens 
621*78f17100SMatthew Ahrens 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
622*78f17100SMatthew Ahrens 		nvlist_free(zhp->zfs_props);
623*78f17100SMatthew Ahrens 		free(zhp);
624*78f17100SMatthew Ahrens 		return (NULL);
625*78f17100SMatthew Ahrens 	}
626*78f17100SMatthew Ahrens 
627*78f17100SMatthew Ahrens 	return (zhp);
628*78f17100SMatthew Ahrens }
629*78f17100SMatthew Ahrens 
630fa9e4066Sahrens /*
631fa9e4066Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
632fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
633fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
634fa9e4066Sahrens  */
635fa9e4066Sahrens zfs_handle_t *
63699653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
637fa9e4066Sahrens {
638fa9e4066Sahrens 	zfs_handle_t *zhp;
63999653d4eSeschrock 	char errbuf[1024];
64099653d4eSeschrock 
64199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
64299653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
643fa9e4066Sahrens 
644fa9e4066Sahrens 	/*
64599653d4eSeschrock 	 * Validate the name before we even try to open it.
646fa9e4066Sahrens 	 */
647f18faf3fSek 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
64899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
64999653d4eSeschrock 		    "invalid dataset name"));
65099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
651fa9e4066Sahrens 		return (NULL);
652fa9e4066Sahrens 	}
653fa9e4066Sahrens 
654fa9e4066Sahrens 	/*
655fa9e4066Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
656fa9e4066Sahrens 	 */
657fa9e4066Sahrens 	errno = 0;
65899653d4eSeschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
659ece3d9b3Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
660fa9e4066Sahrens 		return (NULL);
661fa9e4066Sahrens 	}
662fa9e4066Sahrens 
663fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
66499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
66594de1d4cSeschrock 		zfs_close(zhp);
666fa9e4066Sahrens 		return (NULL);
667fa9e4066Sahrens 	}
668fa9e4066Sahrens 
669fa9e4066Sahrens 	return (zhp);
670fa9e4066Sahrens }
671fa9e4066Sahrens 
672fa9e4066Sahrens /*
673fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
674fa9e4066Sahrens  */
675fa9e4066Sahrens void
676fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
677fa9e4066Sahrens {
678fa9e4066Sahrens 	if (zhp->zfs_mntopts)
679fa9e4066Sahrens 		free(zhp->zfs_mntopts);
680e9dbad6fSeschrock 	nvlist_free(zhp->zfs_props);
681e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
68292241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
683fa9e4066Sahrens 	free(zhp);
684fa9e4066Sahrens }
685fa9e4066Sahrens 
686ebedde84SEric Taylor typedef struct mnttab_node {
687ebedde84SEric Taylor 	struct mnttab mtn_mt;
688ebedde84SEric Taylor 	avl_node_t mtn_node;
689ebedde84SEric Taylor } mnttab_node_t;
690ebedde84SEric Taylor 
691ebedde84SEric Taylor static int
692ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
693ebedde84SEric Taylor {
694ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
695ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
696ebedde84SEric Taylor 	int rv;
697ebedde84SEric Taylor 
698ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
699ebedde84SEric Taylor 
700ebedde84SEric Taylor 	if (rv == 0)
701ebedde84SEric Taylor 		return (0);
702ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
703ebedde84SEric Taylor }
704ebedde84SEric Taylor 
705ebedde84SEric Taylor void
706ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
707ebedde84SEric Taylor {
708ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
709ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
710ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
711b2634b9cSEric Taylor }
712b2634b9cSEric Taylor 
713b2634b9cSEric Taylor void
714b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
715b2634b9cSEric Taylor {
716b2634b9cSEric Taylor 	struct mnttab entry;
717ebedde84SEric Taylor 
718ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
719ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
720ebedde84SEric Taylor 		mnttab_node_t *mtn;
721ebedde84SEric Taylor 
722ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
723ebedde84SEric Taylor 			continue;
724ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
725ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
726ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
727ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
728ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
729ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
730ebedde84SEric Taylor 	}
731ebedde84SEric Taylor }
732ebedde84SEric Taylor 
733ebedde84SEric Taylor void
734ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
735ebedde84SEric Taylor {
736ebedde84SEric Taylor 	void *cookie = NULL;
737ebedde84SEric Taylor 	mnttab_node_t *mtn;
738ebedde84SEric Taylor 
739ebedde84SEric Taylor 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
740ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
741ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
742ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
743ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
744ebedde84SEric Taylor 		free(mtn);
745ebedde84SEric Taylor 	}
746ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
747ebedde84SEric Taylor }
748ebedde84SEric Taylor 
749b2634b9cSEric Taylor void
750b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
751b2634b9cSEric Taylor {
752b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
753b2634b9cSEric Taylor }
754b2634b9cSEric Taylor 
755ebedde84SEric Taylor int
756ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
757ebedde84SEric Taylor     struct mnttab *entry)
758ebedde84SEric Taylor {
759ebedde84SEric Taylor 	mnttab_node_t find;
760ebedde84SEric Taylor 	mnttab_node_t *mtn;
761ebedde84SEric Taylor 
762b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
763b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
764b2634b9cSEric Taylor 
765b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
766b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
767b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
768b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
769b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
770b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
771b2634b9cSEric Taylor 			return (0);
772b2634b9cSEric Taylor 		else
773b2634b9cSEric Taylor 			return (ENOENT);
774b2634b9cSEric Taylor 	}
775b2634b9cSEric Taylor 
776ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
777b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
778ebedde84SEric Taylor 
779ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
780ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
781ebedde84SEric Taylor 	if (mtn) {
782ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
783ebedde84SEric Taylor 		return (0);
784ebedde84SEric Taylor 	}
785ebedde84SEric Taylor 	return (ENOENT);
786ebedde84SEric Taylor }
787ebedde84SEric Taylor 
788ebedde84SEric Taylor void
789ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
790ebedde84SEric Taylor     const char *mountp, const char *mntopts)
791ebedde84SEric Taylor {
792ebedde84SEric Taylor 	mnttab_node_t *mtn;
793ebedde84SEric Taylor 
794ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
795ebedde84SEric Taylor 		return;
796ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
797ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
798ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
799ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
800ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
801ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
802ebedde84SEric Taylor }
803ebedde84SEric Taylor 
804ebedde84SEric Taylor void
805ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
806ebedde84SEric Taylor {
807ebedde84SEric Taylor 	mnttab_node_t find;
808ebedde84SEric Taylor 	mnttab_node_t *ret;
809ebedde84SEric Taylor 
810ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
811ebedde84SEric Taylor 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
812ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
813ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
814ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
815ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
816ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
817ebedde84SEric Taylor 		free(ret);
818ebedde84SEric Taylor 	}
819ebedde84SEric Taylor }
820ebedde84SEric Taylor 
8217b97dc1aSrm int
8227b97dc1aSrm zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
8237b97dc1aSrm {
82429ab75c9Srm 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
8257b97dc1aSrm 
8267b97dc1aSrm 	if (zpool_handle == NULL)
8277b97dc1aSrm 		return (-1);
8287b97dc1aSrm 
8297b97dc1aSrm 	*spa_version = zpool_get_prop_int(zpool_handle,
8307b97dc1aSrm 	    ZPOOL_PROP_VERSION, NULL);
8317b97dc1aSrm 	return (0);
8327b97dc1aSrm }
8337b97dc1aSrm 
8347b97dc1aSrm /*
8357b97dc1aSrm  * The choice of reservation property depends on the SPA version.
8367b97dc1aSrm  */
8377b97dc1aSrm static int
8387b97dc1aSrm zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
8397b97dc1aSrm {
8407b97dc1aSrm 	int spa_version;
8417b97dc1aSrm 
8427b97dc1aSrm 	if (zfs_spa_version(zhp, &spa_version) < 0)
8437b97dc1aSrm 		return (-1);
8447b97dc1aSrm 
8457b97dc1aSrm 	if (spa_version >= SPA_VERSION_REFRESERVATION)
8467b97dc1aSrm 		*resv_prop = ZFS_PROP_REFRESERVATION;
8477b97dc1aSrm 	else
8487b97dc1aSrm 		*resv_prop = ZFS_PROP_RESERVATION;
8497b97dc1aSrm 
8507b97dc1aSrm 	return (0);
8517b97dc1aSrm }
8527b97dc1aSrm 
853e9dbad6fSeschrock /*
854e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
855e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
856e9dbad6fSeschrock  * strings.
857e9dbad6fSeschrock  */
8580a48a24eStimh nvlist_t *
8590a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
860990b4856Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
861e9dbad6fSeschrock {
862e9dbad6fSeschrock 	nvpair_t *elem;
863e9dbad6fSeschrock 	uint64_t intval;
864e9dbad6fSeschrock 	char *strval;
865990b4856Slling 	zfs_prop_t prop;
866e9dbad6fSeschrock 	nvlist_t *ret;
867da6c28aaSamw 	int chosen_normal = -1;
868da6c28aaSamw 	int chosen_utf = -1;
869e9dbad6fSeschrock 
870990b4856Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
871990b4856Slling 		(void) no_memory(hdl);
872990b4856Slling 		return (NULL);
873e9dbad6fSeschrock 	}
874e9dbad6fSeschrock 
87514843421SMatthew Ahrens 	/*
87614843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
87714843421SMatthew Ahrens 	 */
87814843421SMatthew Ahrens 
879e9dbad6fSeschrock 	elem = NULL;
880e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
881990b4856Slling 		const char *propname = nvpair_name(elem);
882e9dbad6fSeschrock 
88314843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
88414843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
885990b4856Slling 			/*
88614843421SMatthew Ahrens 			 * This is a user property: make sure it's a
887990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
888990b4856Slling 			 */
889990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
890990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
891990b4856Slling 				    "'%s' must be a string"), propname);
892990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
893990b4856Slling 				goto error;
894990b4856Slling 			}
895990b4856Slling 
896990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
897990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
898990b4856Slling 				    "property name '%s' is too long"),
899990b4856Slling 				    propname);
900990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
901990b4856Slling 				goto error;
902fa9e4066Sahrens 			}
903fa9e4066Sahrens 
904e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
905e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
906e9dbad6fSeschrock 				(void) no_memory(hdl);
907e9dbad6fSeschrock 				goto error;
908e9dbad6fSeschrock 			}
909e9dbad6fSeschrock 			continue;
910e9dbad6fSeschrock 		}
911e9dbad6fSeschrock 
91214843421SMatthew Ahrens 		/*
91314843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
91414843421SMatthew Ahrens 		 * snapshots.
91514843421SMatthew Ahrens 		 */
916bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
917bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
918bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
919bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
920bb0ade09Sahrens 			goto error;
921bb0ade09Sahrens 		}
922bb0ade09Sahrens 
92314843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
92414843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
92514843421SMatthew Ahrens 			char newpropname[128];
92614843421SMatthew Ahrens 			char domain[128];
92714843421SMatthew Ahrens 			uint64_t rid;
92814843421SMatthew Ahrens 			uint64_t valary[3];
92914843421SMatthew Ahrens 
93014843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
93114843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
93214843421SMatthew Ahrens 				zfs_error_aux(hdl,
93314843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
93414843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
93514843421SMatthew Ahrens 				    propname);
93614843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
93714843421SMatthew Ahrens 				goto error;
93814843421SMatthew Ahrens 			}
93914843421SMatthew Ahrens 
94014843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
94114843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
94214843421SMatthew Ahrens 				zfs_error_aux(hdl,
94314843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
94414843421SMatthew Ahrens 				    propname);
94514843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
94614843421SMatthew Ahrens 				    errbuf);
94714843421SMatthew Ahrens 				goto error;
94814843421SMatthew Ahrens 			}
94914843421SMatthew Ahrens 
95014843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
95114843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
95214843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
95314843421SMatthew Ahrens 					intval = 0;
95414843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
95514843421SMatthew Ahrens 				    strval, &intval) != 0) {
95614843421SMatthew Ahrens 					(void) zfs_error(hdl,
95714843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
95814843421SMatthew Ahrens 					goto error;
95914843421SMatthew Ahrens 				}
96014843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
96114843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
96214843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
96314843421SMatthew Ahrens 				if (intval == 0) {
96414843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
96514843421SMatthew Ahrens 					    "use 'none' to disable "
96614843421SMatthew Ahrens 					    "userquota/groupquota"));
96714843421SMatthew Ahrens 					goto error;
96814843421SMatthew Ahrens 				}
96914843421SMatthew Ahrens 			} else {
97014843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
97114843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
97214843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
97314843421SMatthew Ahrens 				goto error;
97414843421SMatthew Ahrens 			}
97514843421SMatthew Ahrens 
9762d5843dbSMatthew Ahrens 			/*
9772d5843dbSMatthew Ahrens 			 * Encode the prop name as
9782d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
9792d5843dbSMatthew Ahrens 			 * for the kernel to decode.
9802d5843dbSMatthew Ahrens 			 */
98114843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
9822d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
9832d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
98414843421SMatthew Ahrens 			valary[0] = uqtype;
98514843421SMatthew Ahrens 			valary[1] = rid;
98614843421SMatthew Ahrens 			valary[2] = intval;
98714843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
98814843421SMatthew Ahrens 			    valary, 3) != 0) {
98914843421SMatthew Ahrens 				(void) no_memory(hdl);
99014843421SMatthew Ahrens 				goto error;
99114843421SMatthew Ahrens 			}
99214843421SMatthew Ahrens 			continue;
99319b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
99419b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
99519b94df9SMatthew Ahrens 			    "'%s' is readonly"),
99619b94df9SMatthew Ahrens 			    propname);
99719b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
99819b94df9SMatthew Ahrens 			goto error;
99914843421SMatthew Ahrens 		}
100014843421SMatthew Ahrens 
100114843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
100214843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
100314843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
100414843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
100514843421SMatthew Ahrens 			goto error;
100614843421SMatthew Ahrens 		}
100714843421SMatthew Ahrens 
1008e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1009e9dbad6fSeschrock 			zfs_error_aux(hdl,
1010e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1011e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1012e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1013e9dbad6fSeschrock 			goto error;
1014e9dbad6fSeschrock 		}
1015e9dbad6fSeschrock 
1016e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1017da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
1018e9dbad6fSeschrock 			zfs_error_aux(hdl,
1019e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1020e9dbad6fSeschrock 			    propname);
1021e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1022e9dbad6fSeschrock 			goto error;
1023e9dbad6fSeschrock 		}
1024e9dbad6fSeschrock 
1025990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1026990b4856Slling 		    &strval, &intval, errbuf) != 0)
1027e9dbad6fSeschrock 			goto error;
1028fa9e4066Sahrens 
1029e9dbad6fSeschrock 		/*
1030e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1031e9dbad6fSeschrock 		 */
1032e9dbad6fSeschrock 		switch (prop) {
1033e7437265Sahrens 		case ZFS_PROP_VERSION:
1034e7437265Sahrens 		{
1035e7437265Sahrens 			int version;
1036e7437265Sahrens 
1037e7437265Sahrens 			if (zhp == NULL)
1038e7437265Sahrens 				break;
1039e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1040e7437265Sahrens 			if (intval < version) {
1041e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1042e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1043e7437265Sahrens 				    version);
1044e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1045e7437265Sahrens 				goto error;
1046e7437265Sahrens 			}
1047e7437265Sahrens 			break;
1048e7437265Sahrens 		}
1049e7437265Sahrens 
1050e9dbad6fSeschrock 		case ZFS_PROP_RECORDSIZE:
1051e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1052e9dbad6fSeschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
1053e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1054e9dbad6fSeschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
105599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1056e9dbad6fSeschrock 				    "'%s' must be power of 2 from %u "
1057e9dbad6fSeschrock 				    "to %uk"), propname,
1058e9dbad6fSeschrock 				    (uint_t)SPA_MINBLOCKSIZE,
1059e9dbad6fSeschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
1060e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1061e9dbad6fSeschrock 				goto error;
1062fa9e4066Sahrens 			}
1063fa9e4066Sahrens 			break;
1064fa9e4066Sahrens 
10654201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
10664201a95eSRic Aleshire 		{
10674201a95eSRic Aleshire 			/*
10684201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
10694201a95eSRic Aleshire 			 * internal hex label string.
10704201a95eSRic Aleshire 			 */
10714201a95eSRic Aleshire 
10724201a95eSRic Aleshire 			m_label_t *new_sl;
10734201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
10744201a95eSRic Aleshire 
10754201a95eSRic Aleshire 			/* Default value is already OK. */
10764201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
10774201a95eSRic Aleshire 				break;
10784201a95eSRic Aleshire 
10794201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
10804201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
10814201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
10824201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
10834201a95eSRic Aleshire 				goto badlabel;
10844201a95eSRic Aleshire 			}
10854201a95eSRic Aleshire 
10864201a95eSRic Aleshire 			/* Now translate to hex internal label string */
10874201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
10884201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
10894201a95eSRic Aleshire 				if (hex)
10904201a95eSRic Aleshire 					free(hex);
10914201a95eSRic Aleshire 				goto badlabel;
10924201a95eSRic Aleshire 			}
10934201a95eSRic Aleshire 			m_label_free(new_sl);
10944201a95eSRic Aleshire 
10954201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
10964201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
10974201a95eSRic Aleshire 				free(hex);
10984201a95eSRic Aleshire 				break;
10994201a95eSRic Aleshire 			}
11004201a95eSRic Aleshire 
11014201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1102569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
11034201a95eSRic Aleshire 			    DATA_TYPE_STRING);
11044201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
11054201a95eSRic Aleshire 			    hex) == 0);
11064201a95eSRic Aleshire 			free(hex);
11074201a95eSRic Aleshire 
11084201a95eSRic Aleshire 			break;
11094201a95eSRic Aleshire 
11104201a95eSRic Aleshire badlabel:
11114201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11124201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
11134201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11144201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
11154201a95eSRic Aleshire 			goto error;
11164201a95eSRic Aleshire 
11174201a95eSRic Aleshire 		}
11184201a95eSRic Aleshire 
1119e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
112089eef05eSrm 		{
112189eef05eSrm 			namecheck_err_t why;
112289eef05eSrm 
1123e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1124e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1125e9dbad6fSeschrock 				break;
1126fa9e4066Sahrens 
112789eef05eSrm 			if (mountpoint_namecheck(strval, &why)) {
112889eef05eSrm 				switch (why) {
112989eef05eSrm 				case NAME_ERR_LEADING_SLASH:
113089eef05eSrm 					zfs_error_aux(hdl,
113189eef05eSrm 					    dgettext(TEXT_DOMAIN,
113289eef05eSrm 					    "'%s' must be an absolute path, "
113389eef05eSrm 					    "'none', or 'legacy'"), propname);
113489eef05eSrm 					break;
113589eef05eSrm 				case NAME_ERR_TOOLONG:
113689eef05eSrm 					zfs_error_aux(hdl,
113789eef05eSrm 					    dgettext(TEXT_DOMAIN,
113889eef05eSrm 					    "component of '%s' is too long"),
113989eef05eSrm 					    propname);
114089eef05eSrm 					break;
114189eef05eSrm 				}
1142e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1143e9dbad6fSeschrock 				goto error;
1144fa9e4066Sahrens 			}
114589eef05eSrm 		}
114689eef05eSrm 
1147f3861e1aSahl 			/*FALLTHRU*/
1148fa9e4066Sahrens 
1149da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1150f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1151f3861e1aSahl 			/*
1152da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1153da6c28aaSamw 			 * properties, check if it can be set in a
1154da6c28aaSamw 			 * global/non-global zone based on
1155f3861e1aSahl 			 * the zoned property value:
1156f3861e1aSahl 			 *
1157f3861e1aSahl 			 *		global zone	    non-global zone
1158f3861e1aSahl 			 * --------------------------------------------------
1159f3861e1aSahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1160f3861e1aSahl 			 *		sharenfs (no)	    sharenfs (no)
1161da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1162f3861e1aSahl 			 *
1163f3861e1aSahl 			 * zoned=off	mountpoint (yes)	N/A
1164f3861e1aSahl 			 *		sharenfs (yes)
1165da6c28aaSamw 			 *		sharesmb (yes)
1166f3861e1aSahl 			 */
1167e9dbad6fSeschrock 			if (zoned) {
1168e9dbad6fSeschrock 				if (getzoneid() == GLOBAL_ZONEID) {
1169e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1170e9dbad6fSeschrock 					    "'%s' cannot be set on "
1171e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1172e9dbad6fSeschrock 					    propname);
1173e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1174e9dbad6fSeschrock 					    errbuf);
1175e9dbad6fSeschrock 					goto error;
1176da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1177da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
1178e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1179e9dbad6fSeschrock 					    "'%s' cannot be set in "
1180e9dbad6fSeschrock 					    "a non-global zone"), propname);
1181e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1182e9dbad6fSeschrock 					    errbuf);
1183e9dbad6fSeschrock 					goto error;
1184fa9e4066Sahrens 				}
1185e9dbad6fSeschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
1186e9dbad6fSeschrock 				/*
1187e9dbad6fSeschrock 				 * If zoned property is 'off', this must be in
118814843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1189e9dbad6fSeschrock 				 */
1190e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1191e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1192e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1193e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1194e9dbad6fSeschrock 				goto error;
1195fa9e4066Sahrens 			}
1196f3861e1aSahl 
119767331909Sdougm 			/*
119867331909Sdougm 			 * At this point, it is legitimate to set the
119967331909Sdougm 			 * property. Now we want to make sure that the
120067331909Sdougm 			 * property value is valid if it is sharenfs.
120167331909Sdougm 			 */
1202da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1203da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
1204fac3008cSeschrock 			    strcmp(strval, "on") != 0 &&
1205fac3008cSeschrock 			    strcmp(strval, "off") != 0) {
1206da6c28aaSamw 				zfs_share_proto_t proto;
1207da6c28aaSamw 
1208da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1209da6c28aaSamw 					proto = PROTO_SMB;
1210da6c28aaSamw 				else
1211da6c28aaSamw 					proto = PROTO_NFS;
121267331909Sdougm 
121367331909Sdougm 				/*
1214da6c28aaSamw 				 * Must be an valid sharing protocol
1215da6c28aaSamw 				 * option string so init the libshare
1216da6c28aaSamw 				 * in order to enable the parser and
1217da6c28aaSamw 				 * then parse the options. We use the
1218da6c28aaSamw 				 * control API since we don't care about
1219da6c28aaSamw 				 * the current configuration and don't
122067331909Sdougm 				 * want the overhead of loading it
122167331909Sdougm 				 * until we actually do something.
122267331909Sdougm 				 */
122367331909Sdougm 
1224fac3008cSeschrock 				if (zfs_init_libshare(hdl,
1225fac3008cSeschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
1226fac3008cSeschrock 					/*
1227fac3008cSeschrock 					 * An error occurred so we can't do
1228fac3008cSeschrock 					 * anything
1229fac3008cSeschrock 					 */
1230fac3008cSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1231fac3008cSeschrock 					    "'%s' cannot be set: problem "
1232fac3008cSeschrock 					    "in share initialization"),
1233fac3008cSeschrock 					    propname);
1234fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1235fac3008cSeschrock 					    errbuf);
1236fac3008cSeschrock 					goto error;
1237fac3008cSeschrock 				}
123867331909Sdougm 
1239da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
1240fac3008cSeschrock 					/*
1241fac3008cSeschrock 					 * There was an error in parsing so
1242fac3008cSeschrock 					 * deal with it by issuing an error
1243fac3008cSeschrock 					 * message and leaving after
1244fac3008cSeschrock 					 * uninitializing the the libshare
1245fac3008cSeschrock 					 * interface.
1246fac3008cSeschrock 					 */
1247fac3008cSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1248fac3008cSeschrock 					    "'%s' cannot be set to invalid "
1249fac3008cSeschrock 					    "options"), propname);
1250fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1251fac3008cSeschrock 					    errbuf);
1252fac3008cSeschrock 					zfs_uninit_libshare(hdl);
1253fac3008cSeschrock 					goto error;
1254fac3008cSeschrock 				}
125567331909Sdougm 				zfs_uninit_libshare(hdl);
125667331909Sdougm 			}
125767331909Sdougm 
1258da6c28aaSamw 			break;
1259da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1260da6c28aaSamw 			chosen_utf = (int)intval;
1261da6c28aaSamw 			break;
1262da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1263da6c28aaSamw 			chosen_normal = (int)intval;
1264f3861e1aSahl 			break;
1265e9dbad6fSeschrock 		}
1266fa9e4066Sahrens 
1267e9dbad6fSeschrock 		/*
1268e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1269e9dbad6fSeschrock 		 * checks to enforce.
1270e9dbad6fSeschrock 		 */
1271e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1272e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1273e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1274e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1275e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1276e9dbad6fSeschrock 			char buf[64];
1277e9dbad6fSeschrock 
1278e9dbad6fSeschrock 			switch (prop) {
1279e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1280a9799022Sck 			case ZFS_PROP_REFRESERVATION:
1281e9dbad6fSeschrock 				if (intval > volsize) {
1282e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1283e9dbad6fSeschrock 					    "'%s' is greater than current "
1284e9dbad6fSeschrock 					    "volume size"), propname);
1285e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1286e9dbad6fSeschrock 					    errbuf);
1287e9dbad6fSeschrock 					goto error;
1288e9dbad6fSeschrock 				}
1289e9dbad6fSeschrock 				break;
1290e9dbad6fSeschrock 
1291e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1292e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1293e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1294e9dbad6fSeschrock 					    sizeof (buf));
1295e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1296e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1297e9dbad6fSeschrock 					    "volume block size (%s)"),
1298e9dbad6fSeschrock 					    propname, buf);
1299e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1300e9dbad6fSeschrock 					    errbuf);
1301e9dbad6fSeschrock 					goto error;
1302e9dbad6fSeschrock 				}
1303e9dbad6fSeschrock 
1304e9dbad6fSeschrock 				if (intval == 0) {
1305e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1306e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1307e9dbad6fSeschrock 					    propname);
1308e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1309e9dbad6fSeschrock 					    errbuf);
1310e9dbad6fSeschrock 					goto error;
1311e9dbad6fSeschrock 				}
1312f3861e1aSahl 				break;
1313fa9e4066Sahrens 			}
1314e9dbad6fSeschrock 		}
1315e9dbad6fSeschrock 	}
1316fa9e4066Sahrens 
1317da6c28aaSamw 	/*
1318da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1319da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1320da6c28aaSamw 	 *
1321da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1322da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1323da6c28aaSamw 	 */
1324de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1325da6c28aaSamw 		if (nvlist_add_uint64(ret,
1326da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1327da6c28aaSamw 			(void) no_memory(hdl);
1328da6c28aaSamw 			goto error;
1329da6c28aaSamw 		}
1330de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1331da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1332da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1333da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1334da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1335da6c28aaSamw 		goto error;
1336da6c28aaSamw 	}
133736db6475SEric Taylor 	return (ret);
133836db6475SEric Taylor 
133936db6475SEric Taylor error:
134036db6475SEric Taylor 	nvlist_free(ret);
134136db6475SEric Taylor 	return (NULL);
134236db6475SEric Taylor }
134336db6475SEric Taylor 
134436db6475SEric Taylor int
134536db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
134636db6475SEric Taylor {
134736db6475SEric Taylor 	uint64_t old_volsize;
134836db6475SEric Taylor 	uint64_t new_volsize;
134936db6475SEric Taylor 	uint64_t old_reservation;
135036db6475SEric Taylor 	uint64_t new_reservation;
135136db6475SEric Taylor 	zfs_prop_t resv_prop;
1352c61ea566SGeorge Wilson 	nvlist_t *props;
1353da6c28aaSamw 
1354e9dbad6fSeschrock 	/*
1355e9dbad6fSeschrock 	 * If this is an existing volume, and someone is setting the volsize,
1356e9dbad6fSeschrock 	 * make sure that it matches the reservation, or add it if necessary.
1357e9dbad6fSeschrock 	 */
135836db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
135936db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
136036db6475SEric Taylor 		return (-1);
136136db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1362c61ea566SGeorge Wilson 
1363c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1364c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1365c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1366c61ea566SGeorge Wilson 
1367c61ea566SGeorge Wilson 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1368c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1369c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1370c61ea566SGeorge Wilson 		fnvlist_free(props);
137136db6475SEric Taylor 		return (0);
1372fa9e4066Sahrens 	}
137336db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1374c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1375c61ea566SGeorge Wilson 		fnvlist_free(props);
137636db6475SEric Taylor 		return (-1);
1377c61ea566SGeorge Wilson 	}
1378c61ea566SGeorge Wilson 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1379c61ea566SGeorge Wilson 	fnvlist_free(props);
1380c61ea566SGeorge Wilson 
138136db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
138236db6475SEric Taylor 	    new_reservation) != 0) {
138336db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
138436db6475SEric Taylor 		return (-1);
138536db6475SEric Taylor 	}
138636db6475SEric Taylor 	return (1);
1387fa9e4066Sahrens }
1388fa9e4066Sahrens 
138992241e0bSTom Erickson void
139092241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
139192241e0bSTom Erickson     char *errbuf)
139292241e0bSTom Erickson {
139392241e0bSTom Erickson 	switch (err) {
139492241e0bSTom Erickson 
139592241e0bSTom Erickson 	case ENOSPC:
139692241e0bSTom Erickson 		/*
139792241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
139892241e0bSTom Erickson 		 * something different; setting a quota or reservation
139992241e0bSTom Erickson 		 * doesn't use any disk space.
140092241e0bSTom Erickson 		 */
140192241e0bSTom Erickson 		switch (prop) {
140292241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
140392241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
140492241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140592241e0bSTom Erickson 			    "size is less than current used or "
140692241e0bSTom Erickson 			    "reserved space"));
140792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
140892241e0bSTom Erickson 			break;
140992241e0bSTom Erickson 
141092241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
141192241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
141292241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141392241e0bSTom Erickson 			    "size is greater than available space"));
141492241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
141592241e0bSTom Erickson 			break;
141692241e0bSTom Erickson 
141792241e0bSTom Erickson 		default:
141892241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
141992241e0bSTom Erickson 			break;
142092241e0bSTom Erickson 		}
142192241e0bSTom Erickson 		break;
142292241e0bSTom Erickson 
142392241e0bSTom Erickson 	case EBUSY:
142492241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
142592241e0bSTom Erickson 		break;
142692241e0bSTom Erickson 
142792241e0bSTom Erickson 	case EROFS:
142892241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
142992241e0bSTom Erickson 		break;
143092241e0bSTom Erickson 
143192241e0bSTom Erickson 	case ENOTSUP:
143292241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143392241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
143492241e0bSTom Erickson 		    "property or value"));
143592241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
143692241e0bSTom Erickson 		break;
143792241e0bSTom Erickson 
143892241e0bSTom Erickson 	case ERANGE:
143992241e0bSTom Erickson 		if (prop == ZFS_PROP_COMPRESSION) {
144092241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144192241e0bSTom Erickson 			    "property setting is not allowed on "
144292241e0bSTom Erickson 			    "bootable datasets"));
144392241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
144492241e0bSTom Erickson 		} else {
144592241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
144692241e0bSTom Erickson 		}
144792241e0bSTom Erickson 		break;
144892241e0bSTom Erickson 
1449ab003da8SJim Dunham 	case EINVAL:
1450ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1451ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1452ab003da8SJim Dunham 		} else {
1453ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1454ab003da8SJim Dunham 		}
1455ab003da8SJim Dunham 		break;
1456ab003da8SJim Dunham 
145792241e0bSTom Erickson 	case EOVERFLOW:
145892241e0bSTom Erickson 		/*
145992241e0bSTom Erickson 		 * This platform can't address a volume this big.
146092241e0bSTom Erickson 		 */
146192241e0bSTom Erickson #ifdef _ILP32
146292241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
146392241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
146492241e0bSTom Erickson 			break;
146592241e0bSTom Erickson 		}
146692241e0bSTom Erickson #endif
146792241e0bSTom Erickson 		/* FALLTHROUGH */
146892241e0bSTom Erickson 	default:
146992241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
147092241e0bSTom Erickson 	}
147192241e0bSTom Erickson }
147292241e0bSTom Erickson 
1473fa9e4066Sahrens /*
1474fa9e4066Sahrens  * Given a property name and value, set the property for the given dataset.
1475fa9e4066Sahrens  */
1476fa9e4066Sahrens int
1477e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1478fa9e4066Sahrens {
1479fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1480e9dbad6fSeschrock 	int ret = -1;
1481e9dbad6fSeschrock 	prop_changelist_t *cl = NULL;
148299653d4eSeschrock 	char errbuf[1024];
148399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1484e9dbad6fSeschrock 	nvlist_t *nvl = NULL, *realprops;
1485e9dbad6fSeschrock 	zfs_prop_t prop;
14864445fffbSMatthew Ahrens 	boolean_t do_prefix = B_TRUE;
148736db6475SEric Taylor 	int added_resv;
148899653d4eSeschrock 
148999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1490e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
149199653d4eSeschrock 	    zhp->zfs_name);
149299653d4eSeschrock 
1493e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1494e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1495e9dbad6fSeschrock 		(void) no_memory(hdl);
1496e9dbad6fSeschrock 		goto error;
1497fa9e4066Sahrens 	}
1498fa9e4066Sahrens 
14990a48a24eStimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1500e9dbad6fSeschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1501e9dbad6fSeschrock 		goto error;
1502990b4856Slling 
1503e9dbad6fSeschrock 	nvlist_free(nvl);
1504e9dbad6fSeschrock 	nvl = realprops;
1505e9dbad6fSeschrock 
1506e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
1507e9dbad6fSeschrock 
150836db6475SEric Taylor 	if (prop == ZFS_PROP_VOLSIZE) {
150936db6475SEric Taylor 		if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
151036db6475SEric Taylor 			goto error;
151136db6475SEric Taylor 	}
151236db6475SEric Taylor 
15130069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1514e9dbad6fSeschrock 		goto error;
1515fa9e4066Sahrens 
1516fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
151799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151899653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
151999653d4eSeschrock 		    "in a non-global zone"));
152099653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1521fa9e4066Sahrens 		goto error;
1522fa9e4066Sahrens 	}
1523fa9e4066Sahrens 
15240068372bSMark J Musante 	/*
15254445fffbSMatthew Ahrens 	 * We don't want to unmount & remount the dataset when changing
15264445fffbSMatthew Ahrens 	 * its canmount property to 'on' or 'noauto'.  We only use
15274445fffbSMatthew Ahrens 	 * the changelist logic to unmount when setting canmount=off.
15280068372bSMark J Musante 	 */
15294445fffbSMatthew Ahrens 	if (prop == ZFS_PROP_CANMOUNT) {
15304445fffbSMatthew Ahrens 		uint64_t idx;
15314445fffbSMatthew Ahrens 		int err = zprop_string_to_index(prop, propval, &idx,
15324445fffbSMatthew Ahrens 		    ZFS_TYPE_DATASET);
15334445fffbSMatthew Ahrens 		if (err == 0 && idx != ZFS_CANMOUNT_OFF)
15344445fffbSMatthew Ahrens 			do_prefix = B_FALSE;
15354445fffbSMatthew Ahrens 	}
1536a227b7f4Shs 
1537a227b7f4Shs 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
15380068372bSMark J Musante 		goto error;
1539fa9e4066Sahrens 
1540fa9e4066Sahrens 	/*
1541fa9e4066Sahrens 	 * Execute the corresponding ioctl() to set this property.
1542fa9e4066Sahrens 	 */
1543fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1544fa9e4066Sahrens 
1545990b4856Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1546e9dbad6fSeschrock 		goto error;
1547e9dbad6fSeschrock 
1548ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1549743a77edSAlan Wright 
1550fa9e4066Sahrens 	if (ret != 0) {
155192241e0bSTom Erickson 		zfs_setprop_error(hdl, prop, errno, errbuf);
155236db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
155336db6475SEric Taylor 			/* clean up the volsize property we tried to set */
155436db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
155536db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
155636db6475SEric Taylor 			nvlist_free(nvl);
155736db6475SEric Taylor 			zcmd_free_nvlists(&zc);
155836db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
155936db6475SEric Taylor 				goto error;
156036db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
156136db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
156236db6475SEric Taylor 			    old_volsize) != 0)
156336db6475SEric Taylor 				goto error;
156436db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
156536db6475SEric Taylor 				goto error;
156636db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
156736db6475SEric Taylor 		}
1568fa9e4066Sahrens 	} else {
1569a227b7f4Shs 		if (do_prefix)
1570a227b7f4Shs 			ret = changelist_postfix(cl);
1571a227b7f4Shs 
1572fa9e4066Sahrens 		/*
1573fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1574fa9e4066Sahrens 		 * is reflected.
1575fa9e4066Sahrens 		 */
1576a227b7f4Shs 		if (ret == 0)
1577e9dbad6fSeschrock 			(void) get_stats(zhp);
1578fa9e4066Sahrens 	}
1579fa9e4066Sahrens 
1580fa9e4066Sahrens error:
1581e9dbad6fSeschrock 	nvlist_free(nvl);
1582e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1583e9dbad6fSeschrock 	if (cl)
1584e9dbad6fSeschrock 		changelist_free(cl);
1585fa9e4066Sahrens 	return (ret);
1586fa9e4066Sahrens }
1587fa9e4066Sahrens 
1588fa9e4066Sahrens /*
158992241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
159092241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1591fa9e4066Sahrens  */
1592fa9e4066Sahrens int
159392241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1594fa9e4066Sahrens {
1595fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1596fa9e4066Sahrens 	int ret;
1597fa9e4066Sahrens 	prop_changelist_t *cl;
159899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
159999653d4eSeschrock 	char errbuf[1024];
1600e9dbad6fSeschrock 	zfs_prop_t prop;
160199653d4eSeschrock 
160299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
160399653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1604fa9e4066Sahrens 
160592241e0bSTom Erickson 	zc.zc_cookie = received;
1606990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1607e9dbad6fSeschrock 		/*
1608e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1609e9dbad6fSeschrock 		 * small, so just do it here.
1610e9dbad6fSeschrock 		 */
1611e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1612e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1613e9dbad6fSeschrock 			    "invalid property"));
1614e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1615e9dbad6fSeschrock 		}
1616e9dbad6fSeschrock 
1617e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1618e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1619e9dbad6fSeschrock 
1620e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1621e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1622e9dbad6fSeschrock 
1623e9dbad6fSeschrock 		return (0);
1624e9dbad6fSeschrock 	}
1625e9dbad6fSeschrock 
1626fa9e4066Sahrens 	/*
1627fa9e4066Sahrens 	 * Verify that this property is inheritable.
1628fa9e4066Sahrens 	 */
162999653d4eSeschrock 	if (zfs_prop_readonly(prop))
163099653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1631fa9e4066Sahrens 
163292241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
163399653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1634fa9e4066Sahrens 
1635fa9e4066Sahrens 	/*
1636fa9e4066Sahrens 	 * Check to see if the value applies to this type
1637fa9e4066Sahrens 	 */
163899653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
163999653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1640fa9e4066Sahrens 
1641bf7c2d40Srm 	/*
164236db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1643bf7c2d40Srm 	 */
1644bf7c2d40Srm 	propname = zfs_prop_to_name(prop);
1645fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1646e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1647fa9e4066Sahrens 
1648fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1649fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
165099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
165199653d4eSeschrock 		    "dataset is used in a non-global zone"));
165299653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1653fa9e4066Sahrens 	}
1654fa9e4066Sahrens 
1655fa9e4066Sahrens 	/*
1656fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1657fa9e4066Sahrens 	 */
16580069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1659fa9e4066Sahrens 		return (-1);
1660fa9e4066Sahrens 
1661fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
166299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
166399653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
166499653d4eSeschrock 		    "in a non-global zone"));
166599653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1666fa9e4066Sahrens 		goto error;
1667fa9e4066Sahrens 	}
1668fa9e4066Sahrens 
1669fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1670fa9e4066Sahrens 		goto error;
1671fa9e4066Sahrens 
1672e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
167399653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1674fa9e4066Sahrens 	} else {
1675fa9e4066Sahrens 
1676efc555ebSnd 		if ((ret = changelist_postfix(cl)) != 0)
1677fa9e4066Sahrens 			goto error;
1678fa9e4066Sahrens 
1679fa9e4066Sahrens 		/*
1680fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1681fa9e4066Sahrens 		 */
1682fa9e4066Sahrens 		(void) get_stats(zhp);
1683fa9e4066Sahrens 	}
1684fa9e4066Sahrens 
1685fa9e4066Sahrens error:
1686fa9e4066Sahrens 	changelist_free(cl);
1687fa9e4066Sahrens 	return (ret);
1688fa9e4066Sahrens }
1689fa9e4066Sahrens 
16907f7322feSeschrock /*
16917f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
16927f7322feSeschrock  * extract them appropriately.
16937f7322feSeschrock  */
16947f7322feSeschrock static uint64_t
16957f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16967f7322feSeschrock {
16977f7322feSeschrock 	nvlist_t *nv;
16987f7322feSeschrock 	uint64_t value;
16997f7322feSeschrock 
1700a2eea2e1Sahrens 	*source = NULL;
17017f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
17027f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1703990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1704990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
17057f7322feSeschrock 	} else {
17062e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
17072e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
17087f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
17097f7322feSeschrock 		*source = "";
17107f7322feSeschrock 	}
17117f7322feSeschrock 
17127f7322feSeschrock 	return (value);
17137f7322feSeschrock }
17147f7322feSeschrock 
17157f7322feSeschrock static char *
17167f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
17177f7322feSeschrock {
17187f7322feSeschrock 	nvlist_t *nv;
17197f7322feSeschrock 	char *value;
17207f7322feSeschrock 
1721a2eea2e1Sahrens 	*source = NULL;
17227f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
17237f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1724990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1725990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
17267f7322feSeschrock 	} else {
17272e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
17282e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
17297f7322feSeschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
17307f7322feSeschrock 			value = "";
17317f7322feSeschrock 		*source = "";
17327f7322feSeschrock 	}
17337f7322feSeschrock 
17347f7322feSeschrock 	return (value);
17357f7322feSeschrock }
17367f7322feSeschrock 
173792241e0bSTom Erickson static boolean_t
173892241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
173992241e0bSTom Erickson {
174092241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
174192241e0bSTom Erickson }
174292241e0bSTom Erickson 
174392241e0bSTom Erickson static void
174492241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
174592241e0bSTom Erickson {
174692241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
174792241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
174892241e0bSTom Erickson }
174992241e0bSTom Erickson 
175092241e0bSTom Erickson static void
175192241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
175292241e0bSTom Erickson {
175392241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
175492241e0bSTom Erickson 	*cookie = 0;
175592241e0bSTom Erickson }
175692241e0bSTom Erickson 
1757fa9e4066Sahrens /*
1758fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1759fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1760fa9e4066Sahrens  *
1761fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1762fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1763fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1764fa9e4066Sahrens  * the source "temporary".
1765fa9e4066Sahrens  */
176699653d4eSeschrock static int
1767990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
176899653d4eSeschrock     char **source, uint64_t *val)
1769fa9e4066Sahrens {
1770bd00f61bSrm 	zfs_cmd_t zc = { 0 };
177196510749Stimh 	nvlist_t *zplprops = NULL;
1772fa9e4066Sahrens 	struct mnttab mnt;
17733ccfa83cSahrens 	char *mntopt_on = NULL;
17743ccfa83cSahrens 	char *mntopt_off = NULL;
177592241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1776fa9e4066Sahrens 
1777fa9e4066Sahrens 	*source = NULL;
1778fa9e4066Sahrens 
17793ccfa83cSahrens 	switch (prop) {
17803ccfa83cSahrens 	case ZFS_PROP_ATIME:
17813ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
17823ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
17833ccfa83cSahrens 		break;
17843ccfa83cSahrens 
17853ccfa83cSahrens 	case ZFS_PROP_DEVICES:
17863ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
17873ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
17883ccfa83cSahrens 		break;
17893ccfa83cSahrens 
17903ccfa83cSahrens 	case ZFS_PROP_EXEC:
17913ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
17923ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
17933ccfa83cSahrens 		break;
17943ccfa83cSahrens 
17953ccfa83cSahrens 	case ZFS_PROP_READONLY:
17963ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
17973ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
17983ccfa83cSahrens 		break;
17993ccfa83cSahrens 
18003ccfa83cSahrens 	case ZFS_PROP_SETUID:
18013ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
18023ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
18033ccfa83cSahrens 		break;
18043ccfa83cSahrens 
18053ccfa83cSahrens 	case ZFS_PROP_XATTR:
18063ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
18073ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
18083ccfa83cSahrens 		break;
1809da6c28aaSamw 
1810da6c28aaSamw 	case ZFS_PROP_NBMAND:
1811da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
1812da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
1813da6c28aaSamw 		break;
18143ccfa83cSahrens 	}
18153ccfa83cSahrens 
18163bb79becSeschrock 	/*
18173bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
18183bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
18193bb79becSeschrock 	 * we're looking up a property which requires its presence.
18203bb79becSeschrock 	 */
18213bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
18223ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1823ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
1824ebedde84SEric Taylor 		struct mnttab entry;
18253bb79becSeschrock 
1826ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1827ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
18283ccfa83cSahrens 			    entry.mnt_mntopts);
18293ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
18303ccfa83cSahrens 				return (-1);
18313ccfa83cSahrens 		}
18323bb79becSeschrock 
18333bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
18343bb79becSeschrock 	}
18353bb79becSeschrock 
1836fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
1837fa9e4066Sahrens 		mnt.mnt_mntopts = "";
1838fa9e4066Sahrens 	else
1839fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1840fa9e4066Sahrens 
1841fa9e4066Sahrens 	switch (prop) {
1842fa9e4066Sahrens 	case ZFS_PROP_ATIME:
1843fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
1844fa9e4066Sahrens 	case ZFS_PROP_EXEC:
18453ccfa83cSahrens 	case ZFS_PROP_READONLY:
18463ccfa83cSahrens 	case ZFS_PROP_SETUID:
18473ccfa83cSahrens 	case ZFS_PROP_XATTR:
1848da6c28aaSamw 	case ZFS_PROP_NBMAND:
184999653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
1850fa9e4066Sahrens 
185192241e0bSTom Erickson 		if (received)
185292241e0bSTom Erickson 			break;
185392241e0bSTom Erickson 
18543ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
185599653d4eSeschrock 			*val = B_TRUE;
1856fa9e4066Sahrens 			if (src)
1857990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
18583ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
185999653d4eSeschrock 			*val = B_FALSE;
1860fa9e4066Sahrens 			if (src)
1861990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
1862fa9e4066Sahrens 		}
186399653d4eSeschrock 		break;
1864fa9e4066Sahrens 
18653ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
1866d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
1867fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
1868a9799022Sck 	case ZFS_PROP_REFQUOTA:
1869fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
1870a9799022Sck 	case ZFS_PROP_REFRESERVATION:
1871a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
187292241e0bSTom Erickson 
187392241e0bSTom Erickson 		if (*source == NULL) {
187492241e0bSTom Erickson 			/* not default, must be local */
1875fa9e4066Sahrens 			*source = zhp->zfs_name;
187692241e0bSTom Erickson 		}
187799653d4eSeschrock 		break;
1878fa9e4066Sahrens 
1879fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
188099653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
188199653d4eSeschrock 		break;
1882fa9e4066Sahrens 
188339c23413Seschrock 	case ZFS_PROP_NUMCLONES:
188439c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
188539c23413Seschrock 		break;
188639c23413Seschrock 
1887bd00f61bSrm 	case ZFS_PROP_VERSION:
1888de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
1889de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
1890de8267e0Stimh 	case ZFS_PROP_CASE:
1891de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1892de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
18933d7934e1Srm 			return (-1);
1894bd00f61bSrm 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1895de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1896de8267e0Stimh 			zcmd_free_nvlists(&zc);
1897f4b94bdeSMatthew Ahrens 			return (-1);
1898bd00f61bSrm 		}
1899de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1900de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1901de8267e0Stimh 		    val) != 0) {
1902de8267e0Stimh 			zcmd_free_nvlists(&zc);
1903f4b94bdeSMatthew Ahrens 			return (-1);
1904de8267e0Stimh 		}
190596510749Stimh 		if (zplprops)
190696510749Stimh 			nvlist_free(zplprops);
1907de8267e0Stimh 		zcmd_free_nvlists(&zc);
1908bd00f61bSrm 		break;
1909bd00f61bSrm 
1910ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
1911ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
1912ca48f36fSKeith M Wesolowski 		break;
1913ca48f36fSKeith M Wesolowski 
1914fa9e4066Sahrens 	default:
1915e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
191691ebeef5Sahrens 		case PROP_TYPE_NUMBER:
191791ebeef5Sahrens 		case PROP_TYPE_INDEX:
1918e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
191974e7dc98SMatthew Ahrens 			/*
192014843421SMatthew Ahrens 			 * If we tried to use a default value for a
192174e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
192231f572c2STom Erickson 			 * present.
192374e7dc98SMatthew Ahrens 			 */
192474e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
192531f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
192631f572c2STom Erickson 				*source = NULL;
192774e7dc98SMatthew Ahrens 			}
1928e7437265Sahrens 			break;
1929e7437265Sahrens 
193091ebeef5Sahrens 		case PROP_TYPE_STRING:
1931e7437265Sahrens 		default:
1932e7437265Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1933e7437265Sahrens 			    "cannot get non-numeric property"));
1934e7437265Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1935e7437265Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
1936e7437265Sahrens 		}
1937fa9e4066Sahrens 	}
1938fa9e4066Sahrens 
1939fa9e4066Sahrens 	return (0);
1940fa9e4066Sahrens }
1941fa9e4066Sahrens 
1942fa9e4066Sahrens /*
1943fa9e4066Sahrens  * Calculate the source type, given the raw source string.
1944fa9e4066Sahrens  */
1945fa9e4066Sahrens static void
1946990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1947fa9e4066Sahrens     char *statbuf, size_t statlen)
1948fa9e4066Sahrens {
1949990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1950fa9e4066Sahrens 		return;
1951fa9e4066Sahrens 
1952fa9e4066Sahrens 	if (source == NULL) {
1953990b4856Slling 		*srctype = ZPROP_SRC_NONE;
1954fa9e4066Sahrens 	} else if (source[0] == '\0') {
1955990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
195692241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
195792241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
1958fa9e4066Sahrens 	} else {
1959fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
1960990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
1961fa9e4066Sahrens 		} else {
1962fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
1963990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
1964fa9e4066Sahrens 		}
1965fa9e4066Sahrens 	}
1966fa9e4066Sahrens 
1967fa9e4066Sahrens }
1968fa9e4066Sahrens 
196992241e0bSTom Erickson int
197092241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
197192241e0bSTom Erickson     size_t proplen, boolean_t literal)
197292241e0bSTom Erickson {
197392241e0bSTom Erickson 	zfs_prop_t prop;
197492241e0bSTom Erickson 	int err = 0;
197592241e0bSTom Erickson 
197692241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
197792241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
197892241e0bSTom Erickson 			return (-1);
197992241e0bSTom Erickson 
198092241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
198192241e0bSTom Erickson 
198292241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
198392241e0bSTom Erickson 		uint64_t cookie;
198492241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
198592241e0bSTom Erickson 			return (-1);
198692241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
198792241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
198892241e0bSTom Erickson 		    NULL, NULL, 0, literal);
198992241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
199092241e0bSTom Erickson 	} else {
199192241e0bSTom Erickson 		nvlist_t *propval;
199292241e0bSTom Erickson 		char *recvdval;
199392241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
199492241e0bSTom Erickson 		    propname, &propval) != 0)
199592241e0bSTom Erickson 			return (-1);
199692241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
199792241e0bSTom Erickson 		    &recvdval) == 0);
199892241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
199992241e0bSTom Erickson 	}
200092241e0bSTom Erickson 
200192241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
200292241e0bSTom Erickson }
200392241e0bSTom Erickson 
200419b94df9SMatthew Ahrens static int
200519b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
200619b94df9SMatthew Ahrens {
200719b94df9SMatthew Ahrens 	nvlist_t *value;
200819b94df9SMatthew Ahrens 	nvpair_t *pair;
200919b94df9SMatthew Ahrens 
201019b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
201119b94df9SMatthew Ahrens 	if (value == NULL)
201219b94df9SMatthew Ahrens 		return (-1);
201319b94df9SMatthew Ahrens 
201419b94df9SMatthew Ahrens 	propbuf[0] = '\0';
201519b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
201619b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
201719b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
201819b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
201919b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
202019b94df9SMatthew Ahrens 	}
202119b94df9SMatthew Ahrens 
202219b94df9SMatthew Ahrens 	return (0);
202319b94df9SMatthew Ahrens }
202419b94df9SMatthew Ahrens 
202519b94df9SMatthew Ahrens struct get_clones_arg {
202619b94df9SMatthew Ahrens 	uint64_t numclones;
202719b94df9SMatthew Ahrens 	nvlist_t *value;
202819b94df9SMatthew Ahrens 	const char *origin;
202919b94df9SMatthew Ahrens 	char buf[ZFS_MAXNAMELEN];
203019b94df9SMatthew Ahrens };
203119b94df9SMatthew Ahrens 
203219b94df9SMatthew Ahrens int
203319b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
203419b94df9SMatthew Ahrens {
203519b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
203619b94df9SMatthew Ahrens 
203719b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
203819b94df9SMatthew Ahrens 		zfs_close(zhp);
203919b94df9SMatthew Ahrens 		return (0);
204019b94df9SMatthew Ahrens 	}
204119b94df9SMatthew Ahrens 
204219b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
204319b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
204419b94df9SMatthew Ahrens 		goto out;
204519b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
20463b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
204719b94df9SMatthew Ahrens 		gca->numclones--;
204819b94df9SMatthew Ahrens 	}
204919b94df9SMatthew Ahrens 
205019b94df9SMatthew Ahrens out:
205119b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
205219b94df9SMatthew Ahrens 	zfs_close(zhp);
205319b94df9SMatthew Ahrens 	return (0);
205419b94df9SMatthew Ahrens }
205519b94df9SMatthew Ahrens 
205619b94df9SMatthew Ahrens nvlist_t *
205719b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
205819b94df9SMatthew Ahrens {
205919b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
206019b94df9SMatthew Ahrens 
206119b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
206219b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
206319b94df9SMatthew Ahrens 		struct get_clones_arg gca;
206419b94df9SMatthew Ahrens 
206519b94df9SMatthew Ahrens 		/*
206619b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
206719b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
206819b94df9SMatthew Ahrens 		 */
206919b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
207019b94df9SMatthew Ahrens 			return (NULL);
207119b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
207219b94df9SMatthew Ahrens 			return (NULL);
207319b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
207419b94df9SMatthew Ahrens 			nvlist_free(nv);
207519b94df9SMatthew Ahrens 			return (NULL);
207619b94df9SMatthew Ahrens 		}
207719b94df9SMatthew Ahrens 
207819b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
207919b94df9SMatthew Ahrens 		gca.value = value;
208019b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
208119b94df9SMatthew Ahrens 
208219b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
208319b94df9SMatthew Ahrens 			zfs_handle_t *root;
208419b94df9SMatthew Ahrens 			char pool[ZFS_MAXNAMELEN];
208519b94df9SMatthew Ahrens 			char *cp = pool;
208619b94df9SMatthew Ahrens 
208719b94df9SMatthew Ahrens 			/* get the pool name */
208819b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
208919b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
209019b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
209119b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
209219b94df9SMatthew Ahrens 
209319b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
209419b94df9SMatthew Ahrens 		}
209519b94df9SMatthew Ahrens 
209619b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
209719b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
209819b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
209919b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
210019b94df9SMatthew Ahrens 			nvlist_free(nv);
210119b94df9SMatthew Ahrens 			nvlist_free(value);
210219b94df9SMatthew Ahrens 			return (NULL);
210319b94df9SMatthew Ahrens 		}
210419b94df9SMatthew Ahrens 		nvlist_free(nv);
210519b94df9SMatthew Ahrens 		nvlist_free(value);
210619b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
210719b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
210819b94df9SMatthew Ahrens 	}
210919b94df9SMatthew Ahrens 
211019b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
211119b94df9SMatthew Ahrens 
211219b94df9SMatthew Ahrens 	return (value);
211319b94df9SMatthew Ahrens }
211419b94df9SMatthew Ahrens 
2115fa9e4066Sahrens /*
2116fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2117fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2118fa9e4066Sahrens  * human-readable form.
2119fa9e4066Sahrens  *
2120fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2121fa9e4066Sahrens  */
2122fa9e4066Sahrens int
2123fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2124990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2125fa9e4066Sahrens {
2126fa9e4066Sahrens 	char *source = NULL;
2127fa9e4066Sahrens 	uint64_t val;
2128fa9e4066Sahrens 	char *str;
2129e9dbad6fSeschrock 	const char *strval;
213092241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2131fa9e4066Sahrens 
2132fa9e4066Sahrens 	/*
2133fa9e4066Sahrens 	 * Check to see if this property applies to our object
2134fa9e4066Sahrens 	 */
2135fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2136fa9e4066Sahrens 		return (-1);
2137fa9e4066Sahrens 
213892241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
213992241e0bSTom Erickson 		return (-1);
214092241e0bSTom Erickson 
2141fa9e4066Sahrens 	if (src)
2142990b4856Slling 		*src = ZPROP_SRC_NONE;
2143fa9e4066Sahrens 
2144fa9e4066Sahrens 	switch (prop) {
2145fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2146fa9e4066Sahrens 		/*
2147fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2148fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2149fa9e4066Sahrens 		 */
2150fa9e4066Sahrens 		{
2151a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2152a2eea2e1Sahrens 			time_t time = (time_t)val;
2153fa9e4066Sahrens 			struct tm t;
2154fa9e4066Sahrens 
2155fa9e4066Sahrens 			if (literal ||
2156fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2157fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2158fa9e4066Sahrens 			    &t) == 0)
2159a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2160fa9e4066Sahrens 		}
2161fa9e4066Sahrens 		break;
2162fa9e4066Sahrens 
2163fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2164fa9e4066Sahrens 		/*
2165fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2166fa9e4066Sahrens 		 *
2167fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2168fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2169fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2170fa9e4066Sahrens 		 *
2171fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2172fa9e4066Sahrens 		 * root to any values we return.
2173fa9e4066Sahrens 		 */
217429ab75c9Srm 
21757f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2176fa9e4066Sahrens 
2177b21718f0Sgw 		if (str[0] == '/') {
217829ab75c9Srm 			char buf[MAXPATHLEN];
217929ab75c9Srm 			char *root = buf;
2180a79992aaSTom Erickson 			const char *relpath;
2181fa9e4066Sahrens 
2182a79992aaSTom Erickson 			/*
2183a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2184a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2185a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2186a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2187a79992aaSTom Erickson 			 * inherited.
2188a79992aaSTom Erickson 			 */
2189a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2190a79992aaSTom Erickson 				relpath = "";
2191a79992aaSTom Erickson 			} else {
2192a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2193a79992aaSTom Erickson 				if (relpath[0] == '/')
2194a79992aaSTom Erickson 					relpath++;
2195a79992aaSTom Erickson 			}
2196b21718f0Sgw 
219729ab75c9Srm 			if ((zpool_get_prop(zhp->zpool_hdl,
219829ab75c9Srm 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
219929ab75c9Srm 			    (strcmp(root, "-") == 0))
220029ab75c9Srm 				root[0] = '\0';
2201b21718f0Sgw 			/*
2202b21718f0Sgw 			 * Special case an alternate root of '/'. This will
2203b21718f0Sgw 			 * avoid having multiple leading slashes in the
2204b21718f0Sgw 			 * mountpoint path.
2205b21718f0Sgw 			 */
2206b21718f0Sgw 			if (strcmp(root, "/") == 0)
2207b21718f0Sgw 				root++;
2208b21718f0Sgw 
2209b21718f0Sgw 			/*
2210b21718f0Sgw 			 * If the mountpoint is '/' then skip over this
2211b21718f0Sgw 			 * if we are obtaining either an alternate root or
2212b21718f0Sgw 			 * an inherited mountpoint.
2213b21718f0Sgw 			 */
2214b21718f0Sgw 			if (str[1] == '\0' && (root[0] != '\0' ||
2215b21718f0Sgw 			    relpath[0] != '\0'))
22167f7322feSeschrock 				str++;
2217fa9e4066Sahrens 
2218fa9e4066Sahrens 			if (relpath[0] == '\0')
2219fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
22207f7322feSeschrock 				    root, str);
2221fa9e4066Sahrens 			else
2222fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
22237f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2224fa9e4066Sahrens 				    relpath);
2225fa9e4066Sahrens 		} else {
2226fa9e4066Sahrens 			/* 'legacy' or 'none' */
22277f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2228fa9e4066Sahrens 		}
2229fa9e4066Sahrens 
2230fa9e4066Sahrens 		break;
2231fa9e4066Sahrens 
2232fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
2233a2eea2e1Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2234fa9e4066Sahrens 		    proplen);
2235fa9e4066Sahrens 		/*
2236fa9e4066Sahrens 		 * If there is no parent at all, return failure to indicate that
2237fa9e4066Sahrens 		 * it doesn't apply to this dataset.
2238fa9e4066Sahrens 		 */
2239fa9e4066Sahrens 		if (propbuf[0] == '\0')
2240fa9e4066Sahrens 			return (-1);
2241fa9e4066Sahrens 		break;
2242fa9e4066Sahrens 
224319b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
224419b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
224519b94df9SMatthew Ahrens 			return (-1);
224619b94df9SMatthew Ahrens 		break;
224719b94df9SMatthew Ahrens 
2248fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2249a9799022Sck 	case ZFS_PROP_REFQUOTA:
2250fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2251a9799022Sck 	case ZFS_PROP_REFRESERVATION:
2252a9799022Sck 
225399653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
225499653d4eSeschrock 			return (-1);
2255fa9e4066Sahrens 
2256fa9e4066Sahrens 		/*
2257fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2258fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2259fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2260fa9e4066Sahrens 		 * that its set locally.
2261fa9e4066Sahrens 		 */
2262fa9e4066Sahrens 		if (val == 0) {
2263fa9e4066Sahrens 			if (literal)
2264fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2265fa9e4066Sahrens 			else
2266fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2267fa9e4066Sahrens 		} else {
2268fa9e4066Sahrens 			if (literal)
22695ad82045Snd 				(void) snprintf(propbuf, proplen, "%llu",
2270b1b8ab34Slling 				    (u_longlong_t)val);
2271fa9e4066Sahrens 			else
2272fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2273fa9e4066Sahrens 		}
2274fa9e4066Sahrens 		break;
2275fa9e4066Sahrens 
2276187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2277fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
227899653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
227999653d4eSeschrock 			return (-1);
2280b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2281b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2282b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2283fa9e4066Sahrens 		break;
2284fa9e4066Sahrens 
2285fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2286fa9e4066Sahrens 		switch (zhp->zfs_type) {
2287fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2288fa9e4066Sahrens 			str = "filesystem";
2289fa9e4066Sahrens 			break;
2290fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2291fa9e4066Sahrens 			str = "volume";
2292fa9e4066Sahrens 			break;
2293fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2294fa9e4066Sahrens 			str = "snapshot";
2295fa9e4066Sahrens 			break;
2296*78f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
2297*78f17100SMatthew Ahrens 			str = "bookmark";
2298*78f17100SMatthew Ahrens 			break;
2299fa9e4066Sahrens 		default:
230099653d4eSeschrock 			abort();
2301fa9e4066Sahrens 		}
2302fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2303fa9e4066Sahrens 		break;
2304fa9e4066Sahrens 
2305fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2306fa9e4066Sahrens 		/*
2307fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2308fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2309fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2310fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2311fa9e4066Sahrens 		 */
231299653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
231399653d4eSeschrock 		    src, &source, &val) != 0)
231499653d4eSeschrock 			return (-1);
231599653d4eSeschrock 		if (val)
2316fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2317fa9e4066Sahrens 		else
2318fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2319fa9e4066Sahrens 		break;
2320fa9e4066Sahrens 
2321fa9e4066Sahrens 	case ZFS_PROP_NAME:
2322fa9e4066Sahrens 		/*
2323fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2324fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2325fa9e4066Sahrens 		 * consumers.
2326fa9e4066Sahrens 		 */
2327fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2328fa9e4066Sahrens 		break;
2329fa9e4066Sahrens 
23304201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
23314201a95eSRic Aleshire 		{
23324201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
23334201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
23344201a95eSRic Aleshire 
23354201a95eSRic Aleshire 			(void) strlcpy(propbuf,
23364201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
23374201a95eSRic Aleshire 
23384201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
23394201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
23404201a95eSRic Aleshire 				break;
23414201a95eSRic Aleshire 
23424201a95eSRic Aleshire 			/*
23434201a95eSRic Aleshire 			 * Try to translate the internal hex string to
23444201a95eSRic Aleshire 			 * human-readable output.  If there are any
23454201a95eSRic Aleshire 			 * problems just use the hex string.
23464201a95eSRic Aleshire 			 */
23474201a95eSRic Aleshire 
23484201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
23494201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
23504201a95eSRic Aleshire 				m_label_free(new_sl);
23514201a95eSRic Aleshire 				break;
23524201a95eSRic Aleshire 			}
23534201a95eSRic Aleshire 
23544201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
23554201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
23564201a95eSRic Aleshire 				if (ascii)
23574201a95eSRic Aleshire 					free(ascii);
23584201a95eSRic Aleshire 				m_label_free(new_sl);
23594201a95eSRic Aleshire 				break;
23604201a95eSRic Aleshire 			}
23614201a95eSRic Aleshire 			m_label_free(new_sl);
23624201a95eSRic Aleshire 
23634201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
23644201a95eSRic Aleshire 			free(ascii);
23654201a95eSRic Aleshire 		}
23664201a95eSRic Aleshire 		break;
23674201a95eSRic Aleshire 
2368f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2369f0f3ef5aSGarrett D'Amore 		/*
2370f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2371f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2372f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2373f0f3ef5aSGarrett D'Amore 		 */
2374f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2375f0f3ef5aSGarrett D'Amore 			return (-1);
2376f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2377f0f3ef5aSGarrett D'Amore 		break;
2378f0f3ef5aSGarrett D'Amore 
2379fa9e4066Sahrens 	default:
2380e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
238191ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2382e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2383e7437265Sahrens 			    &source, &val) != 0)
2384e7437265Sahrens 				return (-1);
2385e7437265Sahrens 			if (literal)
2386e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2387e7437265Sahrens 				    (u_longlong_t)val);
2388e7437265Sahrens 			else
2389e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2390e7437265Sahrens 			break;
2391e7437265Sahrens 
239291ebeef5Sahrens 		case PROP_TYPE_STRING:
2393e7437265Sahrens 			(void) strlcpy(propbuf,
2394e7437265Sahrens 			    getprop_string(zhp, prop, &source), proplen);
2395e7437265Sahrens 			break;
2396e7437265Sahrens 
239791ebeef5Sahrens 		case PROP_TYPE_INDEX:
2398fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2399fe192f76Sahrens 			    &source, &val) != 0)
2400fe192f76Sahrens 				return (-1);
2401fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2402e7437265Sahrens 				return (-1);
2403e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2404e7437265Sahrens 			break;
2405e7437265Sahrens 
2406e7437265Sahrens 		default:
2407e7437265Sahrens 			abort();
2408e7437265Sahrens 		}
2409fa9e4066Sahrens 	}
2410fa9e4066Sahrens 
2411fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2412fa9e4066Sahrens 
2413fa9e4066Sahrens 	return (0);
2414fa9e4066Sahrens }
2415fa9e4066Sahrens 
2416fa9e4066Sahrens /*
2417fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2418fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2419fa9e4066Sahrens  * hard-coded property types.
2420fa9e4066Sahrens  */
2421fa9e4066Sahrens uint64_t
2422fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2423fa9e4066Sahrens {
2424fa9e4066Sahrens 	char *source;
242599653d4eSeschrock 	uint64_t val;
242699653d4eSeschrock 
24273cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2428fa9e4066Sahrens 
242999653d4eSeschrock 	return (val);
2430fa9e4066Sahrens }
2431fa9e4066Sahrens 
24327b97dc1aSrm int
24337b97dc1aSrm zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
24347b97dc1aSrm {
24357b97dc1aSrm 	char buf[64];
24367b97dc1aSrm 
243714843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
24387b97dc1aSrm 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
24397b97dc1aSrm }
24407b97dc1aSrm 
2441fa9e4066Sahrens /*
2442fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2443fa9e4066Sahrens  */
2444fa9e4066Sahrens int
2445fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2446990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2447fa9e4066Sahrens {
2448fa9e4066Sahrens 	char *source;
2449fa9e4066Sahrens 
2450fa9e4066Sahrens 	/*
2451fa9e4066Sahrens 	 * Check to see if this property applies to our object
2452fa9e4066Sahrens 	 */
2453e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2454ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
245599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
245699653d4eSeschrock 		    zfs_prop_to_name(prop)));
2457e45ce728Sahrens 	}
2458fa9e4066Sahrens 
2459fa9e4066Sahrens 	if (src)
2460990b4856Slling 		*src = ZPROP_SRC_NONE;
2461fa9e4066Sahrens 
246299653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
246399653d4eSeschrock 		return (-1);
2464fa9e4066Sahrens 
2465fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2466fa9e4066Sahrens 
2467fa9e4066Sahrens 	return (0);
2468fa9e4066Sahrens }
2469fa9e4066Sahrens 
247014843421SMatthew Ahrens static int
247114843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
247214843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
247314843421SMatthew Ahrens {
247414843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
247514843421SMatthew Ahrens 	idmap_stat status;
247614843421SMatthew Ahrens 	int err = EINVAL;
247714843421SMatthew Ahrens 
24781fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
247914843421SMatthew Ahrens 		goto out;
248014843421SMatthew Ahrens 
248114843421SMatthew Ahrens 	if (isuser) {
248214843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
248314843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
248414843421SMatthew Ahrens 	} else {
248514843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
248614843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
248714843421SMatthew Ahrens 	}
248814843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
248914843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
249014843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
249114843421SMatthew Ahrens 		err = 0;
249214843421SMatthew Ahrens 	else
249314843421SMatthew Ahrens 		err = EINVAL;
249414843421SMatthew Ahrens out:
249514843421SMatthew Ahrens 	if (get_hdl)
249614843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
249714843421SMatthew Ahrens 	return (err);
249814843421SMatthew Ahrens }
249914843421SMatthew Ahrens 
250014843421SMatthew Ahrens /*
250114843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
250214843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
250314843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
250414843421SMatthew Ahrens  */
250514843421SMatthew Ahrens static int
250614843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
250714843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
250814843421SMatthew Ahrens {
250914843421SMatthew Ahrens 	zfs_userquota_prop_t type;
251014843421SMatthew Ahrens 	char *cp, *end;
25113b12c289SMatthew Ahrens 	char *numericsid = NULL;
251214843421SMatthew Ahrens 	boolean_t isuser;
251314843421SMatthew Ahrens 
251414843421SMatthew Ahrens 	domain[0] = '\0';
251514843421SMatthew Ahrens 
251614843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
251714843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
251814843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
251914843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
252014843421SMatthew Ahrens 			break;
252114843421SMatthew Ahrens 	}
252214843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
252314843421SMatthew Ahrens 		return (EINVAL);
252414843421SMatthew Ahrens 	*typep = type;
252514843421SMatthew Ahrens 
252614843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
252714843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
252814843421SMatthew Ahrens 
252914843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
253014843421SMatthew Ahrens 
253114843421SMatthew Ahrens 	if (strchr(cp, '@')) {
253214843421SMatthew Ahrens 		/*
253314843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
25343b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
253514843421SMatthew Ahrens 		 */
25363b12c289SMatthew Ahrens 		directory_error_t e;
253714843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
253814843421SMatthew Ahrens 			return (ENOENT);
25393b12c289SMatthew Ahrens 		if (isuser) {
25403b12c289SMatthew Ahrens 			e = directory_sid_from_user_name(NULL,
25413b12c289SMatthew Ahrens 			    cp, &numericsid);
25423b12c289SMatthew Ahrens 		} else {
25433b12c289SMatthew Ahrens 			e = directory_sid_from_group_name(NULL,
25443b12c289SMatthew Ahrens 			    cp, &numericsid);
25453b12c289SMatthew Ahrens 		}
25463b12c289SMatthew Ahrens 		if (e != NULL) {
25473b12c289SMatthew Ahrens 			directory_error_free(e);
254814843421SMatthew Ahrens 			return (ENOENT);
25493b12c289SMatthew Ahrens 		}
25503b12c289SMatthew Ahrens 		if (numericsid == NULL)
255114843421SMatthew Ahrens 			return (ENOENT);
25523b12c289SMatthew Ahrens 		cp = numericsid;
25533b12c289SMatthew Ahrens 		/* will be further decoded below */
25543b12c289SMatthew Ahrens 	}
25553b12c289SMatthew Ahrens 
25563b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
255714843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
25583b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
255914843421SMatthew Ahrens 		cp = strrchr(domain, '-');
256014843421SMatthew Ahrens 		*cp = '\0';
256114843421SMatthew Ahrens 		cp++;
256214843421SMatthew Ahrens 
256314843421SMatthew Ahrens 		errno = 0;
256414843421SMatthew Ahrens 		*ridp = strtoull(cp, &end, 10);
25653b12c289SMatthew Ahrens 		if (numericsid) {
25663b12c289SMatthew Ahrens 			free(numericsid);
25673b12c289SMatthew Ahrens 			numericsid = NULL;
25683b12c289SMatthew Ahrens 		}
2569777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
257014843421SMatthew Ahrens 			return (EINVAL);
257114843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
257214843421SMatthew Ahrens 		/*
257314843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
257414843421SMatthew Ahrens 		 * turned into a uid/gid
257514843421SMatthew Ahrens 		 */
257614843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
257714843421SMatthew Ahrens 			return (ENOENT);
257814843421SMatthew Ahrens 		if (isuser) {
257914843421SMatthew Ahrens 			struct passwd *pw;
258014843421SMatthew Ahrens 			pw = getpwnam(cp);
258114843421SMatthew Ahrens 			if (pw == NULL)
258214843421SMatthew Ahrens 				return (ENOENT);
258314843421SMatthew Ahrens 			*ridp = pw->pw_uid;
258414843421SMatthew Ahrens 		} else {
258514843421SMatthew Ahrens 			struct group *gr;
258614843421SMatthew Ahrens 			gr = getgrnam(cp);
258714843421SMatthew Ahrens 			if (gr == NULL)
258814843421SMatthew Ahrens 				return (ENOENT);
258914843421SMatthew Ahrens 			*ridp = gr->gr_gid;
259014843421SMatthew Ahrens 		}
259114843421SMatthew Ahrens 	} else {
259214843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
259314843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
259414843421SMatthew Ahrens 		idmap_rid_t rid;
259514843421SMatthew Ahrens 		char *mapdomain;
259614843421SMatthew Ahrens 
259714843421SMatthew Ahrens 		if (*end != '\0')
259814843421SMatthew Ahrens 			return (EINVAL);
259914843421SMatthew Ahrens 		if (id > MAXUID) {
260014843421SMatthew Ahrens 			/* It's an ephemeral ID. */
260114843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
260214843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
260314843421SMatthew Ahrens 				return (ENOENT);
26043b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
260514843421SMatthew Ahrens 			*ridp = rid;
260614843421SMatthew Ahrens 		} else {
260714843421SMatthew Ahrens 			*ridp = id;
260814843421SMatthew Ahrens 		}
260914843421SMatthew Ahrens 	}
261014843421SMatthew Ahrens 
26113b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
261214843421SMatthew Ahrens 	return (0);
261314843421SMatthew Ahrens }
261414843421SMatthew Ahrens 
2615edea4b55SLin Ling static int
2616edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2617edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
261814843421SMatthew Ahrens {
261914843421SMatthew Ahrens 	int err;
262014843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
262114843421SMatthew Ahrens 
262219b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
262314843421SMatthew Ahrens 
262414843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
262514843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2626edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2627edea4b55SLin Ling 	zc.zc_objset_type = *typep;
262814843421SMatthew Ahrens 	if (err)
262914843421SMatthew Ahrens 		return (err);
263014843421SMatthew Ahrens 
263114843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
263214843421SMatthew Ahrens 	if (err)
263314843421SMatthew Ahrens 		return (err);
263414843421SMatthew Ahrens 
2635edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2636edea4b55SLin Ling 	return (0);
2637edea4b55SLin Ling }
2638edea4b55SLin Ling 
2639edea4b55SLin Ling int
2640edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2641edea4b55SLin Ling     uint64_t *propvalue)
2642edea4b55SLin Ling {
2643edea4b55SLin Ling 	zfs_userquota_prop_t type;
2644edea4b55SLin Ling 
2645edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2646edea4b55SLin Ling 	    &type));
2647edea4b55SLin Ling }
2648edea4b55SLin Ling 
2649edea4b55SLin Ling int
2650edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2651edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2652edea4b55SLin Ling {
2653edea4b55SLin Ling 	int err;
2654edea4b55SLin Ling 	uint64_t propvalue;
2655edea4b55SLin Ling 	zfs_userquota_prop_t type;
2656edea4b55SLin Ling 
2657edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2658edea4b55SLin Ling 	    &type);
2659edea4b55SLin Ling 
2660edea4b55SLin Ling 	if (err)
2661edea4b55SLin Ling 		return (err);
2662edea4b55SLin Ling 
266314843421SMatthew Ahrens 	if (literal) {
2664edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2665edea4b55SLin Ling 	} else if (propvalue == 0 &&
266614843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
266714843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
266814843421SMatthew Ahrens 	} else {
2669edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
267014843421SMatthew Ahrens 	}
267114843421SMatthew Ahrens 	return (0);
267214843421SMatthew Ahrens }
267314843421SMatthew Ahrens 
267419b94df9SMatthew Ahrens int
267519b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
267619b94df9SMatthew Ahrens     uint64_t *propvalue)
26778ac09fceSRichard Lowe {
267819b94df9SMatthew Ahrens 	int err;
267919b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
268019b94df9SMatthew Ahrens 	const char *snapname;
2681ebedde84SEric Taylor 
268219b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2683e5351341SMatthew Ahrens 
268419b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
268519b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
268619b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
268719b94df9SMatthew Ahrens 	} else {
268819b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
268919b94df9SMatthew Ahrens 		char *cp;
2690e5351341SMatthew Ahrens 
269119b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
269219b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
269319b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
269419b94df9SMatthew Ahrens 		if (cp != NULL)
269519b94df9SMatthew Ahrens 			*cp = '\0';
269619b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
269719b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
26988ac09fceSRichard Lowe 	}
269919b94df9SMatthew Ahrens 
270019b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
270119b94df9SMatthew Ahrens 	if (err)
270219b94df9SMatthew Ahrens 		return (err);
270319b94df9SMatthew Ahrens 
270419b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
270519b94df9SMatthew Ahrens 	return (0);
2706ebedde84SEric Taylor }
2707ebedde84SEric Taylor 
2708fa9e4066Sahrens int
270919b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
271019b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
2711fa9e4066Sahrens {
271219b94df9SMatthew Ahrens 	int err;
271319b94df9SMatthew Ahrens 	uint64_t propvalue;
27143cb34c60Sahrens 
271519b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2716ebedde84SEric Taylor 
271719b94df9SMatthew Ahrens 	if (err)
271819b94df9SMatthew Ahrens 		return (err);
27198ac09fceSRichard Lowe 
272019b94df9SMatthew Ahrens 	if (literal) {
272119b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
272219b94df9SMatthew Ahrens 	} else {
272319b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
2724fa9e4066Sahrens 	}
272519b94df9SMatthew Ahrens 	return (0);
27267f7322feSeschrock }
27277f7322feSeschrock 
27287f7322feSeschrock /*
272919b94df9SMatthew Ahrens  * Returns the name of the given zfs handle.
27307f7322feSeschrock  */
273119b94df9SMatthew Ahrens const char *
273219b94df9SMatthew Ahrens zfs_get_name(const zfs_handle_t *zhp)
27337f7322feSeschrock {
273419b94df9SMatthew Ahrens 	return (zhp->zfs_name);
273519b94df9SMatthew Ahrens }
27368ac09fceSRichard Lowe 
273719b94df9SMatthew Ahrens /*
273819b94df9SMatthew Ahrens  * Returns the type of the given zfs handle.
273919b94df9SMatthew Ahrens  */
274019b94df9SMatthew Ahrens zfs_type_t
274119b94df9SMatthew Ahrens zfs_get_type(const zfs_handle_t *zhp)
274219b94df9SMatthew Ahrens {
274319b94df9SMatthew Ahrens 	return (zhp->zfs_type);
27447f7322feSeschrock }
27457f7322feSeschrock 
2746d41c4376SMark J Musante /*
2747d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
2748d41c4376SMark J Musante  *
2749d41c4376SMark J Musante  * Needs to handle these cases:
2750d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2751d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2752d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
2753d41c4376SMark J Musante  */
2754d41c4376SMark J Musante static boolean_t
2755d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
2756d41c4376SMark J Musante {
2757d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
2758d41c4376SMark J Musante 
2759d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
2760d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
2761d41c4376SMark J Musante 		return (B_FALSE);
2762d41c4376SMark J Musante 
2763d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
2764d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2765d41c4376SMark J Musante }
2766d41c4376SMark J Musante 
2767fa9e4066Sahrens /*
2768fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
276919b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
277019b94df9SMatthew Ahrens  * pool).
2771fa9e4066Sahrens  */
2772fa9e4066Sahrens static int
2773fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
2774fa9e4066Sahrens {
277519b94df9SMatthew Ahrens 	char *slashp;
2776fa9e4066Sahrens 
277719b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
2778fa9e4066Sahrens 
277919b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
278019b94df9SMatthew Ahrens 		return (-1);
278119b94df9SMatthew Ahrens 	*slashp = '\0';
2782fa9e4066Sahrens 
2783fa9e4066Sahrens 	return (0);
2784fa9e4066Sahrens }
2785fa9e4066Sahrens 
2786fa9e4066Sahrens /*
27877f1f55eaSvb  * If accept_ancestor is false, then check to make sure that the given path has
27887f1f55eaSvb  * a parent, and that it exists.  If accept_ancestor is true, then find the
27897f1f55eaSvb  * closest existing ancestor for the given path.  In prefixlen return the
27907f1f55eaSvb  * length of already existing prefix of the given path.  We also fetch the
27917f1f55eaSvb  * 'zoned' property, which is used to validate property settings when creating
27927f1f55eaSvb  * new datasets.
2793fa9e4066Sahrens  */
2794fa9e4066Sahrens static int
27957f1f55eaSvb check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27967f1f55eaSvb     boolean_t accept_ancestor, int *prefixlen)
2797fa9e4066Sahrens {
2798fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2799fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
2800fa9e4066Sahrens 	char *slash;
28017f7322feSeschrock 	zfs_handle_t *zhp;
280299653d4eSeschrock 	char errbuf[1024];
2803d41c4376SMark J Musante 	uint64_t is_zoned;
280499653d4eSeschrock 
2805deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
2806deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2807fa9e4066Sahrens 
2808fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
2809fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
281099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
281199653d4eSeschrock 		    "missing dataset name"));
281299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2813fa9e4066Sahrens 	}
2814fa9e4066Sahrens 
2815fa9e4066Sahrens 	/* check to see if the pool exists */
2816fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2817fa9e4066Sahrens 		slash = parent + strlen(parent);
28188ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
2819fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
282099653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2821fa9e4066Sahrens 	    errno == ENOENT) {
282299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
282399653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
282499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2825fa9e4066Sahrens 	}
2826fa9e4066Sahrens 
2827fa9e4066Sahrens 	/* check to see if the parent dataset exists */
28287f1f55eaSvb 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
28297f1f55eaSvb 		if (errno == ENOENT && accept_ancestor) {
28307f1f55eaSvb 			/*
28317f1f55eaSvb 			 * Go deeper to find an ancestor, give up on top level.
28327f1f55eaSvb 			 */
28337f1f55eaSvb 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
28347f1f55eaSvb 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28357f1f55eaSvb 				    "no such pool '%s'"), zc.zc_name);
28367f1f55eaSvb 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
28377f1f55eaSvb 			}
28387f1f55eaSvb 		} else if (errno == ENOENT) {
283999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
284099653d4eSeschrock 			    "parent does not exist"));
284199653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
28427f1f55eaSvb 		} else
284399653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2844fa9e4066Sahrens 	}
2845fa9e4066Sahrens 
2846d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2847d41c4376SMark J Musante 	if (zoned != NULL)
2848d41c4376SMark J Musante 		*zoned = is_zoned;
2849d41c4376SMark J Musante 
2850fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
2851d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
285299653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
28537f7322feSeschrock 		zfs_close(zhp);
2854fa9e4066Sahrens 		return (-1);
2855fa9e4066Sahrens 	}
2856fa9e4066Sahrens 
2857fa9e4066Sahrens 	/* make sure parent is a filesystem */
28587f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
285999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
286099653d4eSeschrock 		    "parent is not a filesystem"));
286199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
28627f7322feSeschrock 		zfs_close(zhp);
2863fa9e4066Sahrens 		return (-1);
2864fa9e4066Sahrens 	}
2865fa9e4066Sahrens 
28667f7322feSeschrock 	zfs_close(zhp);
28677f1f55eaSvb 	if (prefixlen != NULL)
28687f1f55eaSvb 		*prefixlen = strlen(parent);
28697f1f55eaSvb 	return (0);
28707f1f55eaSvb }
28717f1f55eaSvb 
28727f1f55eaSvb /*
28737f1f55eaSvb  * Finds whether the dataset of the given type(s) exists.
28747f1f55eaSvb  */
28757f1f55eaSvb boolean_t
28767f1f55eaSvb zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
28777f1f55eaSvb {
28787f1f55eaSvb 	zfs_handle_t *zhp;
28797f1f55eaSvb 
2880f18faf3fSek 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
28817f1f55eaSvb 		return (B_FALSE);
28827f1f55eaSvb 
28837f1f55eaSvb 	/*
28847f1f55eaSvb 	 * Try to get stats for the dataset, which will tell us if it exists.
28857f1f55eaSvb 	 */
28867f1f55eaSvb 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
28877f1f55eaSvb 		int ds_type = zhp->zfs_type;
28887f1f55eaSvb 
28897f1f55eaSvb 		zfs_close(zhp);
28907f1f55eaSvb 		if (types & ds_type)
28917f1f55eaSvb 			return (B_TRUE);
28927f1f55eaSvb 	}
28937f1f55eaSvb 	return (B_FALSE);
28947f1f55eaSvb }
28957f1f55eaSvb 
28963cb34c60Sahrens /*
28973cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
28983cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
28993cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
29003cb34c60Sahrens  */
29013cb34c60Sahrens int
29023cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
29033cb34c60Sahrens {
29043cb34c60Sahrens 	zfs_handle_t *h;
29053cb34c60Sahrens 	char *cp;
29063cb34c60Sahrens 	const char *opname;
29073cb34c60Sahrens 
29083cb34c60Sahrens 	/* make sure prefix exists */
29093cb34c60Sahrens 	cp = target + prefixlen;
29103cb34c60Sahrens 	if (*cp != '/') {
29113cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
29123cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
29133cb34c60Sahrens 	} else {
29143cb34c60Sahrens 		*cp = '\0';
29153cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
29163cb34c60Sahrens 		*cp = '/';
29173cb34c60Sahrens 	}
29183cb34c60Sahrens 	if (h == NULL)
29193cb34c60Sahrens 		return (-1);
29203cb34c60Sahrens 	zfs_close(h);
29213cb34c60Sahrens 
29223cb34c60Sahrens 	/*
29233cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
29243cb34c60Sahrens 	 * up to the prefixlen-long one.
29253cb34c60Sahrens 	 */
29263cb34c60Sahrens 	for (cp = target + prefixlen + 1;
29273cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
29283cb34c60Sahrens 
29293cb34c60Sahrens 		*cp = '\0';
29303cb34c60Sahrens 
29313cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
29323cb34c60Sahrens 		if (h) {
29333cb34c60Sahrens 			/* it already exists, nothing to do here */
29343cb34c60Sahrens 			zfs_close(h);
29353cb34c60Sahrens 			continue;
29363cb34c60Sahrens 		}
29373cb34c60Sahrens 
29383cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
29393cb34c60Sahrens 		    NULL) != 0) {
29403cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
29413cb34c60Sahrens 			goto ancestorerr;
29423cb34c60Sahrens 		}
29433cb34c60Sahrens 
29443cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
29453cb34c60Sahrens 		if (h == NULL) {
29463cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
29473cb34c60Sahrens 			goto ancestorerr;
29483cb34c60Sahrens 		}
29493cb34c60Sahrens 
29503cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
29513cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
29523cb34c60Sahrens 			goto ancestorerr;
29533cb34c60Sahrens 		}
29543cb34c60Sahrens 
29553cb34c60Sahrens 		if (zfs_share(h) != 0) {
29563cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
29573cb34c60Sahrens 			goto ancestorerr;
29583cb34c60Sahrens 		}
29593cb34c60Sahrens 
29603cb34c60Sahrens 		zfs_close(h);
29613cb34c60Sahrens 	}
29623cb34c60Sahrens 
29633cb34c60Sahrens 	return (0);
29643cb34c60Sahrens 
29653cb34c60Sahrens ancestorerr:
29663cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29673cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
29683cb34c60Sahrens 	return (-1);
29693cb34c60Sahrens }
29703cb34c60Sahrens 
29717f1f55eaSvb /*
29727f1f55eaSvb  * Creates non-existing ancestors of the given path.
29737f1f55eaSvb  */
29747f1f55eaSvb int
29757f1f55eaSvb zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
29767f1f55eaSvb {
29777f1f55eaSvb 	int prefix;
29787f1f55eaSvb 	char *path_copy;
29797f1f55eaSvb 	int rc;
29807f1f55eaSvb 
2981d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
29827f1f55eaSvb 		return (-1);
29837f1f55eaSvb 
29847f1f55eaSvb 	if ((path_copy = strdup(path)) != NULL) {
29857f1f55eaSvb 		rc = create_parents(hdl, path_copy, prefix);
29867f1f55eaSvb 		free(path_copy);
29877f1f55eaSvb 	}
29887f1f55eaSvb 	if (path_copy == NULL || rc != 0)
29897f1f55eaSvb 		return (-1);
29907f1f55eaSvb 
2991fa9e4066Sahrens 	return (0);
2992fa9e4066Sahrens }
2993fa9e4066Sahrens 
2994fa9e4066Sahrens /*
2995e9dbad6fSeschrock  * Create a new filesystem or volume.
2996fa9e4066Sahrens  */
2997fa9e4066Sahrens int
299899653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2999e9dbad6fSeschrock     nvlist_t *props)
3000fa9e4066Sahrens {
3001fa9e4066Sahrens 	int ret;
3002fa9e4066Sahrens 	uint64_t size = 0;
3003fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
300499653d4eSeschrock 	char errbuf[1024];
3005e9dbad6fSeschrock 	uint64_t zoned;
30064445fffbSMatthew Ahrens 	dmu_objset_type_t ost;
300799653d4eSeschrock 
300899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
300999653d4eSeschrock 	    "cannot create '%s'"), path);
3010fa9e4066Sahrens 
3011fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3012f18faf3fSek 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
301399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3014fa9e4066Sahrens 
3015fa9e4066Sahrens 	/* validate parents exist */
30167f1f55eaSvb 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3017fa9e4066Sahrens 		return (-1);
3018fa9e4066Sahrens 
3019fa9e4066Sahrens 	/*
3020fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3021fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3022fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3023fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3024fa9e4066Sahrens 	 * first try to see if the dataset exists.
3025fa9e4066Sahrens 	 */
30264445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
302799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
302899653d4eSeschrock 		    "dataset already exists"));
302999653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3030fa9e4066Sahrens 	}
3031fa9e4066Sahrens 
3032fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
30334445fffbSMatthew Ahrens 		ost = DMU_OST_ZVOL;
3034fa9e4066Sahrens 	else
30354445fffbSMatthew Ahrens 		ost = DMU_OST_ZFS;
3036fa9e4066Sahrens 
30370a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3038b1b8ab34Slling 	    zoned, NULL, errbuf)) == 0)
3039e9dbad6fSeschrock 		return (-1);
3040e9dbad6fSeschrock 
3041fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
30425c5460e9Seschrock 		/*
30435c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
30445c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
30455c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
30465c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
30475c5460e9Seschrock 		 * zero.
30485c5460e9Seschrock 		 */
3049e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3050e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3051e9dbad6fSeschrock 			nvlist_free(props);
305299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3053e9dbad6fSeschrock 			    "missing volume size"));
3054e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3055e9dbad6fSeschrock 		}
3056e9dbad6fSeschrock 
3057e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3058e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3059e9dbad6fSeschrock 		    &blocksize)) != 0) {
3060e9dbad6fSeschrock 			if (ret == ENOENT) {
3061e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3062e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3063e9dbad6fSeschrock 			} else {
3064e9dbad6fSeschrock 				nvlist_free(props);
3065e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3066e9dbad6fSeschrock 				    "missing volume block size"));
3067e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3068e9dbad6fSeschrock 			}
3069fa9e4066Sahrens 		}
3070fa9e4066Sahrens 
3071e9dbad6fSeschrock 		if (size == 0) {
3072e9dbad6fSeschrock 			nvlist_free(props);
307399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3074e9dbad6fSeschrock 			    "volume size cannot be zero"));
3075e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30765c5460e9Seschrock 		}
30775c5460e9Seschrock 
30785c5460e9Seschrock 		if (size % blocksize != 0) {
3079e9dbad6fSeschrock 			nvlist_free(props);
308099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3081e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3082e9dbad6fSeschrock 			    "size"));
3083e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30845c5460e9Seschrock 		}
3085fa9e4066Sahrens 	}
3086fa9e4066Sahrens 
3087fa9e4066Sahrens 	/* create the dataset */
30884445fffbSMatthew Ahrens 	ret = lzc_create(path, ost, props);
30894445fffbSMatthew Ahrens 	nvlist_free(props);
3090e9dbad6fSeschrock 
3091fa9e4066Sahrens 	/* check for failure */
3092fa9e4066Sahrens 	if (ret != 0) {
3093fa9e4066Sahrens 		char parent[ZFS_MAXNAMELEN];
3094fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3095fa9e4066Sahrens 
3096fa9e4066Sahrens 		switch (errno) {
3097fa9e4066Sahrens 		case ENOENT:
309899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309999653d4eSeschrock 			    "no such parent '%s'"), parent);
310099653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3101fa9e4066Sahrens 
3102fa9e4066Sahrens 		case EINVAL:
310399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3104d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
310599653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3106fa9e4066Sahrens 
3107fa9e4066Sahrens 		case EDOM:
310899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3109e9dbad6fSeschrock 			    "volume block size must be power of 2 from "
3110e9dbad6fSeschrock 			    "%u to %uk"),
3111fa9e4066Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3112fa9e4066Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
311399653d4eSeschrock 
3114e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
311599653d4eSeschrock 
311640feaa91Sahrens 		case ENOTSUP:
311740feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
311840feaa91Sahrens 			    "pool must be upgraded to set this "
311940feaa91Sahrens 			    "property or value"));
312040feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3121fa9e4066Sahrens #ifdef _ILP32
3122fa9e4066Sahrens 		case EOVERFLOW:
3123fa9e4066Sahrens 			/*
3124fa9e4066Sahrens 			 * This platform can't address a volume this big.
3125fa9e4066Sahrens 			 */
312699653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
312799653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
312899653d4eSeschrock 				    errbuf));
3129fa9e4066Sahrens #endif
313099653d4eSeschrock 			/* FALLTHROUGH */
3131fa9e4066Sahrens 		default:
313299653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3133fa9e4066Sahrens 		}
3134fa9e4066Sahrens 	}
3135fa9e4066Sahrens 
3136fa9e4066Sahrens 	return (0);
3137fa9e4066Sahrens }
3138fa9e4066Sahrens 
3139fa9e4066Sahrens /*
3140fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
314165fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
314265fec9f6SChristopher Siden  * does not exist this function does nothing.
3143fa9e4066Sahrens  */
3144fa9e4066Sahrens int
3145842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3146fa9e4066Sahrens {
3147fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3148fa9e4066Sahrens 
3149*78f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3150*78f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
3151*78f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
3152*78f17100SMatthew Ahrens 		int error = lzc_destroy_bookmarks(nv, NULL);
3153*78f17100SMatthew Ahrens 		fnvlist_free(nv);
3154*78f17100SMatthew Ahrens 		if (error != 0) {
3155*78f17100SMatthew Ahrens 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3156*78f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3157*78f17100SMatthew Ahrens 			    zhp->zfs_name));
3158*78f17100SMatthew Ahrens 		}
3159*78f17100SMatthew Ahrens 		return (0);
3160*78f17100SMatthew Ahrens 	}
3161*78f17100SMatthew Ahrens 
3162fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3163fa9e4066Sahrens 
3164e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3165fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3166fa9e4066Sahrens 	} else {
3167fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3168fa9e4066Sahrens 	}
3169fa9e4066Sahrens 
3170842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
317165fec9f6SChristopher Siden 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
317265fec9f6SChristopher Siden 	    errno != ENOENT) {
3173ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
317499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
317599653d4eSeschrock 		    zhp->zfs_name));
31761d452cf5Sahrens 	}
3177fa9e4066Sahrens 
3178fa9e4066Sahrens 	remove_mountpoint(zhp);
3179fa9e4066Sahrens 
3180fa9e4066Sahrens 	return (0);
3181fa9e4066Sahrens }
3182fa9e4066Sahrens 
31831d452cf5Sahrens struct destroydata {
318419b94df9SMatthew Ahrens 	nvlist_t *nvl;
318519b94df9SMatthew Ahrens 	const char *snapname;
31861d452cf5Sahrens };
31871d452cf5Sahrens 
31881d452cf5Sahrens static int
3189681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
31901d452cf5Sahrens {
31911d452cf5Sahrens 	struct destroydata *dd = arg;
31921d452cf5Sahrens 	char name[ZFS_MAXNAMELEN];
3193681d9761SEric Taylor 	int rv = 0;
31941d452cf5Sahrens 
319519b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
319619b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
31971d452cf5Sahrens 
3198a7a845e4SSteven Hartland 	if (lzc_exists(name))
319919b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
32001d452cf5Sahrens 
320119b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
320219b94df9SMatthew Ahrens 	zfs_close(zhp);
32033ccfa83cSahrens 	return (rv);
32041d452cf5Sahrens }
32051d452cf5Sahrens 
32061d452cf5Sahrens /*
32071d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
32081d452cf5Sahrens  */
32091d452cf5Sahrens int
3210842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
32111d452cf5Sahrens {
32121d452cf5Sahrens 	int ret;
32131d452cf5Sahrens 	struct destroydata dd = { 0 };
32141d452cf5Sahrens 
32151d452cf5Sahrens 	dd.snapname = snapname;
321619b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
321719b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
32181d452cf5Sahrens 
3219a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
322019b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
32211d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
322219b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
322319b94df9SMatthew Ahrens 	} else {
32243b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
32251d452cf5Sahrens 	}
322619b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
322719b94df9SMatthew Ahrens 	return (ret);
322819b94df9SMatthew Ahrens }
322919b94df9SMatthew Ahrens 
323019b94df9SMatthew Ahrens /*
32313b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
323219b94df9SMatthew Ahrens  */
323319b94df9SMatthew Ahrens int
32343b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
323519b94df9SMatthew Ahrens {
323619b94df9SMatthew Ahrens 	int ret;
32374445fffbSMatthew Ahrens 	nvlist_t *errlist;
32381d452cf5Sahrens 
32394445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
32401d452cf5Sahrens 
32413b2aab18SMatthew Ahrens 	if (ret == 0)
32423b2aab18SMatthew Ahrens 		return (0);
32434445fffbSMatthew Ahrens 
3244a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
32453b2aab18SMatthew Ahrens 		char errbuf[1024];
32463b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
32473b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
32483b2aab18SMatthew Ahrens 
32493b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
32503b2aab18SMatthew Ahrens 	}
32513b2aab18SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
32523b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
32533b2aab18SMatthew Ahrens 		char errbuf[1024];
32543b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
32553b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
32563b2aab18SMatthew Ahrens 		    nvpair_name(pair));
32573b2aab18SMatthew Ahrens 
32583b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
32593b2aab18SMatthew Ahrens 		case EEXIST:
32603b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
32613b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
32623b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
32633b2aab18SMatthew Ahrens 			break;
32643b2aab18SMatthew Ahrens 		default:
32653b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
32663b2aab18SMatthew Ahrens 			break;
32671d452cf5Sahrens 		}
32681d452cf5Sahrens 	}
32691d452cf5Sahrens 
32704445fffbSMatthew Ahrens 	return (ret);
32711d452cf5Sahrens }
32721d452cf5Sahrens 
3273fa9e4066Sahrens /*
3274fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3275fa9e4066Sahrens  */
3276fa9e4066Sahrens int
3277e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3278fa9e4066Sahrens {
3279fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
3280fa9e4066Sahrens 	int ret;
328199653d4eSeschrock 	char errbuf[1024];
328299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3283e9dbad6fSeschrock 	uint64_t zoned;
3284fa9e4066Sahrens 
3285fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3286fa9e4066Sahrens 
328799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
328899653d4eSeschrock 	    "cannot create '%s'"), target);
328999653d4eSeschrock 
329019b94df9SMatthew Ahrens 	/* validate the target/clone name */
3291f18faf3fSek 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
329299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3293fa9e4066Sahrens 
3294fa9e4066Sahrens 	/* validate parents exist */
32957f1f55eaSvb 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3296fa9e4066Sahrens 		return (-1);
3297fa9e4066Sahrens 
3298fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3299fa9e4066Sahrens 
3300fa9e4066Sahrens 	/* do the clone */
3301e9dbad6fSeschrock 
3302e9dbad6fSeschrock 	if (props) {
33034445fffbSMatthew Ahrens 		zfs_type_t type;
33044445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
33054445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
33064445fffbSMatthew Ahrens 		} else {
33074445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
33084445fffbSMatthew Ahrens 		}
33090a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
33100a48a24eStimh 		    zhp, errbuf)) == NULL)
3311e9dbad6fSeschrock 			return (-1);
3312e9dbad6fSeschrock 	}
3313fa9e4066Sahrens 
33144445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
33154445fffbSMatthew Ahrens 	nvlist_free(props);
3316e9dbad6fSeschrock 
3317fa9e4066Sahrens 	if (ret != 0) {
3318fa9e4066Sahrens 		switch (errno) {
3319fa9e4066Sahrens 
3320fa9e4066Sahrens 		case ENOENT:
3321fa9e4066Sahrens 			/*
3322fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3323fa9e4066Sahrens 			 * above, but there may a race condition that has since
3324fa9e4066Sahrens 			 * destroyed the parent.
3325fa9e4066Sahrens 			 *
3326fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3327fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3328fa9e4066Sahrens 			 * dataset doesn't exist.
3329fa9e4066Sahrens 			 */
333099653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
333199653d4eSeschrock 			    "no such parent '%s'"), parent);
333299653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3333fa9e4066Sahrens 
333499653d4eSeschrock 		case EXDEV:
333599653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
333699653d4eSeschrock 			    "source and target pools differ"));
333799653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
333899653d4eSeschrock 			    errbuf));
3339fa9e4066Sahrens 
334099653d4eSeschrock 		default:
334199653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
334299653d4eSeschrock 			    errbuf));
334399653d4eSeschrock 		}
334499653d4eSeschrock 	}
3345fa9e4066Sahrens 
334699653d4eSeschrock 	return (ret);
334799653d4eSeschrock }
334899653d4eSeschrock 
334999653d4eSeschrock /*
335099653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
335199653d4eSeschrock  */
335299653d4eSeschrock int
335399653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
335499653d4eSeschrock {
335599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
335699653d4eSeschrock 	zfs_cmd_t zc = { 0 };
335799653d4eSeschrock 	char parent[MAXPATHLEN];
335899653d4eSeschrock 	int ret;
335999653d4eSeschrock 	char errbuf[1024];
336099653d4eSeschrock 
336199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
336299653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
336399653d4eSeschrock 
336499653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
336599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
336699653d4eSeschrock 		    "snapshots can not be promoted"));
336799653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
336899653d4eSeschrock 	}
336999653d4eSeschrock 
33703cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
337199653d4eSeschrock 	if (parent[0] == '\0') {
337299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
337399653d4eSeschrock 		    "not a cloned filesystem"));
337499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
337599653d4eSeschrock 	}
337699653d4eSeschrock 
33773cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3378e9dbad6fSeschrock 	    sizeof (zc.zc_value));
337999653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3380ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
338199653d4eSeschrock 
338299653d4eSeschrock 	if (ret != 0) {
33830b69c2f0Sahrens 		int save_errno = errno;
33840b69c2f0Sahrens 
33850b69c2f0Sahrens 		switch (save_errno) {
338699653d4eSeschrock 		case EEXIST:
3387681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
338899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3389681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3390681d9761SEric Taylor 			    zc.zc_string, parent);
339199653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3392fa9e4066Sahrens 
3393fa9e4066Sahrens 		default:
33940b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3395fa9e4066Sahrens 		}
3396fa9e4066Sahrens 	}
3397e9dbad6fSeschrock 	return (ret);
33981d452cf5Sahrens }
33991d452cf5Sahrens 
34004445fffbSMatthew Ahrens typedef struct snapdata {
34014445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
34024445fffbSMatthew Ahrens 	const char *sd_snapname;
34034445fffbSMatthew Ahrens } snapdata_t;
34044445fffbSMatthew Ahrens 
34054445fffbSMatthew Ahrens static int
34064445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
34074445fffbSMatthew Ahrens {
34084445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
34094445fffbSMatthew Ahrens 	char name[ZFS_MAXNAMELEN];
34104445fffbSMatthew Ahrens 	int rv = 0;
34114445fffbSMatthew Ahrens 
3412ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3413ca48f36fSKeith M Wesolowski 		(void) snprintf(name, sizeof (name),
3414ca48f36fSKeith M Wesolowski 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
34154445fffbSMatthew Ahrens 
3416ca48f36fSKeith M Wesolowski 		fnvlist_add_boolean(sd->sd_nvl, name);
34174445fffbSMatthew Ahrens 
3418ca48f36fSKeith M Wesolowski 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3419ca48f36fSKeith M Wesolowski 	}
34204445fffbSMatthew Ahrens 	zfs_close(zhp);
3421ca48f36fSKeith M Wesolowski 
34224445fffbSMatthew Ahrens 	return (rv);
34234445fffbSMatthew Ahrens }
34244445fffbSMatthew Ahrens 
3425fa9e4066Sahrens /*
34264445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
34274445fffbSMatthew Ahrens  * created.
3428fa9e4066Sahrens  */
3429fa9e4066Sahrens int
34304445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3431fa9e4066Sahrens {
3432fa9e4066Sahrens 	int ret;
343399653d4eSeschrock 	char errbuf[1024];
34344445fffbSMatthew Ahrens 	nvpair_t *elem;
34354445fffbSMatthew Ahrens 	nvlist_t *errors;
3436fa9e4066Sahrens 
343799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34384445fffbSMatthew Ahrens 	    "cannot create snapshots "));
343999653d4eSeschrock 
34404445fffbSMatthew Ahrens 	elem = NULL;
34414445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
34424445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
34434445fffbSMatthew Ahrens 
34444445fffbSMatthew Ahrens 		/* validate the target name */
34454445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
34464445fffbSMatthew Ahrens 		    B_TRUE)) {
34474445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
34484445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
34494445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
34504445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
34514445fffbSMatthew Ahrens 		}
34524445fffbSMatthew Ahrens 	}
3453fa9e4066Sahrens 
34544445fffbSMatthew Ahrens 	if (props != NULL &&
34554445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
34564445fffbSMatthew Ahrens 	    props, B_FALSE, NULL, errbuf)) == NULL) {
34574445fffbSMatthew Ahrens 		return (-1);
34584445fffbSMatthew Ahrens 	}
3459bb0ade09Sahrens 
34604445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
34614445fffbSMatthew Ahrens 
34624445fffbSMatthew Ahrens 	if (ret != 0) {
34634445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
34644445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
34654445fffbSMatthew Ahrens 		    elem != NULL;
34664445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
34674445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
34684445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
34694445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
34704445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
34714445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
34724445fffbSMatthew Ahrens 			printed = B_TRUE;
3473bb0ade09Sahrens 		}
34744445fffbSMatthew Ahrens 		if (!printed) {
34754445fffbSMatthew Ahrens 			switch (ret) {
34764445fffbSMatthew Ahrens 			case EXDEV:
34774445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34784445fffbSMatthew Ahrens 				    "multiple snapshots of same "
34794445fffbSMatthew Ahrens 				    "fs not allowed"));
34804445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3481bb0ade09Sahrens 
34824445fffbSMatthew Ahrens 				break;
34834445fffbSMatthew Ahrens 			default:
34844445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
34854445fffbSMatthew Ahrens 			}
34864445fffbSMatthew Ahrens 		}
3487bb0ade09Sahrens 	}
3488bb0ade09Sahrens 
34894445fffbSMatthew Ahrens 	nvlist_free(props);
34904445fffbSMatthew Ahrens 	nvlist_free(errors);
34914445fffbSMatthew Ahrens 	return (ret);
34924445fffbSMatthew Ahrens }
34934445fffbSMatthew Ahrens 
34944445fffbSMatthew Ahrens int
34954445fffbSMatthew Ahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
34964445fffbSMatthew Ahrens     nvlist_t *props)
34974445fffbSMatthew Ahrens {
34984445fffbSMatthew Ahrens 	int ret;
34994445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
35004445fffbSMatthew Ahrens 	char fsname[ZFS_MAXNAMELEN];
35014445fffbSMatthew Ahrens 	char *cp;
35024445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
35034445fffbSMatthew Ahrens 	char errbuf[1024];
35044445fffbSMatthew Ahrens 
35054445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35064445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
35074445fffbSMatthew Ahrens 
35084445fffbSMatthew Ahrens 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
35094445fffbSMatthew Ahrens 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35104445fffbSMatthew Ahrens 
35114445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
35124445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
35134445fffbSMatthew Ahrens 	*cp = '\0';
35144445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
3515fa9e4066Sahrens 
35164445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3517fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3518fa9e4066Sahrens 		return (-1);
3519fa9e4066Sahrens 	}
3520fa9e4066Sahrens 
35214445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
35224445fffbSMatthew Ahrens 	if (recursive) {
35234445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
35244445fffbSMatthew Ahrens 	} else {
35254445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
3526681d9761SEric Taylor 	}
3527fa9e4066Sahrens 
35284445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
35294445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
3530fa9e4066Sahrens 	zfs_close(zhp);
3531fa9e4066Sahrens 	return (ret);
3532fa9e4066Sahrens }
3533fa9e4066Sahrens 
3534fa9e4066Sahrens /*
3535b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3536b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3537b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3538b12a1c38Slling  * group.
3539fa9e4066Sahrens  */
3540b12a1c38Slling typedef struct rollback_data {
3541b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3542b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3543c391e322Sahrens 	boolean_t	cb_error;
3544c391e322Sahrens 	boolean_t	cb_force;
3545b12a1c38Slling } rollback_data_t;
3546b12a1c38Slling 
3547b12a1c38Slling static int
3548*78f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3549b12a1c38Slling {
3550b12a1c38Slling 	rollback_data_t *cbp = data;
3551*78f17100SMatthew Ahrens 	prop_changelist_t *clp;
3552*78f17100SMatthew Ahrens 
3553*78f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
3554*78f17100SMatthew Ahrens 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3555*78f17100SMatthew Ahrens 	    cbp->cb_force ? MS_FORCE: 0);
3556*78f17100SMatthew Ahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
3557*78f17100SMatthew Ahrens 		cbp->cb_error = B_TRUE;
3558*78f17100SMatthew Ahrens 		zfs_close(zhp);
3559*78f17100SMatthew Ahrens 		return (0);
3560*78f17100SMatthew Ahrens 	}
3561*78f17100SMatthew Ahrens 	if (zfs_destroy(zhp, B_FALSE) != 0)
3562*78f17100SMatthew Ahrens 		cbp->cb_error = B_TRUE;
3563*78f17100SMatthew Ahrens 	else
3564*78f17100SMatthew Ahrens 		changelist_remove(clp, zhp->zfs_name);
3565*78f17100SMatthew Ahrens 	(void) changelist_postfix(clp);
3566*78f17100SMatthew Ahrens 	changelist_free(clp);
3567b12a1c38Slling 
3568*78f17100SMatthew Ahrens 	zfs_close(zhp);
3569*78f17100SMatthew Ahrens 	return (0);
3570*78f17100SMatthew Ahrens }
3571b12a1c38Slling 
3572*78f17100SMatthew Ahrens static int
3573*78f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
3574*78f17100SMatthew Ahrens {
3575*78f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
3576b12a1c38Slling 
3577*78f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3578*78f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3579*78f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
3580c391e322Sahrens 
3581*78f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3582b12a1c38Slling 	}
3583b12a1c38Slling 
3584b12a1c38Slling 	zfs_close(zhp);
3585b12a1c38Slling 	return (0);
3586b12a1c38Slling }
3587b12a1c38Slling 
3588b12a1c38Slling /*
3589b12a1c38Slling  * Given a dataset, rollback to a specific snapshot, discarding any
3590b12a1c38Slling  * data changes since then and making it the active dataset.
3591b12a1c38Slling  *
3592*78f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
3593*78f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
3594b12a1c38Slling  */
3595b12a1c38Slling int
3596c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3597b12a1c38Slling {
3598b12a1c38Slling 	rollback_data_t cb = { 0 };
35994ccbb6e7Sahrens 	int err;
36007b97dc1aSrm 	boolean_t restore_resv = 0;
36017b97dc1aSrm 	uint64_t old_volsize, new_volsize;
36027b97dc1aSrm 	zfs_prop_t resv_prop;
3603b12a1c38Slling 
36044ccbb6e7Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
36054ccbb6e7Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3606b12a1c38Slling 
3607b12a1c38Slling 	/*
36082e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
3609b12a1c38Slling 	 */
3610c391e322Sahrens 	cb.cb_force = force;
3611b12a1c38Slling 	cb.cb_target = snap->zfs_name;
3612b12a1c38Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3613*78f17100SMatthew Ahrens 	(void) zfs_iter_snapshots(zhp, rollback_destroy, &cb);
3614*78f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3615b12a1c38Slling 
3616c391e322Sahrens 	if (cb.cb_error)
3617c391e322Sahrens 		return (-1);
3618b12a1c38Slling 
3619b12a1c38Slling 	/*
3620b12a1c38Slling 	 * Now that we have verified that the snapshot is the latest,
3621b12a1c38Slling 	 * rollback to the given snapshot.
3622b12a1c38Slling 	 */
3623b12a1c38Slling 
36247b97dc1aSrm 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
36257b97dc1aSrm 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
36267b97dc1aSrm 			return (-1);
36277b97dc1aSrm 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36287b97dc1aSrm 		restore_resv =
36297b97dc1aSrm 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
36307b97dc1aSrm 	}
36314ccbb6e7Sahrens 
3632b12a1c38Slling 	/*
36334ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
36344ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
36354ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
36364ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
36374ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
3638b12a1c38Slling 	 */
3639a7027df1SMatthew Ahrens 	err = lzc_rollback(zhp->zfs_name, NULL, 0);
3640a7027df1SMatthew Ahrens 	if (err != 0) {
36414ccbb6e7Sahrens 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
36424ccbb6e7Sahrens 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
36434ccbb6e7Sahrens 		    zhp->zfs_name);
3644b9415e83Srm 		return (err);
3645b9415e83Srm 	}
36467b97dc1aSrm 
36477b97dc1aSrm 	/*
36487b97dc1aSrm 	 * For volumes, if the pre-rollback volsize matched the pre-
36497b97dc1aSrm 	 * rollback reservation and the volsize has changed then set
36507b97dc1aSrm 	 * the reservation property to the post-rollback volsize.
36517b97dc1aSrm 	 * Make a new handle since the rollback closed the dataset.
36527b97dc1aSrm 	 */
3653b9415e83Srm 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3654b9415e83Srm 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
36557b97dc1aSrm 		if (restore_resv) {
36567b97dc1aSrm 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36577b97dc1aSrm 			if (old_volsize != new_volsize)
3658b9415e83Srm 				err = zfs_prop_set_int(zhp, resv_prop,
3659b9415e83Srm 				    new_volsize);
36607b97dc1aSrm 		}
36617b97dc1aSrm 		zfs_close(zhp);
36624ccbb6e7Sahrens 	}
36634ccbb6e7Sahrens 	return (err);
3664b12a1c38Slling }
3665b12a1c38Slling 
3666fa9e4066Sahrens /*
3667fa9e4066Sahrens  * Renames the given dataset.
3668fa9e4066Sahrens  */
3669fa9e4066Sahrens int
36706a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
36716a9cb0eaSEric Schrock     boolean_t force_unmount)
3672fa9e4066Sahrens {
3673fa9e4066Sahrens 	int ret;
3674fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3675fa9e4066Sahrens 	char *delim;
3676cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3677cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3678cdf5b4caSmmusante 	char *parentname = NULL;
3679fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
368099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
368199653d4eSeschrock 	char errbuf[1024];
3682fa9e4066Sahrens 
3683fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3684fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3685fa9e4066Sahrens 		return (0);
3686fa9e4066Sahrens 
368799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
368899653d4eSeschrock 	    "cannot rename to '%s'"), target);
368999653d4eSeschrock 
3690fa9e4066Sahrens 	/*
3691fa9e4066Sahrens 	 * Make sure the target name is valid
3692fa9e4066Sahrens 	 */
3693fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
369498579b20Snd 		if ((strchr(target, '@') == NULL) ||
369598579b20Snd 		    *target == '@') {
369698579b20Snd 			/*
369798579b20Snd 			 * Snapshot target name is abbreviated,
369898579b20Snd 			 * reconstruct full dataset name
369998579b20Snd 			 */
370098579b20Snd 			(void) strlcpy(parent, zhp->zfs_name,
370198579b20Snd 			    sizeof (parent));
370298579b20Snd 			delim = strchr(parent, '@');
370398579b20Snd 			if (strchr(target, '@') == NULL)
370498579b20Snd 				*(++delim) = '\0';
370598579b20Snd 			else
370698579b20Snd 				*delim = '\0';
370798579b20Snd 			(void) strlcat(parent, target, sizeof (parent));
370898579b20Snd 			target = parent;
370998579b20Snd 		} else {
371098579b20Snd 			/*
371198579b20Snd 			 * Make sure we're renaming within the same dataset.
371298579b20Snd 			 */
371398579b20Snd 			delim = strchr(target, '@');
371498579b20Snd 			if (strncmp(zhp->zfs_name, target, delim - target)
371598579b20Snd 			    != 0 || zhp->zfs_name[delim - target] != '@') {
371698579b20Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
371798579b20Snd 				    "snapshots must be part of same "
371898579b20Snd 				    "dataset"));
371998579b20Snd 				return (zfs_error(hdl, EZFS_CROSSTARGET,
3720b1b8ab34Slling 				    errbuf));
372198579b20Snd 			}
3722fa9e4066Sahrens 		}
3723f18faf3fSek 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
372498579b20Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3725fa9e4066Sahrens 	} else {
3726cdf5b4caSmmusante 		if (recursive) {
3727cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3728cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
3729cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3730cdf5b4caSmmusante 		}
3731cdf5b4caSmmusante 
3732f18faf3fSek 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
373398579b20Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3734e9dbad6fSeschrock 
3735fa9e4066Sahrens 		/* validate parents */
3736d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3737fa9e4066Sahrens 			return (-1);
3738fa9e4066Sahrens 
3739fa9e4066Sahrens 		/* make sure we're in the same pool */
3740fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3741fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3742fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
374399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
374499653d4eSeschrock 			    "datasets must be within same pool"));
374599653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3746fa9e4066Sahrens 		}
3747f2fdf992Snd 
3748f2fdf992Snd 		/* new name cannot be a child of the current dataset name */
3749d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
3750f2fdf992Snd 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3751d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
3752f2fdf992Snd 			    "current dataset name"));
3753f2fdf992Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3754f2fdf992Snd 		}
3755fa9e4066Sahrens 	}
3756fa9e4066Sahrens 
375799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
375899653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
375999653d4eSeschrock 
3760fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3761fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
376299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
376399653d4eSeschrock 		    "dataset is used in a non-global zone"));
376499653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3765fa9e4066Sahrens 	}
3766fa9e4066Sahrens 
3767cdf5b4caSmmusante 	if (recursive) {
3768fa9e4066Sahrens 
3769f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3770f0c5ee21Smmusante 		if (parentname == NULL) {
3771f0c5ee21Smmusante 			ret = -1;
3772f0c5ee21Smmusante 			goto error;
3773f0c5ee21Smmusante 		}
3774cdf5b4caSmmusante 		delim = strchr(parentname, '@');
3775cdf5b4caSmmusante 		*delim = '\0';
3776990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3777cdf5b4caSmmusante 		if (zhrp == NULL) {
3778f0c5ee21Smmusante 			ret = -1;
3779f0c5ee21Smmusante 			goto error;
3780cdf5b4caSmmusante 		}
3781fa9e4066Sahrens 
3782cdf5b4caSmmusante 	} else {
37836a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
37846a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
3785cdf5b4caSmmusante 			return (-1);
3786cdf5b4caSmmusante 
3787cdf5b4caSmmusante 		if (changelist_haszonedchild(cl)) {
3788cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3789cdf5b4caSmmusante 			    "child dataset with inherited mountpoint is used "
3790cdf5b4caSmmusante 			    "in a non-global zone"));
3791cdf5b4caSmmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3792cdf5b4caSmmusante 			goto error;
3793cdf5b4caSmmusante 		}
3794cdf5b4caSmmusante 
3795cdf5b4caSmmusante 		if ((ret = changelist_prefix(cl)) != 0)
3796cdf5b4caSmmusante 			goto error;
3797cdf5b4caSmmusante 	}
3798fa9e4066Sahrens 
3799e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3800fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3801fa9e4066Sahrens 	else
3802fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3803fa9e4066Sahrens 
380498579b20Snd 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3805e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
380698579b20Snd 
3807cdf5b4caSmmusante 	zc.zc_cookie = recursive;
3808cdf5b4caSmmusante 
3809ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3810cdf5b4caSmmusante 		/*
3811cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
3812cdf5b4caSmmusante 		 * be in zc.zc_name
3813cdf5b4caSmmusante 		 */
3814cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38153cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
3816cdf5b4caSmmusante 
3817cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
3818cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3819cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
3820cdf5b4caSmmusante 			    "with the new name"));
3821a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3822cdf5b4caSmmusante 		} else {
3823cdf5b4caSmmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3824cdf5b4caSmmusante 		}
3825fa9e4066Sahrens 
3826fa9e4066Sahrens 		/*
3827fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
3828fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
3829fa9e4066Sahrens 		 */
3830681d9761SEric Taylor 		if (!recursive)
3831cdf5b4caSmmusante 			(void) changelist_postfix(cl);
3832fa9e4066Sahrens 	} else {
3833681d9761SEric Taylor 		if (!recursive) {
3834cdf5b4caSmmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
3835cdf5b4caSmmusante 			ret = changelist_postfix(cl);
3836cdf5b4caSmmusante 		}
3837fa9e4066Sahrens 	}
3838fa9e4066Sahrens 
3839fa9e4066Sahrens error:
3840cdf5b4caSmmusante 	if (parentname) {
3841cdf5b4caSmmusante 		free(parentname);
3842cdf5b4caSmmusante 	}
3843cdf5b4caSmmusante 	if (zhrp) {
3844cdf5b4caSmmusante 		zfs_close(zhrp);
3845cdf5b4caSmmusante 	}
3846cdf5b4caSmmusante 	if (cl) {
3847cdf5b4caSmmusante 		changelist_free(cl);
3848cdf5b4caSmmusante 	}
3849fa9e4066Sahrens 	return (ret);
3850fa9e4066Sahrens }
3851fa9e4066Sahrens 
3852e9dbad6fSeschrock nvlist_t *
3853e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
3854e9dbad6fSeschrock {
3855e9dbad6fSeschrock 	return (zhp->zfs_user_props);
3856e9dbad6fSeschrock }
3857e9dbad6fSeschrock 
385892241e0bSTom Erickson nvlist_t *
385992241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
386092241e0bSTom Erickson {
386192241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
386292241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
386392241e0bSTom Erickson 			return (NULL);
386492241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
386592241e0bSTom Erickson }
386692241e0bSTom Erickson 
3867b1b8ab34Slling /*
3868b1b8ab34Slling  * This function is used by 'zfs list' to determine the exact set of columns to
3869b1b8ab34Slling  * display, and their maximum widths.  This does two main things:
3870b1b8ab34Slling  *
3871b1b8ab34Slling  *      - If this is a list of all properties, then expand the list to include
3872b1b8ab34Slling  *        all native properties, and set a flag so that for each dataset we look
3873b1b8ab34Slling  *        for new unique user properties and add them to the list.
3874b1b8ab34Slling  *
3875b1b8ab34Slling  *      - For non fixed-width properties, keep track of the maximum width seen
387692241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
387792241e0bSTom Erickson  *        requested received property values, we also need to compute the width
387892241e0bSTom Erickson  *        of the RECEIVED column.
3879b1b8ab34Slling  */
3880b1b8ab34Slling int
388143d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
388243d68d68SYuri Pankov     boolean_t literal)
3883b1b8ab34Slling {
3884b1b8ab34Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3885990b4856Slling 	zprop_list_t *entry;
3886990b4856Slling 	zprop_list_t **last, **start;
3887b1b8ab34Slling 	nvlist_t *userprops, *propval;
3888b1b8ab34Slling 	nvpair_t *elem;
3889b1b8ab34Slling 	char *strval;
3890b1b8ab34Slling 	char buf[ZFS_MAXPROPLEN];
3891b1b8ab34Slling 
3892990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3893b1b8ab34Slling 		return (-1);
3894e9dbad6fSeschrock 
3895e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
3896e9dbad6fSeschrock 
3897e9dbad6fSeschrock 	entry = *plp;
3898e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3899e9dbad6fSeschrock 		/*
3900e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
3901e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
3902e9dbad6fSeschrock 		 * non-native property.
3903e9dbad6fSeschrock 		 */
3904e9dbad6fSeschrock 		start = plp;
3905e9dbad6fSeschrock 		while (*start != NULL) {
3906990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
3907e9dbad6fSeschrock 				break;
3908e9dbad6fSeschrock 			start = &(*start)->pl_next;
3909e9dbad6fSeschrock 		}
3910e9dbad6fSeschrock 
3911e9dbad6fSeschrock 		elem = NULL;
3912e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3913e9dbad6fSeschrock 			/*
3914e9dbad6fSeschrock 			 * See if we've already found this property in our list.
3915e9dbad6fSeschrock 			 */
3916e9dbad6fSeschrock 			for (last = start; *last != NULL;
3917e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
3918e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
3919e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
3920e9dbad6fSeschrock 					break;
3921e9dbad6fSeschrock 			}
3922e9dbad6fSeschrock 
3923e9dbad6fSeschrock 			if (*last == NULL) {
3924e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
3925990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
3926e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
3927e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
3928e9dbad6fSeschrock 					free(entry);
3929e9dbad6fSeschrock 					return (-1);
3930e9dbad6fSeschrock 				}
3931e9dbad6fSeschrock 
3932990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
3933e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
3934e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
3935e9dbad6fSeschrock 				*last = entry;
3936e9dbad6fSeschrock 			}
3937e9dbad6fSeschrock 		}
3938e9dbad6fSeschrock 	}
3939e9dbad6fSeschrock 
3940e9dbad6fSeschrock 	/*
3941e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
3942e9dbad6fSeschrock 	 */
3943e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
394443d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
3945e9dbad6fSeschrock 			continue;
3946e9dbad6fSeschrock 
3947990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
3948e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
394943d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
3950e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
3951e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
3952e9dbad6fSeschrock 			}
395392241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
395492241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
395543d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
395692241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
395792241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
395892241e0bSTom Erickson 		} else {
395992241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
396092241e0bSTom Erickson 			    &propval) == 0) {
396192241e0bSTom Erickson 				verify(nvlist_lookup_string(propval,
396292241e0bSTom Erickson 				    ZPROP_VALUE, &strval) == 0);
396392241e0bSTom Erickson 				if (strlen(strval) > entry->pl_width)
396492241e0bSTom Erickson 					entry->pl_width = strlen(strval);
396592241e0bSTom Erickson 			}
396692241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
396792241e0bSTom Erickson 			    entry->pl_user_prop,
396843d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
396992241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
397092241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
3971e9dbad6fSeschrock 		}
3972e9dbad6fSeschrock 	}
3973e9dbad6fSeschrock 
3974e9dbad6fSeschrock 	return (0);
3975e9dbad6fSeschrock }
3976ecd6cf80Smarks 
3977ecd6cf80Smarks int
3978ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3979743a77edSAlan Wright     char *resource, void *export, void *sharetab,
3980743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
3981ecd6cf80Smarks {
3982ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
3983ecd6cf80Smarks 	int error;
3984ecd6cf80Smarks 
3985ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3986ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3987743a77edSAlan Wright 	if (resource)
3988743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3989ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3990ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3991da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
3992ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
3993ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3994ecd6cf80Smarks 	return (error);
3995ecd6cf80Smarks }
39962e5e9e19SSanjeev Bagewadi 
39972e5e9e19SSanjeev Bagewadi void
39982e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
39992e5e9e19SSanjeev Bagewadi {
40002e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
40012e5e9e19SSanjeev Bagewadi 
40022e5e9e19SSanjeev Bagewadi 	/*
40032e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
40042e5e9e19SSanjeev Bagewadi 	 * properties.
40052e5e9e19SSanjeev Bagewadi 	 */
40062e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
40072e5e9e19SSanjeev Bagewadi 
40082e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
40092e5e9e19SSanjeev Bagewadi 
40102e5e9e19SSanjeev Bagewadi 	while (curr) {
40112e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
40122e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
40132e5e9e19SSanjeev Bagewadi 
401414843421SMatthew Ahrens 		/*
4015faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4016faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4017faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4018faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4019faaa6415SEric Schrock 		 * software, for example).
402014843421SMatthew Ahrens 		 */
402114843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
40222e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
40232e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
40242e5e9e19SSanjeev Bagewadi 		curr = next;
40252e5e9e19SSanjeev Bagewadi 	}
40262e5e9e19SSanjeev Bagewadi }
4027743a77edSAlan Wright 
4028743a77edSAlan Wright static int
4029743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4030743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4031743a77edSAlan Wright {
4032743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4033743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4034743a77edSAlan Wright 	int error;
4035743a77edSAlan Wright 
4036743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4037743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4038743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4039743a77edSAlan Wright 
4040743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4041743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4042743a77edSAlan Wright 			(void) no_memory(hdl);
4043743a77edSAlan Wright 			return (NULL);
4044743a77edSAlan Wright 		}
4045743a77edSAlan Wright 	}
4046743a77edSAlan Wright 
4047743a77edSAlan Wright 	switch (cmd) {
4048743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4049743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4050743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4051743a77edSAlan Wright 		break;
4052743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4053743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4054743a77edSAlan Wright 		    resource1) != 0) {
4055743a77edSAlan Wright 				(void) no_memory(hdl);
4056743a77edSAlan Wright 				return (-1);
4057743a77edSAlan Wright 		}
4058743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4059743a77edSAlan Wright 		    resource2) != 0) {
4060743a77edSAlan Wright 				(void) no_memory(hdl);
4061743a77edSAlan Wright 				return (-1);
4062743a77edSAlan Wright 		}
4063743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4064743a77edSAlan Wright 			nvlist_free(nvlist);
4065743a77edSAlan Wright 			return (-1);
4066743a77edSAlan Wright 		}
4067743a77edSAlan Wright 		break;
4068743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4069743a77edSAlan Wright 		break;
4070743a77edSAlan Wright 	default:
4071743a77edSAlan Wright 		return (-1);
4072743a77edSAlan Wright 	}
4073743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4074743a77edSAlan Wright 	if (nvlist)
4075743a77edSAlan Wright 		nvlist_free(nvlist);
4076743a77edSAlan Wright 	return (error);
4077743a77edSAlan Wright }
4078743a77edSAlan Wright 
4079743a77edSAlan Wright int
4080743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4081743a77edSAlan Wright     char *path, char *resource)
4082743a77edSAlan Wright {
4083743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4084743a77edSAlan Wright 	    resource, NULL));
4085743a77edSAlan Wright }
4086743a77edSAlan Wright 
4087743a77edSAlan Wright int
4088743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4089743a77edSAlan Wright     char *path, char *resource)
4090743a77edSAlan Wright {
4091743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4092743a77edSAlan Wright 	    resource, NULL));
4093743a77edSAlan Wright }
4094743a77edSAlan Wright 
4095743a77edSAlan Wright int
4096743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4097743a77edSAlan Wright {
4098743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4099743a77edSAlan Wright 	    NULL, NULL));
4100743a77edSAlan Wright }
4101743a77edSAlan Wright 
4102743a77edSAlan Wright int
4103743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4104743a77edSAlan Wright     char *oldname, char *newname)
4105743a77edSAlan Wright {
4106743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4107743a77edSAlan Wright 	    oldname, newname));
4108743a77edSAlan Wright }
410914843421SMatthew Ahrens 
411014843421SMatthew Ahrens int
411114843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
411214843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
411314843421SMatthew Ahrens {
411414843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
411514843421SMatthew Ahrens 	zfs_useracct_t buf[100];
411670f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
411770f56fa6SYuri Pankov 	int ret;
411814843421SMatthew Ahrens 
411919b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
412014843421SMatthew Ahrens 
412114843421SMatthew Ahrens 	zc.zc_objset_type = type;
412214843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
412314843421SMatthew Ahrens 
412470f56fa6SYuri Pankov 	for (;;) {
412514843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
412614843421SMatthew Ahrens 
412714843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
412870f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
41293b2aab18SMatthew Ahrens 			char errbuf[1024];
413070f56fa6SYuri Pankov 
413170f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
413270f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
413370f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
413470f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
413570f56fa6SYuri Pankov 		}
413670f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
413714843421SMatthew Ahrens 			break;
413814843421SMatthew Ahrens 
413914843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
414070f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
414170f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
414270f56fa6SYuri Pankov 				return (ret);
414314843421SMatthew Ahrens 			zua++;
414414843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
414514843421SMatthew Ahrens 		}
414614843421SMatthew Ahrens 	}
414714843421SMatthew Ahrens 
414870f56fa6SYuri Pankov 	return (0);
414914843421SMatthew Ahrens }
4150842727c2SChris Kirby 
41513b2aab18SMatthew Ahrens struct holdarg {
41523b2aab18SMatthew Ahrens 	nvlist_t *nvl;
41533b2aab18SMatthew Ahrens 	const char *snapname;
41543b2aab18SMatthew Ahrens 	const char *tag;
41553b2aab18SMatthew Ahrens 	boolean_t recursive;
4156bb6e7075SMatthew Ahrens 	int error;
41573b2aab18SMatthew Ahrens };
41583b2aab18SMatthew Ahrens 
41593b2aab18SMatthew Ahrens static int
41603b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
41613b2aab18SMatthew Ahrens {
41623b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
41633b2aab18SMatthew Ahrens 	char name[ZFS_MAXNAMELEN];
41643b2aab18SMatthew Ahrens 	int rv = 0;
41653b2aab18SMatthew Ahrens 
41663b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
41673b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
41683b2aab18SMatthew Ahrens 
4169a7a845e4SSteven Hartland 	if (lzc_exists(name))
41703b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
41713b2aab18SMatthew Ahrens 
41723b2aab18SMatthew Ahrens 	if (ha->recursive)
41733b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
41743b2aab18SMatthew Ahrens 	zfs_close(zhp);
41753b2aab18SMatthew Ahrens 	return (rv);
41763b2aab18SMatthew Ahrens }
41773b2aab18SMatthew Ahrens 
4178842727c2SChris Kirby int
4179842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4180a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4181842727c2SChris Kirby {
41823b2aab18SMatthew Ahrens 	int ret;
41833b2aab18SMatthew Ahrens 	struct holdarg ha;
4184842727c2SChris Kirby 
41853b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
41863b2aab18SMatthew Ahrens 	ha.snapname = snapname;
41873b2aab18SMatthew Ahrens 	ha.tag = tag;
41883b2aab18SMatthew Ahrens 	ha.recursive = recursive;
41893b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4190013023d4SMartin Matuska 
4191a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4192a7a845e4SSteven Hartland 		char errbuf[1024];
4193a7a845e4SSteven Hartland 
4194013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4195013023d4SMartin Matuska 		ret = ENOENT;
4196a7a845e4SSteven Hartland 		(void) snprintf(errbuf, sizeof (errbuf),
4197a7a845e4SSteven Hartland 		    dgettext(TEXT_DOMAIN,
4198a7a845e4SSteven Hartland 		    "cannot hold snapshot '%s@%s'"),
4199a7a845e4SSteven Hartland 		    zhp->zfs_name, snapname);
4200a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4201013023d4SMartin Matuska 		return (ret);
4202013023d4SMartin Matuska 	}
4203013023d4SMartin Matuska 
4204a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
42053b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4206a7f53a56SChris Kirby 
4207a7a845e4SSteven Hartland 	return (ret);
4208a7a845e4SSteven Hartland }
4209a7a845e4SSteven Hartland 
4210a7a845e4SSteven Hartland int
4211a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4212a7a845e4SSteven Hartland {
4213a7a845e4SSteven Hartland 	int ret;
4214a7a845e4SSteven Hartland 	nvlist_t *errors;
4215a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4216a7a845e4SSteven Hartland 	char errbuf[1024];
4217a7a845e4SSteven Hartland 	nvpair_t *elem;
4218a7a845e4SSteven Hartland 
4219a7a845e4SSteven Hartland 	errors = NULL;
4220a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4221a7a845e4SSteven Hartland 
4222a7a845e4SSteven Hartland 	if (ret == 0) {
4223a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4224a7a845e4SSteven Hartland 		fnvlist_free(errors);
42253b2aab18SMatthew Ahrens 		return (0);
4226a7a845e4SSteven Hartland 	}
4227842727c2SChris Kirby 
4228a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
42293b2aab18SMatthew Ahrens 		/* no hold-specific errors */
42303b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
42313b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
42323b2aab18SMatthew Ahrens 		switch (ret) {
42333b2aab18SMatthew Ahrens 		case ENOTSUP:
42343b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42353b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
42363b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
42373b2aab18SMatthew Ahrens 			break;
42383b2aab18SMatthew Ahrens 		case EINVAL:
42393b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
42403b2aab18SMatthew Ahrens 			break;
42413b2aab18SMatthew Ahrens 		default:
42423b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
42433b2aab18SMatthew Ahrens 		}
42443b2aab18SMatthew Ahrens 	}
4245842727c2SChris Kirby 
42463b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
42473b2aab18SMatthew Ahrens 	    elem != NULL;
42483b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
42493b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
42503b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
42513b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
42523b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
425315508ac0SChris Kirby 		case E2BIG:
425415508ac0SChris Kirby 			/*
425515508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
425615508ac0SChris Kirby 			 * prepended. So even if we passed the length check
425715508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
425815508ac0SChris Kirby 			 * up being slightly too long.
425915508ac0SChris Kirby 			 */
42603b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
42613b2aab18SMatthew Ahrens 			break;
4262842727c2SChris Kirby 		case EINVAL:
42633b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
42643b2aab18SMatthew Ahrens 			break;
4265842727c2SChris Kirby 		case EEXIST:
42663b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
42673b2aab18SMatthew Ahrens 			break;
4268842727c2SChris Kirby 		default:
42693b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
42703b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4271842727c2SChris Kirby 		}
4272842727c2SChris Kirby 	}
4273842727c2SChris Kirby 
42743b2aab18SMatthew Ahrens 	fnvlist_free(errors);
42753b2aab18SMatthew Ahrens 	return (ret);
42763b2aab18SMatthew Ahrens }
42773b2aab18SMatthew Ahrens 
42783b2aab18SMatthew Ahrens static int
42793b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
42803b2aab18SMatthew Ahrens {
42813b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
42823b2aab18SMatthew Ahrens 	char name[ZFS_MAXNAMELEN];
42833b2aab18SMatthew Ahrens 	int rv = 0;
4284bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
42853b2aab18SMatthew Ahrens 
42863b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
42873b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
42883b2aab18SMatthew Ahrens 
4289bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4290bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4291bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4292bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4293bb6e7075SMatthew Ahrens 	} else {
4294bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4295bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4296bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4297bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
42983b2aab18SMatthew Ahrens 	}
42993b2aab18SMatthew Ahrens 
43003b2aab18SMatthew Ahrens 	if (ha->recursive)
43013b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
43023b2aab18SMatthew Ahrens 	zfs_close(zhp);
43033b2aab18SMatthew Ahrens 	return (rv);
4304842727c2SChris Kirby }
4305842727c2SChris Kirby 
4306842727c2SChris Kirby int
4307842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4308842727c2SChris Kirby     boolean_t recursive)
4309842727c2SChris Kirby {
43103b2aab18SMatthew Ahrens 	int ret;
43113b2aab18SMatthew Ahrens 	struct holdarg ha;
4312a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
43133b2aab18SMatthew Ahrens 	nvpair_t *elem;
4314842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4315013023d4SMartin Matuska 	char errbuf[1024];
4316842727c2SChris Kirby 
43173b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
43183b2aab18SMatthew Ahrens 	ha.snapname = snapname;
43193b2aab18SMatthew Ahrens 	ha.tag = tag;
43203b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4321bb6e7075SMatthew Ahrens 	ha.error = 0;
43223b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4323013023d4SMartin Matuska 
4324a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4325013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4326bb6e7075SMatthew Ahrens 		ret = ha.error;
4327013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4328013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4329013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
4330013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4331bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
4332bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4333bb6e7075SMatthew Ahrens 		} else {
4334bb6e7075SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
4335bb6e7075SMatthew Ahrens 		}
4336013023d4SMartin Matuska 		return (ret);
4337013023d4SMartin Matuska 	}
4338013023d4SMartin Matuska 
43393b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
43403b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4341842727c2SChris Kirby 
4342a7a845e4SSteven Hartland 	if (ret == 0) {
4343a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4344a7a845e4SSteven Hartland 		fnvlist_free(errors);
43453b2aab18SMatthew Ahrens 		return (0);
4346a7a845e4SSteven Hartland 	}
43473b2aab18SMatthew Ahrens 
4348a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
43493b2aab18SMatthew Ahrens 		/* no hold-specific errors */
4350842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
43513b2aab18SMatthew Ahrens 		    "cannot release"));
4352842727c2SChris Kirby 		switch (errno) {
4353842727c2SChris Kirby 		case ENOTSUP:
4354842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4355842727c2SChris Kirby 			    "pool must be upgraded"));
43563b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
43573b2aab18SMatthew Ahrens 			break;
43583b2aab18SMatthew Ahrens 		default:
43593b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
43603b2aab18SMatthew Ahrens 		}
43613b2aab18SMatthew Ahrens 	}
43623b2aab18SMatthew Ahrens 
43633b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
43643b2aab18SMatthew Ahrens 	    elem != NULL;
43653b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
43663b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
43673b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
43683b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
43693b2aab18SMatthew Ahrens 		    nvpair_name(elem));
43703b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
43713b2aab18SMatthew Ahrens 		case ESRCH:
43723b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
43733b2aab18SMatthew Ahrens 			break;
4374842727c2SChris Kirby 		case EINVAL:
43753b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
43763b2aab18SMatthew Ahrens 			break;
4377842727c2SChris Kirby 		default:
43783b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
43793b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4380842727c2SChris Kirby 		}
4381842727c2SChris Kirby 	}
4382842727c2SChris Kirby 
43833b2aab18SMatthew Ahrens 	fnvlist_free(errors);
43843b2aab18SMatthew Ahrens 	return (ret);
4385842727c2SChris Kirby }
4386ca45db41SChris Kirby 
43871af68beaSAlexander Stetsenko int
43881af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
43891af68beaSAlexander Stetsenko {
43901af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
43911af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
43921af68beaSAlexander Stetsenko 	int nvsz = 2048;
43931af68beaSAlexander Stetsenko 	void *nvbuf;
43941af68beaSAlexander Stetsenko 	int err = 0;
43953b2aab18SMatthew Ahrens 	char errbuf[1024];
43961af68beaSAlexander Stetsenko 
43971af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
43981af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
43991af68beaSAlexander Stetsenko 
44001af68beaSAlexander Stetsenko tryagain:
44011af68beaSAlexander Stetsenko 
44021af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
44031af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
44041af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
44051af68beaSAlexander Stetsenko 		goto out;
44061af68beaSAlexander Stetsenko 	}
44071af68beaSAlexander Stetsenko 
44081af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
44091af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
44101af68beaSAlexander Stetsenko 
44111af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
44121af68beaSAlexander Stetsenko 
4413d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
44141af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
44151af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
44161af68beaSAlexander Stetsenko 		    zc.zc_name);
44171af68beaSAlexander Stetsenko 		switch (errno) {
44181af68beaSAlexander Stetsenko 		case ENOMEM:
44191af68beaSAlexander Stetsenko 			free(nvbuf);
44201af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
44211af68beaSAlexander Stetsenko 			goto tryagain;
44221af68beaSAlexander Stetsenko 
44231af68beaSAlexander Stetsenko 		case ENOTSUP:
44241af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44251af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
44261af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
44271af68beaSAlexander Stetsenko 			break;
44281af68beaSAlexander Stetsenko 		case EINVAL:
44291af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
44301af68beaSAlexander Stetsenko 			break;
44311af68beaSAlexander Stetsenko 		case ENOENT:
44321af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
44331af68beaSAlexander Stetsenko 			break;
44341af68beaSAlexander Stetsenko 		default:
44351af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
44361af68beaSAlexander Stetsenko 			break;
44371af68beaSAlexander Stetsenko 		}
44381af68beaSAlexander Stetsenko 	} else {
44391af68beaSAlexander Stetsenko 		/* success */
44401af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
44411af68beaSAlexander Stetsenko 		if (rc) {
44421af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
44431af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
44441af68beaSAlexander Stetsenko 			    zc.zc_name);
44451af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
44461af68beaSAlexander Stetsenko 		}
44471af68beaSAlexander Stetsenko 	}
44481af68beaSAlexander Stetsenko 
44491af68beaSAlexander Stetsenko 	free(nvbuf);
44501af68beaSAlexander Stetsenko out:
44511af68beaSAlexander Stetsenko 	return (err);
44521af68beaSAlexander Stetsenko }
44531af68beaSAlexander Stetsenko 
44541af68beaSAlexander Stetsenko int
44551af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
44561af68beaSAlexander Stetsenko {
44571af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
44581af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
44591af68beaSAlexander Stetsenko 	char *nvbuf;
44603b2aab18SMatthew Ahrens 	char errbuf[1024];
44611af68beaSAlexander Stetsenko 	size_t nvsz;
44621af68beaSAlexander Stetsenko 	int err;
44631af68beaSAlexander Stetsenko 
44641af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
44651af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
44661af68beaSAlexander Stetsenko 
44671af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
44681af68beaSAlexander Stetsenko 	assert(err == 0);
44691af68beaSAlexander Stetsenko 
44701af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
44711af68beaSAlexander Stetsenko 
44721af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
44731af68beaSAlexander Stetsenko 	assert(err == 0);
44741af68beaSAlexander Stetsenko 
44751af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
44761af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
44771af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
44781af68beaSAlexander Stetsenko 
44791af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
44801af68beaSAlexander Stetsenko 
44811af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
44821af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
44831af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
44841af68beaSAlexander Stetsenko 		    zc.zc_name);
44851af68beaSAlexander Stetsenko 		switch (errno) {
44861af68beaSAlexander Stetsenko 		case ENOTSUP:
44871af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44881af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
44891af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
44901af68beaSAlexander Stetsenko 			break;
44911af68beaSAlexander Stetsenko 		case EINVAL:
44921af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
44931af68beaSAlexander Stetsenko 			break;
44941af68beaSAlexander Stetsenko 		case ENOENT:
44951af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
44961af68beaSAlexander Stetsenko 			break;
44971af68beaSAlexander Stetsenko 		default:
44981af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
44991af68beaSAlexander Stetsenko 			break;
45001af68beaSAlexander Stetsenko 		}
45011af68beaSAlexander Stetsenko 	}
45021af68beaSAlexander Stetsenko 
45031af68beaSAlexander Stetsenko 	free(nvbuf);
45041af68beaSAlexander Stetsenko 
45051af68beaSAlexander Stetsenko 	return (err);
45061af68beaSAlexander Stetsenko }
45071af68beaSAlexander Stetsenko 
45081af68beaSAlexander Stetsenko int
45091af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
45101af68beaSAlexander Stetsenko {
45113b2aab18SMatthew Ahrens 	int err;
45123b2aab18SMatthew Ahrens 	char errbuf[1024];
45131af68beaSAlexander Stetsenko 
45143b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
45151af68beaSAlexander Stetsenko 
45163b2aab18SMatthew Ahrens 	if (err != 0) {
45173b2aab18SMatthew Ahrens 		libzfs_handle_t *hdl = zhp->zfs_hdl;
45181af68beaSAlexander Stetsenko 
45191af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
45201af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
45213b2aab18SMatthew Ahrens 		    zhp->zfs_name);
45223b2aab18SMatthew Ahrens 		switch (err) {
45231af68beaSAlexander Stetsenko 		case ENOTSUP:
45241af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45251af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
45261af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
45271af68beaSAlexander Stetsenko 			break;
45281af68beaSAlexander Stetsenko 		case EINVAL:
45291af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
45301af68beaSAlexander Stetsenko 			break;
45311af68beaSAlexander Stetsenko 		case ENOENT:
45321af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
45331af68beaSAlexander Stetsenko 			break;
45341af68beaSAlexander Stetsenko 		default:
45351af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
45361af68beaSAlexander Stetsenko 			break;
45371af68beaSAlexander Stetsenko 		}
45381af68beaSAlexander Stetsenko 	}
45391af68beaSAlexander Stetsenko 
45401af68beaSAlexander Stetsenko 	return (err);
45411af68beaSAlexander Stetsenko }
45421af68beaSAlexander Stetsenko 
45433e30c24aSWill Andrews /*
45443e30c24aSWill Andrews  * Convert the zvol's volume size to an appropriate reservation.
45453e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
45463e30c24aSWill Andrews  * suite's shell version in reservation.kshlib.
45473e30c24aSWill Andrews  */
4548c1449561SEric Taylor uint64_t
4549c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4550c1449561SEric Taylor {
4551c1449561SEric Taylor 	uint64_t numdb;
4552c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4553c1449561SEric Taylor 	int ncopies;
4554c1449561SEric Taylor 	char *strval;
4555c1449561SEric Taylor 
4556c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4557c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4558c1449561SEric Taylor 		ncopies = atoi(strval);
4559c1449561SEric Taylor 	else
4560c1449561SEric Taylor 		ncopies = 1;
4561c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4562c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4563c1449561SEric Taylor 	    &volblocksize) != 0)
4564c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4565c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4566c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4567c1449561SEric Taylor 	numdb = 7;
4568c1449561SEric Taylor 	/* calculate number of indirects */
4569c1449561SEric Taylor 	while (nblocks > 1) {
4570c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4571c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4572c1449561SEric Taylor 		numdb += nblocks;
4573c1449561SEric Taylor 	}
4574c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4575c1449561SEric Taylor 	volsize *= ncopies;
4576c1449561SEric Taylor 	/*
4577c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4578c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4579c1449561SEric Taylor 	 * 1100 bytes
4580c1449561SEric Taylor 	 */
4581c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4582c1449561SEric Taylor 	volsize += numdb;
4583c1449561SEric Taylor 	return (volsize);
4584c1449561SEric Taylor }
4585