xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_dataset.c (revision e2dcee5754c56d91c6e1ff847db294541069ca0d)
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.
25*a1988827SMatthew Ahrens  * 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.
2943d68d68SYuri Pankov  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <ctype.h>
33fa9e4066Sahrens #include <errno.h>
34fa9e4066Sahrens #include <libintl.h>
35fa9e4066Sahrens #include <math.h>
36fa9e4066Sahrens #include <stdio.h>
37fa9e4066Sahrens #include <stdlib.h>
38fa9e4066Sahrens #include <strings.h>
39fa9e4066Sahrens #include <unistd.h>
403cb34c60Sahrens #include <stddef.h>
41fa9e4066Sahrens #include <zone.h>
4299653d4eSeschrock #include <fcntl.h>
43fa9e4066Sahrens #include <sys/mntent.h>
44b12a1c38Slling #include <sys/mount.h>
45ecd6cf80Smarks #include <priv.h>
46ecd6cf80Smarks #include <pwd.h>
47ecd6cf80Smarks #include <grp.h>
48ecd6cf80Smarks #include <stddef.h>
49ecd6cf80Smarks #include <ucred.h>
5014843421SMatthew Ahrens #include <idmap.h>
5114843421SMatthew Ahrens #include <aclutils.h>
523b12c289SMatthew Ahrens #include <directory.h>
53fa9e4066Sahrens 
54c1449561SEric Taylor #include <sys/dnode.h>
55fa9e4066Sahrens #include <sys/spa.h>
56e9dbad6fSeschrock #include <sys/zap.h>
57fa9e4066Sahrens #include <libzfs.h>
58fa9e4066Sahrens 
59fa9e4066Sahrens #include "zfs_namecheck.h"
60fa9e4066Sahrens #include "zfs_prop.h"
61fa9e4066Sahrens #include "libzfs_impl.h"
62ecd6cf80Smarks #include "zfs_deleg.h"
63fa9e4066Sahrens 
6414843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6514843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
66cdf5b4caSmmusante 
67fa9e4066Sahrens /*
68fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
69fa9e4066Sahrens  * readable form.
70fa9e4066Sahrens  */
71fa9e4066Sahrens const char *
zfs_type_to_name(zfs_type_t type)72fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
73fa9e4066Sahrens {
74fa9e4066Sahrens 	switch (type) {
75fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
76fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
77fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
78fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
79fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
80fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
81fa9e4066Sahrens 	}
82fa9e4066Sahrens 
83fa9e4066Sahrens 	return (NULL);
84fa9e4066Sahrens }
85fa9e4066Sahrens 
86fa9e4066Sahrens /*
87fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
88fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
89fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
90fa9e4066Sahrens  * acceptable types.
91fa9e4066Sahrens  */
92fa9e4066Sahrens static const char *
path_to_str(const char * path,int types)93fa9e4066Sahrens path_to_str(const char *path, int types)
94fa9e4066Sahrens {
95fa9e4066Sahrens 	/*
96fa9e4066Sahrens 	 * When given a single type, always report the exact type.
97fa9e4066Sahrens 	 */
98fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
99fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
100fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
101fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
102fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
103fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
104fa9e4066Sahrens 
105fa9e4066Sahrens 	/*
106fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
107fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
108fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
109fa9e4066Sahrens 	 * snapshot attribute and try again.
110fa9e4066Sahrens 	 */
111fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
112fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
113fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
114fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
115fa9e4066Sahrens 	}
116fa9e4066Sahrens 
117fa9e4066Sahrens 	/*
118fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
119fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
120fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
121fa9e4066Sahrens 	 */
122fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
123fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
124fa9e4066Sahrens 
125fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
126fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
127fa9e4066Sahrens }
128fa9e4066Sahrens 
129fa9e4066Sahrens /*
130fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
13114843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
13214843421SMatthew Ahrens  * explain exactly why the name was not valid.
133fa9e4066Sahrens  */
13499d5e173STim Haley int
zfs_validate_name(libzfs_handle_t * hdl,const char * path,int type,boolean_t modifying)135f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
136f18faf3fSek110237     boolean_t modifying)
137fa9e4066Sahrens {
138fa9e4066Sahrens 	namecheck_err_t why;
139fa9e4066Sahrens 	char what;
140fa9e4066Sahrens 
1411af68beaSAlexander Stetsenko 	(void) zfs_prop_get_table();
14254da3412SMarcel Telka 	if (entity_namecheck(path, &why, &what) != 0) {
14399653d4eSeschrock 		if (hdl != NULL) {
144fa9e4066Sahrens 			switch (why) {
145b81d61a6Slling 			case NAME_ERR_TOOLONG:
14699653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14799653d4eSeschrock 				    "name is too long"));
148b81d61a6Slling 				break;
149b81d61a6Slling 
150fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
15199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15299653d4eSeschrock 				    "leading slash in name"));
153fa9e4066Sahrens 				break;
154fa9e4066Sahrens 
155fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15699653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15799653d4eSeschrock 				    "empty component in name"));
158fa9e4066Sahrens 				break;
159fa9e4066Sahrens 
160fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
16199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16299653d4eSeschrock 				    "trailing slash in name"));
163fa9e4066Sahrens 				break;
164fa9e4066Sahrens 
165fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16699653d4eSeschrock 				zfs_error_aux(hdl,
167fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16899653d4eSeschrock 				    "'%c' in name"), what);
169fa9e4066Sahrens 				break;
170fa9e4066Sahrens 
171639f4a28SMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
17299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173639f4a28SMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
174639f4a28SMarcel Telka 				    "name"));
175fa9e4066Sahrens 				break;
1765ad82045Snd150628 
1775ad82045Snd150628 			case NAME_ERR_NOLETTER:
1785ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1795ad82045Snd150628 				    "pool doesn't begin with a letter"));
1805ad82045Snd150628 				break;
1815ad82045Snd150628 
1825ad82045Snd150628 			case NAME_ERR_RESERVED:
1835ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1845ad82045Snd150628 				    "name is reserved"));
1855ad82045Snd150628 				break;
1865ad82045Snd150628 
1875ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1885ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1895ad82045Snd150628 				    "reserved disk name"));
1905ad82045Snd150628 				break;
191fa9e4066Sahrens 			}
192fa9e4066Sahrens 		}
193fa9e4066Sahrens 
194fa9e4066Sahrens 		return (0);
195fa9e4066Sahrens 	}
196fa9e4066Sahrens 
197fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19899653d4eSeschrock 		if (hdl != NULL)
19999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200639f4a28SMarcel Telka 			    "snapshot delimiter '@' is not expected here"));
201fa9e4066Sahrens 		return (0);
202fa9e4066Sahrens 	}
203fa9e4066Sahrens 
2041d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2051d452cf5Sahrens 		if (hdl != NULL)
2061d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
207d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2081d452cf5Sahrens 		return (0);
2091d452cf5Sahrens 	}
2101d452cf5Sahrens 
211639f4a28SMarcel Telka 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
212639f4a28SMarcel Telka 		if (hdl != NULL)
213639f4a28SMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
214639f4a28SMarcel Telka 			    "bookmark delimiter '#' is not expected here"));
215639f4a28SMarcel Telka 		return (0);
216639f4a28SMarcel Telka 	}
217639f4a28SMarcel Telka 
218639f4a28SMarcel Telka 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
219639f4a28SMarcel Telka 		if (hdl != NULL)
220639f4a28SMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
221639f4a28SMarcel Telka 			    "missing '#' delimiter in bookmark name"));
222639f4a28SMarcel Telka 		return (0);
223639f4a28SMarcel Telka 	}
224639f4a28SMarcel Telka 
225f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
226f18faf3fSek110237 		if (hdl != NULL)
227f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
228f18faf3fSek110237 			    "invalid character %c in name"), '%');
229f18faf3fSek110237 		return (0);
230f18faf3fSek110237 	}
231f18faf3fSek110237 
23299653d4eSeschrock 	return (-1);
233fa9e4066Sahrens }
234fa9e4066Sahrens 
235fa9e4066Sahrens int
zfs_name_valid(const char * name,zfs_type_t type)236fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
237fa9e4066Sahrens {
238e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
239e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
240f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
241fa9e4066Sahrens }
242fa9e4066Sahrens 
243fa9e4066Sahrens /*
244e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
245e9dbad6fSeschrock  * properties into a separate nvlist.
246e9dbad6fSeschrock  */
247fac3008cSeschrock static nvlist_t *
process_user_props(zfs_handle_t * zhp,nvlist_t * props)248fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
249e9dbad6fSeschrock {
250e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
251e9dbad6fSeschrock 	nvpair_t *elem;
252e9dbad6fSeschrock 	nvlist_t *propval;
253fac3008cSeschrock 	nvlist_t *nvl;
254e9dbad6fSeschrock 
255fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
256fac3008cSeschrock 		(void) no_memory(hdl);
257fac3008cSeschrock 		return (NULL);
258fac3008cSeschrock 	}
259e9dbad6fSeschrock 
260e9dbad6fSeschrock 	elem = NULL;
261fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
262e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
263e9dbad6fSeschrock 			continue;
264e9dbad6fSeschrock 
265e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
266fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
267fac3008cSeschrock 			nvlist_free(nvl);
268fac3008cSeschrock 			(void) no_memory(hdl);
269fac3008cSeschrock 			return (NULL);
270fac3008cSeschrock 		}
271e9dbad6fSeschrock 	}
272e9dbad6fSeschrock 
273fac3008cSeschrock 	return (nvl);
274e9dbad6fSeschrock }
275e9dbad6fSeschrock 
27629ab75c9Srm160521 static zpool_handle_t *
zpool_add_handle(zfs_handle_t * zhp,const char * pool_name)27729ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
27829ab75c9Srm160521 {
27929ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
28029ab75c9Srm160521 	zpool_handle_t *zph;
28129ab75c9Srm160521 
28229ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
28329ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
28429ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
28529ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
28629ab75c9Srm160521 	}
28729ab75c9Srm160521 	return (zph);
28829ab75c9Srm160521 }
28929ab75c9Srm160521 
29029ab75c9Srm160521 static zpool_handle_t *
zpool_find_handle(zfs_handle_t * zhp,const char * pool_name,int len)29129ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
29229ab75c9Srm160521 {
29329ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
29429ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
29529ab75c9Srm160521 
29629ab75c9Srm160521 	while ((zph != NULL) &&
29729ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
29829ab75c9Srm160521 		zph = zph->zpool_next;
29929ab75c9Srm160521 	return (zph);
30029ab75c9Srm160521 }
30129ab75c9Srm160521 
30229ab75c9Srm160521 /*
30329ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
30429ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
30529ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
30629ab75c9Srm160521  */
30729ab75c9Srm160521 static zpool_handle_t *
zpool_handle(zfs_handle_t * zhp)30829ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
30929ab75c9Srm160521 {
31029ab75c9Srm160521 	char *pool_name;
31129ab75c9Srm160521 	int len;
31229ab75c9Srm160521 	zpool_handle_t *zph;
31329ab75c9Srm160521 
31478f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
31529ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
31629ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
31729ab75c9Srm160521 
31829ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
31929ab75c9Srm160521 	if (zph == NULL)
32029ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
32129ab75c9Srm160521 
32229ab75c9Srm160521 	free(pool_name);
32329ab75c9Srm160521 	return (zph);
32429ab75c9Srm160521 }
32529ab75c9Srm160521 
32629ab75c9Srm160521 void
zpool_free_handles(libzfs_handle_t * hdl)32729ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
32829ab75c9Srm160521 {
32929ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
33029ab75c9Srm160521 
33129ab75c9Srm160521 	while (zph != NULL) {
33229ab75c9Srm160521 		next = zph->zpool_next;
33329ab75c9Srm160521 		zpool_close(zph);
33429ab75c9Srm160521 		zph = next;
33529ab75c9Srm160521 	}
33629ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
33729ab75c9Srm160521 }
33829ab75c9Srm160521 
339e9dbad6fSeschrock /*
340fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
341fa9e4066Sahrens  */
342fa9e4066Sahrens static int
get_stats_ioctl(zfs_handle_t * zhp,zfs_cmd_t * zc)343ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
344fa9e4066Sahrens {
345e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
346fa9e4066Sahrens 
347ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
348fa9e4066Sahrens 
349ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3507f7322feSeschrock 		if (errno == ENOMEM) {
351ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
35299653d4eSeschrock 				return (-1);
353e9dbad6fSeschrock 			}
3547f7322feSeschrock 		} else {
355fa9e4066Sahrens 			return (-1);
3567f7322feSeschrock 		}
3577f7322feSeschrock 	}
358ebedde84SEric Taylor 	return (0);
359fac3008cSeschrock }
360fac3008cSeschrock 
36192241e0bSTom Erickson /*
36292241e0bSTom Erickson  * Utility function to get the received properties of the given object.
36392241e0bSTom Erickson  */
36492241e0bSTom Erickson static int
get_recvd_props_ioctl(zfs_handle_t * zhp)36592241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
36692241e0bSTom Erickson {
36792241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
36892241e0bSTom Erickson 	nvlist_t *recvdprops;
36992241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
37092241e0bSTom Erickson 	int err;
37192241e0bSTom Erickson 
37292241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
37392241e0bSTom Erickson 		return (-1);
37492241e0bSTom Erickson 
37592241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
37692241e0bSTom Erickson 
37792241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
37892241e0bSTom Erickson 		if (errno == ENOMEM) {
37992241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
38092241e0bSTom Erickson 				return (-1);
38192241e0bSTom Erickson 			}
38292241e0bSTom Erickson 		} else {
38392241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
38492241e0bSTom Erickson 			return (-1);
38592241e0bSTom Erickson 		}
38692241e0bSTom Erickson 	}
38792241e0bSTom Erickson 
38892241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
38992241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
39092241e0bSTom Erickson 	if (err != 0)
39192241e0bSTom Erickson 		return (-1);
39292241e0bSTom Erickson 
39392241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
39492241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
39592241e0bSTom Erickson 
39692241e0bSTom Erickson 	return (0);
39792241e0bSTom Erickson }
39892241e0bSTom Erickson 
399ebedde84SEric Taylor static int
put_stats_zhdl(zfs_handle_t * zhp,zfs_cmd_t * zc)400ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
401ebedde84SEric Taylor {
402ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
403ebedde84SEric Taylor 
404ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
405ebedde84SEric Taylor 
406ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
407ebedde84SEric Taylor 		return (-1);
408ebedde84SEric Taylor 	}
409fac3008cSeschrock 
41014843421SMatthew Ahrens 	/*
41114843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
41214843421SMatthew Ahrens 	 * storing them in zfs_props?
41314843421SMatthew Ahrens 	 */
414fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
415fac3008cSeschrock 		nvlist_free(allprops);
416fac3008cSeschrock 		return (-1);
417fac3008cSeschrock 	}
418fac3008cSeschrock 
41999653d4eSeschrock 	nvlist_free(zhp->zfs_props);
420fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
42199653d4eSeschrock 
422fac3008cSeschrock 	zhp->zfs_props = allprops;
423fac3008cSeschrock 	zhp->zfs_user_props = userprops;
42499653d4eSeschrock 
425fa9e4066Sahrens 	return (0);
426fa9e4066Sahrens }
427fa9e4066Sahrens 
428ebedde84SEric Taylor static int
get_stats(zfs_handle_t * zhp)429ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
430ebedde84SEric Taylor {
431ebedde84SEric Taylor 	int rc = 0;
432ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
433ebedde84SEric Taylor 
434ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
435ebedde84SEric Taylor 		return (-1);
436ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
437ebedde84SEric Taylor 		rc = -1;
438ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
439ebedde84SEric Taylor 		rc = -1;
440ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
441ebedde84SEric Taylor 	return (rc);
442ebedde84SEric Taylor }
443ebedde84SEric Taylor 
444fa9e4066Sahrens /*
445fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
446fa9e4066Sahrens  */
447fa9e4066Sahrens void
zfs_refresh_properties(zfs_handle_t * zhp)448fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
449fa9e4066Sahrens {
450fa9e4066Sahrens 	(void) get_stats(zhp);
451fa9e4066Sahrens }
452fa9e4066Sahrens 
453fa9e4066Sahrens /*
454fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
455fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
456fa9e4066Sahrens  */
457ebedde84SEric Taylor static int
make_dataset_handle_common(zfs_handle_t * zhp,zfs_cmd_t * zc)458ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
459fa9e4066Sahrens {
460503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
461ebedde84SEric Taylor 		return (-1);
46231fd60d3Sahrens 
463fa9e4066Sahrens 	/*
464fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
465fa9e4066Sahrens 	 * the high-level type.
466fa9e4066Sahrens 	 */
467a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
468a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
469a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
470a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
471a2eea2e1Sahrens 	else
472a2eea2e1Sahrens 		abort();
473a2eea2e1Sahrens 
474fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
475fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
476fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
477fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
478fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
479fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
480fa9e4066Sahrens 	else
48199653d4eSeschrock 		abort();	/* we should never see any other types */
482fa9e4066Sahrens 
4839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4849fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4859fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
486ebedde84SEric Taylor 	return (0);
487ebedde84SEric Taylor }
488ebedde84SEric Taylor 
489ebedde84SEric Taylor zfs_handle_t *
make_dataset_handle(libzfs_handle_t * hdl,const char * path)490ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
491ebedde84SEric Taylor {
492ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
493ebedde84SEric Taylor 
494ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
495ebedde84SEric Taylor 
496ebedde84SEric Taylor 	if (zhp == NULL)
497ebedde84SEric Taylor 		return (NULL);
498ebedde84SEric Taylor 
499ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
500ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
501ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
502ebedde84SEric Taylor 		free(zhp);
503ebedde84SEric Taylor 		return (NULL);
504ebedde84SEric Taylor 	}
505ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
506ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
507ebedde84SEric Taylor 		free(zhp);
508ebedde84SEric Taylor 		return (NULL);
509ebedde84SEric Taylor 	}
510ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
511ebedde84SEric Taylor 		free(zhp);
512ebedde84SEric Taylor 		zhp = NULL;
513ebedde84SEric Taylor 	}
514ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
515ebedde84SEric Taylor 	return (zhp);
516ebedde84SEric Taylor }
517ebedde84SEric Taylor 
51819b94df9SMatthew Ahrens zfs_handle_t *
make_dataset_handle_zc(libzfs_handle_t * hdl,zfs_cmd_t * zc)519ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
520ebedde84SEric Taylor {
521ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
522ebedde84SEric Taylor 
523ebedde84SEric Taylor 	if (zhp == NULL)
524ebedde84SEric Taylor 		return (NULL);
525ebedde84SEric Taylor 
526ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
527ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
528ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
529ebedde84SEric Taylor 		free(zhp);
530ebedde84SEric Taylor 		return (NULL);
531ebedde84SEric Taylor 	}
532fa9e4066Sahrens 	return (zhp);
533fa9e4066Sahrens }
534fa9e4066Sahrens 
53519b94df9SMatthew Ahrens zfs_handle_t *
zfs_handle_dup(zfs_handle_t * zhp_orig)53619b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
53719b94df9SMatthew Ahrens {
53819b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
53919b94df9SMatthew Ahrens 
54019b94df9SMatthew Ahrens 	if (zhp == NULL)
54119b94df9SMatthew Ahrens 		return (NULL);
54219b94df9SMatthew Ahrens 
54319b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
54419b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
54519b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
54619b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
54719b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
54819b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
54919b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
55019b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
55119b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
55219b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55319b94df9SMatthew Ahrens 			zfs_close(zhp);
55419b94df9SMatthew Ahrens 			return (NULL);
55519b94df9SMatthew Ahrens 		}
55619b94df9SMatthew Ahrens 	}
55719b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
55819b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
55919b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
56019b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
56119b94df9SMatthew Ahrens 			zfs_close(zhp);
56219b94df9SMatthew Ahrens 			return (NULL);
56319b94df9SMatthew Ahrens 		}
56419b94df9SMatthew Ahrens 	}
56519b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
56619b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
56719b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
56819b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
56919b94df9SMatthew Ahrens 			zfs_close(zhp);
57019b94df9SMatthew Ahrens 			return (NULL);
57119b94df9SMatthew Ahrens 		}
57219b94df9SMatthew Ahrens 	}
57319b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
57419b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
57519b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
57619b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
57719b94df9SMatthew Ahrens 	}
57819b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
57919b94df9SMatthew Ahrens 	return (zhp);
58019b94df9SMatthew Ahrens }
58119b94df9SMatthew Ahrens 
58278f17100SMatthew Ahrens boolean_t
zfs_bookmark_exists(const char * path)58378f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
58478f17100SMatthew Ahrens {
58578f17100SMatthew Ahrens 	nvlist_t *bmarks;
58678f17100SMatthew Ahrens 	nvlist_t *props;
587*a1988827SMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
58878f17100SMatthew Ahrens 	char *bmark_name;
58978f17100SMatthew Ahrens 	char *pound;
59078f17100SMatthew Ahrens 	int err;
59178f17100SMatthew Ahrens 	boolean_t rv;
59278f17100SMatthew Ahrens 
59378f17100SMatthew Ahrens 
59478f17100SMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
59578f17100SMatthew Ahrens 	pound = strchr(fsname, '#');
59678f17100SMatthew Ahrens 	if (pound == NULL)
59778f17100SMatthew Ahrens 		return (B_FALSE);
59878f17100SMatthew Ahrens 
59978f17100SMatthew Ahrens 	*pound = '\0';
60078f17100SMatthew Ahrens 	bmark_name = pound + 1;
60178f17100SMatthew Ahrens 	props = fnvlist_alloc();
60278f17100SMatthew Ahrens 	err = lzc_get_bookmarks(fsname, props, &bmarks);
60378f17100SMatthew Ahrens 	nvlist_free(props);
60478f17100SMatthew Ahrens 	if (err != 0) {
60578f17100SMatthew Ahrens 		nvlist_free(bmarks);
60678f17100SMatthew Ahrens 		return (B_FALSE);
60778f17100SMatthew Ahrens 	}
60878f17100SMatthew Ahrens 
60978f17100SMatthew Ahrens 	rv = nvlist_exists(bmarks, bmark_name);
61078f17100SMatthew Ahrens 	nvlist_free(bmarks);
61178f17100SMatthew Ahrens 	return (rv);
61278f17100SMatthew Ahrens }
61378f17100SMatthew Ahrens 
61478f17100SMatthew Ahrens zfs_handle_t *
make_bookmark_handle(zfs_handle_t * parent,const char * path,nvlist_t * bmark_props)61578f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
61678f17100SMatthew Ahrens     nvlist_t *bmark_props)
61778f17100SMatthew Ahrens {
61878f17100SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
61978f17100SMatthew Ahrens 
62078f17100SMatthew Ahrens 	if (zhp == NULL)
62178f17100SMatthew Ahrens 		return (NULL);
62278f17100SMatthew Ahrens 
62378f17100SMatthew Ahrens 	/* Fill in the name. */
62478f17100SMatthew Ahrens 	zhp->zfs_hdl = parent->zfs_hdl;
62578f17100SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
62678f17100SMatthew Ahrens 
62778f17100SMatthew Ahrens 	/* Set the property lists. */
62878f17100SMatthew Ahrens 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
62978f17100SMatthew Ahrens 		free(zhp);
63078f17100SMatthew Ahrens 		return (NULL);
63178f17100SMatthew Ahrens 	}
63278f17100SMatthew Ahrens 
63378f17100SMatthew Ahrens 	/* Set the types. */
63478f17100SMatthew Ahrens 	zhp->zfs_head_type = parent->zfs_head_type;
63578f17100SMatthew Ahrens 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
63678f17100SMatthew Ahrens 
63778f17100SMatthew Ahrens 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
63878f17100SMatthew Ahrens 		nvlist_free(zhp->zfs_props);
63978f17100SMatthew Ahrens 		free(zhp);
64078f17100SMatthew Ahrens 		return (NULL);
64178f17100SMatthew Ahrens 	}
64278f17100SMatthew Ahrens 
64378f17100SMatthew Ahrens 	return (zhp);
64478f17100SMatthew Ahrens }
64578f17100SMatthew Ahrens 
646639f4a28SMarcel Telka struct zfs_open_bookmarks_cb_data {
647639f4a28SMarcel Telka 	const char *path;
648639f4a28SMarcel Telka 	zfs_handle_t *zhp;
649639f4a28SMarcel Telka };
650639f4a28SMarcel Telka 
651639f4a28SMarcel Telka static int
zfs_open_bookmarks_cb(zfs_handle_t * zhp,void * data)652639f4a28SMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
653639f4a28SMarcel Telka {
654639f4a28SMarcel Telka 	struct zfs_open_bookmarks_cb_data *dp = data;
655639f4a28SMarcel Telka 
656fa9e4066Sahrens 	/*
657639f4a28SMarcel Telka 	 * Is it the one we are looking for?
658639f4a28SMarcel Telka 	 */
659639f4a28SMarcel Telka 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
660639f4a28SMarcel Telka 		/*
661639f4a28SMarcel Telka 		 * We found it.  Save it and let the caller know we are done.
662639f4a28SMarcel Telka 		 */
663639f4a28SMarcel Telka 		dp->zhp = zhp;
664639f4a28SMarcel Telka 		return (EEXIST);
665639f4a28SMarcel Telka 	}
666639f4a28SMarcel Telka 
667639f4a28SMarcel Telka 	/*
668639f4a28SMarcel Telka 	 * Not found.  Close the handle and ask for another one.
669639f4a28SMarcel Telka 	 */
670639f4a28SMarcel Telka 	zfs_close(zhp);
671639f4a28SMarcel Telka 	return (0);
672639f4a28SMarcel Telka }
673639f4a28SMarcel Telka 
674639f4a28SMarcel Telka /*
675639f4a28SMarcel Telka  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
676fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
677fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
678fa9e4066Sahrens  */
679fa9e4066Sahrens zfs_handle_t *
zfs_open(libzfs_handle_t * hdl,const char * path,int types)68099653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
681fa9e4066Sahrens {
682fa9e4066Sahrens 	zfs_handle_t *zhp;
68399653d4eSeschrock 	char errbuf[1024];
684639f4a28SMarcel Telka 	char *bookp;
68599653d4eSeschrock 
68699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
68799653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
688fa9e4066Sahrens 
689fa9e4066Sahrens 	/*
69099653d4eSeschrock 	 * Validate the name before we even try to open it.
691fa9e4066Sahrens 	 */
692639f4a28SMarcel Telka 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
69399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
694fa9e4066Sahrens 		return (NULL);
695fa9e4066Sahrens 	}
696fa9e4066Sahrens 
697fa9e4066Sahrens 	/*
698639f4a28SMarcel Telka 	 * Bookmarks needs to be handled separately.
699639f4a28SMarcel Telka 	 */
700639f4a28SMarcel Telka 	bookp = strchr(path, '#');
701639f4a28SMarcel Telka 	if (bookp == NULL) {
702639f4a28SMarcel Telka 		/*
703639f4a28SMarcel Telka 		 * Try to get stats for the dataset, which will tell us if it
704639f4a28SMarcel Telka 		 * exists.
705fa9e4066Sahrens 		 */
706fa9e4066Sahrens 		errno = 0;
70799653d4eSeschrock 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
708ece3d9b3Slling 			(void) zfs_standard_error(hdl, errno, errbuf);
709fa9e4066Sahrens 			return (NULL);
710fa9e4066Sahrens 		}
711639f4a28SMarcel Telka 	} else {
712639f4a28SMarcel Telka 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
713639f4a28SMarcel Telka 		zfs_handle_t *pzhp;
714639f4a28SMarcel Telka 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
715639f4a28SMarcel Telka 
716639f4a28SMarcel Telka 		/*
717639f4a28SMarcel Telka 		 * We need to cut out '#' and everything after '#'
718639f4a28SMarcel Telka 		 * to get the parent dataset name only.
719639f4a28SMarcel Telka 		 */
720639f4a28SMarcel Telka 		assert(bookp - path < sizeof (dsname));
721639f4a28SMarcel Telka 		(void) strncpy(dsname, path, bookp - path);
722639f4a28SMarcel Telka 		dsname[bookp - path] = '\0';
723639f4a28SMarcel Telka 
724639f4a28SMarcel Telka 		/*
725639f4a28SMarcel Telka 		 * Create handle for the parent dataset.
726639f4a28SMarcel Telka 		 */
727639f4a28SMarcel Telka 		errno = 0;
728639f4a28SMarcel Telka 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
729639f4a28SMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
730639f4a28SMarcel Telka 			return (NULL);
731639f4a28SMarcel Telka 		}
732639f4a28SMarcel Telka 
733639f4a28SMarcel Telka 		/*
734639f4a28SMarcel Telka 		 * Iterate bookmarks to find the right one.
735639f4a28SMarcel Telka 		 */
736639f4a28SMarcel Telka 		errno = 0;
737639f4a28SMarcel Telka 		if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
738639f4a28SMarcel Telka 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
739639f4a28SMarcel Telka 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
740639f4a28SMarcel Telka 			zfs_close(pzhp);
741639f4a28SMarcel Telka 			return (NULL);
742639f4a28SMarcel Telka 		}
743639f4a28SMarcel Telka 		if (cb_data.zhp == NULL) {
744639f4a28SMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
745639f4a28SMarcel Telka 			zfs_close(pzhp);
746639f4a28SMarcel Telka 			return (NULL);
747639f4a28SMarcel Telka 		}
748639f4a28SMarcel Telka 		zhp = cb_data.zhp;
749639f4a28SMarcel Telka 
750639f4a28SMarcel Telka 		/*
751639f4a28SMarcel Telka 		 * Cleanup.
752639f4a28SMarcel Telka 		 */
753639f4a28SMarcel Telka 		zfs_close(pzhp);
754639f4a28SMarcel Telka 	}
755fa9e4066Sahrens 
756fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
75799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
75894de1d4cSeschrock 		zfs_close(zhp);
759fa9e4066Sahrens 		return (NULL);
760fa9e4066Sahrens 	}
761fa9e4066Sahrens 
762fa9e4066Sahrens 	return (zhp);
763fa9e4066Sahrens }
764fa9e4066Sahrens 
765fa9e4066Sahrens /*
766fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
767fa9e4066Sahrens  */
768fa9e4066Sahrens void
zfs_close(zfs_handle_t * zhp)769fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
770fa9e4066Sahrens {
771fa9e4066Sahrens 	if (zhp->zfs_mntopts)
772fa9e4066Sahrens 		free(zhp->zfs_mntopts);
77399653d4eSeschrock 	nvlist_free(zhp->zfs_props);
774e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
77592241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
776fa9e4066Sahrens 	free(zhp);
777fa9e4066Sahrens }
778fa9e4066Sahrens 
779ebedde84SEric Taylor typedef struct mnttab_node {
780ebedde84SEric Taylor 	struct mnttab mtn_mt;
781ebedde84SEric Taylor 	avl_node_t mtn_node;
782ebedde84SEric Taylor } mnttab_node_t;
783ebedde84SEric Taylor 
784ebedde84SEric Taylor static int
libzfs_mnttab_cache_compare(const void * arg1,const void * arg2)785ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
786ebedde84SEric Taylor {
787ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
788ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
789ebedde84SEric Taylor 	int rv;
790ebedde84SEric Taylor 
791ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
792ebedde84SEric Taylor 
793ebedde84SEric Taylor 	if (rv == 0)
794ebedde84SEric Taylor 		return (0);
795ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
796ebedde84SEric Taylor }
797ebedde84SEric Taylor 
798ebedde84SEric Taylor void
libzfs_mnttab_init(libzfs_handle_t * hdl)799ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
800ebedde84SEric Taylor {
801ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
802ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
803ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
804b2634b9cSEric Taylor }
805b2634b9cSEric Taylor 
806b2634b9cSEric Taylor void
libzfs_mnttab_update(libzfs_handle_t * hdl)807b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
808b2634b9cSEric Taylor {
809b2634b9cSEric Taylor 	struct mnttab entry;
810ebedde84SEric Taylor 
811ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
812ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
813ebedde84SEric Taylor 		mnttab_node_t *mtn;
814ebedde84SEric Taylor 
815ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
816ebedde84SEric Taylor 			continue;
817ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
818ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
819ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
820ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
821ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
822ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
823ebedde84SEric Taylor 	}
824ebedde84SEric Taylor }
825ebedde84SEric Taylor 
826ebedde84SEric Taylor void
libzfs_mnttab_fini(libzfs_handle_t * hdl)827ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
828ebedde84SEric Taylor {
829ebedde84SEric Taylor 	void *cookie = NULL;
830ebedde84SEric Taylor 	mnttab_node_t *mtn;
831ebedde84SEric Taylor 
832ebedde84SEric Taylor 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
833ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
834ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
835ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
836ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
837ebedde84SEric Taylor 		free(mtn);
838ebedde84SEric Taylor 	}
839ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
840ebedde84SEric Taylor }
841ebedde84SEric Taylor 
842b2634b9cSEric Taylor void
libzfs_mnttab_cache(libzfs_handle_t * hdl,boolean_t enable)843b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
844b2634b9cSEric Taylor {
845b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
846b2634b9cSEric Taylor }
847b2634b9cSEric Taylor 
848ebedde84SEric Taylor int
libzfs_mnttab_find(libzfs_handle_t * hdl,const char * fsname,struct mnttab * entry)849ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
850ebedde84SEric Taylor     struct mnttab *entry)
851ebedde84SEric Taylor {
852ebedde84SEric Taylor 	mnttab_node_t find;
853ebedde84SEric Taylor 	mnttab_node_t *mtn;
854ebedde84SEric Taylor 
855b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
856b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
857b2634b9cSEric Taylor 
858b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
859b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
860b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
861b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
862b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
863b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
864b2634b9cSEric Taylor 			return (0);
865b2634b9cSEric Taylor 		else
866b2634b9cSEric Taylor 			return (ENOENT);
867b2634b9cSEric Taylor 	}
868b2634b9cSEric Taylor 
869ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
870b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
871ebedde84SEric Taylor 
872ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
873ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
874ebedde84SEric Taylor 	if (mtn) {
875ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
876ebedde84SEric Taylor 		return (0);
877ebedde84SEric Taylor 	}
878ebedde84SEric Taylor 	return (ENOENT);
879ebedde84SEric Taylor }
880ebedde84SEric Taylor 
881ebedde84SEric Taylor void
libzfs_mnttab_add(libzfs_handle_t * hdl,const char * special,const char * mountp,const char * mntopts)882ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
883ebedde84SEric Taylor     const char *mountp, const char *mntopts)
884ebedde84SEric Taylor {
885ebedde84SEric Taylor 	mnttab_node_t *mtn;
886ebedde84SEric Taylor 
887ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
888ebedde84SEric Taylor 		return;
889ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
890ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
891ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
892ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
893ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
894ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
895ebedde84SEric Taylor }
896ebedde84SEric Taylor 
897ebedde84SEric Taylor void
libzfs_mnttab_remove(libzfs_handle_t * hdl,const char * fsname)898ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
899ebedde84SEric Taylor {
900ebedde84SEric Taylor 	mnttab_node_t find;
901ebedde84SEric Taylor 	mnttab_node_t *ret;
902ebedde84SEric Taylor 
903ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
904ebedde84SEric Taylor 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
905ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
906ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
907ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
908ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
909ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
910ebedde84SEric Taylor 		free(ret);
911ebedde84SEric Taylor 	}
912ebedde84SEric Taylor }
913ebedde84SEric Taylor 
9147b97dc1aSrm160521 int
zfs_spa_version(zfs_handle_t * zhp,int * spa_version)9157b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9167b97dc1aSrm160521 {
91729ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9187b97dc1aSrm160521 
9197b97dc1aSrm160521 	if (zpool_handle == NULL)
9207b97dc1aSrm160521 		return (-1);
9217b97dc1aSrm160521 
9227b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
9237b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
9247b97dc1aSrm160521 	return (0);
9257b97dc1aSrm160521 }
9267b97dc1aSrm160521 
9277b97dc1aSrm160521 /*
9287b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
9297b97dc1aSrm160521  */
9307b97dc1aSrm160521 static int
zfs_which_resv_prop(zfs_handle_t * zhp,zfs_prop_t * resv_prop)9317b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9327b97dc1aSrm160521 {
9337b97dc1aSrm160521 	int spa_version;
9347b97dc1aSrm160521 
9357b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
9367b97dc1aSrm160521 		return (-1);
9377b97dc1aSrm160521 
9387b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
9397b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
9407b97dc1aSrm160521 	else
9417b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
9427b97dc1aSrm160521 
9437b97dc1aSrm160521 	return (0);
9447b97dc1aSrm160521 }
9457b97dc1aSrm160521 
946b1b8ab34Slling /*
947e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
948e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
949e9dbad6fSeschrock  * strings.
950fa9e4066Sahrens  */
9510a48a24eStimh nvlist_t *
zfs_valid_proplist(libzfs_handle_t * hdl,zfs_type_t type,nvlist_t * nvl,uint64_t zoned,zfs_handle_t * zhp,const char * errbuf)9520a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
953990b4856Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
954fa9e4066Sahrens {
955e9dbad6fSeschrock 	nvpair_t *elem;
956e9dbad6fSeschrock 	uint64_t intval;
957e9dbad6fSeschrock 	char *strval;
958990b4856Slling 	zfs_prop_t prop;
959e9dbad6fSeschrock 	nvlist_t *ret;
960da6c28aaSamw 	int chosen_normal = -1;
961da6c28aaSamw 	int chosen_utf = -1;
962990b4856Slling 
963e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
964e9dbad6fSeschrock 		(void) no_memory(hdl);
965e9dbad6fSeschrock 		return (NULL);
966e9dbad6fSeschrock 	}
967fa9e4066Sahrens 
96814843421SMatthew Ahrens 	/*
96914843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
97014843421SMatthew Ahrens 	 */
97114843421SMatthew Ahrens 
972e9dbad6fSeschrock 	elem = NULL;
973e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
974990b4856Slling 		const char *propname = nvpair_name(elem);
97599653d4eSeschrock 
97614843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
97714843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
978fa9e4066Sahrens 			/*
97914843421SMatthew Ahrens 			 * This is a user property: make sure it's a
980990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
981990b4856Slling 			 */
982990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
983990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984990b4856Slling 				    "'%s' must be a string"), propname);
985990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
986990b4856Slling 				goto error;
987990b4856Slling 			}
988990b4856Slling 
989990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
990e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991e9dbad6fSeschrock 				    "property name '%s' is too long"),
992e9dbad6fSeschrock 				    propname);
993990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
994e9dbad6fSeschrock 				goto error;
995e9dbad6fSeschrock 			}
996e9dbad6fSeschrock 
997e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
998e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
999e9dbad6fSeschrock 				(void) no_memory(hdl);
1000e9dbad6fSeschrock 				goto error;
1001e9dbad6fSeschrock 			}
1002e9dbad6fSeschrock 			continue;
1003e9dbad6fSeschrock 		}
1004fa9e4066Sahrens 
100514843421SMatthew Ahrens 		/*
100614843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
100714843421SMatthew Ahrens 		 * snapshots.
100814843421SMatthew Ahrens 		 */
1009bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
1010bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
1012bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1013bb0ade09Sahrens 			goto error;
1014bb0ade09Sahrens 		}
1015bb0ade09Sahrens 
101614843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
101714843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
101814843421SMatthew Ahrens 			char newpropname[128];
101914843421SMatthew Ahrens 			char domain[128];
102014843421SMatthew Ahrens 			uint64_t rid;
102114843421SMatthew Ahrens 			uint64_t valary[3];
102214843421SMatthew Ahrens 
102314843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
102414843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
102514843421SMatthew Ahrens 				zfs_error_aux(hdl,
102614843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
102714843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
102814843421SMatthew Ahrens 				    propname);
102914843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
103014843421SMatthew Ahrens 				goto error;
103114843421SMatthew Ahrens 			}
103214843421SMatthew Ahrens 
103314843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
103414843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
103514843421SMatthew Ahrens 				zfs_error_aux(hdl,
103614843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
103714843421SMatthew Ahrens 				    propname);
103814843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
103914843421SMatthew Ahrens 				    errbuf);
104014843421SMatthew Ahrens 				goto error;
104114843421SMatthew Ahrens 			}
104214843421SMatthew Ahrens 
104314843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
104414843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
104514843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
104614843421SMatthew Ahrens 					intval = 0;
104714843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
104814843421SMatthew Ahrens 				    strval, &intval) != 0) {
104914843421SMatthew Ahrens 					(void) zfs_error(hdl,
105014843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
105114843421SMatthew Ahrens 					goto error;
105214843421SMatthew Ahrens 				}
105314843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
105414843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
105514843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
105614843421SMatthew Ahrens 				if (intval == 0) {
105714843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105814843421SMatthew Ahrens 					    "use 'none' to disable "
105914843421SMatthew Ahrens 					    "userquota/groupquota"));
106014843421SMatthew Ahrens 					goto error;
106114843421SMatthew Ahrens 				}
106214843421SMatthew Ahrens 			} else {
106314843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106414843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
106514843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
106614843421SMatthew Ahrens 				goto error;
106714843421SMatthew Ahrens 			}
106814843421SMatthew Ahrens 
10692d5843dbSMatthew Ahrens 			/*
10702d5843dbSMatthew Ahrens 			 * Encode the prop name as
10712d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
10722d5843dbSMatthew Ahrens 			 * for the kernel to decode.
10732d5843dbSMatthew Ahrens 			 */
107414843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
10752d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10762d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
107714843421SMatthew Ahrens 			valary[0] = uqtype;
107814843421SMatthew Ahrens 			valary[1] = rid;
107914843421SMatthew Ahrens 			valary[2] = intval;
108014843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
108114843421SMatthew Ahrens 			    valary, 3) != 0) {
108214843421SMatthew Ahrens 				(void) no_memory(hdl);
108314843421SMatthew Ahrens 				goto error;
108414843421SMatthew Ahrens 			}
108514843421SMatthew Ahrens 			continue;
108619b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
108719b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
108819b94df9SMatthew Ahrens 			    "'%s' is readonly"),
108919b94df9SMatthew Ahrens 			    propname);
109019b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
109119b94df9SMatthew Ahrens 			goto error;
109214843421SMatthew Ahrens 		}
109314843421SMatthew Ahrens 
109414843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
109514843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109614843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
109714843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
109814843421SMatthew Ahrens 			goto error;
109914843421SMatthew Ahrens 		}
110014843421SMatthew Ahrens 
1101e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1102e9dbad6fSeschrock 			zfs_error_aux(hdl,
1103e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1104e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1105e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1106e9dbad6fSeschrock 			goto error;
1107e9dbad6fSeschrock 		}
1108e9dbad6fSeschrock 
1109e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1110da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
1111e9dbad6fSeschrock 			zfs_error_aux(hdl,
1112e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1113e9dbad6fSeschrock 			    propname);
1114e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1115e9dbad6fSeschrock 			goto error;
1116e9dbad6fSeschrock 		}
1117e9dbad6fSeschrock 
1118990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1119990b4856Slling 		    &strval, &intval, errbuf) != 0)
1120e9dbad6fSeschrock 			goto error;
1121e9dbad6fSeschrock 
1122e9dbad6fSeschrock 		/*
1123e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1124e9dbad6fSeschrock 		 */
1125e9dbad6fSeschrock 		switch (prop) {
1126e7437265Sahrens 		case ZFS_PROP_VERSION:
1127e7437265Sahrens 		{
1128e7437265Sahrens 			int version;
1129e7437265Sahrens 
1130e7437265Sahrens 			if (zhp == NULL)
1131e7437265Sahrens 				break;
1132e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1133e7437265Sahrens 			if (intval < version) {
1134e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1135e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1136e7437265Sahrens 				    version);
1137e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1138e7437265Sahrens 				goto error;
1139e7437265Sahrens 			}
1140e7437265Sahrens 			break;
1141e7437265Sahrens 		}
1142e7437265Sahrens 
1143e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1144b5152584SMatthew Ahrens 		case ZFS_PROP_RECORDSIZE:
1145b5152584SMatthew Ahrens 		{
1146b5152584SMatthew Ahrens 			int maxbs = SPA_MAXBLOCKSIZE;
1147b5152584SMatthew Ahrens 			if (zhp != NULL) {
1148b5152584SMatthew Ahrens 				maxbs = zpool_get_prop_int(zhp->zpool_hdl,
1149b5152584SMatthew Ahrens 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1150b5152584SMatthew Ahrens 			}
1151b5152584SMatthew Ahrens 			/*
1152b5152584SMatthew Ahrens 			 * Volumes are limited to a volblocksize of 128KB,
1153b5152584SMatthew Ahrens 			 * because they typically service workloads with
1154b5152584SMatthew Ahrens 			 * small random writes, which incur a large performance
1155b5152584SMatthew Ahrens 			 * penalty with large blocks.
1156b5152584SMatthew Ahrens 			 */
1157b5152584SMatthew Ahrens 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1158b5152584SMatthew Ahrens 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1159b5152584SMatthew Ahrens 			/*
1160b5152584SMatthew Ahrens 			 * The value must be a power of two between
1161b5152584SMatthew Ahrens 			 * SPA_MINBLOCKSIZE and maxbs.
1162b5152584SMatthew Ahrens 			 */
1163e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1164b5152584SMatthew Ahrens 			    intval > maxbs || !ISP2(intval)) {
1165e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1166b5152584SMatthew Ahrens 				    "'%s' must be power of 2 from 512B "
1167b5152584SMatthew Ahrens 				    "to %uKB"), propname, maxbs >> 10);
1168e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1169e9dbad6fSeschrock 				goto error;
1170e9dbad6fSeschrock 			}
1171e9dbad6fSeschrock 			break;
1172b5152584SMatthew Ahrens 		}
11734201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
11744201a95eSRic Aleshire 		{
11754201a95eSRic Aleshire 			/*
11764201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
11774201a95eSRic Aleshire 			 * internal hex label string.
11784201a95eSRic Aleshire 			 */
11794201a95eSRic Aleshire 
11804201a95eSRic Aleshire 			m_label_t *new_sl;
11814201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
11824201a95eSRic Aleshire 
11834201a95eSRic Aleshire 			/* Default value is already OK. */
11844201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
11854201a95eSRic Aleshire 				break;
11864201a95eSRic Aleshire 
11874201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
11884201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
11894201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
11904201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
11914201a95eSRic Aleshire 				goto badlabel;
11924201a95eSRic Aleshire 			}
11934201a95eSRic Aleshire 
11944201a95eSRic Aleshire 			/* Now translate to hex internal label string */
11954201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
11964201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
11974201a95eSRic Aleshire 				if (hex)
11984201a95eSRic Aleshire 					free(hex);
11994201a95eSRic Aleshire 				goto badlabel;
12004201a95eSRic Aleshire 			}
12014201a95eSRic Aleshire 			m_label_free(new_sl);
12024201a95eSRic Aleshire 
12034201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
12044201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
12054201a95eSRic Aleshire 				free(hex);
12064201a95eSRic Aleshire 				break;
12074201a95eSRic Aleshire 			}
12084201a95eSRic Aleshire 
12094201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1210569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
12114201a95eSRic Aleshire 			    DATA_TYPE_STRING);
12124201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12134201a95eSRic Aleshire 			    hex) == 0);
12144201a95eSRic Aleshire 			free(hex);
12154201a95eSRic Aleshire 
12164201a95eSRic Aleshire 			break;
12174201a95eSRic Aleshire 
12184201a95eSRic Aleshire badlabel:
12194201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12204201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
12214201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12224201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
12234201a95eSRic Aleshire 			goto error;
12244201a95eSRic Aleshire 
12254201a95eSRic Aleshire 		}
12264201a95eSRic Aleshire 
1227e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
122889eef05eSrm160521 		{
122989eef05eSrm160521 			namecheck_err_t why;
123089eef05eSrm160521 
1231e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1232e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1233e9dbad6fSeschrock 				break;
1234e9dbad6fSeschrock 
123589eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
123689eef05eSrm160521 				switch (why) {
123789eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
123889eef05eSrm160521 					zfs_error_aux(hdl,
123989eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1240e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1241e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
124289eef05eSrm160521 					break;
124389eef05eSrm160521 				case NAME_ERR_TOOLONG:
124489eef05eSrm160521 					zfs_error_aux(hdl,
124589eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
124689eef05eSrm160521 					    "component of '%s' is too long"),
124789eef05eSrm160521 					    propname);
124889eef05eSrm160521 					break;
124989eef05eSrm160521 				}
1250e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1251e9dbad6fSeschrock 				goto error;
1252e9dbad6fSeschrock 			}
125389eef05eSrm160521 		}
125489eef05eSrm160521 
1255f3861e1aSahl 			/*FALLTHRU*/
1256e9dbad6fSeschrock 
1257da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1258f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1259e9dbad6fSeschrock 			/*
1260da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1261da6c28aaSamw 			 * properties, check if it can be set in a
1262da6c28aaSamw 			 * global/non-global zone based on
1263f3861e1aSahl 			 * the zoned property value:
1264fa9e4066Sahrens 			 *
1265fa9e4066Sahrens 			 *		global zone	    non-global zone
1266f3861e1aSahl 			 * --------------------------------------------------
1267fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1268fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1269da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1270fa9e4066Sahrens 			 *
1271fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1272fa9e4066Sahrens 			 *		sharenfs (yes)
1273da6c28aaSamw 			 *		sharesmb (yes)
1274fa9e4066Sahrens 			 */
1275e9dbad6fSeschrock 			if (zoned) {
1276fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
127799653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1278e9dbad6fSeschrock 					    "'%s' cannot be set on "
1279e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1280e9dbad6fSeschrock 					    propname);
1281e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1282e9dbad6fSeschrock 					    errbuf);
1283e9dbad6fSeschrock 					goto error;
1284da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1285da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
128699653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1287e9dbad6fSeschrock 					    "'%s' cannot be set in "
1288e9dbad6fSeschrock 					    "a non-global zone"), propname);
1289e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1290e9dbad6fSeschrock 					    errbuf);
1291e9dbad6fSeschrock 					goto error;
1292fa9e4066Sahrens 				}
1293fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1294fa9e4066Sahrens 				/*
1295fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
129614843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1297fa9e4066Sahrens 				 */
129899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1299e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1300e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1301e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1302e9dbad6fSeschrock 				goto error;
1303fa9e4066Sahrens 			}
1304f3861e1aSahl 
130567331909Sdougm 			/*
130667331909Sdougm 			 * At this point, it is legitimate to set the
130767331909Sdougm 			 * property. Now we want to make sure that the
130867331909Sdougm 			 * property value is valid if it is sharenfs.
130967331909Sdougm 			 */
1310da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1311da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
131267331909Sdougm 			    strcmp(strval, "on") != 0 &&
131367331909Sdougm 			    strcmp(strval, "off") != 0) {
1314da6c28aaSamw 				zfs_share_proto_t proto;
1315da6c28aaSamw 
1316da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1317da6c28aaSamw 					proto = PROTO_SMB;
1318da6c28aaSamw 				else
1319da6c28aaSamw 					proto = PROTO_NFS;
132067331909Sdougm 
132167331909Sdougm 				/*
1322da6c28aaSamw 				 * Must be an valid sharing protocol
1323da6c28aaSamw 				 * option string so init the libshare
1324da6c28aaSamw 				 * in order to enable the parser and
1325da6c28aaSamw 				 * then parse the options. We use the
1326da6c28aaSamw 				 * control API since we don't care about
1327da6c28aaSamw 				 * the current configuration and don't
132867331909Sdougm 				 * want the overhead of loading it
132967331909Sdougm 				 * until we actually do something.
133067331909Sdougm 				 */
133167331909Sdougm 
133267331909Sdougm 				if (zfs_init_libshare(hdl,
133367331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1334fac3008cSeschrock 					/*
1335fac3008cSeschrock 					 * An error occurred so we can't do
1336fac3008cSeschrock 					 * anything
1337fac3008cSeschrock 					 */
133867331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133967331909Sdougm 					    "'%s' cannot be set: problem "
134067331909Sdougm 					    "in share initialization"),
134167331909Sdougm 					    propname);
1342fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1343fac3008cSeschrock 					    errbuf);
134467331909Sdougm 					goto error;
134567331909Sdougm 				}
134667331909Sdougm 
1347da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
134867331909Sdougm 					/*
134967331909Sdougm 					 * There was an error in parsing so
135067331909Sdougm 					 * deal with it by issuing an error
135167331909Sdougm 					 * message and leaving after
135267331909Sdougm 					 * uninitializing the the libshare
135367331909Sdougm 					 * interface.
135467331909Sdougm 					 */
135567331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135667331909Sdougm 					    "'%s' cannot be set to invalid "
135767331909Sdougm 					    "options"), propname);
1358fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1359fac3008cSeschrock 					    errbuf);
136067331909Sdougm 					zfs_uninit_libshare(hdl);
136167331909Sdougm 					goto error;
136267331909Sdougm 				}
136367331909Sdougm 				zfs_uninit_libshare(hdl);
136467331909Sdougm 			}
136567331909Sdougm 
1366f3861e1aSahl 			break;
1367da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1368da6c28aaSamw 			chosen_utf = (int)intval;
1369da6c28aaSamw 			break;
1370da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1371da6c28aaSamw 			chosen_normal = (int)intval;
1372da6c28aaSamw 			break;
1373fa9e4066Sahrens 		}
1374fa9e4066Sahrens 
1375e9dbad6fSeschrock 		/*
1376e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1377e9dbad6fSeschrock 		 * checks to enforce.
1378e9dbad6fSeschrock 		 */
1379e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1380e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1381e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1382e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1383e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1384e9dbad6fSeschrock 			char buf[64];
1385e9dbad6fSeschrock 
1386e9dbad6fSeschrock 			switch (prop) {
1387e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1388a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
1389e9dbad6fSeschrock 				if (intval > volsize) {
1390e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1391e9dbad6fSeschrock 					    "'%s' is greater than current "
1392e9dbad6fSeschrock 					    "volume size"), propname);
1393e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1394e9dbad6fSeschrock 					    errbuf);
1395e9dbad6fSeschrock 					goto error;
1396e9dbad6fSeschrock 				}
1397e9dbad6fSeschrock 				break;
1398e9dbad6fSeschrock 
1399e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1400e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1401e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1402e9dbad6fSeschrock 					    sizeof (buf));
1403e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1404e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1405e9dbad6fSeschrock 					    "volume block size (%s)"),
1406e9dbad6fSeschrock 					    propname, buf);
1407e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1408e9dbad6fSeschrock 					    errbuf);
1409e9dbad6fSeschrock 					goto error;
1410e9dbad6fSeschrock 				}
1411e9dbad6fSeschrock 
1412e9dbad6fSeschrock 				if (intval == 0) {
1413e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1414e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1415e9dbad6fSeschrock 					    propname);
1416e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1417e9dbad6fSeschrock 					    errbuf);
1418e9dbad6fSeschrock 					goto error;
1419e9dbad6fSeschrock 				}
1420f3861e1aSahl 				break;
1421e9dbad6fSeschrock 			}
1422e9dbad6fSeschrock 		}
1423e9dbad6fSeschrock 	}
1424e9dbad6fSeschrock 
1425e9dbad6fSeschrock 	/*
1426da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1427da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1428da6c28aaSamw 	 *
1429da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1430da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1431da6c28aaSamw 	 */
1432de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1433da6c28aaSamw 		if (nvlist_add_uint64(ret,
1434da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1435da6c28aaSamw 			(void) no_memory(hdl);
1436da6c28aaSamw 			goto error;
1437da6c28aaSamw 		}
1438de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1439da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1440da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1441da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1442da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1443da6c28aaSamw 		goto error;
1444da6c28aaSamw 	}
1445e9dbad6fSeschrock 	return (ret);
1446e9dbad6fSeschrock 
1447e9dbad6fSeschrock error:
1448e9dbad6fSeschrock 	nvlist_free(ret);
1449e9dbad6fSeschrock 	return (NULL);
1450e9dbad6fSeschrock }
1451e9dbad6fSeschrock 
145236db6475SEric Taylor int
zfs_add_synthetic_resv(zfs_handle_t * zhp,nvlist_t * nvl)145336db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
145436db6475SEric Taylor {
145536db6475SEric Taylor 	uint64_t old_volsize;
145636db6475SEric Taylor 	uint64_t new_volsize;
145736db6475SEric Taylor 	uint64_t old_reservation;
145836db6475SEric Taylor 	uint64_t new_reservation;
145936db6475SEric Taylor 	zfs_prop_t resv_prop;
1460c61ea566SGeorge Wilson 	nvlist_t *props;
146136db6475SEric Taylor 
146236db6475SEric Taylor 	/*
146336db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
146436db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
146536db6475SEric Taylor 	 */
146636db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
146736db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
146836db6475SEric Taylor 		return (-1);
146936db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1470c61ea566SGeorge Wilson 
1471c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1472c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1473c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1474c61ea566SGeorge Wilson 
1475c61ea566SGeorge Wilson 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1476c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1477c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1478c61ea566SGeorge Wilson 		fnvlist_free(props);
147936db6475SEric Taylor 		return (0);
148036db6475SEric Taylor 	}
148136db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1482c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1483c61ea566SGeorge Wilson 		fnvlist_free(props);
148436db6475SEric Taylor 		return (-1);
1485c61ea566SGeorge Wilson 	}
1486c61ea566SGeorge Wilson 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1487c61ea566SGeorge Wilson 	fnvlist_free(props);
1488c61ea566SGeorge Wilson 
148936db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
149036db6475SEric Taylor 	    new_reservation) != 0) {
149136db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
149236db6475SEric Taylor 		return (-1);
149336db6475SEric Taylor 	}
149436db6475SEric Taylor 	return (1);
149536db6475SEric Taylor }
149636db6475SEric Taylor 
149792241e0bSTom Erickson void
zfs_setprop_error(libzfs_handle_t * hdl,zfs_prop_t prop,int err,char * errbuf)149892241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
149992241e0bSTom Erickson     char *errbuf)
150092241e0bSTom Erickson {
150192241e0bSTom Erickson 	switch (err) {
150292241e0bSTom Erickson 
150392241e0bSTom Erickson 	case ENOSPC:
150492241e0bSTom Erickson 		/*
150592241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
150692241e0bSTom Erickson 		 * something different; setting a quota or reservation
150792241e0bSTom Erickson 		 * doesn't use any disk space.
150892241e0bSTom Erickson 		 */
150992241e0bSTom Erickson 		switch (prop) {
151092241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
151192241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
151292241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151392241e0bSTom Erickson 			    "size is less than current used or "
151492241e0bSTom Erickson 			    "reserved space"));
151592241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
151692241e0bSTom Erickson 			break;
151792241e0bSTom Erickson 
151892241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
151992241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
152092241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152192241e0bSTom Erickson 			    "size is greater than available space"));
152292241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
152392241e0bSTom Erickson 			break;
152492241e0bSTom Erickson 
152592241e0bSTom Erickson 		default:
152692241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
152792241e0bSTom Erickson 			break;
152892241e0bSTom Erickson 		}
152992241e0bSTom Erickson 		break;
153092241e0bSTom Erickson 
153192241e0bSTom Erickson 	case EBUSY:
153292241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
153392241e0bSTom Erickson 		break;
153492241e0bSTom Erickson 
153592241e0bSTom Erickson 	case EROFS:
153692241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
153792241e0bSTom Erickson 		break;
153892241e0bSTom Erickson 
15396fdcb3d1SWill Andrews 	case E2BIG:
15406fdcb3d1SWill Andrews 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15416fdcb3d1SWill Andrews 		    "property value too long"));
15426fdcb3d1SWill Andrews 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
15436fdcb3d1SWill Andrews 		break;
15446fdcb3d1SWill Andrews 
154592241e0bSTom Erickson 	case ENOTSUP:
154692241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
154792241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
154892241e0bSTom Erickson 		    "property or value"));
154992241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
155092241e0bSTom Erickson 		break;
155192241e0bSTom Erickson 
155292241e0bSTom Erickson 	case ERANGE:
1553b5152584SMatthew Ahrens 		if (prop == ZFS_PROP_COMPRESSION ||
1554b5152584SMatthew Ahrens 		    prop == ZFS_PROP_RECORDSIZE) {
155592241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155692241e0bSTom Erickson 			    "property setting is not allowed on "
155792241e0bSTom Erickson 			    "bootable datasets"));
155892241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
155992241e0bSTom Erickson 		} else {
156092241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
156192241e0bSTom Erickson 		}
156292241e0bSTom Erickson 		break;
156392241e0bSTom Erickson 
1564ab003da8SJim Dunham 	case EINVAL:
1565ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1566ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1567ab003da8SJim Dunham 		} else {
1568ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1569ab003da8SJim Dunham 		}
1570ab003da8SJim Dunham 		break;
1571ab003da8SJim Dunham 
157292241e0bSTom Erickson 	case EOVERFLOW:
157392241e0bSTom Erickson 		/*
157492241e0bSTom Erickson 		 * This platform can't address a volume this big.
157592241e0bSTom Erickson 		 */
157692241e0bSTom Erickson #ifdef _ILP32
157792241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
157892241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
157992241e0bSTom Erickson 			break;
158092241e0bSTom Erickson 		}
158192241e0bSTom Erickson #endif
158292241e0bSTom Erickson 		/* FALLTHROUGH */
158392241e0bSTom Erickson 	default:
158492241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
158592241e0bSTom Erickson 	}
158692241e0bSTom Erickson }
158792241e0bSTom Erickson 
1588e9dbad6fSeschrock /*
1589e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1590e9dbad6fSeschrock  */
1591e9dbad6fSeschrock int
zfs_prop_set(zfs_handle_t * zhp,const char * propname,const char * propval)1592e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1593e9dbad6fSeschrock {
1594e9dbad6fSeschrock 	int ret = -1;
1595e9dbad6fSeschrock 	char errbuf[1024];
1596e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
159730925561SChris Williamson 	nvlist_t *nvl = NULL;
1598e9dbad6fSeschrock 
1599e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1600e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1601e9dbad6fSeschrock 	    zhp->zfs_name);
1602e9dbad6fSeschrock 
1603e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1604e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1605e9dbad6fSeschrock 		(void) no_memory(hdl);
1606e9dbad6fSeschrock 		goto error;
1607e9dbad6fSeschrock 	}
1608e9dbad6fSeschrock 
160930925561SChris Williamson 	ret = zfs_prop_set_list(zhp, nvl);
161030925561SChris Williamson 
161130925561SChris Williamson error:
161230925561SChris Williamson 	nvlist_free(nvl);
161330925561SChris Williamson 	return (ret);
161430925561SChris Williamson }
161530925561SChris Williamson 
161630925561SChris Williamson 
161730925561SChris Williamson 
161830925561SChris Williamson /*
161930925561SChris Williamson  * Given an nvlist of property names and values, set the properties for the
162030925561SChris Williamson  * given dataset.
162130925561SChris Williamson  */
162230925561SChris Williamson int
zfs_prop_set_list(zfs_handle_t * zhp,nvlist_t * props)162330925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
162430925561SChris Williamson {
162530925561SChris Williamson 	zfs_cmd_t zc = { 0 };
162630925561SChris Williamson 	int ret = -1;
162730925561SChris Williamson 	prop_changelist_t **cls = NULL;
162830925561SChris Williamson 	int cl_idx;
162930925561SChris Williamson 	char errbuf[1024];
163030925561SChris Williamson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
163130925561SChris Williamson 	nvlist_t *nvl;
163230925561SChris Williamson 	int nvl_len;
163330925561SChris Williamson 	int added_resv;
163430925561SChris Williamson 
163530925561SChris Williamson 	(void) snprintf(errbuf, sizeof (errbuf),
163630925561SChris Williamson 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
163730925561SChris Williamson 	    zhp->zfs_name);
163830925561SChris Williamson 
163930925561SChris Williamson 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1640e9dbad6fSeschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1641e9dbad6fSeschrock 		goto error;
1642990b4856Slling 
164330925561SChris Williamson 	/*
164430925561SChris Williamson 	 * We have to check for any extra properties which need to be added
164530925561SChris Williamson 	 * before computing the length of the nvlist.
164630925561SChris Williamson 	 */
164730925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
164830925561SChris Williamson 	    elem != NULL;
164930925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
165030925561SChris Williamson 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
165130925561SChris Williamson 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
165230925561SChris Williamson 			goto error;
165330925561SChris Williamson 		}
165430925561SChris Williamson 	}
165530925561SChris Williamson 	/*
165630925561SChris Williamson 	 * Check how many properties we're setting and allocate an array to
165730925561SChris Williamson 	 * store changelist pointers for postfix().
165830925561SChris Williamson 	 */
165930925561SChris Williamson 	nvl_len = 0;
166030925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
166130925561SChris Williamson 	    elem != NULL;
166230925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem))
166330925561SChris Williamson 		nvl_len++;
166430925561SChris Williamson 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
166530925561SChris Williamson 		goto error;
1666e9dbad6fSeschrock 
166730925561SChris Williamson 	cl_idx = 0;
166830925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
166930925561SChris Williamson 	    elem != NULL;
167030925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
1671e9dbad6fSeschrock 
167230925561SChris Williamson 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
167330925561SChris Williamson 
167430925561SChris Williamson 		assert(cl_idx < nvl_len);
167530925561SChris Williamson 		/*
167630925561SChris Williamson 		 * We don't want to unmount & remount the dataset when changing
167730925561SChris Williamson 		 * its canmount property to 'on' or 'noauto'.  We only use
167830925561SChris Williamson 		 * the changelist logic to unmount when setting canmount=off.
167930925561SChris Williamson 		 */
168030925561SChris Williamson 		if (!(prop == ZFS_PROP_CANMOUNT &&
168130925561SChris Williamson 		    fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) {
168230925561SChris Williamson 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
168330925561SChris Williamson 			if (cls[cl_idx] == NULL)
168436db6475SEric Taylor 				goto error;
168536db6475SEric Taylor 		}
168636db6475SEric Taylor 
168730925561SChris Williamson 		if (prop == ZFS_PROP_MOUNTPOINT &&
168830925561SChris Williamson 		    changelist_haszonedchild(cls[cl_idx])) {
168999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1690fa9e4066Sahrens 			    "child dataset with inherited mountpoint is used "
169199653d4eSeschrock 			    "in a non-global zone"));
169299653d4eSeschrock 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1693fa9e4066Sahrens 			goto error;
1694fa9e4066Sahrens 		}
1695fa9e4066Sahrens 
169630925561SChris Williamson 		if (cls[cl_idx] != NULL &&
169730925561SChris Williamson 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1698fa9e4066Sahrens 			goto error;
1699fa9e4066Sahrens 
170030925561SChris Williamson 		cl_idx++;
170130925561SChris Williamson 	}
170230925561SChris Williamson 	assert(cl_idx == nvl_len);
170330925561SChris Williamson 
1704fa9e4066Sahrens 	/*
170530925561SChris Williamson 	 * Execute the corresponding ioctl() to set this list of properties.
1706fa9e4066Sahrens 	 */
1707fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1708fa9e4066Sahrens 
170930925561SChris Williamson 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
171030925561SChris Williamson 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1711e9dbad6fSeschrock 		goto error;
1712e9dbad6fSeschrock 
1713ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1714743a77edSAlan Wright 
1715fa9e4066Sahrens 	if (ret != 0) {
171630925561SChris Williamson 		/* Get the list of unset properties back and report them. */
171730925561SChris Williamson 		nvlist_t *errorprops = NULL;
171830925561SChris Williamson 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
171930925561SChris Williamson 			goto error;
172030925561SChris Williamson 		for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
172130925561SChris Williamson 		    elem != NULL;
172230925561SChris Williamson 		    elem = nvlist_next_nvpair(nvl, elem)) {
172330925561SChris Williamson 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
172492241e0bSTom Erickson 			zfs_setprop_error(hdl, prop, errno, errbuf);
172530925561SChris Williamson 		}
172630925561SChris Williamson 		nvlist_free(errorprops);
172730925561SChris Williamson 
172836db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
172936db6475SEric Taylor 			/* clean up the volsize property we tried to set */
173036db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
173136db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
173236db6475SEric Taylor 			nvlist_free(nvl);
173330925561SChris Williamson 			nvl = NULL;
173436db6475SEric Taylor 			zcmd_free_nvlists(&zc);
173530925561SChris Williamson 
173636db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
173736db6475SEric Taylor 				goto error;
173836db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
173936db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
174036db6475SEric Taylor 			    old_volsize) != 0)
174136db6475SEric Taylor 				goto error;
174236db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
174336db6475SEric Taylor 				goto error;
174436db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
174536db6475SEric Taylor 		}
1746fa9e4066Sahrens 	} else {
174730925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
174830925561SChris Williamson 			if (cls[cl_idx] != NULL) {
174930925561SChris Williamson 				int clp_err = changelist_postfix(cls[cl_idx]);
175030925561SChris Williamson 				if (clp_err != 0)
175130925561SChris Williamson 					ret = clp_err;
175230925561SChris Williamson 			}
175330925561SChris Williamson 		}
1754a227b7f4Shs24103 
1755fa9e4066Sahrens 		/*
1756fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1757fa9e4066Sahrens 		 * is reflected.
1758fa9e4066Sahrens 		 */
1759a227b7f4Shs24103 		if (ret == 0)
1760fa9e4066Sahrens 			(void) get_stats(zhp);
1761fa9e4066Sahrens 	}
1762fa9e4066Sahrens 
1763fa9e4066Sahrens error:
1764e9dbad6fSeschrock 	nvlist_free(nvl);
1765e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
176630925561SChris Williamson 	if (cls != NULL) {
176730925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
176830925561SChris Williamson 			if (cls[cl_idx] != NULL)
176930925561SChris Williamson 				changelist_free(cls[cl_idx]);
177030925561SChris Williamson 		}
177130925561SChris Williamson 		free(cls);
177230925561SChris Williamson 	}
1773fa9e4066Sahrens 	return (ret);
1774fa9e4066Sahrens }
1775fa9e4066Sahrens 
1776fa9e4066Sahrens /*
177792241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
177892241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1779fa9e4066Sahrens  */
1780fa9e4066Sahrens int
zfs_prop_inherit(zfs_handle_t * zhp,const char * propname,boolean_t received)178192241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1782fa9e4066Sahrens {
1783fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1784fa9e4066Sahrens 	int ret;
1785fa9e4066Sahrens 	prop_changelist_t *cl;
178699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
178799653d4eSeschrock 	char errbuf[1024];
1788e9dbad6fSeschrock 	zfs_prop_t prop;
178999653d4eSeschrock 
179099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
179199653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1792fa9e4066Sahrens 
179392241e0bSTom Erickson 	zc.zc_cookie = received;
1794990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1795e9dbad6fSeschrock 		/*
1796e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1797e9dbad6fSeschrock 		 * small, so just do it here.
1798e9dbad6fSeschrock 		 */
1799e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1800e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1801e9dbad6fSeschrock 			    "invalid property"));
1802e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1803e9dbad6fSeschrock 		}
1804e9dbad6fSeschrock 
1805e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1806e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1807e9dbad6fSeschrock 
1808e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1809e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1810e9dbad6fSeschrock 
1811e9dbad6fSeschrock 		return (0);
1812e9dbad6fSeschrock 	}
1813e9dbad6fSeschrock 
1814fa9e4066Sahrens 	/*
1815fa9e4066Sahrens 	 * Verify that this property is inheritable.
1816fa9e4066Sahrens 	 */
181799653d4eSeschrock 	if (zfs_prop_readonly(prop))
181899653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1819fa9e4066Sahrens 
182092241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
182199653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1822fa9e4066Sahrens 
1823fa9e4066Sahrens 	/*
1824fa9e4066Sahrens 	 * Check to see if the value applies to this type
1825fa9e4066Sahrens 	 */
182699653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
182799653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1828fa9e4066Sahrens 
1829bf7c2d40Srm160521 	/*
183036db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1831bf7c2d40Srm160521 	 */
1832bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1833fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1834e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1835fa9e4066Sahrens 
1836fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1837fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
183899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183999653d4eSeschrock 		    "dataset is used in a non-global zone"));
184099653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1841fa9e4066Sahrens 	}
1842fa9e4066Sahrens 
1843fa9e4066Sahrens 	/*
1844fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1845fa9e4066Sahrens 	 */
18460069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1847fa9e4066Sahrens 		return (-1);
1848fa9e4066Sahrens 
1849fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
185099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185199653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
185299653d4eSeschrock 		    "in a non-global zone"));
185399653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1854fa9e4066Sahrens 		goto error;
1855fa9e4066Sahrens 	}
1856fa9e4066Sahrens 
1857fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1858fa9e4066Sahrens 		goto error;
1859fa9e4066Sahrens 
1860e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
186199653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1862fa9e4066Sahrens 	} else {
1863fa9e4066Sahrens 
1864efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1865fa9e4066Sahrens 			goto error;
1866fa9e4066Sahrens 
1867fa9e4066Sahrens 		/*
1868fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1869fa9e4066Sahrens 		 */
1870fa9e4066Sahrens 		(void) get_stats(zhp);
1871fa9e4066Sahrens 	}
1872fa9e4066Sahrens 
1873fa9e4066Sahrens error:
1874fa9e4066Sahrens 	changelist_free(cl);
1875fa9e4066Sahrens 	return (ret);
1876fa9e4066Sahrens }
1877fa9e4066Sahrens 
1878fa9e4066Sahrens /*
18797f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
18807f7322feSeschrock  * extract them appropriately.
18817f7322feSeschrock  */
18827f7322feSeschrock static uint64_t
getprop_uint64(zfs_handle_t * zhp,zfs_prop_t prop,char ** source)18837f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18847f7322feSeschrock {
18857f7322feSeschrock 	nvlist_t *nv;
18867f7322feSeschrock 	uint64_t value;
18877f7322feSeschrock 
1888a2eea2e1Sahrens 	*source = NULL;
18897f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
18907f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1891990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1892990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
18937f7322feSeschrock 	} else {
18942e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
18952e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
18967f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
18977f7322feSeschrock 		*source = "";
18987f7322feSeschrock 	}
18997f7322feSeschrock 
19007f7322feSeschrock 	return (value);
19017f7322feSeschrock }
19027f7322feSeschrock 
1903c1a50c7eSMatthew Ahrens static const char *
getprop_string(zfs_handle_t * zhp,zfs_prop_t prop,char ** source)19047f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19057f7322feSeschrock {
19067f7322feSeschrock 	nvlist_t *nv;
1907c1a50c7eSMatthew Ahrens 	const char *value;
19087f7322feSeschrock 
1909a2eea2e1Sahrens 	*source = NULL;
19107f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19117f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1912c1a50c7eSMatthew Ahrens 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1913990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19147f7322feSeschrock 	} else {
19152e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
19162e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
1917c1a50c7eSMatthew Ahrens 		value = zfs_prop_default_string(prop);
19187f7322feSeschrock 		*source = "";
19197f7322feSeschrock 	}
19207f7322feSeschrock 
19217f7322feSeschrock 	return (value);
19227f7322feSeschrock }
19237f7322feSeschrock 
192492241e0bSTom Erickson static boolean_t
zfs_is_recvd_props_mode(zfs_handle_t * zhp)192592241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
192692241e0bSTom Erickson {
192792241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
192892241e0bSTom Erickson }
192992241e0bSTom Erickson 
193092241e0bSTom Erickson static void
zfs_set_recvd_props_mode(zfs_handle_t * zhp,uint64_t * cookie)193192241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
193292241e0bSTom Erickson {
193392241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
193492241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
193592241e0bSTom Erickson }
193692241e0bSTom Erickson 
193792241e0bSTom Erickson static void
zfs_unset_recvd_props_mode(zfs_handle_t * zhp,uint64_t * cookie)193892241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
193992241e0bSTom Erickson {
194092241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
194192241e0bSTom Erickson 	*cookie = 0;
194292241e0bSTom Erickson }
194392241e0bSTom Erickson 
19447f7322feSeschrock /*
1945fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1946fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1947fa9e4066Sahrens  *
1948fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1949fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1950fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1951fa9e4066Sahrens  * the source "temporary".
1952fa9e4066Sahrens  */
195399653d4eSeschrock static int
get_numeric_property(zfs_handle_t * zhp,zfs_prop_t prop,zprop_source_t * src,char ** source,uint64_t * val)1954990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
195599653d4eSeschrock     char **source, uint64_t *val)
1956fa9e4066Sahrens {
1957bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
195896510749Stimh 	nvlist_t *zplprops = NULL;
1959fa9e4066Sahrens 	struct mnttab mnt;
19603ccfa83cSahrens 	char *mntopt_on = NULL;
19613ccfa83cSahrens 	char *mntopt_off = NULL;
196292241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1963fa9e4066Sahrens 
1964fa9e4066Sahrens 	*source = NULL;
1965fa9e4066Sahrens 
19663ccfa83cSahrens 	switch (prop) {
19673ccfa83cSahrens 	case ZFS_PROP_ATIME:
19683ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
19693ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
19703ccfa83cSahrens 		break;
19713ccfa83cSahrens 
19723ccfa83cSahrens 	case ZFS_PROP_DEVICES:
19733ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
19743ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
19753ccfa83cSahrens 		break;
19763ccfa83cSahrens 
19773ccfa83cSahrens 	case ZFS_PROP_EXEC:
19783ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
19793ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
19803ccfa83cSahrens 		break;
19813ccfa83cSahrens 
19823ccfa83cSahrens 	case ZFS_PROP_READONLY:
19833ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
19843ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
19853ccfa83cSahrens 		break;
19863ccfa83cSahrens 
19873ccfa83cSahrens 	case ZFS_PROP_SETUID:
19883ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
19893ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
19903ccfa83cSahrens 		break;
19913ccfa83cSahrens 
19923ccfa83cSahrens 	case ZFS_PROP_XATTR:
19933ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
19943ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
19953ccfa83cSahrens 		break;
1996da6c28aaSamw 
1997da6c28aaSamw 	case ZFS_PROP_NBMAND:
1998da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
1999da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2000da6c28aaSamw 		break;
20013ccfa83cSahrens 	}
20023ccfa83cSahrens 
20033bb79becSeschrock 	/*
20043bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
20053bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
20063bb79becSeschrock 	 * we're looking up a property which requires its presence.
20073bb79becSeschrock 	 */
20083bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
20093ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2010ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2011ebedde84SEric Taylor 		struct mnttab entry;
20123bb79becSeschrock 
2013ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2014ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
20153ccfa83cSahrens 			    entry.mnt_mntopts);
20163ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
20173bb79becSeschrock 				return (-1);
20183ccfa83cSahrens 		}
20193bb79becSeschrock 
20203bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
20213bb79becSeschrock 	}
20223bb79becSeschrock 
2023fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2024fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2025fa9e4066Sahrens 	else
2026fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2027fa9e4066Sahrens 
2028fa9e4066Sahrens 	switch (prop) {
2029fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2030fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2031fa9e4066Sahrens 	case ZFS_PROP_EXEC:
20323ccfa83cSahrens 	case ZFS_PROP_READONLY:
20333ccfa83cSahrens 	case ZFS_PROP_SETUID:
20343ccfa83cSahrens 	case ZFS_PROP_XATTR:
2035da6c28aaSamw 	case ZFS_PROP_NBMAND:
203699653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2037fa9e4066Sahrens 
203892241e0bSTom Erickson 		if (received)
203992241e0bSTom Erickson 			break;
204092241e0bSTom Erickson 
20413ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
204299653d4eSeschrock 			*val = B_TRUE;
2043fa9e4066Sahrens 			if (src)
2044990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
20453ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
204699653d4eSeschrock 			*val = B_FALSE;
2047fa9e4066Sahrens 			if (src)
2048990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2049fa9e4066Sahrens 		}
205099653d4eSeschrock 		break;
2051fa9e4066Sahrens 
20523ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
2053d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
2054fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2055a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2056fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2057a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2058a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2059a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2060a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2061a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2062a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
206392241e0bSTom Erickson 
206492241e0bSTom Erickson 		if (*source == NULL) {
206592241e0bSTom Erickson 			/* not default, must be local */
2066fa9e4066Sahrens 			*source = zhp->zfs_name;
206792241e0bSTom Erickson 		}
206899653d4eSeschrock 		break;
2069fa9e4066Sahrens 
2070fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
207199653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
207299653d4eSeschrock 		break;
2073fa9e4066Sahrens 
207439c23413Seschrock 	case ZFS_PROP_NUMCLONES:
207539c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
207639c23413Seschrock 		break;
207739c23413Seschrock 
2078bd00f61bSrm160521 	case ZFS_PROP_VERSION:
2079de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2080de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2081de8267e0Stimh 	case ZFS_PROP_CASE:
2082de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2083de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
20843d7934e1Srm160521 			return (-1);
2085bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2086de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2087de8267e0Stimh 			zcmd_free_nvlists(&zc);
2088f4b94bdeSMatthew Ahrens 			return (-1);
2089bd00f61bSrm160521 		}
2090de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2091de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2092de8267e0Stimh 		    val) != 0) {
2093de8267e0Stimh 			zcmd_free_nvlists(&zc);
2094f4b94bdeSMatthew Ahrens 			return (-1);
2095de8267e0Stimh 		}
209696510749Stimh 		nvlist_free(zplprops);
2097de8267e0Stimh 		zcmd_free_nvlists(&zc);
2098bd00f61bSrm160521 		break;
2099bd00f61bSrm160521 
2100ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
2101ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
2102ca48f36fSKeith M Wesolowski 		break;
2103ca48f36fSKeith M Wesolowski 
2104fa9e4066Sahrens 	default:
2105e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
210691ebeef5Sahrens 		case PROP_TYPE_NUMBER:
210791ebeef5Sahrens 		case PROP_TYPE_INDEX:
2108e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
210974e7dc98SMatthew Ahrens 			/*
211014843421SMatthew Ahrens 			 * If we tried to use a default value for a
211174e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
211231f572c2STom Erickson 			 * present.
211374e7dc98SMatthew Ahrens 			 */
211474e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
211531f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
211631f572c2STom Erickson 				*source = NULL;
211774e7dc98SMatthew Ahrens 			}
2118e7437265Sahrens 			break;
2119e7437265Sahrens 
212091ebeef5Sahrens 		case PROP_TYPE_STRING:
2121e7437265Sahrens 		default:
212299653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
212399653d4eSeschrock 			    "cannot get non-numeric property"));
212499653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
212599653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
2126fa9e4066Sahrens 		}
2127e7437265Sahrens 	}
2128fa9e4066Sahrens 
2129fa9e4066Sahrens 	return (0);
2130fa9e4066Sahrens }
2131fa9e4066Sahrens 
2132fa9e4066Sahrens /*
2133fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2134fa9e4066Sahrens  */
2135fa9e4066Sahrens static void
get_source(zfs_handle_t * zhp,zprop_source_t * srctype,char * source,char * statbuf,size_t statlen)2136990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2137fa9e4066Sahrens     char *statbuf, size_t statlen)
2138fa9e4066Sahrens {
2139990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2140fa9e4066Sahrens 		return;
2141fa9e4066Sahrens 
2142fa9e4066Sahrens 	if (source == NULL) {
2143990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2144fa9e4066Sahrens 	} else if (source[0] == '\0') {
2145990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
214692241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
214792241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
2148fa9e4066Sahrens 	} else {
2149fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2150990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2151fa9e4066Sahrens 		} else {
2152fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2153990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2154fa9e4066Sahrens 		}
2155fa9e4066Sahrens 	}
2156fa9e4066Sahrens 
2157fa9e4066Sahrens }
2158fa9e4066Sahrens 
215992241e0bSTom Erickson int
zfs_prop_get_recvd(zfs_handle_t * zhp,const char * propname,char * propbuf,size_t proplen,boolean_t literal)216092241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
216192241e0bSTom Erickson     size_t proplen, boolean_t literal)
216292241e0bSTom Erickson {
216392241e0bSTom Erickson 	zfs_prop_t prop;
216492241e0bSTom Erickson 	int err = 0;
216592241e0bSTom Erickson 
216692241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
216792241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
216892241e0bSTom Erickson 			return (-1);
216992241e0bSTom Erickson 
217092241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
217192241e0bSTom Erickson 
217292241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
217392241e0bSTom Erickson 		uint64_t cookie;
217492241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
217592241e0bSTom Erickson 			return (-1);
217692241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
217792241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
217892241e0bSTom Erickson 		    NULL, NULL, 0, literal);
217992241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
218092241e0bSTom Erickson 	} else {
218192241e0bSTom Erickson 		nvlist_t *propval;
218292241e0bSTom Erickson 		char *recvdval;
218392241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
218492241e0bSTom Erickson 		    propname, &propval) != 0)
218592241e0bSTom Erickson 			return (-1);
218692241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
218792241e0bSTom Erickson 		    &recvdval) == 0);
218892241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
218992241e0bSTom Erickson 	}
219092241e0bSTom Erickson 
219192241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
219292241e0bSTom Erickson }
219392241e0bSTom Erickson 
219419b94df9SMatthew Ahrens static int
get_clones_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)219519b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
219619b94df9SMatthew Ahrens {
219719b94df9SMatthew Ahrens 	nvlist_t *value;
219819b94df9SMatthew Ahrens 	nvpair_t *pair;
219919b94df9SMatthew Ahrens 
220019b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
220119b94df9SMatthew Ahrens 	if (value == NULL)
220219b94df9SMatthew Ahrens 		return (-1);
220319b94df9SMatthew Ahrens 
220419b94df9SMatthew Ahrens 	propbuf[0] = '\0';
220519b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
220619b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
220719b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
220819b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
220919b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
221019b94df9SMatthew Ahrens 	}
221119b94df9SMatthew Ahrens 
221219b94df9SMatthew Ahrens 	return (0);
221319b94df9SMatthew Ahrens }
221419b94df9SMatthew Ahrens 
221519b94df9SMatthew Ahrens struct get_clones_arg {
221619b94df9SMatthew Ahrens 	uint64_t numclones;
221719b94df9SMatthew Ahrens 	nvlist_t *value;
221819b94df9SMatthew Ahrens 	const char *origin;
2219*a1988827SMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
222019b94df9SMatthew Ahrens };
222119b94df9SMatthew Ahrens 
222219b94df9SMatthew Ahrens int
get_clones_cb(zfs_handle_t * zhp,void * arg)222319b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
222419b94df9SMatthew Ahrens {
222519b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
222619b94df9SMatthew Ahrens 
222719b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
222819b94df9SMatthew Ahrens 		zfs_close(zhp);
222919b94df9SMatthew Ahrens 		return (0);
223019b94df9SMatthew Ahrens 	}
223119b94df9SMatthew Ahrens 
223219b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
223319b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
223419b94df9SMatthew Ahrens 		goto out;
223519b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
22363b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
223719b94df9SMatthew Ahrens 		gca->numclones--;
223819b94df9SMatthew Ahrens 	}
223919b94df9SMatthew Ahrens 
224019b94df9SMatthew Ahrens out:
224119b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
224219b94df9SMatthew Ahrens 	zfs_close(zhp);
224319b94df9SMatthew Ahrens 	return (0);
224419b94df9SMatthew Ahrens }
224519b94df9SMatthew Ahrens 
224619b94df9SMatthew Ahrens nvlist_t *
zfs_get_clones_nvl(zfs_handle_t * zhp)224719b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
224819b94df9SMatthew Ahrens {
224919b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
225019b94df9SMatthew Ahrens 
225119b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
225219b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
225319b94df9SMatthew Ahrens 		struct get_clones_arg gca;
225419b94df9SMatthew Ahrens 
225519b94df9SMatthew Ahrens 		/*
225619b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
225719b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
225819b94df9SMatthew Ahrens 		 */
225919b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
226019b94df9SMatthew Ahrens 			return (NULL);
226119b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
226219b94df9SMatthew Ahrens 			return (NULL);
226319b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
226419b94df9SMatthew Ahrens 			nvlist_free(nv);
226519b94df9SMatthew Ahrens 			return (NULL);
226619b94df9SMatthew Ahrens 		}
226719b94df9SMatthew Ahrens 
226819b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
226919b94df9SMatthew Ahrens 		gca.value = value;
227019b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
227119b94df9SMatthew Ahrens 
227219b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
227319b94df9SMatthew Ahrens 			zfs_handle_t *root;
2274*a1988827SMatthew Ahrens 			char pool[ZFS_MAX_DATASET_NAME_LEN];
227519b94df9SMatthew Ahrens 			char *cp = pool;
227619b94df9SMatthew Ahrens 
227719b94df9SMatthew Ahrens 			/* get the pool name */
227819b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
227919b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
228019b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
228119b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
228219b94df9SMatthew Ahrens 
228319b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
228419b94df9SMatthew Ahrens 		}
228519b94df9SMatthew Ahrens 
228619b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
228719b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
228819b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
228919b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
229019b94df9SMatthew Ahrens 			nvlist_free(nv);
229119b94df9SMatthew Ahrens 			nvlist_free(value);
229219b94df9SMatthew Ahrens 			return (NULL);
229319b94df9SMatthew Ahrens 		}
229419b94df9SMatthew Ahrens 		nvlist_free(nv);
229519b94df9SMatthew Ahrens 		nvlist_free(value);
229619b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
229719b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
229819b94df9SMatthew Ahrens 	}
229919b94df9SMatthew Ahrens 
230019b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
230119b94df9SMatthew Ahrens 
230219b94df9SMatthew Ahrens 	return (value);
230319b94df9SMatthew Ahrens }
230419b94df9SMatthew Ahrens 
2305fa9e4066Sahrens /*
2306fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2307fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2308fa9e4066Sahrens  * human-readable form.
2309fa9e4066Sahrens  *
2310fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2311fa9e4066Sahrens  */
2312fa9e4066Sahrens int
zfs_prop_get(zfs_handle_t * zhp,zfs_prop_t prop,char * propbuf,size_t proplen,zprop_source_t * src,char * statbuf,size_t statlen,boolean_t literal)2313fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2314990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2315fa9e4066Sahrens {
2316fa9e4066Sahrens 	char *source = NULL;
2317fa9e4066Sahrens 	uint64_t val;
2318c1a50c7eSMatthew Ahrens 	const char *str;
2319e9dbad6fSeschrock 	const char *strval;
232092241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2321fa9e4066Sahrens 
2322fa9e4066Sahrens 	/*
2323fa9e4066Sahrens 	 * Check to see if this property applies to our object
2324fa9e4066Sahrens 	 */
2325fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2326fa9e4066Sahrens 		return (-1);
2327fa9e4066Sahrens 
232892241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
232992241e0bSTom Erickson 		return (-1);
233092241e0bSTom Erickson 
2331fa9e4066Sahrens 	if (src)
2332990b4856Slling 		*src = ZPROP_SRC_NONE;
2333fa9e4066Sahrens 
2334fa9e4066Sahrens 	switch (prop) {
2335fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2336fa9e4066Sahrens 		/*
2337fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2338fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2339fa9e4066Sahrens 		 */
2340fa9e4066Sahrens 		{
2341a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2342a2eea2e1Sahrens 			time_t time = (time_t)val;
2343fa9e4066Sahrens 			struct tm t;
2344fa9e4066Sahrens 
2345fa9e4066Sahrens 			if (literal ||
2346fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2347fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2348fa9e4066Sahrens 			    &t) == 0)
2349a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2350fa9e4066Sahrens 		}
2351fa9e4066Sahrens 		break;
2352fa9e4066Sahrens 
2353fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2354fa9e4066Sahrens 		/*
2355fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2356fa9e4066Sahrens 		 *
2357fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2358fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2359fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2360fa9e4066Sahrens 		 *
2361fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2362fa9e4066Sahrens 		 * root to any values we return.
2363fa9e4066Sahrens 		 */
236429ab75c9Srm160521 
23657f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2366fa9e4066Sahrens 
2367b21718f0Sgw25295 		if (str[0] == '/') {
236829ab75c9Srm160521 			char buf[MAXPATHLEN];
236929ab75c9Srm160521 			char *root = buf;
2370a79992aaSTom Erickson 			const char *relpath;
2371fa9e4066Sahrens 
2372a79992aaSTom Erickson 			/*
2373a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2374a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2375a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2376a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2377a79992aaSTom Erickson 			 * inherited.
2378a79992aaSTom Erickson 			 */
2379a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2380a79992aaSTom Erickson 				relpath = "";
2381a79992aaSTom Erickson 			} else {
2382a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2383fa9e4066Sahrens 				if (relpath[0] == '/')
2384fa9e4066Sahrens 					relpath++;
2385a79992aaSTom Erickson 			}
2386b21718f0Sgw25295 
238729ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
2388c58b3526SAdam Stevko 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2389c58b3526SAdam Stevko 			    B_FALSE)) || (strcmp(root, "-") == 0))
239029ab75c9Srm160521 				root[0] = '\0';
2391b21718f0Sgw25295 			/*
2392b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2393b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2394b21718f0Sgw25295 			 * mountpoint path.
2395b21718f0Sgw25295 			 */
2396b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2397b21718f0Sgw25295 				root++;
2398b21718f0Sgw25295 
2399b21718f0Sgw25295 			/*
2400b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2401b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2402b21718f0Sgw25295 			 * an inherited mountpoint.
2403b21718f0Sgw25295 			 */
2404b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2405b21718f0Sgw25295 			    relpath[0] != '\0'))
24067f7322feSeschrock 				str++;
2407fa9e4066Sahrens 
2408fa9e4066Sahrens 			if (relpath[0] == '\0')
2409fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
24107f7322feSeschrock 				    root, str);
2411fa9e4066Sahrens 			else
2412fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
24137f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2414fa9e4066Sahrens 				    relpath);
2415fa9e4066Sahrens 		} else {
2416fa9e4066Sahrens 			/* 'legacy' or 'none' */
24177f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2418fa9e4066Sahrens 		}
2419fa9e4066Sahrens 
2420fa9e4066Sahrens 		break;
2421fa9e4066Sahrens 
2422fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
2423c1a50c7eSMatthew Ahrens 		str = getprop_string(zhp, prop, &source);
2424c1a50c7eSMatthew Ahrens 		if (str == NULL)
2425fa9e4066Sahrens 			return (-1);
2426c1a50c7eSMatthew Ahrens 		(void) strlcpy(propbuf, str, proplen);
2427fa9e4066Sahrens 		break;
2428fa9e4066Sahrens 
242919b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
243019b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
243119b94df9SMatthew Ahrens 			return (-1);
243219b94df9SMatthew Ahrens 		break;
243319b94df9SMatthew Ahrens 
2434fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2435a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2436fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2437a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2438a9799022Sck153898 
243999653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
244099653d4eSeschrock 			return (-1);
2441fa9e4066Sahrens 
2442fa9e4066Sahrens 		/*
2443fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2444fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2445fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2446fa9e4066Sahrens 		 * that its set locally.
2447fa9e4066Sahrens 		 */
2448fa9e4066Sahrens 		if (val == 0) {
2449fa9e4066Sahrens 			if (literal)
2450fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2451fa9e4066Sahrens 			else
2452fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2453fa9e4066Sahrens 		} else {
2454fa9e4066Sahrens 			if (literal)
24555ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24565ad82045Snd150628 				    (u_longlong_t)val);
2457fa9e4066Sahrens 			else
2458fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2459fa9e4066Sahrens 		}
2460fa9e4066Sahrens 		break;
2461fa9e4066Sahrens 
2462a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2463a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2464a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2465a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2466a2afb611SJerry Jelinek 
2467a2afb611SJerry Jelinek 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2468a2afb611SJerry Jelinek 			return (-1);
2469a2afb611SJerry Jelinek 
2470a2afb611SJerry Jelinek 		/*
2471a2afb611SJerry Jelinek 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2472a2afb611SJerry Jelinek 		 * literal is set), and indicate that it's the default value.
2473a2afb611SJerry Jelinek 		 * Otherwise, we print the number nicely and indicate that it's
2474a2afb611SJerry Jelinek 		 * set locally.
2475a2afb611SJerry Jelinek 		 */
2476a2afb611SJerry Jelinek 		if (literal) {
2477a2afb611SJerry Jelinek 			(void) snprintf(propbuf, proplen, "%llu",
2478a2afb611SJerry Jelinek 			    (u_longlong_t)val);
2479a2afb611SJerry Jelinek 		} else if (val == UINT64_MAX) {
2480a2afb611SJerry Jelinek 			(void) strlcpy(propbuf, "none", proplen);
2481a2afb611SJerry Jelinek 		} else {
2482a2afb611SJerry Jelinek 			zfs_nicenum(val, propbuf, proplen);
2483a2afb611SJerry Jelinek 		}
2484a2afb611SJerry Jelinek 		break;
2485a2afb611SJerry Jelinek 
2486187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2487fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
248899653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
248999653d4eSeschrock 			return (-1);
2490b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2491b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2492b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2493fa9e4066Sahrens 		break;
2494fa9e4066Sahrens 
2495fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2496fa9e4066Sahrens 		switch (zhp->zfs_type) {
2497fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2498fa9e4066Sahrens 			str = "filesystem";
2499fa9e4066Sahrens 			break;
2500fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2501fa9e4066Sahrens 			str = "volume";
2502fa9e4066Sahrens 			break;
2503fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2504fa9e4066Sahrens 			str = "snapshot";
2505fa9e4066Sahrens 			break;
250678f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
250778f17100SMatthew Ahrens 			str = "bookmark";
250878f17100SMatthew Ahrens 			break;
2509fa9e4066Sahrens 		default:
251099653d4eSeschrock 			abort();
2511fa9e4066Sahrens 		}
2512fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2513fa9e4066Sahrens 		break;
2514fa9e4066Sahrens 
2515fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2516fa9e4066Sahrens 		/*
2517fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2518fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2519fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2520fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2521fa9e4066Sahrens 		 */
252299653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
252399653d4eSeschrock 		    src, &source, &val) != 0)
252499653d4eSeschrock 			return (-1);
252599653d4eSeschrock 		if (val)
2526fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2527fa9e4066Sahrens 		else
2528fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2529fa9e4066Sahrens 		break;
2530fa9e4066Sahrens 
2531fa9e4066Sahrens 	case ZFS_PROP_NAME:
2532fa9e4066Sahrens 		/*
2533fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2534fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2535fa9e4066Sahrens 		 * consumers.
2536fa9e4066Sahrens 		 */
2537fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2538fa9e4066Sahrens 		break;
2539fa9e4066Sahrens 
25404201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
25414201a95eSRic Aleshire 		{
25424201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
25434201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
25444201a95eSRic Aleshire 
25454201a95eSRic Aleshire 			(void) strlcpy(propbuf,
25464201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
25474201a95eSRic Aleshire 
25484201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
25494201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
25504201a95eSRic Aleshire 				break;
25514201a95eSRic Aleshire 
25524201a95eSRic Aleshire 			/*
25534201a95eSRic Aleshire 			 * Try to translate the internal hex string to
25544201a95eSRic Aleshire 			 * human-readable output.  If there are any
25554201a95eSRic Aleshire 			 * problems just use the hex string.
25564201a95eSRic Aleshire 			 */
25574201a95eSRic Aleshire 
25584201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
25594201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
25604201a95eSRic Aleshire 				m_label_free(new_sl);
25614201a95eSRic Aleshire 				break;
25624201a95eSRic Aleshire 			}
25634201a95eSRic Aleshire 
25644201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
25654201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
25664201a95eSRic Aleshire 				if (ascii)
25674201a95eSRic Aleshire 					free(ascii);
25684201a95eSRic Aleshire 				m_label_free(new_sl);
25694201a95eSRic Aleshire 				break;
25704201a95eSRic Aleshire 			}
25714201a95eSRic Aleshire 			m_label_free(new_sl);
25724201a95eSRic Aleshire 
25734201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
25744201a95eSRic Aleshire 			free(ascii);
25754201a95eSRic Aleshire 		}
25764201a95eSRic Aleshire 		break;
25774201a95eSRic Aleshire 
2578f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2579f0f3ef5aSGarrett D'Amore 		/*
2580f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2581f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2582f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2583f0f3ef5aSGarrett D'Amore 		 */
2584f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2585f0f3ef5aSGarrett D'Amore 			return (-1);
2586f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2587f0f3ef5aSGarrett D'Amore 		break;
2588f0f3ef5aSGarrett D'Amore 
2589fa9e4066Sahrens 	default:
2590e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
259191ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2592e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2593e7437265Sahrens 			    &source, &val) != 0)
2594e7437265Sahrens 				return (-1);
2595e7437265Sahrens 			if (literal)
2596e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2597e7437265Sahrens 				    (u_longlong_t)val);
2598e7437265Sahrens 			else
2599e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2600e7437265Sahrens 			break;
2601e7437265Sahrens 
260291ebeef5Sahrens 		case PROP_TYPE_STRING:
2603c1a50c7eSMatthew Ahrens 			str = getprop_string(zhp, prop, &source);
2604c1a50c7eSMatthew Ahrens 			if (str == NULL)
2605c1a50c7eSMatthew Ahrens 				return (-1);
2606c1a50c7eSMatthew Ahrens 			(void) strlcpy(propbuf, str, proplen);
2607e7437265Sahrens 			break;
2608e7437265Sahrens 
260991ebeef5Sahrens 		case PROP_TYPE_INDEX:
2610fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2611fe192f76Sahrens 			    &source, &val) != 0)
2612fe192f76Sahrens 				return (-1);
2613fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2614e7437265Sahrens 				return (-1);
2615e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2616e7437265Sahrens 			break;
2617e7437265Sahrens 
2618e7437265Sahrens 		default:
261999653d4eSeschrock 			abort();
2620fa9e4066Sahrens 		}
2621e7437265Sahrens 	}
2622fa9e4066Sahrens 
2623fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2624fa9e4066Sahrens 
2625fa9e4066Sahrens 	return (0);
2626fa9e4066Sahrens }
2627fa9e4066Sahrens 
2628fa9e4066Sahrens /*
2629fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2630fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2631fa9e4066Sahrens  * hard-coded property types.
2632fa9e4066Sahrens  */
2633fa9e4066Sahrens uint64_t
zfs_prop_get_int(zfs_handle_t * zhp,zfs_prop_t prop)2634fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2635fa9e4066Sahrens {
2636fa9e4066Sahrens 	char *source;
263799653d4eSeschrock 	uint64_t val;
2638fa9e4066Sahrens 
26393cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
264099653d4eSeschrock 
264199653d4eSeschrock 	return (val);
2642fa9e4066Sahrens }
2643fa9e4066Sahrens 
26447b97dc1aSrm160521 int
zfs_prop_set_int(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t val)26457b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
26467b97dc1aSrm160521 {
26477b97dc1aSrm160521 	char buf[64];
26487b97dc1aSrm160521 
264914843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
26507b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
26517b97dc1aSrm160521 }
26527b97dc1aSrm160521 
2653fa9e4066Sahrens /*
2654fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2655fa9e4066Sahrens  */
2656fa9e4066Sahrens int
zfs_prop_get_numeric(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t * value,zprop_source_t * src,char * statbuf,size_t statlen)2657fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2658990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2659fa9e4066Sahrens {
2660fa9e4066Sahrens 	char *source;
2661fa9e4066Sahrens 
2662fa9e4066Sahrens 	/*
2663fa9e4066Sahrens 	 * Check to see if this property applies to our object
2664fa9e4066Sahrens 	 */
2665e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2666ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
266799653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
266899653d4eSeschrock 		    zfs_prop_to_name(prop)));
2669e45ce728Sahrens 	}
2670fa9e4066Sahrens 
2671fa9e4066Sahrens 	if (src)
2672990b4856Slling 		*src = ZPROP_SRC_NONE;
2673fa9e4066Sahrens 
267499653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
267599653d4eSeschrock 		return (-1);
2676fa9e4066Sahrens 
2677fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2678fa9e4066Sahrens 
2679fa9e4066Sahrens 	return (0);
2680fa9e4066Sahrens }
2681fa9e4066Sahrens 
268214843421SMatthew Ahrens static int
idmap_id_to_numeric_domain_rid(uid_t id,boolean_t isuser,char ** domainp,idmap_rid_t * ridp)268314843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
268414843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
268514843421SMatthew Ahrens {
268614843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
268714843421SMatthew Ahrens 	idmap_stat status;
268814843421SMatthew Ahrens 	int err = EINVAL;
268914843421SMatthew Ahrens 
26901fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
269114843421SMatthew Ahrens 		goto out;
269214843421SMatthew Ahrens 
269314843421SMatthew Ahrens 	if (isuser) {
269414843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
269514843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
269614843421SMatthew Ahrens 	} else {
269714843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
269814843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
269914843421SMatthew Ahrens 	}
270014843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
270114843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
270214843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
270314843421SMatthew Ahrens 		err = 0;
270414843421SMatthew Ahrens 	else
270514843421SMatthew Ahrens 		err = EINVAL;
270614843421SMatthew Ahrens out:
270714843421SMatthew Ahrens 	if (get_hdl)
270814843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
270914843421SMatthew Ahrens 	return (err);
271014843421SMatthew Ahrens }
271114843421SMatthew Ahrens 
271214843421SMatthew Ahrens /*
271314843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
271414843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
271514843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
271614843421SMatthew Ahrens  */
271714843421SMatthew Ahrens static int
userquota_propname_decode(const char * propname,boolean_t zoned,zfs_userquota_prop_t * typep,char * domain,int domainlen,uint64_t * ridp)271814843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
271914843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
272014843421SMatthew Ahrens {
272114843421SMatthew Ahrens 	zfs_userquota_prop_t type;
272214843421SMatthew Ahrens 	char *cp, *end;
27233b12c289SMatthew Ahrens 	char *numericsid = NULL;
272414843421SMatthew Ahrens 	boolean_t isuser;
272514843421SMatthew Ahrens 
272614843421SMatthew Ahrens 	domain[0] = '\0';
27271ed6b69aSGordon Ross 	*ridp = 0;
272814843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
272914843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
273014843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
273114843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
273214843421SMatthew Ahrens 			break;
273314843421SMatthew Ahrens 	}
273414843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
273514843421SMatthew Ahrens 		return (EINVAL);
273614843421SMatthew Ahrens 	*typep = type;
273714843421SMatthew Ahrens 
273814843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
273914843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
274014843421SMatthew Ahrens 
274114843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
274214843421SMatthew Ahrens 
274314843421SMatthew Ahrens 	if (strchr(cp, '@')) {
274414843421SMatthew Ahrens 		/*
274514843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
27463b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
274714843421SMatthew Ahrens 		 */
27481ed6b69aSGordon Ross 		int flag = 0;
27491ed6b69aSGordon Ross 		idmap_stat stat, map_stat;
27501ed6b69aSGordon Ross 		uid_t pid;
27511ed6b69aSGordon Ross 		idmap_rid_t rid;
27521ed6b69aSGordon Ross 		idmap_get_handle_t *gh = NULL;
27531ed6b69aSGordon Ross 
27541ed6b69aSGordon Ross 		stat = idmap_get_create(&gh);
27551ed6b69aSGordon Ross 		if (stat != IDMAP_SUCCESS) {
27561ed6b69aSGordon Ross 			idmap_get_destroy(gh);
27571ed6b69aSGordon Ross 			return (ENOMEM);
27581ed6b69aSGordon Ross 		}
275914843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
276014843421SMatthew Ahrens 			return (ENOENT);
27613b12c289SMatthew Ahrens 		if (isuser) {
27621ed6b69aSGordon Ross 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
27631ed6b69aSGordon Ross 			if (stat < 0)
27641ed6b69aSGordon Ross 				return (ENOENT);
27651ed6b69aSGordon Ross 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
27661ed6b69aSGordon Ross 			    &rid, &map_stat);
27673b12c289SMatthew Ahrens 		} else {
27681ed6b69aSGordon Ross 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
27691ed6b69aSGordon Ross 			if (stat < 0)
27701ed6b69aSGordon Ross 				return (ENOENT);
27711ed6b69aSGordon Ross 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
27721ed6b69aSGordon Ross 			    &rid, &map_stat);
27733b12c289SMatthew Ahrens 		}
27741ed6b69aSGordon Ross 		if (stat < 0) {
27751ed6b69aSGordon Ross 			idmap_get_destroy(gh);
27761ed6b69aSGordon Ross 			return (ENOENT);
27771ed6b69aSGordon Ross 		}
27781ed6b69aSGordon Ross 		stat = idmap_get_mappings(gh);
27791ed6b69aSGordon Ross 		idmap_get_destroy(gh);
27801ed6b69aSGordon Ross 
27811ed6b69aSGordon Ross 		if (stat < 0) {
278214843421SMatthew Ahrens 			return (ENOENT);
27833b12c289SMatthew Ahrens 		}
27843b12c289SMatthew Ahrens 		if (numericsid == NULL)
278514843421SMatthew Ahrens 			return (ENOENT);
27863b12c289SMatthew Ahrens 		cp = numericsid;
27871ed6b69aSGordon Ross 		*ridp = rid;
27883b12c289SMatthew Ahrens 		/* will be further decoded below */
27893b12c289SMatthew Ahrens 	}
27903b12c289SMatthew Ahrens 
27913b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
279214843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
27933b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
27941ed6b69aSGordon Ross 		errno = 0;
27951ed6b69aSGordon Ross 		if (*ridp == 0) {
279614843421SMatthew Ahrens 			cp = strrchr(domain, '-');
279714843421SMatthew Ahrens 			*cp = '\0';
279814843421SMatthew Ahrens 			cp++;
279914843421SMatthew Ahrens 			*ridp = strtoull(cp, &end, 10);
28001ed6b69aSGordon Ross 		} else {
28011ed6b69aSGordon Ross 			end = "";
28021ed6b69aSGordon Ross 		}
28033b12c289SMatthew Ahrens 		if (numericsid) {
28043b12c289SMatthew Ahrens 			free(numericsid);
28053b12c289SMatthew Ahrens 			numericsid = NULL;
28063b12c289SMatthew Ahrens 		}
2807777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
280814843421SMatthew Ahrens 			return (EINVAL);
280914843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
281014843421SMatthew Ahrens 		/*
281114843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
281214843421SMatthew Ahrens 		 * turned into a uid/gid
281314843421SMatthew Ahrens 		 */
281414843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
281514843421SMatthew Ahrens 			return (ENOENT);
281614843421SMatthew Ahrens 		if (isuser) {
281714843421SMatthew Ahrens 			struct passwd *pw;
281814843421SMatthew Ahrens 			pw = getpwnam(cp);
281914843421SMatthew Ahrens 			if (pw == NULL)
282014843421SMatthew Ahrens 				return (ENOENT);
282114843421SMatthew Ahrens 			*ridp = pw->pw_uid;
282214843421SMatthew Ahrens 		} else {
282314843421SMatthew Ahrens 			struct group *gr;
282414843421SMatthew Ahrens 			gr = getgrnam(cp);
282514843421SMatthew Ahrens 			if (gr == NULL)
282614843421SMatthew Ahrens 				return (ENOENT);
282714843421SMatthew Ahrens 			*ridp = gr->gr_gid;
282814843421SMatthew Ahrens 		}
282914843421SMatthew Ahrens 	} else {
283014843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
283114843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
283214843421SMatthew Ahrens 		idmap_rid_t rid;
283314843421SMatthew Ahrens 		char *mapdomain;
283414843421SMatthew Ahrens 
283514843421SMatthew Ahrens 		if (*end != '\0')
283614843421SMatthew Ahrens 			return (EINVAL);
283714843421SMatthew Ahrens 		if (id > MAXUID) {
283814843421SMatthew Ahrens 			/* It's an ephemeral ID. */
283914843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
284014843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
284114843421SMatthew Ahrens 				return (ENOENT);
28423b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
284314843421SMatthew Ahrens 			*ridp = rid;
284414843421SMatthew Ahrens 		} else {
284514843421SMatthew Ahrens 			*ridp = id;
284614843421SMatthew Ahrens 		}
284714843421SMatthew Ahrens 	}
284814843421SMatthew Ahrens 
28493b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
285014843421SMatthew Ahrens 	return (0);
285114843421SMatthew Ahrens }
285214843421SMatthew Ahrens 
2853edea4b55SLin Ling static int
zfs_prop_get_userquota_common(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue,zfs_userquota_prop_t * typep)2854edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2855edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
285614843421SMatthew Ahrens {
285714843421SMatthew Ahrens 	int err;
285814843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
285914843421SMatthew Ahrens 
286019b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
286114843421SMatthew Ahrens 
286214843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
286314843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2864edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2865edea4b55SLin Ling 	zc.zc_objset_type = *typep;
286614843421SMatthew Ahrens 	if (err)
286714843421SMatthew Ahrens 		return (err);
286814843421SMatthew Ahrens 
286914843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
287014843421SMatthew Ahrens 	if (err)
287114843421SMatthew Ahrens 		return (err);
287214843421SMatthew Ahrens 
2873edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2874edea4b55SLin Ling 	return (0);
2875edea4b55SLin Ling }
2876edea4b55SLin Ling 
2877edea4b55SLin Ling int
zfs_prop_get_userquota_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)2878edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2879edea4b55SLin Ling     uint64_t *propvalue)
2880edea4b55SLin Ling {
2881edea4b55SLin Ling 	zfs_userquota_prop_t type;
2882edea4b55SLin Ling 
2883edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2884edea4b55SLin Ling 	    &type));
2885edea4b55SLin Ling }
2886edea4b55SLin Ling 
2887edea4b55SLin Ling int
zfs_prop_get_userquota(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)2888edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2889edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2890edea4b55SLin Ling {
2891edea4b55SLin Ling 	int err;
2892edea4b55SLin Ling 	uint64_t propvalue;
2893edea4b55SLin Ling 	zfs_userquota_prop_t type;
2894edea4b55SLin Ling 
2895edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2896edea4b55SLin Ling 	    &type);
2897edea4b55SLin Ling 
2898edea4b55SLin Ling 	if (err)
2899edea4b55SLin Ling 		return (err);
2900edea4b55SLin Ling 
290114843421SMatthew Ahrens 	if (literal) {
2902edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2903edea4b55SLin Ling 	} else if (propvalue == 0 &&
290414843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
290514843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
290614843421SMatthew Ahrens 	} else {
2907edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
290814843421SMatthew Ahrens 	}
290914843421SMatthew Ahrens 	return (0);
291014843421SMatthew Ahrens }
291114843421SMatthew Ahrens 
291219b94df9SMatthew Ahrens int
zfs_prop_get_written_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)291319b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
291419b94df9SMatthew Ahrens     uint64_t *propvalue)
291519b94df9SMatthew Ahrens {
291619b94df9SMatthew Ahrens 	int err;
291719b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
291819b94df9SMatthew Ahrens 	const char *snapname;
291919b94df9SMatthew Ahrens 
292019b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
292119b94df9SMatthew Ahrens 
292219b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
292319b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
292419b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
292519b94df9SMatthew Ahrens 	} else {
292619b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
292719b94df9SMatthew Ahrens 		char *cp;
292819b94df9SMatthew Ahrens 
292919b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
293019b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
293119b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
293219b94df9SMatthew Ahrens 		if (cp != NULL)
293319b94df9SMatthew Ahrens 			*cp = '\0';
293419b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
293519b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
293619b94df9SMatthew Ahrens 	}
293719b94df9SMatthew Ahrens 
293819b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
293919b94df9SMatthew Ahrens 	if (err)
294019b94df9SMatthew Ahrens 		return (err);
294119b94df9SMatthew Ahrens 
294219b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
294319b94df9SMatthew Ahrens 	return (0);
294419b94df9SMatthew Ahrens }
294519b94df9SMatthew Ahrens 
294619b94df9SMatthew Ahrens int
zfs_prop_get_written(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)294719b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
294819b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
294919b94df9SMatthew Ahrens {
295019b94df9SMatthew Ahrens 	int err;
295119b94df9SMatthew Ahrens 	uint64_t propvalue;
295219b94df9SMatthew Ahrens 
295319b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
295419b94df9SMatthew Ahrens 
295519b94df9SMatthew Ahrens 	if (err)
295619b94df9SMatthew Ahrens 		return (err);
295719b94df9SMatthew Ahrens 
295819b94df9SMatthew Ahrens 	if (literal) {
295919b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
296019b94df9SMatthew Ahrens 	} else {
296119b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
296219b94df9SMatthew Ahrens 	}
296319b94df9SMatthew Ahrens 	return (0);
296419b94df9SMatthew Ahrens }
296519b94df9SMatthew Ahrens 
2966fa9e4066Sahrens /*
2967fa9e4066Sahrens  * Returns the name of the given zfs handle.
2968fa9e4066Sahrens  */
2969fa9e4066Sahrens const char *
zfs_get_name(const zfs_handle_t * zhp)2970fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
2971fa9e4066Sahrens {
2972fa9e4066Sahrens 	return (zhp->zfs_name);
2973fa9e4066Sahrens }
2974fa9e4066Sahrens 
2975fa9e4066Sahrens /*
2976fa9e4066Sahrens  * Returns the type of the given zfs handle.
2977fa9e4066Sahrens  */
2978fa9e4066Sahrens zfs_type_t
zfs_get_type(const zfs_handle_t * zhp)2979fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
2980fa9e4066Sahrens {
2981fa9e4066Sahrens 	return (zhp->zfs_type);
2982fa9e4066Sahrens }
2983fa9e4066Sahrens 
29847f7322feSeschrock /*
2985d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
2986d41c4376SMark J Musante  *
2987d41c4376SMark J Musante  * Needs to handle these cases:
2988d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2989d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2990d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
2991d41c4376SMark J Musante  */
2992d41c4376SMark J Musante static boolean_t
is_descendant(const char * ds1,const char * ds2)2993d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
2994d41c4376SMark J Musante {
2995d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
2996d41c4376SMark J Musante 
2997d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
2998d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
2999d41c4376SMark J Musante 		return (B_FALSE);
3000d41c4376SMark J Musante 
3001d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
3002d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3003d41c4376SMark J Musante }
3004d41c4376SMark J Musante 
3005d41c4376SMark J Musante /*
3006fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
300719b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
300819b94df9SMatthew Ahrens  * pool).
3009fa9e4066Sahrens  */
3010fa9e4066Sahrens static int
parent_name(const char * path,char * buf,size_t buflen)3011fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3012fa9e4066Sahrens {
301319b94df9SMatthew Ahrens 	char *slashp;
3014fa9e4066Sahrens 
301519b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
301619b94df9SMatthew Ahrens 
301719b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
3018fa9e4066Sahrens 		return (-1);
301919b94df9SMatthew Ahrens 	*slashp = '\0';
3020fa9e4066Sahrens 
3021fa9e4066Sahrens 	return (0);
3022fa9e4066Sahrens }
3023fa9e4066Sahrens 
3024fa9e4066Sahrens /*
30257f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
30267f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
30277f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
30287f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
30297f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
30307f1f55eaSvb160487  * new datasets.
3031fa9e4066Sahrens  */
3032fa9e4066Sahrens static int
check_parents(libzfs_handle_t * hdl,const char * path,uint64_t * zoned,boolean_t accept_ancestor,int * prefixlen)30337f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
30347f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
3035fa9e4066Sahrens {
3036fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3037*a1988827SMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3038fa9e4066Sahrens 	char *slash;
30397f7322feSeschrock 	zfs_handle_t *zhp;
304099653d4eSeschrock 	char errbuf[1024];
3041d41c4376SMark J Musante 	uint64_t is_zoned;
304299653d4eSeschrock 
3043deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
3044deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3045fa9e4066Sahrens 
3046fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
3047fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
304899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
304999653d4eSeschrock 		    "missing dataset name"));
305099653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3051fa9e4066Sahrens 	}
3052fa9e4066Sahrens 
3053fa9e4066Sahrens 	/* check to see if the pool exists */
3054fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
3055fa9e4066Sahrens 		slash = parent + strlen(parent);
30568ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
3057fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
305899653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3059fa9e4066Sahrens 	    errno == ENOENT) {
306099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
306199653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
306299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3063fa9e4066Sahrens 	}
3064fa9e4066Sahrens 
3065fa9e4066Sahrens 	/* check to see if the parent dataset exists */
30667f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
30677f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
30687f1f55eaSvb160487 			/*
30697f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
30707f1f55eaSvb160487 			 */
30717f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
30727f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30737f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
30747f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
30757f1f55eaSvb160487 			}
30767f1f55eaSvb160487 		} else if (errno == ENOENT) {
307799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
307899653d4eSeschrock 			    "parent does not exist"));
307999653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
30807f1f55eaSvb160487 		} else
308199653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3082fa9e4066Sahrens 	}
3083fa9e4066Sahrens 
3084d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3085d41c4376SMark J Musante 	if (zoned != NULL)
3086d41c4376SMark J Musante 		*zoned = is_zoned;
3087d41c4376SMark J Musante 
3088fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
3089d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
309099653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
30917f7322feSeschrock 		zfs_close(zhp);
3092fa9e4066Sahrens 		return (-1);
3093fa9e4066Sahrens 	}
3094fa9e4066Sahrens 
3095fa9e4066Sahrens 	/* make sure parent is a filesystem */
30967f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
309799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309899653d4eSeschrock 		    "parent is not a filesystem"));
309999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
31007f7322feSeschrock 		zfs_close(zhp);
3101fa9e4066Sahrens 		return (-1);
3102fa9e4066Sahrens 	}
3103fa9e4066Sahrens 
31047f7322feSeschrock 	zfs_close(zhp);
31057f1f55eaSvb160487 	if (prefixlen != NULL)
31067f1f55eaSvb160487 		*prefixlen = strlen(parent);
31077f1f55eaSvb160487 	return (0);
31087f1f55eaSvb160487 }
31097f1f55eaSvb160487 
31107f1f55eaSvb160487 /*
31117f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
31127f1f55eaSvb160487  */
31137f1f55eaSvb160487 boolean_t
zfs_dataset_exists(libzfs_handle_t * hdl,const char * path,zfs_type_t types)31147f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
31157f1f55eaSvb160487 {
31167f1f55eaSvb160487 	zfs_handle_t *zhp;
31177f1f55eaSvb160487 
3118f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
31197f1f55eaSvb160487 		return (B_FALSE);
31207f1f55eaSvb160487 
31217f1f55eaSvb160487 	/*
31227f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
31237f1f55eaSvb160487 	 */
31247f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
31257f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
31267f1f55eaSvb160487 
31277f1f55eaSvb160487 		zfs_close(zhp);
31287f1f55eaSvb160487 		if (types & ds_type)
31297f1f55eaSvb160487 			return (B_TRUE);
31307f1f55eaSvb160487 	}
31317f1f55eaSvb160487 	return (B_FALSE);
31327f1f55eaSvb160487 }
31337f1f55eaSvb160487 
31347f1f55eaSvb160487 /*
31353cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
31363cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
31373cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
31383cb34c60Sahrens  */
31393cb34c60Sahrens int
create_parents(libzfs_handle_t * hdl,char * target,int prefixlen)31403cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
31413cb34c60Sahrens {
31423cb34c60Sahrens 	zfs_handle_t *h;
31433cb34c60Sahrens 	char *cp;
31443cb34c60Sahrens 	const char *opname;
31453cb34c60Sahrens 
31463cb34c60Sahrens 	/* make sure prefix exists */
31473cb34c60Sahrens 	cp = target + prefixlen;
31483cb34c60Sahrens 	if (*cp != '/') {
31493cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
31503cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31513cb34c60Sahrens 	} else {
31523cb34c60Sahrens 		*cp = '\0';
31533cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31543cb34c60Sahrens 		*cp = '/';
31553cb34c60Sahrens 	}
31563cb34c60Sahrens 	if (h == NULL)
31573cb34c60Sahrens 		return (-1);
31583cb34c60Sahrens 	zfs_close(h);
31593cb34c60Sahrens 
31603cb34c60Sahrens 	/*
31613cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
31623cb34c60Sahrens 	 * up to the prefixlen-long one.
31633cb34c60Sahrens 	 */
31643cb34c60Sahrens 	for (cp = target + prefixlen + 1;
31653cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
31663cb34c60Sahrens 
31673cb34c60Sahrens 		*cp = '\0';
31683cb34c60Sahrens 
31693cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
31703cb34c60Sahrens 		if (h) {
31713cb34c60Sahrens 			/* it already exists, nothing to do here */
31723cb34c60Sahrens 			zfs_close(h);
31733cb34c60Sahrens 			continue;
31743cb34c60Sahrens 		}
31753cb34c60Sahrens 
31763cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
31773cb34c60Sahrens 		    NULL) != 0) {
31783cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
31793cb34c60Sahrens 			goto ancestorerr;
31803cb34c60Sahrens 		}
31813cb34c60Sahrens 
31823cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
31833cb34c60Sahrens 		if (h == NULL) {
31843cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
31853cb34c60Sahrens 			goto ancestorerr;
31863cb34c60Sahrens 		}
31873cb34c60Sahrens 
31883cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
31893cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
31903cb34c60Sahrens 			goto ancestorerr;
31913cb34c60Sahrens 		}
31923cb34c60Sahrens 
31933cb34c60Sahrens 		if (zfs_share(h) != 0) {
31943cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
31953cb34c60Sahrens 			goto ancestorerr;
31963cb34c60Sahrens 		}
31973cb34c60Sahrens 
31983cb34c60Sahrens 		zfs_close(h);
31993cb34c60Sahrens 	}
32003cb34c60Sahrens 
32013cb34c60Sahrens 	return (0);
32023cb34c60Sahrens 
32033cb34c60Sahrens ancestorerr:
32043cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32053cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
32063cb34c60Sahrens 	return (-1);
32073cb34c60Sahrens }
32083cb34c60Sahrens 
32093cb34c60Sahrens /*
32107f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
32117f1f55eaSvb160487  */
32127f1f55eaSvb160487 int
zfs_create_ancestors(libzfs_handle_t * hdl,const char * path)32137f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
32147f1f55eaSvb160487 {
32157f1f55eaSvb160487 	int prefix;
32167f1f55eaSvb160487 	char *path_copy;
32177f1f55eaSvb160487 	int rc;
32187f1f55eaSvb160487 
3219d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
32207f1f55eaSvb160487 		return (-1);
32217f1f55eaSvb160487 
32227f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
32237f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
32247f1f55eaSvb160487 		free(path_copy);
32257f1f55eaSvb160487 	}
32267f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
32277f1f55eaSvb160487 		return (-1);
32287f1f55eaSvb160487 
3229fa9e4066Sahrens 	return (0);
3230fa9e4066Sahrens }
3231fa9e4066Sahrens 
3232fa9e4066Sahrens /*
3233e9dbad6fSeschrock  * Create a new filesystem or volume.
3234fa9e4066Sahrens  */
3235fa9e4066Sahrens int
zfs_create(libzfs_handle_t * hdl,const char * path,zfs_type_t type,nvlist_t * props)323699653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3237e9dbad6fSeschrock     nvlist_t *props)
3238fa9e4066Sahrens {
3239fa9e4066Sahrens 	int ret;
3240fa9e4066Sahrens 	uint64_t size = 0;
3241fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
324299653d4eSeschrock 	char errbuf[1024];
3243e9dbad6fSeschrock 	uint64_t zoned;
32444445fffbSMatthew Ahrens 	dmu_objset_type_t ost;
324599653d4eSeschrock 
324699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
324799653d4eSeschrock 	    "cannot create '%s'"), path);
3248fa9e4066Sahrens 
3249fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3250f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
325199653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3252fa9e4066Sahrens 
3253fa9e4066Sahrens 	/* validate parents exist */
32547f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3255fa9e4066Sahrens 		return (-1);
3256fa9e4066Sahrens 
3257fa9e4066Sahrens 	/*
3258fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3259fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3260fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3261fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3262fa9e4066Sahrens 	 * first try to see if the dataset exists.
3263fa9e4066Sahrens 	 */
32644445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
326599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
326699653d4eSeschrock 		    "dataset already exists"));
326799653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3268fa9e4066Sahrens 	}
3269fa9e4066Sahrens 
3270fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
32714445fffbSMatthew Ahrens 		ost = DMU_OST_ZVOL;
3272fa9e4066Sahrens 	else
32734445fffbSMatthew Ahrens 		ost = DMU_OST_ZFS;
3274fa9e4066Sahrens 
32750a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3276b1b8ab34Slling 	    zoned, NULL, errbuf)) == 0)
3277e9dbad6fSeschrock 		return (-1);
3278e9dbad6fSeschrock 
3279fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
32805c5460e9Seschrock 		/*
32815c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
32825c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
32835c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
32845c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
32855c5460e9Seschrock 		 * zero.
32865c5460e9Seschrock 		 */
3287e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3288e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3289e9dbad6fSeschrock 			nvlist_free(props);
329099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3291e9dbad6fSeschrock 			    "missing volume size"));
3292e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3293fa9e4066Sahrens 		}
3294fa9e4066Sahrens 
3295e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3296e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3297e9dbad6fSeschrock 		    &blocksize)) != 0) {
3298e9dbad6fSeschrock 			if (ret == ENOENT) {
3299e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3300e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3301e9dbad6fSeschrock 			} else {
3302e9dbad6fSeschrock 				nvlist_free(props);
330399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3304e9dbad6fSeschrock 				    "missing volume block size"));
3305e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3306e9dbad6fSeschrock 			}
3307e9dbad6fSeschrock 		}
3308e9dbad6fSeschrock 
3309e9dbad6fSeschrock 		if (size == 0) {
3310e9dbad6fSeschrock 			nvlist_free(props);
3311e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3312e9dbad6fSeschrock 			    "volume size cannot be zero"));
3313e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
33145c5460e9Seschrock 		}
33155c5460e9Seschrock 
33165c5460e9Seschrock 		if (size % blocksize != 0) {
3317e9dbad6fSeschrock 			nvlist_free(props);
331899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3319e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3320e9dbad6fSeschrock 			    "size"));
3321e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3322e9dbad6fSeschrock 		}
33235c5460e9Seschrock 	}
33245c5460e9Seschrock 
3325fa9e4066Sahrens 	/* create the dataset */
33264445fffbSMatthew Ahrens 	ret = lzc_create(path, ost, props);
33274445fffbSMatthew Ahrens 	nvlist_free(props);
3328e9dbad6fSeschrock 
3329fa9e4066Sahrens 	/* check for failure */
3330fa9e4066Sahrens 	if (ret != 0) {
3331*a1988827SMatthew Ahrens 		char parent[ZFS_MAX_DATASET_NAME_LEN];
3332fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3333fa9e4066Sahrens 
3334fa9e4066Sahrens 		switch (errno) {
3335fa9e4066Sahrens 		case ENOENT:
333699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
333799653d4eSeschrock 			    "no such parent '%s'"), parent);
333899653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3339fa9e4066Sahrens 
3340fa9e4066Sahrens 		case EINVAL:
334199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3342d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
334399653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3344fa9e4066Sahrens 
3345fa9e4066Sahrens 		case EDOM:
334699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3347e9dbad6fSeschrock 			    "volume block size must be power of 2 from "
3348b5152584SMatthew Ahrens 			    "512B to 128KB"));
334999653d4eSeschrock 
3350e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
335199653d4eSeschrock 
335240feaa91Sahrens 		case ENOTSUP:
335340feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
335440feaa91Sahrens 			    "pool must be upgraded to set this "
335540feaa91Sahrens 			    "property or value"));
335640feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3357fa9e4066Sahrens #ifdef _ILP32
3358fa9e4066Sahrens 		case EOVERFLOW:
3359fa9e4066Sahrens 			/*
3360fa9e4066Sahrens 			 * This platform can't address a volume this big.
3361fa9e4066Sahrens 			 */
336299653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
336399653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
336499653d4eSeschrock 				    errbuf));
3365fa9e4066Sahrens #endif
336699653d4eSeschrock 			/* FALLTHROUGH */
3367fa9e4066Sahrens 		default:
336899653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3369fa9e4066Sahrens 		}
3370fa9e4066Sahrens 	}
3371fa9e4066Sahrens 
3372fa9e4066Sahrens 	return (0);
3373fa9e4066Sahrens }
3374fa9e4066Sahrens 
3375fa9e4066Sahrens /*
3376fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
337765fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
337865fec9f6SChristopher Siden  * does not exist this function does nothing.
3379fa9e4066Sahrens  */
3380fa9e4066Sahrens int
zfs_destroy(zfs_handle_t * zhp,boolean_t defer)3381842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3382fa9e4066Sahrens {
3383fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3384fa9e4066Sahrens 
338578f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
338678f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
338778f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
338878f17100SMatthew Ahrens 		int error = lzc_destroy_bookmarks(nv, NULL);
338978f17100SMatthew Ahrens 		fnvlist_free(nv);
339078f17100SMatthew Ahrens 		if (error != 0) {
339178f17100SMatthew Ahrens 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
339278f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
339378f17100SMatthew Ahrens 			    zhp->zfs_name));
339478f17100SMatthew Ahrens 		}
339578f17100SMatthew Ahrens 		return (0);
339678f17100SMatthew Ahrens 	}
339778f17100SMatthew Ahrens 
3398fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3399fa9e4066Sahrens 
3400e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3401fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3402fa9e4066Sahrens 	} else {
3403fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3404fa9e4066Sahrens 	}
3405fa9e4066Sahrens 
3406842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
340765fec9f6SChristopher Siden 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
340865fec9f6SChristopher Siden 	    errno != ENOENT) {
3409ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
341099653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
341199653d4eSeschrock 		    zhp->zfs_name));
34121d452cf5Sahrens 	}
3413fa9e4066Sahrens 
3414fa9e4066Sahrens 	remove_mountpoint(zhp);
3415fa9e4066Sahrens 
3416fa9e4066Sahrens 	return (0);
3417fa9e4066Sahrens }
3418fa9e4066Sahrens 
34191d452cf5Sahrens struct destroydata {
342019b94df9SMatthew Ahrens 	nvlist_t *nvl;
342119b94df9SMatthew Ahrens 	const char *snapname;
34221d452cf5Sahrens };
34231d452cf5Sahrens 
34241d452cf5Sahrens static int
zfs_check_snap_cb(zfs_handle_t * zhp,void * arg)3425681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
34261d452cf5Sahrens {
34271d452cf5Sahrens 	struct destroydata *dd = arg;
3428*a1988827SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3429681d9761SEric Taylor 	int rv = 0;
34301d452cf5Sahrens 
343119b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
343219b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
34331d452cf5Sahrens 
3434a7a845e4SSteven Hartland 	if (lzc_exists(name))
343519b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
34361d452cf5Sahrens 
343719b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
34383ccfa83cSahrens 	zfs_close(zhp);
34393ccfa83cSahrens 	return (rv);
34401d452cf5Sahrens }
34411d452cf5Sahrens 
34421d452cf5Sahrens /*
34431d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
34441d452cf5Sahrens  */
34451d452cf5Sahrens int
zfs_destroy_snaps(zfs_handle_t * zhp,char * snapname,boolean_t defer)3446842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
34471d452cf5Sahrens {
34481d452cf5Sahrens 	int ret;
34491d452cf5Sahrens 	struct destroydata dd = { 0 };
34501d452cf5Sahrens 
34511d452cf5Sahrens 	dd.snapname = snapname;
345219b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
345319b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
34541d452cf5Sahrens 
3455a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
345619b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
34571d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
345819b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
345919b94df9SMatthew Ahrens 	} else {
34603b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
346119b94df9SMatthew Ahrens 	}
346219b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
346319b94df9SMatthew Ahrens 	return (ret);
3464e5351341SMatthew Ahrens }
3465e5351341SMatthew Ahrens 
346619b94df9SMatthew Ahrens /*
34673b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
346819b94df9SMatthew Ahrens  */
346919b94df9SMatthew Ahrens int
zfs_destroy_snaps_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,boolean_t defer)34703b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
347119b94df9SMatthew Ahrens {
347219b94df9SMatthew Ahrens 	int ret;
34734445fffbSMatthew Ahrens 	nvlist_t *errlist;
347419b94df9SMatthew Ahrens 
34754445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
34761d452cf5Sahrens 
34773b2aab18SMatthew Ahrens 	if (ret == 0)
34783b2aab18SMatthew Ahrens 		return (0);
34793b2aab18SMatthew Ahrens 
3480a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
34813b2aab18SMatthew Ahrens 		char errbuf[1024];
34823b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
34833b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
34843b2aab18SMatthew Ahrens 
34853b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
34863b2aab18SMatthew Ahrens 	}
34874445fffbSMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
34884445fffbSMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
34891d452cf5Sahrens 		char errbuf[1024];
34904445fffbSMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
34914445fffbSMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
34924445fffbSMatthew Ahrens 		    nvpair_name(pair));
34931d452cf5Sahrens 
34944445fffbSMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
34951d452cf5Sahrens 		case EEXIST:
34963b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
34973b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
34983b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
34994445fffbSMatthew Ahrens 			break;
35001d452cf5Sahrens 		default:
35013b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
35024445fffbSMatthew Ahrens 			break;
35034445fffbSMatthew Ahrens 		}
35041d452cf5Sahrens 	}
35051d452cf5Sahrens 
3506c3a755d3SJan Schlien 	nvlist_free(errlist);
35074445fffbSMatthew Ahrens 	return (ret);
35081d452cf5Sahrens }
35091d452cf5Sahrens 
3510fa9e4066Sahrens /*
3511fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3512fa9e4066Sahrens  */
3513fa9e4066Sahrens int
zfs_clone(zfs_handle_t * zhp,const char * target,nvlist_t * props)3514e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3515fa9e4066Sahrens {
3516*a1988827SMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3517fa9e4066Sahrens 	int ret;
351899653d4eSeschrock 	char errbuf[1024];
351999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3520e9dbad6fSeschrock 	uint64_t zoned;
3521fa9e4066Sahrens 
3522fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3523fa9e4066Sahrens 
352499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
352599653d4eSeschrock 	    "cannot create '%s'"), target);
352699653d4eSeschrock 
352719b94df9SMatthew Ahrens 	/* validate the target/clone name */
3528f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
352999653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3530fa9e4066Sahrens 
3531fa9e4066Sahrens 	/* validate parents exist */
35327f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3533fa9e4066Sahrens 		return (-1);
3534fa9e4066Sahrens 
3535fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3536fa9e4066Sahrens 
3537fa9e4066Sahrens 	/* do the clone */
3538e9dbad6fSeschrock 
3539e9dbad6fSeschrock 	if (props) {
35404445fffbSMatthew Ahrens 		zfs_type_t type;
35414445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
35424445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
35434445fffbSMatthew Ahrens 		} else {
35444445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
35454445fffbSMatthew Ahrens 		}
35460a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
35470a48a24eStimh 		    zhp, errbuf)) == NULL)
3548e9dbad6fSeschrock 			return (-1);
3549e9dbad6fSeschrock 	}
3550e9dbad6fSeschrock 
35514445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
3552e9dbad6fSeschrock 	nvlist_free(props);
3553e9dbad6fSeschrock 
3554fa9e4066Sahrens 	if (ret != 0) {
3555fa9e4066Sahrens 		switch (errno) {
3556fa9e4066Sahrens 
3557fa9e4066Sahrens 		case ENOENT:
3558fa9e4066Sahrens 			/*
3559fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3560fa9e4066Sahrens 			 * above, but there may a race condition that has since
3561fa9e4066Sahrens 			 * destroyed the parent.
3562fa9e4066Sahrens 			 *
3563fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3564fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3565fa9e4066Sahrens 			 * dataset doesn't exist.
3566fa9e4066Sahrens 			 */
356799653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
356899653d4eSeschrock 			    "no such parent '%s'"), parent);
356999653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3570fa9e4066Sahrens 
357199653d4eSeschrock 		case EXDEV:
357299653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
357399653d4eSeschrock 			    "source and target pools differ"));
357499653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
357599653d4eSeschrock 			    errbuf));
357699653d4eSeschrock 
357799653d4eSeschrock 		default:
357899653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
357999653d4eSeschrock 			    errbuf));
358099653d4eSeschrock 		}
358199653d4eSeschrock 	}
358299653d4eSeschrock 
358399653d4eSeschrock 	return (ret);
358499653d4eSeschrock }
358599653d4eSeschrock 
358699653d4eSeschrock /*
358799653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
358899653d4eSeschrock  */
358999653d4eSeschrock int
zfs_promote(zfs_handle_t * zhp)359099653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
359199653d4eSeschrock {
359299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
359399653d4eSeschrock 	zfs_cmd_t zc = { 0 };
359499653d4eSeschrock 	char parent[MAXPATHLEN];
359599653d4eSeschrock 	int ret;
359699653d4eSeschrock 	char errbuf[1024];
359799653d4eSeschrock 
359899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
359999653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
360099653d4eSeschrock 
360199653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
360299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
360399653d4eSeschrock 		    "snapshots can not be promoted"));
360499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
360599653d4eSeschrock 	}
360699653d4eSeschrock 
36073cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
360899653d4eSeschrock 	if (parent[0] == '\0') {
360999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
361099653d4eSeschrock 		    "not a cloned filesystem"));
361199653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
361299653d4eSeschrock 	}
361399653d4eSeschrock 
36143cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3615e9dbad6fSeschrock 	    sizeof (zc.zc_value));
361699653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3617ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
361899653d4eSeschrock 
361999653d4eSeschrock 	if (ret != 0) {
36200b69c2f0Sahrens 		int save_errno = errno;
3621fa9e4066Sahrens 
36220b69c2f0Sahrens 		switch (save_errno) {
3623fa9e4066Sahrens 		case EEXIST:
3624681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
362599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3626681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3627681d9761SEric Taylor 			    zc.zc_string, parent);
362899653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3629fa9e4066Sahrens 
3630fa9e4066Sahrens 		default:
36310b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3632fa9e4066Sahrens 		}
3633fa9e4066Sahrens 	}
3634e9dbad6fSeschrock 	return (ret);
36351d452cf5Sahrens }
36361d452cf5Sahrens 
36374445fffbSMatthew Ahrens typedef struct snapdata {
36384445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
36394445fffbSMatthew Ahrens 	const char *sd_snapname;
36404445fffbSMatthew Ahrens } snapdata_t;
36414445fffbSMatthew Ahrens 
36424445fffbSMatthew Ahrens static int
zfs_snapshot_cb(zfs_handle_t * zhp,void * arg)36434445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
36444445fffbSMatthew Ahrens {
36454445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
3646*a1988827SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
36474445fffbSMatthew Ahrens 	int rv = 0;
36484445fffbSMatthew Ahrens 
3649ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
36504445fffbSMatthew Ahrens 		(void) snprintf(name, sizeof (name),
36514445fffbSMatthew Ahrens 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
36524445fffbSMatthew Ahrens 
36534445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd->sd_nvl, name);
36544445fffbSMatthew Ahrens 
36554445fffbSMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3656ca48f36fSKeith M Wesolowski 	}
36574445fffbSMatthew Ahrens 	zfs_close(zhp);
3658ca48f36fSKeith M Wesolowski 
36594445fffbSMatthew Ahrens 	return (rv);
36604445fffbSMatthew Ahrens }
36614445fffbSMatthew Ahrens 
3662fa9e4066Sahrens /*
36634445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
36644445fffbSMatthew Ahrens  * created.
3665fa9e4066Sahrens  */
3666fa9e4066Sahrens int
zfs_snapshot_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,nvlist_t * props)36674445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
36684445fffbSMatthew Ahrens {
36694445fffbSMatthew Ahrens 	int ret;
36704445fffbSMatthew Ahrens 	char errbuf[1024];
36714445fffbSMatthew Ahrens 	nvpair_t *elem;
36724445fffbSMatthew Ahrens 	nvlist_t *errors;
36734445fffbSMatthew Ahrens 
36744445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36754445fffbSMatthew Ahrens 	    "cannot create snapshots "));
36764445fffbSMatthew Ahrens 
36774445fffbSMatthew Ahrens 	elem = NULL;
36784445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
36794445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
36804445fffbSMatthew Ahrens 
36814445fffbSMatthew Ahrens 		/* validate the target name */
36824445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
36834445fffbSMatthew Ahrens 		    B_TRUE)) {
36844445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
36854445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
36864445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
36874445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
36884445fffbSMatthew Ahrens 		}
36894445fffbSMatthew Ahrens 	}
36904445fffbSMatthew Ahrens 
36914445fffbSMatthew Ahrens 	if (props != NULL &&
36924445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
36934445fffbSMatthew Ahrens 	    props, B_FALSE, NULL, errbuf)) == NULL) {
36944445fffbSMatthew Ahrens 		return (-1);
36954445fffbSMatthew Ahrens 	}
36964445fffbSMatthew Ahrens 
36974445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
36984445fffbSMatthew Ahrens 
36994445fffbSMatthew Ahrens 	if (ret != 0) {
37004445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
37014445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
37024445fffbSMatthew Ahrens 		    elem != NULL;
37034445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
37044445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
37054445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
37064445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
37074445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
37084445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
37094445fffbSMatthew Ahrens 			printed = B_TRUE;
37104445fffbSMatthew Ahrens 		}
37114445fffbSMatthew Ahrens 		if (!printed) {
37124445fffbSMatthew Ahrens 			switch (ret) {
37134445fffbSMatthew Ahrens 			case EXDEV:
37144445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37154445fffbSMatthew Ahrens 				    "multiple snapshots of same "
37164445fffbSMatthew Ahrens 				    "fs not allowed"));
37174445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
37184445fffbSMatthew Ahrens 
37194445fffbSMatthew Ahrens 				break;
37204445fffbSMatthew Ahrens 			default:
37214445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
37224445fffbSMatthew Ahrens 			}
37234445fffbSMatthew Ahrens 		}
37244445fffbSMatthew Ahrens 	}
37254445fffbSMatthew Ahrens 
37264445fffbSMatthew Ahrens 	nvlist_free(props);
37274445fffbSMatthew Ahrens 	nvlist_free(errors);
37284445fffbSMatthew Ahrens 	return (ret);
37294445fffbSMatthew Ahrens }
37304445fffbSMatthew Ahrens 
37314445fffbSMatthew Ahrens int
zfs_snapshot(libzfs_handle_t * hdl,const char * path,boolean_t recursive,nvlist_t * props)3732bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3733bb0ade09Sahrens     nvlist_t *props)
3734fa9e4066Sahrens {
3735fa9e4066Sahrens 	int ret;
37364445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
3737*a1988827SMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
37384445fffbSMatthew Ahrens 	char *cp;
37394445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
374099653d4eSeschrock 	char errbuf[1024];
3741fa9e4066Sahrens 
374299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37434445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
374499653d4eSeschrock 
3745f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
374699653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3747fa9e4066Sahrens 
37484445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
37494445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
37504445fffbSMatthew Ahrens 	*cp = '\0';
37514445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
3752bb0ade09Sahrens 
37534445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3754fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3755fa9e4066Sahrens 		return (-1);
3756fa9e4066Sahrens 	}
3757fa9e4066Sahrens 
37584445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
37594445fffbSMatthew Ahrens 	if (recursive) {
37604445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
37614445fffbSMatthew Ahrens 	} else {
37624445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
3763681d9761SEric Taylor 	}
3764fa9e4066Sahrens 
37654445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
37664445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
3767fa9e4066Sahrens 	zfs_close(zhp);
3768fa9e4066Sahrens 	return (ret);
3769fa9e4066Sahrens }
3770fa9e4066Sahrens 
3771fa9e4066Sahrens /*
3772b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3773b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3774b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3775b12a1c38Slling  * group.
3776fa9e4066Sahrens  */
3777b12a1c38Slling typedef struct rollback_data {
3778b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3779b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3780c391e322Sahrens 	boolean_t	cb_error;
3781c391e322Sahrens 	boolean_t	cb_force;
3782b12a1c38Slling } rollback_data_t;
3783b12a1c38Slling 
3784b12a1c38Slling static int
rollback_destroy_dependent(zfs_handle_t * zhp,void * data)378578f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3786b12a1c38Slling {
3787b12a1c38Slling 	rollback_data_t *cbp = data;
3788c391e322Sahrens 	prop_changelist_t *clp;
3789c391e322Sahrens 
379078f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
37910069fd67STim Haley 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3792c391e322Sahrens 	    cbp->cb_force ? MS_FORCE: 0);
3793c391e322Sahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
3794c391e322Sahrens 		cbp->cb_error = B_TRUE;
3795c391e322Sahrens 		zfs_close(zhp);
3796c391e322Sahrens 		return (0);
3797c391e322Sahrens 	}
3798842727c2SChris Kirby 	if (zfs_destroy(zhp, B_FALSE) != 0)
3799c391e322Sahrens 		cbp->cb_error = B_TRUE;
3800c391e322Sahrens 	else
3801c391e322Sahrens 		changelist_remove(clp, zhp->zfs_name);
3802ba7b046eSahrens 	(void) changelist_postfix(clp);
3803c391e322Sahrens 	changelist_free(clp);
380478f17100SMatthew Ahrens 
380578f17100SMatthew Ahrens 	zfs_close(zhp);
380678f17100SMatthew Ahrens 	return (0);
380778f17100SMatthew Ahrens }
380878f17100SMatthew Ahrens 
380978f17100SMatthew Ahrens static int
rollback_destroy(zfs_handle_t * zhp,void * data)381078f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
381178f17100SMatthew Ahrens {
381278f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
381378f17100SMatthew Ahrens 
381478f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
381578f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
381678f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
381778f17100SMatthew Ahrens 
381878f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3819b12a1c38Slling 	}
3820b12a1c38Slling 
3821b12a1c38Slling 	zfs_close(zhp);
3822b12a1c38Slling 	return (0);
3823b12a1c38Slling }
3824b12a1c38Slling 
3825b12a1c38Slling /*
38264ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
38274ccbb6e7Sahrens  * data changes since then and making it the active dataset.
38284ccbb6e7Sahrens  *
382978f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
383078f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
3831b12a1c38Slling  */
38324ccbb6e7Sahrens int
zfs_rollback(zfs_handle_t * zhp,zfs_handle_t * snap,boolean_t force)3833c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3834fa9e4066Sahrens {
38354ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
38364ccbb6e7Sahrens 	int err;
38377b97dc1aSrm160521 	boolean_t restore_resv = 0;
38387b97dc1aSrm160521 	uint64_t old_volsize, new_volsize;
38397b97dc1aSrm160521 	zfs_prop_t resv_prop;
3840fa9e4066Sahrens 
3841fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3842fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3843fa9e4066Sahrens 
38444ccbb6e7Sahrens 	/*
38452e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
38464ccbb6e7Sahrens 	 */
3847c391e322Sahrens 	cb.cb_force = force;
38484ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
38494ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
385078f17100SMatthew Ahrens 	(void) zfs_iter_snapshots(zhp, rollback_destroy, &cb);
385178f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
38524ccbb6e7Sahrens 
3853c391e322Sahrens 	if (cb.cb_error)
3854c391e322Sahrens 		return (-1);
38554ccbb6e7Sahrens 
38564ccbb6e7Sahrens 	/*
38574ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
38584ccbb6e7Sahrens 	 * rollback to the given snapshot.
38594ccbb6e7Sahrens 	 */
38604ccbb6e7Sahrens 
38617b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
38627b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
38637b97dc1aSrm160521 			return (-1);
38647b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
38657b97dc1aSrm160521 		restore_resv =
38667b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
38677b97dc1aSrm160521 	}
3868fa9e4066Sahrens 
3869fa9e4066Sahrens 	/*
38704ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
38714ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
38724ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
38734ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
38744ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
3875fa9e4066Sahrens 	 */
3876a7027df1SMatthew Ahrens 	err = lzc_rollback(zhp->zfs_name, NULL, 0);
3877a7027df1SMatthew Ahrens 	if (err != 0) {
3878ece3d9b3Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
387999653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
388099653d4eSeschrock 		    zhp->zfs_name);
3881b9415e83Srm160521 		return (err);
3882b9415e83Srm160521 	}
3883fa9e4066Sahrens 
38847b97dc1aSrm160521 	/*
38857b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
38867b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
38877b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
38887b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
38897b97dc1aSrm160521 	 */
3890b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3891b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
38927b97dc1aSrm160521 		if (restore_resv) {
38937b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
38947b97dc1aSrm160521 			if (old_volsize != new_volsize)
3895b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
3896b9415e83Srm160521 				    new_volsize);
38977b97dc1aSrm160521 		}
38987b97dc1aSrm160521 		zfs_close(zhp);
38997b97dc1aSrm160521 	}
39004ccbb6e7Sahrens 	return (err);
3901b12a1c38Slling }
3902b12a1c38Slling 
3903b12a1c38Slling /*
3904fa9e4066Sahrens  * Renames the given dataset.
3905fa9e4066Sahrens  */
3906fa9e4066Sahrens int
zfs_rename(zfs_handle_t * zhp,const char * target,boolean_t recursive,boolean_t force_unmount)39076a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
39086a9cb0eaSEric Schrock     boolean_t force_unmount)
3909fa9e4066Sahrens {
3910fa9e4066Sahrens 	int ret;
3911fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3912fa9e4066Sahrens 	char *delim;
3913cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3914cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3915cdf5b4caSmmusante 	char *parentname = NULL;
3916*a1988827SMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
391799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
391899653d4eSeschrock 	char errbuf[1024];
3919fa9e4066Sahrens 
3920fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3921fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3922fa9e4066Sahrens 		return (0);
3923fa9e4066Sahrens 
392499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
392599653d4eSeschrock 	    "cannot rename to '%s'"), target);
392699653d4eSeschrock 
3927fa9e4066Sahrens 	/*
3928fa9e4066Sahrens 	 * Make sure the target name is valid
3929fa9e4066Sahrens 	 */
3930fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
393198579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
393298579b20Snd150628 		    *target == '@') {
393398579b20Snd150628 			/*
393498579b20Snd150628 			 * Snapshot target name is abbreviated,
393598579b20Snd150628 			 * reconstruct full dataset name
393698579b20Snd150628 			 */
393798579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
393898579b20Snd150628 			    sizeof (parent));
393998579b20Snd150628 			delim = strchr(parent, '@');
394098579b20Snd150628 			if (strchr(target, '@') == NULL)
394198579b20Snd150628 				*(++delim) = '\0';
394298579b20Snd150628 			else
394398579b20Snd150628 				*delim = '\0';
394498579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
394598579b20Snd150628 			target = parent;
394698579b20Snd150628 		} else {
3947fa9e4066Sahrens 			/*
3948fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
3949fa9e4066Sahrens 			 */
395098579b20Snd150628 			delim = strchr(target, '@');
395198579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
395298579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
395399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
395498579b20Snd150628 				    "snapshots must be part of same "
395598579b20Snd150628 				    "dataset"));
395698579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
395798579b20Snd150628 				    errbuf));
3958fa9e4066Sahrens 			}
395998579b20Snd150628 		}
3960f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
396198579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3962fa9e4066Sahrens 	} else {
3963cdf5b4caSmmusante 		if (recursive) {
3964cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3965cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
3966cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3967cdf5b4caSmmusante 		}
3968cdf5b4caSmmusante 
3969f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
397098579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3971e9dbad6fSeschrock 
3972fa9e4066Sahrens 		/* validate parents */
3973d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3974fa9e4066Sahrens 			return (-1);
3975fa9e4066Sahrens 
3976fa9e4066Sahrens 		/* make sure we're in the same pool */
3977fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3978fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3979fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
398099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
398199653d4eSeschrock 			    "datasets must be within same pool"));
398299653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3983fa9e4066Sahrens 		}
3984f2fdf992Snd150628 
3985f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
3986d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
3987f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3988d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
3989f2fdf992Snd150628 			    "current dataset name"));
3990f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3991f2fdf992Snd150628 		}
3992fa9e4066Sahrens 	}
3993fa9e4066Sahrens 
399499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
399599653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
399699653d4eSeschrock 
3997fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3998fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
399999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
400099653d4eSeschrock 		    "dataset is used in a non-global zone"));
400199653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4002fa9e4066Sahrens 	}
4003fa9e4066Sahrens 
4004cdf5b4caSmmusante 	if (recursive) {
4005f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4006f0c5ee21Smmusante 		if (parentname == NULL) {
4007f0c5ee21Smmusante 			ret = -1;
4008f0c5ee21Smmusante 			goto error;
4009f0c5ee21Smmusante 		}
4010cdf5b4caSmmusante 		delim = strchr(parentname, '@');
4011cdf5b4caSmmusante 		*delim = '\0';
4012990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4013cdf5b4caSmmusante 		if (zhrp == NULL) {
4014f0c5ee21Smmusante 			ret = -1;
4015f0c5ee21Smmusante 			goto error;
4016cdf5b4caSmmusante 		}
401733cde0d0SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
40186a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
40196a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
402099653d4eSeschrock 			return (-1);
4021fa9e4066Sahrens 
4022fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
402399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
402499653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
402599653d4eSeschrock 			    "in a non-global zone"));
4026e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4027fa9e4066Sahrens 			goto error;
4028fa9e4066Sahrens 		}
4029fa9e4066Sahrens 
4030fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
4031fa9e4066Sahrens 			goto error;
4032cdf5b4caSmmusante 	}
4033fa9e4066Sahrens 
4034e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
4035fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4036fa9e4066Sahrens 	else
4037fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4038fa9e4066Sahrens 
403998579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4040e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
404198579b20Snd150628 
4042cdf5b4caSmmusante 	zc.zc_cookie = recursive;
4043cdf5b4caSmmusante 
4044ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4045cdf5b4caSmmusante 		/*
4046cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
4047cdf5b4caSmmusante 		 * be in zc.zc_name
4048cdf5b4caSmmusante 		 */
4049cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40503cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
4051cdf5b4caSmmusante 
4052cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
4053cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4054cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
4055cdf5b4caSmmusante 			    "with the new name"));
4056a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4057cdf5b4caSmmusante 		} else {
405899653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4059cdf5b4caSmmusante 		}
4060fa9e4066Sahrens 
4061fa9e4066Sahrens 		/*
4062fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
4063fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
4064fa9e4066Sahrens 		 */
406533cde0d0SMatthew Ahrens 		if (cl != NULL)
4066fa9e4066Sahrens 			(void) changelist_postfix(cl);
4067cdf5b4caSmmusante 	} else {
406833cde0d0SMatthew Ahrens 		if (cl != NULL) {
4069fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
4070fa9e4066Sahrens 			ret = changelist_postfix(cl);
4071fa9e4066Sahrens 		}
4072cdf5b4caSmmusante 	}
4073fa9e4066Sahrens 
4074fa9e4066Sahrens error:
407533cde0d0SMatthew Ahrens 	if (parentname != NULL) {
4076cdf5b4caSmmusante 		free(parentname);
4077cdf5b4caSmmusante 	}
407833cde0d0SMatthew Ahrens 	if (zhrp != NULL) {
4079cdf5b4caSmmusante 		zfs_close(zhrp);
4080cdf5b4caSmmusante 	}
408133cde0d0SMatthew Ahrens 	if (cl != NULL) {
4082fa9e4066Sahrens 		changelist_free(cl);
4083cdf5b4caSmmusante 	}
4084fa9e4066Sahrens 	return (ret);
4085fa9e4066Sahrens }
4086fa9e4066Sahrens 
4087e9dbad6fSeschrock nvlist_t *
zfs_get_user_props(zfs_handle_t * zhp)4088e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4089e9dbad6fSeschrock {
4090e9dbad6fSeschrock 	return (zhp->zfs_user_props);
4091e9dbad6fSeschrock }
4092e9dbad6fSeschrock 
409392241e0bSTom Erickson nvlist_t *
zfs_get_recvd_props(zfs_handle_t * zhp)409492241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
409592241e0bSTom Erickson {
409692241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
409792241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
409892241e0bSTom Erickson 			return (NULL);
409992241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
410092241e0bSTom Erickson }
410192241e0bSTom Erickson 
4102e9dbad6fSeschrock /*
4103e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
4104e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
4105e9dbad6fSeschrock  *
4106e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
4107e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
4108e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
4109e9dbad6fSeschrock  *
4110e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
411192241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
411292241e0bSTom Erickson  *        requested received property values, we also need to compute the width
411392241e0bSTom Erickson  *        of the RECEIVED column.
4114e9dbad6fSeschrock  */
4115e9dbad6fSeschrock int
zfs_expand_proplist(zfs_handle_t * zhp,zprop_list_t ** plp,boolean_t received,boolean_t literal)411643d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
411743d68d68SYuri Pankov     boolean_t literal)
4118e9dbad6fSeschrock {
4119e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4120990b4856Slling 	zprop_list_t *entry;
4121990b4856Slling 	zprop_list_t **last, **start;
4122e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
4123e9dbad6fSeschrock 	nvpair_t *elem;
4124e9dbad6fSeschrock 	char *strval;
4125e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
4126e9dbad6fSeschrock 
4127990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4128e9dbad6fSeschrock 		return (-1);
4129e9dbad6fSeschrock 
4130e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4131e9dbad6fSeschrock 
4132e9dbad6fSeschrock 	entry = *plp;
4133e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4134e9dbad6fSeschrock 		/*
4135e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4136e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4137e9dbad6fSeschrock 		 * non-native property.
4138e9dbad6fSeschrock 		 */
4139e9dbad6fSeschrock 		start = plp;
4140e9dbad6fSeschrock 		while (*start != NULL) {
4141990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4142e9dbad6fSeschrock 				break;
4143e9dbad6fSeschrock 			start = &(*start)->pl_next;
4144e9dbad6fSeschrock 		}
4145e9dbad6fSeschrock 
4146e9dbad6fSeschrock 		elem = NULL;
4147e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4148e9dbad6fSeschrock 			/*
4149e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4150e9dbad6fSeschrock 			 */
4151e9dbad6fSeschrock 			for (last = start; *last != NULL;
4152e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4153e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4154e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4155e9dbad6fSeschrock 					break;
4156e9dbad6fSeschrock 			}
4157e9dbad6fSeschrock 
4158e9dbad6fSeschrock 			if (*last == NULL) {
4159e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4160990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4161e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4162e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4163e9dbad6fSeschrock 					free(entry);
4164e9dbad6fSeschrock 					return (-1);
4165e9dbad6fSeschrock 				}
4166e9dbad6fSeschrock 
4167990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4168e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4169e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4170e9dbad6fSeschrock 				*last = entry;
4171e9dbad6fSeschrock 			}
4172e9dbad6fSeschrock 		}
4173e9dbad6fSeschrock 	}
4174e9dbad6fSeschrock 
4175e9dbad6fSeschrock 	/*
4176e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4177e9dbad6fSeschrock 	 */
4178e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
417943d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
4180e9dbad6fSeschrock 			continue;
4181e9dbad6fSeschrock 
4182990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4183e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
418443d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4185e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4186e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4187e9dbad6fSeschrock 			}
418892241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
418992241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
419043d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
419192241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
419292241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
419392241e0bSTom Erickson 		} else {
419492241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
419592241e0bSTom Erickson 			    &propval) == 0) {
4196e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
4197990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
4198e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
4199e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
4200e9dbad6fSeschrock 			}
420192241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
420292241e0bSTom Erickson 			    entry->pl_user_prop,
420343d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
420492241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
420592241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
420692241e0bSTom Erickson 		}
4207e9dbad6fSeschrock 	}
4208e9dbad6fSeschrock 
4209e9dbad6fSeschrock 	return (0);
4210e9dbad6fSeschrock }
4211ecd6cf80Smarks 
4212ecd6cf80Smarks int
zfs_deleg_share_nfs(libzfs_handle_t * hdl,char * dataset,char * path,char * resource,void * export,void * sharetab,int sharemax,zfs_share_op_t operation)4213ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4214743a77edSAlan Wright     char *resource, void *export, void *sharetab,
4215743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
4216ecd6cf80Smarks {
4217ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4218ecd6cf80Smarks 	int error;
4219ecd6cf80Smarks 
4220ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4221ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4222743a77edSAlan Wright 	if (resource)
4223743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4224ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4225ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4226da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4227ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4228ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4229ecd6cf80Smarks 	return (error);
4230ecd6cf80Smarks }
42312e5e9e19SSanjeev Bagewadi 
42322e5e9e19SSanjeev Bagewadi void
zfs_prune_proplist(zfs_handle_t * zhp,uint8_t * props)42332e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
42342e5e9e19SSanjeev Bagewadi {
42352e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
42362e5e9e19SSanjeev Bagewadi 
42372e5e9e19SSanjeev Bagewadi 	/*
42382e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
42392e5e9e19SSanjeev Bagewadi 	 * properties.
42402e5e9e19SSanjeev Bagewadi 	 */
42412e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
42422e5e9e19SSanjeev Bagewadi 
42432e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
42442e5e9e19SSanjeev Bagewadi 
42452e5e9e19SSanjeev Bagewadi 	while (curr) {
42462e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
42472e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
42482e5e9e19SSanjeev Bagewadi 
424914843421SMatthew Ahrens 		/*
4250faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4251faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4252faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4253faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4254faaa6415SEric Schrock 		 * software, for example).
425514843421SMatthew Ahrens 		 */
425614843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
42572e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
42582e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
42592e5e9e19SSanjeev Bagewadi 		curr = next;
42602e5e9e19SSanjeev Bagewadi 	}
42612e5e9e19SSanjeev Bagewadi }
4262743a77edSAlan Wright 
4263743a77edSAlan Wright static int
zfs_smb_acl_mgmt(libzfs_handle_t * hdl,char * dataset,char * path,zfs_smb_acl_op_t cmd,char * resource1,char * resource2)4264743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4265743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4266743a77edSAlan Wright {
4267743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4268743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4269743a77edSAlan Wright 	int error;
4270743a77edSAlan Wright 
4271743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4272743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4273743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4274743a77edSAlan Wright 
4275743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4276743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4277743a77edSAlan Wright 			(void) no_memory(hdl);
427830925561SChris Williamson 			return (0);
4279743a77edSAlan Wright 		}
4280743a77edSAlan Wright 	}
4281743a77edSAlan Wright 
4282743a77edSAlan Wright 	switch (cmd) {
4283743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4284743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4285743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4286743a77edSAlan Wright 		break;
4287743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4288743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4289743a77edSAlan Wright 		    resource1) != 0) {
4290743a77edSAlan Wright 				(void) no_memory(hdl);
4291743a77edSAlan Wright 				return (-1);
4292743a77edSAlan Wright 		}
4293743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4294743a77edSAlan Wright 		    resource2) != 0) {
4295743a77edSAlan Wright 				(void) no_memory(hdl);
4296743a77edSAlan Wright 				return (-1);
4297743a77edSAlan Wright 		}
4298743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4299743a77edSAlan Wright 			nvlist_free(nvlist);
4300743a77edSAlan Wright 			return (-1);
4301743a77edSAlan Wright 		}
4302743a77edSAlan Wright 		break;
4303743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4304743a77edSAlan Wright 		break;
4305743a77edSAlan Wright 	default:
4306743a77edSAlan Wright 		return (-1);
4307743a77edSAlan Wright 	}
4308743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4309743a77edSAlan Wright 	nvlist_free(nvlist);
4310743a77edSAlan Wright 	return (error);
4311743a77edSAlan Wright }
4312743a77edSAlan Wright 
4313743a77edSAlan Wright int
zfs_smb_acl_add(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4314743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4315743a77edSAlan Wright     char *path, char *resource)
4316743a77edSAlan Wright {
4317743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4318743a77edSAlan Wright 	    resource, NULL));
4319743a77edSAlan Wright }
4320743a77edSAlan Wright 
4321743a77edSAlan Wright int
zfs_smb_acl_remove(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4322743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4323743a77edSAlan Wright     char *path, char *resource)
4324743a77edSAlan Wright {
4325743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4326743a77edSAlan Wright 	    resource, NULL));
4327743a77edSAlan Wright }
4328743a77edSAlan Wright 
4329743a77edSAlan Wright int
zfs_smb_acl_purge(libzfs_handle_t * hdl,char * dataset,char * path)4330743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4331743a77edSAlan Wright {
4332743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4333743a77edSAlan Wright 	    NULL, NULL));
4334743a77edSAlan Wright }
4335743a77edSAlan Wright 
4336743a77edSAlan Wright int
zfs_smb_acl_rename(libzfs_handle_t * hdl,char * dataset,char * path,char * oldname,char * newname)4337743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4338743a77edSAlan Wright     char *oldname, char *newname)
4339743a77edSAlan Wright {
4340743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4341743a77edSAlan Wright 	    oldname, newname));
4342743a77edSAlan Wright }
434314843421SMatthew Ahrens 
434414843421SMatthew Ahrens int
zfs_userspace(zfs_handle_t * zhp,zfs_userquota_prop_t type,zfs_userspace_cb_t func,void * arg)434514843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
434614843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
434714843421SMatthew Ahrens {
434814843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
434914843421SMatthew Ahrens 	zfs_useracct_t buf[100];
435070f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
435170f56fa6SYuri Pankov 	int ret;
435214843421SMatthew Ahrens 
435319b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
435414843421SMatthew Ahrens 
435514843421SMatthew Ahrens 	zc.zc_objset_type = type;
435614843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
435714843421SMatthew Ahrens 
435870f56fa6SYuri Pankov 	for (;;) {
435914843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
436014843421SMatthew Ahrens 
436114843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
436270f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
43633b2aab18SMatthew Ahrens 			char errbuf[1024];
436470f56fa6SYuri Pankov 
436570f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
436670f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
436770f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
436870f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
436970f56fa6SYuri Pankov 		}
437070f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
437114843421SMatthew Ahrens 			break;
437214843421SMatthew Ahrens 
437314843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
437470f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
437570f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
437670f56fa6SYuri Pankov 				return (ret);
437714843421SMatthew Ahrens 			zua++;
437814843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
437914843421SMatthew Ahrens 		}
438014843421SMatthew Ahrens 	}
438114843421SMatthew Ahrens 
438270f56fa6SYuri Pankov 	return (0);
438314843421SMatthew Ahrens }
4384842727c2SChris Kirby 
43853b2aab18SMatthew Ahrens struct holdarg {
43863b2aab18SMatthew Ahrens 	nvlist_t *nvl;
43873b2aab18SMatthew Ahrens 	const char *snapname;
43883b2aab18SMatthew Ahrens 	const char *tag;
43893b2aab18SMatthew Ahrens 	boolean_t recursive;
4390bb6e7075SMatthew Ahrens 	int error;
43913b2aab18SMatthew Ahrens };
43923b2aab18SMatthew Ahrens 
43933b2aab18SMatthew Ahrens static int
zfs_hold_one(zfs_handle_t * zhp,void * arg)43943b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
43953b2aab18SMatthew Ahrens {
43963b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
4397*a1988827SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
43983b2aab18SMatthew Ahrens 	int rv = 0;
43993b2aab18SMatthew Ahrens 
44003b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
44013b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
44023b2aab18SMatthew Ahrens 
4403a7a845e4SSteven Hartland 	if (lzc_exists(name))
44043b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
44053b2aab18SMatthew Ahrens 
44063b2aab18SMatthew Ahrens 	if (ha->recursive)
44073b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
44083b2aab18SMatthew Ahrens 	zfs_close(zhp);
44093b2aab18SMatthew Ahrens 	return (rv);
44103b2aab18SMatthew Ahrens }
44113b2aab18SMatthew Ahrens 
4412842727c2SChris Kirby int
zfs_hold(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive,int cleanup_fd)4413842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4414a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4415842727c2SChris Kirby {
44163b2aab18SMatthew Ahrens 	int ret;
44173b2aab18SMatthew Ahrens 	struct holdarg ha;
4418842727c2SChris Kirby 
44193b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
44203b2aab18SMatthew Ahrens 	ha.snapname = snapname;
44213b2aab18SMatthew Ahrens 	ha.tag = tag;
44223b2aab18SMatthew Ahrens 	ha.recursive = recursive;
44233b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4424013023d4SMartin Matuska 
4425a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4426a7a845e4SSteven Hartland 		char errbuf[1024];
4427a7a845e4SSteven Hartland 
4428013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4429013023d4SMartin Matuska 		ret = ENOENT;
4430013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4431013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4432013023d4SMartin Matuska 		    "cannot hold snapshot '%s@%s'"),
4433013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4434a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4435013023d4SMartin Matuska 		return (ret);
4436013023d4SMartin Matuska 	}
4437013023d4SMartin Matuska 
4438a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
44393b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4440a7f53a56SChris Kirby 
4441a7a845e4SSteven Hartland 	return (ret);
4442a7a845e4SSteven Hartland }
4443842727c2SChris Kirby 
4444a7a845e4SSteven Hartland int
zfs_hold_nvl(zfs_handle_t * zhp,int cleanup_fd,nvlist_t * holds)4445a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4446a7a845e4SSteven Hartland {
4447a7a845e4SSteven Hartland 	int ret;
4448a7a845e4SSteven Hartland 	nvlist_t *errors;
4449a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4450a7a845e4SSteven Hartland 	char errbuf[1024];
4451a7a845e4SSteven Hartland 	nvpair_t *elem;
4452a7a845e4SSteven Hartland 
4453a7a845e4SSteven Hartland 	errors = NULL;
4454a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4455a7a845e4SSteven Hartland 
4456a7a845e4SSteven Hartland 	if (ret == 0) {
4457a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4458a7a845e4SSteven Hartland 		fnvlist_free(errors);
4459a7a845e4SSteven Hartland 		return (0);
4460a7a845e4SSteven Hartland 	}
4461a7a845e4SSteven Hartland 
4462a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
44633b2aab18SMatthew Ahrens 		/* no hold-specific errors */
44643b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
44653b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
44663b2aab18SMatthew Ahrens 		switch (ret) {
44673b2aab18SMatthew Ahrens 		case ENOTSUP:
44683b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44693b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
44703b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
44713b2aab18SMatthew Ahrens 			break;
44723b2aab18SMatthew Ahrens 		case EINVAL:
44733b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
44743b2aab18SMatthew Ahrens 			break;
44753b2aab18SMatthew Ahrens 		default:
44763b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
44773b2aab18SMatthew Ahrens 		}
44783b2aab18SMatthew Ahrens 	}
4479842727c2SChris Kirby 
44803b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
44813b2aab18SMatthew Ahrens 	    elem != NULL;
44823b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
44833b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
44843b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
44853b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
44863b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
448715508ac0SChris Kirby 		case E2BIG:
448815508ac0SChris Kirby 			/*
448915508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
449015508ac0SChris Kirby 			 * prepended. So even if we passed the length check
449115508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
449215508ac0SChris Kirby 			 * up being slightly too long.
449315508ac0SChris Kirby 			 */
44943b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
44953b2aab18SMatthew Ahrens 			break;
4496842727c2SChris Kirby 		case EINVAL:
44973b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
44983b2aab18SMatthew Ahrens 			break;
4499842727c2SChris Kirby 		case EEXIST:
45003b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
45013b2aab18SMatthew Ahrens 			break;
4502842727c2SChris Kirby 		default:
45033b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
45043b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4505842727c2SChris Kirby 		}
4506842727c2SChris Kirby 	}
4507842727c2SChris Kirby 
45083b2aab18SMatthew Ahrens 	fnvlist_free(errors);
45093b2aab18SMatthew Ahrens 	return (ret);
45103b2aab18SMatthew Ahrens }
45113b2aab18SMatthew Ahrens 
45123b2aab18SMatthew Ahrens static int
zfs_release_one(zfs_handle_t * zhp,void * arg)45133b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
45143b2aab18SMatthew Ahrens {
45153b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
4516*a1988827SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
45173b2aab18SMatthew Ahrens 	int rv = 0;
4518bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
45193b2aab18SMatthew Ahrens 
45203b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
45213b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
45223b2aab18SMatthew Ahrens 
4523bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4524bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4525bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4526bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4527bb6e7075SMatthew Ahrens 	} else {
4528bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4529bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4530bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4531bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
45323b2aab18SMatthew Ahrens 	}
45333b2aab18SMatthew Ahrens 
45343b2aab18SMatthew Ahrens 	if (ha->recursive)
45353b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
45363b2aab18SMatthew Ahrens 	zfs_close(zhp);
45373b2aab18SMatthew Ahrens 	return (rv);
4538842727c2SChris Kirby }
4539842727c2SChris Kirby 
4540842727c2SChris Kirby int
zfs_release(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive)4541842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4542842727c2SChris Kirby     boolean_t recursive)
4543842727c2SChris Kirby {
45443b2aab18SMatthew Ahrens 	int ret;
45453b2aab18SMatthew Ahrens 	struct holdarg ha;
4546a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
45473b2aab18SMatthew Ahrens 	nvpair_t *elem;
4548842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4549013023d4SMartin Matuska 	char errbuf[1024];
4550842727c2SChris Kirby 
45513b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
45523b2aab18SMatthew Ahrens 	ha.snapname = snapname;
45533b2aab18SMatthew Ahrens 	ha.tag = tag;
45543b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4555bb6e7075SMatthew Ahrens 	ha.error = 0;
45563b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4557013023d4SMartin Matuska 
4558a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4559013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4560bb6e7075SMatthew Ahrens 		ret = ha.error;
4561013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4562013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4563013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
4564013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4565bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
4566bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4567bb6e7075SMatthew Ahrens 		} else {
4568013023d4SMartin Matuska 			(void) zfs_standard_error(hdl, ret, errbuf);
4569bb6e7075SMatthew Ahrens 		}
4570013023d4SMartin Matuska 		return (ret);
4571013023d4SMartin Matuska 	}
4572013023d4SMartin Matuska 
45733b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
45743b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4575842727c2SChris Kirby 
4576a7a845e4SSteven Hartland 	if (ret == 0) {
4577a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4578a7a845e4SSteven Hartland 		fnvlist_free(errors);
45793b2aab18SMatthew Ahrens 		return (0);
4580a7a845e4SSteven Hartland 	}
4581842727c2SChris Kirby 
4582a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
45833b2aab18SMatthew Ahrens 		/* no hold-specific errors */
4584842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
45853b2aab18SMatthew Ahrens 		    "cannot release"));
4586842727c2SChris Kirby 		switch (errno) {
4587842727c2SChris Kirby 		case ENOTSUP:
4588842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4589842727c2SChris Kirby 			    "pool must be upgraded"));
45903b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
45913b2aab18SMatthew Ahrens 			break;
4592842727c2SChris Kirby 		default:
45933b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4594842727c2SChris Kirby 		}
4595842727c2SChris Kirby 	}
4596842727c2SChris Kirby 
45973b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
45983b2aab18SMatthew Ahrens 	    elem != NULL;
45993b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
46003b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
46013b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
46023b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
46033b2aab18SMatthew Ahrens 		    nvpair_name(elem));
46043b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
46053b2aab18SMatthew Ahrens 		case ESRCH:
46063b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
46073b2aab18SMatthew Ahrens 			break;
46083b2aab18SMatthew Ahrens 		case EINVAL:
46093b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
46103b2aab18SMatthew Ahrens 			break;
46113b2aab18SMatthew Ahrens 		default:
46123b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
46133b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
46143b2aab18SMatthew Ahrens 		}
46153b2aab18SMatthew Ahrens 	}
46163b2aab18SMatthew Ahrens 
46173b2aab18SMatthew Ahrens 	fnvlist_free(errors);
46183b2aab18SMatthew Ahrens 	return (ret);
4619842727c2SChris Kirby }
4620ca45db41SChris Kirby 
46211af68beaSAlexander Stetsenko int
zfs_get_fsacl(zfs_handle_t * zhp,nvlist_t ** nvl)46221af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
46231af68beaSAlexander Stetsenko {
46241af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
46251af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
46261af68beaSAlexander Stetsenko 	int nvsz = 2048;
46271af68beaSAlexander Stetsenko 	void *nvbuf;
46281af68beaSAlexander Stetsenko 	int err = 0;
46293b2aab18SMatthew Ahrens 	char errbuf[1024];
46301af68beaSAlexander Stetsenko 
46311af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
46321af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
46331af68beaSAlexander Stetsenko 
46341af68beaSAlexander Stetsenko tryagain:
46351af68beaSAlexander Stetsenko 
46361af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
46371af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
46381af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
46391af68beaSAlexander Stetsenko 		goto out;
46401af68beaSAlexander Stetsenko 	}
46411af68beaSAlexander Stetsenko 
46421af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
46431af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
46441af68beaSAlexander Stetsenko 
4645*a1988827SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
46461af68beaSAlexander Stetsenko 
4647d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
46481af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
46491af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
46501af68beaSAlexander Stetsenko 		    zc.zc_name);
46511af68beaSAlexander Stetsenko 		switch (errno) {
46521af68beaSAlexander Stetsenko 		case ENOMEM:
46531af68beaSAlexander Stetsenko 			free(nvbuf);
46541af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
46551af68beaSAlexander Stetsenko 			goto tryagain;
46561af68beaSAlexander Stetsenko 
46571af68beaSAlexander Stetsenko 		case ENOTSUP:
46581af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46591af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
46601af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
46611af68beaSAlexander Stetsenko 			break;
46621af68beaSAlexander Stetsenko 		case EINVAL:
46631af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
46641af68beaSAlexander Stetsenko 			break;
46651af68beaSAlexander Stetsenko 		case ENOENT:
46661af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
46671af68beaSAlexander Stetsenko 			break;
46681af68beaSAlexander Stetsenko 		default:
46691af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
46701af68beaSAlexander Stetsenko 			break;
46711af68beaSAlexander Stetsenko 		}
46721af68beaSAlexander Stetsenko 	} else {
46731af68beaSAlexander Stetsenko 		/* success */
46741af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
46751af68beaSAlexander Stetsenko 		if (rc) {
46761af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
46771af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
46781af68beaSAlexander Stetsenko 			    zc.zc_name);
46791af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
46801af68beaSAlexander Stetsenko 		}
46811af68beaSAlexander Stetsenko 	}
46821af68beaSAlexander Stetsenko 
46831af68beaSAlexander Stetsenko 	free(nvbuf);
46841af68beaSAlexander Stetsenko out:
46851af68beaSAlexander Stetsenko 	return (err);
46861af68beaSAlexander Stetsenko }
46871af68beaSAlexander Stetsenko 
46881af68beaSAlexander Stetsenko int
zfs_set_fsacl(zfs_handle_t * zhp,boolean_t un,nvlist_t * nvl)46891af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
46901af68beaSAlexander Stetsenko {
46911af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
46921af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
46931af68beaSAlexander Stetsenko 	char *nvbuf;
46943b2aab18SMatthew Ahrens 	char errbuf[1024];
46951af68beaSAlexander Stetsenko 	size_t nvsz;
46961af68beaSAlexander Stetsenko 	int err;
46971af68beaSAlexander Stetsenko 
46981af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
46991af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
47001af68beaSAlexander Stetsenko 
47011af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
47021af68beaSAlexander Stetsenko 	assert(err == 0);
47031af68beaSAlexander Stetsenko 
47041af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
47051af68beaSAlexander Stetsenko 
47061af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
47071af68beaSAlexander Stetsenko 	assert(err == 0);
47081af68beaSAlexander Stetsenko 
47091af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
47101af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
47111af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
47121af68beaSAlexander Stetsenko 
47131af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
47141af68beaSAlexander Stetsenko 
47151af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
47161af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
47171af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
47181af68beaSAlexander Stetsenko 		    zc.zc_name);
47191af68beaSAlexander Stetsenko 		switch (errno) {
47201af68beaSAlexander Stetsenko 		case ENOTSUP:
47211af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
47221af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
47231af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
47241af68beaSAlexander Stetsenko 			break;
47251af68beaSAlexander Stetsenko 		case EINVAL:
47261af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
47271af68beaSAlexander Stetsenko 			break;
47281af68beaSAlexander Stetsenko 		case ENOENT:
47291af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
47301af68beaSAlexander Stetsenko 			break;
47311af68beaSAlexander Stetsenko 		default:
47321af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
47331af68beaSAlexander Stetsenko 			break;
47341af68beaSAlexander Stetsenko 		}
47351af68beaSAlexander Stetsenko 	}
47361af68beaSAlexander Stetsenko 
47371af68beaSAlexander Stetsenko 	free(nvbuf);
47381af68beaSAlexander Stetsenko 
47391af68beaSAlexander Stetsenko 	return (err);
47401af68beaSAlexander Stetsenko }
47411af68beaSAlexander Stetsenko 
47421af68beaSAlexander Stetsenko int
zfs_get_holds(zfs_handle_t * zhp,nvlist_t ** nvl)47431af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
47441af68beaSAlexander Stetsenko {
47453b2aab18SMatthew Ahrens 	int err;
47463b2aab18SMatthew Ahrens 	char errbuf[1024];
47473b2aab18SMatthew Ahrens 
47483b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
47493b2aab18SMatthew Ahrens 
47503b2aab18SMatthew Ahrens 	if (err != 0) {
47511af68beaSAlexander Stetsenko 		libzfs_handle_t *hdl = zhp->zfs_hdl;
47521af68beaSAlexander Stetsenko 
47531af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
47541af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
47553b2aab18SMatthew Ahrens 		    zhp->zfs_name);
47563b2aab18SMatthew Ahrens 		switch (err) {
47571af68beaSAlexander Stetsenko 		case ENOTSUP:
47581af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
47591af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
47601af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
47611af68beaSAlexander Stetsenko 			break;
47621af68beaSAlexander Stetsenko 		case EINVAL:
47631af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
47641af68beaSAlexander Stetsenko 			break;
47651af68beaSAlexander Stetsenko 		case ENOENT:
47661af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
47671af68beaSAlexander Stetsenko 			break;
47681af68beaSAlexander Stetsenko 		default:
47691af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
47701af68beaSAlexander Stetsenko 			break;
47711af68beaSAlexander Stetsenko 		}
47721af68beaSAlexander Stetsenko 	}
47731af68beaSAlexander Stetsenko 
47741af68beaSAlexander Stetsenko 	return (err);
47751af68beaSAlexander Stetsenko }
47761af68beaSAlexander Stetsenko 
47773e30c24aSWill Andrews /*
47783e30c24aSWill Andrews  * Convert the zvol's volume size to an appropriate reservation.
47793e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
47803e30c24aSWill Andrews  * suite's shell version in reservation.kshlib.
47813e30c24aSWill Andrews  */
4782c1449561SEric Taylor uint64_t
zvol_volsize_to_reservation(uint64_t volsize,nvlist_t * props)4783c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4784c1449561SEric Taylor {
4785c1449561SEric Taylor 	uint64_t numdb;
4786c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4787c1449561SEric Taylor 	int ncopies;
4788c1449561SEric Taylor 	char *strval;
4789c1449561SEric Taylor 
4790c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4791c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4792c1449561SEric Taylor 		ncopies = atoi(strval);
4793c1449561SEric Taylor 	else
4794c1449561SEric Taylor 		ncopies = 1;
4795c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4796c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4797c1449561SEric Taylor 	    &volblocksize) != 0)
4798c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4799c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4800c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4801c1449561SEric Taylor 	numdb = 7;
4802c1449561SEric Taylor 	/* calculate number of indirects */
4803c1449561SEric Taylor 	while (nblocks > 1) {
4804c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4805c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4806c1449561SEric Taylor 		numdb += nblocks;
4807c1449561SEric Taylor 	}
4808c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4809c1449561SEric Taylor 	volsize *= ncopies;
4810c1449561SEric Taylor 	/*
4811c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4812c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4813c1449561SEric Taylor 	 * 1100 bytes
4814c1449561SEric Taylor 	 */
4815c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4816c1449561SEric Taylor 	volsize += numdb;
4817c1449561SEric Taylor 	return (volsize);
4818c1449561SEric Taylor }
4819