xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision bb6e70758d0c30c09f148026d6e686e21cfc8d18)
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.
24*bb6e7075SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
25f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
2670f56fa6SYuri Pankov  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
27013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
28a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <ctype.h>
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <libintl.h>
34fa9e4066Sahrens #include <math.h>
35fa9e4066Sahrens #include <stdio.h>
36fa9e4066Sahrens #include <stdlib.h>
37fa9e4066Sahrens #include <strings.h>
38fa9e4066Sahrens #include <unistd.h>
393cb34c60Sahrens #include <stddef.h>
40fa9e4066Sahrens #include <zone.h>
4199653d4eSeschrock #include <fcntl.h>
42fa9e4066Sahrens #include <sys/mntent.h>
43b12a1c38Slling #include <sys/mount.h>
44ecd6cf80Smarks #include <priv.h>
45ecd6cf80Smarks #include <pwd.h>
46ecd6cf80Smarks #include <grp.h>
47ecd6cf80Smarks #include <stddef.h>
48ecd6cf80Smarks #include <ucred.h>
4914843421SMatthew Ahrens #include <idmap.h>
5014843421SMatthew Ahrens #include <aclutils.h>
513b12c289SMatthew Ahrens #include <directory.h>
52fa9e4066Sahrens 
53c1449561SEric Taylor #include <sys/dnode.h>
54fa9e4066Sahrens #include <sys/spa.h>
55e9dbad6fSeschrock #include <sys/zap.h>
56fa9e4066Sahrens #include <libzfs.h>
57fa9e4066Sahrens 
58fa9e4066Sahrens #include "zfs_namecheck.h"
59fa9e4066Sahrens #include "zfs_prop.h"
60fa9e4066Sahrens #include "libzfs_impl.h"
61ecd6cf80Smarks #include "zfs_deleg.h"
62fa9e4066Sahrens 
6314843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6414843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
65cdf5b4caSmmusante 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
68fa9e4066Sahrens  * readable form.
69fa9e4066Sahrens  */
70fa9e4066Sahrens const char *
71fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
72fa9e4066Sahrens {
73fa9e4066Sahrens 	switch (type) {
74fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
75fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
76fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
77fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
78fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
79fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
80fa9e4066Sahrens 	}
81fa9e4066Sahrens 
82fa9e4066Sahrens 	return (NULL);
83fa9e4066Sahrens }
84fa9e4066Sahrens 
85fa9e4066Sahrens /*
86fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
87fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
88fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
89fa9e4066Sahrens  * acceptable types.
90fa9e4066Sahrens  */
91fa9e4066Sahrens static const char *
92fa9e4066Sahrens path_to_str(const char *path, int types)
93fa9e4066Sahrens {
94fa9e4066Sahrens 	/*
95fa9e4066Sahrens 	 * When given a single type, always report the exact type.
96fa9e4066Sahrens 	 */
97fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
98fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
99fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
100fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
101fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
102fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
103fa9e4066Sahrens 
104fa9e4066Sahrens 	/*
105fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
106fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
107fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
108fa9e4066Sahrens 	 * snapshot attribute and try again.
109fa9e4066Sahrens 	 */
110fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
111fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
112fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
113fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
114fa9e4066Sahrens 	}
115fa9e4066Sahrens 
116fa9e4066Sahrens 	/*
117fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
118fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
119fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
120fa9e4066Sahrens 	 */
121fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
122fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
123fa9e4066Sahrens 
124fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
125fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
126fa9e4066Sahrens }
127fa9e4066Sahrens 
128fa9e4066Sahrens /*
129fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
13014843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
13114843421SMatthew Ahrens  * explain exactly why the name was not valid.
132fa9e4066Sahrens  */
13399d5e173STim Haley int
134f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
135f18faf3fSek110237     boolean_t modifying)
136fa9e4066Sahrens {
137fa9e4066Sahrens 	namecheck_err_t why;
138fa9e4066Sahrens 	char what;
139fa9e4066Sahrens 
1401af68beaSAlexander Stetsenko 	(void) zfs_prop_get_table();
141fa9e4066Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
14299653d4eSeschrock 		if (hdl != NULL) {
143fa9e4066Sahrens 			switch (why) {
144b81d61a6Slling 			case NAME_ERR_TOOLONG:
14599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14699653d4eSeschrock 				    "name is too long"));
147b81d61a6Slling 				break;
148b81d61a6Slling 
149fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
15099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15199653d4eSeschrock 				    "leading slash in name"));
152fa9e4066Sahrens 				break;
153fa9e4066Sahrens 
154fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15699653d4eSeschrock 				    "empty component in name"));
157fa9e4066Sahrens 				break;
158fa9e4066Sahrens 
159fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
16099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16199653d4eSeschrock 				    "trailing slash in name"));
162fa9e4066Sahrens 				break;
163fa9e4066Sahrens 
164fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16599653d4eSeschrock 				zfs_error_aux(hdl,
166fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16799653d4eSeschrock 				    "'%c' in name"), what);
168fa9e4066Sahrens 				break;
169fa9e4066Sahrens 
170fa9e4066Sahrens 			case NAME_ERR_MULTIPLE_AT:
17199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17299653d4eSeschrock 				    "multiple '@' delimiters in name"));
173fa9e4066Sahrens 				break;
1745ad82045Snd150628 
1755ad82045Snd150628 			case NAME_ERR_NOLETTER:
1765ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1775ad82045Snd150628 				    "pool doesn't begin with a letter"));
1785ad82045Snd150628 				break;
1795ad82045Snd150628 
1805ad82045Snd150628 			case NAME_ERR_RESERVED:
1815ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825ad82045Snd150628 				    "name is reserved"));
1835ad82045Snd150628 				break;
1845ad82045Snd150628 
1855ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1865ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1875ad82045Snd150628 				    "reserved disk name"));
1885ad82045Snd150628 				break;
189fa9e4066Sahrens 			}
190fa9e4066Sahrens 		}
191fa9e4066Sahrens 
192fa9e4066Sahrens 		return (0);
193fa9e4066Sahrens 	}
194fa9e4066Sahrens 
195fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19699653d4eSeschrock 		if (hdl != NULL)
19799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19899653d4eSeschrock 			    "snapshot delimiter '@' in filesystem name"));
199fa9e4066Sahrens 		return (0);
200fa9e4066Sahrens 	}
201fa9e4066Sahrens 
2021d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2031d452cf5Sahrens 		if (hdl != NULL)
2041d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2061d452cf5Sahrens 		return (0);
2071d452cf5Sahrens 	}
2081d452cf5Sahrens 
209f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
210f18faf3fSek110237 		if (hdl != NULL)
211f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
212f18faf3fSek110237 			    "invalid character %c in name"), '%');
213f18faf3fSek110237 		return (0);
214f18faf3fSek110237 	}
215f18faf3fSek110237 
21699653d4eSeschrock 	return (-1);
217fa9e4066Sahrens }
218fa9e4066Sahrens 
219fa9e4066Sahrens int
220fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
221fa9e4066Sahrens {
222e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
223e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
224f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
225fa9e4066Sahrens }
226fa9e4066Sahrens 
227fa9e4066Sahrens /*
228e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
229e9dbad6fSeschrock  * properties into a separate nvlist.
230e9dbad6fSeschrock  */
231fac3008cSeschrock static nvlist_t *
232fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
233e9dbad6fSeschrock {
234e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
235e9dbad6fSeschrock 	nvpair_t *elem;
236e9dbad6fSeschrock 	nvlist_t *propval;
237fac3008cSeschrock 	nvlist_t *nvl;
238e9dbad6fSeschrock 
239fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
240fac3008cSeschrock 		(void) no_memory(hdl);
241fac3008cSeschrock 		return (NULL);
242fac3008cSeschrock 	}
243e9dbad6fSeschrock 
244e9dbad6fSeschrock 	elem = NULL;
245fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
246e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
247e9dbad6fSeschrock 			continue;
248e9dbad6fSeschrock 
249e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
250fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
251fac3008cSeschrock 			nvlist_free(nvl);
252fac3008cSeschrock 			(void) no_memory(hdl);
253fac3008cSeschrock 			return (NULL);
254fac3008cSeschrock 		}
255e9dbad6fSeschrock 	}
256e9dbad6fSeschrock 
257fac3008cSeschrock 	return (nvl);
258e9dbad6fSeschrock }
259e9dbad6fSeschrock 
26029ab75c9Srm160521 static zpool_handle_t *
26129ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
26229ab75c9Srm160521 {
26329ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26429ab75c9Srm160521 	zpool_handle_t *zph;
26529ab75c9Srm160521 
26629ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
26729ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
26829ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
26929ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
27029ab75c9Srm160521 	}
27129ab75c9Srm160521 	return (zph);
27229ab75c9Srm160521 }
27329ab75c9Srm160521 
27429ab75c9Srm160521 static zpool_handle_t *
27529ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
27629ab75c9Srm160521 {
27729ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
27829ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
27929ab75c9Srm160521 
28029ab75c9Srm160521 	while ((zph != NULL) &&
28129ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
28229ab75c9Srm160521 		zph = zph->zpool_next;
28329ab75c9Srm160521 	return (zph);
28429ab75c9Srm160521 }
28529ab75c9Srm160521 
28629ab75c9Srm160521 /*
28729ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
28829ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
28929ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
29029ab75c9Srm160521  */
29129ab75c9Srm160521 static zpool_handle_t *
29229ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
29329ab75c9Srm160521 {
29429ab75c9Srm160521 	char *pool_name;
29529ab75c9Srm160521 	int len;
29629ab75c9Srm160521 	zpool_handle_t *zph;
29729ab75c9Srm160521 
29829ab75c9Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
29929ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
30029ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
30129ab75c9Srm160521 
30229ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
30329ab75c9Srm160521 	if (zph == NULL)
30429ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
30529ab75c9Srm160521 
30629ab75c9Srm160521 	free(pool_name);
30729ab75c9Srm160521 	return (zph);
30829ab75c9Srm160521 }
30929ab75c9Srm160521 
31029ab75c9Srm160521 void
31129ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
31229ab75c9Srm160521 {
31329ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
31429ab75c9Srm160521 
31529ab75c9Srm160521 	while (zph != NULL) {
31629ab75c9Srm160521 		next = zph->zpool_next;
31729ab75c9Srm160521 		zpool_close(zph);
31829ab75c9Srm160521 		zph = next;
31929ab75c9Srm160521 	}
32029ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
32129ab75c9Srm160521 }
32229ab75c9Srm160521 
323e9dbad6fSeschrock /*
324fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
325fa9e4066Sahrens  */
326fa9e4066Sahrens static int
327ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
328fa9e4066Sahrens {
329e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
330fa9e4066Sahrens 
331ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
332fa9e4066Sahrens 
333ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3347f7322feSeschrock 		if (errno == ENOMEM) {
335ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
33699653d4eSeschrock 				return (-1);
337e9dbad6fSeschrock 			}
3387f7322feSeschrock 		} else {
339fa9e4066Sahrens 			return (-1);
3407f7322feSeschrock 		}
3417f7322feSeschrock 	}
342ebedde84SEric Taylor 	return (0);
343fac3008cSeschrock }
344fac3008cSeschrock 
34592241e0bSTom Erickson /*
34692241e0bSTom Erickson  * Utility function to get the received properties of the given object.
34792241e0bSTom Erickson  */
34892241e0bSTom Erickson static int
34992241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
35092241e0bSTom Erickson {
35192241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
35292241e0bSTom Erickson 	nvlist_t *recvdprops;
35392241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
35492241e0bSTom Erickson 	int err;
35592241e0bSTom Erickson 
35692241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
35792241e0bSTom Erickson 		return (-1);
35892241e0bSTom Erickson 
35992241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
36092241e0bSTom Erickson 
36192241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
36292241e0bSTom Erickson 		if (errno == ENOMEM) {
36392241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
36492241e0bSTom Erickson 				return (-1);
36592241e0bSTom Erickson 			}
36692241e0bSTom Erickson 		} else {
36792241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
36892241e0bSTom Erickson 			return (-1);
36992241e0bSTom Erickson 		}
37092241e0bSTom Erickson 	}
37192241e0bSTom Erickson 
37292241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
37392241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
37492241e0bSTom Erickson 	if (err != 0)
37592241e0bSTom Erickson 		return (-1);
37692241e0bSTom Erickson 
37792241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
37892241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
37992241e0bSTom Erickson 
38092241e0bSTom Erickson 	return (0);
38192241e0bSTom Erickson }
38292241e0bSTom Erickson 
383ebedde84SEric Taylor static int
384ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
385ebedde84SEric Taylor {
386ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
387ebedde84SEric Taylor 
388ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
389ebedde84SEric Taylor 
390ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
391ebedde84SEric Taylor 		return (-1);
392ebedde84SEric Taylor 	}
393fac3008cSeschrock 
39414843421SMatthew Ahrens 	/*
39514843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
39614843421SMatthew Ahrens 	 * storing them in zfs_props?
39714843421SMatthew Ahrens 	 */
398fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
399fac3008cSeschrock 		nvlist_free(allprops);
400fac3008cSeschrock 		return (-1);
401fac3008cSeschrock 	}
402fac3008cSeschrock 
40399653d4eSeschrock 	nvlist_free(zhp->zfs_props);
404fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
40599653d4eSeschrock 
406fac3008cSeschrock 	zhp->zfs_props = allprops;
407fac3008cSeschrock 	zhp->zfs_user_props = userprops;
40899653d4eSeschrock 
409fa9e4066Sahrens 	return (0);
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412ebedde84SEric Taylor static int
413ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
414ebedde84SEric Taylor {
415ebedde84SEric Taylor 	int rc = 0;
416ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
417ebedde84SEric Taylor 
418ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
419ebedde84SEric Taylor 		return (-1);
420ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
421ebedde84SEric Taylor 		rc = -1;
422ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
423ebedde84SEric Taylor 		rc = -1;
424ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
425ebedde84SEric Taylor 	return (rc);
426ebedde84SEric Taylor }
427ebedde84SEric Taylor 
428fa9e4066Sahrens /*
429fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
430fa9e4066Sahrens  */
431fa9e4066Sahrens void
432fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
433fa9e4066Sahrens {
434fa9e4066Sahrens 	(void) get_stats(zhp);
435fa9e4066Sahrens }
436fa9e4066Sahrens 
437fa9e4066Sahrens /*
438fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
439fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
440fa9e4066Sahrens  */
441ebedde84SEric Taylor static int
442ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
443fa9e4066Sahrens {
444503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
445ebedde84SEric Taylor 		return (-1);
44631fd60d3Sahrens 
447fa9e4066Sahrens 	/*
448fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
449fa9e4066Sahrens 	 * the high-level type.
450fa9e4066Sahrens 	 */
451a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
452a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
453a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
454a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
455a2eea2e1Sahrens 	else
456a2eea2e1Sahrens 		abort();
457a2eea2e1Sahrens 
458fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
459fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
460fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
461fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
462fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
463fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
464fa9e4066Sahrens 	else
46599653d4eSeschrock 		abort();	/* we should never see any other types */
466fa9e4066Sahrens 
4679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
470ebedde84SEric Taylor 	return (0);
471ebedde84SEric Taylor }
472ebedde84SEric Taylor 
473ebedde84SEric Taylor zfs_handle_t *
474ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
475ebedde84SEric Taylor {
476ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
477ebedde84SEric Taylor 
478ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
479ebedde84SEric Taylor 
480ebedde84SEric Taylor 	if (zhp == NULL)
481ebedde84SEric Taylor 		return (NULL);
482ebedde84SEric Taylor 
483ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
484ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
485ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
486ebedde84SEric Taylor 		free(zhp);
487ebedde84SEric Taylor 		return (NULL);
488ebedde84SEric Taylor 	}
489ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
490ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
491ebedde84SEric Taylor 		free(zhp);
492ebedde84SEric Taylor 		return (NULL);
493ebedde84SEric Taylor 	}
494ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
495ebedde84SEric Taylor 		free(zhp);
496ebedde84SEric Taylor 		zhp = NULL;
497ebedde84SEric Taylor 	}
498ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
499ebedde84SEric Taylor 	return (zhp);
500ebedde84SEric Taylor }
501ebedde84SEric Taylor 
50219b94df9SMatthew Ahrens zfs_handle_t *
503ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
504ebedde84SEric Taylor {
505ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
506ebedde84SEric Taylor 
507ebedde84SEric Taylor 	if (zhp == NULL)
508ebedde84SEric Taylor 		return (NULL);
509ebedde84SEric Taylor 
510ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
511ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
512ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
513ebedde84SEric Taylor 		free(zhp);
514ebedde84SEric Taylor 		return (NULL);
515ebedde84SEric Taylor 	}
516fa9e4066Sahrens 	return (zhp);
517fa9e4066Sahrens }
518fa9e4066Sahrens 
51919b94df9SMatthew Ahrens zfs_handle_t *
52019b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
52119b94df9SMatthew Ahrens {
52219b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
52319b94df9SMatthew Ahrens 
52419b94df9SMatthew Ahrens 	if (zhp == NULL)
52519b94df9SMatthew Ahrens 		return (NULL);
52619b94df9SMatthew Ahrens 
52719b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
52819b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
52919b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
53019b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
53119b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
53219b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
53319b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
53419b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
53519b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
53619b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
53719b94df9SMatthew Ahrens 			zfs_close(zhp);
53819b94df9SMatthew Ahrens 			return (NULL);
53919b94df9SMatthew Ahrens 		}
54019b94df9SMatthew Ahrens 	}
54119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
54219b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
54319b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
54419b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54519b94df9SMatthew Ahrens 			zfs_close(zhp);
54619b94df9SMatthew Ahrens 			return (NULL);
54719b94df9SMatthew Ahrens 		}
54819b94df9SMatthew Ahrens 	}
54919b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
55019b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
55119b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
55219b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55319b94df9SMatthew Ahrens 			zfs_close(zhp);
55419b94df9SMatthew Ahrens 			return (NULL);
55519b94df9SMatthew Ahrens 		}
55619b94df9SMatthew Ahrens 	}
55719b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
55819b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
55919b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
56019b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
56119b94df9SMatthew Ahrens 	}
56219b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
56319b94df9SMatthew Ahrens 	return (zhp);
56419b94df9SMatthew Ahrens }
56519b94df9SMatthew Ahrens 
566fa9e4066Sahrens /*
567fa9e4066Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
568fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
569fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
570fa9e4066Sahrens  */
571fa9e4066Sahrens zfs_handle_t *
57299653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
573fa9e4066Sahrens {
574fa9e4066Sahrens 	zfs_handle_t *zhp;
57599653d4eSeschrock 	char errbuf[1024];
57699653d4eSeschrock 
57799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
57899653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
579fa9e4066Sahrens 
580fa9e4066Sahrens 	/*
58199653d4eSeschrock 	 * Validate the name before we even try to open it.
582fa9e4066Sahrens 	 */
583f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
58499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
58599653d4eSeschrock 		    "invalid dataset name"));
58699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
587fa9e4066Sahrens 		return (NULL);
588fa9e4066Sahrens 	}
589fa9e4066Sahrens 
590fa9e4066Sahrens 	/*
591fa9e4066Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
592fa9e4066Sahrens 	 */
593fa9e4066Sahrens 	errno = 0;
59499653d4eSeschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
595ece3d9b3Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
596fa9e4066Sahrens 		return (NULL);
597fa9e4066Sahrens 	}
598fa9e4066Sahrens 
599fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
60099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
60194de1d4cSeschrock 		zfs_close(zhp);
602fa9e4066Sahrens 		return (NULL);
603fa9e4066Sahrens 	}
604fa9e4066Sahrens 
605fa9e4066Sahrens 	return (zhp);
606fa9e4066Sahrens }
607fa9e4066Sahrens 
608fa9e4066Sahrens /*
609fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
610fa9e4066Sahrens  */
611fa9e4066Sahrens void
612fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
613fa9e4066Sahrens {
614fa9e4066Sahrens 	if (zhp->zfs_mntopts)
615fa9e4066Sahrens 		free(zhp->zfs_mntopts);
61699653d4eSeschrock 	nvlist_free(zhp->zfs_props);
617e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
61892241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
619fa9e4066Sahrens 	free(zhp);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622ebedde84SEric Taylor typedef struct mnttab_node {
623ebedde84SEric Taylor 	struct mnttab mtn_mt;
624ebedde84SEric Taylor 	avl_node_t mtn_node;
625ebedde84SEric Taylor } mnttab_node_t;
626ebedde84SEric Taylor 
627ebedde84SEric Taylor static int
628ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
629ebedde84SEric Taylor {
630ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
631ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
632ebedde84SEric Taylor 	int rv;
633ebedde84SEric Taylor 
634ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
635ebedde84SEric Taylor 
636ebedde84SEric Taylor 	if (rv == 0)
637ebedde84SEric Taylor 		return (0);
638ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
639ebedde84SEric Taylor }
640ebedde84SEric Taylor 
641ebedde84SEric Taylor void
642ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
643ebedde84SEric Taylor {
644ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
645ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
646ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
647b2634b9cSEric Taylor }
648b2634b9cSEric Taylor 
649b2634b9cSEric Taylor void
650b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
651b2634b9cSEric Taylor {
652b2634b9cSEric Taylor 	struct mnttab entry;
653ebedde84SEric Taylor 
654ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
655ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
656ebedde84SEric Taylor 		mnttab_node_t *mtn;
657ebedde84SEric Taylor 
658ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
659ebedde84SEric Taylor 			continue;
660ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
661ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
662ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
663ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
664ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
665ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
666ebedde84SEric Taylor 	}
667ebedde84SEric Taylor }
668ebedde84SEric Taylor 
669ebedde84SEric Taylor void
670ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
671ebedde84SEric Taylor {
672ebedde84SEric Taylor 	void *cookie = NULL;
673ebedde84SEric Taylor 	mnttab_node_t *mtn;
674ebedde84SEric Taylor 
675ebedde84SEric Taylor 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
676ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
677ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
678ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
679ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
680ebedde84SEric Taylor 		free(mtn);
681ebedde84SEric Taylor 	}
682ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
683ebedde84SEric Taylor }
684ebedde84SEric Taylor 
685b2634b9cSEric Taylor void
686b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
687b2634b9cSEric Taylor {
688b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
689b2634b9cSEric Taylor }
690b2634b9cSEric Taylor 
691ebedde84SEric Taylor int
692ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
693ebedde84SEric Taylor     struct mnttab *entry)
694ebedde84SEric Taylor {
695ebedde84SEric Taylor 	mnttab_node_t find;
696ebedde84SEric Taylor 	mnttab_node_t *mtn;
697ebedde84SEric Taylor 
698b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
699b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
700b2634b9cSEric Taylor 
701b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
702b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
703b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
704b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
705b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
706b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
707b2634b9cSEric Taylor 			return (0);
708b2634b9cSEric Taylor 		else
709b2634b9cSEric Taylor 			return (ENOENT);
710b2634b9cSEric Taylor 	}
711b2634b9cSEric Taylor 
712ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
713b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
714ebedde84SEric Taylor 
715ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
716ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
717ebedde84SEric Taylor 	if (mtn) {
718ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
719ebedde84SEric Taylor 		return (0);
720ebedde84SEric Taylor 	}
721ebedde84SEric Taylor 	return (ENOENT);
722ebedde84SEric Taylor }
723ebedde84SEric Taylor 
724ebedde84SEric Taylor void
725ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
726ebedde84SEric Taylor     const char *mountp, const char *mntopts)
727ebedde84SEric Taylor {
728ebedde84SEric Taylor 	mnttab_node_t *mtn;
729ebedde84SEric Taylor 
730ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
731ebedde84SEric Taylor 		return;
732ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
733ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
734ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
735ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
736ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
737ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
738ebedde84SEric Taylor }
739ebedde84SEric Taylor 
740ebedde84SEric Taylor void
741ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
742ebedde84SEric Taylor {
743ebedde84SEric Taylor 	mnttab_node_t find;
744ebedde84SEric Taylor 	mnttab_node_t *ret;
745ebedde84SEric Taylor 
746ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
747ebedde84SEric Taylor 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
748ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
749ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
750ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
751ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
752ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
753ebedde84SEric Taylor 		free(ret);
754ebedde84SEric Taylor 	}
755ebedde84SEric Taylor }
756ebedde84SEric Taylor 
7577b97dc1aSrm160521 int
7587b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
7597b97dc1aSrm160521 {
76029ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
7617b97dc1aSrm160521 
7627b97dc1aSrm160521 	if (zpool_handle == NULL)
7637b97dc1aSrm160521 		return (-1);
7647b97dc1aSrm160521 
7657b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
7667b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
7677b97dc1aSrm160521 	return (0);
7687b97dc1aSrm160521 }
7697b97dc1aSrm160521 
7707b97dc1aSrm160521 /*
7717b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
7727b97dc1aSrm160521  */
7737b97dc1aSrm160521 static int
7747b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
7757b97dc1aSrm160521 {
7767b97dc1aSrm160521 	int spa_version;
7777b97dc1aSrm160521 
7787b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
7797b97dc1aSrm160521 		return (-1);
7807b97dc1aSrm160521 
7817b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
7827b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
7837b97dc1aSrm160521 	else
7847b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
7857b97dc1aSrm160521 
7867b97dc1aSrm160521 	return (0);
7877b97dc1aSrm160521 }
7887b97dc1aSrm160521 
789b1b8ab34Slling /*
790e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
791e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
792e9dbad6fSeschrock  * strings.
793fa9e4066Sahrens  */
7940a48a24eStimh nvlist_t *
7950a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
796990b4856Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
797fa9e4066Sahrens {
798e9dbad6fSeschrock 	nvpair_t *elem;
799e9dbad6fSeschrock 	uint64_t intval;
800e9dbad6fSeschrock 	char *strval;
801990b4856Slling 	zfs_prop_t prop;
802e9dbad6fSeschrock 	nvlist_t *ret;
803da6c28aaSamw 	int chosen_normal = -1;
804da6c28aaSamw 	int chosen_utf = -1;
805990b4856Slling 
806e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
807e9dbad6fSeschrock 		(void) no_memory(hdl);
808e9dbad6fSeschrock 		return (NULL);
809e9dbad6fSeschrock 	}
810fa9e4066Sahrens 
81114843421SMatthew Ahrens 	/*
81214843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
81314843421SMatthew Ahrens 	 */
81414843421SMatthew Ahrens 
815e9dbad6fSeschrock 	elem = NULL;
816e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
817990b4856Slling 		const char *propname = nvpair_name(elem);
81899653d4eSeschrock 
81914843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
82014843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
821fa9e4066Sahrens 			/*
82214843421SMatthew Ahrens 			 * This is a user property: make sure it's a
823990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
824990b4856Slling 			 */
825990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
826990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
827990b4856Slling 				    "'%s' must be a string"), propname);
828990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
829990b4856Slling 				goto error;
830990b4856Slling 			}
831990b4856Slling 
832990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
833e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
834e9dbad6fSeschrock 				    "property name '%s' is too long"),
835e9dbad6fSeschrock 				    propname);
836990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
837e9dbad6fSeschrock 				goto error;
838e9dbad6fSeschrock 			}
839e9dbad6fSeschrock 
840e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
841e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
842e9dbad6fSeschrock 				(void) no_memory(hdl);
843e9dbad6fSeschrock 				goto error;
844e9dbad6fSeschrock 			}
845e9dbad6fSeschrock 			continue;
846e9dbad6fSeschrock 		}
847fa9e4066Sahrens 
84814843421SMatthew Ahrens 		/*
84914843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
85014843421SMatthew Ahrens 		 * snapshots.
85114843421SMatthew Ahrens 		 */
852bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
853bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
854bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
855bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
856bb0ade09Sahrens 			goto error;
857bb0ade09Sahrens 		}
858bb0ade09Sahrens 
85914843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
86014843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
86114843421SMatthew Ahrens 			char newpropname[128];
86214843421SMatthew Ahrens 			char domain[128];
86314843421SMatthew Ahrens 			uint64_t rid;
86414843421SMatthew Ahrens 			uint64_t valary[3];
86514843421SMatthew Ahrens 
86614843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
86714843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
86814843421SMatthew Ahrens 				zfs_error_aux(hdl,
86914843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
87014843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
87114843421SMatthew Ahrens 				    propname);
87214843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
87314843421SMatthew Ahrens 				goto error;
87414843421SMatthew Ahrens 			}
87514843421SMatthew Ahrens 
87614843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
87714843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
87814843421SMatthew Ahrens 				zfs_error_aux(hdl,
87914843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
88014843421SMatthew Ahrens 				    propname);
88114843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
88214843421SMatthew Ahrens 				    errbuf);
88314843421SMatthew Ahrens 				goto error;
88414843421SMatthew Ahrens 			}
88514843421SMatthew Ahrens 
88614843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
88714843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
88814843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
88914843421SMatthew Ahrens 					intval = 0;
89014843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
89114843421SMatthew Ahrens 				    strval, &intval) != 0) {
89214843421SMatthew Ahrens 					(void) zfs_error(hdl,
89314843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
89414843421SMatthew Ahrens 					goto error;
89514843421SMatthew Ahrens 				}
89614843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
89714843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
89814843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
89914843421SMatthew Ahrens 				if (intval == 0) {
90014843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90114843421SMatthew Ahrens 					    "use 'none' to disable "
90214843421SMatthew Ahrens 					    "userquota/groupquota"));
90314843421SMatthew Ahrens 					goto error;
90414843421SMatthew Ahrens 				}
90514843421SMatthew Ahrens 			} else {
90614843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90714843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
90814843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
90914843421SMatthew Ahrens 				goto error;
91014843421SMatthew Ahrens 			}
91114843421SMatthew Ahrens 
9122d5843dbSMatthew Ahrens 			/*
9132d5843dbSMatthew Ahrens 			 * Encode the prop name as
9142d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
9152d5843dbSMatthew Ahrens 			 * for the kernel to decode.
9162d5843dbSMatthew Ahrens 			 */
91714843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
9182d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
9192d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
92014843421SMatthew Ahrens 			valary[0] = uqtype;
92114843421SMatthew Ahrens 			valary[1] = rid;
92214843421SMatthew Ahrens 			valary[2] = intval;
92314843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
92414843421SMatthew Ahrens 			    valary, 3) != 0) {
92514843421SMatthew Ahrens 				(void) no_memory(hdl);
92614843421SMatthew Ahrens 				goto error;
92714843421SMatthew Ahrens 			}
92814843421SMatthew Ahrens 			continue;
92919b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
93019b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93119b94df9SMatthew Ahrens 			    "'%s' is readonly"),
93219b94df9SMatthew Ahrens 			    propname);
93319b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
93419b94df9SMatthew Ahrens 			goto error;
93514843421SMatthew Ahrens 		}
93614843421SMatthew Ahrens 
93714843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
93814843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93914843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
94014843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
94114843421SMatthew Ahrens 			goto error;
94214843421SMatthew Ahrens 		}
94314843421SMatthew Ahrens 
944e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
945e9dbad6fSeschrock 			zfs_error_aux(hdl,
946e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
947e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
948e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
949e9dbad6fSeschrock 			goto error;
950e9dbad6fSeschrock 		}
951e9dbad6fSeschrock 
952e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
953da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
954e9dbad6fSeschrock 			zfs_error_aux(hdl,
955e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
956e9dbad6fSeschrock 			    propname);
957e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
958e9dbad6fSeschrock 			goto error;
959e9dbad6fSeschrock 		}
960e9dbad6fSeschrock 
961990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
962990b4856Slling 		    &strval, &intval, errbuf) != 0)
963e9dbad6fSeschrock 			goto error;
964e9dbad6fSeschrock 
965e9dbad6fSeschrock 		/*
966e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
967e9dbad6fSeschrock 		 */
968e9dbad6fSeschrock 		switch (prop) {
969e7437265Sahrens 		case ZFS_PROP_VERSION:
970e7437265Sahrens 		{
971e7437265Sahrens 			int version;
972e7437265Sahrens 
973e7437265Sahrens 			if (zhp == NULL)
974e7437265Sahrens 				break;
975e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
976e7437265Sahrens 			if (intval < version) {
977e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
978e7437265Sahrens 				    "Can not downgrade; already at version %u"),
979e7437265Sahrens 				    version);
980e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
981e7437265Sahrens 				goto error;
982e7437265Sahrens 			}
983e7437265Sahrens 			break;
984e7437265Sahrens 		}
985e7437265Sahrens 
986e9dbad6fSeschrock 		case ZFS_PROP_RECORDSIZE:
987e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
988e9dbad6fSeschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
989e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
990e9dbad6fSeschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
991e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
992e9dbad6fSeschrock 				    "'%s' must be power of 2 from %u "
993e9dbad6fSeschrock 				    "to %uk"), propname,
994e9dbad6fSeschrock 				    (uint_t)SPA_MINBLOCKSIZE,
995e9dbad6fSeschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
996e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
997e9dbad6fSeschrock 				goto error;
998e9dbad6fSeschrock 			}
999e9dbad6fSeschrock 			break;
1000e9dbad6fSeschrock 
10014201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
10024201a95eSRic Aleshire 		{
10034201a95eSRic Aleshire 			/*
10044201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
10054201a95eSRic Aleshire 			 * internal hex label string.
10064201a95eSRic Aleshire 			 */
10074201a95eSRic Aleshire 
10084201a95eSRic Aleshire 			m_label_t *new_sl;
10094201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
10104201a95eSRic Aleshire 
10114201a95eSRic Aleshire 			/* Default value is already OK. */
10124201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
10134201a95eSRic Aleshire 				break;
10144201a95eSRic Aleshire 
10154201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
10164201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
10174201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
10184201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
10194201a95eSRic Aleshire 				goto badlabel;
10204201a95eSRic Aleshire 			}
10214201a95eSRic Aleshire 
10224201a95eSRic Aleshire 			/* Now translate to hex internal label string */
10234201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
10244201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
10254201a95eSRic Aleshire 				if (hex)
10264201a95eSRic Aleshire 					free(hex);
10274201a95eSRic Aleshire 				goto badlabel;
10284201a95eSRic Aleshire 			}
10294201a95eSRic Aleshire 			m_label_free(new_sl);
10304201a95eSRic Aleshire 
10314201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
10324201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
10334201a95eSRic Aleshire 				free(hex);
10344201a95eSRic Aleshire 				break;
10354201a95eSRic Aleshire 			}
10364201a95eSRic Aleshire 
10374201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1038569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
10394201a95eSRic Aleshire 			    DATA_TYPE_STRING);
10404201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
10414201a95eSRic Aleshire 			    hex) == 0);
10424201a95eSRic Aleshire 			free(hex);
10434201a95eSRic Aleshire 
10444201a95eSRic Aleshire 			break;
10454201a95eSRic Aleshire 
10464201a95eSRic Aleshire badlabel:
10474201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10484201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
10494201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
10504201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
10514201a95eSRic Aleshire 			goto error;
10524201a95eSRic Aleshire 
10534201a95eSRic Aleshire 		}
10544201a95eSRic Aleshire 
1055e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
105689eef05eSrm160521 		{
105789eef05eSrm160521 			namecheck_err_t why;
105889eef05eSrm160521 
1059e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1060e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1061e9dbad6fSeschrock 				break;
1062e9dbad6fSeschrock 
106389eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
106489eef05eSrm160521 				switch (why) {
106589eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
106689eef05eSrm160521 					zfs_error_aux(hdl,
106789eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1068e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1069e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
107089eef05eSrm160521 					break;
107189eef05eSrm160521 				case NAME_ERR_TOOLONG:
107289eef05eSrm160521 					zfs_error_aux(hdl,
107389eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
107489eef05eSrm160521 					    "component of '%s' is too long"),
107589eef05eSrm160521 					    propname);
107689eef05eSrm160521 					break;
107789eef05eSrm160521 				}
1078e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1079e9dbad6fSeschrock 				goto error;
1080e9dbad6fSeschrock 			}
108189eef05eSrm160521 		}
108289eef05eSrm160521 
1083f3861e1aSahl 			/*FALLTHRU*/
1084e9dbad6fSeschrock 
1085da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1086f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1087e9dbad6fSeschrock 			/*
1088da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1089da6c28aaSamw 			 * properties, check if it can be set in a
1090da6c28aaSamw 			 * global/non-global zone based on
1091f3861e1aSahl 			 * the zoned property value:
1092fa9e4066Sahrens 			 *
1093fa9e4066Sahrens 			 *		global zone	    non-global zone
1094f3861e1aSahl 			 * --------------------------------------------------
1095fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1096fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1097da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1098fa9e4066Sahrens 			 *
1099fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1100fa9e4066Sahrens 			 *		sharenfs (yes)
1101da6c28aaSamw 			 *		sharesmb (yes)
1102fa9e4066Sahrens 			 */
1103e9dbad6fSeschrock 			if (zoned) {
1104fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
110599653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1106e9dbad6fSeschrock 					    "'%s' cannot be set on "
1107e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1108e9dbad6fSeschrock 					    propname);
1109e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1110e9dbad6fSeschrock 					    errbuf);
1111e9dbad6fSeschrock 					goto error;
1112da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1113da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
111499653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115e9dbad6fSeschrock 					    "'%s' cannot be set in "
1116e9dbad6fSeschrock 					    "a non-global zone"), propname);
1117e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1118e9dbad6fSeschrock 					    errbuf);
1119e9dbad6fSeschrock 					goto error;
1120fa9e4066Sahrens 				}
1121fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1122fa9e4066Sahrens 				/*
1123fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
112414843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1125fa9e4066Sahrens 				 */
112699653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1128e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1129e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1130e9dbad6fSeschrock 				goto error;
1131fa9e4066Sahrens 			}
1132f3861e1aSahl 
113367331909Sdougm 			/*
113467331909Sdougm 			 * At this point, it is legitimate to set the
113567331909Sdougm 			 * property. Now we want to make sure that the
113667331909Sdougm 			 * property value is valid if it is sharenfs.
113767331909Sdougm 			 */
1138da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1139da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
114067331909Sdougm 			    strcmp(strval, "on") != 0 &&
114167331909Sdougm 			    strcmp(strval, "off") != 0) {
1142da6c28aaSamw 				zfs_share_proto_t proto;
1143da6c28aaSamw 
1144da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1145da6c28aaSamw 					proto = PROTO_SMB;
1146da6c28aaSamw 				else
1147da6c28aaSamw 					proto = PROTO_NFS;
114867331909Sdougm 
114967331909Sdougm 				/*
1150da6c28aaSamw 				 * Must be an valid sharing protocol
1151da6c28aaSamw 				 * option string so init the libshare
1152da6c28aaSamw 				 * in order to enable the parser and
1153da6c28aaSamw 				 * then parse the options. We use the
1154da6c28aaSamw 				 * control API since we don't care about
1155da6c28aaSamw 				 * the current configuration and don't
115667331909Sdougm 				 * want the overhead of loading it
115767331909Sdougm 				 * until we actually do something.
115867331909Sdougm 				 */
115967331909Sdougm 
116067331909Sdougm 				if (zfs_init_libshare(hdl,
116167331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1162fac3008cSeschrock 					/*
1163fac3008cSeschrock 					 * An error occurred so we can't do
1164fac3008cSeschrock 					 * anything
1165fac3008cSeschrock 					 */
116667331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116767331909Sdougm 					    "'%s' cannot be set: problem "
116867331909Sdougm 					    "in share initialization"),
116967331909Sdougm 					    propname);
1170fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1171fac3008cSeschrock 					    errbuf);
117267331909Sdougm 					goto error;
117367331909Sdougm 				}
117467331909Sdougm 
1175da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
117667331909Sdougm 					/*
117767331909Sdougm 					 * There was an error in parsing so
117867331909Sdougm 					 * deal with it by issuing an error
117967331909Sdougm 					 * message and leaving after
118067331909Sdougm 					 * uninitializing the the libshare
118167331909Sdougm 					 * interface.
118267331909Sdougm 					 */
118367331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118467331909Sdougm 					    "'%s' cannot be set to invalid "
118567331909Sdougm 					    "options"), propname);
1186fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1187fac3008cSeschrock 					    errbuf);
118867331909Sdougm 					zfs_uninit_libshare(hdl);
118967331909Sdougm 					goto error;
119067331909Sdougm 				}
119167331909Sdougm 				zfs_uninit_libshare(hdl);
119267331909Sdougm 			}
119367331909Sdougm 
1194f3861e1aSahl 			break;
1195da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1196da6c28aaSamw 			chosen_utf = (int)intval;
1197da6c28aaSamw 			break;
1198da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1199da6c28aaSamw 			chosen_normal = (int)intval;
1200da6c28aaSamw 			break;
1201fa9e4066Sahrens 		}
1202fa9e4066Sahrens 
1203e9dbad6fSeschrock 		/*
1204e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1205e9dbad6fSeschrock 		 * checks to enforce.
1206e9dbad6fSeschrock 		 */
1207e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1208e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1209e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1210e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1211e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1212e9dbad6fSeschrock 			char buf[64];
1213e9dbad6fSeschrock 
1214e9dbad6fSeschrock 			switch (prop) {
1215e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1216a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
1217e9dbad6fSeschrock 				if (intval > volsize) {
1218e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1219e9dbad6fSeschrock 					    "'%s' is greater than current "
1220e9dbad6fSeschrock 					    "volume size"), propname);
1221e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1222e9dbad6fSeschrock 					    errbuf);
1223e9dbad6fSeschrock 					goto error;
1224e9dbad6fSeschrock 				}
1225e9dbad6fSeschrock 				break;
1226e9dbad6fSeschrock 
1227e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1228e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1229e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1230e9dbad6fSeschrock 					    sizeof (buf));
1231e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1232e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1233e9dbad6fSeschrock 					    "volume block size (%s)"),
1234e9dbad6fSeschrock 					    propname, buf);
1235e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1236e9dbad6fSeschrock 					    errbuf);
1237e9dbad6fSeschrock 					goto error;
1238e9dbad6fSeschrock 				}
1239e9dbad6fSeschrock 
1240e9dbad6fSeschrock 				if (intval == 0) {
1241e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1242e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1243e9dbad6fSeschrock 					    propname);
1244e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1245e9dbad6fSeschrock 					    errbuf);
1246e9dbad6fSeschrock 					goto error;
1247e9dbad6fSeschrock 				}
1248f3861e1aSahl 				break;
1249e9dbad6fSeschrock 			}
1250e9dbad6fSeschrock 		}
1251e9dbad6fSeschrock 	}
1252e9dbad6fSeschrock 
1253e9dbad6fSeschrock 	/*
1254da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1255da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1256da6c28aaSamw 	 *
1257da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1258da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1259da6c28aaSamw 	 */
1260de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1261da6c28aaSamw 		if (nvlist_add_uint64(ret,
1262da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1263da6c28aaSamw 			(void) no_memory(hdl);
1264da6c28aaSamw 			goto error;
1265da6c28aaSamw 		}
1266de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1267da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1268da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1269da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1270da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1271da6c28aaSamw 		goto error;
1272da6c28aaSamw 	}
1273e9dbad6fSeschrock 	return (ret);
1274e9dbad6fSeschrock 
1275e9dbad6fSeschrock error:
1276e9dbad6fSeschrock 	nvlist_free(ret);
1277e9dbad6fSeschrock 	return (NULL);
1278e9dbad6fSeschrock }
1279e9dbad6fSeschrock 
128036db6475SEric Taylor int
128136db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
128236db6475SEric Taylor {
128336db6475SEric Taylor 	uint64_t old_volsize;
128436db6475SEric Taylor 	uint64_t new_volsize;
128536db6475SEric Taylor 	uint64_t old_reservation;
128636db6475SEric Taylor 	uint64_t new_reservation;
128736db6475SEric Taylor 	zfs_prop_t resv_prop;
1288c61ea566SGeorge Wilson 	nvlist_t *props;
128936db6475SEric Taylor 
129036db6475SEric Taylor 	/*
129136db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
129236db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
129336db6475SEric Taylor 	 */
129436db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
129536db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
129636db6475SEric Taylor 		return (-1);
129736db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1298c61ea566SGeorge Wilson 
1299c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1300c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1301c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1302c61ea566SGeorge Wilson 
1303c61ea566SGeorge Wilson 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1304c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1305c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1306c61ea566SGeorge Wilson 		fnvlist_free(props);
130736db6475SEric Taylor 		return (0);
130836db6475SEric Taylor 	}
130936db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1310c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1311c61ea566SGeorge Wilson 		fnvlist_free(props);
131236db6475SEric Taylor 		return (-1);
1313c61ea566SGeorge Wilson 	}
1314c61ea566SGeorge Wilson 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1315c61ea566SGeorge Wilson 	fnvlist_free(props);
1316c61ea566SGeorge Wilson 
131736db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
131836db6475SEric Taylor 	    new_reservation) != 0) {
131936db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
132036db6475SEric Taylor 		return (-1);
132136db6475SEric Taylor 	}
132236db6475SEric Taylor 	return (1);
132336db6475SEric Taylor }
132436db6475SEric Taylor 
132592241e0bSTom Erickson void
132692241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
132792241e0bSTom Erickson     char *errbuf)
132892241e0bSTom Erickson {
132992241e0bSTom Erickson 	switch (err) {
133092241e0bSTom Erickson 
133192241e0bSTom Erickson 	case ENOSPC:
133292241e0bSTom Erickson 		/*
133392241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
133492241e0bSTom Erickson 		 * something different; setting a quota or reservation
133592241e0bSTom Erickson 		 * doesn't use any disk space.
133692241e0bSTom Erickson 		 */
133792241e0bSTom Erickson 		switch (prop) {
133892241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
133992241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
134092241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
134192241e0bSTom Erickson 			    "size is less than current used or "
134292241e0bSTom Erickson 			    "reserved space"));
134392241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
134492241e0bSTom Erickson 			break;
134592241e0bSTom Erickson 
134692241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
134792241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
134892241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
134992241e0bSTom Erickson 			    "size is greater than available space"));
135092241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
135192241e0bSTom Erickson 			break;
135292241e0bSTom Erickson 
135392241e0bSTom Erickson 		default:
135492241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
135592241e0bSTom Erickson 			break;
135692241e0bSTom Erickson 		}
135792241e0bSTom Erickson 		break;
135892241e0bSTom Erickson 
135992241e0bSTom Erickson 	case EBUSY:
136092241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
136192241e0bSTom Erickson 		break;
136292241e0bSTom Erickson 
136392241e0bSTom Erickson 	case EROFS:
136492241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
136592241e0bSTom Erickson 		break;
136692241e0bSTom Erickson 
136792241e0bSTom Erickson 	case ENOTSUP:
136892241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136992241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
137092241e0bSTom Erickson 		    "property or value"));
137192241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
137292241e0bSTom Erickson 		break;
137392241e0bSTom Erickson 
137492241e0bSTom Erickson 	case ERANGE:
137592241e0bSTom Erickson 		if (prop == ZFS_PROP_COMPRESSION) {
137692241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137792241e0bSTom Erickson 			    "property setting is not allowed on "
137892241e0bSTom Erickson 			    "bootable datasets"));
137992241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
138092241e0bSTom Erickson 		} else {
138192241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
138292241e0bSTom Erickson 		}
138392241e0bSTom Erickson 		break;
138492241e0bSTom Erickson 
1385ab003da8SJim Dunham 	case EINVAL:
1386ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1387ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1388ab003da8SJim Dunham 		} else {
1389ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1390ab003da8SJim Dunham 		}
1391ab003da8SJim Dunham 		break;
1392ab003da8SJim Dunham 
139392241e0bSTom Erickson 	case EOVERFLOW:
139492241e0bSTom Erickson 		/*
139592241e0bSTom Erickson 		 * This platform can't address a volume this big.
139692241e0bSTom Erickson 		 */
139792241e0bSTom Erickson #ifdef _ILP32
139892241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
139992241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
140092241e0bSTom Erickson 			break;
140192241e0bSTom Erickson 		}
140292241e0bSTom Erickson #endif
140392241e0bSTom Erickson 		/* FALLTHROUGH */
140492241e0bSTom Erickson 	default:
140592241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
140692241e0bSTom Erickson 	}
140792241e0bSTom Erickson }
140892241e0bSTom Erickson 
1409e9dbad6fSeschrock /*
1410e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1411e9dbad6fSeschrock  */
1412e9dbad6fSeschrock int
1413e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1414e9dbad6fSeschrock {
1415e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1416e9dbad6fSeschrock 	int ret = -1;
1417e9dbad6fSeschrock 	prop_changelist_t *cl = NULL;
1418e9dbad6fSeschrock 	char errbuf[1024];
1419e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1420e9dbad6fSeschrock 	nvlist_t *nvl = NULL, *realprops;
1421e9dbad6fSeschrock 	zfs_prop_t prop;
14224445fffbSMatthew Ahrens 	boolean_t do_prefix = B_TRUE;
142336db6475SEric Taylor 	int added_resv;
1424e9dbad6fSeschrock 
1425e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1426e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1427e9dbad6fSeschrock 	    zhp->zfs_name);
1428e9dbad6fSeschrock 
1429e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1430e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1431e9dbad6fSeschrock 		(void) no_memory(hdl);
1432e9dbad6fSeschrock 		goto error;
1433e9dbad6fSeschrock 	}
1434e9dbad6fSeschrock 
14350a48a24eStimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1436e9dbad6fSeschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1437e9dbad6fSeschrock 		goto error;
1438990b4856Slling 
1439e9dbad6fSeschrock 	nvlist_free(nvl);
1440e9dbad6fSeschrock 	nvl = realprops;
1441e9dbad6fSeschrock 
1442e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
1443e9dbad6fSeschrock 
144436db6475SEric Taylor 	if (prop == ZFS_PROP_VOLSIZE) {
144536db6475SEric Taylor 		if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
144636db6475SEric Taylor 			goto error;
144736db6475SEric Taylor 	}
144836db6475SEric Taylor 
14490069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1450e9dbad6fSeschrock 		goto error;
1451fa9e4066Sahrens 
1452fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
145399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1454fa9e4066Sahrens 		    "child dataset with inherited mountpoint is used "
145599653d4eSeschrock 		    "in a non-global zone"));
145699653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1457fa9e4066Sahrens 		goto error;
1458fa9e4066Sahrens 	}
1459fa9e4066Sahrens 
14600068372bSMark J Musante 	/*
14614445fffbSMatthew Ahrens 	 * We don't want to unmount & remount the dataset when changing
14624445fffbSMatthew Ahrens 	 * its canmount property to 'on' or 'noauto'.  We only use
14634445fffbSMatthew Ahrens 	 * the changelist logic to unmount when setting canmount=off.
14640068372bSMark J Musante 	 */
14654445fffbSMatthew Ahrens 	if (prop == ZFS_PROP_CANMOUNT) {
14664445fffbSMatthew Ahrens 		uint64_t idx;
14674445fffbSMatthew Ahrens 		int err = zprop_string_to_index(prop, propval, &idx,
14684445fffbSMatthew Ahrens 		    ZFS_TYPE_DATASET);
14694445fffbSMatthew Ahrens 		if (err == 0 && idx != ZFS_CANMOUNT_OFF)
14704445fffbSMatthew Ahrens 			do_prefix = B_FALSE;
14714445fffbSMatthew Ahrens 	}
1472a227b7f4Shs24103 
1473a227b7f4Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1474fa9e4066Sahrens 		goto error;
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	/*
1477fa9e4066Sahrens 	 * Execute the corresponding ioctl() to set this property.
1478fa9e4066Sahrens 	 */
1479fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1480fa9e4066Sahrens 
1481990b4856Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1482e9dbad6fSeschrock 		goto error;
1483e9dbad6fSeschrock 
1484ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1485743a77edSAlan Wright 
1486fa9e4066Sahrens 	if (ret != 0) {
148792241e0bSTom Erickson 		zfs_setprop_error(hdl, prop, errno, errbuf);
148836db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
148936db6475SEric Taylor 			/* clean up the volsize property we tried to set */
149036db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
149136db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
149236db6475SEric Taylor 			nvlist_free(nvl);
149336db6475SEric Taylor 			zcmd_free_nvlists(&zc);
149436db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
149536db6475SEric Taylor 				goto error;
149636db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
149736db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
149836db6475SEric Taylor 			    old_volsize) != 0)
149936db6475SEric Taylor 				goto error;
150036db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
150136db6475SEric Taylor 				goto error;
150236db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
150336db6475SEric Taylor 		}
1504fa9e4066Sahrens 	} else {
1505a227b7f4Shs24103 		if (do_prefix)
1506a227b7f4Shs24103 			ret = changelist_postfix(cl);
1507a227b7f4Shs24103 
1508fa9e4066Sahrens 		/*
1509fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1510fa9e4066Sahrens 		 * is reflected.
1511fa9e4066Sahrens 		 */
1512a227b7f4Shs24103 		if (ret == 0)
1513fa9e4066Sahrens 			(void) get_stats(zhp);
1514fa9e4066Sahrens 	}
1515fa9e4066Sahrens 
1516fa9e4066Sahrens error:
1517e9dbad6fSeschrock 	nvlist_free(nvl);
1518e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1519e9dbad6fSeschrock 	if (cl)
1520fa9e4066Sahrens 		changelist_free(cl);
1521fa9e4066Sahrens 	return (ret);
1522fa9e4066Sahrens }
1523fa9e4066Sahrens 
1524fa9e4066Sahrens /*
152592241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
152692241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1527fa9e4066Sahrens  */
1528fa9e4066Sahrens int
152992241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1530fa9e4066Sahrens {
1531fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1532fa9e4066Sahrens 	int ret;
1533fa9e4066Sahrens 	prop_changelist_t *cl;
153499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
153599653d4eSeschrock 	char errbuf[1024];
1536e9dbad6fSeschrock 	zfs_prop_t prop;
153799653d4eSeschrock 
153899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
153999653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1540fa9e4066Sahrens 
154192241e0bSTom Erickson 	zc.zc_cookie = received;
1542990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1543e9dbad6fSeschrock 		/*
1544e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1545e9dbad6fSeschrock 		 * small, so just do it here.
1546e9dbad6fSeschrock 		 */
1547e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1548e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1549e9dbad6fSeschrock 			    "invalid property"));
1550e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1551e9dbad6fSeschrock 		}
1552e9dbad6fSeschrock 
1553e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1554e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1555e9dbad6fSeschrock 
1556e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1557e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1558e9dbad6fSeschrock 
1559e9dbad6fSeschrock 		return (0);
1560e9dbad6fSeschrock 	}
1561e9dbad6fSeschrock 
1562fa9e4066Sahrens 	/*
1563fa9e4066Sahrens 	 * Verify that this property is inheritable.
1564fa9e4066Sahrens 	 */
156599653d4eSeschrock 	if (zfs_prop_readonly(prop))
156699653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1567fa9e4066Sahrens 
156892241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
156999653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1570fa9e4066Sahrens 
1571fa9e4066Sahrens 	/*
1572fa9e4066Sahrens 	 * Check to see if the value applies to this type
1573fa9e4066Sahrens 	 */
157499653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
157599653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1576fa9e4066Sahrens 
1577bf7c2d40Srm160521 	/*
157836db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1579bf7c2d40Srm160521 	 */
1580bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1581fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1582e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1583fa9e4066Sahrens 
1584fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1585fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
158699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158799653d4eSeschrock 		    "dataset is used in a non-global zone"));
158899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1589fa9e4066Sahrens 	}
1590fa9e4066Sahrens 
1591fa9e4066Sahrens 	/*
1592fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1593fa9e4066Sahrens 	 */
15940069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1595fa9e4066Sahrens 		return (-1);
1596fa9e4066Sahrens 
1597fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
159899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
159999653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
160099653d4eSeschrock 		    "in a non-global zone"));
160199653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1602fa9e4066Sahrens 		goto error;
1603fa9e4066Sahrens 	}
1604fa9e4066Sahrens 
1605fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1606fa9e4066Sahrens 		goto error;
1607fa9e4066Sahrens 
1608e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
160999653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1610fa9e4066Sahrens 	} else {
1611fa9e4066Sahrens 
1612efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1613fa9e4066Sahrens 			goto error;
1614fa9e4066Sahrens 
1615fa9e4066Sahrens 		/*
1616fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1617fa9e4066Sahrens 		 */
1618fa9e4066Sahrens 		(void) get_stats(zhp);
1619fa9e4066Sahrens 	}
1620fa9e4066Sahrens 
1621fa9e4066Sahrens error:
1622fa9e4066Sahrens 	changelist_free(cl);
1623fa9e4066Sahrens 	return (ret);
1624fa9e4066Sahrens }
1625fa9e4066Sahrens 
1626fa9e4066Sahrens /*
16277f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
16287f7322feSeschrock  * extract them appropriately.
16297f7322feSeschrock  */
16307f7322feSeschrock static uint64_t
16317f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16327f7322feSeschrock {
16337f7322feSeschrock 	nvlist_t *nv;
16347f7322feSeschrock 	uint64_t value;
16357f7322feSeschrock 
1636a2eea2e1Sahrens 	*source = NULL;
16377f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
16387f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1639990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1640990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
16417f7322feSeschrock 	} else {
16422e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
16432e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
16447f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
16457f7322feSeschrock 		*source = "";
16467f7322feSeschrock 	}
16477f7322feSeschrock 
16487f7322feSeschrock 	return (value);
16497f7322feSeschrock }
16507f7322feSeschrock 
16517f7322feSeschrock static char *
16527f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16537f7322feSeschrock {
16547f7322feSeschrock 	nvlist_t *nv;
16557f7322feSeschrock 	char *value;
16567f7322feSeschrock 
1657a2eea2e1Sahrens 	*source = NULL;
16587f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
16597f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1660990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1661990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
16627f7322feSeschrock 	} else {
16632e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
16642e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
16657f7322feSeschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
16667f7322feSeschrock 			value = "";
16677f7322feSeschrock 		*source = "";
16687f7322feSeschrock 	}
16697f7322feSeschrock 
16707f7322feSeschrock 	return (value);
16717f7322feSeschrock }
16727f7322feSeschrock 
167392241e0bSTom Erickson static boolean_t
167492241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
167592241e0bSTom Erickson {
167692241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
167792241e0bSTom Erickson }
167892241e0bSTom Erickson 
167992241e0bSTom Erickson static void
168092241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
168192241e0bSTom Erickson {
168292241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
168392241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
168492241e0bSTom Erickson }
168592241e0bSTom Erickson 
168692241e0bSTom Erickson static void
168792241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
168892241e0bSTom Erickson {
168992241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
169092241e0bSTom Erickson 	*cookie = 0;
169192241e0bSTom Erickson }
169292241e0bSTom Erickson 
16937f7322feSeschrock /*
1694fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1695fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1696fa9e4066Sahrens  *
1697fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1698fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1699fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1700fa9e4066Sahrens  * the source "temporary".
1701fa9e4066Sahrens  */
170299653d4eSeschrock static int
1703990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
170499653d4eSeschrock     char **source, uint64_t *val)
1705fa9e4066Sahrens {
1706bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
170796510749Stimh 	nvlist_t *zplprops = NULL;
1708fa9e4066Sahrens 	struct mnttab mnt;
17093ccfa83cSahrens 	char *mntopt_on = NULL;
17103ccfa83cSahrens 	char *mntopt_off = NULL;
171192241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1712fa9e4066Sahrens 
1713fa9e4066Sahrens 	*source = NULL;
1714fa9e4066Sahrens 
17153ccfa83cSahrens 	switch (prop) {
17163ccfa83cSahrens 	case ZFS_PROP_ATIME:
17173ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
17183ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
17193ccfa83cSahrens 		break;
17203ccfa83cSahrens 
17213ccfa83cSahrens 	case ZFS_PROP_DEVICES:
17223ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
17233ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
17243ccfa83cSahrens 		break;
17253ccfa83cSahrens 
17263ccfa83cSahrens 	case ZFS_PROP_EXEC:
17273ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
17283ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
17293ccfa83cSahrens 		break;
17303ccfa83cSahrens 
17313ccfa83cSahrens 	case ZFS_PROP_READONLY:
17323ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
17333ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
17343ccfa83cSahrens 		break;
17353ccfa83cSahrens 
17363ccfa83cSahrens 	case ZFS_PROP_SETUID:
17373ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
17383ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
17393ccfa83cSahrens 		break;
17403ccfa83cSahrens 
17413ccfa83cSahrens 	case ZFS_PROP_XATTR:
17423ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
17433ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
17443ccfa83cSahrens 		break;
1745da6c28aaSamw 
1746da6c28aaSamw 	case ZFS_PROP_NBMAND:
1747da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
1748da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
1749da6c28aaSamw 		break;
17503ccfa83cSahrens 	}
17513ccfa83cSahrens 
17523bb79becSeschrock 	/*
17533bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
17543bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
17553bb79becSeschrock 	 * we're looking up a property which requires its presence.
17563bb79becSeschrock 	 */
17573bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
17583ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1759ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
1760ebedde84SEric Taylor 		struct mnttab entry;
17613bb79becSeschrock 
1762ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1763ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
17643ccfa83cSahrens 			    entry.mnt_mntopts);
17653ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
17663bb79becSeschrock 				return (-1);
17673ccfa83cSahrens 		}
17683bb79becSeschrock 
17693bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
17703bb79becSeschrock 	}
17713bb79becSeschrock 
1772fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
1773fa9e4066Sahrens 		mnt.mnt_mntopts = "";
1774fa9e4066Sahrens 	else
1775fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1776fa9e4066Sahrens 
1777fa9e4066Sahrens 	switch (prop) {
1778fa9e4066Sahrens 	case ZFS_PROP_ATIME:
1779fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
1780fa9e4066Sahrens 	case ZFS_PROP_EXEC:
17813ccfa83cSahrens 	case ZFS_PROP_READONLY:
17823ccfa83cSahrens 	case ZFS_PROP_SETUID:
17833ccfa83cSahrens 	case ZFS_PROP_XATTR:
1784da6c28aaSamw 	case ZFS_PROP_NBMAND:
178599653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
1786fa9e4066Sahrens 
178792241e0bSTom Erickson 		if (received)
178892241e0bSTom Erickson 			break;
178992241e0bSTom Erickson 
17903ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
179199653d4eSeschrock 			*val = B_TRUE;
1792fa9e4066Sahrens 			if (src)
1793990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
17943ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
179599653d4eSeschrock 			*val = B_FALSE;
1796fa9e4066Sahrens 			if (src)
1797990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
1798fa9e4066Sahrens 		}
179999653d4eSeschrock 		break;
1800fa9e4066Sahrens 
18013ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
1802d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
1803fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
1804a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
1805fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
1806a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
1807a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
180892241e0bSTom Erickson 
180992241e0bSTom Erickson 		if (*source == NULL) {
181092241e0bSTom Erickson 			/* not default, must be local */
1811fa9e4066Sahrens 			*source = zhp->zfs_name;
181292241e0bSTom Erickson 		}
181399653d4eSeschrock 		break;
1814fa9e4066Sahrens 
1815fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
181699653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
181799653d4eSeschrock 		break;
1818fa9e4066Sahrens 
181939c23413Seschrock 	case ZFS_PROP_NUMCLONES:
182039c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
182139c23413Seschrock 		break;
182239c23413Seschrock 
1823bd00f61bSrm160521 	case ZFS_PROP_VERSION:
1824de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
1825de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
1826de8267e0Stimh 	case ZFS_PROP_CASE:
1827de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1828de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
18293d7934e1Srm160521 			return (-1);
1830bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1831de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1832de8267e0Stimh 			zcmd_free_nvlists(&zc);
1833f4b94bdeSMatthew Ahrens 			return (-1);
1834bd00f61bSrm160521 		}
1835de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1836de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1837de8267e0Stimh 		    val) != 0) {
1838de8267e0Stimh 			zcmd_free_nvlists(&zc);
1839f4b94bdeSMatthew Ahrens 			return (-1);
1840de8267e0Stimh 		}
184196510749Stimh 		if (zplprops)
184296510749Stimh 			nvlist_free(zplprops);
1843de8267e0Stimh 		zcmd_free_nvlists(&zc);
1844bd00f61bSrm160521 		break;
1845bd00f61bSrm160521 
1846fa9e4066Sahrens 	default:
1847e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
184891ebeef5Sahrens 		case PROP_TYPE_NUMBER:
184991ebeef5Sahrens 		case PROP_TYPE_INDEX:
1850e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
185174e7dc98SMatthew Ahrens 			/*
185214843421SMatthew Ahrens 			 * If we tried to use a default value for a
185374e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
185431f572c2STom Erickson 			 * present.
185574e7dc98SMatthew Ahrens 			 */
185674e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
185731f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
185831f572c2STom Erickson 				*source = NULL;
185974e7dc98SMatthew Ahrens 			}
1860e7437265Sahrens 			break;
1861e7437265Sahrens 
186291ebeef5Sahrens 		case PROP_TYPE_STRING:
1863e7437265Sahrens 		default:
186499653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
186599653d4eSeschrock 			    "cannot get non-numeric property"));
186699653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
186799653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
1868fa9e4066Sahrens 		}
1869e7437265Sahrens 	}
1870fa9e4066Sahrens 
1871fa9e4066Sahrens 	return (0);
1872fa9e4066Sahrens }
1873fa9e4066Sahrens 
1874fa9e4066Sahrens /*
1875fa9e4066Sahrens  * Calculate the source type, given the raw source string.
1876fa9e4066Sahrens  */
1877fa9e4066Sahrens static void
1878990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1879fa9e4066Sahrens     char *statbuf, size_t statlen)
1880fa9e4066Sahrens {
1881990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1882fa9e4066Sahrens 		return;
1883fa9e4066Sahrens 
1884fa9e4066Sahrens 	if (source == NULL) {
1885990b4856Slling 		*srctype = ZPROP_SRC_NONE;
1886fa9e4066Sahrens 	} else if (source[0] == '\0') {
1887990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
188892241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
188992241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
1890fa9e4066Sahrens 	} else {
1891fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
1892990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
1893fa9e4066Sahrens 		} else {
1894fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
1895990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
1896fa9e4066Sahrens 		}
1897fa9e4066Sahrens 	}
1898fa9e4066Sahrens 
1899fa9e4066Sahrens }
1900fa9e4066Sahrens 
190192241e0bSTom Erickson int
190292241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
190392241e0bSTom Erickson     size_t proplen, boolean_t literal)
190492241e0bSTom Erickson {
190592241e0bSTom Erickson 	zfs_prop_t prop;
190692241e0bSTom Erickson 	int err = 0;
190792241e0bSTom Erickson 
190892241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
190992241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
191092241e0bSTom Erickson 			return (-1);
191192241e0bSTom Erickson 
191292241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
191392241e0bSTom Erickson 
191492241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
191592241e0bSTom Erickson 		uint64_t cookie;
191692241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
191792241e0bSTom Erickson 			return (-1);
191892241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
191992241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
192092241e0bSTom Erickson 		    NULL, NULL, 0, literal);
192192241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
192292241e0bSTom Erickson 	} else {
192392241e0bSTom Erickson 		nvlist_t *propval;
192492241e0bSTom Erickson 		char *recvdval;
192592241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
192692241e0bSTom Erickson 		    propname, &propval) != 0)
192792241e0bSTom Erickson 			return (-1);
192892241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
192992241e0bSTom Erickson 		    &recvdval) == 0);
193092241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
193192241e0bSTom Erickson 	}
193292241e0bSTom Erickson 
193392241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
193492241e0bSTom Erickson }
193592241e0bSTom Erickson 
193619b94df9SMatthew Ahrens static int
193719b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
193819b94df9SMatthew Ahrens {
193919b94df9SMatthew Ahrens 	nvlist_t *value;
194019b94df9SMatthew Ahrens 	nvpair_t *pair;
194119b94df9SMatthew Ahrens 
194219b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
194319b94df9SMatthew Ahrens 	if (value == NULL)
194419b94df9SMatthew Ahrens 		return (-1);
194519b94df9SMatthew Ahrens 
194619b94df9SMatthew Ahrens 	propbuf[0] = '\0';
194719b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
194819b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
194919b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
195019b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
195119b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
195219b94df9SMatthew Ahrens 	}
195319b94df9SMatthew Ahrens 
195419b94df9SMatthew Ahrens 	return (0);
195519b94df9SMatthew Ahrens }
195619b94df9SMatthew Ahrens 
195719b94df9SMatthew Ahrens struct get_clones_arg {
195819b94df9SMatthew Ahrens 	uint64_t numclones;
195919b94df9SMatthew Ahrens 	nvlist_t *value;
196019b94df9SMatthew Ahrens 	const char *origin;
196119b94df9SMatthew Ahrens 	char buf[ZFS_MAXNAMELEN];
196219b94df9SMatthew Ahrens };
196319b94df9SMatthew Ahrens 
196419b94df9SMatthew Ahrens int
196519b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
196619b94df9SMatthew Ahrens {
196719b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
196819b94df9SMatthew Ahrens 
196919b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
197019b94df9SMatthew Ahrens 		zfs_close(zhp);
197119b94df9SMatthew Ahrens 		return (0);
197219b94df9SMatthew Ahrens 	}
197319b94df9SMatthew Ahrens 
197419b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
197519b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
197619b94df9SMatthew Ahrens 		goto out;
197719b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
19783b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
197919b94df9SMatthew Ahrens 		gca->numclones--;
198019b94df9SMatthew Ahrens 	}
198119b94df9SMatthew Ahrens 
198219b94df9SMatthew Ahrens out:
198319b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
198419b94df9SMatthew Ahrens 	zfs_close(zhp);
198519b94df9SMatthew Ahrens 	return (0);
198619b94df9SMatthew Ahrens }
198719b94df9SMatthew Ahrens 
198819b94df9SMatthew Ahrens nvlist_t *
198919b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
199019b94df9SMatthew Ahrens {
199119b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
199219b94df9SMatthew Ahrens 
199319b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
199419b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
199519b94df9SMatthew Ahrens 		struct get_clones_arg gca;
199619b94df9SMatthew Ahrens 
199719b94df9SMatthew Ahrens 		/*
199819b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
199919b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
200019b94df9SMatthew Ahrens 		 */
200119b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
200219b94df9SMatthew Ahrens 			return (NULL);
200319b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
200419b94df9SMatthew Ahrens 			return (NULL);
200519b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
200619b94df9SMatthew Ahrens 			nvlist_free(nv);
200719b94df9SMatthew Ahrens 			return (NULL);
200819b94df9SMatthew Ahrens 		}
200919b94df9SMatthew Ahrens 
201019b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
201119b94df9SMatthew Ahrens 		gca.value = value;
201219b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
201319b94df9SMatthew Ahrens 
201419b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
201519b94df9SMatthew Ahrens 			zfs_handle_t *root;
201619b94df9SMatthew Ahrens 			char pool[ZFS_MAXNAMELEN];
201719b94df9SMatthew Ahrens 			char *cp = pool;
201819b94df9SMatthew Ahrens 
201919b94df9SMatthew Ahrens 			/* get the pool name */
202019b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
202119b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
202219b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
202319b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
202419b94df9SMatthew Ahrens 
202519b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
202619b94df9SMatthew Ahrens 		}
202719b94df9SMatthew Ahrens 
202819b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
202919b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
203019b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
203119b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
203219b94df9SMatthew Ahrens 			nvlist_free(nv);
203319b94df9SMatthew Ahrens 			nvlist_free(value);
203419b94df9SMatthew Ahrens 			return (NULL);
203519b94df9SMatthew Ahrens 		}
203619b94df9SMatthew Ahrens 		nvlist_free(nv);
203719b94df9SMatthew Ahrens 		nvlist_free(value);
203819b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
203919b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
204019b94df9SMatthew Ahrens 	}
204119b94df9SMatthew Ahrens 
204219b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
204319b94df9SMatthew Ahrens 
204419b94df9SMatthew Ahrens 	return (value);
204519b94df9SMatthew Ahrens }
204619b94df9SMatthew Ahrens 
2047fa9e4066Sahrens /*
2048fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2049fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2050fa9e4066Sahrens  * human-readable form.
2051fa9e4066Sahrens  *
2052fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2053fa9e4066Sahrens  */
2054fa9e4066Sahrens int
2055fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2056990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2057fa9e4066Sahrens {
2058fa9e4066Sahrens 	char *source = NULL;
2059fa9e4066Sahrens 	uint64_t val;
2060fa9e4066Sahrens 	char *str;
2061e9dbad6fSeschrock 	const char *strval;
206292241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2063fa9e4066Sahrens 
2064fa9e4066Sahrens 	/*
2065fa9e4066Sahrens 	 * Check to see if this property applies to our object
2066fa9e4066Sahrens 	 */
2067fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2068fa9e4066Sahrens 		return (-1);
2069fa9e4066Sahrens 
207092241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
207192241e0bSTom Erickson 		return (-1);
207292241e0bSTom Erickson 
2073fa9e4066Sahrens 	if (src)
2074990b4856Slling 		*src = ZPROP_SRC_NONE;
2075fa9e4066Sahrens 
2076fa9e4066Sahrens 	switch (prop) {
2077fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2078fa9e4066Sahrens 		/*
2079fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2080fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2081fa9e4066Sahrens 		 */
2082fa9e4066Sahrens 		{
2083a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2084a2eea2e1Sahrens 			time_t time = (time_t)val;
2085fa9e4066Sahrens 			struct tm t;
2086fa9e4066Sahrens 
2087fa9e4066Sahrens 			if (literal ||
2088fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2089fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2090fa9e4066Sahrens 			    &t) == 0)
2091a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2092fa9e4066Sahrens 		}
2093fa9e4066Sahrens 		break;
2094fa9e4066Sahrens 
2095fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2096fa9e4066Sahrens 		/*
2097fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2098fa9e4066Sahrens 		 *
2099fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2100fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2101fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2102fa9e4066Sahrens 		 *
2103fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2104fa9e4066Sahrens 		 * root to any values we return.
2105fa9e4066Sahrens 		 */
210629ab75c9Srm160521 
21077f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2108fa9e4066Sahrens 
2109b21718f0Sgw25295 		if (str[0] == '/') {
211029ab75c9Srm160521 			char buf[MAXPATHLEN];
211129ab75c9Srm160521 			char *root = buf;
2112a79992aaSTom Erickson 			const char *relpath;
2113fa9e4066Sahrens 
2114a79992aaSTom Erickson 			/*
2115a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2116a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2117a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2118a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2119a79992aaSTom Erickson 			 * inherited.
2120a79992aaSTom Erickson 			 */
2121a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2122a79992aaSTom Erickson 				relpath = "";
2123a79992aaSTom Erickson 			} else {
2124a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2125fa9e4066Sahrens 				if (relpath[0] == '/')
2126fa9e4066Sahrens 					relpath++;
2127a79992aaSTom Erickson 			}
2128b21718f0Sgw25295 
212929ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
213029ab75c9Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
213129ab75c9Srm160521 			    (strcmp(root, "-") == 0))
213229ab75c9Srm160521 				root[0] = '\0';
2133b21718f0Sgw25295 			/*
2134b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2135b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2136b21718f0Sgw25295 			 * mountpoint path.
2137b21718f0Sgw25295 			 */
2138b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2139b21718f0Sgw25295 				root++;
2140b21718f0Sgw25295 
2141b21718f0Sgw25295 			/*
2142b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2143b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2144b21718f0Sgw25295 			 * an inherited mountpoint.
2145b21718f0Sgw25295 			 */
2146b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2147b21718f0Sgw25295 			    relpath[0] != '\0'))
21487f7322feSeschrock 				str++;
2149fa9e4066Sahrens 
2150fa9e4066Sahrens 			if (relpath[0] == '\0')
2151fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
21527f7322feSeschrock 				    root, str);
2153fa9e4066Sahrens 			else
2154fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
21557f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2156fa9e4066Sahrens 				    relpath);
2157fa9e4066Sahrens 		} else {
2158fa9e4066Sahrens 			/* 'legacy' or 'none' */
21597f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2160fa9e4066Sahrens 		}
2161fa9e4066Sahrens 
2162fa9e4066Sahrens 		break;
2163fa9e4066Sahrens 
2164fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
2165a2eea2e1Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2166fa9e4066Sahrens 		    proplen);
2167fa9e4066Sahrens 		/*
2168fa9e4066Sahrens 		 * If there is no parent at all, return failure to indicate that
2169fa9e4066Sahrens 		 * it doesn't apply to this dataset.
2170fa9e4066Sahrens 		 */
2171fa9e4066Sahrens 		if (propbuf[0] == '\0')
2172fa9e4066Sahrens 			return (-1);
2173fa9e4066Sahrens 		break;
2174fa9e4066Sahrens 
217519b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
217619b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
217719b94df9SMatthew Ahrens 			return (-1);
217819b94df9SMatthew Ahrens 		break;
217919b94df9SMatthew Ahrens 
2180fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2181a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2182fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2183a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2184a9799022Sck153898 
218599653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
218699653d4eSeschrock 			return (-1);
2187fa9e4066Sahrens 
2188fa9e4066Sahrens 		/*
2189fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2190fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2191fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2192fa9e4066Sahrens 		 * that its set locally.
2193fa9e4066Sahrens 		 */
2194fa9e4066Sahrens 		if (val == 0) {
2195fa9e4066Sahrens 			if (literal)
2196fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2197fa9e4066Sahrens 			else
2198fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2199fa9e4066Sahrens 		} else {
2200fa9e4066Sahrens 			if (literal)
22015ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
22025ad82045Snd150628 				    (u_longlong_t)val);
2203fa9e4066Sahrens 			else
2204fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2205fa9e4066Sahrens 		}
2206fa9e4066Sahrens 		break;
2207fa9e4066Sahrens 
2208187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2209fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
221099653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
221199653d4eSeschrock 			return (-1);
2212b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2213b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2214b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2215fa9e4066Sahrens 		break;
2216fa9e4066Sahrens 
2217fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2218fa9e4066Sahrens 		switch (zhp->zfs_type) {
2219fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2220fa9e4066Sahrens 			str = "filesystem";
2221fa9e4066Sahrens 			break;
2222fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2223fa9e4066Sahrens 			str = "volume";
2224fa9e4066Sahrens 			break;
2225fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2226fa9e4066Sahrens 			str = "snapshot";
2227fa9e4066Sahrens 			break;
2228fa9e4066Sahrens 		default:
222999653d4eSeschrock 			abort();
2230fa9e4066Sahrens 		}
2231fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2232fa9e4066Sahrens 		break;
2233fa9e4066Sahrens 
2234fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2235fa9e4066Sahrens 		/*
2236fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2237fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2238fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2239fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2240fa9e4066Sahrens 		 */
224199653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
224299653d4eSeschrock 		    src, &source, &val) != 0)
224399653d4eSeschrock 			return (-1);
224499653d4eSeschrock 		if (val)
2245fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2246fa9e4066Sahrens 		else
2247fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2248fa9e4066Sahrens 		break;
2249fa9e4066Sahrens 
2250fa9e4066Sahrens 	case ZFS_PROP_NAME:
2251fa9e4066Sahrens 		/*
2252fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2253fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2254fa9e4066Sahrens 		 * consumers.
2255fa9e4066Sahrens 		 */
2256fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2257fa9e4066Sahrens 		break;
2258fa9e4066Sahrens 
22594201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
22604201a95eSRic Aleshire 		{
22614201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
22624201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
22634201a95eSRic Aleshire 
22644201a95eSRic Aleshire 			(void) strlcpy(propbuf,
22654201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
22664201a95eSRic Aleshire 
22674201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
22684201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
22694201a95eSRic Aleshire 				break;
22704201a95eSRic Aleshire 
22714201a95eSRic Aleshire 			/*
22724201a95eSRic Aleshire 			 * Try to translate the internal hex string to
22734201a95eSRic Aleshire 			 * human-readable output.  If there are any
22744201a95eSRic Aleshire 			 * problems just use the hex string.
22754201a95eSRic Aleshire 			 */
22764201a95eSRic Aleshire 
22774201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
22784201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
22794201a95eSRic Aleshire 				m_label_free(new_sl);
22804201a95eSRic Aleshire 				break;
22814201a95eSRic Aleshire 			}
22824201a95eSRic Aleshire 
22834201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
22844201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
22854201a95eSRic Aleshire 				if (ascii)
22864201a95eSRic Aleshire 					free(ascii);
22874201a95eSRic Aleshire 				m_label_free(new_sl);
22884201a95eSRic Aleshire 				break;
22894201a95eSRic Aleshire 			}
22904201a95eSRic Aleshire 			m_label_free(new_sl);
22914201a95eSRic Aleshire 
22924201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
22934201a95eSRic Aleshire 			free(ascii);
22944201a95eSRic Aleshire 		}
22954201a95eSRic Aleshire 		break;
22964201a95eSRic Aleshire 
2297f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2298f0f3ef5aSGarrett D'Amore 		/*
2299f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2300f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2301f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2302f0f3ef5aSGarrett D'Amore 		 */
2303f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2304f0f3ef5aSGarrett D'Amore 			return (-1);
2305f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2306f0f3ef5aSGarrett D'Amore 		break;
2307f0f3ef5aSGarrett D'Amore 
2308fa9e4066Sahrens 	default:
2309e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
231091ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2311e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2312e7437265Sahrens 			    &source, &val) != 0)
2313e7437265Sahrens 				return (-1);
2314e7437265Sahrens 			if (literal)
2315e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2316e7437265Sahrens 				    (u_longlong_t)val);
2317e7437265Sahrens 			else
2318e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2319e7437265Sahrens 			break;
2320e7437265Sahrens 
232191ebeef5Sahrens 		case PROP_TYPE_STRING:
2322e7437265Sahrens 			(void) strlcpy(propbuf,
2323e7437265Sahrens 			    getprop_string(zhp, prop, &source), proplen);
2324e7437265Sahrens 			break;
2325e7437265Sahrens 
232691ebeef5Sahrens 		case PROP_TYPE_INDEX:
2327fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2328fe192f76Sahrens 			    &source, &val) != 0)
2329fe192f76Sahrens 				return (-1);
2330fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2331e7437265Sahrens 				return (-1);
2332e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2333e7437265Sahrens 			break;
2334e7437265Sahrens 
2335e7437265Sahrens 		default:
233699653d4eSeschrock 			abort();
2337fa9e4066Sahrens 		}
2338e7437265Sahrens 	}
2339fa9e4066Sahrens 
2340fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2341fa9e4066Sahrens 
2342fa9e4066Sahrens 	return (0);
2343fa9e4066Sahrens }
2344fa9e4066Sahrens 
2345fa9e4066Sahrens /*
2346fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2347fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2348fa9e4066Sahrens  * hard-coded property types.
2349fa9e4066Sahrens  */
2350fa9e4066Sahrens uint64_t
2351fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2352fa9e4066Sahrens {
2353fa9e4066Sahrens 	char *source;
235499653d4eSeschrock 	uint64_t val;
2355fa9e4066Sahrens 
23563cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
235799653d4eSeschrock 
235899653d4eSeschrock 	return (val);
2359fa9e4066Sahrens }
2360fa9e4066Sahrens 
23617b97dc1aSrm160521 int
23627b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
23637b97dc1aSrm160521 {
23647b97dc1aSrm160521 	char buf[64];
23657b97dc1aSrm160521 
236614843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
23677b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
23687b97dc1aSrm160521 }
23697b97dc1aSrm160521 
2370fa9e4066Sahrens /*
2371fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2372fa9e4066Sahrens  */
2373fa9e4066Sahrens int
2374fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2375990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2376fa9e4066Sahrens {
2377fa9e4066Sahrens 	char *source;
2378fa9e4066Sahrens 
2379fa9e4066Sahrens 	/*
2380fa9e4066Sahrens 	 * Check to see if this property applies to our object
2381fa9e4066Sahrens 	 */
2382e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2383ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
238499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
238599653d4eSeschrock 		    zfs_prop_to_name(prop)));
2386e45ce728Sahrens 	}
2387fa9e4066Sahrens 
2388fa9e4066Sahrens 	if (src)
2389990b4856Slling 		*src = ZPROP_SRC_NONE;
2390fa9e4066Sahrens 
239199653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
239299653d4eSeschrock 		return (-1);
2393fa9e4066Sahrens 
2394fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2395fa9e4066Sahrens 
2396fa9e4066Sahrens 	return (0);
2397fa9e4066Sahrens }
2398fa9e4066Sahrens 
239914843421SMatthew Ahrens static int
240014843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
240114843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
240214843421SMatthew Ahrens {
240314843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
240414843421SMatthew Ahrens 	idmap_stat status;
240514843421SMatthew Ahrens 	int err = EINVAL;
240614843421SMatthew Ahrens 
24071fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
240814843421SMatthew Ahrens 		goto out;
240914843421SMatthew Ahrens 
241014843421SMatthew Ahrens 	if (isuser) {
241114843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
241214843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
241314843421SMatthew Ahrens 	} else {
241414843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
241514843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
241614843421SMatthew Ahrens 	}
241714843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
241814843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
241914843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
242014843421SMatthew Ahrens 		err = 0;
242114843421SMatthew Ahrens 	else
242214843421SMatthew Ahrens 		err = EINVAL;
242314843421SMatthew Ahrens out:
242414843421SMatthew Ahrens 	if (get_hdl)
242514843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
242614843421SMatthew Ahrens 	return (err);
242714843421SMatthew Ahrens }
242814843421SMatthew Ahrens 
242914843421SMatthew Ahrens /*
243014843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
243114843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
243214843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
243314843421SMatthew Ahrens  */
243414843421SMatthew Ahrens static int
243514843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
243614843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
243714843421SMatthew Ahrens {
243814843421SMatthew Ahrens 	zfs_userquota_prop_t type;
243914843421SMatthew Ahrens 	char *cp, *end;
24403b12c289SMatthew Ahrens 	char *numericsid = NULL;
244114843421SMatthew Ahrens 	boolean_t isuser;
244214843421SMatthew Ahrens 
244314843421SMatthew Ahrens 	domain[0] = '\0';
244414843421SMatthew Ahrens 
244514843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
244614843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
244714843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
244814843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
244914843421SMatthew Ahrens 			break;
245014843421SMatthew Ahrens 	}
245114843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
245214843421SMatthew Ahrens 		return (EINVAL);
245314843421SMatthew Ahrens 	*typep = type;
245414843421SMatthew Ahrens 
245514843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
245614843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
245714843421SMatthew Ahrens 
245814843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
245914843421SMatthew Ahrens 
246014843421SMatthew Ahrens 	if (strchr(cp, '@')) {
246114843421SMatthew Ahrens 		/*
246214843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
24633b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
246414843421SMatthew Ahrens 		 */
24653b12c289SMatthew Ahrens 		directory_error_t e;
246614843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
246714843421SMatthew Ahrens 			return (ENOENT);
24683b12c289SMatthew Ahrens 		if (isuser) {
24693b12c289SMatthew Ahrens 			e = directory_sid_from_user_name(NULL,
24703b12c289SMatthew Ahrens 			    cp, &numericsid);
24713b12c289SMatthew Ahrens 		} else {
24723b12c289SMatthew Ahrens 			e = directory_sid_from_group_name(NULL,
24733b12c289SMatthew Ahrens 			    cp, &numericsid);
24743b12c289SMatthew Ahrens 		}
24753b12c289SMatthew Ahrens 		if (e != NULL) {
24763b12c289SMatthew Ahrens 			directory_error_free(e);
247714843421SMatthew Ahrens 			return (ENOENT);
24783b12c289SMatthew Ahrens 		}
24793b12c289SMatthew Ahrens 		if (numericsid == NULL)
248014843421SMatthew Ahrens 			return (ENOENT);
24813b12c289SMatthew Ahrens 		cp = numericsid;
24823b12c289SMatthew Ahrens 		/* will be further decoded below */
24833b12c289SMatthew Ahrens 	}
24843b12c289SMatthew Ahrens 
24853b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
248614843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
24873b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
248814843421SMatthew Ahrens 		cp = strrchr(domain, '-');
248914843421SMatthew Ahrens 		*cp = '\0';
249014843421SMatthew Ahrens 		cp++;
249114843421SMatthew Ahrens 
249214843421SMatthew Ahrens 		errno = 0;
249314843421SMatthew Ahrens 		*ridp = strtoull(cp, &end, 10);
24943b12c289SMatthew Ahrens 		if (numericsid) {
24953b12c289SMatthew Ahrens 			free(numericsid);
24963b12c289SMatthew Ahrens 			numericsid = NULL;
24973b12c289SMatthew Ahrens 		}
2498777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
249914843421SMatthew Ahrens 			return (EINVAL);
250014843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
250114843421SMatthew Ahrens 		/*
250214843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
250314843421SMatthew Ahrens 		 * turned into a uid/gid
250414843421SMatthew Ahrens 		 */
250514843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
250614843421SMatthew Ahrens 			return (ENOENT);
250714843421SMatthew Ahrens 		if (isuser) {
250814843421SMatthew Ahrens 			struct passwd *pw;
250914843421SMatthew Ahrens 			pw = getpwnam(cp);
251014843421SMatthew Ahrens 			if (pw == NULL)
251114843421SMatthew Ahrens 				return (ENOENT);
251214843421SMatthew Ahrens 			*ridp = pw->pw_uid;
251314843421SMatthew Ahrens 		} else {
251414843421SMatthew Ahrens 			struct group *gr;
251514843421SMatthew Ahrens 			gr = getgrnam(cp);
251614843421SMatthew Ahrens 			if (gr == NULL)
251714843421SMatthew Ahrens 				return (ENOENT);
251814843421SMatthew Ahrens 			*ridp = gr->gr_gid;
251914843421SMatthew Ahrens 		}
252014843421SMatthew Ahrens 	} else {
252114843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
252214843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
252314843421SMatthew Ahrens 		idmap_rid_t rid;
252414843421SMatthew Ahrens 		char *mapdomain;
252514843421SMatthew Ahrens 
252614843421SMatthew Ahrens 		if (*end != '\0')
252714843421SMatthew Ahrens 			return (EINVAL);
252814843421SMatthew Ahrens 		if (id > MAXUID) {
252914843421SMatthew Ahrens 			/* It's an ephemeral ID. */
253014843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
253114843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
253214843421SMatthew Ahrens 				return (ENOENT);
25333b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
253414843421SMatthew Ahrens 			*ridp = rid;
253514843421SMatthew Ahrens 		} else {
253614843421SMatthew Ahrens 			*ridp = id;
253714843421SMatthew Ahrens 		}
253814843421SMatthew Ahrens 	}
253914843421SMatthew Ahrens 
25403b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
254114843421SMatthew Ahrens 	return (0);
254214843421SMatthew Ahrens }
254314843421SMatthew Ahrens 
2544edea4b55SLin Ling static int
2545edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2546edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
254714843421SMatthew Ahrens {
254814843421SMatthew Ahrens 	int err;
254914843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
255014843421SMatthew Ahrens 
255119b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
255214843421SMatthew Ahrens 
255314843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
255414843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2555edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2556edea4b55SLin Ling 	zc.zc_objset_type = *typep;
255714843421SMatthew Ahrens 	if (err)
255814843421SMatthew Ahrens 		return (err);
255914843421SMatthew Ahrens 
256014843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
256114843421SMatthew Ahrens 	if (err)
256214843421SMatthew Ahrens 		return (err);
256314843421SMatthew Ahrens 
2564edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2565edea4b55SLin Ling 	return (0);
2566edea4b55SLin Ling }
2567edea4b55SLin Ling 
2568edea4b55SLin Ling int
2569edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2570edea4b55SLin Ling     uint64_t *propvalue)
2571edea4b55SLin Ling {
2572edea4b55SLin Ling 	zfs_userquota_prop_t type;
2573edea4b55SLin Ling 
2574edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2575edea4b55SLin Ling 	    &type));
2576edea4b55SLin Ling }
2577edea4b55SLin Ling 
2578edea4b55SLin Ling int
2579edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2580edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2581edea4b55SLin Ling {
2582edea4b55SLin Ling 	int err;
2583edea4b55SLin Ling 	uint64_t propvalue;
2584edea4b55SLin Ling 	zfs_userquota_prop_t type;
2585edea4b55SLin Ling 
2586edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2587edea4b55SLin Ling 	    &type);
2588edea4b55SLin Ling 
2589edea4b55SLin Ling 	if (err)
2590edea4b55SLin Ling 		return (err);
2591edea4b55SLin Ling 
259214843421SMatthew Ahrens 	if (literal) {
2593edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2594edea4b55SLin Ling 	} else if (propvalue == 0 &&
259514843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
259614843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
259714843421SMatthew Ahrens 	} else {
2598edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
259914843421SMatthew Ahrens 	}
260014843421SMatthew Ahrens 	return (0);
260114843421SMatthew Ahrens }
260214843421SMatthew Ahrens 
260319b94df9SMatthew Ahrens int
260419b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
260519b94df9SMatthew Ahrens     uint64_t *propvalue)
260619b94df9SMatthew Ahrens {
260719b94df9SMatthew Ahrens 	int err;
260819b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
260919b94df9SMatthew Ahrens 	const char *snapname;
261019b94df9SMatthew Ahrens 
261119b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
261219b94df9SMatthew Ahrens 
261319b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
261419b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
261519b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
261619b94df9SMatthew Ahrens 	} else {
261719b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
261819b94df9SMatthew Ahrens 		char *cp;
261919b94df9SMatthew Ahrens 
262019b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
262119b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
262219b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
262319b94df9SMatthew Ahrens 		if (cp != NULL)
262419b94df9SMatthew Ahrens 			*cp = '\0';
262519b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
262619b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
262719b94df9SMatthew Ahrens 	}
262819b94df9SMatthew Ahrens 
262919b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
263019b94df9SMatthew Ahrens 	if (err)
263119b94df9SMatthew Ahrens 		return (err);
263219b94df9SMatthew Ahrens 
263319b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
263419b94df9SMatthew Ahrens 	return (0);
263519b94df9SMatthew Ahrens }
263619b94df9SMatthew Ahrens 
263719b94df9SMatthew Ahrens int
263819b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
263919b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
264019b94df9SMatthew Ahrens {
264119b94df9SMatthew Ahrens 	int err;
264219b94df9SMatthew Ahrens 	uint64_t propvalue;
264319b94df9SMatthew Ahrens 
264419b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
264519b94df9SMatthew Ahrens 
264619b94df9SMatthew Ahrens 	if (err)
264719b94df9SMatthew Ahrens 		return (err);
264819b94df9SMatthew Ahrens 
264919b94df9SMatthew Ahrens 	if (literal) {
265019b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
265119b94df9SMatthew Ahrens 	} else {
265219b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
265319b94df9SMatthew Ahrens 	}
265419b94df9SMatthew Ahrens 	return (0);
265519b94df9SMatthew Ahrens }
265619b94df9SMatthew Ahrens 
2657fa9e4066Sahrens /*
2658fa9e4066Sahrens  * Returns the name of the given zfs handle.
2659fa9e4066Sahrens  */
2660fa9e4066Sahrens const char *
2661fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
2662fa9e4066Sahrens {
2663fa9e4066Sahrens 	return (zhp->zfs_name);
2664fa9e4066Sahrens }
2665fa9e4066Sahrens 
2666fa9e4066Sahrens /*
2667fa9e4066Sahrens  * Returns the type of the given zfs handle.
2668fa9e4066Sahrens  */
2669fa9e4066Sahrens zfs_type_t
2670fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
2671fa9e4066Sahrens {
2672fa9e4066Sahrens 	return (zhp->zfs_type);
2673fa9e4066Sahrens }
2674fa9e4066Sahrens 
26757f7322feSeschrock /*
2676d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
2677d41c4376SMark J Musante  *
2678d41c4376SMark J Musante  * Needs to handle these cases:
2679d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2680d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2681d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
2682d41c4376SMark J Musante  */
2683d41c4376SMark J Musante static boolean_t
2684d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
2685d41c4376SMark J Musante {
2686d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
2687d41c4376SMark J Musante 
2688d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
2689d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
2690d41c4376SMark J Musante 		return (B_FALSE);
2691d41c4376SMark J Musante 
2692d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
2693d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2694d41c4376SMark J Musante }
2695d41c4376SMark J Musante 
2696d41c4376SMark J Musante /*
2697fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
269819b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
269919b94df9SMatthew Ahrens  * pool).
2700fa9e4066Sahrens  */
2701fa9e4066Sahrens static int
2702fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
2703fa9e4066Sahrens {
270419b94df9SMatthew Ahrens 	char *slashp;
2705fa9e4066Sahrens 
270619b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
270719b94df9SMatthew Ahrens 
270819b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
2709fa9e4066Sahrens 		return (-1);
271019b94df9SMatthew Ahrens 	*slashp = '\0';
2711fa9e4066Sahrens 
2712fa9e4066Sahrens 	return (0);
2713fa9e4066Sahrens }
2714fa9e4066Sahrens 
2715fa9e4066Sahrens /*
27167f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
27177f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
27187f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
27197f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
27207f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
27217f1f55eaSvb160487  * new datasets.
2722fa9e4066Sahrens  */
2723fa9e4066Sahrens static int
27247f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27257f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
2726fa9e4066Sahrens {
2727fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2728fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
2729fa9e4066Sahrens 	char *slash;
27307f7322feSeschrock 	zfs_handle_t *zhp;
273199653d4eSeschrock 	char errbuf[1024];
2732d41c4376SMark J Musante 	uint64_t is_zoned;
273399653d4eSeschrock 
2734deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
2735deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2736fa9e4066Sahrens 
2737fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
2738fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
273999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
274099653d4eSeschrock 		    "missing dataset name"));
274199653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2742fa9e4066Sahrens 	}
2743fa9e4066Sahrens 
2744fa9e4066Sahrens 	/* check to see if the pool exists */
2745fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2746fa9e4066Sahrens 		slash = parent + strlen(parent);
27478ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
2748fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
274999653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2750fa9e4066Sahrens 	    errno == ENOENT) {
275199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
275299653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
275399653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2754fa9e4066Sahrens 	}
2755fa9e4066Sahrens 
2756fa9e4066Sahrens 	/* check to see if the parent dataset exists */
27577f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27587f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
27597f1f55eaSvb160487 			/*
27607f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
27617f1f55eaSvb160487 			 */
27627f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27637f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27647f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
27657f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27667f1f55eaSvb160487 			}
27677f1f55eaSvb160487 		} else if (errno == ENOENT) {
276899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
276999653d4eSeschrock 			    "parent does not exist"));
277099653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27717f1f55eaSvb160487 		} else
277299653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2773fa9e4066Sahrens 	}
2774fa9e4066Sahrens 
2775d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2776d41c4376SMark J Musante 	if (zoned != NULL)
2777d41c4376SMark J Musante 		*zoned = is_zoned;
2778d41c4376SMark J Musante 
2779fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
2780d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
278199653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27827f7322feSeschrock 		zfs_close(zhp);
2783fa9e4066Sahrens 		return (-1);
2784fa9e4066Sahrens 	}
2785fa9e4066Sahrens 
2786fa9e4066Sahrens 	/* make sure parent is a filesystem */
27877f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
278899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
278999653d4eSeschrock 		    "parent is not a filesystem"));
279099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27917f7322feSeschrock 		zfs_close(zhp);
2792fa9e4066Sahrens 		return (-1);
2793fa9e4066Sahrens 	}
2794fa9e4066Sahrens 
27957f7322feSeschrock 	zfs_close(zhp);
27967f1f55eaSvb160487 	if (prefixlen != NULL)
27977f1f55eaSvb160487 		*prefixlen = strlen(parent);
27987f1f55eaSvb160487 	return (0);
27997f1f55eaSvb160487 }
28007f1f55eaSvb160487 
28017f1f55eaSvb160487 /*
28027f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
28037f1f55eaSvb160487  */
28047f1f55eaSvb160487 boolean_t
28057f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
28067f1f55eaSvb160487 {
28077f1f55eaSvb160487 	zfs_handle_t *zhp;
28087f1f55eaSvb160487 
2809f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
28107f1f55eaSvb160487 		return (B_FALSE);
28117f1f55eaSvb160487 
28127f1f55eaSvb160487 	/*
28137f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
28147f1f55eaSvb160487 	 */
28157f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
28167f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
28177f1f55eaSvb160487 
28187f1f55eaSvb160487 		zfs_close(zhp);
28197f1f55eaSvb160487 		if (types & ds_type)
28207f1f55eaSvb160487 			return (B_TRUE);
28217f1f55eaSvb160487 	}
28227f1f55eaSvb160487 	return (B_FALSE);
28237f1f55eaSvb160487 }
28247f1f55eaSvb160487 
28257f1f55eaSvb160487 /*
28263cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
28273cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
28283cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
28293cb34c60Sahrens  */
28303cb34c60Sahrens int
28313cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
28323cb34c60Sahrens {
28333cb34c60Sahrens 	zfs_handle_t *h;
28343cb34c60Sahrens 	char *cp;
28353cb34c60Sahrens 	const char *opname;
28363cb34c60Sahrens 
28373cb34c60Sahrens 	/* make sure prefix exists */
28383cb34c60Sahrens 	cp = target + prefixlen;
28393cb34c60Sahrens 	if (*cp != '/') {
28403cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
28413cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28423cb34c60Sahrens 	} else {
28433cb34c60Sahrens 		*cp = '\0';
28443cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28453cb34c60Sahrens 		*cp = '/';
28463cb34c60Sahrens 	}
28473cb34c60Sahrens 	if (h == NULL)
28483cb34c60Sahrens 		return (-1);
28493cb34c60Sahrens 	zfs_close(h);
28503cb34c60Sahrens 
28513cb34c60Sahrens 	/*
28523cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28533cb34c60Sahrens 	 * up to the prefixlen-long one.
28543cb34c60Sahrens 	 */
28553cb34c60Sahrens 	for (cp = target + prefixlen + 1;
28563cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28573cb34c60Sahrens 
28583cb34c60Sahrens 		*cp = '\0';
28593cb34c60Sahrens 
28603cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
28613cb34c60Sahrens 		if (h) {
28623cb34c60Sahrens 			/* it already exists, nothing to do here */
28633cb34c60Sahrens 			zfs_close(h);
28643cb34c60Sahrens 			continue;
28653cb34c60Sahrens 		}
28663cb34c60Sahrens 
28673cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28683cb34c60Sahrens 		    NULL) != 0) {
28693cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28703cb34c60Sahrens 			goto ancestorerr;
28713cb34c60Sahrens 		}
28723cb34c60Sahrens 
28733cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28743cb34c60Sahrens 		if (h == NULL) {
28753cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28763cb34c60Sahrens 			goto ancestorerr;
28773cb34c60Sahrens 		}
28783cb34c60Sahrens 
28793cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28803cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28813cb34c60Sahrens 			goto ancestorerr;
28823cb34c60Sahrens 		}
28833cb34c60Sahrens 
28843cb34c60Sahrens 		if (zfs_share(h) != 0) {
28853cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28863cb34c60Sahrens 			goto ancestorerr;
28873cb34c60Sahrens 		}
28883cb34c60Sahrens 
28893cb34c60Sahrens 		zfs_close(h);
28903cb34c60Sahrens 	}
28913cb34c60Sahrens 
28923cb34c60Sahrens 	return (0);
28933cb34c60Sahrens 
28943cb34c60Sahrens ancestorerr:
28953cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28963cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28973cb34c60Sahrens 	return (-1);
28983cb34c60Sahrens }
28993cb34c60Sahrens 
29003cb34c60Sahrens /*
29017f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
29027f1f55eaSvb160487  */
29037f1f55eaSvb160487 int
29047f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
29057f1f55eaSvb160487 {
29067f1f55eaSvb160487 	int prefix;
29077f1f55eaSvb160487 	char *path_copy;
29087f1f55eaSvb160487 	int rc;
29097f1f55eaSvb160487 
2910d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
29117f1f55eaSvb160487 		return (-1);
29127f1f55eaSvb160487 
29137f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
29147f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
29157f1f55eaSvb160487 		free(path_copy);
29167f1f55eaSvb160487 	}
29177f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
29187f1f55eaSvb160487 		return (-1);
29197f1f55eaSvb160487 
2920fa9e4066Sahrens 	return (0);
2921fa9e4066Sahrens }
2922fa9e4066Sahrens 
2923fa9e4066Sahrens /*
2924e9dbad6fSeschrock  * Create a new filesystem or volume.
2925fa9e4066Sahrens  */
2926fa9e4066Sahrens int
292799653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2928e9dbad6fSeschrock     nvlist_t *props)
2929fa9e4066Sahrens {
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;
29354445fffbSMatthew Ahrens 	dmu_objset_type_t ost;
293699653d4eSeschrock 
293799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
293899653d4eSeschrock 	    "cannot create '%s'"), path);
2939fa9e4066Sahrens 
2940fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
2941f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
294299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2943fa9e4066Sahrens 
2944fa9e4066Sahrens 	/* validate parents exist */
29457f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2946fa9e4066Sahrens 		return (-1);
2947fa9e4066Sahrens 
2948fa9e4066Sahrens 	/*
2949fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
2950fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
2951fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2952fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2953fa9e4066Sahrens 	 * first try to see if the dataset exists.
2954fa9e4066Sahrens 	 */
29554445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, 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)
29624445fffbSMatthew Ahrens 		ost = DMU_OST_ZVOL;
2963fa9e4066Sahrens 	else
29644445fffbSMatthew Ahrens 		ost = 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 
3016fa9e4066Sahrens 	/* create the dataset */
30174445fffbSMatthew Ahrens 	ret = lzc_create(path, ost, props);
30184445fffbSMatthew Ahrens 	nvlist_free(props);
3019e9dbad6fSeschrock 
3020fa9e4066Sahrens 	/* check for failure */
3021fa9e4066Sahrens 	if (ret != 0) {
3022fa9e4066Sahrens 		char parent[ZFS_MAXNAMELEN];
3023fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3024fa9e4066Sahrens 
3025fa9e4066Sahrens 		switch (errno) {
3026fa9e4066Sahrens 		case ENOENT:
302799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
302899653d4eSeschrock 			    "no such parent '%s'"), parent);
302999653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3030fa9e4066Sahrens 
3031fa9e4066Sahrens 		case EINVAL:
303299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3033d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
303499653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3035fa9e4066Sahrens 
3036fa9e4066Sahrens 		case EDOM:
303799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3038e9dbad6fSeschrock 			    "volume block size must be power of 2 from "
3039e9dbad6fSeschrock 			    "%u to %uk"),
3040fa9e4066Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3041fa9e4066Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
304299653d4eSeschrock 
3043e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
304499653d4eSeschrock 
304540feaa91Sahrens 		case ENOTSUP:
304640feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
304740feaa91Sahrens 			    "pool must be upgraded to set this "
304840feaa91Sahrens 			    "property or value"));
304940feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3050fa9e4066Sahrens #ifdef _ILP32
3051fa9e4066Sahrens 		case EOVERFLOW:
3052fa9e4066Sahrens 			/*
3053fa9e4066Sahrens 			 * This platform can't address a volume this big.
3054fa9e4066Sahrens 			 */
305599653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
305699653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
305799653d4eSeschrock 				    errbuf));
3058fa9e4066Sahrens #endif
305999653d4eSeschrock 			/* FALLTHROUGH */
3060fa9e4066Sahrens 		default:
306199653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3062fa9e4066Sahrens 		}
3063fa9e4066Sahrens 	}
3064fa9e4066Sahrens 
3065fa9e4066Sahrens 	return (0);
3066fa9e4066Sahrens }
3067fa9e4066Sahrens 
3068fa9e4066Sahrens /*
3069fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
307065fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
307165fec9f6SChristopher Siden  * does not exist this function does nothing.
3072fa9e4066Sahrens  */
3073fa9e4066Sahrens int
3074842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3075fa9e4066Sahrens {
3076fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3077fa9e4066Sahrens 
3078fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3079fa9e4066Sahrens 
3080e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3081fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3082fa9e4066Sahrens 	} else {
3083fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3084fa9e4066Sahrens 	}
3085fa9e4066Sahrens 
3086842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
308765fec9f6SChristopher Siden 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
308865fec9f6SChristopher Siden 	    errno != ENOENT) {
3089ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
309099653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
309199653d4eSeschrock 		    zhp->zfs_name));
30921d452cf5Sahrens 	}
3093fa9e4066Sahrens 
3094fa9e4066Sahrens 	remove_mountpoint(zhp);
3095fa9e4066Sahrens 
3096fa9e4066Sahrens 	return (0);
3097fa9e4066Sahrens }
3098fa9e4066Sahrens 
30991d452cf5Sahrens struct destroydata {
310019b94df9SMatthew Ahrens 	nvlist_t *nvl;
310119b94df9SMatthew Ahrens 	const char *snapname;
31021d452cf5Sahrens };
31031d452cf5Sahrens 
31041d452cf5Sahrens static int
3105681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
31061d452cf5Sahrens {
31071d452cf5Sahrens 	struct destroydata *dd = arg;
31081d452cf5Sahrens 	char name[ZFS_MAXNAMELEN];
3109681d9761SEric Taylor 	int rv = 0;
31101d452cf5Sahrens 
311119b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
311219b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
31131d452cf5Sahrens 
3114a7a845e4SSteven Hartland 	if (lzc_exists(name))
311519b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
31161d452cf5Sahrens 
311719b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
31183ccfa83cSahrens 	zfs_close(zhp);
31193ccfa83cSahrens 	return (rv);
31201d452cf5Sahrens }
31211d452cf5Sahrens 
31221d452cf5Sahrens /*
31231d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31241d452cf5Sahrens  */
31251d452cf5Sahrens int
3126842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
31271d452cf5Sahrens {
31281d452cf5Sahrens 	int ret;
31291d452cf5Sahrens 	struct destroydata dd = { 0 };
31301d452cf5Sahrens 
31311d452cf5Sahrens 	dd.snapname = snapname;
313219b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
313319b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
31341d452cf5Sahrens 
3135a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
313619b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31371d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
313819b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
313919b94df9SMatthew Ahrens 	} else {
31403b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
314119b94df9SMatthew Ahrens 	}
314219b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
314319b94df9SMatthew Ahrens 	return (ret);
3144e5351341SMatthew Ahrens }
3145e5351341SMatthew Ahrens 
314619b94df9SMatthew Ahrens /*
31473b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
314819b94df9SMatthew Ahrens  */
314919b94df9SMatthew Ahrens int
31503b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
315119b94df9SMatthew Ahrens {
315219b94df9SMatthew Ahrens 	int ret;
31534445fffbSMatthew Ahrens 	nvlist_t *errlist;
315419b94df9SMatthew Ahrens 
31554445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
31561d452cf5Sahrens 
31573b2aab18SMatthew Ahrens 	if (ret == 0)
31583b2aab18SMatthew Ahrens 		return (0);
31593b2aab18SMatthew Ahrens 
3160a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
31613b2aab18SMatthew Ahrens 		char errbuf[1024];
31623b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
31633b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
31643b2aab18SMatthew Ahrens 
31653b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
31663b2aab18SMatthew Ahrens 	}
31674445fffbSMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
31684445fffbSMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
31691d452cf5Sahrens 		char errbuf[1024];
31704445fffbSMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
31714445fffbSMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
31724445fffbSMatthew Ahrens 		    nvpair_name(pair));
31731d452cf5Sahrens 
31744445fffbSMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
31751d452cf5Sahrens 		case EEXIST:
31763b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
31773b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
31783b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
31794445fffbSMatthew Ahrens 			break;
31801d452cf5Sahrens 		default:
31813b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
31824445fffbSMatthew Ahrens 			break;
31834445fffbSMatthew Ahrens 		}
31841d452cf5Sahrens 	}
31851d452cf5Sahrens 
31864445fffbSMatthew Ahrens 	return (ret);
31871d452cf5Sahrens }
31881d452cf5Sahrens 
3189fa9e4066Sahrens /*
3190fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3191fa9e4066Sahrens  */
3192fa9e4066Sahrens int
3193e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3194fa9e4066Sahrens {
3195fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
3196fa9e4066Sahrens 	int ret;
319799653d4eSeschrock 	char errbuf[1024];
319899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3199e9dbad6fSeschrock 	uint64_t zoned;
3200fa9e4066Sahrens 
3201fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3202fa9e4066Sahrens 
320399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
320499653d4eSeschrock 	    "cannot create '%s'"), target);
320599653d4eSeschrock 
320619b94df9SMatthew Ahrens 	/* validate the target/clone name */
3207f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
320899653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3209fa9e4066Sahrens 
3210fa9e4066Sahrens 	/* validate parents exist */
32117f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3212fa9e4066Sahrens 		return (-1);
3213fa9e4066Sahrens 
3214fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3215fa9e4066Sahrens 
3216fa9e4066Sahrens 	/* do the clone */
3217e9dbad6fSeschrock 
3218e9dbad6fSeschrock 	if (props) {
32194445fffbSMatthew Ahrens 		zfs_type_t type;
32204445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
32214445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
32224445fffbSMatthew Ahrens 		} else {
32234445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
32244445fffbSMatthew Ahrens 		}
32250a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
32260a48a24eStimh 		    zhp, errbuf)) == NULL)
3227e9dbad6fSeschrock 			return (-1);
3228e9dbad6fSeschrock 	}
3229e9dbad6fSeschrock 
32304445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
3231e9dbad6fSeschrock 	nvlist_free(props);
3232e9dbad6fSeschrock 
3233fa9e4066Sahrens 	if (ret != 0) {
3234fa9e4066Sahrens 		switch (errno) {
3235fa9e4066Sahrens 
3236fa9e4066Sahrens 		case ENOENT:
3237fa9e4066Sahrens 			/*
3238fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3239fa9e4066Sahrens 			 * above, but there may a race condition that has since
3240fa9e4066Sahrens 			 * destroyed the parent.
3241fa9e4066Sahrens 			 *
3242fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3243fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3244fa9e4066Sahrens 			 * dataset doesn't exist.
3245fa9e4066Sahrens 			 */
324699653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
324799653d4eSeschrock 			    "no such parent '%s'"), parent);
324899653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3249fa9e4066Sahrens 
325099653d4eSeschrock 		case EXDEV:
325199653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
325299653d4eSeschrock 			    "source and target pools differ"));
325399653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
325499653d4eSeschrock 			    errbuf));
325599653d4eSeschrock 
325699653d4eSeschrock 		default:
325799653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
325899653d4eSeschrock 			    errbuf));
325999653d4eSeschrock 		}
326099653d4eSeschrock 	}
326199653d4eSeschrock 
326299653d4eSeschrock 	return (ret);
326399653d4eSeschrock }
326499653d4eSeschrock 
326599653d4eSeschrock /*
326699653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
326799653d4eSeschrock  */
326899653d4eSeschrock int
326999653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
327099653d4eSeschrock {
327199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
327299653d4eSeschrock 	zfs_cmd_t zc = { 0 };
327399653d4eSeschrock 	char parent[MAXPATHLEN];
327499653d4eSeschrock 	int ret;
327599653d4eSeschrock 	char errbuf[1024];
327699653d4eSeschrock 
327799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
327899653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
327999653d4eSeschrock 
328099653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
328199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
328299653d4eSeschrock 		    "snapshots can not be promoted"));
328399653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
328499653d4eSeschrock 	}
328599653d4eSeschrock 
32863cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
328799653d4eSeschrock 	if (parent[0] == '\0') {
328899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
328999653d4eSeschrock 		    "not a cloned filesystem"));
329099653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
329199653d4eSeschrock 	}
329299653d4eSeschrock 
32933cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3294e9dbad6fSeschrock 	    sizeof (zc.zc_value));
329599653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3296ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
329799653d4eSeschrock 
329899653d4eSeschrock 	if (ret != 0) {
32990b69c2f0Sahrens 		int save_errno = errno;
3300fa9e4066Sahrens 
33010b69c2f0Sahrens 		switch (save_errno) {
3302fa9e4066Sahrens 		case EEXIST:
3303681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
330499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3305681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3306681d9761SEric Taylor 			    zc.zc_string, parent);
330799653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3308fa9e4066Sahrens 
3309fa9e4066Sahrens 		default:
33100b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3311fa9e4066Sahrens 		}
3312fa9e4066Sahrens 	}
3313e9dbad6fSeschrock 	return (ret);
33141d452cf5Sahrens }
33151d452cf5Sahrens 
33164445fffbSMatthew Ahrens typedef struct snapdata {
33174445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
33184445fffbSMatthew Ahrens 	const char *sd_snapname;
33194445fffbSMatthew Ahrens } snapdata_t;
33204445fffbSMatthew Ahrens 
33214445fffbSMatthew Ahrens static int
33224445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
33234445fffbSMatthew Ahrens {
33244445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
33254445fffbSMatthew Ahrens 	char name[ZFS_MAXNAMELEN];
33264445fffbSMatthew Ahrens 	int rv = 0;
33274445fffbSMatthew Ahrens 
33284445fffbSMatthew Ahrens 	(void) snprintf(name, sizeof (name),
33294445fffbSMatthew Ahrens 	    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
33304445fffbSMatthew Ahrens 
33314445fffbSMatthew Ahrens 	fnvlist_add_boolean(sd->sd_nvl, name);
33324445fffbSMatthew Ahrens 
33334445fffbSMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
33344445fffbSMatthew Ahrens 	zfs_close(zhp);
33354445fffbSMatthew Ahrens 	return (rv);
33364445fffbSMatthew Ahrens }
33374445fffbSMatthew Ahrens 
3338fa9e4066Sahrens /*
33394445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
33404445fffbSMatthew Ahrens  * created.
3341fa9e4066Sahrens  */
3342fa9e4066Sahrens int
33434445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
33444445fffbSMatthew Ahrens {
33454445fffbSMatthew Ahrens 	int ret;
33464445fffbSMatthew Ahrens 	char errbuf[1024];
33474445fffbSMatthew Ahrens 	nvpair_t *elem;
33484445fffbSMatthew Ahrens 	nvlist_t *errors;
33494445fffbSMatthew Ahrens 
33504445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33514445fffbSMatthew Ahrens 	    "cannot create snapshots "));
33524445fffbSMatthew Ahrens 
33534445fffbSMatthew Ahrens 	elem = NULL;
33544445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
33554445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
33564445fffbSMatthew Ahrens 
33574445fffbSMatthew Ahrens 		/* validate the target name */
33584445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
33594445fffbSMatthew Ahrens 		    B_TRUE)) {
33604445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
33614445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
33624445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
33634445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
33644445fffbSMatthew Ahrens 		}
33654445fffbSMatthew Ahrens 	}
33664445fffbSMatthew Ahrens 
33674445fffbSMatthew Ahrens 	if (props != NULL &&
33684445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
33694445fffbSMatthew Ahrens 	    props, B_FALSE, NULL, errbuf)) == NULL) {
33704445fffbSMatthew Ahrens 		return (-1);
33714445fffbSMatthew Ahrens 	}
33724445fffbSMatthew Ahrens 
33734445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
33744445fffbSMatthew Ahrens 
33754445fffbSMatthew Ahrens 	if (ret != 0) {
33764445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
33774445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
33784445fffbSMatthew Ahrens 		    elem != NULL;
33794445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
33804445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
33814445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
33824445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
33834445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
33844445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
33854445fffbSMatthew Ahrens 			printed = B_TRUE;
33864445fffbSMatthew Ahrens 		}
33874445fffbSMatthew Ahrens 		if (!printed) {
33884445fffbSMatthew Ahrens 			switch (ret) {
33894445fffbSMatthew Ahrens 			case EXDEV:
33904445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33914445fffbSMatthew Ahrens 				    "multiple snapshots of same "
33924445fffbSMatthew Ahrens 				    "fs not allowed"));
33934445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
33944445fffbSMatthew Ahrens 
33954445fffbSMatthew Ahrens 				break;
33964445fffbSMatthew Ahrens 			default:
33974445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
33984445fffbSMatthew Ahrens 			}
33994445fffbSMatthew Ahrens 		}
34004445fffbSMatthew Ahrens 	}
34014445fffbSMatthew Ahrens 
34024445fffbSMatthew Ahrens 	nvlist_free(props);
34034445fffbSMatthew Ahrens 	nvlist_free(errors);
34044445fffbSMatthew Ahrens 	return (ret);
34054445fffbSMatthew Ahrens }
34064445fffbSMatthew Ahrens 
34074445fffbSMatthew Ahrens int
3408bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3409bb0ade09Sahrens     nvlist_t *props)
3410fa9e4066Sahrens {
3411fa9e4066Sahrens 	int ret;
34124445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
34134445fffbSMatthew Ahrens 	char fsname[ZFS_MAXNAMELEN];
34144445fffbSMatthew Ahrens 	char *cp;
34154445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
341699653d4eSeschrock 	char errbuf[1024];
3417fa9e4066Sahrens 
341899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34194445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
342099653d4eSeschrock 
3421f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
342299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3423fa9e4066Sahrens 
34244445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
34254445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
34264445fffbSMatthew Ahrens 	*cp = '\0';
34274445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
3428bb0ade09Sahrens 
34294445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3430fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3431fa9e4066Sahrens 		return (-1);
3432fa9e4066Sahrens 	}
3433fa9e4066Sahrens 
34344445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
34354445fffbSMatthew Ahrens 	if (recursive) {
34364445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
34374445fffbSMatthew Ahrens 	} else {
34384445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
3439681d9761SEric Taylor 	}
3440fa9e4066Sahrens 
34414445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
34424445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
3443fa9e4066Sahrens 	zfs_close(zhp);
3444fa9e4066Sahrens 	return (ret);
3445fa9e4066Sahrens }
3446fa9e4066Sahrens 
3447fa9e4066Sahrens /*
3448b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3449b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3450b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3451b12a1c38Slling  * group.
3452fa9e4066Sahrens  */
3453b12a1c38Slling typedef struct rollback_data {
3454b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3455b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3456c391e322Sahrens 	boolean_t	cb_error;
345799653d4eSeschrock 	boolean_t	cb_dependent;
3458c391e322Sahrens 	boolean_t	cb_force;
3459b12a1c38Slling } rollback_data_t;
3460b12a1c38Slling 
3461b12a1c38Slling static int
3462b12a1c38Slling rollback_destroy(zfs_handle_t *zhp, void *data)
3463b12a1c38Slling {
3464b12a1c38Slling 	rollback_data_t *cbp = data;
3465b12a1c38Slling 
3466b12a1c38Slling 	if (!cbp->cb_dependent) {
3467b12a1c38Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3468b12a1c38Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3469b12a1c38Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3470b12a1c38Slling 		    cbp->cb_create) {
3471b12a1c38Slling 
347299653d4eSeschrock 			cbp->cb_dependent = B_TRUE;
34734ccbb6e7Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
34744ccbb6e7Sahrens 			    rollback_destroy, cbp);
347599653d4eSeschrock 			cbp->cb_dependent = B_FALSE;
3476b12a1c38Slling 
3477842727c2SChris Kirby 			cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3478b12a1c38Slling 		}
3479b12a1c38Slling 	} else {
3480c391e322Sahrens 		/* We must destroy this clone; first unmount it */
3481c391e322Sahrens 		prop_changelist_t *clp;
3482c391e322Sahrens 
34830069fd67STim Haley 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3484c391e322Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
3485c391e322Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
3486c391e322Sahrens 			cbp->cb_error = B_TRUE;
3487c391e322Sahrens 			zfs_close(zhp);
3488c391e322Sahrens 			return (0);
3489c391e322Sahrens 		}
3490842727c2SChris Kirby 		if (zfs_destroy(zhp, B_FALSE) != 0)
3491c391e322Sahrens 			cbp->cb_error = B_TRUE;
3492c391e322Sahrens 		else
3493c391e322Sahrens 			changelist_remove(clp, zhp->zfs_name);
3494ba7b046eSahrens 		(void) changelist_postfix(clp);
3495c391e322Sahrens 		changelist_free(clp);
3496b12a1c38Slling 	}
3497b12a1c38Slling 
3498b12a1c38Slling 	zfs_close(zhp);
3499b12a1c38Slling 	return (0);
3500b12a1c38Slling }
3501b12a1c38Slling 
3502b12a1c38Slling /*
35034ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35044ccbb6e7Sahrens  * data changes since then and making it the active dataset.
35054ccbb6e7Sahrens  *
35064ccbb6e7Sahrens  * Any snapshots more recent than the target are destroyed, along with
35074ccbb6e7Sahrens  * their dependents.
3508b12a1c38Slling  */
35094ccbb6e7Sahrens int
3510c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3511fa9e4066Sahrens {
35124ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
35134ccbb6e7Sahrens 	int err;
3514fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
35157b97dc1aSrm160521 	boolean_t restore_resv = 0;
35167b97dc1aSrm160521 	uint64_t old_volsize, new_volsize;
35177b97dc1aSrm160521 	zfs_prop_t resv_prop;
3518fa9e4066Sahrens 
3519fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3520fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3521fa9e4066Sahrens 
35224ccbb6e7Sahrens 	/*
35232e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
35244ccbb6e7Sahrens 	 */
3525c391e322Sahrens 	cb.cb_force = force;
35264ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
35274ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
35284ccbb6e7Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
35294ccbb6e7Sahrens 
3530c391e322Sahrens 	if (cb.cb_error)
3531c391e322Sahrens 		return (-1);
35324ccbb6e7Sahrens 
35334ccbb6e7Sahrens 	/*
35344ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
35354ccbb6e7Sahrens 	 * rollback to the given snapshot.
35364ccbb6e7Sahrens 	 */
35374ccbb6e7Sahrens 
35387b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
35397b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
35407b97dc1aSrm160521 			return (-1);
35417b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35427b97dc1aSrm160521 		restore_resv =
35437b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
35447b97dc1aSrm160521 	}
3545fa9e4066Sahrens 
3546fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3547fa9e4066Sahrens 
3548e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3549fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3550fa9e4066Sahrens 	else
3551fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3552fa9e4066Sahrens 
3553fa9e4066Sahrens 	/*
35544ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
35554ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
35564ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
35574ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
35584ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
35597b97dc1aSrm160521 	 *
3560fa9e4066Sahrens 	 */
35614ccbb6e7Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3562ece3d9b3Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
356399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
356499653d4eSeschrock 		    zhp->zfs_name);
3565b9415e83Srm160521 		return (err);
3566b9415e83Srm160521 	}
3567fa9e4066Sahrens 
35687b97dc1aSrm160521 	/*
35697b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
35707b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
35717b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
35727b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
35737b97dc1aSrm160521 	 */
3574b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3575b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
35767b97dc1aSrm160521 		if (restore_resv) {
35777b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35787b97dc1aSrm160521 			if (old_volsize != new_volsize)
3579b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
3580b9415e83Srm160521 				    new_volsize);
35817b97dc1aSrm160521 		}
35827b97dc1aSrm160521 		zfs_close(zhp);
35837b97dc1aSrm160521 	}
35844ccbb6e7Sahrens 	return (err);
3585b12a1c38Slling }
3586b12a1c38Slling 
3587b12a1c38Slling /*
3588fa9e4066Sahrens  * Renames the given dataset.
3589fa9e4066Sahrens  */
3590fa9e4066Sahrens int
35916a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
35926a9cb0eaSEric Schrock     boolean_t force_unmount)
3593fa9e4066Sahrens {
3594fa9e4066Sahrens 	int ret;
3595fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3596fa9e4066Sahrens 	char *delim;
3597cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3598cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3599cdf5b4caSmmusante 	char *parentname = NULL;
3600fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
360199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
360299653d4eSeschrock 	char errbuf[1024];
3603fa9e4066Sahrens 
3604fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3605fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3606fa9e4066Sahrens 		return (0);
3607fa9e4066Sahrens 
360899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
360999653d4eSeschrock 	    "cannot rename to '%s'"), target);
361099653d4eSeschrock 
3611fa9e4066Sahrens 	/*
3612fa9e4066Sahrens 	 * Make sure the target name is valid
3613fa9e4066Sahrens 	 */
3614fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
361598579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
361698579b20Snd150628 		    *target == '@') {
361798579b20Snd150628 			/*
361898579b20Snd150628 			 * Snapshot target name is abbreviated,
361998579b20Snd150628 			 * reconstruct full dataset name
362098579b20Snd150628 			 */
362198579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
362298579b20Snd150628 			    sizeof (parent));
362398579b20Snd150628 			delim = strchr(parent, '@');
362498579b20Snd150628 			if (strchr(target, '@') == NULL)
362598579b20Snd150628 				*(++delim) = '\0';
362698579b20Snd150628 			else
362798579b20Snd150628 				*delim = '\0';
362898579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
362998579b20Snd150628 			target = parent;
363098579b20Snd150628 		} else {
3631fa9e4066Sahrens 			/*
3632fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
3633fa9e4066Sahrens 			 */
363498579b20Snd150628 			delim = strchr(target, '@');
363598579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
363698579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
363799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
363898579b20Snd150628 				    "snapshots must be part of same "
363998579b20Snd150628 				    "dataset"));
364098579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
364198579b20Snd150628 				    errbuf));
3642fa9e4066Sahrens 			}
364398579b20Snd150628 		}
3644f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
364598579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3646fa9e4066Sahrens 	} else {
3647cdf5b4caSmmusante 		if (recursive) {
3648cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3649cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
3650cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3651cdf5b4caSmmusante 		}
3652cdf5b4caSmmusante 
3653f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
365498579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3655e9dbad6fSeschrock 
3656fa9e4066Sahrens 		/* validate parents */
3657d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3658fa9e4066Sahrens 			return (-1);
3659fa9e4066Sahrens 
3660fa9e4066Sahrens 		/* make sure we're in the same pool */
3661fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3662fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3663fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
366499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
366599653d4eSeschrock 			    "datasets must be within same pool"));
366699653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3667fa9e4066Sahrens 		}
3668f2fdf992Snd150628 
3669f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
3670d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
3671f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3672d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
3673f2fdf992Snd150628 			    "current dataset name"));
3674f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3675f2fdf992Snd150628 		}
3676fa9e4066Sahrens 	}
3677fa9e4066Sahrens 
367899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
367999653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
368099653d4eSeschrock 
3681fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3682fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
368399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
368499653d4eSeschrock 		    "dataset is used in a non-global zone"));
368599653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3686fa9e4066Sahrens 	}
3687fa9e4066Sahrens 
3688cdf5b4caSmmusante 	if (recursive) {
3689cdf5b4caSmmusante 
3690f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3691f0c5ee21Smmusante 		if (parentname == NULL) {
3692f0c5ee21Smmusante 			ret = -1;
3693f0c5ee21Smmusante 			goto error;
3694f0c5ee21Smmusante 		}
3695cdf5b4caSmmusante 		delim = strchr(parentname, '@');
3696cdf5b4caSmmusante 		*delim = '\0';
3697990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3698cdf5b4caSmmusante 		if (zhrp == NULL) {
3699f0c5ee21Smmusante 			ret = -1;
3700f0c5ee21Smmusante 			goto error;
3701cdf5b4caSmmusante 		}
3702cdf5b4caSmmusante 
3703cdf5b4caSmmusante 	} else {
37046a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
37056a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
370699653d4eSeschrock 			return (-1);
3707fa9e4066Sahrens 
3708fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
370999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
371099653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
371199653d4eSeschrock 			    "in a non-global zone"));
3712e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3713fa9e4066Sahrens 			goto error;
3714fa9e4066Sahrens 		}
3715fa9e4066Sahrens 
3716fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
3717fa9e4066Sahrens 			goto error;
3718cdf5b4caSmmusante 	}
3719fa9e4066Sahrens 
3720e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3721fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3722fa9e4066Sahrens 	else
3723fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3724fa9e4066Sahrens 
372598579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3726e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
372798579b20Snd150628 
3728cdf5b4caSmmusante 	zc.zc_cookie = recursive;
3729cdf5b4caSmmusante 
3730ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3731cdf5b4caSmmusante 		/*
3732cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
3733cdf5b4caSmmusante 		 * be in zc.zc_name
3734cdf5b4caSmmusante 		 */
3735cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37363cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
3737cdf5b4caSmmusante 
3738cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
3739cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3740cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
3741cdf5b4caSmmusante 			    "with the new name"));
3742a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3743cdf5b4caSmmusante 		} else {
374499653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3745cdf5b4caSmmusante 		}
3746fa9e4066Sahrens 
3747fa9e4066Sahrens 		/*
3748fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
3749fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
3750fa9e4066Sahrens 		 */
3751681d9761SEric Taylor 		if (!recursive)
3752fa9e4066Sahrens 			(void) changelist_postfix(cl);
3753cdf5b4caSmmusante 	} else {
3754681d9761SEric Taylor 		if (!recursive) {
3755fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
3756fa9e4066Sahrens 			ret = changelist_postfix(cl);
3757fa9e4066Sahrens 		}
3758cdf5b4caSmmusante 	}
3759fa9e4066Sahrens 
3760fa9e4066Sahrens error:
3761cdf5b4caSmmusante 	if (parentname) {
3762cdf5b4caSmmusante 		free(parentname);
3763cdf5b4caSmmusante 	}
3764cdf5b4caSmmusante 	if (zhrp) {
3765cdf5b4caSmmusante 		zfs_close(zhrp);
3766cdf5b4caSmmusante 	}
3767cdf5b4caSmmusante 	if (cl) {
3768fa9e4066Sahrens 		changelist_free(cl);
3769cdf5b4caSmmusante 	}
3770fa9e4066Sahrens 	return (ret);
3771fa9e4066Sahrens }
3772fa9e4066Sahrens 
3773e9dbad6fSeschrock nvlist_t *
3774e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
3775e9dbad6fSeschrock {
3776e9dbad6fSeschrock 	return (zhp->zfs_user_props);
3777e9dbad6fSeschrock }
3778e9dbad6fSeschrock 
377992241e0bSTom Erickson nvlist_t *
378092241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
378192241e0bSTom Erickson {
378292241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
378392241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
378492241e0bSTom Erickson 			return (NULL);
378592241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
378692241e0bSTom Erickson }
378792241e0bSTom Erickson 
3788e9dbad6fSeschrock /*
3789e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
3790e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
3791e9dbad6fSeschrock  *
3792e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
3793e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
3794e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
3795e9dbad6fSeschrock  *
3796e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
379792241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
379892241e0bSTom Erickson  *        requested received property values, we also need to compute the width
379992241e0bSTom Erickson  *        of the RECEIVED column.
3800e9dbad6fSeschrock  */
3801e9dbad6fSeschrock int
380292241e0bSTom Erickson zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3803e9dbad6fSeschrock {
3804e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3805990b4856Slling 	zprop_list_t *entry;
3806990b4856Slling 	zprop_list_t **last, **start;
3807e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
3808e9dbad6fSeschrock 	nvpair_t *elem;
3809e9dbad6fSeschrock 	char *strval;
3810e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
3811e9dbad6fSeschrock 
3812990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3813e9dbad6fSeschrock 		return (-1);
3814e9dbad6fSeschrock 
3815e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
3816e9dbad6fSeschrock 
3817e9dbad6fSeschrock 	entry = *plp;
3818e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3819e9dbad6fSeschrock 		/*
3820e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
3821e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
3822e9dbad6fSeschrock 		 * non-native property.
3823e9dbad6fSeschrock 		 */
3824e9dbad6fSeschrock 		start = plp;
3825e9dbad6fSeschrock 		while (*start != NULL) {
3826990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
3827e9dbad6fSeschrock 				break;
3828e9dbad6fSeschrock 			start = &(*start)->pl_next;
3829e9dbad6fSeschrock 		}
3830e9dbad6fSeschrock 
3831e9dbad6fSeschrock 		elem = NULL;
3832e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3833e9dbad6fSeschrock 			/*
3834e9dbad6fSeschrock 			 * See if we've already found this property in our list.
3835e9dbad6fSeschrock 			 */
3836e9dbad6fSeschrock 			for (last = start; *last != NULL;
3837e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
3838e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
3839e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
3840e9dbad6fSeschrock 					break;
3841e9dbad6fSeschrock 			}
3842e9dbad6fSeschrock 
3843e9dbad6fSeschrock 			if (*last == NULL) {
3844e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
3845990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
3846e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
3847e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
3848e9dbad6fSeschrock 					free(entry);
3849e9dbad6fSeschrock 					return (-1);
3850e9dbad6fSeschrock 				}
3851e9dbad6fSeschrock 
3852990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
3853e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
3854e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
3855e9dbad6fSeschrock 				*last = entry;
3856e9dbad6fSeschrock 			}
3857e9dbad6fSeschrock 		}
3858e9dbad6fSeschrock 	}
3859e9dbad6fSeschrock 
3860e9dbad6fSeschrock 	/*
3861e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
3862e9dbad6fSeschrock 	 */
3863e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3864e9dbad6fSeschrock 		if (entry->pl_fixed)
3865e9dbad6fSeschrock 			continue;
3866e9dbad6fSeschrock 
3867990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
3868e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
3869e9dbad6fSeschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3870e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
3871e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
3872e9dbad6fSeschrock 			}
387392241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
387492241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
387592241e0bSTom Erickson 			    buf, sizeof (buf), B_FALSE) == 0)
387692241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
387792241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
387892241e0bSTom Erickson 		} else {
387992241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
388092241e0bSTom Erickson 			    &propval) == 0) {
3881e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
3882990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
3883e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
3884e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
3885e9dbad6fSeschrock 			}
388692241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
388792241e0bSTom Erickson 			    entry->pl_user_prop,
388892241e0bSTom Erickson 			    buf, sizeof (buf), B_FALSE) == 0)
388992241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
389092241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
389192241e0bSTom Erickson 		}
3892e9dbad6fSeschrock 	}
3893e9dbad6fSeschrock 
3894e9dbad6fSeschrock 	return (0);
3895e9dbad6fSeschrock }
3896ecd6cf80Smarks 
3897ecd6cf80Smarks int
3898ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3899743a77edSAlan Wright     char *resource, void *export, void *sharetab,
3900743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
3901ecd6cf80Smarks {
3902ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
3903ecd6cf80Smarks 	int error;
3904ecd6cf80Smarks 
3905ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3906ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3907743a77edSAlan Wright 	if (resource)
3908743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3909ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3910ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3911da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
3912ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
3913ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3914ecd6cf80Smarks 	return (error);
3915ecd6cf80Smarks }
39162e5e9e19SSanjeev Bagewadi 
39172e5e9e19SSanjeev Bagewadi void
39182e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
39192e5e9e19SSanjeev Bagewadi {
39202e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
39212e5e9e19SSanjeev Bagewadi 
39222e5e9e19SSanjeev Bagewadi 	/*
39232e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
39242e5e9e19SSanjeev Bagewadi 	 * properties.
39252e5e9e19SSanjeev Bagewadi 	 */
39262e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
39272e5e9e19SSanjeev Bagewadi 
39282e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
39292e5e9e19SSanjeev Bagewadi 
39302e5e9e19SSanjeev Bagewadi 	while (curr) {
39312e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
39322e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
39332e5e9e19SSanjeev Bagewadi 
393414843421SMatthew Ahrens 		/*
3935faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
3936faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
3937faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
3938faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
3939faaa6415SEric Schrock 		 * software, for example).
394014843421SMatthew Ahrens 		 */
394114843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
39422e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
39432e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
39442e5e9e19SSanjeev Bagewadi 		curr = next;
39452e5e9e19SSanjeev Bagewadi 	}
39462e5e9e19SSanjeev Bagewadi }
3947743a77edSAlan Wright 
3948743a77edSAlan Wright static int
3949743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3950743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3951743a77edSAlan Wright {
3952743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
3953743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
3954743a77edSAlan Wright 	int error;
3955743a77edSAlan Wright 
3956743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3957743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3958743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
3959743a77edSAlan Wright 
3960743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
3961743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3962743a77edSAlan Wright 			(void) no_memory(hdl);
3963743a77edSAlan Wright 			return (NULL);
3964743a77edSAlan Wright 		}
3965743a77edSAlan Wright 	}
3966743a77edSAlan Wright 
3967743a77edSAlan Wright 	switch (cmd) {
3968743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
3969743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
3970743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3971743a77edSAlan Wright 		break;
3972743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
3973743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3974743a77edSAlan Wright 		    resource1) != 0) {
3975743a77edSAlan Wright 				(void) no_memory(hdl);
3976743a77edSAlan Wright 				return (-1);
3977743a77edSAlan Wright 		}
3978743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3979743a77edSAlan Wright 		    resource2) != 0) {
3980743a77edSAlan Wright 				(void) no_memory(hdl);
3981743a77edSAlan Wright 				return (-1);
3982743a77edSAlan Wright 		}
3983743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3984743a77edSAlan Wright 			nvlist_free(nvlist);
3985743a77edSAlan Wright 			return (-1);
3986743a77edSAlan Wright 		}
3987743a77edSAlan Wright 		break;
3988743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
3989743a77edSAlan Wright 		break;
3990743a77edSAlan Wright 	default:
3991743a77edSAlan Wright 		return (-1);
3992743a77edSAlan Wright 	}
3993743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3994743a77edSAlan Wright 	if (nvlist)
3995743a77edSAlan Wright 		nvlist_free(nvlist);
3996743a77edSAlan Wright 	return (error);
3997743a77edSAlan Wright }
3998743a77edSAlan Wright 
3999743a77edSAlan Wright int
4000743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4001743a77edSAlan Wright     char *path, char *resource)
4002743a77edSAlan Wright {
4003743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4004743a77edSAlan Wright 	    resource, NULL));
4005743a77edSAlan Wright }
4006743a77edSAlan Wright 
4007743a77edSAlan Wright int
4008743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4009743a77edSAlan Wright     char *path, char *resource)
4010743a77edSAlan Wright {
4011743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4012743a77edSAlan Wright 	    resource, NULL));
4013743a77edSAlan Wright }
4014743a77edSAlan Wright 
4015743a77edSAlan Wright int
4016743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4017743a77edSAlan Wright {
4018743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4019743a77edSAlan Wright 	    NULL, NULL));
4020743a77edSAlan Wright }
4021743a77edSAlan Wright 
4022743a77edSAlan Wright int
4023743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4024743a77edSAlan Wright     char *oldname, char *newname)
4025743a77edSAlan Wright {
4026743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4027743a77edSAlan Wright 	    oldname, newname));
4028743a77edSAlan Wright }
402914843421SMatthew Ahrens 
403014843421SMatthew Ahrens int
403114843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
403214843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
403314843421SMatthew Ahrens {
403414843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
403514843421SMatthew Ahrens 	zfs_useracct_t buf[100];
403670f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
403770f56fa6SYuri Pankov 	int ret;
403814843421SMatthew Ahrens 
403919b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
404014843421SMatthew Ahrens 
404114843421SMatthew Ahrens 	zc.zc_objset_type = type;
404214843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
404314843421SMatthew Ahrens 
404470f56fa6SYuri Pankov 	for (;;) {
404514843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
404614843421SMatthew Ahrens 
404714843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
404870f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
40493b2aab18SMatthew Ahrens 			char errbuf[1024];
405070f56fa6SYuri Pankov 
405170f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
405270f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
405370f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
405470f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
405570f56fa6SYuri Pankov 		}
405670f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
405714843421SMatthew Ahrens 			break;
405814843421SMatthew Ahrens 
405914843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
406070f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
406170f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
406270f56fa6SYuri Pankov 				return (ret);
406314843421SMatthew Ahrens 			zua++;
406414843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
406514843421SMatthew Ahrens 		}
406614843421SMatthew Ahrens 	}
406714843421SMatthew Ahrens 
406870f56fa6SYuri Pankov 	return (0);
406914843421SMatthew Ahrens }
4070842727c2SChris Kirby 
40713b2aab18SMatthew Ahrens struct holdarg {
40723b2aab18SMatthew Ahrens 	nvlist_t *nvl;
40733b2aab18SMatthew Ahrens 	const char *snapname;
40743b2aab18SMatthew Ahrens 	const char *tag;
40753b2aab18SMatthew Ahrens 	boolean_t recursive;
4076*bb6e7075SMatthew Ahrens 	int error;
40773b2aab18SMatthew Ahrens };
40783b2aab18SMatthew Ahrens 
40793b2aab18SMatthew Ahrens static int
40803b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
40813b2aab18SMatthew Ahrens {
40823b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
40833b2aab18SMatthew Ahrens 	char name[ZFS_MAXNAMELEN];
40843b2aab18SMatthew Ahrens 	int rv = 0;
40853b2aab18SMatthew Ahrens 
40863b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
40873b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
40883b2aab18SMatthew Ahrens 
4089a7a845e4SSteven Hartland 	if (lzc_exists(name))
40903b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
40913b2aab18SMatthew Ahrens 
40923b2aab18SMatthew Ahrens 	if (ha->recursive)
40933b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
40943b2aab18SMatthew Ahrens 	zfs_close(zhp);
40953b2aab18SMatthew Ahrens 	return (rv);
40963b2aab18SMatthew Ahrens }
40973b2aab18SMatthew Ahrens 
4098842727c2SChris Kirby int
4099842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4100a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4101842727c2SChris Kirby {
41023b2aab18SMatthew Ahrens 	int ret;
41033b2aab18SMatthew Ahrens 	struct holdarg ha;
4104842727c2SChris Kirby 
41053b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
41063b2aab18SMatthew Ahrens 	ha.snapname = snapname;
41073b2aab18SMatthew Ahrens 	ha.tag = tag;
41083b2aab18SMatthew Ahrens 	ha.recursive = recursive;
41093b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4110013023d4SMartin Matuska 
4111a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4112a7a845e4SSteven Hartland 		char errbuf[1024];
4113a7a845e4SSteven Hartland 
4114013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4115013023d4SMartin Matuska 		ret = ENOENT;
4116013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4117013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4118013023d4SMartin Matuska 		    "cannot hold snapshot '%s@%s'"),
4119013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4120a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4121013023d4SMartin Matuska 		return (ret);
4122013023d4SMartin Matuska 	}
4123013023d4SMartin Matuska 
4124a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
41253b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4126a7f53a56SChris Kirby 
4127a7a845e4SSteven Hartland 	return (ret);
4128a7a845e4SSteven Hartland }
4129842727c2SChris Kirby 
4130a7a845e4SSteven Hartland int
4131a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4132a7a845e4SSteven Hartland {
4133a7a845e4SSteven Hartland 	int ret;
4134a7a845e4SSteven Hartland 	nvlist_t *errors;
4135a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4136a7a845e4SSteven Hartland 	char errbuf[1024];
4137a7a845e4SSteven Hartland 	nvpair_t *elem;
4138a7a845e4SSteven Hartland 
4139a7a845e4SSteven Hartland 	errors = NULL;
4140a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4141a7a845e4SSteven Hartland 
4142a7a845e4SSteven Hartland 	if (ret == 0) {
4143a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4144a7a845e4SSteven Hartland 		fnvlist_free(errors);
4145a7a845e4SSteven Hartland 		return (0);
4146a7a845e4SSteven Hartland 	}
4147a7a845e4SSteven Hartland 
4148a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
41493b2aab18SMatthew Ahrens 		/* no hold-specific errors */
41503b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
41513b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
41523b2aab18SMatthew Ahrens 		switch (ret) {
41533b2aab18SMatthew Ahrens 		case ENOTSUP:
41543b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41553b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
41563b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
41573b2aab18SMatthew Ahrens 			break;
41583b2aab18SMatthew Ahrens 		case EINVAL:
41593b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
41603b2aab18SMatthew Ahrens 			break;
41613b2aab18SMatthew Ahrens 		default:
41623b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
41633b2aab18SMatthew Ahrens 		}
41643b2aab18SMatthew Ahrens 	}
4165842727c2SChris Kirby 
41663b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
41673b2aab18SMatthew Ahrens 	    elem != NULL;
41683b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
41693b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
41703b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
41713b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
41723b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
417315508ac0SChris Kirby 		case E2BIG:
417415508ac0SChris Kirby 			/*
417515508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
417615508ac0SChris Kirby 			 * prepended. So even if we passed the length check
417715508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
417815508ac0SChris Kirby 			 * up being slightly too long.
417915508ac0SChris Kirby 			 */
41803b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
41813b2aab18SMatthew Ahrens 			break;
4182842727c2SChris Kirby 		case EINVAL:
41833b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
41843b2aab18SMatthew Ahrens 			break;
4185842727c2SChris Kirby 		case EEXIST:
41863b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
41873b2aab18SMatthew Ahrens 			break;
4188842727c2SChris Kirby 		default:
41893b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
41903b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4191842727c2SChris Kirby 		}
4192842727c2SChris Kirby 	}
4193842727c2SChris Kirby 
41943b2aab18SMatthew Ahrens 	fnvlist_free(errors);
41953b2aab18SMatthew Ahrens 	return (ret);
41963b2aab18SMatthew Ahrens }
41973b2aab18SMatthew Ahrens 
41983b2aab18SMatthew Ahrens static int
41993b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
42003b2aab18SMatthew Ahrens {
42013b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
42023b2aab18SMatthew Ahrens 	char name[ZFS_MAXNAMELEN];
42033b2aab18SMatthew Ahrens 	int rv = 0;
4204*bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
42053b2aab18SMatthew Ahrens 
42063b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
42073b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
42083b2aab18SMatthew Ahrens 
4209*bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4210*bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4211*bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4212*bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4213*bb6e7075SMatthew Ahrens 	} else {
4214*bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4215*bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4216*bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4217*bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
42183b2aab18SMatthew Ahrens 	}
42193b2aab18SMatthew Ahrens 
42203b2aab18SMatthew Ahrens 	if (ha->recursive)
42213b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
42223b2aab18SMatthew Ahrens 	zfs_close(zhp);
42233b2aab18SMatthew Ahrens 	return (rv);
4224842727c2SChris Kirby }
4225842727c2SChris Kirby 
4226842727c2SChris Kirby int
4227842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4228842727c2SChris Kirby     boolean_t recursive)
4229842727c2SChris Kirby {
42303b2aab18SMatthew Ahrens 	int ret;
42313b2aab18SMatthew Ahrens 	struct holdarg ha;
4232a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
42333b2aab18SMatthew Ahrens 	nvpair_t *elem;
4234842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4235013023d4SMartin Matuska 	char errbuf[1024];
4236842727c2SChris Kirby 
42373b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
42383b2aab18SMatthew Ahrens 	ha.snapname = snapname;
42393b2aab18SMatthew Ahrens 	ha.tag = tag;
42403b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4241*bb6e7075SMatthew Ahrens 	ha.error = 0;
42423b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4243013023d4SMartin Matuska 
4244a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4245013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4246*bb6e7075SMatthew Ahrens 		ret = ha.error;
4247013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4248013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4249013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
4250013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4251*bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
4252*bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4253*bb6e7075SMatthew Ahrens 		} else {
4254013023d4SMartin Matuska 			(void) zfs_standard_error(hdl, ret, errbuf);
4255*bb6e7075SMatthew Ahrens 		}
4256013023d4SMartin Matuska 		return (ret);
4257013023d4SMartin Matuska 	}
4258013023d4SMartin Matuska 
42593b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
42603b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4261842727c2SChris Kirby 
4262a7a845e4SSteven Hartland 	if (ret == 0) {
4263a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4264a7a845e4SSteven Hartland 		fnvlist_free(errors);
42653b2aab18SMatthew Ahrens 		return (0);
4266a7a845e4SSteven Hartland 	}
4267842727c2SChris Kirby 
4268a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
42693b2aab18SMatthew Ahrens 		/* no hold-specific errors */
4270842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
42713b2aab18SMatthew Ahrens 		    "cannot release"));
4272842727c2SChris Kirby 		switch (errno) {
4273842727c2SChris Kirby 		case ENOTSUP:
4274842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4275842727c2SChris Kirby 			    "pool must be upgraded"));
42763b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
42773b2aab18SMatthew Ahrens 			break;
4278842727c2SChris Kirby 		default:
42793b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4280842727c2SChris Kirby 		}
4281842727c2SChris Kirby 	}
4282842727c2SChris Kirby 
42833b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
42843b2aab18SMatthew Ahrens 	    elem != NULL;
42853b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
42863b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
42873b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
42883b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
42893b2aab18SMatthew Ahrens 		    nvpair_name(elem));
42903b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
42913b2aab18SMatthew Ahrens 		case ESRCH:
42923b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
42933b2aab18SMatthew Ahrens 			break;
42943b2aab18SMatthew Ahrens 		case EINVAL:
42953b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
42963b2aab18SMatthew Ahrens 			break;
42973b2aab18SMatthew Ahrens 		default:
42983b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
42993b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
43003b2aab18SMatthew Ahrens 		}
43013b2aab18SMatthew Ahrens 	}
43023b2aab18SMatthew Ahrens 
43033b2aab18SMatthew Ahrens 	fnvlist_free(errors);
43043b2aab18SMatthew Ahrens 	return (ret);
4305842727c2SChris Kirby }
4306ca45db41SChris Kirby 
43071af68beaSAlexander Stetsenko int
43081af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
43091af68beaSAlexander Stetsenko {
43101af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
43111af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
43121af68beaSAlexander Stetsenko 	int nvsz = 2048;
43131af68beaSAlexander Stetsenko 	void *nvbuf;
43141af68beaSAlexander Stetsenko 	int err = 0;
43153b2aab18SMatthew Ahrens 	char errbuf[1024];
43161af68beaSAlexander Stetsenko 
43171af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
43181af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
43191af68beaSAlexander Stetsenko 
43201af68beaSAlexander Stetsenko tryagain:
43211af68beaSAlexander Stetsenko 
43221af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
43231af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
43241af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
43251af68beaSAlexander Stetsenko 		goto out;
43261af68beaSAlexander Stetsenko 	}
43271af68beaSAlexander Stetsenko 
43281af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
43291af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
43301af68beaSAlexander Stetsenko 
43311af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
43321af68beaSAlexander Stetsenko 
4333d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
43341af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
43351af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
43361af68beaSAlexander Stetsenko 		    zc.zc_name);
43371af68beaSAlexander Stetsenko 		switch (errno) {
43381af68beaSAlexander Stetsenko 		case ENOMEM:
43391af68beaSAlexander Stetsenko 			free(nvbuf);
43401af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
43411af68beaSAlexander Stetsenko 			goto tryagain;
43421af68beaSAlexander Stetsenko 
43431af68beaSAlexander Stetsenko 		case ENOTSUP:
43441af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43451af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
43461af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
43471af68beaSAlexander Stetsenko 			break;
43481af68beaSAlexander Stetsenko 		case EINVAL:
43491af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
43501af68beaSAlexander Stetsenko 			break;
43511af68beaSAlexander Stetsenko 		case ENOENT:
43521af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
43531af68beaSAlexander Stetsenko 			break;
43541af68beaSAlexander Stetsenko 		default:
43551af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
43561af68beaSAlexander Stetsenko 			break;
43571af68beaSAlexander Stetsenko 		}
43581af68beaSAlexander Stetsenko 	} else {
43591af68beaSAlexander Stetsenko 		/* success */
43601af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
43611af68beaSAlexander Stetsenko 		if (rc) {
43621af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
43631af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
43641af68beaSAlexander Stetsenko 			    zc.zc_name);
43651af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
43661af68beaSAlexander Stetsenko 		}
43671af68beaSAlexander Stetsenko 	}
43681af68beaSAlexander Stetsenko 
43691af68beaSAlexander Stetsenko 	free(nvbuf);
43701af68beaSAlexander Stetsenko out:
43711af68beaSAlexander Stetsenko 	return (err);
43721af68beaSAlexander Stetsenko }
43731af68beaSAlexander Stetsenko 
43741af68beaSAlexander Stetsenko int
43751af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
43761af68beaSAlexander Stetsenko {
43771af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
43781af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
43791af68beaSAlexander Stetsenko 	char *nvbuf;
43803b2aab18SMatthew Ahrens 	char errbuf[1024];
43811af68beaSAlexander Stetsenko 	size_t nvsz;
43821af68beaSAlexander Stetsenko 	int err;
43831af68beaSAlexander Stetsenko 
43841af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
43851af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
43861af68beaSAlexander Stetsenko 
43871af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
43881af68beaSAlexander Stetsenko 	assert(err == 0);
43891af68beaSAlexander Stetsenko 
43901af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
43911af68beaSAlexander Stetsenko 
43921af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
43931af68beaSAlexander Stetsenko 	assert(err == 0);
43941af68beaSAlexander Stetsenko 
43951af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
43961af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
43971af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
43981af68beaSAlexander Stetsenko 
43991af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
44001af68beaSAlexander Stetsenko 
44011af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
44021af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
44031af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
44041af68beaSAlexander Stetsenko 		    zc.zc_name);
44051af68beaSAlexander Stetsenko 		switch (errno) {
44061af68beaSAlexander Stetsenko 		case ENOTSUP:
44071af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44081af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
44091af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
44101af68beaSAlexander Stetsenko 			break;
44111af68beaSAlexander Stetsenko 		case EINVAL:
44121af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
44131af68beaSAlexander Stetsenko 			break;
44141af68beaSAlexander Stetsenko 		case ENOENT:
44151af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
44161af68beaSAlexander Stetsenko 			break;
44171af68beaSAlexander Stetsenko 		default:
44181af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
44191af68beaSAlexander Stetsenko 			break;
44201af68beaSAlexander Stetsenko 		}
44211af68beaSAlexander Stetsenko 	}
44221af68beaSAlexander Stetsenko 
44231af68beaSAlexander Stetsenko 	free(nvbuf);
44241af68beaSAlexander Stetsenko 
44251af68beaSAlexander Stetsenko 	return (err);
44261af68beaSAlexander Stetsenko }
44271af68beaSAlexander Stetsenko 
44281af68beaSAlexander Stetsenko int
44291af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
44301af68beaSAlexander Stetsenko {
44313b2aab18SMatthew Ahrens 	int err;
44323b2aab18SMatthew Ahrens 	char errbuf[1024];
44333b2aab18SMatthew Ahrens 
44343b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
44353b2aab18SMatthew Ahrens 
44363b2aab18SMatthew Ahrens 	if (err != 0) {
44371af68beaSAlexander Stetsenko 		libzfs_handle_t *hdl = zhp->zfs_hdl;
44381af68beaSAlexander Stetsenko 
44391af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
44401af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
44413b2aab18SMatthew Ahrens 		    zhp->zfs_name);
44423b2aab18SMatthew Ahrens 		switch (err) {
44431af68beaSAlexander Stetsenko 		case ENOTSUP:
44441af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44451af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
44461af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
44471af68beaSAlexander Stetsenko 			break;
44481af68beaSAlexander Stetsenko 		case EINVAL:
44491af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
44501af68beaSAlexander Stetsenko 			break;
44511af68beaSAlexander Stetsenko 		case ENOENT:
44521af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
44531af68beaSAlexander Stetsenko 			break;
44541af68beaSAlexander Stetsenko 		default:
44551af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
44561af68beaSAlexander Stetsenko 			break;
44571af68beaSAlexander Stetsenko 		}
44581af68beaSAlexander Stetsenko 	}
44591af68beaSAlexander Stetsenko 
44601af68beaSAlexander Stetsenko 	return (err);
44611af68beaSAlexander Stetsenko }
44621af68beaSAlexander Stetsenko 
44633e30c24aSWill Andrews /*
44643e30c24aSWill Andrews  * Convert the zvol's volume size to an appropriate reservation.
44653e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
44663e30c24aSWill Andrews  * suite's shell version in reservation.kshlib.
44673e30c24aSWill Andrews  */
4468c1449561SEric Taylor uint64_t
4469c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4470c1449561SEric Taylor {
4471c1449561SEric Taylor 	uint64_t numdb;
4472c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4473c1449561SEric Taylor 	int ncopies;
4474c1449561SEric Taylor 	char *strval;
4475c1449561SEric Taylor 
4476c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4477c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4478c1449561SEric Taylor 		ncopies = atoi(strval);
4479c1449561SEric Taylor 	else
4480c1449561SEric Taylor 		ncopies = 1;
4481c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4482c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4483c1449561SEric Taylor 	    &volblocksize) != 0)
4484c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4485c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4486c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4487c1449561SEric Taylor 	numdb = 7;
4488c1449561SEric Taylor 	/* calculate number of indirects */
4489c1449561SEric Taylor 	while (nblocks > 1) {
4490c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4491c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4492c1449561SEric Taylor 		numdb += nblocks;
4493c1449561SEric Taylor 	}
4494c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4495c1449561SEric Taylor 	volsize *= ncopies;
4496c1449561SEric Taylor 	/*
4497c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4498c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4499c1449561SEric Taylor 	 * 1100 bytes
4500c1449561SEric Taylor 	 */
4501c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4502c1449561SEric Taylor 	volsize += numdb;
4503c1449561SEric Taylor 	return (volsize);
4504c1449561SEric Taylor }
4505