xref: /titanic_51/usr/src/lib/libzfs/common/libzfs_dataset.c (revision 1a4cea1bca45d5775aeb88646a63db027947246f)
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.
24a2afb611SJerry Jelinek  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25e9316f76SJoe Stein  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
27013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
28a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
29c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
308808ac5dSYuri Pankov  * Copyright 2016 Nexenta Systems, Inc.
31fa9e4066Sahrens  */
32fa9e4066Sahrens 
33fa9e4066Sahrens #include <ctype.h>
34fa9e4066Sahrens #include <errno.h>
35fa9e4066Sahrens #include <libintl.h>
36fa9e4066Sahrens #include <math.h>
37fa9e4066Sahrens #include <stdio.h>
38fa9e4066Sahrens #include <stdlib.h>
39fa9e4066Sahrens #include <strings.h>
40fa9e4066Sahrens #include <unistd.h>
413cb34c60Sahrens #include <stddef.h>
42fa9e4066Sahrens #include <zone.h>
4399653d4eSeschrock #include <fcntl.h>
44fa9e4066Sahrens #include <sys/mntent.h>
45b12a1c38Slling #include <sys/mount.h>
46ecd6cf80Smarks #include <priv.h>
47ecd6cf80Smarks #include <pwd.h>
48ecd6cf80Smarks #include <grp.h>
49ecd6cf80Smarks #include <stddef.h>
50ecd6cf80Smarks #include <ucred.h>
5114843421SMatthew Ahrens #include <idmap.h>
5214843421SMatthew Ahrens #include <aclutils.h>
533b12c289SMatthew Ahrens #include <directory.h>
54fa9e4066Sahrens 
55c1449561SEric Taylor #include <sys/dnode.h>
56fa9e4066Sahrens #include <sys/spa.h>
57e9dbad6fSeschrock #include <sys/zap.h>
58fa9e4066Sahrens #include <libzfs.h>
59fa9e4066Sahrens 
60fa9e4066Sahrens #include "zfs_namecheck.h"
61fa9e4066Sahrens #include "zfs_prop.h"
62fa9e4066Sahrens #include "libzfs_impl.h"
63ecd6cf80Smarks #include "zfs_deleg.h"
64fa9e4066Sahrens 
6514843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6614843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
67cdf5b4caSmmusante 
68fa9e4066Sahrens /*
69fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
70fa9e4066Sahrens  * readable form.
71fa9e4066Sahrens  */
72fa9e4066Sahrens const char *
73fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
74fa9e4066Sahrens {
75fa9e4066Sahrens 	switch (type) {
76fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
77fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
78fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
79fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
80fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
81fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
82fa9e4066Sahrens 	}
83fa9e4066Sahrens 
84fa9e4066Sahrens 	return (NULL);
85fa9e4066Sahrens }
86fa9e4066Sahrens 
87fa9e4066Sahrens /*
88fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
89fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
90fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
91fa9e4066Sahrens  * acceptable types.
92fa9e4066Sahrens  */
93fa9e4066Sahrens static const char *
94fa9e4066Sahrens path_to_str(const char *path, int types)
95fa9e4066Sahrens {
96fa9e4066Sahrens 	/*
97fa9e4066Sahrens 	 * When given a single type, always report the exact type.
98fa9e4066Sahrens 	 */
99fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
100fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
101fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
102fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
103fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
104fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
105fa9e4066Sahrens 
106fa9e4066Sahrens 	/*
107fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
108fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
109fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
110fa9e4066Sahrens 	 * snapshot attribute and try again.
111fa9e4066Sahrens 	 */
112fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
113fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
114fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
115fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
116fa9e4066Sahrens 	}
117fa9e4066Sahrens 
118fa9e4066Sahrens 	/*
119fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
120fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
121fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
122fa9e4066Sahrens 	 */
123fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
124fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
125fa9e4066Sahrens 
126fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
127fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
128fa9e4066Sahrens }
129fa9e4066Sahrens 
130fa9e4066Sahrens /*
131fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
13214843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
13314843421SMatthew Ahrens  * explain exactly why the name was not valid.
134fa9e4066Sahrens  */
13599d5e173STim Haley int
136f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
137f18faf3fSek110237     boolean_t modifying)
138fa9e4066Sahrens {
139fa9e4066Sahrens 	namecheck_err_t why;
140fa9e4066Sahrens 	char what;
141fa9e4066Sahrens 
1421af68beaSAlexander Stetsenko 	(void) zfs_prop_get_table();
14323962479SMarcel Telka 	if (entity_namecheck(path, &why, &what) != 0) {
14499653d4eSeschrock 		if (hdl != NULL) {
145fa9e4066Sahrens 			switch (why) {
146b81d61a6Slling 			case NAME_ERR_TOOLONG:
14799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14899653d4eSeschrock 				    "name is too long"));
149b81d61a6Slling 				break;
150b81d61a6Slling 
151fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
15299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15399653d4eSeschrock 				    "leading slash in name"));
154fa9e4066Sahrens 				break;
155fa9e4066Sahrens 
156fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15899653d4eSeschrock 				    "empty component in name"));
159fa9e4066Sahrens 				break;
160fa9e4066Sahrens 
161fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
16299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16399653d4eSeschrock 				    "trailing slash in name"));
164fa9e4066Sahrens 				break;
165fa9e4066Sahrens 
166fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16799653d4eSeschrock 				zfs_error_aux(hdl,
168fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16999653d4eSeschrock 				    "'%c' in name"), what);
170fa9e4066Sahrens 				break;
171fa9e4066Sahrens 
172fbc66171SMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
17399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174fbc66171SMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
175fbc66171SMarcel Telka 				    "name"));
176fa9e4066Sahrens 				break;
1775ad82045Snd150628 
1785ad82045Snd150628 			case NAME_ERR_NOLETTER:
1795ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1805ad82045Snd150628 				    "pool doesn't begin with a letter"));
1815ad82045Snd150628 				break;
1825ad82045Snd150628 
1835ad82045Snd150628 			case NAME_ERR_RESERVED:
1845ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1855ad82045Snd150628 				    "name is reserved"));
1865ad82045Snd150628 				break;
1875ad82045Snd150628 
1885ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1895ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1905ad82045Snd150628 				    "reserved disk name"));
1915ad82045Snd150628 				break;
192fa9e4066Sahrens 			}
193fa9e4066Sahrens 		}
194fa9e4066Sahrens 
195fa9e4066Sahrens 		return (0);
196fa9e4066Sahrens 	}
197fa9e4066Sahrens 
198fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19999653d4eSeschrock 		if (hdl != NULL)
20099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
201fbc66171SMarcel Telka 			    "snapshot delimiter '@' is not expected here"));
202fa9e4066Sahrens 		return (0);
203fa9e4066Sahrens 	}
204fa9e4066Sahrens 
2051d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2061d452cf5Sahrens 		if (hdl != NULL)
2071d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
208d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2091d452cf5Sahrens 		return (0);
2101d452cf5Sahrens 	}
2111d452cf5Sahrens 
212fbc66171SMarcel Telka 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
213fbc66171SMarcel Telka 		if (hdl != NULL)
214fbc66171SMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
215fbc66171SMarcel Telka 			    "bookmark delimiter '#' is not expected here"));
216fbc66171SMarcel Telka 		return (0);
217fbc66171SMarcel Telka 	}
218fbc66171SMarcel Telka 
219fbc66171SMarcel Telka 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
220fbc66171SMarcel Telka 		if (hdl != NULL)
221fbc66171SMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
222fbc66171SMarcel Telka 			    "missing '#' delimiter in bookmark name"));
223fbc66171SMarcel Telka 		return (0);
224fbc66171SMarcel Telka 	}
225fbc66171SMarcel Telka 
226f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
227f18faf3fSek110237 		if (hdl != NULL)
228f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
229f18faf3fSek110237 			    "invalid character %c in name"), '%');
230f18faf3fSek110237 		return (0);
231f18faf3fSek110237 	}
232f18faf3fSek110237 
23399653d4eSeschrock 	return (-1);
234fa9e4066Sahrens }
235fa9e4066Sahrens 
236fa9e4066Sahrens int
237fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
238fa9e4066Sahrens {
239e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
240e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
241f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
242fa9e4066Sahrens }
243fa9e4066Sahrens 
244fa9e4066Sahrens /*
245e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
246e9dbad6fSeschrock  * properties into a separate nvlist.
247e9dbad6fSeschrock  */
248fac3008cSeschrock static nvlist_t *
249fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
250e9dbad6fSeschrock {
251e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
252e9dbad6fSeschrock 	nvpair_t *elem;
253e9dbad6fSeschrock 	nvlist_t *propval;
254fac3008cSeschrock 	nvlist_t *nvl;
255e9dbad6fSeschrock 
256fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
257fac3008cSeschrock 		(void) no_memory(hdl);
258fac3008cSeschrock 		return (NULL);
259fac3008cSeschrock 	}
260e9dbad6fSeschrock 
261e9dbad6fSeschrock 	elem = NULL;
262fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
263e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
264e9dbad6fSeschrock 			continue;
265e9dbad6fSeschrock 
266e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
267fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
268fac3008cSeschrock 			nvlist_free(nvl);
269fac3008cSeschrock 			(void) no_memory(hdl);
270fac3008cSeschrock 			return (NULL);
271fac3008cSeschrock 		}
272e9dbad6fSeschrock 	}
273e9dbad6fSeschrock 
274fac3008cSeschrock 	return (nvl);
275e9dbad6fSeschrock }
276e9dbad6fSeschrock 
27729ab75c9Srm160521 static zpool_handle_t *
27829ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
27929ab75c9Srm160521 {
28029ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
28129ab75c9Srm160521 	zpool_handle_t *zph;
28229ab75c9Srm160521 
28329ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
28429ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
28529ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
28629ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
28729ab75c9Srm160521 	}
28829ab75c9Srm160521 	return (zph);
28929ab75c9Srm160521 }
29029ab75c9Srm160521 
29129ab75c9Srm160521 static zpool_handle_t *
29229ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
29329ab75c9Srm160521 {
29429ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
29529ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
29629ab75c9Srm160521 
29729ab75c9Srm160521 	while ((zph != NULL) &&
29829ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
29929ab75c9Srm160521 		zph = zph->zpool_next;
30029ab75c9Srm160521 	return (zph);
30129ab75c9Srm160521 }
30229ab75c9Srm160521 
30329ab75c9Srm160521 /*
30429ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
30529ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
30629ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
30729ab75c9Srm160521  */
30829ab75c9Srm160521 static zpool_handle_t *
30929ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
31029ab75c9Srm160521 {
31129ab75c9Srm160521 	char *pool_name;
31229ab75c9Srm160521 	int len;
31329ab75c9Srm160521 	zpool_handle_t *zph;
31429ab75c9Srm160521 
31578f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
31629ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
31729ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
31829ab75c9Srm160521 
31929ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
32029ab75c9Srm160521 	if (zph == NULL)
32129ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
32229ab75c9Srm160521 
32329ab75c9Srm160521 	free(pool_name);
32429ab75c9Srm160521 	return (zph);
32529ab75c9Srm160521 }
32629ab75c9Srm160521 
32729ab75c9Srm160521 void
32829ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
32929ab75c9Srm160521 {
33029ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
33129ab75c9Srm160521 
33229ab75c9Srm160521 	while (zph != NULL) {
33329ab75c9Srm160521 		next = zph->zpool_next;
33429ab75c9Srm160521 		zpool_close(zph);
33529ab75c9Srm160521 		zph = next;
33629ab75c9Srm160521 	}
33729ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
33829ab75c9Srm160521 }
33929ab75c9Srm160521 
340e9dbad6fSeschrock /*
341fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
342fa9e4066Sahrens  */
343fa9e4066Sahrens static int
344ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
345fa9e4066Sahrens {
346e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
347fa9e4066Sahrens 
348ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
349fa9e4066Sahrens 
350ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3517f7322feSeschrock 		if (errno == ENOMEM) {
352ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
35399653d4eSeschrock 				return (-1);
354e9dbad6fSeschrock 			}
3557f7322feSeschrock 		} else {
356fa9e4066Sahrens 			return (-1);
3577f7322feSeschrock 		}
3587f7322feSeschrock 	}
359ebedde84SEric Taylor 	return (0);
360fac3008cSeschrock }
361fac3008cSeschrock 
36292241e0bSTom Erickson /*
36392241e0bSTom Erickson  * Utility function to get the received properties of the given object.
36492241e0bSTom Erickson  */
36592241e0bSTom Erickson static int
36692241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
36792241e0bSTom Erickson {
36892241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
36992241e0bSTom Erickson 	nvlist_t *recvdprops;
37092241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
37192241e0bSTom Erickson 	int err;
37292241e0bSTom Erickson 
37392241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
37492241e0bSTom Erickson 		return (-1);
37592241e0bSTom Erickson 
37692241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
37792241e0bSTom Erickson 
37892241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
37992241e0bSTom Erickson 		if (errno == ENOMEM) {
38092241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
38192241e0bSTom Erickson 				return (-1);
38292241e0bSTom Erickson 			}
38392241e0bSTom Erickson 		} else {
38492241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
38592241e0bSTom Erickson 			return (-1);
38692241e0bSTom Erickson 		}
38792241e0bSTom Erickson 	}
38892241e0bSTom Erickson 
38992241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
39092241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
39192241e0bSTom Erickson 	if (err != 0)
39292241e0bSTom Erickson 		return (-1);
39392241e0bSTom Erickson 
39492241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
39592241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
39692241e0bSTom Erickson 
39792241e0bSTom Erickson 	return (0);
39892241e0bSTom Erickson }
39992241e0bSTom Erickson 
400ebedde84SEric Taylor static int
401ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
402ebedde84SEric Taylor {
403ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
404ebedde84SEric Taylor 
405ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
406ebedde84SEric Taylor 
407ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
408ebedde84SEric Taylor 		return (-1);
409ebedde84SEric Taylor 	}
410fac3008cSeschrock 
41114843421SMatthew Ahrens 	/*
41214843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
41314843421SMatthew Ahrens 	 * storing them in zfs_props?
41414843421SMatthew Ahrens 	 */
415fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
416fac3008cSeschrock 		nvlist_free(allprops);
417fac3008cSeschrock 		return (-1);
418fac3008cSeschrock 	}
419fac3008cSeschrock 
42099653d4eSeschrock 	nvlist_free(zhp->zfs_props);
421fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
42299653d4eSeschrock 
423fac3008cSeschrock 	zhp->zfs_props = allprops;
424fac3008cSeschrock 	zhp->zfs_user_props = userprops;
42599653d4eSeschrock 
426fa9e4066Sahrens 	return (0);
427fa9e4066Sahrens }
428fa9e4066Sahrens 
429ebedde84SEric Taylor static int
430ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
431ebedde84SEric Taylor {
432ebedde84SEric Taylor 	int rc = 0;
433ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
434ebedde84SEric Taylor 
435ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
436ebedde84SEric Taylor 		return (-1);
437ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
438ebedde84SEric Taylor 		rc = -1;
439ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
440ebedde84SEric Taylor 		rc = -1;
441ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
442ebedde84SEric Taylor 	return (rc);
443ebedde84SEric Taylor }
444ebedde84SEric Taylor 
445fa9e4066Sahrens /*
446fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
447fa9e4066Sahrens  */
448fa9e4066Sahrens void
449fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
450fa9e4066Sahrens {
451fa9e4066Sahrens 	(void) get_stats(zhp);
452fa9e4066Sahrens }
453fa9e4066Sahrens 
454fa9e4066Sahrens /*
455fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
456fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
457fa9e4066Sahrens  */
458ebedde84SEric Taylor static int
459ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
460fa9e4066Sahrens {
461503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
462ebedde84SEric Taylor 		return (-1);
46331fd60d3Sahrens 
464fa9e4066Sahrens 	/*
465fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
466fa9e4066Sahrens 	 * the high-level type.
467fa9e4066Sahrens 	 */
468a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
469a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
470a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
471a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
472a2eea2e1Sahrens 	else
473a2eea2e1Sahrens 		abort();
474a2eea2e1Sahrens 
475fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
476fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
477fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
478fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
479fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
480fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
481fa9e4066Sahrens 	else
48299653d4eSeschrock 		abort();	/* we should never see any other types */
483fa9e4066Sahrens 
4849fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4859fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4869fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
487ebedde84SEric Taylor 	return (0);
488ebedde84SEric Taylor }
489ebedde84SEric Taylor 
490ebedde84SEric Taylor zfs_handle_t *
491ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
492ebedde84SEric Taylor {
493ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
494ebedde84SEric Taylor 
495ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
496ebedde84SEric Taylor 
497ebedde84SEric Taylor 	if (zhp == NULL)
498ebedde84SEric Taylor 		return (NULL);
499ebedde84SEric Taylor 
500ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
501ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
502ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
503ebedde84SEric Taylor 		free(zhp);
504ebedde84SEric Taylor 		return (NULL);
505ebedde84SEric Taylor 	}
506ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
507ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
508ebedde84SEric Taylor 		free(zhp);
509ebedde84SEric Taylor 		return (NULL);
510ebedde84SEric Taylor 	}
511ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
512ebedde84SEric Taylor 		free(zhp);
513ebedde84SEric Taylor 		zhp = NULL;
514ebedde84SEric Taylor 	}
515ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
516ebedde84SEric Taylor 	return (zhp);
517ebedde84SEric Taylor }
518ebedde84SEric Taylor 
51919b94df9SMatthew Ahrens zfs_handle_t *
520ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
521ebedde84SEric Taylor {
522ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
523ebedde84SEric Taylor 
524ebedde84SEric Taylor 	if (zhp == NULL)
525ebedde84SEric Taylor 		return (NULL);
526ebedde84SEric Taylor 
527ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
528ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
529ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
530ebedde84SEric Taylor 		free(zhp);
531ebedde84SEric Taylor 		return (NULL);
532ebedde84SEric Taylor 	}
533fa9e4066Sahrens 	return (zhp);
534fa9e4066Sahrens }
535fa9e4066Sahrens 
53619b94df9SMatthew Ahrens zfs_handle_t *
53719b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
53819b94df9SMatthew Ahrens {
53919b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
54019b94df9SMatthew Ahrens 
54119b94df9SMatthew Ahrens 	if (zhp == NULL)
54219b94df9SMatthew Ahrens 		return (NULL);
54319b94df9SMatthew Ahrens 
54419b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
54519b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
54619b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
54719b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
54819b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
54919b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
55019b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
55119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
55219b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
55319b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55419b94df9SMatthew Ahrens 			zfs_close(zhp);
55519b94df9SMatthew Ahrens 			return (NULL);
55619b94df9SMatthew Ahrens 		}
55719b94df9SMatthew Ahrens 	}
55819b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
55919b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
56019b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
56119b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
56219b94df9SMatthew Ahrens 			zfs_close(zhp);
56319b94df9SMatthew Ahrens 			return (NULL);
56419b94df9SMatthew Ahrens 		}
56519b94df9SMatthew Ahrens 	}
56619b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
56719b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
56819b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
56919b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
57019b94df9SMatthew Ahrens 			zfs_close(zhp);
57119b94df9SMatthew Ahrens 			return (NULL);
57219b94df9SMatthew Ahrens 		}
57319b94df9SMatthew Ahrens 	}
57419b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
57519b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
57619b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
57719b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
57819b94df9SMatthew Ahrens 	}
57919b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
58019b94df9SMatthew Ahrens 	return (zhp);
58119b94df9SMatthew Ahrens }
58219b94df9SMatthew Ahrens 
58378f17100SMatthew Ahrens boolean_t
58478f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
58578f17100SMatthew Ahrens {
58678f17100SMatthew Ahrens 	nvlist_t *bmarks;
58778f17100SMatthew Ahrens 	nvlist_t *props;
58840a5c998SMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
58978f17100SMatthew Ahrens 	char *bmark_name;
59078f17100SMatthew Ahrens 	char *pound;
59178f17100SMatthew Ahrens 	int err;
59278f17100SMatthew Ahrens 	boolean_t rv;
59378f17100SMatthew Ahrens 
59478f17100SMatthew Ahrens 
59578f17100SMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
59678f17100SMatthew Ahrens 	pound = strchr(fsname, '#');
59778f17100SMatthew Ahrens 	if (pound == NULL)
59878f17100SMatthew Ahrens 		return (B_FALSE);
59978f17100SMatthew Ahrens 
60078f17100SMatthew Ahrens 	*pound = '\0';
60178f17100SMatthew Ahrens 	bmark_name = pound + 1;
60278f17100SMatthew Ahrens 	props = fnvlist_alloc();
60378f17100SMatthew Ahrens 	err = lzc_get_bookmarks(fsname, props, &bmarks);
60478f17100SMatthew Ahrens 	nvlist_free(props);
60578f17100SMatthew Ahrens 	if (err != 0) {
60678f17100SMatthew Ahrens 		nvlist_free(bmarks);
60778f17100SMatthew Ahrens 		return (B_FALSE);
60878f17100SMatthew Ahrens 	}
60978f17100SMatthew Ahrens 
61078f17100SMatthew Ahrens 	rv = nvlist_exists(bmarks, bmark_name);
61178f17100SMatthew Ahrens 	nvlist_free(bmarks);
61278f17100SMatthew Ahrens 	return (rv);
61378f17100SMatthew Ahrens }
61478f17100SMatthew Ahrens 
61578f17100SMatthew Ahrens zfs_handle_t *
61678f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
61778f17100SMatthew Ahrens     nvlist_t *bmark_props)
61878f17100SMatthew Ahrens {
61978f17100SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
62078f17100SMatthew Ahrens 
62178f17100SMatthew Ahrens 	if (zhp == NULL)
62278f17100SMatthew Ahrens 		return (NULL);
62378f17100SMatthew Ahrens 
62478f17100SMatthew Ahrens 	/* Fill in the name. */
62578f17100SMatthew Ahrens 	zhp->zfs_hdl = parent->zfs_hdl;
62678f17100SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
62778f17100SMatthew Ahrens 
62878f17100SMatthew Ahrens 	/* Set the property lists. */
62978f17100SMatthew Ahrens 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
63078f17100SMatthew Ahrens 		free(zhp);
63178f17100SMatthew Ahrens 		return (NULL);
63278f17100SMatthew Ahrens 	}
63378f17100SMatthew Ahrens 
63478f17100SMatthew Ahrens 	/* Set the types. */
63578f17100SMatthew Ahrens 	zhp->zfs_head_type = parent->zfs_head_type;
63678f17100SMatthew Ahrens 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
63778f17100SMatthew Ahrens 
63878f17100SMatthew Ahrens 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
63978f17100SMatthew Ahrens 		nvlist_free(zhp->zfs_props);
64078f17100SMatthew Ahrens 		free(zhp);
64178f17100SMatthew Ahrens 		return (NULL);
64278f17100SMatthew Ahrens 	}
64378f17100SMatthew Ahrens 
64478f17100SMatthew Ahrens 	return (zhp);
64578f17100SMatthew Ahrens }
64678f17100SMatthew Ahrens 
647fbc66171SMarcel Telka struct zfs_open_bookmarks_cb_data {
648fbc66171SMarcel Telka 	const char *path;
649fbc66171SMarcel Telka 	zfs_handle_t *zhp;
650fbc66171SMarcel Telka };
651fbc66171SMarcel Telka 
652fbc66171SMarcel Telka static int
653fbc66171SMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
654fbc66171SMarcel Telka {
655fbc66171SMarcel Telka 	struct zfs_open_bookmarks_cb_data *dp = data;
656fbc66171SMarcel Telka 
657fa9e4066Sahrens 	/*
658fbc66171SMarcel Telka 	 * Is it the one we are looking for?
659fbc66171SMarcel Telka 	 */
660fbc66171SMarcel Telka 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
661fbc66171SMarcel Telka 		/*
662fbc66171SMarcel Telka 		 * We found it.  Save it and let the caller know we are done.
663fbc66171SMarcel Telka 		 */
664fbc66171SMarcel Telka 		dp->zhp = zhp;
665fbc66171SMarcel Telka 		return (EEXIST);
666fbc66171SMarcel Telka 	}
667fbc66171SMarcel Telka 
668fbc66171SMarcel Telka 	/*
669fbc66171SMarcel Telka 	 * Not found.  Close the handle and ask for another one.
670fbc66171SMarcel Telka 	 */
671fbc66171SMarcel Telka 	zfs_close(zhp);
672fbc66171SMarcel Telka 	return (0);
673fbc66171SMarcel Telka }
674fbc66171SMarcel Telka 
675fbc66171SMarcel Telka /*
676fbc66171SMarcel Telka  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
677fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
678fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
679fa9e4066Sahrens  */
680fa9e4066Sahrens zfs_handle_t *
68199653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
682fa9e4066Sahrens {
683fa9e4066Sahrens 	zfs_handle_t *zhp;
68499653d4eSeschrock 	char errbuf[1024];
685fbc66171SMarcel Telka 	char *bookp;
68699653d4eSeschrock 
68799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
68899653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
689fa9e4066Sahrens 
690fa9e4066Sahrens 	/*
69199653d4eSeschrock 	 * Validate the name before we even try to open it.
692fa9e4066Sahrens 	 */
693fbc66171SMarcel Telka 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
69499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
695fa9e4066Sahrens 		return (NULL);
696fa9e4066Sahrens 	}
697fa9e4066Sahrens 
698fa9e4066Sahrens 	/*
699fbc66171SMarcel Telka 	 * Bookmarks needs to be handled separately.
700fbc66171SMarcel Telka 	 */
701fbc66171SMarcel Telka 	bookp = strchr(path, '#');
702fbc66171SMarcel Telka 	if (bookp == NULL) {
703fbc66171SMarcel Telka 		/*
704fbc66171SMarcel Telka 		 * Try to get stats for the dataset, which will tell us if it
705fbc66171SMarcel Telka 		 * exists.
706fa9e4066Sahrens 		 */
707fa9e4066Sahrens 		errno = 0;
70899653d4eSeschrock 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
709ece3d9b3Slling 			(void) zfs_standard_error(hdl, errno, errbuf);
710fa9e4066Sahrens 			return (NULL);
711fa9e4066Sahrens 		}
712fbc66171SMarcel Telka 	} else {
713fbc66171SMarcel Telka 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
714fbc66171SMarcel Telka 		zfs_handle_t *pzhp;
715fbc66171SMarcel Telka 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
716fbc66171SMarcel Telka 
717fbc66171SMarcel Telka 		/*
718fbc66171SMarcel Telka 		 * We need to cut out '#' and everything after '#'
719fbc66171SMarcel Telka 		 * to get the parent dataset name only.
720fbc66171SMarcel Telka 		 */
721fbc66171SMarcel Telka 		assert(bookp - path < sizeof (dsname));
722fbc66171SMarcel Telka 		(void) strncpy(dsname, path, bookp - path);
723fbc66171SMarcel Telka 		dsname[bookp - path] = '\0';
724fbc66171SMarcel Telka 
725fbc66171SMarcel Telka 		/*
726fbc66171SMarcel Telka 		 * Create handle for the parent dataset.
727fbc66171SMarcel Telka 		 */
728fbc66171SMarcel Telka 		errno = 0;
729fbc66171SMarcel Telka 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
730fbc66171SMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
731fbc66171SMarcel Telka 			return (NULL);
732fbc66171SMarcel Telka 		}
733fbc66171SMarcel Telka 
734fbc66171SMarcel Telka 		/*
735fbc66171SMarcel Telka 		 * Iterate bookmarks to find the right one.
736fbc66171SMarcel Telka 		 */
737fbc66171SMarcel Telka 		errno = 0;
738fbc66171SMarcel Telka 		if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
739fbc66171SMarcel Telka 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
740fbc66171SMarcel Telka 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
741fbc66171SMarcel Telka 			zfs_close(pzhp);
742fbc66171SMarcel Telka 			return (NULL);
743fbc66171SMarcel Telka 		}
744fbc66171SMarcel Telka 		if (cb_data.zhp == NULL) {
745fbc66171SMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
746fbc66171SMarcel Telka 			zfs_close(pzhp);
747fbc66171SMarcel Telka 			return (NULL);
748fbc66171SMarcel Telka 		}
749fbc66171SMarcel Telka 		zhp = cb_data.zhp;
750fbc66171SMarcel Telka 
751fbc66171SMarcel Telka 		/*
752fbc66171SMarcel Telka 		 * Cleanup.
753fbc66171SMarcel Telka 		 */
754fbc66171SMarcel Telka 		zfs_close(pzhp);
755fbc66171SMarcel Telka 	}
756fa9e4066Sahrens 
757fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
75899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
75994de1d4cSeschrock 		zfs_close(zhp);
760fa9e4066Sahrens 		return (NULL);
761fa9e4066Sahrens 	}
762fa9e4066Sahrens 
763fa9e4066Sahrens 	return (zhp);
764fa9e4066Sahrens }
765fa9e4066Sahrens 
766fa9e4066Sahrens /*
767fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
768fa9e4066Sahrens  */
769fa9e4066Sahrens void
770fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
771fa9e4066Sahrens {
772fa9e4066Sahrens 	if (zhp->zfs_mntopts)
773fa9e4066Sahrens 		free(zhp->zfs_mntopts);
77499653d4eSeschrock 	nvlist_free(zhp->zfs_props);
775e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
77692241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
777fa9e4066Sahrens 	free(zhp);
778fa9e4066Sahrens }
779fa9e4066Sahrens 
780ebedde84SEric Taylor typedef struct mnttab_node {
781ebedde84SEric Taylor 	struct mnttab mtn_mt;
782ebedde84SEric Taylor 	avl_node_t mtn_node;
783ebedde84SEric Taylor } mnttab_node_t;
784ebedde84SEric Taylor 
785ebedde84SEric Taylor static int
786ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
787ebedde84SEric Taylor {
788ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
789ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
790ebedde84SEric Taylor 	int rv;
791ebedde84SEric Taylor 
792ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
793ebedde84SEric Taylor 
794ebedde84SEric Taylor 	if (rv == 0)
795ebedde84SEric Taylor 		return (0);
796ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
797ebedde84SEric Taylor }
798ebedde84SEric Taylor 
799ebedde84SEric Taylor void
800ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
801ebedde84SEric Taylor {
802ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
803ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
804ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
805b2634b9cSEric Taylor }
806b2634b9cSEric Taylor 
807b2634b9cSEric Taylor void
808b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
809b2634b9cSEric Taylor {
810b2634b9cSEric Taylor 	struct mnttab entry;
811ebedde84SEric Taylor 
812ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
813ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
814ebedde84SEric Taylor 		mnttab_node_t *mtn;
815ebedde84SEric Taylor 
816ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
817ebedde84SEric Taylor 			continue;
818ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
819ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
820ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
821ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
822ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
823ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
824ebedde84SEric Taylor 	}
825ebedde84SEric Taylor }
826ebedde84SEric Taylor 
827ebedde84SEric Taylor void
828ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
829ebedde84SEric Taylor {
830ebedde84SEric Taylor 	void *cookie = NULL;
831ebedde84SEric Taylor 	mnttab_node_t *mtn;
832ebedde84SEric Taylor 
833ebedde84SEric Taylor 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
834ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
835ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
836ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
837ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
838ebedde84SEric Taylor 		free(mtn);
839ebedde84SEric Taylor 	}
840ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
841ebedde84SEric Taylor }
842ebedde84SEric Taylor 
843b2634b9cSEric Taylor void
844b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
845b2634b9cSEric Taylor {
846b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
847b2634b9cSEric Taylor }
848b2634b9cSEric Taylor 
849ebedde84SEric Taylor int
850ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
851ebedde84SEric Taylor     struct mnttab *entry)
852ebedde84SEric Taylor {
853ebedde84SEric Taylor 	mnttab_node_t find;
854ebedde84SEric Taylor 	mnttab_node_t *mtn;
855ebedde84SEric Taylor 
856b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
857b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
858b2634b9cSEric Taylor 
859b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
860b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
861b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
862b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
863b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
864b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
865b2634b9cSEric Taylor 			return (0);
866b2634b9cSEric Taylor 		else
867b2634b9cSEric Taylor 			return (ENOENT);
868b2634b9cSEric Taylor 	}
869b2634b9cSEric Taylor 
870ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
871b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
872ebedde84SEric Taylor 
873ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
874ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
875ebedde84SEric Taylor 	if (mtn) {
876ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
877ebedde84SEric Taylor 		return (0);
878ebedde84SEric Taylor 	}
879ebedde84SEric Taylor 	return (ENOENT);
880ebedde84SEric Taylor }
881ebedde84SEric Taylor 
882ebedde84SEric Taylor void
883ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
884ebedde84SEric Taylor     const char *mountp, const char *mntopts)
885ebedde84SEric Taylor {
886ebedde84SEric Taylor 	mnttab_node_t *mtn;
887ebedde84SEric Taylor 
888ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
889ebedde84SEric Taylor 		return;
890ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
891ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
892ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
893ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
894ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
895ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
896ebedde84SEric Taylor }
897ebedde84SEric Taylor 
898ebedde84SEric Taylor void
899ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
900ebedde84SEric Taylor {
901ebedde84SEric Taylor 	mnttab_node_t find;
902ebedde84SEric Taylor 	mnttab_node_t *ret;
903ebedde84SEric Taylor 
904ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
905ebedde84SEric Taylor 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
906ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
907ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
908ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
909ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
910ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
911ebedde84SEric Taylor 		free(ret);
912ebedde84SEric Taylor 	}
913ebedde84SEric Taylor }
914ebedde84SEric Taylor 
9157b97dc1aSrm160521 int
9167b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9177b97dc1aSrm160521 {
91829ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9197b97dc1aSrm160521 
9207b97dc1aSrm160521 	if (zpool_handle == NULL)
9217b97dc1aSrm160521 		return (-1);
9227b97dc1aSrm160521 
9237b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
9247b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
9257b97dc1aSrm160521 	return (0);
9267b97dc1aSrm160521 }
9277b97dc1aSrm160521 
9287b97dc1aSrm160521 /*
9297b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
9307b97dc1aSrm160521  */
9317b97dc1aSrm160521 static int
9327b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9337b97dc1aSrm160521 {
9347b97dc1aSrm160521 	int spa_version;
9357b97dc1aSrm160521 
9367b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
9377b97dc1aSrm160521 		return (-1);
9387b97dc1aSrm160521 
9397b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
9407b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
9417b97dc1aSrm160521 	else
9427b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
9437b97dc1aSrm160521 
9447b97dc1aSrm160521 	return (0);
9457b97dc1aSrm160521 }
9467b97dc1aSrm160521 
947b1b8ab34Slling /*
948e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
949e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
950e9dbad6fSeschrock  * strings.
951fa9e4066Sahrens  */
9520a48a24eStimh nvlist_t *
9530a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
954e9316f76SJoe Stein     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
955e9316f76SJoe Stein     const char *errbuf)
956fa9e4066Sahrens {
957e9dbad6fSeschrock 	nvpair_t *elem;
958e9dbad6fSeschrock 	uint64_t intval;
959e9dbad6fSeschrock 	char *strval;
960990b4856Slling 	zfs_prop_t prop;
961e9dbad6fSeschrock 	nvlist_t *ret;
962da6c28aaSamw 	int chosen_normal = -1;
963da6c28aaSamw 	int chosen_utf = -1;
964990b4856Slling 
965e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
966e9dbad6fSeschrock 		(void) no_memory(hdl);
967e9dbad6fSeschrock 		return (NULL);
968e9dbad6fSeschrock 	}
969fa9e4066Sahrens 
97014843421SMatthew Ahrens 	/*
97114843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
97214843421SMatthew Ahrens 	 */
97314843421SMatthew Ahrens 
974e9dbad6fSeschrock 	elem = NULL;
975e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
976990b4856Slling 		const char *propname = nvpair_name(elem);
97799653d4eSeschrock 
97814843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
97914843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
980fa9e4066Sahrens 			/*
98114843421SMatthew Ahrens 			 * This is a user property: make sure it's a
982990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
983990b4856Slling 			 */
984990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
985990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986990b4856Slling 				    "'%s' must be a string"), propname);
987990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
988990b4856Slling 				goto error;
989990b4856Slling 			}
990990b4856Slling 
991990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
992e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
993e9dbad6fSeschrock 				    "property name '%s' is too long"),
994e9dbad6fSeschrock 				    propname);
995990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
996e9dbad6fSeschrock 				goto error;
997e9dbad6fSeschrock 			}
998e9dbad6fSeschrock 
999e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
1000e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
1001e9dbad6fSeschrock 				(void) no_memory(hdl);
1002e9dbad6fSeschrock 				goto error;
1003e9dbad6fSeschrock 			}
1004e9dbad6fSeschrock 			continue;
1005e9dbad6fSeschrock 		}
1006fa9e4066Sahrens 
100714843421SMatthew Ahrens 		/*
100814843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
100914843421SMatthew Ahrens 		 * snapshots.
101014843421SMatthew Ahrens 		 */
1011bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
1012bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
1014bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1015bb0ade09Sahrens 			goto error;
1016bb0ade09Sahrens 		}
1017bb0ade09Sahrens 
101814843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
101914843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
102014843421SMatthew Ahrens 			char newpropname[128];
102114843421SMatthew Ahrens 			char domain[128];
102214843421SMatthew Ahrens 			uint64_t rid;
102314843421SMatthew Ahrens 			uint64_t valary[3];
102414843421SMatthew Ahrens 
102514843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
102614843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
102714843421SMatthew Ahrens 				zfs_error_aux(hdl,
102814843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
102914843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
103014843421SMatthew Ahrens 				    propname);
103114843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
103214843421SMatthew Ahrens 				goto error;
103314843421SMatthew Ahrens 			}
103414843421SMatthew Ahrens 
103514843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
103614843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
103714843421SMatthew Ahrens 				zfs_error_aux(hdl,
103814843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
103914843421SMatthew Ahrens 				    propname);
104014843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
104114843421SMatthew Ahrens 				    errbuf);
104214843421SMatthew Ahrens 				goto error;
104314843421SMatthew Ahrens 			}
104414843421SMatthew Ahrens 
104514843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
104614843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
104714843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
104814843421SMatthew Ahrens 					intval = 0;
104914843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
105014843421SMatthew Ahrens 				    strval, &intval) != 0) {
105114843421SMatthew Ahrens 					(void) zfs_error(hdl,
105214843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
105314843421SMatthew Ahrens 					goto error;
105414843421SMatthew Ahrens 				}
105514843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
105614843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
105714843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
105814843421SMatthew Ahrens 				if (intval == 0) {
105914843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106014843421SMatthew Ahrens 					    "use 'none' to disable "
106114843421SMatthew Ahrens 					    "userquota/groupquota"));
106214843421SMatthew Ahrens 					goto error;
106314843421SMatthew Ahrens 				}
106414843421SMatthew Ahrens 			} else {
106514843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106614843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
106714843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
106814843421SMatthew Ahrens 				goto error;
106914843421SMatthew Ahrens 			}
107014843421SMatthew Ahrens 
10712d5843dbSMatthew Ahrens 			/*
10722d5843dbSMatthew Ahrens 			 * Encode the prop name as
10732d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
10742d5843dbSMatthew Ahrens 			 * for the kernel to decode.
10752d5843dbSMatthew Ahrens 			 */
107614843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
10772d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10782d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
107914843421SMatthew Ahrens 			valary[0] = uqtype;
108014843421SMatthew Ahrens 			valary[1] = rid;
108114843421SMatthew Ahrens 			valary[2] = intval;
108214843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
108314843421SMatthew Ahrens 			    valary, 3) != 0) {
108414843421SMatthew Ahrens 				(void) no_memory(hdl);
108514843421SMatthew Ahrens 				goto error;
108614843421SMatthew Ahrens 			}
108714843421SMatthew Ahrens 			continue;
108819b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
108919b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109019b94df9SMatthew Ahrens 			    "'%s' is readonly"),
109119b94df9SMatthew Ahrens 			    propname);
109219b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
109319b94df9SMatthew Ahrens 			goto error;
109414843421SMatthew Ahrens 		}
109514843421SMatthew Ahrens 
109614843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
109714843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109814843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
109914843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
110014843421SMatthew Ahrens 			goto error;
110114843421SMatthew Ahrens 		}
110214843421SMatthew Ahrens 
1103e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1104e9dbad6fSeschrock 			zfs_error_aux(hdl,
1105e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1106e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1107e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1108e9dbad6fSeschrock 			goto error;
1109e9dbad6fSeschrock 		}
1110e9dbad6fSeschrock 
1111e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1112da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
1113e9dbad6fSeschrock 			zfs_error_aux(hdl,
1114e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1115e9dbad6fSeschrock 			    propname);
1116e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1117e9dbad6fSeschrock 			goto error;
1118e9dbad6fSeschrock 		}
1119e9dbad6fSeschrock 
1120990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1121990b4856Slling 		    &strval, &intval, errbuf) != 0)
1122e9dbad6fSeschrock 			goto error;
1123e9dbad6fSeschrock 
1124e9dbad6fSeschrock 		/*
1125e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1126e9dbad6fSeschrock 		 */
1127e9dbad6fSeschrock 		switch (prop) {
1128e7437265Sahrens 		case ZFS_PROP_VERSION:
1129e7437265Sahrens 		{
1130e7437265Sahrens 			int version;
1131e7437265Sahrens 
1132e7437265Sahrens 			if (zhp == NULL)
1133e7437265Sahrens 				break;
1134e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1135e7437265Sahrens 			if (intval < version) {
1136e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1137e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1138e7437265Sahrens 				    version);
1139e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1140e7437265Sahrens 				goto error;
1141e7437265Sahrens 			}
1142e7437265Sahrens 			break;
1143e7437265Sahrens 		}
1144e7437265Sahrens 
1145e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1146b5152584SMatthew Ahrens 		case ZFS_PROP_RECORDSIZE:
1147b5152584SMatthew Ahrens 		{
1148b5152584SMatthew Ahrens 			int maxbs = SPA_MAXBLOCKSIZE;
1149e9316f76SJoe Stein 			if (zpool_hdl != NULL) {
1150e9316f76SJoe Stein 				maxbs = zpool_get_prop_int(zpool_hdl,
1151b5152584SMatthew Ahrens 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1152b5152584SMatthew Ahrens 			}
1153b5152584SMatthew Ahrens 			/*
1154b5152584SMatthew Ahrens 			 * Volumes are limited to a volblocksize of 128KB,
1155b5152584SMatthew Ahrens 			 * because they typically service workloads with
1156b5152584SMatthew Ahrens 			 * small random writes, which incur a large performance
1157b5152584SMatthew Ahrens 			 * penalty with large blocks.
1158b5152584SMatthew Ahrens 			 */
1159b5152584SMatthew Ahrens 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1160b5152584SMatthew Ahrens 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1161b5152584SMatthew Ahrens 			/*
1162b5152584SMatthew Ahrens 			 * The value must be a power of two between
1163b5152584SMatthew Ahrens 			 * SPA_MINBLOCKSIZE and maxbs.
1164b5152584SMatthew Ahrens 			 */
1165e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1166b5152584SMatthew Ahrens 			    intval > maxbs || !ISP2(intval)) {
1167e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1168b5152584SMatthew Ahrens 				    "'%s' must be power of 2 from 512B "
1169b5152584SMatthew Ahrens 				    "to %uKB"), propname, maxbs >> 10);
1170e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1171e9dbad6fSeschrock 				goto error;
1172e9dbad6fSeschrock 			}
1173e9dbad6fSeschrock 			break;
1174b5152584SMatthew Ahrens 		}
11754201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
11764201a95eSRic Aleshire 		{
11774201a95eSRic Aleshire 			/*
11784201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
11794201a95eSRic Aleshire 			 * internal hex label string.
11804201a95eSRic Aleshire 			 */
11814201a95eSRic Aleshire 
11824201a95eSRic Aleshire 			m_label_t *new_sl;
11834201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
11844201a95eSRic Aleshire 
11854201a95eSRic Aleshire 			/* Default value is already OK. */
11864201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
11874201a95eSRic Aleshire 				break;
11884201a95eSRic Aleshire 
11894201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
11904201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
11914201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
11924201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
11934201a95eSRic Aleshire 				goto badlabel;
11944201a95eSRic Aleshire 			}
11954201a95eSRic Aleshire 
11964201a95eSRic Aleshire 			/* Now translate to hex internal label string */
11974201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
11984201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
11994201a95eSRic Aleshire 				if (hex)
12004201a95eSRic Aleshire 					free(hex);
12014201a95eSRic Aleshire 				goto badlabel;
12024201a95eSRic Aleshire 			}
12034201a95eSRic Aleshire 			m_label_free(new_sl);
12044201a95eSRic Aleshire 
12054201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
12064201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
12074201a95eSRic Aleshire 				free(hex);
12084201a95eSRic Aleshire 				break;
12094201a95eSRic Aleshire 			}
12104201a95eSRic Aleshire 
12114201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1212569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
12134201a95eSRic Aleshire 			    DATA_TYPE_STRING);
12144201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12154201a95eSRic Aleshire 			    hex) == 0);
12164201a95eSRic Aleshire 			free(hex);
12174201a95eSRic Aleshire 
12184201a95eSRic Aleshire 			break;
12194201a95eSRic Aleshire 
12204201a95eSRic Aleshire badlabel:
12214201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12224201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
12234201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12244201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
12254201a95eSRic Aleshire 			goto error;
12264201a95eSRic Aleshire 
12274201a95eSRic Aleshire 		}
12284201a95eSRic Aleshire 
1229e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
123089eef05eSrm160521 		{
123189eef05eSrm160521 			namecheck_err_t why;
123289eef05eSrm160521 
1233e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1234e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1235e9dbad6fSeschrock 				break;
1236e9dbad6fSeschrock 
123789eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
123889eef05eSrm160521 				switch (why) {
123989eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
124089eef05eSrm160521 					zfs_error_aux(hdl,
124189eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1242e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1243e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
124489eef05eSrm160521 					break;
124589eef05eSrm160521 				case NAME_ERR_TOOLONG:
124689eef05eSrm160521 					zfs_error_aux(hdl,
124789eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
124889eef05eSrm160521 					    "component of '%s' is too long"),
124989eef05eSrm160521 					    propname);
125089eef05eSrm160521 					break;
125189eef05eSrm160521 				}
1252e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1253e9dbad6fSeschrock 				goto error;
1254e9dbad6fSeschrock 			}
125589eef05eSrm160521 		}
125689eef05eSrm160521 
1257f3861e1aSahl 			/*FALLTHRU*/
1258e9dbad6fSeschrock 
1259da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1260f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1261e9dbad6fSeschrock 			/*
1262da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1263da6c28aaSamw 			 * properties, check if it can be set in a
1264da6c28aaSamw 			 * global/non-global zone based on
1265f3861e1aSahl 			 * the zoned property value:
1266fa9e4066Sahrens 			 *
1267fa9e4066Sahrens 			 *		global zone	    non-global zone
1268f3861e1aSahl 			 * --------------------------------------------------
1269fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1270fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1271da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1272fa9e4066Sahrens 			 *
1273fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1274fa9e4066Sahrens 			 *		sharenfs (yes)
1275da6c28aaSamw 			 *		sharesmb (yes)
1276fa9e4066Sahrens 			 */
1277e9dbad6fSeschrock 			if (zoned) {
1278fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
127999653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1280e9dbad6fSeschrock 					    "'%s' cannot be set on "
1281e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1282e9dbad6fSeschrock 					    propname);
1283e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1284e9dbad6fSeschrock 					    errbuf);
1285e9dbad6fSeschrock 					goto error;
1286da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1287da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
128899653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1289e9dbad6fSeschrock 					    "'%s' cannot be set in "
1290e9dbad6fSeschrock 					    "a non-global zone"), propname);
1291e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1292e9dbad6fSeschrock 					    errbuf);
1293e9dbad6fSeschrock 					goto error;
1294fa9e4066Sahrens 				}
1295fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1296fa9e4066Sahrens 				/*
1297fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
129814843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1299fa9e4066Sahrens 				 */
130099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1301e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1302e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1303e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1304e9dbad6fSeschrock 				goto error;
1305fa9e4066Sahrens 			}
1306f3861e1aSahl 
130767331909Sdougm 			/*
130867331909Sdougm 			 * At this point, it is legitimate to set the
130967331909Sdougm 			 * property. Now we want to make sure that the
131067331909Sdougm 			 * property value is valid if it is sharenfs.
131167331909Sdougm 			 */
1312da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1313da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
131467331909Sdougm 			    strcmp(strval, "on") != 0 &&
131567331909Sdougm 			    strcmp(strval, "off") != 0) {
1316da6c28aaSamw 				zfs_share_proto_t proto;
1317da6c28aaSamw 
1318da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1319da6c28aaSamw 					proto = PROTO_SMB;
1320da6c28aaSamw 				else
1321da6c28aaSamw 					proto = PROTO_NFS;
132267331909Sdougm 
132367331909Sdougm 				/*
1324da6c28aaSamw 				 * Must be an valid sharing protocol
1325da6c28aaSamw 				 * option string so init the libshare
1326da6c28aaSamw 				 * in order to enable the parser and
1327da6c28aaSamw 				 * then parse the options. We use the
1328da6c28aaSamw 				 * control API since we don't care about
1329da6c28aaSamw 				 * the current configuration and don't
133067331909Sdougm 				 * want the overhead of loading it
133167331909Sdougm 				 * until we actually do something.
133267331909Sdougm 				 */
133367331909Sdougm 
133467331909Sdougm 				if (zfs_init_libshare(hdl,
133567331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1336fac3008cSeschrock 					/*
1337fac3008cSeschrock 					 * An error occurred so we can't do
1338fac3008cSeschrock 					 * anything
1339fac3008cSeschrock 					 */
134067331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
134167331909Sdougm 					    "'%s' cannot be set: problem "
134267331909Sdougm 					    "in share initialization"),
134367331909Sdougm 					    propname);
1344fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1345fac3008cSeschrock 					    errbuf);
134667331909Sdougm 					goto error;
134767331909Sdougm 				}
134867331909Sdougm 
1349da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
135067331909Sdougm 					/*
135167331909Sdougm 					 * There was an error in parsing so
135267331909Sdougm 					 * deal with it by issuing an error
135367331909Sdougm 					 * message and leaving after
135467331909Sdougm 					 * uninitializing the the libshare
135567331909Sdougm 					 * interface.
135667331909Sdougm 					 */
135767331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135867331909Sdougm 					    "'%s' cannot be set to invalid "
135967331909Sdougm 					    "options"), propname);
1360fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1361fac3008cSeschrock 					    errbuf);
136267331909Sdougm 					zfs_uninit_libshare(hdl);
136367331909Sdougm 					goto error;
136467331909Sdougm 				}
136567331909Sdougm 				zfs_uninit_libshare(hdl);
136667331909Sdougm 			}
136767331909Sdougm 
1368f3861e1aSahl 			break;
1369da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1370da6c28aaSamw 			chosen_utf = (int)intval;
1371da6c28aaSamw 			break;
1372da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1373da6c28aaSamw 			chosen_normal = (int)intval;
1374da6c28aaSamw 			break;
1375fa9e4066Sahrens 		}
1376fa9e4066Sahrens 
1377e9dbad6fSeschrock 		/*
1378e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1379e9dbad6fSeschrock 		 * checks to enforce.
1380e9dbad6fSeschrock 		 */
1381e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1382e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1383e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1384e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1385e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1386e9dbad6fSeschrock 			char buf[64];
1387e9dbad6fSeschrock 
1388e9dbad6fSeschrock 			switch (prop) {
1389e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1390a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
1391e9dbad6fSeschrock 				if (intval > volsize) {
1392e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1393e9dbad6fSeschrock 					    "'%s' is greater than current "
1394e9dbad6fSeschrock 					    "volume size"), propname);
1395e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1396e9dbad6fSeschrock 					    errbuf);
1397e9dbad6fSeschrock 					goto error;
1398e9dbad6fSeschrock 				}
1399e9dbad6fSeschrock 				break;
1400e9dbad6fSeschrock 
1401e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1402e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1403e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1404e9dbad6fSeschrock 					    sizeof (buf));
1405e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1406e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1407e9dbad6fSeschrock 					    "volume block size (%s)"),
1408e9dbad6fSeschrock 					    propname, buf);
1409e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1410e9dbad6fSeschrock 					    errbuf);
1411e9dbad6fSeschrock 					goto error;
1412e9dbad6fSeschrock 				}
1413e9dbad6fSeschrock 
1414e9dbad6fSeschrock 				if (intval == 0) {
1415e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1416e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1417e9dbad6fSeschrock 					    propname);
1418e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1419e9dbad6fSeschrock 					    errbuf);
1420e9dbad6fSeschrock 					goto error;
1421e9dbad6fSeschrock 				}
1422f3861e1aSahl 				break;
1423e9dbad6fSeschrock 			}
1424e9dbad6fSeschrock 		}
1425e9dbad6fSeschrock 	}
1426e9dbad6fSeschrock 
1427e9dbad6fSeschrock 	/*
1428da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1429da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1430da6c28aaSamw 	 *
1431da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1432da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1433da6c28aaSamw 	 */
1434de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1435da6c28aaSamw 		if (nvlist_add_uint64(ret,
1436da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1437da6c28aaSamw 			(void) no_memory(hdl);
1438da6c28aaSamw 			goto error;
1439da6c28aaSamw 		}
1440de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1441da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1443da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1444da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1445da6c28aaSamw 		goto error;
1446da6c28aaSamw 	}
1447e9dbad6fSeschrock 	return (ret);
1448e9dbad6fSeschrock 
1449e9dbad6fSeschrock error:
1450e9dbad6fSeschrock 	nvlist_free(ret);
1451e9dbad6fSeschrock 	return (NULL);
1452e9dbad6fSeschrock }
1453e9dbad6fSeschrock 
145436db6475SEric Taylor int
145536db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
145636db6475SEric Taylor {
145736db6475SEric Taylor 	uint64_t old_volsize;
145836db6475SEric Taylor 	uint64_t new_volsize;
145936db6475SEric Taylor 	uint64_t old_reservation;
146036db6475SEric Taylor 	uint64_t new_reservation;
146136db6475SEric Taylor 	zfs_prop_t resv_prop;
1462c61ea566SGeorge Wilson 	nvlist_t *props;
146336db6475SEric Taylor 
146436db6475SEric Taylor 	/*
146536db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
146636db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
146736db6475SEric Taylor 	 */
146836db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
146936db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
147036db6475SEric Taylor 		return (-1);
147136db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1472c61ea566SGeorge Wilson 
1473c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1474c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1475c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1476c61ea566SGeorge Wilson 
1477c61ea566SGeorge Wilson 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1478c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1479c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1480c61ea566SGeorge Wilson 		fnvlist_free(props);
148136db6475SEric Taylor 		return (0);
148236db6475SEric Taylor 	}
148336db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1484c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1485c61ea566SGeorge Wilson 		fnvlist_free(props);
148636db6475SEric Taylor 		return (-1);
1487c61ea566SGeorge Wilson 	}
1488c61ea566SGeorge Wilson 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1489c61ea566SGeorge Wilson 	fnvlist_free(props);
1490c61ea566SGeorge Wilson 
149136db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
149236db6475SEric Taylor 	    new_reservation) != 0) {
149336db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
149436db6475SEric Taylor 		return (-1);
149536db6475SEric Taylor 	}
149636db6475SEric Taylor 	return (1);
149736db6475SEric Taylor }
149836db6475SEric Taylor 
149992241e0bSTom Erickson void
150092241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
150192241e0bSTom Erickson     char *errbuf)
150292241e0bSTom Erickson {
150392241e0bSTom Erickson 	switch (err) {
150492241e0bSTom Erickson 
150592241e0bSTom Erickson 	case ENOSPC:
150692241e0bSTom Erickson 		/*
150792241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
150892241e0bSTom Erickson 		 * something different; setting a quota or reservation
150992241e0bSTom Erickson 		 * doesn't use any disk space.
151092241e0bSTom Erickson 		 */
151192241e0bSTom Erickson 		switch (prop) {
151292241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
151392241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
151492241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151592241e0bSTom Erickson 			    "size is less than current used or "
151692241e0bSTom Erickson 			    "reserved space"));
151792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
151892241e0bSTom Erickson 			break;
151992241e0bSTom Erickson 
152092241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
152192241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
152292241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152392241e0bSTom Erickson 			    "size is greater than available space"));
152492241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
152592241e0bSTom Erickson 			break;
152692241e0bSTom Erickson 
152792241e0bSTom Erickson 		default:
152892241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
152992241e0bSTom Erickson 			break;
153092241e0bSTom Erickson 		}
153192241e0bSTom Erickson 		break;
153292241e0bSTom Erickson 
153392241e0bSTom Erickson 	case EBUSY:
153492241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
153592241e0bSTom Erickson 		break;
153692241e0bSTom Erickson 
153792241e0bSTom Erickson 	case EROFS:
153892241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
153992241e0bSTom Erickson 		break;
154092241e0bSTom Erickson 
15416fdcb3d1SWill Andrews 	case E2BIG:
15426fdcb3d1SWill Andrews 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15436fdcb3d1SWill Andrews 		    "property value too long"));
15446fdcb3d1SWill Andrews 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
15456fdcb3d1SWill Andrews 		break;
15466fdcb3d1SWill Andrews 
154792241e0bSTom Erickson 	case ENOTSUP:
154892241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
154992241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
155092241e0bSTom Erickson 		    "property or value"));
155192241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
155292241e0bSTom Erickson 		break;
155392241e0bSTom Erickson 
155492241e0bSTom Erickson 	case ERANGE:
1555b5152584SMatthew Ahrens 		if (prop == ZFS_PROP_COMPRESSION ||
1556b5152584SMatthew Ahrens 		    prop == ZFS_PROP_RECORDSIZE) {
155792241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155892241e0bSTom Erickson 			    "property setting is not allowed on "
155992241e0bSTom Erickson 			    "bootable datasets"));
156092241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
156145818ee1SMatthew Ahrens 		} else if (prop == ZFS_PROP_CHECKSUM ||
156245818ee1SMatthew Ahrens 		    prop == ZFS_PROP_DEDUP) {
156345818ee1SMatthew Ahrens 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156445818ee1SMatthew Ahrens 			    "property setting is not allowed on "
156545818ee1SMatthew Ahrens 			    "root pools"));
156645818ee1SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
156792241e0bSTom Erickson 		} else {
156892241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
156992241e0bSTom Erickson 		}
157092241e0bSTom Erickson 		break;
157192241e0bSTom Erickson 
1572ab003da8SJim Dunham 	case EINVAL:
1573ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1574ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1575ab003da8SJim Dunham 		} else {
1576ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1577ab003da8SJim Dunham 		}
1578ab003da8SJim Dunham 		break;
1579ab003da8SJim Dunham 
158092241e0bSTom Erickson 	case EOVERFLOW:
158192241e0bSTom Erickson 		/*
158292241e0bSTom Erickson 		 * This platform can't address a volume this big.
158392241e0bSTom Erickson 		 */
158492241e0bSTom Erickson #ifdef _ILP32
158592241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
158692241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
158792241e0bSTom Erickson 			break;
158892241e0bSTom Erickson 		}
158992241e0bSTom Erickson #endif
159092241e0bSTom Erickson 		/* FALLTHROUGH */
159192241e0bSTom Erickson 	default:
159292241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
159392241e0bSTom Erickson 	}
159492241e0bSTom Erickson }
159592241e0bSTom Erickson 
1596e9dbad6fSeschrock /*
1597e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1598e9dbad6fSeschrock  */
1599e9dbad6fSeschrock int
1600e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1601e9dbad6fSeschrock {
1602e9dbad6fSeschrock 	int ret = -1;
1603e9dbad6fSeschrock 	char errbuf[1024];
1604e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
160530925561SChris Williamson 	nvlist_t *nvl = NULL;
1606e9dbad6fSeschrock 
1607e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1608e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1609e9dbad6fSeschrock 	    zhp->zfs_name);
1610e9dbad6fSeschrock 
1611e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1612e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1613e9dbad6fSeschrock 		(void) no_memory(hdl);
1614e9dbad6fSeschrock 		goto error;
1615e9dbad6fSeschrock 	}
1616e9dbad6fSeschrock 
161730925561SChris Williamson 	ret = zfs_prop_set_list(zhp, nvl);
161830925561SChris Williamson 
161930925561SChris Williamson error:
162030925561SChris Williamson 	nvlist_free(nvl);
162130925561SChris Williamson 	return (ret);
162230925561SChris Williamson }
162330925561SChris Williamson 
162430925561SChris Williamson 
162530925561SChris Williamson 
162630925561SChris Williamson /*
162730925561SChris Williamson  * Given an nvlist of property names and values, set the properties for the
162830925561SChris Williamson  * given dataset.
162930925561SChris Williamson  */
163030925561SChris Williamson int
163130925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
163230925561SChris Williamson {
163330925561SChris Williamson 	zfs_cmd_t zc = { 0 };
163430925561SChris Williamson 	int ret = -1;
163530925561SChris Williamson 	prop_changelist_t **cls = NULL;
163630925561SChris Williamson 	int cl_idx;
163730925561SChris Williamson 	char errbuf[1024];
163830925561SChris Williamson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
163930925561SChris Williamson 	nvlist_t *nvl;
164030925561SChris Williamson 	int nvl_len;
164130925561SChris Williamson 	int added_resv;
164230925561SChris Williamson 
164330925561SChris Williamson 	(void) snprintf(errbuf, sizeof (errbuf),
164430925561SChris Williamson 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
164530925561SChris Williamson 	    zhp->zfs_name);
164630925561SChris Williamson 
164730925561SChris Williamson 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1648e9316f76SJoe Stein 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1649e9316f76SJoe Stein 	    errbuf)) == NULL)
1650e9dbad6fSeschrock 		goto error;
1651990b4856Slling 
165230925561SChris Williamson 	/*
165330925561SChris Williamson 	 * We have to check for any extra properties which need to be added
165430925561SChris Williamson 	 * before computing the length of the nvlist.
165530925561SChris Williamson 	 */
165630925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
165730925561SChris Williamson 	    elem != NULL;
165830925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
165930925561SChris Williamson 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
166030925561SChris Williamson 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
166130925561SChris Williamson 			goto error;
166230925561SChris Williamson 		}
166330925561SChris Williamson 	}
166430925561SChris Williamson 	/*
166530925561SChris Williamson 	 * Check how many properties we're setting and allocate an array to
166630925561SChris Williamson 	 * store changelist pointers for postfix().
166730925561SChris Williamson 	 */
166830925561SChris Williamson 	nvl_len = 0;
166930925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
167030925561SChris Williamson 	    elem != NULL;
167130925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem))
167230925561SChris Williamson 		nvl_len++;
167330925561SChris Williamson 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
167430925561SChris Williamson 		goto error;
1675e9dbad6fSeschrock 
167630925561SChris Williamson 	cl_idx = 0;
167730925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
167830925561SChris Williamson 	    elem != NULL;
167930925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
1680e9dbad6fSeschrock 
168130925561SChris Williamson 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
168230925561SChris Williamson 
168330925561SChris Williamson 		assert(cl_idx < nvl_len);
168430925561SChris Williamson 		/*
168530925561SChris Williamson 		 * We don't want to unmount & remount the dataset when changing
168630925561SChris Williamson 		 * its canmount property to 'on' or 'noauto'.  We only use
168730925561SChris Williamson 		 * the changelist logic to unmount when setting canmount=off.
168830925561SChris Williamson 		 */
168930925561SChris Williamson 		if (!(prop == ZFS_PROP_CANMOUNT &&
169030925561SChris Williamson 		    fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) {
169130925561SChris Williamson 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
169230925561SChris Williamson 			if (cls[cl_idx] == NULL)
169336db6475SEric Taylor 				goto error;
169436db6475SEric Taylor 		}
169536db6475SEric Taylor 
169630925561SChris Williamson 		if (prop == ZFS_PROP_MOUNTPOINT &&
169730925561SChris Williamson 		    changelist_haszonedchild(cls[cl_idx])) {
169899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1699fa9e4066Sahrens 			    "child dataset with inherited mountpoint is used "
170099653d4eSeschrock 			    "in a non-global zone"));
170199653d4eSeschrock 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1702fa9e4066Sahrens 			goto error;
1703fa9e4066Sahrens 		}
1704fa9e4066Sahrens 
170530925561SChris Williamson 		if (cls[cl_idx] != NULL &&
170630925561SChris Williamson 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1707fa9e4066Sahrens 			goto error;
1708fa9e4066Sahrens 
170930925561SChris Williamson 		cl_idx++;
171030925561SChris Williamson 	}
171130925561SChris Williamson 	assert(cl_idx == nvl_len);
171230925561SChris Williamson 
1713fa9e4066Sahrens 	/*
171430925561SChris Williamson 	 * Execute the corresponding ioctl() to set this list of properties.
1715fa9e4066Sahrens 	 */
1716fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1717fa9e4066Sahrens 
171830925561SChris Williamson 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
171930925561SChris Williamson 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1720e9dbad6fSeschrock 		goto error;
1721e9dbad6fSeschrock 
1722ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1723743a77edSAlan Wright 
1724fa9e4066Sahrens 	if (ret != 0) {
172530925561SChris Williamson 		/* Get the list of unset properties back and report them. */
172630925561SChris Williamson 		nvlist_t *errorprops = NULL;
172730925561SChris Williamson 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
172830925561SChris Williamson 			goto error;
172930925561SChris Williamson 		for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
173030925561SChris Williamson 		    elem != NULL;
173130925561SChris Williamson 		    elem = nvlist_next_nvpair(nvl, elem)) {
173230925561SChris Williamson 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
173392241e0bSTom Erickson 			zfs_setprop_error(hdl, prop, errno, errbuf);
173430925561SChris Williamson 		}
173530925561SChris Williamson 		nvlist_free(errorprops);
173630925561SChris Williamson 
173736db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
173836db6475SEric Taylor 			/* clean up the volsize property we tried to set */
173936db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
174036db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
174136db6475SEric Taylor 			nvlist_free(nvl);
174230925561SChris Williamson 			nvl = NULL;
174336db6475SEric Taylor 			zcmd_free_nvlists(&zc);
174430925561SChris Williamson 
174536db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
174636db6475SEric Taylor 				goto error;
174736db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
174836db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
174936db6475SEric Taylor 			    old_volsize) != 0)
175036db6475SEric Taylor 				goto error;
175136db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
175236db6475SEric Taylor 				goto error;
175336db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
175436db6475SEric Taylor 		}
1755fa9e4066Sahrens 	} else {
175630925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
175730925561SChris Williamson 			if (cls[cl_idx] != NULL) {
175830925561SChris Williamson 				int clp_err = changelist_postfix(cls[cl_idx]);
175930925561SChris Williamson 				if (clp_err != 0)
176030925561SChris Williamson 					ret = clp_err;
176130925561SChris Williamson 			}
176230925561SChris Williamson 		}
1763a227b7f4Shs24103 
1764fa9e4066Sahrens 		/*
1765fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1766fa9e4066Sahrens 		 * is reflected.
1767fa9e4066Sahrens 		 */
1768a227b7f4Shs24103 		if (ret == 0)
1769fa9e4066Sahrens 			(void) get_stats(zhp);
1770fa9e4066Sahrens 	}
1771fa9e4066Sahrens 
1772fa9e4066Sahrens error:
1773e9dbad6fSeschrock 	nvlist_free(nvl);
1774e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
177530925561SChris Williamson 	if (cls != NULL) {
177630925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
177730925561SChris Williamson 			if (cls[cl_idx] != NULL)
177830925561SChris Williamson 				changelist_free(cls[cl_idx]);
177930925561SChris Williamson 		}
178030925561SChris Williamson 		free(cls);
178130925561SChris Williamson 	}
1782fa9e4066Sahrens 	return (ret);
1783fa9e4066Sahrens }
1784fa9e4066Sahrens 
1785fa9e4066Sahrens /*
178692241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
178792241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1788fa9e4066Sahrens  */
1789fa9e4066Sahrens int
179092241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1791fa9e4066Sahrens {
1792fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1793fa9e4066Sahrens 	int ret;
1794fa9e4066Sahrens 	prop_changelist_t *cl;
179599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
179699653d4eSeschrock 	char errbuf[1024];
1797e9dbad6fSeschrock 	zfs_prop_t prop;
179899653d4eSeschrock 
179999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
180099653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1801fa9e4066Sahrens 
180292241e0bSTom Erickson 	zc.zc_cookie = received;
1803990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1804e9dbad6fSeschrock 		/*
1805e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1806e9dbad6fSeschrock 		 * small, so just do it here.
1807e9dbad6fSeschrock 		 */
1808e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1809e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1810e9dbad6fSeschrock 			    "invalid property"));
1811e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1812e9dbad6fSeschrock 		}
1813e9dbad6fSeschrock 
1814e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1815e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1816e9dbad6fSeschrock 
1817e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1818e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1819e9dbad6fSeschrock 
1820e9dbad6fSeschrock 		return (0);
1821e9dbad6fSeschrock 	}
1822e9dbad6fSeschrock 
1823fa9e4066Sahrens 	/*
1824fa9e4066Sahrens 	 * Verify that this property is inheritable.
1825fa9e4066Sahrens 	 */
182699653d4eSeschrock 	if (zfs_prop_readonly(prop))
182799653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1828fa9e4066Sahrens 
182992241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
183099653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1831fa9e4066Sahrens 
1832fa9e4066Sahrens 	/*
1833fa9e4066Sahrens 	 * Check to see if the value applies to this type
1834fa9e4066Sahrens 	 */
183599653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
183699653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1837fa9e4066Sahrens 
1838bf7c2d40Srm160521 	/*
183936db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1840bf7c2d40Srm160521 	 */
1841bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1842fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1843e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1844fa9e4066Sahrens 
1845fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1846fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
184799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184899653d4eSeschrock 		    "dataset is used in a non-global zone"));
184999653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1850fa9e4066Sahrens 	}
1851fa9e4066Sahrens 
1852fa9e4066Sahrens 	/*
1853fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1854fa9e4066Sahrens 	 */
18550069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1856fa9e4066Sahrens 		return (-1);
1857fa9e4066Sahrens 
1858fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
185999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
186099653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
186199653d4eSeschrock 		    "in a non-global zone"));
186299653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1863fa9e4066Sahrens 		goto error;
1864fa9e4066Sahrens 	}
1865fa9e4066Sahrens 
1866fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1867fa9e4066Sahrens 		goto error;
1868fa9e4066Sahrens 
1869e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
187099653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1871fa9e4066Sahrens 	} else {
1872fa9e4066Sahrens 
1873efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1874fa9e4066Sahrens 			goto error;
1875fa9e4066Sahrens 
1876fa9e4066Sahrens 		/*
1877fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1878fa9e4066Sahrens 		 */
1879fa9e4066Sahrens 		(void) get_stats(zhp);
1880fa9e4066Sahrens 	}
1881fa9e4066Sahrens 
1882fa9e4066Sahrens error:
1883fa9e4066Sahrens 	changelist_free(cl);
1884fa9e4066Sahrens 	return (ret);
1885fa9e4066Sahrens }
1886fa9e4066Sahrens 
1887fa9e4066Sahrens /*
18887f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
18897f7322feSeschrock  * extract them appropriately.
18907f7322feSeschrock  */
18917f7322feSeschrock static uint64_t
18927f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18937f7322feSeschrock {
18947f7322feSeschrock 	nvlist_t *nv;
18957f7322feSeschrock 	uint64_t value;
18967f7322feSeschrock 
1897a2eea2e1Sahrens 	*source = NULL;
18987f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
18997f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1900990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1901990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19027f7322feSeschrock 	} else {
19032e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
19042e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
19057f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
19067f7322feSeschrock 		*source = "";
19077f7322feSeschrock 	}
19087f7322feSeschrock 
19097f7322feSeschrock 	return (value);
19107f7322feSeschrock }
19117f7322feSeschrock 
19129c3fd121SMatthew Ahrens static const char *
19137f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19147f7322feSeschrock {
19157f7322feSeschrock 	nvlist_t *nv;
19169c3fd121SMatthew Ahrens 	const char *value;
19177f7322feSeschrock 
1918a2eea2e1Sahrens 	*source = NULL;
19197f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19207f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
19219c3fd121SMatthew Ahrens 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1922990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19237f7322feSeschrock 	} else {
19242e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
19252e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
19269c3fd121SMatthew Ahrens 		value = zfs_prop_default_string(prop);
19277f7322feSeschrock 		*source = "";
19287f7322feSeschrock 	}
19297f7322feSeschrock 
19307f7322feSeschrock 	return (value);
19317f7322feSeschrock }
19327f7322feSeschrock 
193392241e0bSTom Erickson static boolean_t
193492241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
193592241e0bSTom Erickson {
193692241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
193792241e0bSTom Erickson }
193892241e0bSTom Erickson 
193992241e0bSTom Erickson static void
194092241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
194192241e0bSTom Erickson {
194292241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
194392241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
194492241e0bSTom Erickson }
194592241e0bSTom Erickson 
194692241e0bSTom Erickson static void
194792241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
194892241e0bSTom Erickson {
194992241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
195092241e0bSTom Erickson 	*cookie = 0;
195192241e0bSTom Erickson }
195292241e0bSTom Erickson 
19537f7322feSeschrock /*
1954fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1955fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1956fa9e4066Sahrens  *
1957fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1958fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1959fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1960fa9e4066Sahrens  * the source "temporary".
1961fa9e4066Sahrens  */
196299653d4eSeschrock static int
1963990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
196499653d4eSeschrock     char **source, uint64_t *val)
1965fa9e4066Sahrens {
1966bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
196796510749Stimh 	nvlist_t *zplprops = NULL;
1968fa9e4066Sahrens 	struct mnttab mnt;
19693ccfa83cSahrens 	char *mntopt_on = NULL;
19703ccfa83cSahrens 	char *mntopt_off = NULL;
197192241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1972fa9e4066Sahrens 
1973fa9e4066Sahrens 	*source = NULL;
1974fa9e4066Sahrens 
19753ccfa83cSahrens 	switch (prop) {
19763ccfa83cSahrens 	case ZFS_PROP_ATIME:
19773ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
19783ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
19793ccfa83cSahrens 		break;
19803ccfa83cSahrens 
19813ccfa83cSahrens 	case ZFS_PROP_DEVICES:
19823ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
19833ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
19843ccfa83cSahrens 		break;
19853ccfa83cSahrens 
19863ccfa83cSahrens 	case ZFS_PROP_EXEC:
19873ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
19883ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
19893ccfa83cSahrens 		break;
19903ccfa83cSahrens 
1991*1a4cea1bSArne Jansen 	case ZFS_PROP_FOLLOW:
1992*1a4cea1bSArne Jansen 		mntopt_on = MNTOPT_FOLLOW;
1993*1a4cea1bSArne Jansen 		mntopt_off = MNTOPT_NOFOLLOW;
1994*1a4cea1bSArne Jansen 		break;
1995*1a4cea1bSArne Jansen 
19963ccfa83cSahrens 	case ZFS_PROP_READONLY:
19973ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
19983ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
19993ccfa83cSahrens 		break;
20003ccfa83cSahrens 
20013ccfa83cSahrens 	case ZFS_PROP_SETUID:
20023ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
20033ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
20043ccfa83cSahrens 		break;
20053ccfa83cSahrens 
20063ccfa83cSahrens 	case ZFS_PROP_XATTR:
20073ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
20083ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
20093ccfa83cSahrens 		break;
2010da6c28aaSamw 
2011da6c28aaSamw 	case ZFS_PROP_NBMAND:
2012da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
2013da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2014da6c28aaSamw 		break;
20153ccfa83cSahrens 	}
20163ccfa83cSahrens 
20173bb79becSeschrock 	/*
20183bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
20193bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
20203bb79becSeschrock 	 * we're looking up a property which requires its presence.
20213bb79becSeschrock 	 */
20223bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
20233ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2024ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2025ebedde84SEric Taylor 		struct mnttab entry;
20263bb79becSeschrock 
2027ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2028ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
20293ccfa83cSahrens 			    entry.mnt_mntopts);
20303ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
20313bb79becSeschrock 				return (-1);
20323ccfa83cSahrens 		}
20333bb79becSeschrock 
20343bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
20353bb79becSeschrock 	}
20363bb79becSeschrock 
2037fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2038fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2039fa9e4066Sahrens 	else
2040fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2041fa9e4066Sahrens 
2042fa9e4066Sahrens 	switch (prop) {
2043fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2044fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2045fa9e4066Sahrens 	case ZFS_PROP_EXEC:
2046*1a4cea1bSArne Jansen 	case ZFS_PROP_FOLLOW:
20473ccfa83cSahrens 	case ZFS_PROP_READONLY:
20483ccfa83cSahrens 	case ZFS_PROP_SETUID:
20493ccfa83cSahrens 	case ZFS_PROP_XATTR:
2050da6c28aaSamw 	case ZFS_PROP_NBMAND:
205199653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2052fa9e4066Sahrens 
205392241e0bSTom Erickson 		if (received)
205492241e0bSTom Erickson 			break;
205592241e0bSTom Erickson 
20563ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
205799653d4eSeschrock 			*val = B_TRUE;
2058fa9e4066Sahrens 			if (src)
2059990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
20603ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
206199653d4eSeschrock 			*val = B_FALSE;
2062fa9e4066Sahrens 			if (src)
2063990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2064fa9e4066Sahrens 		}
206599653d4eSeschrock 		break;
2066fa9e4066Sahrens 
20673ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
2068d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
2069fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2070a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2071fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2072a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2073a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2074a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2075a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2076a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2077a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
207892241e0bSTom Erickson 
207992241e0bSTom Erickson 		if (*source == NULL) {
208092241e0bSTom Erickson 			/* not default, must be local */
2081fa9e4066Sahrens 			*source = zhp->zfs_name;
208292241e0bSTom Erickson 		}
208399653d4eSeschrock 		break;
2084fa9e4066Sahrens 
2085fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
208699653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
208799653d4eSeschrock 		break;
2088fa9e4066Sahrens 
208939c23413Seschrock 	case ZFS_PROP_NUMCLONES:
209039c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
209139c23413Seschrock 		break;
209239c23413Seschrock 
2093bd00f61bSrm160521 	case ZFS_PROP_VERSION:
2094de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2095de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2096de8267e0Stimh 	case ZFS_PROP_CASE:
2097de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2098de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
20993d7934e1Srm160521 			return (-1);
2100bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2101de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2102de8267e0Stimh 			zcmd_free_nvlists(&zc);
2103f4b94bdeSMatthew Ahrens 			return (-1);
2104bd00f61bSrm160521 		}
2105de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2106de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2107de8267e0Stimh 		    val) != 0) {
2108de8267e0Stimh 			zcmd_free_nvlists(&zc);
2109f4b94bdeSMatthew Ahrens 			return (-1);
2110de8267e0Stimh 		}
211196510749Stimh 		nvlist_free(zplprops);
2112de8267e0Stimh 		zcmd_free_nvlists(&zc);
2113bd00f61bSrm160521 		break;
2114bd00f61bSrm160521 
2115ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
2116ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
2117ca48f36fSKeith M Wesolowski 		break;
2118ca48f36fSKeith M Wesolowski 
2119fa9e4066Sahrens 	default:
2120e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
212191ebeef5Sahrens 		case PROP_TYPE_NUMBER:
212291ebeef5Sahrens 		case PROP_TYPE_INDEX:
2123e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
212474e7dc98SMatthew Ahrens 			/*
212514843421SMatthew Ahrens 			 * If we tried to use a default value for a
212674e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
212731f572c2STom Erickson 			 * present.
212874e7dc98SMatthew Ahrens 			 */
212974e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
213031f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
213131f572c2STom Erickson 				*source = NULL;
213274e7dc98SMatthew Ahrens 			}
2133e7437265Sahrens 			break;
2134e7437265Sahrens 
213591ebeef5Sahrens 		case PROP_TYPE_STRING:
2136e7437265Sahrens 		default:
213799653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
213899653d4eSeschrock 			    "cannot get non-numeric property"));
213999653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
214099653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
2141fa9e4066Sahrens 		}
2142e7437265Sahrens 	}
2143fa9e4066Sahrens 
2144fa9e4066Sahrens 	return (0);
2145fa9e4066Sahrens }
2146fa9e4066Sahrens 
2147fa9e4066Sahrens /*
2148fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2149fa9e4066Sahrens  */
2150fa9e4066Sahrens static void
2151990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2152fa9e4066Sahrens     char *statbuf, size_t statlen)
2153fa9e4066Sahrens {
2154990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2155fa9e4066Sahrens 		return;
2156fa9e4066Sahrens 
2157fa9e4066Sahrens 	if (source == NULL) {
2158990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2159fa9e4066Sahrens 	} else if (source[0] == '\0') {
2160990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
216192241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
216292241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
2163fa9e4066Sahrens 	} else {
2164fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2165990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2166fa9e4066Sahrens 		} else {
2167fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2168990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2169fa9e4066Sahrens 		}
2170fa9e4066Sahrens 	}
2171fa9e4066Sahrens 
2172fa9e4066Sahrens }
2173fa9e4066Sahrens 
217492241e0bSTom Erickson int
217592241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
217692241e0bSTom Erickson     size_t proplen, boolean_t literal)
217792241e0bSTom Erickson {
217892241e0bSTom Erickson 	zfs_prop_t prop;
217992241e0bSTom Erickson 	int err = 0;
218092241e0bSTom Erickson 
218192241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
218292241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
218392241e0bSTom Erickson 			return (-1);
218492241e0bSTom Erickson 
218592241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
218692241e0bSTom Erickson 
218792241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
218892241e0bSTom Erickson 		uint64_t cookie;
218992241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
219092241e0bSTom Erickson 			return (-1);
219192241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
219292241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
219392241e0bSTom Erickson 		    NULL, NULL, 0, literal);
219492241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
219592241e0bSTom Erickson 	} else {
219692241e0bSTom Erickson 		nvlist_t *propval;
219792241e0bSTom Erickson 		char *recvdval;
219892241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
219992241e0bSTom Erickson 		    propname, &propval) != 0)
220092241e0bSTom Erickson 			return (-1);
220192241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
220292241e0bSTom Erickson 		    &recvdval) == 0);
220392241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
220492241e0bSTom Erickson 	}
220592241e0bSTom Erickson 
220692241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
220792241e0bSTom Erickson }
220892241e0bSTom Erickson 
220919b94df9SMatthew Ahrens static int
221019b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
221119b94df9SMatthew Ahrens {
221219b94df9SMatthew Ahrens 	nvlist_t *value;
221319b94df9SMatthew Ahrens 	nvpair_t *pair;
221419b94df9SMatthew Ahrens 
221519b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
221619b94df9SMatthew Ahrens 	if (value == NULL)
221719b94df9SMatthew Ahrens 		return (-1);
221819b94df9SMatthew Ahrens 
221919b94df9SMatthew Ahrens 	propbuf[0] = '\0';
222019b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
222119b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
222219b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
222319b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
222419b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
222519b94df9SMatthew Ahrens 	}
222619b94df9SMatthew Ahrens 
222719b94df9SMatthew Ahrens 	return (0);
222819b94df9SMatthew Ahrens }
222919b94df9SMatthew Ahrens 
223019b94df9SMatthew Ahrens struct get_clones_arg {
223119b94df9SMatthew Ahrens 	uint64_t numclones;
223219b94df9SMatthew Ahrens 	nvlist_t *value;
223319b94df9SMatthew Ahrens 	const char *origin;
223440a5c998SMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
223519b94df9SMatthew Ahrens };
223619b94df9SMatthew Ahrens 
223719b94df9SMatthew Ahrens int
223819b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
223919b94df9SMatthew Ahrens {
224019b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
224119b94df9SMatthew Ahrens 
224219b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
224319b94df9SMatthew Ahrens 		zfs_close(zhp);
224419b94df9SMatthew Ahrens 		return (0);
224519b94df9SMatthew Ahrens 	}
224619b94df9SMatthew Ahrens 
224719b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
224819b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
224919b94df9SMatthew Ahrens 		goto out;
225019b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
22513b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
225219b94df9SMatthew Ahrens 		gca->numclones--;
225319b94df9SMatthew Ahrens 	}
225419b94df9SMatthew Ahrens 
225519b94df9SMatthew Ahrens out:
225619b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
225719b94df9SMatthew Ahrens 	zfs_close(zhp);
225819b94df9SMatthew Ahrens 	return (0);
225919b94df9SMatthew Ahrens }
226019b94df9SMatthew Ahrens 
226119b94df9SMatthew Ahrens nvlist_t *
226219b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
226319b94df9SMatthew Ahrens {
226419b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
226519b94df9SMatthew Ahrens 
226619b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
226719b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
226819b94df9SMatthew Ahrens 		struct get_clones_arg gca;
226919b94df9SMatthew Ahrens 
227019b94df9SMatthew Ahrens 		/*
227119b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
227219b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
227319b94df9SMatthew Ahrens 		 */
227419b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
227519b94df9SMatthew Ahrens 			return (NULL);
227619b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
227719b94df9SMatthew Ahrens 			return (NULL);
227819b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
227919b94df9SMatthew Ahrens 			nvlist_free(nv);
228019b94df9SMatthew Ahrens 			return (NULL);
228119b94df9SMatthew Ahrens 		}
228219b94df9SMatthew Ahrens 
228319b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
228419b94df9SMatthew Ahrens 		gca.value = value;
228519b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
228619b94df9SMatthew Ahrens 
228719b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
228819b94df9SMatthew Ahrens 			zfs_handle_t *root;
228940a5c998SMatthew Ahrens 			char pool[ZFS_MAX_DATASET_NAME_LEN];
229019b94df9SMatthew Ahrens 			char *cp = pool;
229119b94df9SMatthew Ahrens 
229219b94df9SMatthew Ahrens 			/* get the pool name */
229319b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
229419b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
229519b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
229619b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
229719b94df9SMatthew Ahrens 
229819b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
229919b94df9SMatthew Ahrens 		}
230019b94df9SMatthew Ahrens 
230119b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
230219b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
230319b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
230419b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
230519b94df9SMatthew Ahrens 			nvlist_free(nv);
230619b94df9SMatthew Ahrens 			nvlist_free(value);
230719b94df9SMatthew Ahrens 			return (NULL);
230819b94df9SMatthew Ahrens 		}
230919b94df9SMatthew Ahrens 		nvlist_free(nv);
231019b94df9SMatthew Ahrens 		nvlist_free(value);
231119b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
231219b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
231319b94df9SMatthew Ahrens 	}
231419b94df9SMatthew Ahrens 
231519b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
231619b94df9SMatthew Ahrens 
231719b94df9SMatthew Ahrens 	return (value);
231819b94df9SMatthew Ahrens }
231919b94df9SMatthew Ahrens 
2320fa9e4066Sahrens /*
2321fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2322fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2323fa9e4066Sahrens  * human-readable form.
2324fa9e4066Sahrens  *
2325fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2326fa9e4066Sahrens  */
2327fa9e4066Sahrens int
2328fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2329990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2330fa9e4066Sahrens {
2331fa9e4066Sahrens 	char *source = NULL;
2332fa9e4066Sahrens 	uint64_t val;
23339c3fd121SMatthew Ahrens 	const char *str;
2334e9dbad6fSeschrock 	const char *strval;
233592241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2336fa9e4066Sahrens 
2337fa9e4066Sahrens 	/*
2338fa9e4066Sahrens 	 * Check to see if this property applies to our object
2339fa9e4066Sahrens 	 */
2340fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2341fa9e4066Sahrens 		return (-1);
2342fa9e4066Sahrens 
234392241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
234492241e0bSTom Erickson 		return (-1);
234592241e0bSTom Erickson 
2346fa9e4066Sahrens 	if (src)
2347990b4856Slling 		*src = ZPROP_SRC_NONE;
2348fa9e4066Sahrens 
2349fa9e4066Sahrens 	switch (prop) {
2350fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2351fa9e4066Sahrens 		/*
2352fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2353fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2354fa9e4066Sahrens 		 */
2355fa9e4066Sahrens 		{
2356a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2357a2eea2e1Sahrens 			time_t time = (time_t)val;
2358fa9e4066Sahrens 			struct tm t;
2359fa9e4066Sahrens 
2360fa9e4066Sahrens 			if (literal ||
2361fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2362fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2363fa9e4066Sahrens 			    &t) == 0)
2364a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2365fa9e4066Sahrens 		}
2366fa9e4066Sahrens 		break;
2367fa9e4066Sahrens 
2368fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2369fa9e4066Sahrens 		/*
2370fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2371fa9e4066Sahrens 		 *
2372fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2373fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2374fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2375fa9e4066Sahrens 		 *
2376fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2377fa9e4066Sahrens 		 * root to any values we return.
2378fa9e4066Sahrens 		 */
237929ab75c9Srm160521 
23807f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2381fa9e4066Sahrens 
2382b21718f0Sgw25295 		if (str[0] == '/') {
238329ab75c9Srm160521 			char buf[MAXPATHLEN];
238429ab75c9Srm160521 			char *root = buf;
2385a79992aaSTom Erickson 			const char *relpath;
2386fa9e4066Sahrens 
2387a79992aaSTom Erickson 			/*
2388a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2389a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2390a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2391a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2392a79992aaSTom Erickson 			 * inherited.
2393a79992aaSTom Erickson 			 */
2394a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2395a79992aaSTom Erickson 				relpath = "";
2396a79992aaSTom Erickson 			} else {
2397a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2398fa9e4066Sahrens 				if (relpath[0] == '/')
2399fa9e4066Sahrens 					relpath++;
2400a79992aaSTom Erickson 			}
2401b21718f0Sgw25295 
240229ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
2403c58b3526SAdam Stevko 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2404c58b3526SAdam Stevko 			    B_FALSE)) || (strcmp(root, "-") == 0))
240529ab75c9Srm160521 				root[0] = '\0';
2406b21718f0Sgw25295 			/*
2407b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2408b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2409b21718f0Sgw25295 			 * mountpoint path.
2410b21718f0Sgw25295 			 */
2411b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2412b21718f0Sgw25295 				root++;
2413b21718f0Sgw25295 
2414b21718f0Sgw25295 			/*
2415b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2416b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2417b21718f0Sgw25295 			 * an inherited mountpoint.
2418b21718f0Sgw25295 			 */
2419b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2420b21718f0Sgw25295 			    relpath[0] != '\0'))
24217f7322feSeschrock 				str++;
2422fa9e4066Sahrens 
2423fa9e4066Sahrens 			if (relpath[0] == '\0')
2424fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
24257f7322feSeschrock 				    root, str);
2426fa9e4066Sahrens 			else
2427fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
24287f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2429fa9e4066Sahrens 				    relpath);
2430fa9e4066Sahrens 		} else {
2431fa9e4066Sahrens 			/* 'legacy' or 'none' */
24327f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2433fa9e4066Sahrens 		}
2434fa9e4066Sahrens 
2435fa9e4066Sahrens 		break;
2436fa9e4066Sahrens 
2437fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
24389c3fd121SMatthew Ahrens 		str = getprop_string(zhp, prop, &source);
24399c3fd121SMatthew Ahrens 		if (str == NULL)
2440fa9e4066Sahrens 			return (-1);
24419c3fd121SMatthew Ahrens 		(void) strlcpy(propbuf, str, proplen);
2442fa9e4066Sahrens 		break;
2443fa9e4066Sahrens 
244419b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
244519b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
244619b94df9SMatthew Ahrens 			return (-1);
244719b94df9SMatthew Ahrens 		break;
244819b94df9SMatthew Ahrens 
2449fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2450a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2451fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2452a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2453a9799022Sck153898 
245499653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
245599653d4eSeschrock 			return (-1);
2456fa9e4066Sahrens 
2457fa9e4066Sahrens 		/*
2458fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2459fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2460fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2461fa9e4066Sahrens 		 * that its set locally.
2462fa9e4066Sahrens 		 */
2463fa9e4066Sahrens 		if (val == 0) {
2464fa9e4066Sahrens 			if (literal)
2465fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2466fa9e4066Sahrens 			else
2467fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2468fa9e4066Sahrens 		} else {
2469fa9e4066Sahrens 			if (literal)
24705ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24715ad82045Snd150628 				    (u_longlong_t)val);
2472fa9e4066Sahrens 			else
2473fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2474fa9e4066Sahrens 		}
2475fa9e4066Sahrens 		break;
2476fa9e4066Sahrens 
2477a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2478a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2479a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2480a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2481a2afb611SJerry Jelinek 
2482a2afb611SJerry Jelinek 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2483a2afb611SJerry Jelinek 			return (-1);
2484a2afb611SJerry Jelinek 
2485a2afb611SJerry Jelinek 		/*
2486a2afb611SJerry Jelinek 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2487a2afb611SJerry Jelinek 		 * literal is set), and indicate that it's the default value.
2488a2afb611SJerry Jelinek 		 * Otherwise, we print the number nicely and indicate that it's
2489a2afb611SJerry Jelinek 		 * set locally.
2490a2afb611SJerry Jelinek 		 */
2491a2afb611SJerry Jelinek 		if (literal) {
2492a2afb611SJerry Jelinek 			(void) snprintf(propbuf, proplen, "%llu",
2493a2afb611SJerry Jelinek 			    (u_longlong_t)val);
2494a2afb611SJerry Jelinek 		} else if (val == UINT64_MAX) {
2495a2afb611SJerry Jelinek 			(void) strlcpy(propbuf, "none", proplen);
2496a2afb611SJerry Jelinek 		} else {
2497a2afb611SJerry Jelinek 			zfs_nicenum(val, propbuf, proplen);
2498a2afb611SJerry Jelinek 		}
2499a2afb611SJerry Jelinek 		break;
2500a2afb611SJerry Jelinek 
2501187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2502fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
250399653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
250499653d4eSeschrock 			return (-1);
2505b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2506b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2507b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2508fa9e4066Sahrens 		break;
2509fa9e4066Sahrens 
2510fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2511fa9e4066Sahrens 		switch (zhp->zfs_type) {
2512fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2513fa9e4066Sahrens 			str = "filesystem";
2514fa9e4066Sahrens 			break;
2515fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2516fa9e4066Sahrens 			str = "volume";
2517fa9e4066Sahrens 			break;
2518fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2519fa9e4066Sahrens 			str = "snapshot";
2520fa9e4066Sahrens 			break;
252178f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
252278f17100SMatthew Ahrens 			str = "bookmark";
252378f17100SMatthew Ahrens 			break;
2524fa9e4066Sahrens 		default:
252599653d4eSeschrock 			abort();
2526fa9e4066Sahrens 		}
2527fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2528fa9e4066Sahrens 		break;
2529fa9e4066Sahrens 
2530fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2531fa9e4066Sahrens 		/*
2532fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2533fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2534fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2535fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2536fa9e4066Sahrens 		 */
253799653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
253899653d4eSeschrock 		    src, &source, &val) != 0)
253999653d4eSeschrock 			return (-1);
254099653d4eSeschrock 		if (val)
2541fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2542fa9e4066Sahrens 		else
2543fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2544fa9e4066Sahrens 		break;
2545fa9e4066Sahrens 
2546fa9e4066Sahrens 	case ZFS_PROP_NAME:
2547fa9e4066Sahrens 		/*
2548fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2549fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2550fa9e4066Sahrens 		 * consumers.
2551fa9e4066Sahrens 		 */
2552fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2553fa9e4066Sahrens 		break;
2554fa9e4066Sahrens 
25554201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
25564201a95eSRic Aleshire 		{
25574201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
25584201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
25594201a95eSRic Aleshire 
25604201a95eSRic Aleshire 			(void) strlcpy(propbuf,
25614201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
25624201a95eSRic Aleshire 
25634201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
25644201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
25654201a95eSRic Aleshire 				break;
25664201a95eSRic Aleshire 
25674201a95eSRic Aleshire 			/*
25684201a95eSRic Aleshire 			 * Try to translate the internal hex string to
25694201a95eSRic Aleshire 			 * human-readable output.  If there are any
25704201a95eSRic Aleshire 			 * problems just use the hex string.
25714201a95eSRic Aleshire 			 */
25724201a95eSRic Aleshire 
25734201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
25744201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
25754201a95eSRic Aleshire 				m_label_free(new_sl);
25764201a95eSRic Aleshire 				break;
25774201a95eSRic Aleshire 			}
25784201a95eSRic Aleshire 
25794201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
25804201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
25814201a95eSRic Aleshire 				if (ascii)
25824201a95eSRic Aleshire 					free(ascii);
25834201a95eSRic Aleshire 				m_label_free(new_sl);
25844201a95eSRic Aleshire 				break;
25854201a95eSRic Aleshire 			}
25864201a95eSRic Aleshire 			m_label_free(new_sl);
25874201a95eSRic Aleshire 
25884201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
25894201a95eSRic Aleshire 			free(ascii);
25904201a95eSRic Aleshire 		}
25914201a95eSRic Aleshire 		break;
25924201a95eSRic Aleshire 
2593f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2594f0f3ef5aSGarrett D'Amore 		/*
2595f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2596f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2597f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2598f0f3ef5aSGarrett D'Amore 		 */
2599f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2600f0f3ef5aSGarrett D'Amore 			return (-1);
2601f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2602f0f3ef5aSGarrett D'Amore 		break;
2603f0f3ef5aSGarrett D'Amore 
2604fa9e4066Sahrens 	default:
2605e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
260691ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2607e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2608e7437265Sahrens 			    &source, &val) != 0)
2609e7437265Sahrens 				return (-1);
2610e7437265Sahrens 			if (literal)
2611e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2612e7437265Sahrens 				    (u_longlong_t)val);
2613e7437265Sahrens 			else
2614e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2615e7437265Sahrens 			break;
2616e7437265Sahrens 
261791ebeef5Sahrens 		case PROP_TYPE_STRING:
26189c3fd121SMatthew Ahrens 			str = getprop_string(zhp, prop, &source);
26199c3fd121SMatthew Ahrens 			if (str == NULL)
26209c3fd121SMatthew Ahrens 				return (-1);
26219c3fd121SMatthew Ahrens 			(void) strlcpy(propbuf, str, proplen);
2622e7437265Sahrens 			break;
2623e7437265Sahrens 
262491ebeef5Sahrens 		case PROP_TYPE_INDEX:
2625fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2626fe192f76Sahrens 			    &source, &val) != 0)
2627fe192f76Sahrens 				return (-1);
2628fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2629e7437265Sahrens 				return (-1);
2630e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2631e7437265Sahrens 			break;
2632e7437265Sahrens 
2633e7437265Sahrens 		default:
263499653d4eSeschrock 			abort();
2635fa9e4066Sahrens 		}
2636e7437265Sahrens 	}
2637fa9e4066Sahrens 
2638fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2639fa9e4066Sahrens 
2640fa9e4066Sahrens 	return (0);
2641fa9e4066Sahrens }
2642fa9e4066Sahrens 
2643fa9e4066Sahrens /*
2644fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2645fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2646fa9e4066Sahrens  * hard-coded property types.
2647fa9e4066Sahrens  */
2648fa9e4066Sahrens uint64_t
2649fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2650fa9e4066Sahrens {
2651fa9e4066Sahrens 	char *source;
265299653d4eSeschrock 	uint64_t val;
2653fa9e4066Sahrens 
26543cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
265599653d4eSeschrock 
265699653d4eSeschrock 	return (val);
2657fa9e4066Sahrens }
2658fa9e4066Sahrens 
26597b97dc1aSrm160521 int
26607b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
26617b97dc1aSrm160521 {
26627b97dc1aSrm160521 	char buf[64];
26637b97dc1aSrm160521 
266414843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
26657b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
26667b97dc1aSrm160521 }
26677b97dc1aSrm160521 
2668fa9e4066Sahrens /*
2669fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2670fa9e4066Sahrens  */
2671fa9e4066Sahrens int
2672fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2673990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2674fa9e4066Sahrens {
2675fa9e4066Sahrens 	char *source;
2676fa9e4066Sahrens 
2677fa9e4066Sahrens 	/*
2678fa9e4066Sahrens 	 * Check to see if this property applies to our object
2679fa9e4066Sahrens 	 */
2680e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2681ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
268299653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
268399653d4eSeschrock 		    zfs_prop_to_name(prop)));
2684e45ce728Sahrens 	}
2685fa9e4066Sahrens 
2686fa9e4066Sahrens 	if (src)
2687990b4856Slling 		*src = ZPROP_SRC_NONE;
2688fa9e4066Sahrens 
268999653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
269099653d4eSeschrock 		return (-1);
2691fa9e4066Sahrens 
2692fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2693fa9e4066Sahrens 
2694fa9e4066Sahrens 	return (0);
2695fa9e4066Sahrens }
2696fa9e4066Sahrens 
269714843421SMatthew Ahrens static int
269814843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
269914843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
270014843421SMatthew Ahrens {
270114843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
270214843421SMatthew Ahrens 	idmap_stat status;
270314843421SMatthew Ahrens 	int err = EINVAL;
270414843421SMatthew Ahrens 
27051fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
270614843421SMatthew Ahrens 		goto out;
270714843421SMatthew Ahrens 
270814843421SMatthew Ahrens 	if (isuser) {
270914843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
271014843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
271114843421SMatthew Ahrens 	} else {
271214843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
271314843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
271414843421SMatthew Ahrens 	}
271514843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
271614843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
271714843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
271814843421SMatthew Ahrens 		err = 0;
271914843421SMatthew Ahrens 	else
272014843421SMatthew Ahrens 		err = EINVAL;
272114843421SMatthew Ahrens out:
272214843421SMatthew Ahrens 	if (get_hdl)
272314843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
272414843421SMatthew Ahrens 	return (err);
272514843421SMatthew Ahrens }
272614843421SMatthew Ahrens 
272714843421SMatthew Ahrens /*
272814843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
272914843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
273014843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
273114843421SMatthew Ahrens  */
273214843421SMatthew Ahrens static int
273314843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
273414843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
273514843421SMatthew Ahrens {
273614843421SMatthew Ahrens 	zfs_userquota_prop_t type;
273714843421SMatthew Ahrens 	char *cp, *end;
27383b12c289SMatthew Ahrens 	char *numericsid = NULL;
273914843421SMatthew Ahrens 	boolean_t isuser;
274014843421SMatthew Ahrens 
274114843421SMatthew Ahrens 	domain[0] = '\0';
27421ed6b69aSGordon Ross 	*ridp = 0;
274314843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
274414843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
274514843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
274614843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
274714843421SMatthew Ahrens 			break;
274814843421SMatthew Ahrens 	}
274914843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
275014843421SMatthew Ahrens 		return (EINVAL);
275114843421SMatthew Ahrens 	*typep = type;
275214843421SMatthew Ahrens 
275314843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
275414843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
275514843421SMatthew Ahrens 
275614843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
275714843421SMatthew Ahrens 
275814843421SMatthew Ahrens 	if (strchr(cp, '@')) {
275914843421SMatthew Ahrens 		/*
276014843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
27613b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
276214843421SMatthew Ahrens 		 */
27631ed6b69aSGordon Ross 		int flag = 0;
27641ed6b69aSGordon Ross 		idmap_stat stat, map_stat;
27651ed6b69aSGordon Ross 		uid_t pid;
27661ed6b69aSGordon Ross 		idmap_rid_t rid;
27671ed6b69aSGordon Ross 		idmap_get_handle_t *gh = NULL;
27681ed6b69aSGordon Ross 
27691ed6b69aSGordon Ross 		stat = idmap_get_create(&gh);
27701ed6b69aSGordon Ross 		if (stat != IDMAP_SUCCESS) {
27711ed6b69aSGordon Ross 			idmap_get_destroy(gh);
27721ed6b69aSGordon Ross 			return (ENOMEM);
27731ed6b69aSGordon Ross 		}
277414843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
277514843421SMatthew Ahrens 			return (ENOENT);
27763b12c289SMatthew Ahrens 		if (isuser) {
27771ed6b69aSGordon Ross 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
27781ed6b69aSGordon Ross 			if (stat < 0)
27791ed6b69aSGordon Ross 				return (ENOENT);
27801ed6b69aSGordon Ross 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
27811ed6b69aSGordon Ross 			    &rid, &map_stat);
27823b12c289SMatthew Ahrens 		} else {
27831ed6b69aSGordon Ross 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
27841ed6b69aSGordon Ross 			if (stat < 0)
27851ed6b69aSGordon Ross 				return (ENOENT);
27861ed6b69aSGordon Ross 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
27871ed6b69aSGordon Ross 			    &rid, &map_stat);
27883b12c289SMatthew Ahrens 		}
27891ed6b69aSGordon Ross 		if (stat < 0) {
27901ed6b69aSGordon Ross 			idmap_get_destroy(gh);
27911ed6b69aSGordon Ross 			return (ENOENT);
27921ed6b69aSGordon Ross 		}
27931ed6b69aSGordon Ross 		stat = idmap_get_mappings(gh);
27941ed6b69aSGordon Ross 		idmap_get_destroy(gh);
27951ed6b69aSGordon Ross 
27961ed6b69aSGordon Ross 		if (stat < 0) {
279714843421SMatthew Ahrens 			return (ENOENT);
27983b12c289SMatthew Ahrens 		}
27993b12c289SMatthew Ahrens 		if (numericsid == NULL)
280014843421SMatthew Ahrens 			return (ENOENT);
28013b12c289SMatthew Ahrens 		cp = numericsid;
28021ed6b69aSGordon Ross 		*ridp = rid;
28033b12c289SMatthew Ahrens 		/* will be further decoded below */
28043b12c289SMatthew Ahrens 	}
28053b12c289SMatthew Ahrens 
28063b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
280714843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
28083b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
28091ed6b69aSGordon Ross 		errno = 0;
28101ed6b69aSGordon Ross 		if (*ridp == 0) {
281114843421SMatthew Ahrens 			cp = strrchr(domain, '-');
281214843421SMatthew Ahrens 			*cp = '\0';
281314843421SMatthew Ahrens 			cp++;
281414843421SMatthew Ahrens 			*ridp = strtoull(cp, &end, 10);
28151ed6b69aSGordon Ross 		} else {
28161ed6b69aSGordon Ross 			end = "";
28171ed6b69aSGordon Ross 		}
28183b12c289SMatthew Ahrens 		if (numericsid) {
28193b12c289SMatthew Ahrens 			free(numericsid);
28203b12c289SMatthew Ahrens 			numericsid = NULL;
28213b12c289SMatthew Ahrens 		}
2822777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
282314843421SMatthew Ahrens 			return (EINVAL);
282414843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
282514843421SMatthew Ahrens 		/*
282614843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
282714843421SMatthew Ahrens 		 * turned into a uid/gid
282814843421SMatthew Ahrens 		 */
282914843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
283014843421SMatthew Ahrens 			return (ENOENT);
283114843421SMatthew Ahrens 		if (isuser) {
283214843421SMatthew Ahrens 			struct passwd *pw;
283314843421SMatthew Ahrens 			pw = getpwnam(cp);
283414843421SMatthew Ahrens 			if (pw == NULL)
283514843421SMatthew Ahrens 				return (ENOENT);
283614843421SMatthew Ahrens 			*ridp = pw->pw_uid;
283714843421SMatthew Ahrens 		} else {
283814843421SMatthew Ahrens 			struct group *gr;
283914843421SMatthew Ahrens 			gr = getgrnam(cp);
284014843421SMatthew Ahrens 			if (gr == NULL)
284114843421SMatthew Ahrens 				return (ENOENT);
284214843421SMatthew Ahrens 			*ridp = gr->gr_gid;
284314843421SMatthew Ahrens 		}
284414843421SMatthew Ahrens 	} else {
284514843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
284614843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
284714843421SMatthew Ahrens 		idmap_rid_t rid;
284814843421SMatthew Ahrens 		char *mapdomain;
284914843421SMatthew Ahrens 
285014843421SMatthew Ahrens 		if (*end != '\0')
285114843421SMatthew Ahrens 			return (EINVAL);
285214843421SMatthew Ahrens 		if (id > MAXUID) {
285314843421SMatthew Ahrens 			/* It's an ephemeral ID. */
285414843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
285514843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
285614843421SMatthew Ahrens 				return (ENOENT);
28573b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
285814843421SMatthew Ahrens 			*ridp = rid;
285914843421SMatthew Ahrens 		} else {
286014843421SMatthew Ahrens 			*ridp = id;
286114843421SMatthew Ahrens 		}
286214843421SMatthew Ahrens 	}
286314843421SMatthew Ahrens 
28643b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
286514843421SMatthew Ahrens 	return (0);
286614843421SMatthew Ahrens }
286714843421SMatthew Ahrens 
2868edea4b55SLin Ling static int
2869edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2870edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
287114843421SMatthew Ahrens {
287214843421SMatthew Ahrens 	int err;
287314843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
287414843421SMatthew Ahrens 
287519b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
287614843421SMatthew Ahrens 
287714843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
287814843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2879edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2880edea4b55SLin Ling 	zc.zc_objset_type = *typep;
288114843421SMatthew Ahrens 	if (err)
288214843421SMatthew Ahrens 		return (err);
288314843421SMatthew Ahrens 
288414843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
288514843421SMatthew Ahrens 	if (err)
288614843421SMatthew Ahrens 		return (err);
288714843421SMatthew Ahrens 
2888edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2889edea4b55SLin Ling 	return (0);
2890edea4b55SLin Ling }
2891edea4b55SLin Ling 
2892edea4b55SLin Ling int
2893edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2894edea4b55SLin Ling     uint64_t *propvalue)
2895edea4b55SLin Ling {
2896edea4b55SLin Ling 	zfs_userquota_prop_t type;
2897edea4b55SLin Ling 
2898edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2899edea4b55SLin Ling 	    &type));
2900edea4b55SLin Ling }
2901edea4b55SLin Ling 
2902edea4b55SLin Ling int
2903edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2904edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2905edea4b55SLin Ling {
2906edea4b55SLin Ling 	int err;
2907edea4b55SLin Ling 	uint64_t propvalue;
2908edea4b55SLin Ling 	zfs_userquota_prop_t type;
2909edea4b55SLin Ling 
2910edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2911edea4b55SLin Ling 	    &type);
2912edea4b55SLin Ling 
2913edea4b55SLin Ling 	if (err)
2914edea4b55SLin Ling 		return (err);
2915edea4b55SLin Ling 
291614843421SMatthew Ahrens 	if (literal) {
2917edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2918edea4b55SLin Ling 	} else if (propvalue == 0 &&
291914843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
292014843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
292114843421SMatthew Ahrens 	} else {
2922edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
292314843421SMatthew Ahrens 	}
292414843421SMatthew Ahrens 	return (0);
292514843421SMatthew Ahrens }
292614843421SMatthew Ahrens 
292719b94df9SMatthew Ahrens int
292819b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
292919b94df9SMatthew Ahrens     uint64_t *propvalue)
293019b94df9SMatthew Ahrens {
293119b94df9SMatthew Ahrens 	int err;
293219b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
293319b94df9SMatthew Ahrens 	const char *snapname;
293419b94df9SMatthew Ahrens 
293519b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
293619b94df9SMatthew Ahrens 
293719b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
293819b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
293919b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
294019b94df9SMatthew Ahrens 	} else {
294119b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
294219b94df9SMatthew Ahrens 		char *cp;
294319b94df9SMatthew Ahrens 
294419b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
294519b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
294619b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
294719b94df9SMatthew Ahrens 		if (cp != NULL)
294819b94df9SMatthew Ahrens 			*cp = '\0';
294919b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
295019b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
295119b94df9SMatthew Ahrens 	}
295219b94df9SMatthew Ahrens 
295319b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
295419b94df9SMatthew Ahrens 	if (err)
295519b94df9SMatthew Ahrens 		return (err);
295619b94df9SMatthew Ahrens 
295719b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
295819b94df9SMatthew Ahrens 	return (0);
295919b94df9SMatthew Ahrens }
296019b94df9SMatthew Ahrens 
296119b94df9SMatthew Ahrens int
296219b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
296319b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
296419b94df9SMatthew Ahrens {
296519b94df9SMatthew Ahrens 	int err;
296619b94df9SMatthew Ahrens 	uint64_t propvalue;
296719b94df9SMatthew Ahrens 
296819b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
296919b94df9SMatthew Ahrens 
297019b94df9SMatthew Ahrens 	if (err)
297119b94df9SMatthew Ahrens 		return (err);
297219b94df9SMatthew Ahrens 
297319b94df9SMatthew Ahrens 	if (literal) {
297419b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
297519b94df9SMatthew Ahrens 	} else {
297619b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
297719b94df9SMatthew Ahrens 	}
297819b94df9SMatthew Ahrens 	return (0);
297919b94df9SMatthew Ahrens }
298019b94df9SMatthew Ahrens 
2981fa9e4066Sahrens /*
2982fa9e4066Sahrens  * Returns the name of the given zfs handle.
2983fa9e4066Sahrens  */
2984fa9e4066Sahrens const char *
2985fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
2986fa9e4066Sahrens {
2987fa9e4066Sahrens 	return (zhp->zfs_name);
2988fa9e4066Sahrens }
2989fa9e4066Sahrens 
2990fa9e4066Sahrens /*
29918808ac5dSYuri Pankov  * Returns the name of the parent pool for the given zfs handle.
29928808ac5dSYuri Pankov  */
29938808ac5dSYuri Pankov const char *
29948808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp)
29958808ac5dSYuri Pankov {
29968808ac5dSYuri Pankov 	return (zhp->zpool_hdl->zpool_name);
29978808ac5dSYuri Pankov }
29988808ac5dSYuri Pankov 
29998808ac5dSYuri Pankov /*
3000fa9e4066Sahrens  * Returns the type of the given zfs handle.
3001fa9e4066Sahrens  */
3002fa9e4066Sahrens zfs_type_t
3003fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
3004fa9e4066Sahrens {
3005fa9e4066Sahrens 	return (zhp->zfs_type);
3006fa9e4066Sahrens }
3007fa9e4066Sahrens 
30087f7322feSeschrock /*
3009d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
3010d41c4376SMark J Musante  *
3011d41c4376SMark J Musante  * Needs to handle these cases:
3012d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3013d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3014d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
3015d41c4376SMark J Musante  */
3016d41c4376SMark J Musante static boolean_t
3017d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
3018d41c4376SMark J Musante {
3019d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
3020d41c4376SMark J Musante 
3021d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
3022d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
3023d41c4376SMark J Musante 		return (B_FALSE);
3024d41c4376SMark J Musante 
3025d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
3026d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3027d41c4376SMark J Musante }
3028d41c4376SMark J Musante 
3029d41c4376SMark J Musante /*
3030fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
303119b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
303219b94df9SMatthew Ahrens  * pool).
3033fa9e4066Sahrens  */
3034fa9e4066Sahrens static int
3035fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3036fa9e4066Sahrens {
303719b94df9SMatthew Ahrens 	char *slashp;
3038fa9e4066Sahrens 
303919b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
304019b94df9SMatthew Ahrens 
304119b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
3042fa9e4066Sahrens 		return (-1);
304319b94df9SMatthew Ahrens 	*slashp = '\0';
3044fa9e4066Sahrens 
3045fa9e4066Sahrens 	return (0);
3046fa9e4066Sahrens }
3047fa9e4066Sahrens 
3048fa9e4066Sahrens /*
30497f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
30507f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
30517f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
30527f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
30537f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
30547f1f55eaSvb160487  * new datasets.
3055fa9e4066Sahrens  */
3056fa9e4066Sahrens static int
30577f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
30587f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
3059fa9e4066Sahrens {
3060fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
306140a5c998SMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3062fa9e4066Sahrens 	char *slash;
30637f7322feSeschrock 	zfs_handle_t *zhp;
306499653d4eSeschrock 	char errbuf[1024];
3065d41c4376SMark J Musante 	uint64_t is_zoned;
306699653d4eSeschrock 
3067deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
3068deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3069fa9e4066Sahrens 
3070fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
3071fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
307299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
307399653d4eSeschrock 		    "missing dataset name"));
307499653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3075fa9e4066Sahrens 	}
3076fa9e4066Sahrens 
3077fa9e4066Sahrens 	/* check to see if the pool exists */
3078fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
3079fa9e4066Sahrens 		slash = parent + strlen(parent);
30808ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
3081fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
308299653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3083fa9e4066Sahrens 	    errno == ENOENT) {
308499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
308599653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
308699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3087fa9e4066Sahrens 	}
3088fa9e4066Sahrens 
3089fa9e4066Sahrens 	/* check to see if the parent dataset exists */
30907f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
30917f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
30927f1f55eaSvb160487 			/*
30937f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
30947f1f55eaSvb160487 			 */
30957f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
30967f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30977f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
30987f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
30997f1f55eaSvb160487 			}
31007f1f55eaSvb160487 		} else if (errno == ENOENT) {
310199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
310299653d4eSeschrock 			    "parent does not exist"));
310399653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
31047f1f55eaSvb160487 		} else
310599653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3106fa9e4066Sahrens 	}
3107fa9e4066Sahrens 
3108d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3109d41c4376SMark J Musante 	if (zoned != NULL)
3110d41c4376SMark J Musante 		*zoned = is_zoned;
3111d41c4376SMark J Musante 
3112fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
3113d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
311499653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
31157f7322feSeschrock 		zfs_close(zhp);
3116fa9e4066Sahrens 		return (-1);
3117fa9e4066Sahrens 	}
3118fa9e4066Sahrens 
3119fa9e4066Sahrens 	/* make sure parent is a filesystem */
31207f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
312199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
312299653d4eSeschrock 		    "parent is not a filesystem"));
312399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
31247f7322feSeschrock 		zfs_close(zhp);
3125fa9e4066Sahrens 		return (-1);
3126fa9e4066Sahrens 	}
3127fa9e4066Sahrens 
31287f7322feSeschrock 	zfs_close(zhp);
31297f1f55eaSvb160487 	if (prefixlen != NULL)
31307f1f55eaSvb160487 		*prefixlen = strlen(parent);
31317f1f55eaSvb160487 	return (0);
31327f1f55eaSvb160487 }
31337f1f55eaSvb160487 
31347f1f55eaSvb160487 /*
31357f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
31367f1f55eaSvb160487  */
31377f1f55eaSvb160487 boolean_t
31387f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
31397f1f55eaSvb160487 {
31407f1f55eaSvb160487 	zfs_handle_t *zhp;
31417f1f55eaSvb160487 
3142f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
31437f1f55eaSvb160487 		return (B_FALSE);
31447f1f55eaSvb160487 
31457f1f55eaSvb160487 	/*
31467f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
31477f1f55eaSvb160487 	 */
31487f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
31497f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
31507f1f55eaSvb160487 
31517f1f55eaSvb160487 		zfs_close(zhp);
31527f1f55eaSvb160487 		if (types & ds_type)
31537f1f55eaSvb160487 			return (B_TRUE);
31547f1f55eaSvb160487 	}
31557f1f55eaSvb160487 	return (B_FALSE);
31567f1f55eaSvb160487 }
31577f1f55eaSvb160487 
31587f1f55eaSvb160487 /*
31593cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
31603cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
31613cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
31623cb34c60Sahrens  */
31633cb34c60Sahrens int
31643cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
31653cb34c60Sahrens {
31663cb34c60Sahrens 	zfs_handle_t *h;
31673cb34c60Sahrens 	char *cp;
31683cb34c60Sahrens 	const char *opname;
31693cb34c60Sahrens 
31703cb34c60Sahrens 	/* make sure prefix exists */
31713cb34c60Sahrens 	cp = target + prefixlen;
31723cb34c60Sahrens 	if (*cp != '/') {
31733cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
31743cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31753cb34c60Sahrens 	} else {
31763cb34c60Sahrens 		*cp = '\0';
31773cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31783cb34c60Sahrens 		*cp = '/';
31793cb34c60Sahrens 	}
31803cb34c60Sahrens 	if (h == NULL)
31813cb34c60Sahrens 		return (-1);
31823cb34c60Sahrens 	zfs_close(h);
31833cb34c60Sahrens 
31843cb34c60Sahrens 	/*
31853cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
31863cb34c60Sahrens 	 * up to the prefixlen-long one.
31873cb34c60Sahrens 	 */
31883cb34c60Sahrens 	for (cp = target + prefixlen + 1;
31893cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
31903cb34c60Sahrens 
31913cb34c60Sahrens 		*cp = '\0';
31923cb34c60Sahrens 
31933cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
31943cb34c60Sahrens 		if (h) {
31953cb34c60Sahrens 			/* it already exists, nothing to do here */
31963cb34c60Sahrens 			zfs_close(h);
31973cb34c60Sahrens 			continue;
31983cb34c60Sahrens 		}
31993cb34c60Sahrens 
32003cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
32013cb34c60Sahrens 		    NULL) != 0) {
32023cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
32033cb34c60Sahrens 			goto ancestorerr;
32043cb34c60Sahrens 		}
32053cb34c60Sahrens 
32063cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
32073cb34c60Sahrens 		if (h == NULL) {
32083cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
32093cb34c60Sahrens 			goto ancestorerr;
32103cb34c60Sahrens 		}
32113cb34c60Sahrens 
32123cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
32133cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
32143cb34c60Sahrens 			goto ancestorerr;
32153cb34c60Sahrens 		}
32163cb34c60Sahrens 
32173cb34c60Sahrens 		if (zfs_share(h) != 0) {
32183cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
32193cb34c60Sahrens 			goto ancestorerr;
32203cb34c60Sahrens 		}
32213cb34c60Sahrens 
32223cb34c60Sahrens 		zfs_close(h);
32233cb34c60Sahrens 	}
32243cb34c60Sahrens 
32253cb34c60Sahrens 	return (0);
32263cb34c60Sahrens 
32273cb34c60Sahrens ancestorerr:
32283cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32293cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
32303cb34c60Sahrens 	return (-1);
32313cb34c60Sahrens }
32323cb34c60Sahrens 
32333cb34c60Sahrens /*
32347f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
32357f1f55eaSvb160487  */
32367f1f55eaSvb160487 int
32377f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
32387f1f55eaSvb160487 {
32397f1f55eaSvb160487 	int prefix;
32407f1f55eaSvb160487 	char *path_copy;
32417f1f55eaSvb160487 	int rc;
32427f1f55eaSvb160487 
3243d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
32447f1f55eaSvb160487 		return (-1);
32457f1f55eaSvb160487 
32467f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
32477f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
32487f1f55eaSvb160487 		free(path_copy);
32497f1f55eaSvb160487 	}
32507f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
32517f1f55eaSvb160487 		return (-1);
32527f1f55eaSvb160487 
3253fa9e4066Sahrens 	return (0);
3254fa9e4066Sahrens }
3255fa9e4066Sahrens 
3256fa9e4066Sahrens /*
3257e9dbad6fSeschrock  * Create a new filesystem or volume.
3258fa9e4066Sahrens  */
3259fa9e4066Sahrens int
326099653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3261e9dbad6fSeschrock     nvlist_t *props)
3262fa9e4066Sahrens {
3263fa9e4066Sahrens 	int ret;
3264fa9e4066Sahrens 	uint64_t size = 0;
3265fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
326699653d4eSeschrock 	char errbuf[1024];
3267e9dbad6fSeschrock 	uint64_t zoned;
32684445fffbSMatthew Ahrens 	dmu_objset_type_t ost;
326999653d4eSeschrock 
327099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
327199653d4eSeschrock 	    "cannot create '%s'"), path);
3272fa9e4066Sahrens 
3273fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3274f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
327599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3276fa9e4066Sahrens 
3277fa9e4066Sahrens 	/* validate parents exist */
32787f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3279fa9e4066Sahrens 		return (-1);
3280fa9e4066Sahrens 
3281fa9e4066Sahrens 	/*
3282fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3283fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3284fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3285fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3286fa9e4066Sahrens 	 * first try to see if the dataset exists.
3287fa9e4066Sahrens 	 */
32884445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
328999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
329099653d4eSeschrock 		    "dataset already exists"));
329199653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3292fa9e4066Sahrens 	}
3293fa9e4066Sahrens 
3294fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
32954445fffbSMatthew Ahrens 		ost = DMU_OST_ZVOL;
3296fa9e4066Sahrens 	else
32974445fffbSMatthew Ahrens 		ost = DMU_OST_ZFS;
3298fa9e4066Sahrens 
3299e9316f76SJoe Stein 	/* open zpool handle for prop validation */
3300e9316f76SJoe Stein 	char pool_path[MAXNAMELEN];
3301e9316f76SJoe Stein 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3302e9316f76SJoe Stein 
3303e9316f76SJoe Stein 	/* truncate pool_path at first slash */
3304e9316f76SJoe Stein 	char *p = strchr(pool_path, '/');
3305e9316f76SJoe Stein 	if (p != NULL)
3306e9316f76SJoe Stein 		*p = '\0';
3307e9316f76SJoe Stein 
3308e9316f76SJoe Stein 	zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3309e9316f76SJoe Stein 
33100a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3311e9316f76SJoe Stein 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3312e9316f76SJoe Stein 		zpool_close(zpool_handle);
3313e9dbad6fSeschrock 		return (-1);
3314e9316f76SJoe Stein 	}
3315e9316f76SJoe Stein 	zpool_close(zpool_handle);
3316e9dbad6fSeschrock 
3317fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
33185c5460e9Seschrock 		/*
33195c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
33205c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
33215c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
33225c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
33235c5460e9Seschrock 		 * zero.
33245c5460e9Seschrock 		 */
3325e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3326e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3327e9dbad6fSeschrock 			nvlist_free(props);
332899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3329e9dbad6fSeschrock 			    "missing volume size"));
3330e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3331fa9e4066Sahrens 		}
3332fa9e4066Sahrens 
3333e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3334e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3335e9dbad6fSeschrock 		    &blocksize)) != 0) {
3336e9dbad6fSeschrock 			if (ret == ENOENT) {
3337e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3338e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3339e9dbad6fSeschrock 			} else {
3340e9dbad6fSeschrock 				nvlist_free(props);
334199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3342e9dbad6fSeschrock 				    "missing volume block size"));
3343e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3344e9dbad6fSeschrock 			}
3345e9dbad6fSeschrock 		}
3346e9dbad6fSeschrock 
3347e9dbad6fSeschrock 		if (size == 0) {
3348e9dbad6fSeschrock 			nvlist_free(props);
3349e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3350e9dbad6fSeschrock 			    "volume size cannot be zero"));
3351e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
33525c5460e9Seschrock 		}
33535c5460e9Seschrock 
33545c5460e9Seschrock 		if (size % blocksize != 0) {
3355e9dbad6fSeschrock 			nvlist_free(props);
335699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3357e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3358e9dbad6fSeschrock 			    "size"));
3359e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3360e9dbad6fSeschrock 		}
33615c5460e9Seschrock 	}
33625c5460e9Seschrock 
3363fa9e4066Sahrens 	/* create the dataset */
33644445fffbSMatthew Ahrens 	ret = lzc_create(path, ost, props);
33654445fffbSMatthew Ahrens 	nvlist_free(props);
3366e9dbad6fSeschrock 
3367fa9e4066Sahrens 	/* check for failure */
3368fa9e4066Sahrens 	if (ret != 0) {
336940a5c998SMatthew Ahrens 		char parent[ZFS_MAX_DATASET_NAME_LEN];
3370fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3371fa9e4066Sahrens 
3372fa9e4066Sahrens 		switch (errno) {
3373fa9e4066Sahrens 		case ENOENT:
337499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
337599653d4eSeschrock 			    "no such parent '%s'"), parent);
337699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3377fa9e4066Sahrens 
3378fa9e4066Sahrens 		case EINVAL:
337999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3380d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
338199653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3382fa9e4066Sahrens 
338340feaa91Sahrens 		case ENOTSUP:
338440feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
338540feaa91Sahrens 			    "pool must be upgraded to set this "
338640feaa91Sahrens 			    "property or value"));
338740feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3388fa9e4066Sahrens #ifdef _ILP32
3389fa9e4066Sahrens 		case EOVERFLOW:
3390fa9e4066Sahrens 			/*
3391fa9e4066Sahrens 			 * This platform can't address a volume this big.
3392fa9e4066Sahrens 			 */
339399653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
339499653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
339599653d4eSeschrock 				    errbuf));
3396fa9e4066Sahrens #endif
339799653d4eSeschrock 			/* FALLTHROUGH */
3398fa9e4066Sahrens 		default:
339999653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3400fa9e4066Sahrens 		}
3401fa9e4066Sahrens 	}
3402fa9e4066Sahrens 
3403fa9e4066Sahrens 	return (0);
3404fa9e4066Sahrens }
3405fa9e4066Sahrens 
3406fa9e4066Sahrens /*
3407fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
340865fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
340965fec9f6SChristopher Siden  * does not exist this function does nothing.
3410fa9e4066Sahrens  */
3411fa9e4066Sahrens int
3412842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3413fa9e4066Sahrens {
3414fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3415fa9e4066Sahrens 
341678f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
341778f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
341878f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
341978f17100SMatthew Ahrens 		int error = lzc_destroy_bookmarks(nv, NULL);
342078f17100SMatthew Ahrens 		fnvlist_free(nv);
342178f17100SMatthew Ahrens 		if (error != 0) {
342278f17100SMatthew Ahrens 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
342378f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
342478f17100SMatthew Ahrens 			    zhp->zfs_name));
342578f17100SMatthew Ahrens 		}
342678f17100SMatthew Ahrens 		return (0);
342778f17100SMatthew Ahrens 	}
342878f17100SMatthew Ahrens 
3429fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3430fa9e4066Sahrens 
3431e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3432fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3433fa9e4066Sahrens 	} else {
3434fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3435fa9e4066Sahrens 	}
3436fa9e4066Sahrens 
3437842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
343865fec9f6SChristopher Siden 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
343965fec9f6SChristopher Siden 	    errno != ENOENT) {
3440ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
344199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
344299653d4eSeschrock 		    zhp->zfs_name));
34431d452cf5Sahrens 	}
3444fa9e4066Sahrens 
3445fa9e4066Sahrens 	remove_mountpoint(zhp);
3446fa9e4066Sahrens 
3447fa9e4066Sahrens 	return (0);
3448fa9e4066Sahrens }
3449fa9e4066Sahrens 
34501d452cf5Sahrens struct destroydata {
345119b94df9SMatthew Ahrens 	nvlist_t *nvl;
345219b94df9SMatthew Ahrens 	const char *snapname;
34531d452cf5Sahrens };
34541d452cf5Sahrens 
34551d452cf5Sahrens static int
3456681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
34571d452cf5Sahrens {
34581d452cf5Sahrens 	struct destroydata *dd = arg;
345940a5c998SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3460681d9761SEric Taylor 	int rv = 0;
34611d452cf5Sahrens 
346219b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
346319b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
34641d452cf5Sahrens 
3465a7a845e4SSteven Hartland 	if (lzc_exists(name))
346619b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
34671d452cf5Sahrens 
346819b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
34693ccfa83cSahrens 	zfs_close(zhp);
34703ccfa83cSahrens 	return (rv);
34711d452cf5Sahrens }
34721d452cf5Sahrens 
34731d452cf5Sahrens /*
34741d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
34751d452cf5Sahrens  */
34761d452cf5Sahrens int
3477842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
34781d452cf5Sahrens {
34791d452cf5Sahrens 	int ret;
34801d452cf5Sahrens 	struct destroydata dd = { 0 };
34811d452cf5Sahrens 
34821d452cf5Sahrens 	dd.snapname = snapname;
348319b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
348419b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
34851d452cf5Sahrens 
3486a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
348719b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
34881d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
348919b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
349019b94df9SMatthew Ahrens 	} else {
34913b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
349219b94df9SMatthew Ahrens 	}
349319b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
349419b94df9SMatthew Ahrens 	return (ret);
3495e5351341SMatthew Ahrens }
3496e5351341SMatthew Ahrens 
349719b94df9SMatthew Ahrens /*
34983b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
349919b94df9SMatthew Ahrens  */
350019b94df9SMatthew Ahrens int
35013b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
350219b94df9SMatthew Ahrens {
350319b94df9SMatthew Ahrens 	int ret;
35044445fffbSMatthew Ahrens 	nvlist_t *errlist;
350519b94df9SMatthew Ahrens 
35064445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
35071d452cf5Sahrens 
35083b2aab18SMatthew Ahrens 	if (ret == 0)
35093b2aab18SMatthew Ahrens 		return (0);
35103b2aab18SMatthew Ahrens 
3511a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
35123b2aab18SMatthew Ahrens 		char errbuf[1024];
35133b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
35143b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
35153b2aab18SMatthew Ahrens 
35163b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
35173b2aab18SMatthew Ahrens 	}
35184445fffbSMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
35194445fffbSMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
35201d452cf5Sahrens 		char errbuf[1024];
35214445fffbSMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
35224445fffbSMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
35234445fffbSMatthew Ahrens 		    nvpair_name(pair));
35241d452cf5Sahrens 
35254445fffbSMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
35261d452cf5Sahrens 		case EEXIST:
35273b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
35283b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
35293b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
35304445fffbSMatthew Ahrens 			break;
35311d452cf5Sahrens 		default:
35323b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
35334445fffbSMatthew Ahrens 			break;
35344445fffbSMatthew Ahrens 		}
35351d452cf5Sahrens 	}
35361d452cf5Sahrens 
353764512aa9SJan Schlien 	nvlist_free(errlist);
35384445fffbSMatthew Ahrens 	return (ret);
35391d452cf5Sahrens }
35401d452cf5Sahrens 
3541fa9e4066Sahrens /*
3542fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3543fa9e4066Sahrens  */
3544fa9e4066Sahrens int
3545e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3546fa9e4066Sahrens {
354740a5c998SMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3548fa9e4066Sahrens 	int ret;
354999653d4eSeschrock 	char errbuf[1024];
355099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3551e9dbad6fSeschrock 	uint64_t zoned;
3552fa9e4066Sahrens 
3553fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3554fa9e4066Sahrens 
355599653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
355699653d4eSeschrock 	    "cannot create '%s'"), target);
355799653d4eSeschrock 
355819b94df9SMatthew Ahrens 	/* validate the target/clone name */
3559f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
356099653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3561fa9e4066Sahrens 
3562fa9e4066Sahrens 	/* validate parents exist */
35637f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3564fa9e4066Sahrens 		return (-1);
3565fa9e4066Sahrens 
3566fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3567fa9e4066Sahrens 
3568fa9e4066Sahrens 	/* do the clone */
3569e9dbad6fSeschrock 
3570e9dbad6fSeschrock 	if (props) {
35714445fffbSMatthew Ahrens 		zfs_type_t type;
35724445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
35734445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
35744445fffbSMatthew Ahrens 		} else {
35754445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
35764445fffbSMatthew Ahrens 		}
35770a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3578e9316f76SJoe Stein 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3579e9dbad6fSeschrock 			return (-1);
3580e9dbad6fSeschrock 	}
3581e9dbad6fSeschrock 
35824445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
3583e9dbad6fSeschrock 	nvlist_free(props);
3584e9dbad6fSeschrock 
3585fa9e4066Sahrens 	if (ret != 0) {
3586fa9e4066Sahrens 		switch (errno) {
3587fa9e4066Sahrens 
3588fa9e4066Sahrens 		case ENOENT:
3589fa9e4066Sahrens 			/*
3590fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3591fa9e4066Sahrens 			 * above, but there may a race condition that has since
3592fa9e4066Sahrens 			 * destroyed the parent.
3593fa9e4066Sahrens 			 *
3594fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3595fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3596fa9e4066Sahrens 			 * dataset doesn't exist.
3597fa9e4066Sahrens 			 */
359899653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
359999653d4eSeschrock 			    "no such parent '%s'"), parent);
360099653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3601fa9e4066Sahrens 
360299653d4eSeschrock 		case EXDEV:
360399653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
360499653d4eSeschrock 			    "source and target pools differ"));
360599653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
360699653d4eSeschrock 			    errbuf));
360799653d4eSeschrock 
360899653d4eSeschrock 		default:
360999653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
361099653d4eSeschrock 			    errbuf));
361199653d4eSeschrock 		}
361299653d4eSeschrock 	}
361399653d4eSeschrock 
361499653d4eSeschrock 	return (ret);
361599653d4eSeschrock }
361699653d4eSeschrock 
361799653d4eSeschrock /*
361899653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
361999653d4eSeschrock  */
362099653d4eSeschrock int
362199653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
362299653d4eSeschrock {
362399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
362499653d4eSeschrock 	zfs_cmd_t zc = { 0 };
362599653d4eSeschrock 	char parent[MAXPATHLEN];
362699653d4eSeschrock 	int ret;
362799653d4eSeschrock 	char errbuf[1024];
362899653d4eSeschrock 
362999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
363099653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
363199653d4eSeschrock 
363299653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
363399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
363499653d4eSeschrock 		    "snapshots can not be promoted"));
363599653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
363699653d4eSeschrock 	}
363799653d4eSeschrock 
36383cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
363999653d4eSeschrock 	if (parent[0] == '\0') {
364099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
364199653d4eSeschrock 		    "not a cloned filesystem"));
364299653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
364399653d4eSeschrock 	}
364499653d4eSeschrock 
36453cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3646e9dbad6fSeschrock 	    sizeof (zc.zc_value));
364799653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3648ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
364999653d4eSeschrock 
365099653d4eSeschrock 	if (ret != 0) {
36510b69c2f0Sahrens 		int save_errno = errno;
3652fa9e4066Sahrens 
36530b69c2f0Sahrens 		switch (save_errno) {
3654fa9e4066Sahrens 		case EEXIST:
3655681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
365699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3657681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3658681d9761SEric Taylor 			    zc.zc_string, parent);
365999653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3660fa9e4066Sahrens 
3661fa9e4066Sahrens 		default:
36620b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3663fa9e4066Sahrens 		}
3664fa9e4066Sahrens 	}
3665e9dbad6fSeschrock 	return (ret);
36661d452cf5Sahrens }
36671d452cf5Sahrens 
36684445fffbSMatthew Ahrens typedef struct snapdata {
36694445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
36704445fffbSMatthew Ahrens 	const char *sd_snapname;
36714445fffbSMatthew Ahrens } snapdata_t;
36724445fffbSMatthew Ahrens 
36734445fffbSMatthew Ahrens static int
36744445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
36754445fffbSMatthew Ahrens {
36764445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
367740a5c998SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
36784445fffbSMatthew Ahrens 	int rv = 0;
36794445fffbSMatthew Ahrens 
3680ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
36814445fffbSMatthew Ahrens 		(void) snprintf(name, sizeof (name),
36824445fffbSMatthew Ahrens 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
36834445fffbSMatthew Ahrens 
36844445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd->sd_nvl, name);
36854445fffbSMatthew Ahrens 
36864445fffbSMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3687ca48f36fSKeith M Wesolowski 	}
36884445fffbSMatthew Ahrens 	zfs_close(zhp);
3689ca48f36fSKeith M Wesolowski 
36904445fffbSMatthew Ahrens 	return (rv);
36914445fffbSMatthew Ahrens }
36924445fffbSMatthew Ahrens 
3693fa9e4066Sahrens /*
36944445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
36954445fffbSMatthew Ahrens  * created.
3696fa9e4066Sahrens  */
3697fa9e4066Sahrens int
36984445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
36994445fffbSMatthew Ahrens {
37004445fffbSMatthew Ahrens 	int ret;
37014445fffbSMatthew Ahrens 	char errbuf[1024];
37024445fffbSMatthew Ahrens 	nvpair_t *elem;
37034445fffbSMatthew Ahrens 	nvlist_t *errors;
37044445fffbSMatthew Ahrens 
37054445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37064445fffbSMatthew Ahrens 	    "cannot create snapshots "));
37074445fffbSMatthew Ahrens 
37084445fffbSMatthew Ahrens 	elem = NULL;
37094445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
37104445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
37114445fffbSMatthew Ahrens 
37124445fffbSMatthew Ahrens 		/* validate the target name */
37134445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
37144445fffbSMatthew Ahrens 		    B_TRUE)) {
37154445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
37164445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
37174445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
37184445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37194445fffbSMatthew Ahrens 		}
37204445fffbSMatthew Ahrens 	}
37214445fffbSMatthew Ahrens 
3722e9316f76SJoe Stein 	/*
3723e9316f76SJoe Stein 	 * get pool handle for prop validation. assumes all snaps are in the
3724e9316f76SJoe Stein 	 * same pool, as does lzc_snapshot (below).
3725e9316f76SJoe Stein 	 */
3726e9316f76SJoe Stein 	char pool[MAXNAMELEN];
3727e9316f76SJoe Stein 	elem = nvlist_next_nvpair(snaps, NULL);
3728e9316f76SJoe Stein 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3729e9316f76SJoe Stein 	pool[strcspn(pool, "/@")] = '\0';
3730e9316f76SJoe Stein 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3731e9316f76SJoe Stein 
37324445fffbSMatthew Ahrens 	if (props != NULL &&
37334445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3734e9316f76SJoe Stein 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3735e9316f76SJoe Stein 		zpool_close(zpool_hdl);
37364445fffbSMatthew Ahrens 		return (-1);
37374445fffbSMatthew Ahrens 	}
3738e9316f76SJoe Stein 	zpool_close(zpool_hdl);
37394445fffbSMatthew Ahrens 
37404445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
37414445fffbSMatthew Ahrens 
37424445fffbSMatthew Ahrens 	if (ret != 0) {
37434445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
37444445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
37454445fffbSMatthew Ahrens 		    elem != NULL;
37464445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
37474445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
37484445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
37494445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
37504445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
37514445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
37524445fffbSMatthew Ahrens 			printed = B_TRUE;
37534445fffbSMatthew Ahrens 		}
37544445fffbSMatthew Ahrens 		if (!printed) {
37554445fffbSMatthew Ahrens 			switch (ret) {
37564445fffbSMatthew Ahrens 			case EXDEV:
37574445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37584445fffbSMatthew Ahrens 				    "multiple snapshots of same "
37594445fffbSMatthew Ahrens 				    "fs not allowed"));
37604445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
37614445fffbSMatthew Ahrens 
37624445fffbSMatthew Ahrens 				break;
37634445fffbSMatthew Ahrens 			default:
37644445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
37654445fffbSMatthew Ahrens 			}
37664445fffbSMatthew Ahrens 		}
37674445fffbSMatthew Ahrens 	}
37684445fffbSMatthew Ahrens 
37694445fffbSMatthew Ahrens 	nvlist_free(props);
37704445fffbSMatthew Ahrens 	nvlist_free(errors);
37714445fffbSMatthew Ahrens 	return (ret);
37724445fffbSMatthew Ahrens }
37734445fffbSMatthew Ahrens 
37744445fffbSMatthew Ahrens int
3775bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3776bb0ade09Sahrens     nvlist_t *props)
3777fa9e4066Sahrens {
3778fa9e4066Sahrens 	int ret;
37794445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
378040a5c998SMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
37814445fffbSMatthew Ahrens 	char *cp;
37824445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
378399653d4eSeschrock 	char errbuf[1024];
3784fa9e4066Sahrens 
378599653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37864445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
378799653d4eSeschrock 
3788f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
378999653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3790fa9e4066Sahrens 
37914445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
37924445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
37934445fffbSMatthew Ahrens 	*cp = '\0';
37944445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
3795bb0ade09Sahrens 
37964445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3797fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3798fa9e4066Sahrens 		return (-1);
3799fa9e4066Sahrens 	}
3800fa9e4066Sahrens 
38014445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
38024445fffbSMatthew Ahrens 	if (recursive) {
38034445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
38044445fffbSMatthew Ahrens 	} else {
38054445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
3806681d9761SEric Taylor 	}
3807fa9e4066Sahrens 
38084445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
38094445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
3810fa9e4066Sahrens 	zfs_close(zhp);
3811fa9e4066Sahrens 	return (ret);
3812fa9e4066Sahrens }
3813fa9e4066Sahrens 
3814fa9e4066Sahrens /*
3815b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3816b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3817b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3818b12a1c38Slling  * group.
3819fa9e4066Sahrens  */
3820b12a1c38Slling typedef struct rollback_data {
3821b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3822b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3823c391e322Sahrens 	boolean_t	cb_error;
3824c391e322Sahrens 	boolean_t	cb_force;
3825b12a1c38Slling } rollback_data_t;
3826b12a1c38Slling 
3827b12a1c38Slling static int
382878f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3829b12a1c38Slling {
3830b12a1c38Slling 	rollback_data_t *cbp = data;
3831c391e322Sahrens 	prop_changelist_t *clp;
3832c391e322Sahrens 
383378f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
38340069fd67STim Haley 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3835c391e322Sahrens 	    cbp->cb_force ? MS_FORCE: 0);
3836c391e322Sahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
3837c391e322Sahrens 		cbp->cb_error = B_TRUE;
3838c391e322Sahrens 		zfs_close(zhp);
3839c391e322Sahrens 		return (0);
3840c391e322Sahrens 	}
3841842727c2SChris Kirby 	if (zfs_destroy(zhp, B_FALSE) != 0)
3842c391e322Sahrens 		cbp->cb_error = B_TRUE;
3843c391e322Sahrens 	else
3844c391e322Sahrens 		changelist_remove(clp, zhp->zfs_name);
3845ba7b046eSahrens 	(void) changelist_postfix(clp);
3846c391e322Sahrens 	changelist_free(clp);
384778f17100SMatthew Ahrens 
384878f17100SMatthew Ahrens 	zfs_close(zhp);
384978f17100SMatthew Ahrens 	return (0);
385078f17100SMatthew Ahrens }
385178f17100SMatthew Ahrens 
385278f17100SMatthew Ahrens static int
385378f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
385478f17100SMatthew Ahrens {
385578f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
385678f17100SMatthew Ahrens 
385778f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
385878f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
385978f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
386078f17100SMatthew Ahrens 
386178f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3862b12a1c38Slling 	}
3863b12a1c38Slling 
3864b12a1c38Slling 	zfs_close(zhp);
3865b12a1c38Slling 	return (0);
3866b12a1c38Slling }
3867b12a1c38Slling 
3868b12a1c38Slling /*
38694ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
38704ccbb6e7Sahrens  * data changes since then and making it the active dataset.
38714ccbb6e7Sahrens  *
387278f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
387378f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
3874b12a1c38Slling  */
38754ccbb6e7Sahrens int
3876c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3877fa9e4066Sahrens {
38784ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
38794ccbb6e7Sahrens 	int err;
38807b97dc1aSrm160521 	boolean_t restore_resv = 0;
38817b97dc1aSrm160521 	uint64_t old_volsize, new_volsize;
38827b97dc1aSrm160521 	zfs_prop_t resv_prop;
3883fa9e4066Sahrens 
3884fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3885fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3886fa9e4066Sahrens 
38874ccbb6e7Sahrens 	/*
38882e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
38894ccbb6e7Sahrens 	 */
3890c391e322Sahrens 	cb.cb_force = force;
38914ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
38924ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
389378f17100SMatthew Ahrens 	(void) zfs_iter_snapshots(zhp, rollback_destroy, &cb);
389478f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
38954ccbb6e7Sahrens 
3896c391e322Sahrens 	if (cb.cb_error)
3897c391e322Sahrens 		return (-1);
38984ccbb6e7Sahrens 
38994ccbb6e7Sahrens 	/*
39004ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
39014ccbb6e7Sahrens 	 * rollback to the given snapshot.
39024ccbb6e7Sahrens 	 */
39034ccbb6e7Sahrens 
39047b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
39057b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
39067b97dc1aSrm160521 			return (-1);
39077b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
39087b97dc1aSrm160521 		restore_resv =
39097b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
39107b97dc1aSrm160521 	}
3911fa9e4066Sahrens 
3912fa9e4066Sahrens 	/*
39134ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
39144ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
39154ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
39164ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
39174ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
3918fa9e4066Sahrens 	 */
3919a7027df1SMatthew Ahrens 	err = lzc_rollback(zhp->zfs_name, NULL, 0);
3920a7027df1SMatthew Ahrens 	if (err != 0) {
3921ece3d9b3Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
392299653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
392399653d4eSeschrock 		    zhp->zfs_name);
3924b9415e83Srm160521 		return (err);
3925b9415e83Srm160521 	}
3926fa9e4066Sahrens 
39277b97dc1aSrm160521 	/*
39287b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
39297b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
39307b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
39317b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
39327b97dc1aSrm160521 	 */
3933b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3934b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
39357b97dc1aSrm160521 		if (restore_resv) {
39367b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
39377b97dc1aSrm160521 			if (old_volsize != new_volsize)
3938b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
3939b9415e83Srm160521 				    new_volsize);
39407b97dc1aSrm160521 		}
39417b97dc1aSrm160521 		zfs_close(zhp);
39427b97dc1aSrm160521 	}
39434ccbb6e7Sahrens 	return (err);
3944b12a1c38Slling }
3945b12a1c38Slling 
3946b12a1c38Slling /*
3947fa9e4066Sahrens  * Renames the given dataset.
3948fa9e4066Sahrens  */
3949fa9e4066Sahrens int
39506a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
39516a9cb0eaSEric Schrock     boolean_t force_unmount)
3952fa9e4066Sahrens {
3953fa9e4066Sahrens 	int ret;
3954fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3955fa9e4066Sahrens 	char *delim;
3956cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3957cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3958cdf5b4caSmmusante 	char *parentname = NULL;
395940a5c998SMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
396099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
396199653d4eSeschrock 	char errbuf[1024];
3962fa9e4066Sahrens 
3963fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3964fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3965fa9e4066Sahrens 		return (0);
3966fa9e4066Sahrens 
396799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
396899653d4eSeschrock 	    "cannot rename to '%s'"), target);
396999653d4eSeschrock 
3970fa9e4066Sahrens 	/*
3971fa9e4066Sahrens 	 * Make sure the target name is valid
3972fa9e4066Sahrens 	 */
3973fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
397498579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
397598579b20Snd150628 		    *target == '@') {
397698579b20Snd150628 			/*
397798579b20Snd150628 			 * Snapshot target name is abbreviated,
397898579b20Snd150628 			 * reconstruct full dataset name
397998579b20Snd150628 			 */
398098579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
398198579b20Snd150628 			    sizeof (parent));
398298579b20Snd150628 			delim = strchr(parent, '@');
398398579b20Snd150628 			if (strchr(target, '@') == NULL)
398498579b20Snd150628 				*(++delim) = '\0';
398598579b20Snd150628 			else
398698579b20Snd150628 				*delim = '\0';
398798579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
398898579b20Snd150628 			target = parent;
398998579b20Snd150628 		} else {
3990fa9e4066Sahrens 			/*
3991fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
3992fa9e4066Sahrens 			 */
399398579b20Snd150628 			delim = strchr(target, '@');
399498579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
399598579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
399699653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399798579b20Snd150628 				    "snapshots must be part of same "
399898579b20Snd150628 				    "dataset"));
399998579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
400098579b20Snd150628 				    errbuf));
4001fa9e4066Sahrens 			}
400298579b20Snd150628 		}
4003f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
400498579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4005fa9e4066Sahrens 	} else {
4006cdf5b4caSmmusante 		if (recursive) {
4007cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4008cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
4009cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4010cdf5b4caSmmusante 		}
4011cdf5b4caSmmusante 
4012f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
401398579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4014e9dbad6fSeschrock 
4015fa9e4066Sahrens 		/* validate parents */
4016d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4017fa9e4066Sahrens 			return (-1);
4018fa9e4066Sahrens 
4019fa9e4066Sahrens 		/* make sure we're in the same pool */
4020fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4021fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4022fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
402399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
402499653d4eSeschrock 			    "datasets must be within same pool"));
402599653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4026fa9e4066Sahrens 		}
4027f2fdf992Snd150628 
4028f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
4029d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
4030f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4031d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
4032f2fdf992Snd150628 			    "current dataset name"));
4033f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4034f2fdf992Snd150628 		}
4035fa9e4066Sahrens 	}
4036fa9e4066Sahrens 
403799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
403899653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
403999653d4eSeschrock 
4040fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4041fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
404299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
404399653d4eSeschrock 		    "dataset is used in a non-global zone"));
404499653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4045fa9e4066Sahrens 	}
4046fa9e4066Sahrens 
4047cdf5b4caSmmusante 	if (recursive) {
4048f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4049f0c5ee21Smmusante 		if (parentname == NULL) {
4050f0c5ee21Smmusante 			ret = -1;
4051f0c5ee21Smmusante 			goto error;
4052f0c5ee21Smmusante 		}
4053cdf5b4caSmmusante 		delim = strchr(parentname, '@');
4054cdf5b4caSmmusante 		*delim = '\0';
4055990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4056cdf5b4caSmmusante 		if (zhrp == NULL) {
4057f0c5ee21Smmusante 			ret = -1;
4058f0c5ee21Smmusante 			goto error;
4059cdf5b4caSmmusante 		}
406033cde0d0SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
40616a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
40626a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
406399653d4eSeschrock 			return (-1);
4064fa9e4066Sahrens 
4065fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
406699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
406799653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
406899653d4eSeschrock 			    "in a non-global zone"));
4069e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4070fa9e4066Sahrens 			goto error;
4071fa9e4066Sahrens 		}
4072fa9e4066Sahrens 
4073fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
4074fa9e4066Sahrens 			goto error;
4075cdf5b4caSmmusante 	}
4076fa9e4066Sahrens 
4077e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
4078fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4079fa9e4066Sahrens 	else
4080fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4081fa9e4066Sahrens 
408298579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4083e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
408498579b20Snd150628 
4085cdf5b4caSmmusante 	zc.zc_cookie = recursive;
4086cdf5b4caSmmusante 
4087ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4088cdf5b4caSmmusante 		/*
4089cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
4090cdf5b4caSmmusante 		 * be in zc.zc_name
4091cdf5b4caSmmusante 		 */
4092cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40933cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
4094cdf5b4caSmmusante 
4095cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
4096cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4097cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
4098cdf5b4caSmmusante 			    "with the new name"));
4099a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4100cdf5b4caSmmusante 		} else {
410199653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4102cdf5b4caSmmusante 		}
4103fa9e4066Sahrens 
4104fa9e4066Sahrens 		/*
4105fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
4106fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
4107fa9e4066Sahrens 		 */
410833cde0d0SMatthew Ahrens 		if (cl != NULL)
4109fa9e4066Sahrens 			(void) changelist_postfix(cl);
4110cdf5b4caSmmusante 	} else {
411133cde0d0SMatthew Ahrens 		if (cl != NULL) {
4112fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
4113fa9e4066Sahrens 			ret = changelist_postfix(cl);
4114fa9e4066Sahrens 		}
4115cdf5b4caSmmusante 	}
4116fa9e4066Sahrens 
4117fa9e4066Sahrens error:
411833cde0d0SMatthew Ahrens 	if (parentname != NULL) {
4119cdf5b4caSmmusante 		free(parentname);
4120cdf5b4caSmmusante 	}
412133cde0d0SMatthew Ahrens 	if (zhrp != NULL) {
4122cdf5b4caSmmusante 		zfs_close(zhrp);
4123cdf5b4caSmmusante 	}
412433cde0d0SMatthew Ahrens 	if (cl != NULL) {
4125fa9e4066Sahrens 		changelist_free(cl);
4126cdf5b4caSmmusante 	}
4127fa9e4066Sahrens 	return (ret);
4128fa9e4066Sahrens }
4129fa9e4066Sahrens 
4130e9dbad6fSeschrock nvlist_t *
4131e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4132e9dbad6fSeschrock {
4133e9dbad6fSeschrock 	return (zhp->zfs_user_props);
4134e9dbad6fSeschrock }
4135e9dbad6fSeschrock 
413692241e0bSTom Erickson nvlist_t *
413792241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
413892241e0bSTom Erickson {
413992241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
414092241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
414192241e0bSTom Erickson 			return (NULL);
414292241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
414392241e0bSTom Erickson }
414492241e0bSTom Erickson 
4145e9dbad6fSeschrock /*
4146e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
4147e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
4148e9dbad6fSeschrock  *
4149e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
4150e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
4151e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
4152e9dbad6fSeschrock  *
4153e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
415492241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
415592241e0bSTom Erickson  *        requested received property values, we also need to compute the width
415692241e0bSTom Erickson  *        of the RECEIVED column.
4157e9dbad6fSeschrock  */
4158e9dbad6fSeschrock int
415943d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
416043d68d68SYuri Pankov     boolean_t literal)
4161e9dbad6fSeschrock {
4162e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4163990b4856Slling 	zprop_list_t *entry;
4164990b4856Slling 	zprop_list_t **last, **start;
4165e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
4166e9dbad6fSeschrock 	nvpair_t *elem;
4167e9dbad6fSeschrock 	char *strval;
4168e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
4169e9dbad6fSeschrock 
4170990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4171e9dbad6fSeschrock 		return (-1);
4172e9dbad6fSeschrock 
4173e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4174e9dbad6fSeschrock 
4175e9dbad6fSeschrock 	entry = *plp;
4176e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4177e9dbad6fSeschrock 		/*
4178e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4179e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4180e9dbad6fSeschrock 		 * non-native property.
4181e9dbad6fSeschrock 		 */
4182e9dbad6fSeschrock 		start = plp;
4183e9dbad6fSeschrock 		while (*start != NULL) {
4184990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4185e9dbad6fSeschrock 				break;
4186e9dbad6fSeschrock 			start = &(*start)->pl_next;
4187e9dbad6fSeschrock 		}
4188e9dbad6fSeschrock 
4189e9dbad6fSeschrock 		elem = NULL;
4190e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4191e9dbad6fSeschrock 			/*
4192e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4193e9dbad6fSeschrock 			 */
4194e9dbad6fSeschrock 			for (last = start; *last != NULL;
4195e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4196e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4197e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4198e9dbad6fSeschrock 					break;
4199e9dbad6fSeschrock 			}
4200e9dbad6fSeschrock 
4201e9dbad6fSeschrock 			if (*last == NULL) {
4202e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4203990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4204e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4205e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4206e9dbad6fSeschrock 					free(entry);
4207e9dbad6fSeschrock 					return (-1);
4208e9dbad6fSeschrock 				}
4209e9dbad6fSeschrock 
4210990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4211e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4212e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4213e9dbad6fSeschrock 				*last = entry;
4214e9dbad6fSeschrock 			}
4215e9dbad6fSeschrock 		}
4216e9dbad6fSeschrock 	}
4217e9dbad6fSeschrock 
4218e9dbad6fSeschrock 	/*
4219e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4220e9dbad6fSeschrock 	 */
4221e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
422243d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
4223e9dbad6fSeschrock 			continue;
4224e9dbad6fSeschrock 
4225990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4226e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
422743d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4228e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4229e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4230e9dbad6fSeschrock 			}
423192241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
423292241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
423343d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
423492241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
423592241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
423692241e0bSTom Erickson 		} else {
423792241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
423892241e0bSTom Erickson 			    &propval) == 0) {
4239e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
4240990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
4241e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
4242e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
4243e9dbad6fSeschrock 			}
424492241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
424592241e0bSTom Erickson 			    entry->pl_user_prop,
424643d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
424792241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
424892241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
424992241e0bSTom Erickson 		}
4250e9dbad6fSeschrock 	}
4251e9dbad6fSeschrock 
4252e9dbad6fSeschrock 	return (0);
4253e9dbad6fSeschrock }
4254ecd6cf80Smarks 
4255ecd6cf80Smarks int
4256ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4257743a77edSAlan Wright     char *resource, void *export, void *sharetab,
4258743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
4259ecd6cf80Smarks {
4260ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4261ecd6cf80Smarks 	int error;
4262ecd6cf80Smarks 
4263ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4264ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4265743a77edSAlan Wright 	if (resource)
4266743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4267ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4268ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4269da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4270ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4271ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4272ecd6cf80Smarks 	return (error);
4273ecd6cf80Smarks }
42742e5e9e19SSanjeev Bagewadi 
42752e5e9e19SSanjeev Bagewadi void
42762e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
42772e5e9e19SSanjeev Bagewadi {
42782e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
42792e5e9e19SSanjeev Bagewadi 
42802e5e9e19SSanjeev Bagewadi 	/*
42812e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
42822e5e9e19SSanjeev Bagewadi 	 * properties.
42832e5e9e19SSanjeev Bagewadi 	 */
42842e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
42852e5e9e19SSanjeev Bagewadi 
42862e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
42872e5e9e19SSanjeev Bagewadi 
42882e5e9e19SSanjeev Bagewadi 	while (curr) {
42892e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
42902e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
42912e5e9e19SSanjeev Bagewadi 
429214843421SMatthew Ahrens 		/*
4293faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4294faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4295faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4296faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4297faaa6415SEric Schrock 		 * software, for example).
429814843421SMatthew Ahrens 		 */
429914843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
43002e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
43012e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
43022e5e9e19SSanjeev Bagewadi 		curr = next;
43032e5e9e19SSanjeev Bagewadi 	}
43042e5e9e19SSanjeev Bagewadi }
4305743a77edSAlan Wright 
4306743a77edSAlan Wright static int
4307743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4308743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4309743a77edSAlan Wright {
4310743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4311743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4312743a77edSAlan Wright 	int error;
4313743a77edSAlan Wright 
4314743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4315743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4316743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4317743a77edSAlan Wright 
4318743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4319743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4320743a77edSAlan Wright 			(void) no_memory(hdl);
432130925561SChris Williamson 			return (0);
4322743a77edSAlan Wright 		}
4323743a77edSAlan Wright 	}
4324743a77edSAlan Wright 
4325743a77edSAlan Wright 	switch (cmd) {
4326743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4327743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4328743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4329743a77edSAlan Wright 		break;
4330743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4331743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4332743a77edSAlan Wright 		    resource1) != 0) {
4333743a77edSAlan Wright 				(void) no_memory(hdl);
4334743a77edSAlan Wright 				return (-1);
4335743a77edSAlan Wright 		}
4336743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4337743a77edSAlan Wright 		    resource2) != 0) {
4338743a77edSAlan Wright 				(void) no_memory(hdl);
4339743a77edSAlan Wright 				return (-1);
4340743a77edSAlan Wright 		}
4341743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4342743a77edSAlan Wright 			nvlist_free(nvlist);
4343743a77edSAlan Wright 			return (-1);
4344743a77edSAlan Wright 		}
4345743a77edSAlan Wright 		break;
4346743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4347743a77edSAlan Wright 		break;
4348743a77edSAlan Wright 	default:
4349743a77edSAlan Wright 		return (-1);
4350743a77edSAlan Wright 	}
4351743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4352743a77edSAlan Wright 	nvlist_free(nvlist);
4353743a77edSAlan Wright 	return (error);
4354743a77edSAlan Wright }
4355743a77edSAlan Wright 
4356743a77edSAlan Wright int
4357743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4358743a77edSAlan Wright     char *path, char *resource)
4359743a77edSAlan Wright {
4360743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4361743a77edSAlan Wright 	    resource, NULL));
4362743a77edSAlan Wright }
4363743a77edSAlan Wright 
4364743a77edSAlan Wright int
4365743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4366743a77edSAlan Wright     char *path, char *resource)
4367743a77edSAlan Wright {
4368743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4369743a77edSAlan Wright 	    resource, NULL));
4370743a77edSAlan Wright }
4371743a77edSAlan Wright 
4372743a77edSAlan Wright int
4373743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4374743a77edSAlan Wright {
4375743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4376743a77edSAlan Wright 	    NULL, NULL));
4377743a77edSAlan Wright }
4378743a77edSAlan Wright 
4379743a77edSAlan Wright int
4380743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4381743a77edSAlan Wright     char *oldname, char *newname)
4382743a77edSAlan Wright {
4383743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4384743a77edSAlan Wright 	    oldname, newname));
4385743a77edSAlan Wright }
438614843421SMatthew Ahrens 
438714843421SMatthew Ahrens int
438814843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
438914843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
439014843421SMatthew Ahrens {
439114843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
439214843421SMatthew Ahrens 	zfs_useracct_t buf[100];
439370f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
439470f56fa6SYuri Pankov 	int ret;
439514843421SMatthew Ahrens 
439619b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
439714843421SMatthew Ahrens 
439814843421SMatthew Ahrens 	zc.zc_objset_type = type;
439914843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
440014843421SMatthew Ahrens 
440170f56fa6SYuri Pankov 	for (;;) {
440214843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
440314843421SMatthew Ahrens 
440414843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
440570f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
44063b2aab18SMatthew Ahrens 			char errbuf[1024];
440770f56fa6SYuri Pankov 
440870f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
440970f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
441070f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
441170f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
441270f56fa6SYuri Pankov 		}
441370f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
441414843421SMatthew Ahrens 			break;
441514843421SMatthew Ahrens 
441614843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
441770f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
441870f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
441970f56fa6SYuri Pankov 				return (ret);
442014843421SMatthew Ahrens 			zua++;
442114843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
442214843421SMatthew Ahrens 		}
442314843421SMatthew Ahrens 	}
442414843421SMatthew Ahrens 
442570f56fa6SYuri Pankov 	return (0);
442614843421SMatthew Ahrens }
4427842727c2SChris Kirby 
44283b2aab18SMatthew Ahrens struct holdarg {
44293b2aab18SMatthew Ahrens 	nvlist_t *nvl;
44303b2aab18SMatthew Ahrens 	const char *snapname;
44313b2aab18SMatthew Ahrens 	const char *tag;
44323b2aab18SMatthew Ahrens 	boolean_t recursive;
4433bb6e7075SMatthew Ahrens 	int error;
44343b2aab18SMatthew Ahrens };
44353b2aab18SMatthew Ahrens 
44363b2aab18SMatthew Ahrens static int
44373b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
44383b2aab18SMatthew Ahrens {
44393b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
444040a5c998SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
44413b2aab18SMatthew Ahrens 	int rv = 0;
44423b2aab18SMatthew Ahrens 
44433b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
44443b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
44453b2aab18SMatthew Ahrens 
4446a7a845e4SSteven Hartland 	if (lzc_exists(name))
44473b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
44483b2aab18SMatthew Ahrens 
44493b2aab18SMatthew Ahrens 	if (ha->recursive)
44503b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
44513b2aab18SMatthew Ahrens 	zfs_close(zhp);
44523b2aab18SMatthew Ahrens 	return (rv);
44533b2aab18SMatthew Ahrens }
44543b2aab18SMatthew Ahrens 
4455842727c2SChris Kirby int
4456842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4457a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4458842727c2SChris Kirby {
44593b2aab18SMatthew Ahrens 	int ret;
44603b2aab18SMatthew Ahrens 	struct holdarg ha;
4461842727c2SChris Kirby 
44623b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
44633b2aab18SMatthew Ahrens 	ha.snapname = snapname;
44643b2aab18SMatthew Ahrens 	ha.tag = tag;
44653b2aab18SMatthew Ahrens 	ha.recursive = recursive;
44663b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4467013023d4SMartin Matuska 
4468a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4469a7a845e4SSteven Hartland 		char errbuf[1024];
4470a7a845e4SSteven Hartland 
4471013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4472013023d4SMartin Matuska 		ret = ENOENT;
4473013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4474013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4475013023d4SMartin Matuska 		    "cannot hold snapshot '%s@%s'"),
4476013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4477a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4478013023d4SMartin Matuska 		return (ret);
4479013023d4SMartin Matuska 	}
4480013023d4SMartin Matuska 
4481a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
44823b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4483a7f53a56SChris Kirby 
4484a7a845e4SSteven Hartland 	return (ret);
4485a7a845e4SSteven Hartland }
4486842727c2SChris Kirby 
4487a7a845e4SSteven Hartland int
4488a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4489a7a845e4SSteven Hartland {
4490a7a845e4SSteven Hartland 	int ret;
4491a7a845e4SSteven Hartland 	nvlist_t *errors;
4492a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4493a7a845e4SSteven Hartland 	char errbuf[1024];
4494a7a845e4SSteven Hartland 	nvpair_t *elem;
4495a7a845e4SSteven Hartland 
4496a7a845e4SSteven Hartland 	errors = NULL;
4497a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4498a7a845e4SSteven Hartland 
4499a7a845e4SSteven Hartland 	if (ret == 0) {
4500a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4501a7a845e4SSteven Hartland 		fnvlist_free(errors);
4502a7a845e4SSteven Hartland 		return (0);
4503a7a845e4SSteven Hartland 	}
4504a7a845e4SSteven Hartland 
4505a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
45063b2aab18SMatthew Ahrens 		/* no hold-specific errors */
45073b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
45083b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
45093b2aab18SMatthew Ahrens 		switch (ret) {
45103b2aab18SMatthew Ahrens 		case ENOTSUP:
45113b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45123b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
45133b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
45143b2aab18SMatthew Ahrens 			break;
45153b2aab18SMatthew Ahrens 		case EINVAL:
45163b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
45173b2aab18SMatthew Ahrens 			break;
45183b2aab18SMatthew Ahrens 		default:
45193b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
45203b2aab18SMatthew Ahrens 		}
45213b2aab18SMatthew Ahrens 	}
4522842727c2SChris Kirby 
45233b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
45243b2aab18SMatthew Ahrens 	    elem != NULL;
45253b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
45263b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
45273b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
45283b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
45293b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
453015508ac0SChris Kirby 		case E2BIG:
453115508ac0SChris Kirby 			/*
453215508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
453315508ac0SChris Kirby 			 * prepended. So even if we passed the length check
453415508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
453515508ac0SChris Kirby 			 * up being slightly too long.
453615508ac0SChris Kirby 			 */
45373b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
45383b2aab18SMatthew Ahrens 			break;
4539842727c2SChris Kirby 		case EINVAL:
45403b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
45413b2aab18SMatthew Ahrens 			break;
4542842727c2SChris Kirby 		case EEXIST:
45433b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
45443b2aab18SMatthew Ahrens 			break;
4545842727c2SChris Kirby 		default:
45463b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
45473b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4548842727c2SChris Kirby 		}
4549842727c2SChris Kirby 	}
4550842727c2SChris Kirby 
45513b2aab18SMatthew Ahrens 	fnvlist_free(errors);
45523b2aab18SMatthew Ahrens 	return (ret);
45533b2aab18SMatthew Ahrens }
45543b2aab18SMatthew Ahrens 
45553b2aab18SMatthew Ahrens static int
45563b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
45573b2aab18SMatthew Ahrens {
45583b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
455940a5c998SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
45603b2aab18SMatthew Ahrens 	int rv = 0;
4561bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
45623b2aab18SMatthew Ahrens 
45633b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
45643b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
45653b2aab18SMatthew Ahrens 
4566bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4567bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4568bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4569bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4570bb6e7075SMatthew Ahrens 	} else {
4571bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4572bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4573bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4574bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
45753b2aab18SMatthew Ahrens 	}
45763b2aab18SMatthew Ahrens 
45773b2aab18SMatthew Ahrens 	if (ha->recursive)
45783b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
45793b2aab18SMatthew Ahrens 	zfs_close(zhp);
45803b2aab18SMatthew Ahrens 	return (rv);
4581842727c2SChris Kirby }
4582842727c2SChris Kirby 
4583842727c2SChris Kirby int
4584842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4585842727c2SChris Kirby     boolean_t recursive)
4586842727c2SChris Kirby {
45873b2aab18SMatthew Ahrens 	int ret;
45883b2aab18SMatthew Ahrens 	struct holdarg ha;
4589a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
45903b2aab18SMatthew Ahrens 	nvpair_t *elem;
4591842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4592013023d4SMartin Matuska 	char errbuf[1024];
4593842727c2SChris Kirby 
45943b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
45953b2aab18SMatthew Ahrens 	ha.snapname = snapname;
45963b2aab18SMatthew Ahrens 	ha.tag = tag;
45973b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4598bb6e7075SMatthew Ahrens 	ha.error = 0;
45993b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4600013023d4SMartin Matuska 
4601a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4602013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4603bb6e7075SMatthew Ahrens 		ret = ha.error;
4604013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4605013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4606013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
4607013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4608bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
4609bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4610bb6e7075SMatthew Ahrens 		} else {
4611013023d4SMartin Matuska 			(void) zfs_standard_error(hdl, ret, errbuf);
4612bb6e7075SMatthew Ahrens 		}
4613013023d4SMartin Matuska 		return (ret);
4614013023d4SMartin Matuska 	}
4615013023d4SMartin Matuska 
46163b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
46173b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4618842727c2SChris Kirby 
4619a7a845e4SSteven Hartland 	if (ret == 0) {
4620a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4621a7a845e4SSteven Hartland 		fnvlist_free(errors);
46223b2aab18SMatthew Ahrens 		return (0);
4623a7a845e4SSteven Hartland 	}
4624842727c2SChris Kirby 
4625a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
46263b2aab18SMatthew Ahrens 		/* no hold-specific errors */
4627842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
46283b2aab18SMatthew Ahrens 		    "cannot release"));
4629842727c2SChris Kirby 		switch (errno) {
4630842727c2SChris Kirby 		case ENOTSUP:
4631842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4632842727c2SChris Kirby 			    "pool must be upgraded"));
46333b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
46343b2aab18SMatthew Ahrens 			break;
4635842727c2SChris Kirby 		default:
46363b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4637842727c2SChris Kirby 		}
4638842727c2SChris Kirby 	}
4639842727c2SChris Kirby 
46403b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
46413b2aab18SMatthew Ahrens 	    elem != NULL;
46423b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
46433b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
46443b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
46453b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
46463b2aab18SMatthew Ahrens 		    nvpair_name(elem));
46473b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
46483b2aab18SMatthew Ahrens 		case ESRCH:
46493b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
46503b2aab18SMatthew Ahrens 			break;
46513b2aab18SMatthew Ahrens 		case EINVAL:
46523b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
46533b2aab18SMatthew Ahrens 			break;
46543b2aab18SMatthew Ahrens 		default:
46553b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
46563b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
46573b2aab18SMatthew Ahrens 		}
46583b2aab18SMatthew Ahrens 	}
46593b2aab18SMatthew Ahrens 
46603b2aab18SMatthew Ahrens 	fnvlist_free(errors);
46613b2aab18SMatthew Ahrens 	return (ret);
4662842727c2SChris Kirby }
4663ca45db41SChris Kirby 
46641af68beaSAlexander Stetsenko int
46651af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
46661af68beaSAlexander Stetsenko {
46671af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
46681af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
46691af68beaSAlexander Stetsenko 	int nvsz = 2048;
46701af68beaSAlexander Stetsenko 	void *nvbuf;
46711af68beaSAlexander Stetsenko 	int err = 0;
46723b2aab18SMatthew Ahrens 	char errbuf[1024];
46731af68beaSAlexander Stetsenko 
46741af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
46751af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
46761af68beaSAlexander Stetsenko 
46771af68beaSAlexander Stetsenko tryagain:
46781af68beaSAlexander Stetsenko 
46791af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
46801af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
46811af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
46821af68beaSAlexander Stetsenko 		goto out;
46831af68beaSAlexander Stetsenko 	}
46841af68beaSAlexander Stetsenko 
46851af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
46861af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
46871af68beaSAlexander Stetsenko 
468840a5c998SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
46891af68beaSAlexander Stetsenko 
4690d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
46911af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
46921af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
46931af68beaSAlexander Stetsenko 		    zc.zc_name);
46941af68beaSAlexander Stetsenko 		switch (errno) {
46951af68beaSAlexander Stetsenko 		case ENOMEM:
46961af68beaSAlexander Stetsenko 			free(nvbuf);
46971af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
46981af68beaSAlexander Stetsenko 			goto tryagain;
46991af68beaSAlexander Stetsenko 
47001af68beaSAlexander Stetsenko 		case ENOTSUP:
47011af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
47021af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
47031af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
47041af68beaSAlexander Stetsenko 			break;
47051af68beaSAlexander Stetsenko 		case EINVAL:
47061af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
47071af68beaSAlexander Stetsenko 			break;
47081af68beaSAlexander Stetsenko 		case ENOENT:
47091af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
47101af68beaSAlexander Stetsenko 			break;
47111af68beaSAlexander Stetsenko 		default:
47121af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
47131af68beaSAlexander Stetsenko 			break;
47141af68beaSAlexander Stetsenko 		}
47151af68beaSAlexander Stetsenko 	} else {
47161af68beaSAlexander Stetsenko 		/* success */
47171af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
47181af68beaSAlexander Stetsenko 		if (rc) {
47191af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
47201af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
47211af68beaSAlexander Stetsenko 			    zc.zc_name);
47221af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
47231af68beaSAlexander Stetsenko 		}
47241af68beaSAlexander Stetsenko 	}
47251af68beaSAlexander Stetsenko 
47261af68beaSAlexander Stetsenko 	free(nvbuf);
47271af68beaSAlexander Stetsenko out:
47281af68beaSAlexander Stetsenko 	return (err);
47291af68beaSAlexander Stetsenko }
47301af68beaSAlexander Stetsenko 
47311af68beaSAlexander Stetsenko int
47321af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
47331af68beaSAlexander Stetsenko {
47341af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
47351af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
47361af68beaSAlexander Stetsenko 	char *nvbuf;
47373b2aab18SMatthew Ahrens 	char errbuf[1024];
47381af68beaSAlexander Stetsenko 	size_t nvsz;
47391af68beaSAlexander Stetsenko 	int err;
47401af68beaSAlexander Stetsenko 
47411af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
47421af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
47431af68beaSAlexander Stetsenko 
47441af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
47451af68beaSAlexander Stetsenko 	assert(err == 0);
47461af68beaSAlexander Stetsenko 
47471af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
47481af68beaSAlexander Stetsenko 
47491af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
47501af68beaSAlexander Stetsenko 	assert(err == 0);
47511af68beaSAlexander Stetsenko 
47521af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
47531af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
47541af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
47551af68beaSAlexander Stetsenko 
47561af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
47571af68beaSAlexander Stetsenko 
47581af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
47591af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
47601af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
47611af68beaSAlexander Stetsenko 		    zc.zc_name);
47621af68beaSAlexander Stetsenko 		switch (errno) {
47631af68beaSAlexander Stetsenko 		case ENOTSUP:
47641af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
47651af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
47661af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
47671af68beaSAlexander Stetsenko 			break;
47681af68beaSAlexander Stetsenko 		case EINVAL:
47691af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
47701af68beaSAlexander Stetsenko 			break;
47711af68beaSAlexander Stetsenko 		case ENOENT:
47721af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
47731af68beaSAlexander Stetsenko 			break;
47741af68beaSAlexander Stetsenko 		default:
47751af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
47761af68beaSAlexander Stetsenko 			break;
47771af68beaSAlexander Stetsenko 		}
47781af68beaSAlexander Stetsenko 	}
47791af68beaSAlexander Stetsenko 
47801af68beaSAlexander Stetsenko 	free(nvbuf);
47811af68beaSAlexander Stetsenko 
47821af68beaSAlexander Stetsenko 	return (err);
47831af68beaSAlexander Stetsenko }
47841af68beaSAlexander Stetsenko 
47851af68beaSAlexander Stetsenko int
47861af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
47871af68beaSAlexander Stetsenko {
47883b2aab18SMatthew Ahrens 	int err;
47893b2aab18SMatthew Ahrens 	char errbuf[1024];
47903b2aab18SMatthew Ahrens 
47913b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
47923b2aab18SMatthew Ahrens 
47933b2aab18SMatthew Ahrens 	if (err != 0) {
47941af68beaSAlexander Stetsenko 		libzfs_handle_t *hdl = zhp->zfs_hdl;
47951af68beaSAlexander Stetsenko 
47961af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
47971af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
47983b2aab18SMatthew Ahrens 		    zhp->zfs_name);
47993b2aab18SMatthew Ahrens 		switch (err) {
48001af68beaSAlexander Stetsenko 		case ENOTSUP:
48011af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
48021af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
48031af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
48041af68beaSAlexander Stetsenko 			break;
48051af68beaSAlexander Stetsenko 		case EINVAL:
48061af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
48071af68beaSAlexander Stetsenko 			break;
48081af68beaSAlexander Stetsenko 		case ENOENT:
48091af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
48101af68beaSAlexander Stetsenko 			break;
48111af68beaSAlexander Stetsenko 		default:
48121af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
48131af68beaSAlexander Stetsenko 			break;
48141af68beaSAlexander Stetsenko 		}
48151af68beaSAlexander Stetsenko 	}
48161af68beaSAlexander Stetsenko 
48171af68beaSAlexander Stetsenko 	return (err);
48181af68beaSAlexander Stetsenko }
48191af68beaSAlexander Stetsenko 
48203e30c24aSWill Andrews /*
48213e30c24aSWill Andrews  * Convert the zvol's volume size to an appropriate reservation.
48223e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
48233e30c24aSWill Andrews  * suite's shell version in reservation.kshlib.
48243e30c24aSWill Andrews  */
4825c1449561SEric Taylor uint64_t
4826c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4827c1449561SEric Taylor {
4828c1449561SEric Taylor 	uint64_t numdb;
4829c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4830c1449561SEric Taylor 	int ncopies;
4831c1449561SEric Taylor 	char *strval;
4832c1449561SEric Taylor 
4833c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4834c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4835c1449561SEric Taylor 		ncopies = atoi(strval);
4836c1449561SEric Taylor 	else
4837c1449561SEric Taylor 		ncopies = 1;
4838c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4839c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4840c1449561SEric Taylor 	    &volblocksize) != 0)
4841c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4842c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4843c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4844c1449561SEric Taylor 	numdb = 7;
4845c1449561SEric Taylor 	/* calculate number of indirects */
4846c1449561SEric Taylor 	while (nblocks > 1) {
4847c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4848c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4849c1449561SEric Taylor 		numdb += nblocks;
4850c1449561SEric Taylor 	}
4851c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4852c1449561SEric Taylor 	volsize *= ncopies;
4853c1449561SEric Taylor 	/*
4854c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4855c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4856c1449561SEric Taylor 	 * 1100 bytes
4857c1449561SEric Taylor 	 */
4858c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4859c1449561SEric Taylor 	volsize += numdb;
4860c1449561SEric Taylor 	return (volsize);
4861c1449561SEric Taylor }
4862