xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision 6a9cb0ea17f11529b1bb8ca31944abd37736b99e)
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.
241af68beaSAlexander Stetsenko  * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
25d7f601efSGeorge Wilson  * Copyright (c) 2012 by Delphix. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <ctype.h>
29fa9e4066Sahrens #include <errno.h>
30fa9e4066Sahrens #include <libintl.h>
31fa9e4066Sahrens #include <math.h>
32fa9e4066Sahrens #include <stdio.h>
33fa9e4066Sahrens #include <stdlib.h>
34fa9e4066Sahrens #include <strings.h>
35fa9e4066Sahrens #include <unistd.h>
363cb34c60Sahrens #include <stddef.h>
37fa9e4066Sahrens #include <zone.h>
3899653d4eSeschrock #include <fcntl.h>
39fa9e4066Sahrens #include <sys/mntent.h>
40b12a1c38Slling #include <sys/mount.h>
41ecd6cf80Smarks #include <priv.h>
42ecd6cf80Smarks #include <pwd.h>
43ecd6cf80Smarks #include <grp.h>
44ecd6cf80Smarks #include <stddef.h>
45ecd6cf80Smarks #include <ucred.h>
4614843421SMatthew Ahrens #include <idmap.h>
4714843421SMatthew Ahrens #include <aclutils.h>
483b12c289SMatthew Ahrens #include <directory.h>
49fa9e4066Sahrens 
50c1449561SEric Taylor #include <sys/dnode.h>
51fa9e4066Sahrens #include <sys/spa.h>
52e9dbad6fSeschrock #include <sys/zap.h>
53fa9e4066Sahrens #include <libzfs.h>
54fa9e4066Sahrens 
55fa9e4066Sahrens #include "zfs_namecheck.h"
56fa9e4066Sahrens #include "zfs_prop.h"
57fa9e4066Sahrens #include "libzfs_impl.h"
58ecd6cf80Smarks #include "zfs_deleg.h"
59fa9e4066Sahrens 
6014843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6114843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
62cdf5b4caSmmusante 
63fa9e4066Sahrens /*
64fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
65fa9e4066Sahrens  * readable form.
66fa9e4066Sahrens  */
67fa9e4066Sahrens const char *
68fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
69fa9e4066Sahrens {
70fa9e4066Sahrens 	switch (type) {
71fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
72fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
74fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
76fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
77fa9e4066Sahrens 	}
78fa9e4066Sahrens 
79fa9e4066Sahrens 	return (NULL);
80fa9e4066Sahrens }
81fa9e4066Sahrens 
82fa9e4066Sahrens /*
83fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
84fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
85fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
86fa9e4066Sahrens  * acceptable types.
87fa9e4066Sahrens  */
88fa9e4066Sahrens static const char *
89fa9e4066Sahrens path_to_str(const char *path, int types)
90fa9e4066Sahrens {
91fa9e4066Sahrens 	/*
92fa9e4066Sahrens 	 * When given a single type, always report the exact type.
93fa9e4066Sahrens 	 */
94fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
95fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
97fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
99fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
100fa9e4066Sahrens 
101fa9e4066Sahrens 	/*
102fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
103fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
104fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105fa9e4066Sahrens 	 * snapshot attribute and try again.
106fa9e4066Sahrens 	 */
107fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
108fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
109fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111fa9e4066Sahrens 	}
112fa9e4066Sahrens 
113fa9e4066Sahrens 	/*
114fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
115fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
116fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
117fa9e4066Sahrens 	 */
118fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
119fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
120fa9e4066Sahrens 
121fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
122fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
123fa9e4066Sahrens }
124fa9e4066Sahrens 
125fa9e4066Sahrens /*
126fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
12714843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
12814843421SMatthew Ahrens  * explain exactly why the name was not valid.
129fa9e4066Sahrens  */
13099d5e173STim Haley int
131f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
132f18faf3fSek110237     boolean_t modifying)
133fa9e4066Sahrens {
134fa9e4066Sahrens 	namecheck_err_t why;
135fa9e4066Sahrens 	char what;
136fa9e4066Sahrens 
1371af68beaSAlexander Stetsenko 	(void) zfs_prop_get_table();
138fa9e4066Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
13999653d4eSeschrock 		if (hdl != NULL) {
140fa9e4066Sahrens 			switch (why) {
141b81d61a6Slling 			case NAME_ERR_TOOLONG:
14299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14399653d4eSeschrock 				    "name is too long"));
144b81d61a6Slling 				break;
145b81d61a6Slling 
146fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
14799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14899653d4eSeschrock 				    "leading slash in name"));
149fa9e4066Sahrens 				break;
150fa9e4066Sahrens 
151fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15399653d4eSeschrock 				    "empty component in name"));
154fa9e4066Sahrens 				break;
155fa9e4066Sahrens 
156fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
15799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15899653d4eSeschrock 				    "trailing slash in name"));
159fa9e4066Sahrens 				break;
160fa9e4066Sahrens 
161fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16299653d4eSeschrock 				zfs_error_aux(hdl,
163fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16499653d4eSeschrock 				    "'%c' in name"), what);
165fa9e4066Sahrens 				break;
166fa9e4066Sahrens 
167fa9e4066Sahrens 			case NAME_ERR_MULTIPLE_AT:
16899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16999653d4eSeschrock 				    "multiple '@' delimiters in name"));
170fa9e4066Sahrens 				break;
1715ad82045Snd150628 
1725ad82045Snd150628 			case NAME_ERR_NOLETTER:
1735ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1745ad82045Snd150628 				    "pool doesn't begin with a letter"));
1755ad82045Snd150628 				break;
1765ad82045Snd150628 
1775ad82045Snd150628 			case NAME_ERR_RESERVED:
1785ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1795ad82045Snd150628 				    "name is reserved"));
1805ad82045Snd150628 				break;
1815ad82045Snd150628 
1825ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1835ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1845ad82045Snd150628 				    "reserved disk name"));
1855ad82045Snd150628 				break;
186fa9e4066Sahrens 			}
187fa9e4066Sahrens 		}
188fa9e4066Sahrens 
189fa9e4066Sahrens 		return (0);
190fa9e4066Sahrens 	}
191fa9e4066Sahrens 
192fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19399653d4eSeschrock 		if (hdl != NULL)
19499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19599653d4eSeschrock 			    "snapshot delimiter '@' in filesystem name"));
196fa9e4066Sahrens 		return (0);
197fa9e4066Sahrens 	}
198fa9e4066Sahrens 
1991d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2001d452cf5Sahrens 		if (hdl != NULL)
2011d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
202d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2031d452cf5Sahrens 		return (0);
2041d452cf5Sahrens 	}
2051d452cf5Sahrens 
206f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
207f18faf3fSek110237 		if (hdl != NULL)
208f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
209f18faf3fSek110237 			    "invalid character %c in name"), '%');
210f18faf3fSek110237 		return (0);
211f18faf3fSek110237 	}
212f18faf3fSek110237 
21399653d4eSeschrock 	return (-1);
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216fa9e4066Sahrens int
217fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218fa9e4066Sahrens {
219e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
220e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
221f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222fa9e4066Sahrens }
223fa9e4066Sahrens 
224fa9e4066Sahrens /*
225e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
226e9dbad6fSeschrock  * properties into a separate nvlist.
227e9dbad6fSeschrock  */
228fac3008cSeschrock static nvlist_t *
229fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
230e9dbad6fSeschrock {
231e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
232e9dbad6fSeschrock 	nvpair_t *elem;
233e9dbad6fSeschrock 	nvlist_t *propval;
234fac3008cSeschrock 	nvlist_t *nvl;
235e9dbad6fSeschrock 
236fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
237fac3008cSeschrock 		(void) no_memory(hdl);
238fac3008cSeschrock 		return (NULL);
239fac3008cSeschrock 	}
240e9dbad6fSeschrock 
241e9dbad6fSeschrock 	elem = NULL;
242fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
243e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
244e9dbad6fSeschrock 			continue;
245e9dbad6fSeschrock 
246e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
247fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
248fac3008cSeschrock 			nvlist_free(nvl);
249fac3008cSeschrock 			(void) no_memory(hdl);
250fac3008cSeschrock 			return (NULL);
251fac3008cSeschrock 		}
252e9dbad6fSeschrock 	}
253e9dbad6fSeschrock 
254fac3008cSeschrock 	return (nvl);
255e9dbad6fSeschrock }
256e9dbad6fSeschrock 
25729ab75c9Srm160521 static zpool_handle_t *
25829ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
25929ab75c9Srm160521 {
26029ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26129ab75c9Srm160521 	zpool_handle_t *zph;
26229ab75c9Srm160521 
26329ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
26429ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
26529ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
26629ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
26729ab75c9Srm160521 	}
26829ab75c9Srm160521 	return (zph);
26929ab75c9Srm160521 }
27029ab75c9Srm160521 
27129ab75c9Srm160521 static zpool_handle_t *
27229ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
27329ab75c9Srm160521 {
27429ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
27529ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
27629ab75c9Srm160521 
27729ab75c9Srm160521 	while ((zph != NULL) &&
27829ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
27929ab75c9Srm160521 		zph = zph->zpool_next;
28029ab75c9Srm160521 	return (zph);
28129ab75c9Srm160521 }
28229ab75c9Srm160521 
28329ab75c9Srm160521 /*
28429ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
28529ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
28629ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
28729ab75c9Srm160521  */
28829ab75c9Srm160521 static zpool_handle_t *
28929ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
29029ab75c9Srm160521 {
29129ab75c9Srm160521 	char *pool_name;
29229ab75c9Srm160521 	int len;
29329ab75c9Srm160521 	zpool_handle_t *zph;
29429ab75c9Srm160521 
29529ab75c9Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
29629ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
29729ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
29829ab75c9Srm160521 
29929ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
30029ab75c9Srm160521 	if (zph == NULL)
30129ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
30229ab75c9Srm160521 
30329ab75c9Srm160521 	free(pool_name);
30429ab75c9Srm160521 	return (zph);
30529ab75c9Srm160521 }
30629ab75c9Srm160521 
30729ab75c9Srm160521 void
30829ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
30929ab75c9Srm160521 {
31029ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
31129ab75c9Srm160521 
31229ab75c9Srm160521 	while (zph != NULL) {
31329ab75c9Srm160521 		next = zph->zpool_next;
31429ab75c9Srm160521 		zpool_close(zph);
31529ab75c9Srm160521 		zph = next;
31629ab75c9Srm160521 	}
31729ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
31829ab75c9Srm160521 }
31929ab75c9Srm160521 
320e9dbad6fSeschrock /*
321fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
322fa9e4066Sahrens  */
323fa9e4066Sahrens static int
324ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
325fa9e4066Sahrens {
326e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
327fa9e4066Sahrens 
328ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
329fa9e4066Sahrens 
330ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3317f7322feSeschrock 		if (errno == ENOMEM) {
332ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
33399653d4eSeschrock 				return (-1);
334e9dbad6fSeschrock 			}
3357f7322feSeschrock 		} else {
336fa9e4066Sahrens 			return (-1);
3377f7322feSeschrock 		}
3387f7322feSeschrock 	}
339ebedde84SEric Taylor 	return (0);
340fac3008cSeschrock }
341fac3008cSeschrock 
34292241e0bSTom Erickson /*
34392241e0bSTom Erickson  * Utility function to get the received properties of the given object.
34492241e0bSTom Erickson  */
34592241e0bSTom Erickson static int
34692241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
34792241e0bSTom Erickson {
34892241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34992241e0bSTom Erickson 	nvlist_t *recvdprops;
35092241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
35192241e0bSTom Erickson 	int err;
35292241e0bSTom Erickson 
35392241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
35492241e0bSTom Erickson 		return (-1);
35592241e0bSTom Erickson 
35692241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35792241e0bSTom Erickson 
35892241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
35992241e0bSTom Erickson 		if (errno == ENOMEM) {
36092241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
36192241e0bSTom Erickson 				return (-1);
36292241e0bSTom Erickson 			}
36392241e0bSTom Erickson 		} else {
36492241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
36592241e0bSTom Erickson 			return (-1);
36692241e0bSTom Erickson 		}
36792241e0bSTom Erickson 	}
36892241e0bSTom Erickson 
36992241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
37092241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
37192241e0bSTom Erickson 	if (err != 0)
37292241e0bSTom Erickson 		return (-1);
37392241e0bSTom Erickson 
37492241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
37592241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
37692241e0bSTom Erickson 
37792241e0bSTom Erickson 	return (0);
37892241e0bSTom Erickson }
37992241e0bSTom Erickson 
380ebedde84SEric Taylor static int
381ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
382ebedde84SEric Taylor {
383ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
384ebedde84SEric Taylor 
385ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
386ebedde84SEric Taylor 
387ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
388ebedde84SEric Taylor 		return (-1);
389ebedde84SEric Taylor 	}
390fac3008cSeschrock 
39114843421SMatthew Ahrens 	/*
39214843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
39314843421SMatthew Ahrens 	 * storing them in zfs_props?
39414843421SMatthew Ahrens 	 */
395fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
396fac3008cSeschrock 		nvlist_free(allprops);
397fac3008cSeschrock 		return (-1);
398fac3008cSeschrock 	}
399fac3008cSeschrock 
40099653d4eSeschrock 	nvlist_free(zhp->zfs_props);
401fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
40299653d4eSeschrock 
403fac3008cSeschrock 	zhp->zfs_props = allprops;
404fac3008cSeschrock 	zhp->zfs_user_props = userprops;
40599653d4eSeschrock 
406fa9e4066Sahrens 	return (0);
407fa9e4066Sahrens }
408fa9e4066Sahrens 
409ebedde84SEric Taylor static int
410ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
411ebedde84SEric Taylor {
412ebedde84SEric Taylor 	int rc = 0;
413ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
414ebedde84SEric Taylor 
415ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
416ebedde84SEric Taylor 		return (-1);
417ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
418ebedde84SEric Taylor 		rc = -1;
419ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
420ebedde84SEric Taylor 		rc = -1;
421ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
422ebedde84SEric Taylor 	return (rc);
423ebedde84SEric Taylor }
424ebedde84SEric Taylor 
425fa9e4066Sahrens /*
426fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
427fa9e4066Sahrens  */
428fa9e4066Sahrens void
429fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
430fa9e4066Sahrens {
431fa9e4066Sahrens 	(void) get_stats(zhp);
432fa9e4066Sahrens }
433fa9e4066Sahrens 
434fa9e4066Sahrens /*
435fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
436fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
437fa9e4066Sahrens  */
438ebedde84SEric Taylor static int
439ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
440fa9e4066Sahrens {
441503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
442ebedde84SEric Taylor 		return (-1);
44331fd60d3Sahrens 
444fa9e4066Sahrens 	/*
445fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
446fa9e4066Sahrens 	 * the high-level type.
447fa9e4066Sahrens 	 */
448a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
449a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
450a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
451a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
452a2eea2e1Sahrens 	else
453a2eea2e1Sahrens 		abort();
454a2eea2e1Sahrens 
455fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
456fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
457fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
458fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
459fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
460fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
461fa9e4066Sahrens 	else
46299653d4eSeschrock 		abort();	/* we should never see any other types */
463fa9e4066Sahrens 
4649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
467ebedde84SEric Taylor 	return (0);
468ebedde84SEric Taylor }
469ebedde84SEric Taylor 
470ebedde84SEric Taylor zfs_handle_t *
471ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
472ebedde84SEric Taylor {
473ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
474ebedde84SEric Taylor 
475ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
476ebedde84SEric Taylor 
477ebedde84SEric Taylor 	if (zhp == NULL)
478ebedde84SEric Taylor 		return (NULL);
479ebedde84SEric Taylor 
480ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
481ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
482ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
483ebedde84SEric Taylor 		free(zhp);
484ebedde84SEric Taylor 		return (NULL);
485ebedde84SEric Taylor 	}
486ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
487ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
488ebedde84SEric Taylor 		free(zhp);
489ebedde84SEric Taylor 		return (NULL);
490ebedde84SEric Taylor 	}
491ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
492ebedde84SEric Taylor 		free(zhp);
493ebedde84SEric Taylor 		zhp = NULL;
494ebedde84SEric Taylor 	}
495ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
496ebedde84SEric Taylor 	return (zhp);
497ebedde84SEric Taylor }
498ebedde84SEric Taylor 
49919b94df9SMatthew Ahrens zfs_handle_t *
500ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
501ebedde84SEric Taylor {
502ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
503ebedde84SEric Taylor 
504ebedde84SEric Taylor 	if (zhp == NULL)
505ebedde84SEric Taylor 		return (NULL);
506ebedde84SEric Taylor 
507ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
508ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
509ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
510ebedde84SEric Taylor 		free(zhp);
511ebedde84SEric Taylor 		return (NULL);
512ebedde84SEric Taylor 	}
513fa9e4066Sahrens 	return (zhp);
514fa9e4066Sahrens }
515fa9e4066Sahrens 
51619b94df9SMatthew Ahrens zfs_handle_t *
51719b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
51819b94df9SMatthew Ahrens {
51919b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
52019b94df9SMatthew Ahrens 
52119b94df9SMatthew Ahrens 	if (zhp == NULL)
52219b94df9SMatthew Ahrens 		return (NULL);
52319b94df9SMatthew Ahrens 
52419b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
52519b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
52619b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
52719b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
52819b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
52919b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
53019b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
53119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
53219b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
53319b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
53419b94df9SMatthew Ahrens 			zfs_close(zhp);
53519b94df9SMatthew Ahrens 			return (NULL);
53619b94df9SMatthew Ahrens 		}
53719b94df9SMatthew Ahrens 	}
53819b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
53919b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
54019b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
54119b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54219b94df9SMatthew Ahrens 			zfs_close(zhp);
54319b94df9SMatthew Ahrens 			return (NULL);
54419b94df9SMatthew Ahrens 		}
54519b94df9SMatthew Ahrens 	}
54619b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
54719b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
54819b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
54919b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55019b94df9SMatthew Ahrens 			zfs_close(zhp);
55119b94df9SMatthew Ahrens 			return (NULL);
55219b94df9SMatthew Ahrens 		}
55319b94df9SMatthew Ahrens 	}
55419b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
55519b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
55619b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
55719b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
55819b94df9SMatthew Ahrens 	}
55919b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
56019b94df9SMatthew Ahrens 	return (zhp);
56119b94df9SMatthew Ahrens }
56219b94df9SMatthew Ahrens 
563fa9e4066Sahrens /*
564fa9e4066Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
565fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
566fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
567fa9e4066Sahrens  */
568fa9e4066Sahrens zfs_handle_t *
56999653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
570fa9e4066Sahrens {
571fa9e4066Sahrens 	zfs_handle_t *zhp;
57299653d4eSeschrock 	char errbuf[1024];
57399653d4eSeschrock 
57499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
57599653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
576fa9e4066Sahrens 
577fa9e4066Sahrens 	/*
57899653d4eSeschrock 	 * Validate the name before we even try to open it.
579fa9e4066Sahrens 	 */
580f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
58199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
58299653d4eSeschrock 		    "invalid dataset name"));
58399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
584fa9e4066Sahrens 		return (NULL);
585fa9e4066Sahrens 	}
586fa9e4066Sahrens 
587fa9e4066Sahrens 	/*
588fa9e4066Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
589fa9e4066Sahrens 	 */
590fa9e4066Sahrens 	errno = 0;
59199653d4eSeschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
592ece3d9b3Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
593fa9e4066Sahrens 		return (NULL);
594fa9e4066Sahrens 	}
595fa9e4066Sahrens 
596fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
59799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
59894de1d4cSeschrock 		zfs_close(zhp);
599fa9e4066Sahrens 		return (NULL);
600fa9e4066Sahrens 	}
601fa9e4066Sahrens 
602fa9e4066Sahrens 	return (zhp);
603fa9e4066Sahrens }
604fa9e4066Sahrens 
605fa9e4066Sahrens /*
606fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
607fa9e4066Sahrens  */
608fa9e4066Sahrens void
609fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
610fa9e4066Sahrens {
611fa9e4066Sahrens 	if (zhp->zfs_mntopts)
612fa9e4066Sahrens 		free(zhp->zfs_mntopts);
61399653d4eSeschrock 	nvlist_free(zhp->zfs_props);
614e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
61592241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
616fa9e4066Sahrens 	free(zhp);
617fa9e4066Sahrens }
618fa9e4066Sahrens 
619ebedde84SEric Taylor typedef struct mnttab_node {
620ebedde84SEric Taylor 	struct mnttab mtn_mt;
621ebedde84SEric Taylor 	avl_node_t mtn_node;
622ebedde84SEric Taylor } mnttab_node_t;
623ebedde84SEric Taylor 
624ebedde84SEric Taylor static int
625ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
626ebedde84SEric Taylor {
627ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
628ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
629ebedde84SEric Taylor 	int rv;
630ebedde84SEric Taylor 
631ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
632ebedde84SEric Taylor 
633ebedde84SEric Taylor 	if (rv == 0)
634ebedde84SEric Taylor 		return (0);
635ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
636ebedde84SEric Taylor }
637ebedde84SEric Taylor 
638ebedde84SEric Taylor void
639ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
640ebedde84SEric Taylor {
641ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
642ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
643ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
644b2634b9cSEric Taylor }
645b2634b9cSEric Taylor 
646b2634b9cSEric Taylor void
647b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
648b2634b9cSEric Taylor {
649b2634b9cSEric Taylor 	struct mnttab entry;
650ebedde84SEric Taylor 
651ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
652ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
653ebedde84SEric Taylor 		mnttab_node_t *mtn;
654ebedde84SEric Taylor 
655ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
656ebedde84SEric Taylor 			continue;
657ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
658ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
659ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
660ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
661ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
662ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
663ebedde84SEric Taylor 	}
664ebedde84SEric Taylor }
665ebedde84SEric Taylor 
666ebedde84SEric Taylor void
667ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
668ebedde84SEric Taylor {
669ebedde84SEric Taylor 	void *cookie = NULL;
670ebedde84SEric Taylor 	mnttab_node_t *mtn;
671ebedde84SEric Taylor 
672ebedde84SEric Taylor 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
673ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
674ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
675ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
676ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
677ebedde84SEric Taylor 		free(mtn);
678ebedde84SEric Taylor 	}
679ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
680ebedde84SEric Taylor }
681ebedde84SEric Taylor 
682b2634b9cSEric Taylor void
683b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
684b2634b9cSEric Taylor {
685b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
686b2634b9cSEric Taylor }
687b2634b9cSEric Taylor 
688ebedde84SEric Taylor int
689ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
690ebedde84SEric Taylor     struct mnttab *entry)
691ebedde84SEric Taylor {
692ebedde84SEric Taylor 	mnttab_node_t find;
693ebedde84SEric Taylor 	mnttab_node_t *mtn;
694ebedde84SEric Taylor 
695b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
696b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
697b2634b9cSEric Taylor 
698b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
699b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
700b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
701b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
702b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
703b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
704b2634b9cSEric Taylor 			return (0);
705b2634b9cSEric Taylor 		else
706b2634b9cSEric Taylor 			return (ENOENT);
707b2634b9cSEric Taylor 	}
708b2634b9cSEric Taylor 
709ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
710b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
711ebedde84SEric Taylor 
712ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
713ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
714ebedde84SEric Taylor 	if (mtn) {
715ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
716ebedde84SEric Taylor 		return (0);
717ebedde84SEric Taylor 	}
718ebedde84SEric Taylor 	return (ENOENT);
719ebedde84SEric Taylor }
720ebedde84SEric Taylor 
721ebedde84SEric Taylor void
722ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
723ebedde84SEric Taylor     const char *mountp, const char *mntopts)
724ebedde84SEric Taylor {
725ebedde84SEric Taylor 	mnttab_node_t *mtn;
726ebedde84SEric Taylor 
727ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
728ebedde84SEric Taylor 		return;
729ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
730ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
731ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
732ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
733ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
734ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
735ebedde84SEric Taylor }
736ebedde84SEric Taylor 
737ebedde84SEric Taylor void
738ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
739ebedde84SEric Taylor {
740ebedde84SEric Taylor 	mnttab_node_t find;
741ebedde84SEric Taylor 	mnttab_node_t *ret;
742ebedde84SEric Taylor 
743ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
744ebedde84SEric Taylor 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
745ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
746ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
747ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
748ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
749ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
750ebedde84SEric Taylor 		free(ret);
751ebedde84SEric Taylor 	}
752ebedde84SEric Taylor }
753ebedde84SEric Taylor 
7547b97dc1aSrm160521 int
7557b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
7567b97dc1aSrm160521 {
75729ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
7587b97dc1aSrm160521 
7597b97dc1aSrm160521 	if (zpool_handle == NULL)
7607b97dc1aSrm160521 		return (-1);
7617b97dc1aSrm160521 
7627b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
7637b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
7647b97dc1aSrm160521 	return (0);
7657b97dc1aSrm160521 }
7667b97dc1aSrm160521 
7677b97dc1aSrm160521 /*
7687b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
7697b97dc1aSrm160521  */
7707b97dc1aSrm160521 static int
7717b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
7727b97dc1aSrm160521 {
7737b97dc1aSrm160521 	int spa_version;
7747b97dc1aSrm160521 
7757b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
7767b97dc1aSrm160521 		return (-1);
7777b97dc1aSrm160521 
7787b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
7797b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
7807b97dc1aSrm160521 	else
7817b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
7827b97dc1aSrm160521 
7837b97dc1aSrm160521 	return (0);
7847b97dc1aSrm160521 }
7857b97dc1aSrm160521 
786b1b8ab34Slling /*
787e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
788e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
789e9dbad6fSeschrock  * strings.
790fa9e4066Sahrens  */
7910a48a24eStimh nvlist_t *
7920a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
793990b4856Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
794fa9e4066Sahrens {
795e9dbad6fSeschrock 	nvpair_t *elem;
796e9dbad6fSeschrock 	uint64_t intval;
797e9dbad6fSeschrock 	char *strval;
798990b4856Slling 	zfs_prop_t prop;
799e9dbad6fSeschrock 	nvlist_t *ret;
800da6c28aaSamw 	int chosen_normal = -1;
801da6c28aaSamw 	int chosen_utf = -1;
802990b4856Slling 
803e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
804e9dbad6fSeschrock 		(void) no_memory(hdl);
805e9dbad6fSeschrock 		return (NULL);
806e9dbad6fSeschrock 	}
807fa9e4066Sahrens 
80814843421SMatthew Ahrens 	/*
80914843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
81014843421SMatthew Ahrens 	 */
81114843421SMatthew Ahrens 
812e9dbad6fSeschrock 	elem = NULL;
813e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
814990b4856Slling 		const char *propname = nvpair_name(elem);
81599653d4eSeschrock 
81614843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
81714843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
818fa9e4066Sahrens 			/*
81914843421SMatthew Ahrens 			 * This is a user property: make sure it's a
820990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
821990b4856Slling 			 */
822990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
823990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
824990b4856Slling 				    "'%s' must be a string"), propname);
825990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
826990b4856Slling 				goto error;
827990b4856Slling 			}
828990b4856Slling 
829990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
830e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
831e9dbad6fSeschrock 				    "property name '%s' is too long"),
832e9dbad6fSeschrock 				    propname);
833990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
834e9dbad6fSeschrock 				goto error;
835e9dbad6fSeschrock 			}
836e9dbad6fSeschrock 
837e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
838e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
839e9dbad6fSeschrock 				(void) no_memory(hdl);
840e9dbad6fSeschrock 				goto error;
841e9dbad6fSeschrock 			}
842e9dbad6fSeschrock 			continue;
843e9dbad6fSeschrock 		}
844fa9e4066Sahrens 
84514843421SMatthew Ahrens 		/*
84614843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
84714843421SMatthew Ahrens 		 * snapshots.
84814843421SMatthew Ahrens 		 */
849bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
850bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
851bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
852bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
853bb0ade09Sahrens 			goto error;
854bb0ade09Sahrens 		}
855bb0ade09Sahrens 
85614843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
85714843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
85814843421SMatthew Ahrens 			char newpropname[128];
85914843421SMatthew Ahrens 			char domain[128];
86014843421SMatthew Ahrens 			uint64_t rid;
86114843421SMatthew Ahrens 			uint64_t valary[3];
86214843421SMatthew Ahrens 
86314843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
86414843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
86514843421SMatthew Ahrens 				zfs_error_aux(hdl,
86614843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
86714843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
86814843421SMatthew Ahrens 				    propname);
86914843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
87014843421SMatthew Ahrens 				goto error;
87114843421SMatthew Ahrens 			}
87214843421SMatthew Ahrens 
87314843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
87414843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
87514843421SMatthew Ahrens 				zfs_error_aux(hdl,
87614843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
87714843421SMatthew Ahrens 				    propname);
87814843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
87914843421SMatthew Ahrens 				    errbuf);
88014843421SMatthew Ahrens 				goto error;
88114843421SMatthew Ahrens 			}
88214843421SMatthew Ahrens 
88314843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
88414843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
88514843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
88614843421SMatthew Ahrens 					intval = 0;
88714843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
88814843421SMatthew Ahrens 				    strval, &intval) != 0) {
88914843421SMatthew Ahrens 					(void) zfs_error(hdl,
89014843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
89114843421SMatthew Ahrens 					goto error;
89214843421SMatthew Ahrens 				}
89314843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
89414843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
89514843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
89614843421SMatthew Ahrens 				if (intval == 0) {
89714843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
89814843421SMatthew Ahrens 					    "use 'none' to disable "
89914843421SMatthew Ahrens 					    "userquota/groupquota"));
90014843421SMatthew Ahrens 					goto error;
90114843421SMatthew Ahrens 				}
90214843421SMatthew Ahrens 			} else {
90314843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90414843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
90514843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
90614843421SMatthew Ahrens 				goto error;
90714843421SMatthew Ahrens 			}
90814843421SMatthew Ahrens 
9092d5843dbSMatthew Ahrens 			/*
9102d5843dbSMatthew Ahrens 			 * Encode the prop name as
9112d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
9122d5843dbSMatthew Ahrens 			 * for the kernel to decode.
9132d5843dbSMatthew Ahrens 			 */
91414843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
9152d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
9162d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
91714843421SMatthew Ahrens 			valary[0] = uqtype;
91814843421SMatthew Ahrens 			valary[1] = rid;
91914843421SMatthew Ahrens 			valary[2] = intval;
92014843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
92114843421SMatthew Ahrens 			    valary, 3) != 0) {
92214843421SMatthew Ahrens 				(void) no_memory(hdl);
92314843421SMatthew Ahrens 				goto error;
92414843421SMatthew Ahrens 			}
92514843421SMatthew Ahrens 			continue;
92619b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
92719b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
92819b94df9SMatthew Ahrens 			    "'%s' is readonly"),
92919b94df9SMatthew Ahrens 			    propname);
93019b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
93119b94df9SMatthew Ahrens 			goto error;
93214843421SMatthew Ahrens 		}
93314843421SMatthew Ahrens 
93414843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
93514843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93614843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
93714843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
93814843421SMatthew Ahrens 			goto error;
93914843421SMatthew Ahrens 		}
94014843421SMatthew Ahrens 
941e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
942e9dbad6fSeschrock 			zfs_error_aux(hdl,
943e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
944e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
945e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
946e9dbad6fSeschrock 			goto error;
947e9dbad6fSeschrock 		}
948e9dbad6fSeschrock 
949e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
950da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
951e9dbad6fSeschrock 			zfs_error_aux(hdl,
952e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
953e9dbad6fSeschrock 			    propname);
954e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
955e9dbad6fSeschrock 			goto error;
956e9dbad6fSeschrock 		}
957e9dbad6fSeschrock 
958990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
959990b4856Slling 		    &strval, &intval, errbuf) != 0)
960e9dbad6fSeschrock 			goto error;
961e9dbad6fSeschrock 
962e9dbad6fSeschrock 		/*
963e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
964e9dbad6fSeschrock 		 */
965e9dbad6fSeschrock 		switch (prop) {
966e7437265Sahrens 		case ZFS_PROP_VERSION:
967e7437265Sahrens 		{
968e7437265Sahrens 			int version;
969e7437265Sahrens 
970e7437265Sahrens 			if (zhp == NULL)
971e7437265Sahrens 				break;
972e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
973e7437265Sahrens 			if (intval < version) {
974e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
975e7437265Sahrens 				    "Can not downgrade; already at version %u"),
976e7437265Sahrens 				    version);
977e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
978e7437265Sahrens 				goto error;
979e7437265Sahrens 			}
980e7437265Sahrens 			break;
981e7437265Sahrens 		}
982e7437265Sahrens 
983e9dbad6fSeschrock 		case ZFS_PROP_RECORDSIZE:
984e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
985e9dbad6fSeschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
986e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
987e9dbad6fSeschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
988e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
989e9dbad6fSeschrock 				    "'%s' must be power of 2 from %u "
990e9dbad6fSeschrock 				    "to %uk"), propname,
991e9dbad6fSeschrock 				    (uint_t)SPA_MINBLOCKSIZE,
992e9dbad6fSeschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
993e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
994e9dbad6fSeschrock 				goto error;
995e9dbad6fSeschrock 			}
996e9dbad6fSeschrock 			break;
997e9dbad6fSeschrock 
9984201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
9994201a95eSRic Aleshire 		{
10004201a95eSRic Aleshire 			/*
10014201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
10024201a95eSRic Aleshire 			 * internal hex label string.
10034201a95eSRic Aleshire 			 */
10044201a95eSRic Aleshire 
10054201a95eSRic Aleshire 			m_label_t *new_sl;
10064201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
10074201a95eSRic Aleshire 
10084201a95eSRic Aleshire 			/* Default value is already OK. */
10094201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
10104201a95eSRic Aleshire 				break;
10114201a95eSRic Aleshire 
10124201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
10134201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
10144201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
10154201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
10164201a95eSRic Aleshire 				goto badlabel;
10174201a95eSRic Aleshire 			}
10184201a95eSRic Aleshire 
10194201a95eSRic Aleshire 			/* Now translate to hex internal label string */
10204201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
10214201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
10224201a95eSRic Aleshire 				if (hex)
10234201a95eSRic Aleshire 					free(hex);
10244201a95eSRic Aleshire 				goto badlabel;
10254201a95eSRic Aleshire 			}
10264201a95eSRic Aleshire 			m_label_free(new_sl);
10274201a95eSRic Aleshire 
10284201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
10294201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
10304201a95eSRic Aleshire 				free(hex);
10314201a95eSRic Aleshire 				break;
10324201a95eSRic Aleshire 			}
10334201a95eSRic Aleshire 
10344201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1035569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
10364201a95eSRic Aleshire 			    DATA_TYPE_STRING);
10374201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
10384201a95eSRic Aleshire 			    hex) == 0);
10394201a95eSRic Aleshire 			free(hex);
10404201a95eSRic Aleshire 
10414201a95eSRic Aleshire 			break;
10424201a95eSRic Aleshire 
10434201a95eSRic Aleshire badlabel:
10444201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10454201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
10464201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
10474201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
10484201a95eSRic Aleshire 			goto error;
10494201a95eSRic Aleshire 
10504201a95eSRic Aleshire 		}
10514201a95eSRic Aleshire 
1052e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
105389eef05eSrm160521 		{
105489eef05eSrm160521 			namecheck_err_t why;
105589eef05eSrm160521 
1056e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1057e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1058e9dbad6fSeschrock 				break;
1059e9dbad6fSeschrock 
106089eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
106189eef05eSrm160521 				switch (why) {
106289eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
106389eef05eSrm160521 					zfs_error_aux(hdl,
106489eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1065e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1066e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
106789eef05eSrm160521 					break;
106889eef05eSrm160521 				case NAME_ERR_TOOLONG:
106989eef05eSrm160521 					zfs_error_aux(hdl,
107089eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
107189eef05eSrm160521 					    "component of '%s' is too long"),
107289eef05eSrm160521 					    propname);
107389eef05eSrm160521 					break;
107489eef05eSrm160521 				}
1075e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1076e9dbad6fSeschrock 				goto error;
1077e9dbad6fSeschrock 			}
107889eef05eSrm160521 		}
107989eef05eSrm160521 
1080f3861e1aSahl 			/*FALLTHRU*/
1081e9dbad6fSeschrock 
1082da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1083f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1084e9dbad6fSeschrock 			/*
1085da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1086da6c28aaSamw 			 * properties, check if it can be set in a
1087da6c28aaSamw 			 * global/non-global zone based on
1088f3861e1aSahl 			 * the zoned property value:
1089fa9e4066Sahrens 			 *
1090fa9e4066Sahrens 			 *		global zone	    non-global zone
1091f3861e1aSahl 			 * --------------------------------------------------
1092fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1093fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1094da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1095fa9e4066Sahrens 			 *
1096fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1097fa9e4066Sahrens 			 *		sharenfs (yes)
1098da6c28aaSamw 			 *		sharesmb (yes)
1099fa9e4066Sahrens 			 */
1100e9dbad6fSeschrock 			if (zoned) {
1101fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
110299653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1103e9dbad6fSeschrock 					    "'%s' cannot be set on "
1104e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1105e9dbad6fSeschrock 					    propname);
1106e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1107e9dbad6fSeschrock 					    errbuf);
1108e9dbad6fSeschrock 					goto error;
1109da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1110da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
111199653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1112e9dbad6fSeschrock 					    "'%s' cannot be set in "
1113e9dbad6fSeschrock 					    "a non-global zone"), propname);
1114e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1115e9dbad6fSeschrock 					    errbuf);
1116e9dbad6fSeschrock 					goto error;
1117fa9e4066Sahrens 				}
1118fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1119fa9e4066Sahrens 				/*
1120fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
112114843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1122fa9e4066Sahrens 				 */
112399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1124e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1125e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1126e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1127e9dbad6fSeschrock 				goto error;
1128fa9e4066Sahrens 			}
1129f3861e1aSahl 
113067331909Sdougm 			/*
113167331909Sdougm 			 * At this point, it is legitimate to set the
113267331909Sdougm 			 * property. Now we want to make sure that the
113367331909Sdougm 			 * property value is valid if it is sharenfs.
113467331909Sdougm 			 */
1135da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1136da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
113767331909Sdougm 			    strcmp(strval, "on") != 0 &&
113867331909Sdougm 			    strcmp(strval, "off") != 0) {
1139da6c28aaSamw 				zfs_share_proto_t proto;
1140da6c28aaSamw 
1141da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1142da6c28aaSamw 					proto = PROTO_SMB;
1143da6c28aaSamw 				else
1144da6c28aaSamw 					proto = PROTO_NFS;
114567331909Sdougm 
114667331909Sdougm 				/*
1147da6c28aaSamw 				 * Must be an valid sharing protocol
1148da6c28aaSamw 				 * option string so init the libshare
1149da6c28aaSamw 				 * in order to enable the parser and
1150da6c28aaSamw 				 * then parse the options. We use the
1151da6c28aaSamw 				 * control API since we don't care about
1152da6c28aaSamw 				 * the current configuration and don't
115367331909Sdougm 				 * want the overhead of loading it
115467331909Sdougm 				 * until we actually do something.
115567331909Sdougm 				 */
115667331909Sdougm 
115767331909Sdougm 				if (zfs_init_libshare(hdl,
115867331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1159fac3008cSeschrock 					/*
1160fac3008cSeschrock 					 * An error occurred so we can't do
1161fac3008cSeschrock 					 * anything
1162fac3008cSeschrock 					 */
116367331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116467331909Sdougm 					    "'%s' cannot be set: problem "
116567331909Sdougm 					    "in share initialization"),
116667331909Sdougm 					    propname);
1167fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1168fac3008cSeschrock 					    errbuf);
116967331909Sdougm 					goto error;
117067331909Sdougm 				}
117167331909Sdougm 
1172da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
117367331909Sdougm 					/*
117467331909Sdougm 					 * There was an error in parsing so
117567331909Sdougm 					 * deal with it by issuing an error
117667331909Sdougm 					 * message and leaving after
117767331909Sdougm 					 * uninitializing the the libshare
117867331909Sdougm 					 * interface.
117967331909Sdougm 					 */
118067331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118167331909Sdougm 					    "'%s' cannot be set to invalid "
118267331909Sdougm 					    "options"), propname);
1183fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1184fac3008cSeschrock 					    errbuf);
118567331909Sdougm 					zfs_uninit_libshare(hdl);
118667331909Sdougm 					goto error;
118767331909Sdougm 				}
118867331909Sdougm 				zfs_uninit_libshare(hdl);
118967331909Sdougm 			}
119067331909Sdougm 
1191f3861e1aSahl 			break;
1192da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1193da6c28aaSamw 			chosen_utf = (int)intval;
1194da6c28aaSamw 			break;
1195da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1196da6c28aaSamw 			chosen_normal = (int)intval;
1197da6c28aaSamw 			break;
1198fa9e4066Sahrens 		}
1199fa9e4066Sahrens 
1200e9dbad6fSeschrock 		/*
1201e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1202e9dbad6fSeschrock 		 * checks to enforce.
1203e9dbad6fSeschrock 		 */
1204e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1205e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1206e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1207e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1208e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1209e9dbad6fSeschrock 			char buf[64];
1210e9dbad6fSeschrock 
1211e9dbad6fSeschrock 			switch (prop) {
1212e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1213a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
1214e9dbad6fSeschrock 				if (intval > volsize) {
1215e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1216e9dbad6fSeschrock 					    "'%s' is greater than current "
1217e9dbad6fSeschrock 					    "volume size"), propname);
1218e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1219e9dbad6fSeschrock 					    errbuf);
1220e9dbad6fSeschrock 					goto error;
1221e9dbad6fSeschrock 				}
1222e9dbad6fSeschrock 				break;
1223e9dbad6fSeschrock 
1224e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1225e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1226e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1227e9dbad6fSeschrock 					    sizeof (buf));
1228e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1229e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1230e9dbad6fSeschrock 					    "volume block size (%s)"),
1231e9dbad6fSeschrock 					    propname, buf);
1232e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1233e9dbad6fSeschrock 					    errbuf);
1234e9dbad6fSeschrock 					goto error;
1235e9dbad6fSeschrock 				}
1236e9dbad6fSeschrock 
1237e9dbad6fSeschrock 				if (intval == 0) {
1238e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1239e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1240e9dbad6fSeschrock 					    propname);
1241e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1242e9dbad6fSeschrock 					    errbuf);
1243e9dbad6fSeschrock 					goto error;
1244e9dbad6fSeschrock 				}
1245f3861e1aSahl 				break;
1246e9dbad6fSeschrock 			}
1247e9dbad6fSeschrock 		}
1248e9dbad6fSeschrock 	}
1249e9dbad6fSeschrock 
1250e9dbad6fSeschrock 	/*
1251da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1252da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1253da6c28aaSamw 	 *
1254da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1255da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1256da6c28aaSamw 	 */
1257de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1258da6c28aaSamw 		if (nvlist_add_uint64(ret,
1259da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1260da6c28aaSamw 			(void) no_memory(hdl);
1261da6c28aaSamw 			goto error;
1262da6c28aaSamw 		}
1263de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1264da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1265da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1266da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1267da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1268da6c28aaSamw 		goto error;
1269da6c28aaSamw 	}
1270e9dbad6fSeschrock 	return (ret);
1271e9dbad6fSeschrock 
1272e9dbad6fSeschrock error:
1273e9dbad6fSeschrock 	nvlist_free(ret);
1274e9dbad6fSeschrock 	return (NULL);
1275e9dbad6fSeschrock }
1276e9dbad6fSeschrock 
127736db6475SEric Taylor int
127836db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
127936db6475SEric Taylor {
128036db6475SEric Taylor 	uint64_t old_volsize;
128136db6475SEric Taylor 	uint64_t new_volsize;
128236db6475SEric Taylor 	uint64_t old_reservation;
128336db6475SEric Taylor 	uint64_t new_reservation;
128436db6475SEric Taylor 	zfs_prop_t resv_prop;
128536db6475SEric Taylor 
128636db6475SEric Taylor 	/*
128736db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
128836db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
128936db6475SEric Taylor 	 */
129036db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
129136db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
129236db6475SEric Taylor 		return (-1);
129336db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
129436db6475SEric Taylor 	if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
129536db6475SEric Taylor 	    old_reservation) || nvlist_lookup_uint64(nvl,
129636db6475SEric Taylor 	    zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
129736db6475SEric Taylor 		return (0);
129836db6475SEric Taylor 	}
129936db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
130036db6475SEric Taylor 	    &new_volsize) != 0)
130136db6475SEric Taylor 		return (-1);
130236db6475SEric Taylor 	new_reservation = zvol_volsize_to_reservation(new_volsize,
130336db6475SEric Taylor 	    zhp->zfs_props);
130436db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
130536db6475SEric Taylor 	    new_reservation) != 0) {
130636db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
130736db6475SEric Taylor 		return (-1);
130836db6475SEric Taylor 	}
130936db6475SEric Taylor 	return (1);
131036db6475SEric Taylor }
131136db6475SEric Taylor 
131292241e0bSTom Erickson void
131392241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
131492241e0bSTom Erickson     char *errbuf)
131592241e0bSTom Erickson {
131692241e0bSTom Erickson 	switch (err) {
131792241e0bSTom Erickson 
131892241e0bSTom Erickson 	case ENOSPC:
131992241e0bSTom Erickson 		/*
132092241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
132192241e0bSTom Erickson 		 * something different; setting a quota or reservation
132292241e0bSTom Erickson 		 * doesn't use any disk space.
132392241e0bSTom Erickson 		 */
132492241e0bSTom Erickson 		switch (prop) {
132592241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
132692241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
132792241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132892241e0bSTom Erickson 			    "size is less than current used or "
132992241e0bSTom Erickson 			    "reserved space"));
133092241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
133192241e0bSTom Erickson 			break;
133292241e0bSTom Erickson 
133392241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
133492241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
133592241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133692241e0bSTom Erickson 			    "size is greater than available space"));
133792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
133892241e0bSTom Erickson 			break;
133992241e0bSTom Erickson 
134092241e0bSTom Erickson 		default:
134192241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
134292241e0bSTom Erickson 			break;
134392241e0bSTom Erickson 		}
134492241e0bSTom Erickson 		break;
134592241e0bSTom Erickson 
134692241e0bSTom Erickson 	case EBUSY:
134792241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
134892241e0bSTom Erickson 		break;
134992241e0bSTom Erickson 
135092241e0bSTom Erickson 	case EROFS:
135192241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
135292241e0bSTom Erickson 		break;
135392241e0bSTom Erickson 
135492241e0bSTom Erickson 	case ENOTSUP:
135592241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135692241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
135792241e0bSTom Erickson 		    "property or value"));
135892241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
135992241e0bSTom Erickson 		break;
136092241e0bSTom Erickson 
136192241e0bSTom Erickson 	case ERANGE:
136292241e0bSTom Erickson 		if (prop == ZFS_PROP_COMPRESSION) {
136392241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136492241e0bSTom Erickson 			    "property setting is not allowed on "
136592241e0bSTom Erickson 			    "bootable datasets"));
136692241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
136792241e0bSTom Erickson 		} else {
136892241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
136992241e0bSTom Erickson 		}
137092241e0bSTom Erickson 		break;
137192241e0bSTom Erickson 
1372ab003da8SJim Dunham 	case EINVAL:
1373ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1374ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1375ab003da8SJim Dunham 		} else {
1376ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1377ab003da8SJim Dunham 		}
1378ab003da8SJim Dunham 		break;
1379ab003da8SJim Dunham 
138092241e0bSTom Erickson 	case EOVERFLOW:
138192241e0bSTom Erickson 		/*
138292241e0bSTom Erickson 		 * This platform can't address a volume this big.
138392241e0bSTom Erickson 		 */
138492241e0bSTom Erickson #ifdef _ILP32
138592241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
138692241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
138792241e0bSTom Erickson 			break;
138892241e0bSTom Erickson 		}
138992241e0bSTom Erickson #endif
139092241e0bSTom Erickson 		/* FALLTHROUGH */
139192241e0bSTom Erickson 	default:
139292241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
139392241e0bSTom Erickson 	}
139492241e0bSTom Erickson }
139592241e0bSTom Erickson 
1396e9dbad6fSeschrock /*
1397e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1398e9dbad6fSeschrock  */
1399e9dbad6fSeschrock int
1400e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1401e9dbad6fSeschrock {
1402e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1403e9dbad6fSeschrock 	int ret = -1;
1404e9dbad6fSeschrock 	prop_changelist_t *cl = NULL;
1405e9dbad6fSeschrock 	char errbuf[1024];
1406e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1407e9dbad6fSeschrock 	nvlist_t *nvl = NULL, *realprops;
1408e9dbad6fSeschrock 	zfs_prop_t prop;
14090068372bSMark J Musante 	boolean_t do_prefix;
14100068372bSMark J Musante 	uint64_t idx;
141136db6475SEric Taylor 	int added_resv;
1412e9dbad6fSeschrock 
1413e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1414e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1415e9dbad6fSeschrock 	    zhp->zfs_name);
1416e9dbad6fSeschrock 
1417e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1418e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1419e9dbad6fSeschrock 		(void) no_memory(hdl);
1420e9dbad6fSeschrock 		goto error;
1421e9dbad6fSeschrock 	}
1422e9dbad6fSeschrock 
14230a48a24eStimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1424e9dbad6fSeschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1425e9dbad6fSeschrock 		goto error;
1426990b4856Slling 
1427e9dbad6fSeschrock 	nvlist_free(nvl);
1428e9dbad6fSeschrock 	nvl = realprops;
1429e9dbad6fSeschrock 
1430e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
1431e9dbad6fSeschrock 
143236db6475SEric Taylor 	if (prop == ZFS_PROP_VOLSIZE) {
143336db6475SEric Taylor 		if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
143436db6475SEric Taylor 			goto error;
143536db6475SEric Taylor 	}
143636db6475SEric Taylor 
14370069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1438e9dbad6fSeschrock 		goto error;
1439fa9e4066Sahrens 
1440fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
144199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442fa9e4066Sahrens 		    "child dataset with inherited mountpoint is used "
144399653d4eSeschrock 		    "in a non-global zone"));
144499653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1445fa9e4066Sahrens 		goto error;
1446fa9e4066Sahrens 	}
1447fa9e4066Sahrens 
14480068372bSMark J Musante 	/*
14490068372bSMark J Musante 	 * If the dataset's canmount property is being set to noauto,
14500068372bSMark J Musante 	 * then we want to prevent unmounting & remounting it.
14510068372bSMark J Musante 	 */
14520068372bSMark J Musante 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
14530068372bSMark J Musante 	    (zprop_string_to_index(prop, propval, &idx,
14540068372bSMark J Musante 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1455a227b7f4Shs24103 
1456a227b7f4Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1457fa9e4066Sahrens 		goto error;
1458fa9e4066Sahrens 
1459fa9e4066Sahrens 	/*
1460fa9e4066Sahrens 	 * Execute the corresponding ioctl() to set this property.
1461fa9e4066Sahrens 	 */
1462fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1463fa9e4066Sahrens 
1464990b4856Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1465e9dbad6fSeschrock 		goto error;
1466e9dbad6fSeschrock 
1467ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1468743a77edSAlan Wright 
1469fa9e4066Sahrens 	if (ret != 0) {
147092241e0bSTom Erickson 		zfs_setprop_error(hdl, prop, errno, errbuf);
147136db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
147236db6475SEric Taylor 			/* clean up the volsize property we tried to set */
147336db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
147436db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
147536db6475SEric Taylor 			nvlist_free(nvl);
147636db6475SEric Taylor 			zcmd_free_nvlists(&zc);
147736db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
147836db6475SEric Taylor 				goto error;
147936db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
148036db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
148136db6475SEric Taylor 			    old_volsize) != 0)
148236db6475SEric Taylor 				goto error;
148336db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
148436db6475SEric Taylor 				goto error;
148536db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
148636db6475SEric Taylor 		}
1487fa9e4066Sahrens 	} else {
1488a227b7f4Shs24103 		if (do_prefix)
1489a227b7f4Shs24103 			ret = changelist_postfix(cl);
1490a227b7f4Shs24103 
1491fa9e4066Sahrens 		/*
1492fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1493fa9e4066Sahrens 		 * is reflected.
1494fa9e4066Sahrens 		 */
1495a227b7f4Shs24103 		if (ret == 0)
1496fa9e4066Sahrens 			(void) get_stats(zhp);
1497fa9e4066Sahrens 	}
1498fa9e4066Sahrens 
1499fa9e4066Sahrens error:
1500e9dbad6fSeschrock 	nvlist_free(nvl);
1501e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1502e9dbad6fSeschrock 	if (cl)
1503fa9e4066Sahrens 		changelist_free(cl);
1504fa9e4066Sahrens 	return (ret);
1505fa9e4066Sahrens }
1506fa9e4066Sahrens 
1507fa9e4066Sahrens /*
150892241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
150992241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1510fa9e4066Sahrens  */
1511fa9e4066Sahrens int
151292241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1513fa9e4066Sahrens {
1514fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1515fa9e4066Sahrens 	int ret;
1516fa9e4066Sahrens 	prop_changelist_t *cl;
151799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
151899653d4eSeschrock 	char errbuf[1024];
1519e9dbad6fSeschrock 	zfs_prop_t prop;
152099653d4eSeschrock 
152199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
152299653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1523fa9e4066Sahrens 
152492241e0bSTom Erickson 	zc.zc_cookie = received;
1525990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1526e9dbad6fSeschrock 		/*
1527e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1528e9dbad6fSeschrock 		 * small, so just do it here.
1529e9dbad6fSeschrock 		 */
1530e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1531e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532e9dbad6fSeschrock 			    "invalid property"));
1533e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1534e9dbad6fSeschrock 		}
1535e9dbad6fSeschrock 
1536e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1537e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1538e9dbad6fSeschrock 
1539e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1540e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1541e9dbad6fSeschrock 
1542e9dbad6fSeschrock 		return (0);
1543e9dbad6fSeschrock 	}
1544e9dbad6fSeschrock 
1545fa9e4066Sahrens 	/*
1546fa9e4066Sahrens 	 * Verify that this property is inheritable.
1547fa9e4066Sahrens 	 */
154899653d4eSeschrock 	if (zfs_prop_readonly(prop))
154999653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1550fa9e4066Sahrens 
155192241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
155299653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1553fa9e4066Sahrens 
1554fa9e4066Sahrens 	/*
1555fa9e4066Sahrens 	 * Check to see if the value applies to this type
1556fa9e4066Sahrens 	 */
155799653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
155899653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1559fa9e4066Sahrens 
1560bf7c2d40Srm160521 	/*
156136db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1562bf7c2d40Srm160521 	 */
1563bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1564fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1565e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1566fa9e4066Sahrens 
1567fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1568fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
156999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157099653d4eSeschrock 		    "dataset is used in a non-global zone"));
157199653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1572fa9e4066Sahrens 	}
1573fa9e4066Sahrens 
1574fa9e4066Sahrens 	/*
1575fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1576fa9e4066Sahrens 	 */
15770069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1578fa9e4066Sahrens 		return (-1);
1579fa9e4066Sahrens 
1580fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
158199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158299653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
158399653d4eSeschrock 		    "in a non-global zone"));
158499653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1585fa9e4066Sahrens 		goto error;
1586fa9e4066Sahrens 	}
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1589fa9e4066Sahrens 		goto error;
1590fa9e4066Sahrens 
1591e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
159299653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1593fa9e4066Sahrens 	} else {
1594fa9e4066Sahrens 
1595efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1596fa9e4066Sahrens 			goto error;
1597fa9e4066Sahrens 
1598fa9e4066Sahrens 		/*
1599fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1600fa9e4066Sahrens 		 */
1601fa9e4066Sahrens 		(void) get_stats(zhp);
1602fa9e4066Sahrens 	}
1603fa9e4066Sahrens 
1604fa9e4066Sahrens error:
1605fa9e4066Sahrens 	changelist_free(cl);
1606fa9e4066Sahrens 	return (ret);
1607fa9e4066Sahrens }
1608fa9e4066Sahrens 
1609fa9e4066Sahrens /*
16107f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
16117f7322feSeschrock  * extract them appropriately.
16127f7322feSeschrock  */
16137f7322feSeschrock static uint64_t
16147f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16157f7322feSeschrock {
16167f7322feSeschrock 	nvlist_t *nv;
16177f7322feSeschrock 	uint64_t value;
16187f7322feSeschrock 
1619a2eea2e1Sahrens 	*source = NULL;
16207f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
16217f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1622990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1623990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
16247f7322feSeschrock 	} else {
16252e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
16262e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
16277f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
16287f7322feSeschrock 		*source = "";
16297f7322feSeschrock 	}
16307f7322feSeschrock 
16317f7322feSeschrock 	return (value);
16327f7322feSeschrock }
16337f7322feSeschrock 
16347f7322feSeschrock static char *
16357f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16367f7322feSeschrock {
16377f7322feSeschrock 	nvlist_t *nv;
16387f7322feSeschrock 	char *value;
16397f7322feSeschrock 
1640a2eea2e1Sahrens 	*source = NULL;
16417f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
16427f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1643990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1644990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
16457f7322feSeschrock 	} else {
16462e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
16472e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
16487f7322feSeschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
16497f7322feSeschrock 			value = "";
16507f7322feSeschrock 		*source = "";
16517f7322feSeschrock 	}
16527f7322feSeschrock 
16537f7322feSeschrock 	return (value);
16547f7322feSeschrock }
16557f7322feSeschrock 
165692241e0bSTom Erickson static boolean_t
165792241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
165892241e0bSTom Erickson {
165992241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
166092241e0bSTom Erickson }
166192241e0bSTom Erickson 
166292241e0bSTom Erickson static void
166392241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
166492241e0bSTom Erickson {
166592241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
166692241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
166792241e0bSTom Erickson }
166892241e0bSTom Erickson 
166992241e0bSTom Erickson static void
167092241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
167192241e0bSTom Erickson {
167292241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
167392241e0bSTom Erickson 	*cookie = 0;
167492241e0bSTom Erickson }
167592241e0bSTom Erickson 
16767f7322feSeschrock /*
1677fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1678fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1679fa9e4066Sahrens  *
1680fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1681fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1682fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1683fa9e4066Sahrens  * the source "temporary".
1684fa9e4066Sahrens  */
168599653d4eSeschrock static int
1686990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
168799653d4eSeschrock     char **source, uint64_t *val)
1688fa9e4066Sahrens {
1689bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
169096510749Stimh 	nvlist_t *zplprops = NULL;
1691fa9e4066Sahrens 	struct mnttab mnt;
16923ccfa83cSahrens 	char *mntopt_on = NULL;
16933ccfa83cSahrens 	char *mntopt_off = NULL;
169492241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1695fa9e4066Sahrens 
1696fa9e4066Sahrens 	*source = NULL;
1697fa9e4066Sahrens 
16983ccfa83cSahrens 	switch (prop) {
16993ccfa83cSahrens 	case ZFS_PROP_ATIME:
17003ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
17013ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
17023ccfa83cSahrens 		break;
17033ccfa83cSahrens 
17043ccfa83cSahrens 	case ZFS_PROP_DEVICES:
17053ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
17063ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
17073ccfa83cSahrens 		break;
17083ccfa83cSahrens 
17093ccfa83cSahrens 	case ZFS_PROP_EXEC:
17103ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
17113ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
17123ccfa83cSahrens 		break;
17133ccfa83cSahrens 
17143ccfa83cSahrens 	case ZFS_PROP_READONLY:
17153ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
17163ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
17173ccfa83cSahrens 		break;
17183ccfa83cSahrens 
17193ccfa83cSahrens 	case ZFS_PROP_SETUID:
17203ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
17213ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
17223ccfa83cSahrens 		break;
17233ccfa83cSahrens 
17243ccfa83cSahrens 	case ZFS_PROP_XATTR:
17253ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
17263ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
17273ccfa83cSahrens 		break;
1728da6c28aaSamw 
1729da6c28aaSamw 	case ZFS_PROP_NBMAND:
1730da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
1731da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
1732da6c28aaSamw 		break;
17333ccfa83cSahrens 	}
17343ccfa83cSahrens 
17353bb79becSeschrock 	/*
17363bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
17373bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
17383bb79becSeschrock 	 * we're looking up a property which requires its presence.
17393bb79becSeschrock 	 */
17403bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
17413ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1742ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
1743ebedde84SEric Taylor 		struct mnttab entry;
17443bb79becSeschrock 
1745ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1746ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
17473ccfa83cSahrens 			    entry.mnt_mntopts);
17483ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
17493bb79becSeschrock 				return (-1);
17503ccfa83cSahrens 		}
17513bb79becSeschrock 
17523bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
17533bb79becSeschrock 	}
17543bb79becSeschrock 
1755fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
1756fa9e4066Sahrens 		mnt.mnt_mntopts = "";
1757fa9e4066Sahrens 	else
1758fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1759fa9e4066Sahrens 
1760fa9e4066Sahrens 	switch (prop) {
1761fa9e4066Sahrens 	case ZFS_PROP_ATIME:
1762fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
1763fa9e4066Sahrens 	case ZFS_PROP_EXEC:
17643ccfa83cSahrens 	case ZFS_PROP_READONLY:
17653ccfa83cSahrens 	case ZFS_PROP_SETUID:
17663ccfa83cSahrens 	case ZFS_PROP_XATTR:
1767da6c28aaSamw 	case ZFS_PROP_NBMAND:
176899653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
1769fa9e4066Sahrens 
177092241e0bSTom Erickson 		if (received)
177192241e0bSTom Erickson 			break;
177292241e0bSTom Erickson 
17733ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
177499653d4eSeschrock 			*val = B_TRUE;
1775fa9e4066Sahrens 			if (src)
1776990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
17773ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
177899653d4eSeschrock 			*val = B_FALSE;
1779fa9e4066Sahrens 			if (src)
1780990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
1781fa9e4066Sahrens 		}
178299653d4eSeschrock 		break;
1783fa9e4066Sahrens 
17843ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
1785d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
1786fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
1787a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
1788fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
1789a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
1790a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
179192241e0bSTom Erickson 
179292241e0bSTom Erickson 		if (*source == NULL) {
179392241e0bSTom Erickson 			/* not default, must be local */
1794fa9e4066Sahrens 			*source = zhp->zfs_name;
179592241e0bSTom Erickson 		}
179699653d4eSeschrock 		break;
1797fa9e4066Sahrens 
1798fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
179999653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
180099653d4eSeschrock 		break;
1801fa9e4066Sahrens 
180239c23413Seschrock 	case ZFS_PROP_NUMCLONES:
180339c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
180439c23413Seschrock 		break;
180539c23413Seschrock 
1806bd00f61bSrm160521 	case ZFS_PROP_VERSION:
1807de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
1808de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
1809de8267e0Stimh 	case ZFS_PROP_CASE:
1810de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1811de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
18123d7934e1Srm160521 			return (-1);
1813bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1814de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1815de8267e0Stimh 			zcmd_free_nvlists(&zc);
1816f4b94bdeSMatthew Ahrens 			return (-1);
1817bd00f61bSrm160521 		}
1818de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1819de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1820de8267e0Stimh 		    val) != 0) {
1821de8267e0Stimh 			zcmd_free_nvlists(&zc);
1822f4b94bdeSMatthew Ahrens 			return (-1);
1823de8267e0Stimh 		}
182496510749Stimh 		if (zplprops)
182596510749Stimh 			nvlist_free(zplprops);
1826de8267e0Stimh 		zcmd_free_nvlists(&zc);
1827bd00f61bSrm160521 		break;
1828bd00f61bSrm160521 
1829fa9e4066Sahrens 	default:
1830e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
183191ebeef5Sahrens 		case PROP_TYPE_NUMBER:
183291ebeef5Sahrens 		case PROP_TYPE_INDEX:
1833e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
183474e7dc98SMatthew Ahrens 			/*
183514843421SMatthew Ahrens 			 * If we tried to use a default value for a
183674e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
183731f572c2STom Erickson 			 * present.
183874e7dc98SMatthew Ahrens 			 */
183974e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
184031f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
184131f572c2STom Erickson 				*source = NULL;
184274e7dc98SMatthew Ahrens 			}
1843e7437265Sahrens 			break;
1844e7437265Sahrens 
184591ebeef5Sahrens 		case PROP_TYPE_STRING:
1846e7437265Sahrens 		default:
184799653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
184899653d4eSeschrock 			    "cannot get non-numeric property"));
184999653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
185099653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
1851fa9e4066Sahrens 		}
1852e7437265Sahrens 	}
1853fa9e4066Sahrens 
1854fa9e4066Sahrens 	return (0);
1855fa9e4066Sahrens }
1856fa9e4066Sahrens 
1857fa9e4066Sahrens /*
1858fa9e4066Sahrens  * Calculate the source type, given the raw source string.
1859fa9e4066Sahrens  */
1860fa9e4066Sahrens static void
1861990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1862fa9e4066Sahrens     char *statbuf, size_t statlen)
1863fa9e4066Sahrens {
1864990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1865fa9e4066Sahrens 		return;
1866fa9e4066Sahrens 
1867fa9e4066Sahrens 	if (source == NULL) {
1868990b4856Slling 		*srctype = ZPROP_SRC_NONE;
1869fa9e4066Sahrens 	} else if (source[0] == '\0') {
1870990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
187192241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
187292241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
1873fa9e4066Sahrens 	} else {
1874fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
1875990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
1876fa9e4066Sahrens 		} else {
1877fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
1878990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
1879fa9e4066Sahrens 		}
1880fa9e4066Sahrens 	}
1881fa9e4066Sahrens 
1882fa9e4066Sahrens }
1883fa9e4066Sahrens 
188492241e0bSTom Erickson int
188592241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
188692241e0bSTom Erickson     size_t proplen, boolean_t literal)
188792241e0bSTom Erickson {
188892241e0bSTom Erickson 	zfs_prop_t prop;
188992241e0bSTom Erickson 	int err = 0;
189092241e0bSTom Erickson 
189192241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
189292241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
189392241e0bSTom Erickson 			return (-1);
189492241e0bSTom Erickson 
189592241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
189692241e0bSTom Erickson 
189792241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
189892241e0bSTom Erickson 		uint64_t cookie;
189992241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
190092241e0bSTom Erickson 			return (-1);
190192241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
190292241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
190392241e0bSTom Erickson 		    NULL, NULL, 0, literal);
190492241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
190592241e0bSTom Erickson 	} else {
190692241e0bSTom Erickson 		nvlist_t *propval;
190792241e0bSTom Erickson 		char *recvdval;
190892241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
190992241e0bSTom Erickson 		    propname, &propval) != 0)
191092241e0bSTom Erickson 			return (-1);
191192241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
191292241e0bSTom Erickson 		    &recvdval) == 0);
191392241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
191492241e0bSTom Erickson 	}
191592241e0bSTom Erickson 
191692241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
191792241e0bSTom Erickson }
191892241e0bSTom Erickson 
191919b94df9SMatthew Ahrens static int
192019b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
192119b94df9SMatthew Ahrens {
192219b94df9SMatthew Ahrens 	nvlist_t *value;
192319b94df9SMatthew Ahrens 	nvpair_t *pair;
192419b94df9SMatthew Ahrens 
192519b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
192619b94df9SMatthew Ahrens 	if (value == NULL)
192719b94df9SMatthew Ahrens 		return (-1);
192819b94df9SMatthew Ahrens 
192919b94df9SMatthew Ahrens 	propbuf[0] = '\0';
193019b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
193119b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
193219b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
193319b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
193419b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
193519b94df9SMatthew Ahrens 	}
193619b94df9SMatthew Ahrens 
193719b94df9SMatthew Ahrens 	return (0);
193819b94df9SMatthew Ahrens }
193919b94df9SMatthew Ahrens 
194019b94df9SMatthew Ahrens struct get_clones_arg {
194119b94df9SMatthew Ahrens 	uint64_t numclones;
194219b94df9SMatthew Ahrens 	nvlist_t *value;
194319b94df9SMatthew Ahrens 	const char *origin;
194419b94df9SMatthew Ahrens 	char buf[ZFS_MAXNAMELEN];
194519b94df9SMatthew Ahrens };
194619b94df9SMatthew Ahrens 
194719b94df9SMatthew Ahrens int
194819b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
194919b94df9SMatthew Ahrens {
195019b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
195119b94df9SMatthew Ahrens 
195219b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
195319b94df9SMatthew Ahrens 		zfs_close(zhp);
195419b94df9SMatthew Ahrens 		return (0);
195519b94df9SMatthew Ahrens 	}
195619b94df9SMatthew Ahrens 
195719b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
195819b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
195919b94df9SMatthew Ahrens 		goto out;
196019b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
196119b94df9SMatthew Ahrens 		if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
196219b94df9SMatthew Ahrens 			zfs_close(zhp);
196319b94df9SMatthew Ahrens 			return (no_memory(zhp->zfs_hdl));
196419b94df9SMatthew Ahrens 		}
196519b94df9SMatthew Ahrens 		gca->numclones--;
196619b94df9SMatthew Ahrens 	}
196719b94df9SMatthew Ahrens 
196819b94df9SMatthew Ahrens out:
196919b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
197019b94df9SMatthew Ahrens 	zfs_close(zhp);
197119b94df9SMatthew Ahrens 	return (0);
197219b94df9SMatthew Ahrens }
197319b94df9SMatthew Ahrens 
197419b94df9SMatthew Ahrens nvlist_t *
197519b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
197619b94df9SMatthew Ahrens {
197719b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
197819b94df9SMatthew Ahrens 
197919b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
198019b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
198119b94df9SMatthew Ahrens 		struct get_clones_arg gca;
198219b94df9SMatthew Ahrens 
198319b94df9SMatthew Ahrens 		/*
198419b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
198519b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
198619b94df9SMatthew Ahrens 		 */
198719b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
198819b94df9SMatthew Ahrens 			return (NULL);
198919b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
199019b94df9SMatthew Ahrens 			return (NULL);
199119b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
199219b94df9SMatthew Ahrens 			nvlist_free(nv);
199319b94df9SMatthew Ahrens 			return (NULL);
199419b94df9SMatthew Ahrens 		}
199519b94df9SMatthew Ahrens 
199619b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
199719b94df9SMatthew Ahrens 		gca.value = value;
199819b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
199919b94df9SMatthew Ahrens 
200019b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
200119b94df9SMatthew Ahrens 			zfs_handle_t *root;
200219b94df9SMatthew Ahrens 			char pool[ZFS_MAXNAMELEN];
200319b94df9SMatthew Ahrens 			char *cp = pool;
200419b94df9SMatthew Ahrens 
200519b94df9SMatthew Ahrens 			/* get the pool name */
200619b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
200719b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
200819b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
200919b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
201019b94df9SMatthew Ahrens 
201119b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
201219b94df9SMatthew Ahrens 		}
201319b94df9SMatthew Ahrens 
201419b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
201519b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
201619b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
201719b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
201819b94df9SMatthew Ahrens 			nvlist_free(nv);
201919b94df9SMatthew Ahrens 			nvlist_free(value);
202019b94df9SMatthew Ahrens 			return (NULL);
202119b94df9SMatthew Ahrens 		}
202219b94df9SMatthew Ahrens 		nvlist_free(nv);
202319b94df9SMatthew Ahrens 		nvlist_free(value);
202419b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
202519b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
202619b94df9SMatthew Ahrens 	}
202719b94df9SMatthew Ahrens 
202819b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
202919b94df9SMatthew Ahrens 
203019b94df9SMatthew Ahrens 	return (value);
203119b94df9SMatthew Ahrens }
203219b94df9SMatthew Ahrens 
2033fa9e4066Sahrens /*
2034fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2035fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2036fa9e4066Sahrens  * human-readable form.
2037fa9e4066Sahrens  *
2038fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2039fa9e4066Sahrens  */
2040fa9e4066Sahrens int
2041fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2042990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2043fa9e4066Sahrens {
2044fa9e4066Sahrens 	char *source = NULL;
2045fa9e4066Sahrens 	uint64_t val;
2046fa9e4066Sahrens 	char *str;
2047e9dbad6fSeschrock 	const char *strval;
204892241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2049fa9e4066Sahrens 
2050fa9e4066Sahrens 	/*
2051fa9e4066Sahrens 	 * Check to see if this property applies to our object
2052fa9e4066Sahrens 	 */
2053fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2054fa9e4066Sahrens 		return (-1);
2055fa9e4066Sahrens 
205692241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
205792241e0bSTom Erickson 		return (-1);
205892241e0bSTom Erickson 
2059fa9e4066Sahrens 	if (src)
2060990b4856Slling 		*src = ZPROP_SRC_NONE;
2061fa9e4066Sahrens 
2062fa9e4066Sahrens 	switch (prop) {
2063fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2064fa9e4066Sahrens 		/*
2065fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2066fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2067fa9e4066Sahrens 		 */
2068fa9e4066Sahrens 		{
2069a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2070a2eea2e1Sahrens 			time_t time = (time_t)val;
2071fa9e4066Sahrens 			struct tm t;
2072fa9e4066Sahrens 
2073fa9e4066Sahrens 			if (literal ||
2074fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2075fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2076fa9e4066Sahrens 			    &t) == 0)
2077a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2078fa9e4066Sahrens 		}
2079fa9e4066Sahrens 		break;
2080fa9e4066Sahrens 
2081fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2082fa9e4066Sahrens 		/*
2083fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2084fa9e4066Sahrens 		 *
2085fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2086fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2087fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2088fa9e4066Sahrens 		 *
2089fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2090fa9e4066Sahrens 		 * root to any values we return.
2091fa9e4066Sahrens 		 */
209229ab75c9Srm160521 
20937f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2094fa9e4066Sahrens 
2095b21718f0Sgw25295 		if (str[0] == '/') {
209629ab75c9Srm160521 			char buf[MAXPATHLEN];
209729ab75c9Srm160521 			char *root = buf;
2098a79992aaSTom Erickson 			const char *relpath;
2099fa9e4066Sahrens 
2100a79992aaSTom Erickson 			/*
2101a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2102a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2103a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2104a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2105a79992aaSTom Erickson 			 * inherited.
2106a79992aaSTom Erickson 			 */
2107a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2108a79992aaSTom Erickson 				relpath = "";
2109a79992aaSTom Erickson 			} else {
2110a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2111fa9e4066Sahrens 				if (relpath[0] == '/')
2112fa9e4066Sahrens 					relpath++;
2113a79992aaSTom Erickson 			}
2114b21718f0Sgw25295 
211529ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
211629ab75c9Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
211729ab75c9Srm160521 			    (strcmp(root, "-") == 0))
211829ab75c9Srm160521 				root[0] = '\0';
2119b21718f0Sgw25295 			/*
2120b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2121b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2122b21718f0Sgw25295 			 * mountpoint path.
2123b21718f0Sgw25295 			 */
2124b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2125b21718f0Sgw25295 				root++;
2126b21718f0Sgw25295 
2127b21718f0Sgw25295 			/*
2128b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2129b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2130b21718f0Sgw25295 			 * an inherited mountpoint.
2131b21718f0Sgw25295 			 */
2132b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2133b21718f0Sgw25295 			    relpath[0] != '\0'))
21347f7322feSeschrock 				str++;
2135fa9e4066Sahrens 
2136fa9e4066Sahrens 			if (relpath[0] == '\0')
2137fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
21387f7322feSeschrock 				    root, str);
2139fa9e4066Sahrens 			else
2140fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
21417f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2142fa9e4066Sahrens 				    relpath);
2143fa9e4066Sahrens 		} else {
2144fa9e4066Sahrens 			/* 'legacy' or 'none' */
21457f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2146fa9e4066Sahrens 		}
2147fa9e4066Sahrens 
2148fa9e4066Sahrens 		break;
2149fa9e4066Sahrens 
2150fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
2151a2eea2e1Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2152fa9e4066Sahrens 		    proplen);
2153fa9e4066Sahrens 		/*
2154fa9e4066Sahrens 		 * If there is no parent at all, return failure to indicate that
2155fa9e4066Sahrens 		 * it doesn't apply to this dataset.
2156fa9e4066Sahrens 		 */
2157fa9e4066Sahrens 		if (propbuf[0] == '\0')
2158fa9e4066Sahrens 			return (-1);
2159fa9e4066Sahrens 		break;
2160fa9e4066Sahrens 
216119b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
216219b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
216319b94df9SMatthew Ahrens 			return (-1);
216419b94df9SMatthew Ahrens 		break;
216519b94df9SMatthew Ahrens 
2166fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2167a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2168fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2169a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2170a9799022Sck153898 
217199653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
217299653d4eSeschrock 			return (-1);
2173fa9e4066Sahrens 
2174fa9e4066Sahrens 		/*
2175fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2176fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2177fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2178fa9e4066Sahrens 		 * that its set locally.
2179fa9e4066Sahrens 		 */
2180fa9e4066Sahrens 		if (val == 0) {
2181fa9e4066Sahrens 			if (literal)
2182fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2183fa9e4066Sahrens 			else
2184fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2185fa9e4066Sahrens 		} else {
2186fa9e4066Sahrens 			if (literal)
21875ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
21885ad82045Snd150628 				    (u_longlong_t)val);
2189fa9e4066Sahrens 			else
2190fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2191fa9e4066Sahrens 		}
2192fa9e4066Sahrens 		break;
2193fa9e4066Sahrens 
2194187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2195fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
219699653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
219799653d4eSeschrock 			return (-1);
2198b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2199b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2200b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2201fa9e4066Sahrens 		break;
2202fa9e4066Sahrens 
2203fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2204fa9e4066Sahrens 		switch (zhp->zfs_type) {
2205fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2206fa9e4066Sahrens 			str = "filesystem";
2207fa9e4066Sahrens 			break;
2208fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2209fa9e4066Sahrens 			str = "volume";
2210fa9e4066Sahrens 			break;
2211fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2212fa9e4066Sahrens 			str = "snapshot";
2213fa9e4066Sahrens 			break;
2214fa9e4066Sahrens 		default:
221599653d4eSeschrock 			abort();
2216fa9e4066Sahrens 		}
2217fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2218fa9e4066Sahrens 		break;
2219fa9e4066Sahrens 
2220fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2221fa9e4066Sahrens 		/*
2222fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2223fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2224fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2225fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2226fa9e4066Sahrens 		 */
222799653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
222899653d4eSeschrock 		    src, &source, &val) != 0)
222999653d4eSeschrock 			return (-1);
223099653d4eSeschrock 		if (val)
2231fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2232fa9e4066Sahrens 		else
2233fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2234fa9e4066Sahrens 		break;
2235fa9e4066Sahrens 
2236fa9e4066Sahrens 	case ZFS_PROP_NAME:
2237fa9e4066Sahrens 		/*
2238fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2239fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2240fa9e4066Sahrens 		 * consumers.
2241fa9e4066Sahrens 		 */
2242fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2243fa9e4066Sahrens 		break;
2244fa9e4066Sahrens 
22454201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
22464201a95eSRic Aleshire 		{
22474201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
22484201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
22494201a95eSRic Aleshire 
22504201a95eSRic Aleshire 			(void) strlcpy(propbuf,
22514201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
22524201a95eSRic Aleshire 
22534201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
22544201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
22554201a95eSRic Aleshire 				break;
22564201a95eSRic Aleshire 
22574201a95eSRic Aleshire 			/*
22584201a95eSRic Aleshire 			 * Try to translate the internal hex string to
22594201a95eSRic Aleshire 			 * human-readable output.  If there are any
22604201a95eSRic Aleshire 			 * problems just use the hex string.
22614201a95eSRic Aleshire 			 */
22624201a95eSRic Aleshire 
22634201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
22644201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
22654201a95eSRic Aleshire 				m_label_free(new_sl);
22664201a95eSRic Aleshire 				break;
22674201a95eSRic Aleshire 			}
22684201a95eSRic Aleshire 
22694201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
22704201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
22714201a95eSRic Aleshire 				if (ascii)
22724201a95eSRic Aleshire 					free(ascii);
22734201a95eSRic Aleshire 				m_label_free(new_sl);
22744201a95eSRic Aleshire 				break;
22754201a95eSRic Aleshire 			}
22764201a95eSRic Aleshire 			m_label_free(new_sl);
22774201a95eSRic Aleshire 
22784201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
22794201a95eSRic Aleshire 			free(ascii);
22804201a95eSRic Aleshire 		}
22814201a95eSRic Aleshire 		break;
22824201a95eSRic Aleshire 
2283fa9e4066Sahrens 	default:
2284e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
228591ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2286e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2287e7437265Sahrens 			    &source, &val) != 0)
2288e7437265Sahrens 				return (-1);
2289e7437265Sahrens 			if (literal)
2290e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2291e7437265Sahrens 				    (u_longlong_t)val);
2292e7437265Sahrens 			else
2293e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2294e7437265Sahrens 			break;
2295e7437265Sahrens 
229691ebeef5Sahrens 		case PROP_TYPE_STRING:
2297e7437265Sahrens 			(void) strlcpy(propbuf,
2298e7437265Sahrens 			    getprop_string(zhp, prop, &source), proplen);
2299e7437265Sahrens 			break;
2300e7437265Sahrens 
230191ebeef5Sahrens 		case PROP_TYPE_INDEX:
2302fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2303fe192f76Sahrens 			    &source, &val) != 0)
2304fe192f76Sahrens 				return (-1);
2305fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2306e7437265Sahrens 				return (-1);
2307e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2308e7437265Sahrens 			break;
2309e7437265Sahrens 
2310e7437265Sahrens 		default:
231199653d4eSeschrock 			abort();
2312fa9e4066Sahrens 		}
2313e7437265Sahrens 	}
2314fa9e4066Sahrens 
2315fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2316fa9e4066Sahrens 
2317fa9e4066Sahrens 	return (0);
2318fa9e4066Sahrens }
2319fa9e4066Sahrens 
2320fa9e4066Sahrens /*
2321fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2322fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2323fa9e4066Sahrens  * hard-coded property types.
2324fa9e4066Sahrens  */
2325fa9e4066Sahrens uint64_t
2326fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2327fa9e4066Sahrens {
2328fa9e4066Sahrens 	char *source;
232999653d4eSeschrock 	uint64_t val;
2330fa9e4066Sahrens 
23313cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
233299653d4eSeschrock 
233399653d4eSeschrock 	return (val);
2334fa9e4066Sahrens }
2335fa9e4066Sahrens 
23367b97dc1aSrm160521 int
23377b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
23387b97dc1aSrm160521 {
23397b97dc1aSrm160521 	char buf[64];
23407b97dc1aSrm160521 
234114843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
23427b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
23437b97dc1aSrm160521 }
23447b97dc1aSrm160521 
2345fa9e4066Sahrens /*
2346fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2347fa9e4066Sahrens  */
2348fa9e4066Sahrens int
2349fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2350990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2351fa9e4066Sahrens {
2352fa9e4066Sahrens 	char *source;
2353fa9e4066Sahrens 
2354fa9e4066Sahrens 	/*
2355fa9e4066Sahrens 	 * Check to see if this property applies to our object
2356fa9e4066Sahrens 	 */
2357e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2358ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
235999653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
236099653d4eSeschrock 		    zfs_prop_to_name(prop)));
2361e45ce728Sahrens 	}
2362fa9e4066Sahrens 
2363fa9e4066Sahrens 	if (src)
2364990b4856Slling 		*src = ZPROP_SRC_NONE;
2365fa9e4066Sahrens 
236699653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
236799653d4eSeschrock 		return (-1);
2368fa9e4066Sahrens 
2369fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2370fa9e4066Sahrens 
2371fa9e4066Sahrens 	return (0);
2372fa9e4066Sahrens }
2373fa9e4066Sahrens 
237414843421SMatthew Ahrens static int
237514843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
237614843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
237714843421SMatthew Ahrens {
237814843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
237914843421SMatthew Ahrens 	idmap_stat status;
238014843421SMatthew Ahrens 	int err = EINVAL;
238114843421SMatthew Ahrens 
23821fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
238314843421SMatthew Ahrens 		goto out;
238414843421SMatthew Ahrens 
238514843421SMatthew Ahrens 	if (isuser) {
238614843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
238714843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
238814843421SMatthew Ahrens 	} else {
238914843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
239014843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
239114843421SMatthew Ahrens 	}
239214843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
239314843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
239414843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
239514843421SMatthew Ahrens 		err = 0;
239614843421SMatthew Ahrens 	else
239714843421SMatthew Ahrens 		err = EINVAL;
239814843421SMatthew Ahrens out:
239914843421SMatthew Ahrens 	if (get_hdl)
240014843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
240114843421SMatthew Ahrens 	return (err);
240214843421SMatthew Ahrens }
240314843421SMatthew Ahrens 
240414843421SMatthew Ahrens /*
240514843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
240614843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
240714843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
240814843421SMatthew Ahrens  */
240914843421SMatthew Ahrens static int
241014843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
241114843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
241214843421SMatthew Ahrens {
241314843421SMatthew Ahrens 	zfs_userquota_prop_t type;
241414843421SMatthew Ahrens 	char *cp, *end;
24153b12c289SMatthew Ahrens 	char *numericsid = NULL;
241614843421SMatthew Ahrens 	boolean_t isuser;
241714843421SMatthew Ahrens 
241814843421SMatthew Ahrens 	domain[0] = '\0';
241914843421SMatthew Ahrens 
242014843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
242114843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
242214843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
242314843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
242414843421SMatthew Ahrens 			break;
242514843421SMatthew Ahrens 	}
242614843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
242714843421SMatthew Ahrens 		return (EINVAL);
242814843421SMatthew Ahrens 	*typep = type;
242914843421SMatthew Ahrens 
243014843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
243114843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
243214843421SMatthew Ahrens 
243314843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
243414843421SMatthew Ahrens 
243514843421SMatthew Ahrens 	if (strchr(cp, '@')) {
243614843421SMatthew Ahrens 		/*
243714843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
24383b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
243914843421SMatthew Ahrens 		 */
24403b12c289SMatthew Ahrens 		directory_error_t e;
244114843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
244214843421SMatthew Ahrens 			return (ENOENT);
24433b12c289SMatthew Ahrens 		if (isuser) {
24443b12c289SMatthew Ahrens 			e = directory_sid_from_user_name(NULL,
24453b12c289SMatthew Ahrens 			    cp, &numericsid);
24463b12c289SMatthew Ahrens 		} else {
24473b12c289SMatthew Ahrens 			e = directory_sid_from_group_name(NULL,
24483b12c289SMatthew Ahrens 			    cp, &numericsid);
24493b12c289SMatthew Ahrens 		}
24503b12c289SMatthew Ahrens 		if (e != NULL) {
24513b12c289SMatthew Ahrens 			directory_error_free(e);
245214843421SMatthew Ahrens 			return (ENOENT);
24533b12c289SMatthew Ahrens 		}
24543b12c289SMatthew Ahrens 		if (numericsid == NULL)
245514843421SMatthew Ahrens 			return (ENOENT);
24563b12c289SMatthew Ahrens 		cp = numericsid;
24573b12c289SMatthew Ahrens 		/* will be further decoded below */
24583b12c289SMatthew Ahrens 	}
24593b12c289SMatthew Ahrens 
24603b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
246114843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
24623b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
246314843421SMatthew Ahrens 		cp = strrchr(domain, '-');
246414843421SMatthew Ahrens 		*cp = '\0';
246514843421SMatthew Ahrens 		cp++;
246614843421SMatthew Ahrens 
246714843421SMatthew Ahrens 		errno = 0;
246814843421SMatthew Ahrens 		*ridp = strtoull(cp, &end, 10);
24693b12c289SMatthew Ahrens 		if (numericsid) {
24703b12c289SMatthew Ahrens 			free(numericsid);
24713b12c289SMatthew Ahrens 			numericsid = NULL;
24723b12c289SMatthew Ahrens 		}
2473777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
247414843421SMatthew Ahrens 			return (EINVAL);
247514843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
247614843421SMatthew Ahrens 		/*
247714843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
247814843421SMatthew Ahrens 		 * turned into a uid/gid
247914843421SMatthew Ahrens 		 */
248014843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
248114843421SMatthew Ahrens 			return (ENOENT);
248214843421SMatthew Ahrens 		if (isuser) {
248314843421SMatthew Ahrens 			struct passwd *pw;
248414843421SMatthew Ahrens 			pw = getpwnam(cp);
248514843421SMatthew Ahrens 			if (pw == NULL)
248614843421SMatthew Ahrens 				return (ENOENT);
248714843421SMatthew Ahrens 			*ridp = pw->pw_uid;
248814843421SMatthew Ahrens 		} else {
248914843421SMatthew Ahrens 			struct group *gr;
249014843421SMatthew Ahrens 			gr = getgrnam(cp);
249114843421SMatthew Ahrens 			if (gr == NULL)
249214843421SMatthew Ahrens 				return (ENOENT);
249314843421SMatthew Ahrens 			*ridp = gr->gr_gid;
249414843421SMatthew Ahrens 		}
249514843421SMatthew Ahrens 	} else {
249614843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
249714843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
249814843421SMatthew Ahrens 		idmap_rid_t rid;
249914843421SMatthew Ahrens 		char *mapdomain;
250014843421SMatthew Ahrens 
250114843421SMatthew Ahrens 		if (*end != '\0')
250214843421SMatthew Ahrens 			return (EINVAL);
250314843421SMatthew Ahrens 		if (id > MAXUID) {
250414843421SMatthew Ahrens 			/* It's an ephemeral ID. */
250514843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
250614843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
250714843421SMatthew Ahrens 				return (ENOENT);
25083b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
250914843421SMatthew Ahrens 			*ridp = rid;
251014843421SMatthew Ahrens 		} else {
251114843421SMatthew Ahrens 			*ridp = id;
251214843421SMatthew Ahrens 		}
251314843421SMatthew Ahrens 	}
251414843421SMatthew Ahrens 
25153b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
251614843421SMatthew Ahrens 	return (0);
251714843421SMatthew Ahrens }
251814843421SMatthew Ahrens 
2519edea4b55SLin Ling static int
2520edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2521edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
252214843421SMatthew Ahrens {
252314843421SMatthew Ahrens 	int err;
252414843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
252514843421SMatthew Ahrens 
252619b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
252714843421SMatthew Ahrens 
252814843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
252914843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2530edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2531edea4b55SLin Ling 	zc.zc_objset_type = *typep;
253214843421SMatthew Ahrens 	if (err)
253314843421SMatthew Ahrens 		return (err);
253414843421SMatthew Ahrens 
253514843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
253614843421SMatthew Ahrens 	if (err)
253714843421SMatthew Ahrens 		return (err);
253814843421SMatthew Ahrens 
2539edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2540edea4b55SLin Ling 	return (0);
2541edea4b55SLin Ling }
2542edea4b55SLin Ling 
2543edea4b55SLin Ling int
2544edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2545edea4b55SLin Ling     uint64_t *propvalue)
2546edea4b55SLin Ling {
2547edea4b55SLin Ling 	zfs_userquota_prop_t type;
2548edea4b55SLin Ling 
2549edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2550edea4b55SLin Ling 	    &type));
2551edea4b55SLin Ling }
2552edea4b55SLin Ling 
2553edea4b55SLin Ling int
2554edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2555edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2556edea4b55SLin Ling {
2557edea4b55SLin Ling 	int err;
2558edea4b55SLin Ling 	uint64_t propvalue;
2559edea4b55SLin Ling 	zfs_userquota_prop_t type;
2560edea4b55SLin Ling 
2561edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2562edea4b55SLin Ling 	    &type);
2563edea4b55SLin Ling 
2564edea4b55SLin Ling 	if (err)
2565edea4b55SLin Ling 		return (err);
2566edea4b55SLin Ling 
256714843421SMatthew Ahrens 	if (literal) {
2568edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2569edea4b55SLin Ling 	} else if (propvalue == 0 &&
257014843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
257114843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
257214843421SMatthew Ahrens 	} else {
2573edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
257414843421SMatthew Ahrens 	}
257514843421SMatthew Ahrens 	return (0);
257614843421SMatthew Ahrens }
257714843421SMatthew Ahrens 
257819b94df9SMatthew Ahrens int
257919b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
258019b94df9SMatthew Ahrens     uint64_t *propvalue)
258119b94df9SMatthew Ahrens {
258219b94df9SMatthew Ahrens 	int err;
258319b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
258419b94df9SMatthew Ahrens 	const char *snapname;
258519b94df9SMatthew Ahrens 
258619b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
258719b94df9SMatthew Ahrens 
258819b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
258919b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
259019b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
259119b94df9SMatthew Ahrens 	} else {
259219b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
259319b94df9SMatthew Ahrens 		char *cp;
259419b94df9SMatthew Ahrens 
259519b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
259619b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
259719b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
259819b94df9SMatthew Ahrens 		if (cp != NULL)
259919b94df9SMatthew Ahrens 			*cp = '\0';
260019b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
260119b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
260219b94df9SMatthew Ahrens 	}
260319b94df9SMatthew Ahrens 
260419b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
260519b94df9SMatthew Ahrens 	if (err)
260619b94df9SMatthew Ahrens 		return (err);
260719b94df9SMatthew Ahrens 
260819b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
260919b94df9SMatthew Ahrens 	return (0);
261019b94df9SMatthew Ahrens }
261119b94df9SMatthew Ahrens 
261219b94df9SMatthew Ahrens int
261319b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
261419b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
261519b94df9SMatthew Ahrens {
261619b94df9SMatthew Ahrens 	int err;
261719b94df9SMatthew Ahrens 	uint64_t propvalue;
261819b94df9SMatthew Ahrens 
261919b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
262019b94df9SMatthew Ahrens 
262119b94df9SMatthew Ahrens 	if (err)
262219b94df9SMatthew Ahrens 		return (err);
262319b94df9SMatthew Ahrens 
262419b94df9SMatthew Ahrens 	if (literal) {
262519b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
262619b94df9SMatthew Ahrens 	} else {
262719b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
262819b94df9SMatthew Ahrens 	}
262919b94df9SMatthew Ahrens 	return (0);
263019b94df9SMatthew Ahrens }
263119b94df9SMatthew Ahrens 
263219b94df9SMatthew Ahrens int
263319b94df9SMatthew Ahrens zfs_get_snapused_int(zfs_handle_t *firstsnap, zfs_handle_t *lastsnap,
263419b94df9SMatthew Ahrens     uint64_t *usedp)
263519b94df9SMatthew Ahrens {
263619b94df9SMatthew Ahrens 	int err;
263719b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
263819b94df9SMatthew Ahrens 
263919b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, lastsnap->zfs_name, sizeof (zc.zc_name));
264019b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_value, firstsnap->zfs_name, sizeof (zc.zc_value));
264119b94df9SMatthew Ahrens 
264219b94df9SMatthew Ahrens 	err = ioctl(lastsnap->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_SNAPS, &zc);
264319b94df9SMatthew Ahrens 	if (err)
264419b94df9SMatthew Ahrens 		return (err);
264519b94df9SMatthew Ahrens 
264619b94df9SMatthew Ahrens 	*usedp = zc.zc_cookie;
264719b94df9SMatthew Ahrens 
264819b94df9SMatthew Ahrens 	return (0);
264919b94df9SMatthew Ahrens }
265019b94df9SMatthew Ahrens 
2651fa9e4066Sahrens /*
2652fa9e4066Sahrens  * Returns the name of the given zfs handle.
2653fa9e4066Sahrens  */
2654fa9e4066Sahrens const char *
2655fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
2656fa9e4066Sahrens {
2657fa9e4066Sahrens 	return (zhp->zfs_name);
2658fa9e4066Sahrens }
2659fa9e4066Sahrens 
2660fa9e4066Sahrens /*
2661fa9e4066Sahrens  * Returns the type of the given zfs handle.
2662fa9e4066Sahrens  */
2663fa9e4066Sahrens zfs_type_t
2664fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
2665fa9e4066Sahrens {
2666fa9e4066Sahrens 	return (zhp->zfs_type);
2667fa9e4066Sahrens }
2668fa9e4066Sahrens 
26697f7322feSeschrock /*
2670d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
2671d41c4376SMark J Musante  *
2672d41c4376SMark J Musante  * Needs to handle these cases:
2673d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2674d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2675d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
2676d41c4376SMark J Musante  */
2677d41c4376SMark J Musante static boolean_t
2678d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
2679d41c4376SMark J Musante {
2680d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
2681d41c4376SMark J Musante 
2682d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
2683d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
2684d41c4376SMark J Musante 		return (B_FALSE);
2685d41c4376SMark J Musante 
2686d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
2687d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2688d41c4376SMark J Musante }
2689d41c4376SMark J Musante 
2690d41c4376SMark J Musante /*
2691fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
269219b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
269319b94df9SMatthew Ahrens  * pool).
2694fa9e4066Sahrens  */
2695fa9e4066Sahrens static int
2696fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
2697fa9e4066Sahrens {
269819b94df9SMatthew Ahrens 	char *slashp;
2699fa9e4066Sahrens 
270019b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
270119b94df9SMatthew Ahrens 
270219b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
2703fa9e4066Sahrens 		return (-1);
270419b94df9SMatthew Ahrens 	*slashp = '\0';
2705fa9e4066Sahrens 
2706fa9e4066Sahrens 	return (0);
2707fa9e4066Sahrens }
2708fa9e4066Sahrens 
2709fa9e4066Sahrens /*
27107f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
27117f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
27127f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
27137f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
27147f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
27157f1f55eaSvb160487  * new datasets.
2716fa9e4066Sahrens  */
2717fa9e4066Sahrens static int
27187f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27197f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
2720fa9e4066Sahrens {
2721fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2722fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
2723fa9e4066Sahrens 	char *slash;
27247f7322feSeschrock 	zfs_handle_t *zhp;
272599653d4eSeschrock 	char errbuf[1024];
2726d41c4376SMark J Musante 	uint64_t is_zoned;
272799653d4eSeschrock 
2728deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
2729deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2730fa9e4066Sahrens 
2731fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
2732fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
273399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
273499653d4eSeschrock 		    "missing dataset name"));
273599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2736fa9e4066Sahrens 	}
2737fa9e4066Sahrens 
2738fa9e4066Sahrens 	/* check to see if the pool exists */
2739fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2740fa9e4066Sahrens 		slash = parent + strlen(parent);
27418ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
2742fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
274399653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2744fa9e4066Sahrens 	    errno == ENOENT) {
274599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
274699653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
274799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2748fa9e4066Sahrens 	}
2749fa9e4066Sahrens 
2750fa9e4066Sahrens 	/* check to see if the parent dataset exists */
27517f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27527f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
27537f1f55eaSvb160487 			/*
27547f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
27557f1f55eaSvb160487 			 */
27567f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27577f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27587f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
27597f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27607f1f55eaSvb160487 			}
27617f1f55eaSvb160487 		} else if (errno == ENOENT) {
276299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
276399653d4eSeschrock 			    "parent does not exist"));
276499653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27657f1f55eaSvb160487 		} else
276699653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2767fa9e4066Sahrens 	}
2768fa9e4066Sahrens 
2769d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2770d41c4376SMark J Musante 	if (zoned != NULL)
2771d41c4376SMark J Musante 		*zoned = is_zoned;
2772d41c4376SMark J Musante 
2773fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
2774d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
277599653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27767f7322feSeschrock 		zfs_close(zhp);
2777fa9e4066Sahrens 		return (-1);
2778fa9e4066Sahrens 	}
2779fa9e4066Sahrens 
2780fa9e4066Sahrens 	/* make sure parent is a filesystem */
27817f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
278299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
278399653d4eSeschrock 		    "parent is not a filesystem"));
278499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27857f7322feSeschrock 		zfs_close(zhp);
2786fa9e4066Sahrens 		return (-1);
2787fa9e4066Sahrens 	}
2788fa9e4066Sahrens 
27897f7322feSeschrock 	zfs_close(zhp);
27907f1f55eaSvb160487 	if (prefixlen != NULL)
27917f1f55eaSvb160487 		*prefixlen = strlen(parent);
27927f1f55eaSvb160487 	return (0);
27937f1f55eaSvb160487 }
27947f1f55eaSvb160487 
27957f1f55eaSvb160487 /*
27967f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
27977f1f55eaSvb160487  */
27987f1f55eaSvb160487 boolean_t
27997f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
28007f1f55eaSvb160487 {
28017f1f55eaSvb160487 	zfs_handle_t *zhp;
28027f1f55eaSvb160487 
2803f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
28047f1f55eaSvb160487 		return (B_FALSE);
28057f1f55eaSvb160487 
28067f1f55eaSvb160487 	/*
28077f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
28087f1f55eaSvb160487 	 */
28097f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
28107f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
28117f1f55eaSvb160487 
28127f1f55eaSvb160487 		zfs_close(zhp);
28137f1f55eaSvb160487 		if (types & ds_type)
28147f1f55eaSvb160487 			return (B_TRUE);
28157f1f55eaSvb160487 	}
28167f1f55eaSvb160487 	return (B_FALSE);
28177f1f55eaSvb160487 }
28187f1f55eaSvb160487 
28197f1f55eaSvb160487 /*
28203cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
28213cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
28223cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
28233cb34c60Sahrens  */
28243cb34c60Sahrens int
28253cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
28263cb34c60Sahrens {
28273cb34c60Sahrens 	zfs_handle_t *h;
28283cb34c60Sahrens 	char *cp;
28293cb34c60Sahrens 	const char *opname;
28303cb34c60Sahrens 
28313cb34c60Sahrens 	/* make sure prefix exists */
28323cb34c60Sahrens 	cp = target + prefixlen;
28333cb34c60Sahrens 	if (*cp != '/') {
28343cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
28353cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28363cb34c60Sahrens 	} else {
28373cb34c60Sahrens 		*cp = '\0';
28383cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28393cb34c60Sahrens 		*cp = '/';
28403cb34c60Sahrens 	}
28413cb34c60Sahrens 	if (h == NULL)
28423cb34c60Sahrens 		return (-1);
28433cb34c60Sahrens 	zfs_close(h);
28443cb34c60Sahrens 
28453cb34c60Sahrens 	/*
28463cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28473cb34c60Sahrens 	 * up to the prefixlen-long one.
28483cb34c60Sahrens 	 */
28493cb34c60Sahrens 	for (cp = target + prefixlen + 1;
28503cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28513cb34c60Sahrens 		char *logstr;
28523cb34c60Sahrens 
28533cb34c60Sahrens 		*cp = '\0';
28543cb34c60Sahrens 
28553cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
28563cb34c60Sahrens 		if (h) {
28573cb34c60Sahrens 			/* it already exists, nothing to do here */
28583cb34c60Sahrens 			zfs_close(h);
28593cb34c60Sahrens 			continue;
28603cb34c60Sahrens 		}
28613cb34c60Sahrens 
28623cb34c60Sahrens 		logstr = hdl->libzfs_log_str;
28633cb34c60Sahrens 		hdl->libzfs_log_str = NULL;
28643cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28653cb34c60Sahrens 		    NULL) != 0) {
28663cb34c60Sahrens 			hdl->libzfs_log_str = logstr;
28673cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28683cb34c60Sahrens 			goto ancestorerr;
28693cb34c60Sahrens 		}
28703cb34c60Sahrens 
28713cb34c60Sahrens 		hdl->libzfs_log_str = logstr;
28723cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28733cb34c60Sahrens 		if (h == NULL) {
28743cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28753cb34c60Sahrens 			goto ancestorerr;
28763cb34c60Sahrens 		}
28773cb34c60Sahrens 
28783cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28793cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28803cb34c60Sahrens 			goto ancestorerr;
28813cb34c60Sahrens 		}
28823cb34c60Sahrens 
28833cb34c60Sahrens 		if (zfs_share(h) != 0) {
28843cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28853cb34c60Sahrens 			goto ancestorerr;
28863cb34c60Sahrens 		}
28873cb34c60Sahrens 
28883cb34c60Sahrens 		zfs_close(h);
28893cb34c60Sahrens 	}
28903cb34c60Sahrens 
28913cb34c60Sahrens 	return (0);
28923cb34c60Sahrens 
28933cb34c60Sahrens ancestorerr:
28943cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28953cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28963cb34c60Sahrens 	return (-1);
28973cb34c60Sahrens }
28983cb34c60Sahrens 
28993cb34c60Sahrens /*
29007f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
29017f1f55eaSvb160487  */
29027f1f55eaSvb160487 int
29037f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
29047f1f55eaSvb160487 {
29057f1f55eaSvb160487 	int prefix;
29067f1f55eaSvb160487 	char *path_copy;
29077f1f55eaSvb160487 	int rc;
29087f1f55eaSvb160487 
2909d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
29107f1f55eaSvb160487 		return (-1);
29117f1f55eaSvb160487 
29127f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
29137f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
29147f1f55eaSvb160487 		free(path_copy);
29157f1f55eaSvb160487 	}
29167f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
29177f1f55eaSvb160487 		return (-1);
29187f1f55eaSvb160487 
2919fa9e4066Sahrens 	return (0);
2920fa9e4066Sahrens }
2921fa9e4066Sahrens 
2922fa9e4066Sahrens /*
2923e9dbad6fSeschrock  * Create a new filesystem or volume.
2924fa9e4066Sahrens  */
2925fa9e4066Sahrens int
292699653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2927e9dbad6fSeschrock     nvlist_t *props)
2928fa9e4066Sahrens {
2929fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2930fa9e4066Sahrens 	int ret;
2931fa9e4066Sahrens 	uint64_t size = 0;
2932fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
293399653d4eSeschrock 	char errbuf[1024];
2934e9dbad6fSeschrock 	uint64_t zoned;
293599653d4eSeschrock 
293699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
293799653d4eSeschrock 	    "cannot create '%s'"), path);
2938fa9e4066Sahrens 
2939fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
2940f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
294199653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2942fa9e4066Sahrens 
2943fa9e4066Sahrens 	/* validate parents exist */
29447f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2945fa9e4066Sahrens 		return (-1);
2946fa9e4066Sahrens 
2947fa9e4066Sahrens 	/*
2948fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
2949fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
2950fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2951fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2952fa9e4066Sahrens 	 * first try to see if the dataset exists.
2953fa9e4066Sahrens 	 */
2954fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2955990b4856Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
295699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
295799653d4eSeschrock 		    "dataset already exists"));
295899653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2959fa9e4066Sahrens 	}
2960fa9e4066Sahrens 
2961fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
2962fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2963fa9e4066Sahrens 	else
2964fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2965fa9e4066Sahrens 
29660a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
2967b1b8ab34Slling 	    zoned, NULL, errbuf)) == 0)
2968e9dbad6fSeschrock 		return (-1);
2969e9dbad6fSeschrock 
2970fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29715c5460e9Seschrock 		/*
29725c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
29735c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29745c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29755c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29765c5460e9Seschrock 		 * zero.
29775c5460e9Seschrock 		 */
2978e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
2979e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2980e9dbad6fSeschrock 			nvlist_free(props);
298199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2982e9dbad6fSeschrock 			    "missing volume size"));
2983e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2984fa9e4066Sahrens 		}
2985fa9e4066Sahrens 
2986e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
2987e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2988e9dbad6fSeschrock 		    &blocksize)) != 0) {
2989e9dbad6fSeschrock 			if (ret == ENOENT) {
2990e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
2991e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
2992e9dbad6fSeschrock 			} else {
2993e9dbad6fSeschrock 				nvlist_free(props);
299499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2995e9dbad6fSeschrock 				    "missing volume block size"));
2996e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2997e9dbad6fSeschrock 			}
2998e9dbad6fSeschrock 		}
2999e9dbad6fSeschrock 
3000e9dbad6fSeschrock 		if (size == 0) {
3001e9dbad6fSeschrock 			nvlist_free(props);
3002e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3003e9dbad6fSeschrock 			    "volume size cannot be zero"));
3004e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30055c5460e9Seschrock 		}
30065c5460e9Seschrock 
30075c5460e9Seschrock 		if (size % blocksize != 0) {
3008e9dbad6fSeschrock 			nvlist_free(props);
300999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3010e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3011e9dbad6fSeschrock 			    "size"));
3012e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3013e9dbad6fSeschrock 		}
30145c5460e9Seschrock 	}
30155c5460e9Seschrock 
3016990b4856Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3017e9dbad6fSeschrock 		return (-1);
3018e9dbad6fSeschrock 	nvlist_free(props);
3019fa9e4066Sahrens 
3020fa9e4066Sahrens 	/* create the dataset */
3021ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3022fa9e4066Sahrens 
3023e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
3024e9dbad6fSeschrock 
3025fa9e4066Sahrens 	/* check for failure */
3026fa9e4066Sahrens 	if (ret != 0) {
3027fa9e4066Sahrens 		char parent[ZFS_MAXNAMELEN];
3028fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3029fa9e4066Sahrens 
3030fa9e4066Sahrens 		switch (errno) {
3031fa9e4066Sahrens 		case ENOENT:
303299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
303399653d4eSeschrock 			    "no such parent '%s'"), parent);
303499653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3035fa9e4066Sahrens 
3036fa9e4066Sahrens 		case EINVAL:
303799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3038d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
303999653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3040fa9e4066Sahrens 
3041fa9e4066Sahrens 		case EDOM:
304299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3043e9dbad6fSeschrock 			    "volume block size must be power of 2 from "
3044e9dbad6fSeschrock 			    "%u to %uk"),
3045fa9e4066Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3046fa9e4066Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
304799653d4eSeschrock 
3048e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
304999653d4eSeschrock 
305040feaa91Sahrens 		case ENOTSUP:
305140feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
305240feaa91Sahrens 			    "pool must be upgraded to set this "
305340feaa91Sahrens 			    "property or value"));
305440feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3055fa9e4066Sahrens #ifdef _ILP32
3056fa9e4066Sahrens 		case EOVERFLOW:
3057fa9e4066Sahrens 			/*
3058fa9e4066Sahrens 			 * This platform can't address a volume this big.
3059fa9e4066Sahrens 			 */
306099653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
306199653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
306299653d4eSeschrock 				    errbuf));
3063fa9e4066Sahrens #endif
306499653d4eSeschrock 			/* FALLTHROUGH */
3065fa9e4066Sahrens 		default:
306699653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3067fa9e4066Sahrens 		}
3068fa9e4066Sahrens 	}
3069fa9e4066Sahrens 
3070fa9e4066Sahrens 	return (0);
3071fa9e4066Sahrens }
3072fa9e4066Sahrens 
3073fa9e4066Sahrens /*
3074fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3075fa9e4066Sahrens  * isn't mounted, and that there are no active dependents.
3076fa9e4066Sahrens  */
3077fa9e4066Sahrens int
3078842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3079fa9e4066Sahrens {
3080fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3081fa9e4066Sahrens 
3082fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3083fa9e4066Sahrens 
3084e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3085fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3086fa9e4066Sahrens 	} else {
3087fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3088fa9e4066Sahrens 	}
3089fa9e4066Sahrens 
3090842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
3091ecd6cf80Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3092ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
309399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
309499653d4eSeschrock 		    zhp->zfs_name));
30951d452cf5Sahrens 	}
3096fa9e4066Sahrens 
3097fa9e4066Sahrens 	remove_mountpoint(zhp);
3098fa9e4066Sahrens 
3099fa9e4066Sahrens 	return (0);
3100fa9e4066Sahrens }
3101fa9e4066Sahrens 
31021d452cf5Sahrens struct destroydata {
310319b94df9SMatthew Ahrens 	nvlist_t *nvl;
310419b94df9SMatthew Ahrens 	const char *snapname;
31051d452cf5Sahrens };
31061d452cf5Sahrens 
31071d452cf5Sahrens static int
3108681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
31091d452cf5Sahrens {
31101d452cf5Sahrens 	struct destroydata *dd = arg;
31111d452cf5Sahrens 	zfs_handle_t *szhp;
31121d452cf5Sahrens 	char name[ZFS_MAXNAMELEN];
3113681d9761SEric Taylor 	int rv = 0;
31141d452cf5Sahrens 
311519b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
311619b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
31171d452cf5Sahrens 
31181d452cf5Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31191d452cf5Sahrens 	if (szhp) {
312019b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
31211d452cf5Sahrens 		zfs_close(szhp);
31221d452cf5Sahrens 	}
31231d452cf5Sahrens 
312419b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
31253ccfa83cSahrens 	zfs_close(zhp);
31263ccfa83cSahrens 	return (rv);
31271d452cf5Sahrens }
31281d452cf5Sahrens 
31291d452cf5Sahrens /*
31301d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31311d452cf5Sahrens  */
31321d452cf5Sahrens int
3133842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
31341d452cf5Sahrens {
31351d452cf5Sahrens 	int ret;
31361d452cf5Sahrens 	struct destroydata dd = { 0 };
31371d452cf5Sahrens 
31381d452cf5Sahrens 	dd.snapname = snapname;
313919b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
314019b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
31411d452cf5Sahrens 
314219b94df9SMatthew Ahrens 	if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
314319b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31441d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
314519b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
314619b94df9SMatthew Ahrens 	} else {
314719b94df9SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
314819b94df9SMatthew Ahrens 	}
314919b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
315019b94df9SMatthew Ahrens 	return (ret);
3151e5351341SMatthew Ahrens }
3152e5351341SMatthew Ahrens 
315319b94df9SMatthew Ahrens /*
315419b94df9SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.  They must be underneath
315519b94df9SMatthew Ahrens  * the zhp (either snapshots of it, or snapshots of its descendants).
315619b94df9SMatthew Ahrens  */
315719b94df9SMatthew Ahrens int
315819b94df9SMatthew Ahrens zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
315919b94df9SMatthew Ahrens {
316019b94df9SMatthew Ahrens 	int ret;
316119b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
316219b94df9SMatthew Ahrens 
31631d452cf5Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
316419b94df9SMatthew Ahrens 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
316519b94df9SMatthew Ahrens 		return (-1);
3166842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
31671d452cf5Sahrens 
316819b94df9SMatthew Ahrens 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
31691d452cf5Sahrens 	if (ret != 0) {
31701d452cf5Sahrens 		char errbuf[1024];
31711d452cf5Sahrens 
31721d452cf5Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
317319b94df9SMatthew Ahrens 		    "cannot destroy snapshots in %s"), zc.zc_name);
31741d452cf5Sahrens 
31751d452cf5Sahrens 		switch (errno) {
31761d452cf5Sahrens 		case EEXIST:
31771d452cf5Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31781d452cf5Sahrens 			    "snapshot is cloned"));
31791d452cf5Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31801d452cf5Sahrens 
31811d452cf5Sahrens 		default:
31821d452cf5Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31831d452cf5Sahrens 			    errbuf));
31841d452cf5Sahrens 		}
31851d452cf5Sahrens 	}
31861d452cf5Sahrens 
31871d452cf5Sahrens 	return (0);
31881d452cf5Sahrens }
31891d452cf5Sahrens 
3190fa9e4066Sahrens /*
3191fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3192fa9e4066Sahrens  */
3193fa9e4066Sahrens int
3194e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3195fa9e4066Sahrens {
3196fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3197fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
3198fa9e4066Sahrens 	int ret;
319999653d4eSeschrock 	char errbuf[1024];
320099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3201e9dbad6fSeschrock 	zfs_type_t type;
3202e9dbad6fSeschrock 	uint64_t zoned;
3203fa9e4066Sahrens 
3204fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3205fa9e4066Sahrens 
320699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
320799653d4eSeschrock 	    "cannot create '%s'"), target);
320899653d4eSeschrock 
320919b94df9SMatthew Ahrens 	/* validate the target/clone name */
3210f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
321199653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3212fa9e4066Sahrens 
3213fa9e4066Sahrens 	/* validate parents exist */
32147f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3215fa9e4066Sahrens 		return (-1);
3216fa9e4066Sahrens 
3217fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3218fa9e4066Sahrens 
3219fa9e4066Sahrens 	/* do the clone */
3220e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3221fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32225f8e1617Snn35248 		type = ZFS_TYPE_VOLUME;
3223e9dbad6fSeschrock 	} else {
3224fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32255f8e1617Snn35248 		type = ZFS_TYPE_FILESYSTEM;
3226e9dbad6fSeschrock 	}
3227e9dbad6fSeschrock 
3228e9dbad6fSeschrock 	if (props) {
32290a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
32300a48a24eStimh 		    zhp, errbuf)) == NULL)
3231e9dbad6fSeschrock 			return (-1);
3232e9dbad6fSeschrock 
3233990b4856Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3234e9dbad6fSeschrock 			nvlist_free(props);
3235e9dbad6fSeschrock 			return (-1);
3236e9dbad6fSeschrock 		}
3237e9dbad6fSeschrock 
3238e9dbad6fSeschrock 		nvlist_free(props);
3239e9dbad6fSeschrock 	}
3240fa9e4066Sahrens 
3241fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3242e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3243ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3244fa9e4066Sahrens 
3245e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
3246e9dbad6fSeschrock 
3247fa9e4066Sahrens 	if (ret != 0) {
3248fa9e4066Sahrens 		switch (errno) {
3249fa9e4066Sahrens 
3250fa9e4066Sahrens 		case ENOENT:
3251fa9e4066Sahrens 			/*
3252fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3253fa9e4066Sahrens 			 * above, but there may a race condition that has since
3254fa9e4066Sahrens 			 * destroyed the parent.
3255fa9e4066Sahrens 			 *
3256fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3257fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3258fa9e4066Sahrens 			 * dataset doesn't exist.
3259fa9e4066Sahrens 			 */
326099653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
326199653d4eSeschrock 			    "no such parent '%s'"), parent);
326299653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3263fa9e4066Sahrens 
326499653d4eSeschrock 		case EXDEV:
326599653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
326699653d4eSeschrock 			    "source and target pools differ"));
326799653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
326899653d4eSeschrock 			    errbuf));
326999653d4eSeschrock 
327099653d4eSeschrock 		default:
327199653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
327299653d4eSeschrock 			    errbuf));
327399653d4eSeschrock 		}
327499653d4eSeschrock 	}
327599653d4eSeschrock 
327699653d4eSeschrock 	return (ret);
327799653d4eSeschrock }
327899653d4eSeschrock 
327999653d4eSeschrock /*
328099653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
328199653d4eSeschrock  */
328299653d4eSeschrock int
328399653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
328499653d4eSeschrock {
328599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
328699653d4eSeschrock 	zfs_cmd_t zc = { 0 };
328799653d4eSeschrock 	char parent[MAXPATHLEN];
328899653d4eSeschrock 	int ret;
328999653d4eSeschrock 	char errbuf[1024];
329099653d4eSeschrock 
329199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
329299653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
329399653d4eSeschrock 
329499653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
329599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
329699653d4eSeschrock 		    "snapshots can not be promoted"));
329799653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
329899653d4eSeschrock 	}
329999653d4eSeschrock 
33003cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
330199653d4eSeschrock 	if (parent[0] == '\0') {
330299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
330399653d4eSeschrock 		    "not a cloned filesystem"));
330499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
330599653d4eSeschrock 	}
330699653d4eSeschrock 
33073cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3308e9dbad6fSeschrock 	    sizeof (zc.zc_value));
330999653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3310ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
331199653d4eSeschrock 
331299653d4eSeschrock 	if (ret != 0) {
33130b69c2f0Sahrens 		int save_errno = errno;
3314fa9e4066Sahrens 
33150b69c2f0Sahrens 		switch (save_errno) {
3316fa9e4066Sahrens 		case EEXIST:
3317681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
331899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3319681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3320681d9761SEric Taylor 			    zc.zc_string, parent);
332199653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3322fa9e4066Sahrens 
3323fa9e4066Sahrens 		default:
33240b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3325fa9e4066Sahrens 		}
3326fa9e4066Sahrens 	}
3327e9dbad6fSeschrock 	return (ret);
33281d452cf5Sahrens }
33291d452cf5Sahrens 
3330fa9e4066Sahrens /*
333172bdce51Sahl  * Takes a snapshot of the given dataset.
3332fa9e4066Sahrens  */
3333fa9e4066Sahrens int
3334bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3335bb0ade09Sahrens     nvlist_t *props)
3336fa9e4066Sahrens {
3337fa9e4066Sahrens 	const char *delim;
3338bb0ade09Sahrens 	char parent[ZFS_MAXNAMELEN];
3339fa9e4066Sahrens 	zfs_handle_t *zhp;
3340fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3341fa9e4066Sahrens 	int ret;
334299653d4eSeschrock 	char errbuf[1024];
3343fa9e4066Sahrens 
334499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
334599653d4eSeschrock 	    "cannot snapshot '%s'"), path);
334699653d4eSeschrock 
334799653d4eSeschrock 	/* validate the target name */
3348f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
334999653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3350fa9e4066Sahrens 
3351bb0ade09Sahrens 	if (props) {
3352bb0ade09Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3353bb0ade09Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
3354bb0ade09Sahrens 			return (-1);
3355bb0ade09Sahrens 
3356bb0ade09Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3357bb0ade09Sahrens 			nvlist_free(props);
3358bb0ade09Sahrens 			return (-1);
3359bb0ade09Sahrens 		}
3360bb0ade09Sahrens 
3361bb0ade09Sahrens 		nvlist_free(props);
3362bb0ade09Sahrens 	}
3363bb0ade09Sahrens 
3364fa9e4066Sahrens 	/* make sure the parent exists and is of the appropriate type */
33651d452cf5Sahrens 	delim = strchr(path, '@');
33668ac09fceSRichard Lowe 	(void) strncpy(parent, path, delim - path);
3367fa9e4066Sahrens 	parent[delim - path] = '\0';
3368fa9e4066Sahrens 
336999653d4eSeschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3370fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3371bb0ade09Sahrens 		zcmd_free_nvlists(&zc);
3372fa9e4066Sahrens 		return (-1);
3373fa9e4066Sahrens 	}
3374fa9e4066Sahrens 
33751d452cf5Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3376e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3377ecd6cf80Smarks 	if (ZFS_IS_VOLUME(zhp))
3378ecd6cf80Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
3379ecd6cf80Smarks 	else
3380ecd6cf80Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
33811d452cf5Sahrens 	zc.zc_cookie = recursive;
3382ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3383fa9e4066Sahrens 
3384bb0ade09Sahrens 	zcmd_free_nvlists(&zc);
3385bb0ade09Sahrens 
33861d452cf5Sahrens 	/*
33871d452cf5Sahrens 	 * if it was recursive, the one that actually failed will be in
33881d452cf5Sahrens 	 * zc.zc_name.
33891d452cf5Sahrens 	 */
3390681d9761SEric Taylor 	if (ret != 0) {
33911d452cf5Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3392e9dbad6fSeschrock 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
339399653d4eSeschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3394681d9761SEric Taylor 	}
3395fa9e4066Sahrens 
3396fa9e4066Sahrens 	zfs_close(zhp);
3397fa9e4066Sahrens 
3398fa9e4066Sahrens 	return (ret);
3399fa9e4066Sahrens }
3400fa9e4066Sahrens 
3401fa9e4066Sahrens /*
3402b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3403b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3404b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3405b12a1c38Slling  * group.
3406fa9e4066Sahrens  */
3407b12a1c38Slling typedef struct rollback_data {
3408b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3409b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3410c391e322Sahrens 	boolean_t	cb_error;
341199653d4eSeschrock 	boolean_t	cb_dependent;
3412c391e322Sahrens 	boolean_t	cb_force;
3413b12a1c38Slling } rollback_data_t;
3414b12a1c38Slling 
3415b12a1c38Slling static int
3416b12a1c38Slling rollback_destroy(zfs_handle_t *zhp, void *data)
3417b12a1c38Slling {
3418b12a1c38Slling 	rollback_data_t *cbp = data;
3419b12a1c38Slling 
3420b12a1c38Slling 	if (!cbp->cb_dependent) {
3421b12a1c38Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3422b12a1c38Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3423b12a1c38Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3424b12a1c38Slling 		    cbp->cb_create) {
3425ecd6cf80Smarks 			char *logstr;
3426b12a1c38Slling 
342799653d4eSeschrock 			cbp->cb_dependent = B_TRUE;
34284ccbb6e7Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
34294ccbb6e7Sahrens 			    rollback_destroy, cbp);
343099653d4eSeschrock 			cbp->cb_dependent = B_FALSE;
3431b12a1c38Slling 
3432ecd6cf80Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
3433ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
3434842727c2SChris Kirby 			cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3435ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3436b12a1c38Slling 		}
3437b12a1c38Slling 	} else {
3438c391e322Sahrens 		/* We must destroy this clone; first unmount it */
3439c391e322Sahrens 		prop_changelist_t *clp;
3440c391e322Sahrens 
34410069fd67STim Haley 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3442c391e322Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
3443c391e322Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
3444c391e322Sahrens 			cbp->cb_error = B_TRUE;
3445c391e322Sahrens 			zfs_close(zhp);
3446c391e322Sahrens 			return (0);
3447c391e322Sahrens 		}
3448842727c2SChris Kirby 		if (zfs_destroy(zhp, B_FALSE) != 0)
3449c391e322Sahrens 			cbp->cb_error = B_TRUE;
3450c391e322Sahrens 		else
3451c391e322Sahrens 			changelist_remove(clp, zhp->zfs_name);
3452ba7b046eSahrens 		(void) changelist_postfix(clp);
3453c391e322Sahrens 		changelist_free(clp);
3454b12a1c38Slling 	}
3455b12a1c38Slling 
3456b12a1c38Slling 	zfs_close(zhp);
3457b12a1c38Slling 	return (0);
3458b12a1c38Slling }
3459b12a1c38Slling 
3460b12a1c38Slling /*
34614ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
34624ccbb6e7Sahrens  * data changes since then and making it the active dataset.
34634ccbb6e7Sahrens  *
34644ccbb6e7Sahrens  * Any snapshots more recent than the target are destroyed, along with
34654ccbb6e7Sahrens  * their dependents.
3466b12a1c38Slling  */
34674ccbb6e7Sahrens int
3468c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3469fa9e4066Sahrens {
34704ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
34714ccbb6e7Sahrens 	int err;
3472fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
34737b97dc1aSrm160521 	boolean_t restore_resv = 0;
34747b97dc1aSrm160521 	uint64_t old_volsize, new_volsize;
34757b97dc1aSrm160521 	zfs_prop_t resv_prop;
3476fa9e4066Sahrens 
3477fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3478fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3479fa9e4066Sahrens 
34804ccbb6e7Sahrens 	/*
34814ccbb6e7Sahrens 	 * Destroy all recent snapshots and its dependends.
34824ccbb6e7Sahrens 	 */
3483c391e322Sahrens 	cb.cb_force = force;
34844ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
34854ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
34864ccbb6e7Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
34874ccbb6e7Sahrens 
3488c391e322Sahrens 	if (cb.cb_error)
3489c391e322Sahrens 		return (-1);
34904ccbb6e7Sahrens 
34914ccbb6e7Sahrens 	/*
34924ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
34934ccbb6e7Sahrens 	 * rollback to the given snapshot.
34944ccbb6e7Sahrens 	 */
34954ccbb6e7Sahrens 
34967b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
34977b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
34987b97dc1aSrm160521 			return (-1);
34997b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35007b97dc1aSrm160521 		restore_resv =
35017b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
35027b97dc1aSrm160521 	}
3503fa9e4066Sahrens 
3504fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3505fa9e4066Sahrens 
3506e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3507fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3508fa9e4066Sahrens 	else
3509fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3510fa9e4066Sahrens 
3511fa9e4066Sahrens 	/*
35124ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
35134ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
35144ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
35154ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
35164ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
35177b97dc1aSrm160521 	 *
3518fa9e4066Sahrens 	 */
35194ccbb6e7Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3520ece3d9b3Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
352199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
352299653d4eSeschrock 		    zhp->zfs_name);
3523b9415e83Srm160521 		return (err);
3524b9415e83Srm160521 	}
3525fa9e4066Sahrens 
35267b97dc1aSrm160521 	/*
35277b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
35287b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
35297b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
35307b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
35317b97dc1aSrm160521 	 */
3532b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3533b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
35347b97dc1aSrm160521 		if (restore_resv) {
35357b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35367b97dc1aSrm160521 			if (old_volsize != new_volsize)
3537b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
3538b9415e83Srm160521 				    new_volsize);
35397b97dc1aSrm160521 		}
35407b97dc1aSrm160521 		zfs_close(zhp);
35417b97dc1aSrm160521 	}
35424ccbb6e7Sahrens 	return (err);
3543b12a1c38Slling }
3544b12a1c38Slling 
3545b12a1c38Slling /*
3546fa9e4066Sahrens  * Renames the given dataset.
3547fa9e4066Sahrens  */
3548fa9e4066Sahrens int
3549*6a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3550*6a9cb0eaSEric Schrock     boolean_t force_unmount)
3551fa9e4066Sahrens {
3552fa9e4066Sahrens 	int ret;
3553fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3554fa9e4066Sahrens 	char *delim;
3555cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3556cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3557cdf5b4caSmmusante 	char *parentname = NULL;
3558fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
355999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
356099653d4eSeschrock 	char errbuf[1024];
3561fa9e4066Sahrens 
3562fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3563fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3564fa9e4066Sahrens 		return (0);
3565fa9e4066Sahrens 
356699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
356799653d4eSeschrock 	    "cannot rename to '%s'"), target);
356899653d4eSeschrock 
3569fa9e4066Sahrens 	/*
3570fa9e4066Sahrens 	 * Make sure the target name is valid
3571fa9e4066Sahrens 	 */
3572fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
357398579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
357498579b20Snd150628 		    *target == '@') {
357598579b20Snd150628 			/*
357698579b20Snd150628 			 * Snapshot target name is abbreviated,
357798579b20Snd150628 			 * reconstruct full dataset name
357898579b20Snd150628 			 */
357998579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
358098579b20Snd150628 			    sizeof (parent));
358198579b20Snd150628 			delim = strchr(parent, '@');
358298579b20Snd150628 			if (strchr(target, '@') == NULL)
358398579b20Snd150628 				*(++delim) = '\0';
358498579b20Snd150628 			else
358598579b20Snd150628 				*delim = '\0';
358698579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
358798579b20Snd150628 			target = parent;
358898579b20Snd150628 		} else {
3589fa9e4066Sahrens 			/*
3590fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
3591fa9e4066Sahrens 			 */
359298579b20Snd150628 			delim = strchr(target, '@');
359398579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
359498579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
359599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
359698579b20Snd150628 				    "snapshots must be part of same "
359798579b20Snd150628 				    "dataset"));
359898579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
359998579b20Snd150628 				    errbuf));
3600fa9e4066Sahrens 			}
360198579b20Snd150628 		}
3602f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
360398579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3604fa9e4066Sahrens 	} else {
3605cdf5b4caSmmusante 		if (recursive) {
3606cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3607cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
3608cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3609cdf5b4caSmmusante 		}
3610cdf5b4caSmmusante 
3611f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
361298579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3613e9dbad6fSeschrock 
3614fa9e4066Sahrens 		/* validate parents */
3615d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3616fa9e4066Sahrens 			return (-1);
3617fa9e4066Sahrens 
3618fa9e4066Sahrens 		/* make sure we're in the same pool */
3619fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3620fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3621fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
362299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
362399653d4eSeschrock 			    "datasets must be within same pool"));
362499653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3625fa9e4066Sahrens 		}
3626f2fdf992Snd150628 
3627f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
3628d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
3629f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3630d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
3631f2fdf992Snd150628 			    "current dataset name"));
3632f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3633f2fdf992Snd150628 		}
3634fa9e4066Sahrens 	}
3635fa9e4066Sahrens 
363699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
363799653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
363899653d4eSeschrock 
3639fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3640fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
364199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
364299653d4eSeschrock 		    "dataset is used in a non-global zone"));
364399653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3644fa9e4066Sahrens 	}
3645fa9e4066Sahrens 
3646cdf5b4caSmmusante 	if (recursive) {
3647cdf5b4caSmmusante 
3648f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3649f0c5ee21Smmusante 		if (parentname == NULL) {
3650f0c5ee21Smmusante 			ret = -1;
3651f0c5ee21Smmusante 			goto error;
3652f0c5ee21Smmusante 		}
3653cdf5b4caSmmusante 		delim = strchr(parentname, '@');
3654cdf5b4caSmmusante 		*delim = '\0';
3655990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3656cdf5b4caSmmusante 		if (zhrp == NULL) {
3657f0c5ee21Smmusante 			ret = -1;
3658f0c5ee21Smmusante 			goto error;
3659cdf5b4caSmmusante 		}
3660cdf5b4caSmmusante 
3661cdf5b4caSmmusante 	} else {
3662*6a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3663*6a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
366499653d4eSeschrock 			return (-1);
3665fa9e4066Sahrens 
3666fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
366799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
366899653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
366999653d4eSeschrock 			    "in a non-global zone"));
3670e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3671fa9e4066Sahrens 			goto error;
3672fa9e4066Sahrens 		}
3673fa9e4066Sahrens 
3674fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
3675fa9e4066Sahrens 			goto error;
3676cdf5b4caSmmusante 	}
3677fa9e4066Sahrens 
3678e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3679fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3680fa9e4066Sahrens 	else
3681fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3682fa9e4066Sahrens 
368398579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3684e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
368598579b20Snd150628 
3686cdf5b4caSmmusante 	zc.zc_cookie = recursive;
3687cdf5b4caSmmusante 
3688ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3689cdf5b4caSmmusante 		/*
3690cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
3691cdf5b4caSmmusante 		 * be in zc.zc_name
3692cdf5b4caSmmusante 		 */
3693cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36943cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
3695cdf5b4caSmmusante 
3696cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
3697cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3698cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
3699cdf5b4caSmmusante 			    "with the new name"));
3700a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3701cdf5b4caSmmusante 		} else {
370299653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3703cdf5b4caSmmusante 		}
3704fa9e4066Sahrens 
3705fa9e4066Sahrens 		/*
3706fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
3707fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
3708fa9e4066Sahrens 		 */
3709681d9761SEric Taylor 		if (!recursive)
3710fa9e4066Sahrens 			(void) changelist_postfix(cl);
3711cdf5b4caSmmusante 	} else {
3712681d9761SEric Taylor 		if (!recursive) {
3713fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
3714fa9e4066Sahrens 			ret = changelist_postfix(cl);
3715fa9e4066Sahrens 		}
3716cdf5b4caSmmusante 	}
3717fa9e4066Sahrens 
3718fa9e4066Sahrens error:
3719cdf5b4caSmmusante 	if (parentname) {
3720cdf5b4caSmmusante 		free(parentname);
3721cdf5b4caSmmusante 	}
3722cdf5b4caSmmusante 	if (zhrp) {
3723cdf5b4caSmmusante 		zfs_close(zhrp);
3724cdf5b4caSmmusante 	}
3725cdf5b4caSmmusante 	if (cl) {
3726fa9e4066Sahrens 		changelist_free(cl);
3727cdf5b4caSmmusante 	}
3728fa9e4066Sahrens 	return (ret);
3729fa9e4066Sahrens }
3730fa9e4066Sahrens 
3731e9dbad6fSeschrock nvlist_t *
3732e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
3733e9dbad6fSeschrock {
3734e9dbad6fSeschrock 	return (zhp->zfs_user_props);
3735e9dbad6fSeschrock }
3736e9dbad6fSeschrock 
373792241e0bSTom Erickson nvlist_t *
373892241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
373992241e0bSTom Erickson {
374092241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
374192241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
374292241e0bSTom Erickson 			return (NULL);
374392241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
374492241e0bSTom Erickson }
374592241e0bSTom Erickson 
3746e9dbad6fSeschrock /*
3747e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
3748e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
3749e9dbad6fSeschrock  *
3750e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
3751e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
3752e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
3753e9dbad6fSeschrock  *
3754e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
375592241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
375692241e0bSTom Erickson  *        requested received property values, we also need to compute the width
375792241e0bSTom Erickson  *        of the RECEIVED column.
3758e9dbad6fSeschrock  */
3759e9dbad6fSeschrock int
376092241e0bSTom Erickson zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3761e9dbad6fSeschrock {
3762e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3763990b4856Slling 	zprop_list_t *entry;
3764990b4856Slling 	zprop_list_t **last, **start;
3765e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
3766e9dbad6fSeschrock 	nvpair_t *elem;
3767e9dbad6fSeschrock 	char *strval;
3768e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
3769e9dbad6fSeschrock 
3770990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3771e9dbad6fSeschrock 		return (-1);
3772e9dbad6fSeschrock 
3773e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
3774e9dbad6fSeschrock 
3775e9dbad6fSeschrock 	entry = *plp;
3776e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3777e9dbad6fSeschrock 		/*
3778e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
3779e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
3780e9dbad6fSeschrock 		 * non-native property.
3781e9dbad6fSeschrock 		 */
3782e9dbad6fSeschrock 		start = plp;
3783e9dbad6fSeschrock 		while (*start != NULL) {
3784990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
3785e9dbad6fSeschrock 				break;
3786e9dbad6fSeschrock 			start = &(*start)->pl_next;
3787e9dbad6fSeschrock 		}
3788e9dbad6fSeschrock 
3789e9dbad6fSeschrock 		elem = NULL;
3790e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3791e9dbad6fSeschrock 			/*
3792e9dbad6fSeschrock 			 * See if we've already found this property in our list.
3793e9dbad6fSeschrock 			 */
3794e9dbad6fSeschrock 			for (last = start; *last != NULL;
3795e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
3796e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
3797e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
3798e9dbad6fSeschrock 					break;
3799e9dbad6fSeschrock 			}
3800e9dbad6fSeschrock 
3801e9dbad6fSeschrock 			if (*last == NULL) {
3802e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
3803990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
3804e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
3805e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
3806e9dbad6fSeschrock 					free(entry);
3807e9dbad6fSeschrock 					return (-1);
3808e9dbad6fSeschrock 				}
3809e9dbad6fSeschrock 
3810990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
3811e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
3812e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
3813e9dbad6fSeschrock 				*last = entry;
3814e9dbad6fSeschrock 			}
3815e9dbad6fSeschrock 		}
3816e9dbad6fSeschrock 	}
3817e9dbad6fSeschrock 
3818e9dbad6fSeschrock 	/*
3819e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
3820e9dbad6fSeschrock 	 */
3821e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3822e9dbad6fSeschrock 		if (entry->pl_fixed)
3823e9dbad6fSeschrock 			continue;
3824e9dbad6fSeschrock 
3825990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
3826e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
3827e9dbad6fSeschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3828e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
3829e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
3830e9dbad6fSeschrock 			}
383192241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
383292241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
383392241e0bSTom Erickson 			    buf, sizeof (buf), B_FALSE) == 0)
383492241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
383592241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
383692241e0bSTom Erickson 		} else {
383792241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
383892241e0bSTom Erickson 			    &propval) == 0) {
3839e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
3840990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
3841e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
3842e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
3843e9dbad6fSeschrock 			}
384492241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
384592241e0bSTom Erickson 			    entry->pl_user_prop,
384692241e0bSTom Erickson 			    buf, sizeof (buf), B_FALSE) == 0)
384792241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
384892241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
384992241e0bSTom Erickson 		}
3850e9dbad6fSeschrock 	}
3851e9dbad6fSeschrock 
3852e9dbad6fSeschrock 	return (0);
3853e9dbad6fSeschrock }
3854ecd6cf80Smarks 
3855ecd6cf80Smarks int
3856ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3857743a77edSAlan Wright     char *resource, void *export, void *sharetab,
3858743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
3859ecd6cf80Smarks {
3860ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
3861ecd6cf80Smarks 	int error;
3862ecd6cf80Smarks 
3863ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3864ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3865743a77edSAlan Wright 	if (resource)
3866743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3867ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3868ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3869da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
3870ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
3871ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3872ecd6cf80Smarks 	return (error);
3873ecd6cf80Smarks }
38742e5e9e19SSanjeev Bagewadi 
38752e5e9e19SSanjeev Bagewadi void
38762e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
38772e5e9e19SSanjeev Bagewadi {
38782e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
38792e5e9e19SSanjeev Bagewadi 
38802e5e9e19SSanjeev Bagewadi 	/*
38812e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
38822e5e9e19SSanjeev Bagewadi 	 * properties.
38832e5e9e19SSanjeev Bagewadi 	 */
38842e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
38852e5e9e19SSanjeev Bagewadi 
38862e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
38872e5e9e19SSanjeev Bagewadi 
38882e5e9e19SSanjeev Bagewadi 	while (curr) {
38892e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
38902e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
38912e5e9e19SSanjeev Bagewadi 
389214843421SMatthew Ahrens 		/*
3893faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
3894faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
3895faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
3896faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
3897faaa6415SEric Schrock 		 * software, for example).
389814843421SMatthew Ahrens 		 */
389914843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
39002e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
39012e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
39022e5e9e19SSanjeev Bagewadi 		curr = next;
39032e5e9e19SSanjeev Bagewadi 	}
39042e5e9e19SSanjeev Bagewadi }
3905743a77edSAlan Wright 
3906743a77edSAlan Wright static int
3907743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3908743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3909743a77edSAlan Wright {
3910743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
3911743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
3912743a77edSAlan Wright 	int error;
3913743a77edSAlan Wright 
3914743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3915743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3916743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
3917743a77edSAlan Wright 
3918743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
3919743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3920743a77edSAlan Wright 			(void) no_memory(hdl);
3921743a77edSAlan Wright 			return (NULL);
3922743a77edSAlan Wright 		}
3923743a77edSAlan Wright 	}
3924743a77edSAlan Wright 
3925743a77edSAlan Wright 	switch (cmd) {
3926743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
3927743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
3928743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3929743a77edSAlan Wright 		break;
3930743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
3931743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3932743a77edSAlan Wright 		    resource1) != 0) {
3933743a77edSAlan Wright 				(void) no_memory(hdl);
3934743a77edSAlan Wright 				return (-1);
3935743a77edSAlan Wright 		}
3936743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3937743a77edSAlan Wright 		    resource2) != 0) {
3938743a77edSAlan Wright 				(void) no_memory(hdl);
3939743a77edSAlan Wright 				return (-1);
3940743a77edSAlan Wright 		}
3941743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3942743a77edSAlan Wright 			nvlist_free(nvlist);
3943743a77edSAlan Wright 			return (-1);
3944743a77edSAlan Wright 		}
3945743a77edSAlan Wright 		break;
3946743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
3947743a77edSAlan Wright 		break;
3948743a77edSAlan Wright 	default:
3949743a77edSAlan Wright 		return (-1);
3950743a77edSAlan Wright 	}
3951743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3952743a77edSAlan Wright 	if (nvlist)
3953743a77edSAlan Wright 		nvlist_free(nvlist);
3954743a77edSAlan Wright 	return (error);
3955743a77edSAlan Wright }
3956743a77edSAlan Wright 
3957743a77edSAlan Wright int
3958743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3959743a77edSAlan Wright     char *path, char *resource)
3960743a77edSAlan Wright {
3961743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3962743a77edSAlan Wright 	    resource, NULL));
3963743a77edSAlan Wright }
3964743a77edSAlan Wright 
3965743a77edSAlan Wright int
3966743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3967743a77edSAlan Wright     char *path, char *resource)
3968743a77edSAlan Wright {
3969743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3970743a77edSAlan Wright 	    resource, NULL));
3971743a77edSAlan Wright }
3972743a77edSAlan Wright 
3973743a77edSAlan Wright int
3974743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3975743a77edSAlan Wright {
3976743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3977743a77edSAlan Wright 	    NULL, NULL));
3978743a77edSAlan Wright }
3979743a77edSAlan Wright 
3980743a77edSAlan Wright int
3981743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3982743a77edSAlan Wright     char *oldname, char *newname)
3983743a77edSAlan Wright {
3984743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3985743a77edSAlan Wright 	    oldname, newname));
3986743a77edSAlan Wright }
398714843421SMatthew Ahrens 
398814843421SMatthew Ahrens int
398914843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
399014843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
399114843421SMatthew Ahrens {
399214843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
399314843421SMatthew Ahrens 	int error;
399414843421SMatthew Ahrens 	zfs_useracct_t buf[100];
399514843421SMatthew Ahrens 
399619b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
399714843421SMatthew Ahrens 
399814843421SMatthew Ahrens 	zc.zc_objset_type = type;
399914843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
400014843421SMatthew Ahrens 
400114843421SMatthew Ahrens 	/* CONSTCOND */
400214843421SMatthew Ahrens 	while (1) {
400314843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
400414843421SMatthew Ahrens 
400514843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
400614843421SMatthew Ahrens 		error = ioctl(zhp->zfs_hdl->libzfs_fd,
400714843421SMatthew Ahrens 		    ZFS_IOC_USERSPACE_MANY, &zc);
400814843421SMatthew Ahrens 		if (error || zc.zc_nvlist_dst_size == 0)
400914843421SMatthew Ahrens 			break;
401014843421SMatthew Ahrens 
401114843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
40120aea4b19SMatthew Ahrens 			error = func(arg, zua->zu_domain, zua->zu_rid,
40130aea4b19SMatthew Ahrens 			    zua->zu_space);
40140aea4b19SMatthew Ahrens 			if (error != 0)
40150aea4b19SMatthew Ahrens 				return (error);
401614843421SMatthew Ahrens 			zua++;
401714843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
401814843421SMatthew Ahrens 		}
401914843421SMatthew Ahrens 	}
402014843421SMatthew Ahrens 
402114843421SMatthew Ahrens 	return (error);
402214843421SMatthew Ahrens }
4023842727c2SChris Kirby 
4024842727c2SChris Kirby int
4025842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4026c99e4bdcSChris Kirby     boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4027a7f53a56SChris Kirby     int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4028842727c2SChris Kirby {
4029842727c2SChris Kirby 	zfs_cmd_t zc = { 0 };
4030842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4031842727c2SChris Kirby 
4032a7f53a56SChris Kirby 	ASSERT(!recursive || dsobj == 0);
4033a7f53a56SChris Kirby 
4034842727c2SChris Kirby 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4035842727c2SChris Kirby 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4036ca45db41SChris Kirby 	if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4037ca45db41SChris Kirby 	    >= sizeof (zc.zc_string))
4038ca45db41SChris Kirby 		return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4039842727c2SChris Kirby 	zc.zc_cookie = recursive;
4040ca45db41SChris Kirby 	zc.zc_temphold = temphold;
4041c99e4bdcSChris Kirby 	zc.zc_cleanup_fd = cleanup_fd;
4042a7f53a56SChris Kirby 	zc.zc_sendobj = dsobj;
4043a7f53a56SChris Kirby 	zc.zc_createtxg = createtxg;
4044842727c2SChris Kirby 
4045842727c2SChris Kirby 	if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4046842727c2SChris Kirby 		char errbuf[ZFS_MAXNAMELEN+32];
4047842727c2SChris Kirby 
4048842727c2SChris Kirby 		/*
4049842727c2SChris Kirby 		 * if it was recursive, the one that actually failed will be in
4050842727c2SChris Kirby 		 * zc.zc_name.
4051842727c2SChris Kirby 		 */
4052842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4053842727c2SChris Kirby 		    "cannot hold '%s@%s'"), zc.zc_name, snapname);
4054842727c2SChris Kirby 		switch (errno) {
405515508ac0SChris Kirby 		case E2BIG:
405615508ac0SChris Kirby 			/*
405715508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
405815508ac0SChris Kirby 			 * prepended. So even if we passed the length check
405915508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
406015508ac0SChris Kirby 			 * up being slightly too long.
406115508ac0SChris Kirby 			 */
406215508ac0SChris Kirby 			return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4063842727c2SChris Kirby 		case ENOTSUP:
4064842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4065842727c2SChris Kirby 			    "pool must be upgraded"));
4066842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4067842727c2SChris Kirby 		case EINVAL:
4068842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4069842727c2SChris Kirby 		case EEXIST:
4070842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4071818119b8SChris Kirby 		case ENOENT:
4072818119b8SChris Kirby 			if (enoent_ok)
4073a7f53a56SChris Kirby 				return (ENOENT);
4074818119b8SChris Kirby 			/* FALLTHROUGH */
4075842727c2SChris Kirby 		default:
4076842727c2SChris Kirby 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4077842727c2SChris Kirby 		}
4078842727c2SChris Kirby 	}
4079842727c2SChris Kirby 
4080842727c2SChris Kirby 	return (0);
4081842727c2SChris Kirby }
4082842727c2SChris Kirby 
4083842727c2SChris Kirby int
4084842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4085842727c2SChris Kirby     boolean_t recursive)
4086842727c2SChris Kirby {
4087842727c2SChris Kirby 	zfs_cmd_t zc = { 0 };
4088842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4089842727c2SChris Kirby 
4090842727c2SChris Kirby 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4091842727c2SChris Kirby 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4092ca45db41SChris Kirby 	if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4093ca45db41SChris Kirby 	    >= sizeof (zc.zc_string))
4094ca45db41SChris Kirby 		return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4095842727c2SChris Kirby 	zc.zc_cookie = recursive;
4096842727c2SChris Kirby 
4097842727c2SChris Kirby 	if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4098842727c2SChris Kirby 		char errbuf[ZFS_MAXNAMELEN+32];
4099842727c2SChris Kirby 
4100842727c2SChris Kirby 		/*
4101842727c2SChris Kirby 		 * if it was recursive, the one that actually failed will be in
4102842727c2SChris Kirby 		 * zc.zc_name.
4103842727c2SChris Kirby 		 */
4104842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4105620252bcSChris Kirby 		    "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4106620252bcSChris Kirby 		    snapname);
4107842727c2SChris Kirby 		switch (errno) {
4108842727c2SChris Kirby 		case ESRCH:
4109842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4110842727c2SChris Kirby 		case ENOTSUP:
4111842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4112842727c2SChris Kirby 			    "pool must be upgraded"));
4113842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4114842727c2SChris Kirby 		case EINVAL:
4115842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4116842727c2SChris Kirby 		default:
4117842727c2SChris Kirby 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4118842727c2SChris Kirby 		}
4119842727c2SChris Kirby 	}
4120842727c2SChris Kirby 
4121842727c2SChris Kirby 	return (0);
4122842727c2SChris Kirby }
4123ca45db41SChris Kirby 
41241af68beaSAlexander Stetsenko int
41251af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
41261af68beaSAlexander Stetsenko {
41271af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
41281af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
41291af68beaSAlexander Stetsenko 	int nvsz = 2048;
41301af68beaSAlexander Stetsenko 	void *nvbuf;
41311af68beaSAlexander Stetsenko 	int err = 0;
41321af68beaSAlexander Stetsenko 	char errbuf[ZFS_MAXNAMELEN+32];
41331af68beaSAlexander Stetsenko 
41341af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
41351af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
41361af68beaSAlexander Stetsenko 
41371af68beaSAlexander Stetsenko tryagain:
41381af68beaSAlexander Stetsenko 
41391af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
41401af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
41411af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
41421af68beaSAlexander Stetsenko 		goto out;
41431af68beaSAlexander Stetsenko 	}
41441af68beaSAlexander Stetsenko 
41451af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
41461af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
41471af68beaSAlexander Stetsenko 
41481af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
41491af68beaSAlexander Stetsenko 
4150d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
41511af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
41521af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
41531af68beaSAlexander Stetsenko 		    zc.zc_name);
41541af68beaSAlexander Stetsenko 		switch (errno) {
41551af68beaSAlexander Stetsenko 		case ENOMEM:
41561af68beaSAlexander Stetsenko 			free(nvbuf);
41571af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
41581af68beaSAlexander Stetsenko 			goto tryagain;
41591af68beaSAlexander Stetsenko 
41601af68beaSAlexander Stetsenko 		case ENOTSUP:
41611af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41621af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
41631af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
41641af68beaSAlexander Stetsenko 			break;
41651af68beaSAlexander Stetsenko 		case EINVAL:
41661af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
41671af68beaSAlexander Stetsenko 			break;
41681af68beaSAlexander Stetsenko 		case ENOENT:
41691af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
41701af68beaSAlexander Stetsenko 			break;
41711af68beaSAlexander Stetsenko 		default:
41721af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
41731af68beaSAlexander Stetsenko 			break;
41741af68beaSAlexander Stetsenko 		}
41751af68beaSAlexander Stetsenko 	} else {
41761af68beaSAlexander Stetsenko 		/* success */
41771af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
41781af68beaSAlexander Stetsenko 		if (rc) {
41791af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
41801af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
41811af68beaSAlexander Stetsenko 			    zc.zc_name);
41821af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
41831af68beaSAlexander Stetsenko 		}
41841af68beaSAlexander Stetsenko 	}
41851af68beaSAlexander Stetsenko 
41861af68beaSAlexander Stetsenko 	free(nvbuf);
41871af68beaSAlexander Stetsenko out:
41881af68beaSAlexander Stetsenko 	return (err);
41891af68beaSAlexander Stetsenko }
41901af68beaSAlexander Stetsenko 
41911af68beaSAlexander Stetsenko int
41921af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
41931af68beaSAlexander Stetsenko {
41941af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
41951af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
41961af68beaSAlexander Stetsenko 	char *nvbuf;
41971af68beaSAlexander Stetsenko 	char errbuf[ZFS_MAXNAMELEN+32];
41981af68beaSAlexander Stetsenko 	size_t nvsz;
41991af68beaSAlexander Stetsenko 	int err;
42001af68beaSAlexander Stetsenko 
42011af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
42021af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
42031af68beaSAlexander Stetsenko 
42041af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
42051af68beaSAlexander Stetsenko 	assert(err == 0);
42061af68beaSAlexander Stetsenko 
42071af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
42081af68beaSAlexander Stetsenko 
42091af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
42101af68beaSAlexander Stetsenko 	assert(err == 0);
42111af68beaSAlexander Stetsenko 
42121af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
42131af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
42141af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
42151af68beaSAlexander Stetsenko 
42161af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
42171af68beaSAlexander Stetsenko 
42181af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
42191af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
42201af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
42211af68beaSAlexander Stetsenko 		    zc.zc_name);
42221af68beaSAlexander Stetsenko 		switch (errno) {
42231af68beaSAlexander Stetsenko 		case ENOTSUP:
42241af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42251af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
42261af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
42271af68beaSAlexander Stetsenko 			break;
42281af68beaSAlexander Stetsenko 		case EINVAL:
42291af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
42301af68beaSAlexander Stetsenko 			break;
42311af68beaSAlexander Stetsenko 		case ENOENT:
42321af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
42331af68beaSAlexander Stetsenko 			break;
42341af68beaSAlexander Stetsenko 		default:
42351af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
42361af68beaSAlexander Stetsenko 			break;
42371af68beaSAlexander Stetsenko 		}
42381af68beaSAlexander Stetsenko 	}
42391af68beaSAlexander Stetsenko 
42401af68beaSAlexander Stetsenko 	free(nvbuf);
42411af68beaSAlexander Stetsenko 
42421af68beaSAlexander Stetsenko 	return (err);
42431af68beaSAlexander Stetsenko }
42441af68beaSAlexander Stetsenko 
42451af68beaSAlexander Stetsenko int
42461af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
42471af68beaSAlexander Stetsenko {
42481af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
42491af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
42501af68beaSAlexander Stetsenko 	int nvsz = 2048;
42511af68beaSAlexander Stetsenko 	void *nvbuf;
42521af68beaSAlexander Stetsenko 	int err = 0;
42531af68beaSAlexander Stetsenko 	char errbuf[ZFS_MAXNAMELEN+32];
42541af68beaSAlexander Stetsenko 
42551af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
42561af68beaSAlexander Stetsenko 
42571af68beaSAlexander Stetsenko tryagain:
42581af68beaSAlexander Stetsenko 
42591af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
42601af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
42611af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
42621af68beaSAlexander Stetsenko 		goto out;
42631af68beaSAlexander Stetsenko 	}
42641af68beaSAlexander Stetsenko 
42651af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
42661af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
42671af68beaSAlexander Stetsenko 
42681af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
42691af68beaSAlexander Stetsenko 
42701af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
42711af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
42721af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
42731af68beaSAlexander Stetsenko 		    zc.zc_name);
42741af68beaSAlexander Stetsenko 		switch (errno) {
42751af68beaSAlexander Stetsenko 		case ENOMEM:
42761af68beaSAlexander Stetsenko 			free(nvbuf);
42771af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
42781af68beaSAlexander Stetsenko 			goto tryagain;
42791af68beaSAlexander Stetsenko 
42801af68beaSAlexander Stetsenko 		case ENOTSUP:
42811af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42821af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
42831af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
42841af68beaSAlexander Stetsenko 			break;
42851af68beaSAlexander Stetsenko 		case EINVAL:
42861af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
42871af68beaSAlexander Stetsenko 			break;
42881af68beaSAlexander Stetsenko 		case ENOENT:
42891af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
42901af68beaSAlexander Stetsenko 			break;
42911af68beaSAlexander Stetsenko 		default:
42921af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
42931af68beaSAlexander Stetsenko 			break;
42941af68beaSAlexander Stetsenko 		}
42951af68beaSAlexander Stetsenko 	} else {
42961af68beaSAlexander Stetsenko 		/* success */
42971af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
42981af68beaSAlexander Stetsenko 		if (rc) {
42991af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf),
43001af68beaSAlexander Stetsenko 			    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
43011af68beaSAlexander Stetsenko 			    zc.zc_name);
43021af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
43031af68beaSAlexander Stetsenko 		}
43041af68beaSAlexander Stetsenko 	}
43051af68beaSAlexander Stetsenko 
43061af68beaSAlexander Stetsenko 	free(nvbuf);
43071af68beaSAlexander Stetsenko out:
43081af68beaSAlexander Stetsenko 	return (err);
43091af68beaSAlexander Stetsenko }
43101af68beaSAlexander Stetsenko 
4311c1449561SEric Taylor uint64_t
4312c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4313c1449561SEric Taylor {
4314c1449561SEric Taylor 	uint64_t numdb;
4315c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4316c1449561SEric Taylor 	int ncopies;
4317c1449561SEric Taylor 	char *strval;
4318c1449561SEric Taylor 
4319c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4320c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4321c1449561SEric Taylor 		ncopies = atoi(strval);
4322c1449561SEric Taylor 	else
4323c1449561SEric Taylor 		ncopies = 1;
4324c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4325c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4326c1449561SEric Taylor 	    &volblocksize) != 0)
4327c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4328c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4329c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4330c1449561SEric Taylor 	numdb = 7;
4331c1449561SEric Taylor 	/* calculate number of indirects */
4332c1449561SEric Taylor 	while (nblocks > 1) {
4333c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4334c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4335c1449561SEric Taylor 		numdb += nblocks;
4336c1449561SEric Taylor 	}
4337c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4338c1449561SEric Taylor 	volsize *= ncopies;
4339c1449561SEric Taylor 	/*
4340c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4341c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4342c1449561SEric Taylor 	 * 1100 bytes
4343c1449561SEric Taylor 	 */
4344c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4345c1449561SEric Taylor 	volsize += numdb;
4346c1449561SEric Taylor 	return (volsize);
4347c1449561SEric Taylor }
4348