xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision f0f3ef5a19910089bada873cb30b3f5d88cb9e00)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21f3861e1aSahl 
22fa9e4066Sahrens /*
2336db6475SEric Taylor  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
241af68beaSAlexander Stetsenko  * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
25d7f601efSGeorge Wilson  * Copyright (c) 2012 by Delphix. All rights reserved.
26*f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <ctype.h>
30fa9e4066Sahrens #include <errno.h>
31fa9e4066Sahrens #include <libintl.h>
32fa9e4066Sahrens #include <math.h>
33fa9e4066Sahrens #include <stdio.h>
34fa9e4066Sahrens #include <stdlib.h>
35fa9e4066Sahrens #include <strings.h>
36fa9e4066Sahrens #include <unistd.h>
373cb34c60Sahrens #include <stddef.h>
38fa9e4066Sahrens #include <zone.h>
3999653d4eSeschrock #include <fcntl.h>
40fa9e4066Sahrens #include <sys/mntent.h>
41b12a1c38Slling #include <sys/mount.h>
42ecd6cf80Smarks #include <priv.h>
43ecd6cf80Smarks #include <pwd.h>
44ecd6cf80Smarks #include <grp.h>
45ecd6cf80Smarks #include <stddef.h>
46ecd6cf80Smarks #include <ucred.h>
4714843421SMatthew Ahrens #include <idmap.h>
4814843421SMatthew Ahrens #include <aclutils.h>
493b12c289SMatthew Ahrens #include <directory.h>
50fa9e4066Sahrens 
51c1449561SEric Taylor #include <sys/dnode.h>
52fa9e4066Sahrens #include <sys/spa.h>
53e9dbad6fSeschrock #include <sys/zap.h>
54fa9e4066Sahrens #include <libzfs.h>
55fa9e4066Sahrens 
56fa9e4066Sahrens #include "zfs_namecheck.h"
57fa9e4066Sahrens #include "zfs_prop.h"
58fa9e4066Sahrens #include "libzfs_impl.h"
59ecd6cf80Smarks #include "zfs_deleg.h"
60fa9e4066Sahrens 
6114843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6214843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
63cdf5b4caSmmusante 
64fa9e4066Sahrens /*
65fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
66fa9e4066Sahrens  * readable form.
67fa9e4066Sahrens  */
68fa9e4066Sahrens const char *
69fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
70fa9e4066Sahrens {
71fa9e4066Sahrens 	switch (type) {
72fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
73fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
74fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
75fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
76fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
77fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
78fa9e4066Sahrens 	}
79fa9e4066Sahrens 
80fa9e4066Sahrens 	return (NULL);
81fa9e4066Sahrens }
82fa9e4066Sahrens 
83fa9e4066Sahrens /*
84fa9e4066Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
85fa9e4066Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
86fa9e4066Sahrens  * We guess what the type would have been based on the path and the mask of
87fa9e4066Sahrens  * acceptable types.
88fa9e4066Sahrens  */
89fa9e4066Sahrens static const char *
90fa9e4066Sahrens path_to_str(const char *path, int types)
91fa9e4066Sahrens {
92fa9e4066Sahrens 	/*
93fa9e4066Sahrens 	 * When given a single type, always report the exact type.
94fa9e4066Sahrens 	 */
95fa9e4066Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
96fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
97fa9e4066Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
98fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
99fa9e4066Sahrens 	if (types == ZFS_TYPE_VOLUME)
100fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
101fa9e4066Sahrens 
102fa9e4066Sahrens 	/*
103fa9e4066Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
104fa9e4066Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
105fa9e4066Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
106fa9e4066Sahrens 	 * snapshot attribute and try again.
107fa9e4066Sahrens 	 */
108fa9e4066Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
109fa9e4066Sahrens 		if (strchr(path, '@') != NULL)
110fa9e4066Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
111fa9e4066Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
112fa9e4066Sahrens 	}
113fa9e4066Sahrens 
114fa9e4066Sahrens 	/*
115fa9e4066Sahrens 	 * The user has requested either filesystems or volumes.
116fa9e4066Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117fa9e4066Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118fa9e4066Sahrens 	 */
119fa9e4066Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121fa9e4066Sahrens 
122fa9e4066Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123fa9e4066Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124fa9e4066Sahrens }
125fa9e4066Sahrens 
126fa9e4066Sahrens /*
127fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
12814843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
12914843421SMatthew Ahrens  * explain exactly why the name was not valid.
130fa9e4066Sahrens  */
13199d5e173STim Haley int
132f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
133f18faf3fSek110237     boolean_t modifying)
134fa9e4066Sahrens {
135fa9e4066Sahrens 	namecheck_err_t why;
136fa9e4066Sahrens 	char what;
137fa9e4066Sahrens 
1381af68beaSAlexander Stetsenko 	(void) zfs_prop_get_table();
139fa9e4066Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
14099653d4eSeschrock 		if (hdl != NULL) {
141fa9e4066Sahrens 			switch (why) {
142b81d61a6Slling 			case NAME_ERR_TOOLONG:
14399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14499653d4eSeschrock 				    "name is too long"));
145b81d61a6Slling 				break;
146b81d61a6Slling 
147fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
14899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14999653d4eSeschrock 				    "leading slash in name"));
150fa9e4066Sahrens 				break;
151fa9e4066Sahrens 
152fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
15399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15499653d4eSeschrock 				    "empty component in name"));
155fa9e4066Sahrens 				break;
156fa9e4066Sahrens 
157fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
15899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15999653d4eSeschrock 				    "trailing slash in name"));
160fa9e4066Sahrens 				break;
161fa9e4066Sahrens 
162fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
16399653d4eSeschrock 				zfs_error_aux(hdl,
164fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
16599653d4eSeschrock 				    "'%c' in name"), what);
166fa9e4066Sahrens 				break;
167fa9e4066Sahrens 
168fa9e4066Sahrens 			case NAME_ERR_MULTIPLE_AT:
16999653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17099653d4eSeschrock 				    "multiple '@' delimiters in name"));
171fa9e4066Sahrens 				break;
1725ad82045Snd150628 
1735ad82045Snd150628 			case NAME_ERR_NOLETTER:
1745ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1755ad82045Snd150628 				    "pool doesn't begin with a letter"));
1765ad82045Snd150628 				break;
1775ad82045Snd150628 
1785ad82045Snd150628 			case NAME_ERR_RESERVED:
1795ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1805ad82045Snd150628 				    "name is reserved"));
1815ad82045Snd150628 				break;
1825ad82045Snd150628 
1835ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1845ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1855ad82045Snd150628 				    "reserved disk name"));
1865ad82045Snd150628 				break;
187fa9e4066Sahrens 			}
188fa9e4066Sahrens 		}
189fa9e4066Sahrens 
190fa9e4066Sahrens 		return (0);
191fa9e4066Sahrens 	}
192fa9e4066Sahrens 
193fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
19499653d4eSeschrock 		if (hdl != NULL)
19599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19699653d4eSeschrock 			    "snapshot delimiter '@' in filesystem name"));
197fa9e4066Sahrens 		return (0);
198fa9e4066Sahrens 	}
199fa9e4066Sahrens 
2001d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2011d452cf5Sahrens 		if (hdl != NULL)
2021d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
2041d452cf5Sahrens 		return (0);
2051d452cf5Sahrens 	}
2061d452cf5Sahrens 
207f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
208f18faf3fSek110237 		if (hdl != NULL)
209f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
210f18faf3fSek110237 			    "invalid character %c in name"), '%');
211f18faf3fSek110237 		return (0);
212f18faf3fSek110237 	}
213f18faf3fSek110237 
21499653d4eSeschrock 	return (-1);
215fa9e4066Sahrens }
216fa9e4066Sahrens 
217fa9e4066Sahrens int
218fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
219fa9e4066Sahrens {
220e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
221e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
222f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
223fa9e4066Sahrens }
224fa9e4066Sahrens 
225fa9e4066Sahrens /*
226e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
227e9dbad6fSeschrock  * properties into a separate nvlist.
228e9dbad6fSeschrock  */
229fac3008cSeschrock static nvlist_t *
230fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
231e9dbad6fSeschrock {
232e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
233e9dbad6fSeschrock 	nvpair_t *elem;
234e9dbad6fSeschrock 	nvlist_t *propval;
235fac3008cSeschrock 	nvlist_t *nvl;
236e9dbad6fSeschrock 
237fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
238fac3008cSeschrock 		(void) no_memory(hdl);
239fac3008cSeschrock 		return (NULL);
240fac3008cSeschrock 	}
241e9dbad6fSeschrock 
242e9dbad6fSeschrock 	elem = NULL;
243fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
244e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
245e9dbad6fSeschrock 			continue;
246e9dbad6fSeschrock 
247e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
248fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
249fac3008cSeschrock 			nvlist_free(nvl);
250fac3008cSeschrock 			(void) no_memory(hdl);
251fac3008cSeschrock 			return (NULL);
252fac3008cSeschrock 		}
253e9dbad6fSeschrock 	}
254e9dbad6fSeschrock 
255fac3008cSeschrock 	return (nvl);
256e9dbad6fSeschrock }
257e9dbad6fSeschrock 
25829ab75c9Srm160521 static zpool_handle_t *
25929ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
26029ab75c9Srm160521 {
26129ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26229ab75c9Srm160521 	zpool_handle_t *zph;
26329ab75c9Srm160521 
26429ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
26529ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
26629ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
26729ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
26829ab75c9Srm160521 	}
26929ab75c9Srm160521 	return (zph);
27029ab75c9Srm160521 }
27129ab75c9Srm160521 
27229ab75c9Srm160521 static zpool_handle_t *
27329ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
27429ab75c9Srm160521 {
27529ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
27629ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
27729ab75c9Srm160521 
27829ab75c9Srm160521 	while ((zph != NULL) &&
27929ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
28029ab75c9Srm160521 		zph = zph->zpool_next;
28129ab75c9Srm160521 	return (zph);
28229ab75c9Srm160521 }
28329ab75c9Srm160521 
28429ab75c9Srm160521 /*
28529ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
28629ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
28729ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
28829ab75c9Srm160521  */
28929ab75c9Srm160521 static zpool_handle_t *
29029ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
29129ab75c9Srm160521 {
29229ab75c9Srm160521 	char *pool_name;
29329ab75c9Srm160521 	int len;
29429ab75c9Srm160521 	zpool_handle_t *zph;
29529ab75c9Srm160521 
29629ab75c9Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
29729ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
29829ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
29929ab75c9Srm160521 
30029ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
30129ab75c9Srm160521 	if (zph == NULL)
30229ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
30329ab75c9Srm160521 
30429ab75c9Srm160521 	free(pool_name);
30529ab75c9Srm160521 	return (zph);
30629ab75c9Srm160521 }
30729ab75c9Srm160521 
30829ab75c9Srm160521 void
30929ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
31029ab75c9Srm160521 {
31129ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
31229ab75c9Srm160521 
31329ab75c9Srm160521 	while (zph != NULL) {
31429ab75c9Srm160521 		next = zph->zpool_next;
31529ab75c9Srm160521 		zpool_close(zph);
31629ab75c9Srm160521 		zph = next;
31729ab75c9Srm160521 	}
31829ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
31929ab75c9Srm160521 }
32029ab75c9Srm160521 
321e9dbad6fSeschrock /*
322fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
323fa9e4066Sahrens  */
324fa9e4066Sahrens static int
325ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
326fa9e4066Sahrens {
327e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
328fa9e4066Sahrens 
329ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
330fa9e4066Sahrens 
331ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3327f7322feSeschrock 		if (errno == ENOMEM) {
333ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
33499653d4eSeschrock 				return (-1);
335e9dbad6fSeschrock 			}
3367f7322feSeschrock 		} else {
337fa9e4066Sahrens 			return (-1);
3387f7322feSeschrock 		}
3397f7322feSeschrock 	}
340ebedde84SEric Taylor 	return (0);
341fac3008cSeschrock }
342fac3008cSeschrock 
34392241e0bSTom Erickson /*
34492241e0bSTom Erickson  * Utility function to get the received properties of the given object.
34592241e0bSTom Erickson  */
34692241e0bSTom Erickson static int
34792241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
34892241e0bSTom Erickson {
34992241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
35092241e0bSTom Erickson 	nvlist_t *recvdprops;
35192241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
35292241e0bSTom Erickson 	int err;
35392241e0bSTom Erickson 
35492241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
35592241e0bSTom Erickson 		return (-1);
35692241e0bSTom Erickson 
35792241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35892241e0bSTom Erickson 
35992241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
36092241e0bSTom Erickson 		if (errno == ENOMEM) {
36192241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
36292241e0bSTom Erickson 				return (-1);
36392241e0bSTom Erickson 			}
36492241e0bSTom Erickson 		} else {
36592241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
36692241e0bSTom Erickson 			return (-1);
36792241e0bSTom Erickson 		}
36892241e0bSTom Erickson 	}
36992241e0bSTom Erickson 
37092241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
37192241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
37292241e0bSTom Erickson 	if (err != 0)
37392241e0bSTom Erickson 		return (-1);
37492241e0bSTom Erickson 
37592241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
37692241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
37792241e0bSTom Erickson 
37892241e0bSTom Erickson 	return (0);
37992241e0bSTom Erickson }
38092241e0bSTom Erickson 
381ebedde84SEric Taylor static int
382ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
383ebedde84SEric Taylor {
384ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
385ebedde84SEric Taylor 
386ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
387ebedde84SEric Taylor 
388ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
389ebedde84SEric Taylor 		return (-1);
390ebedde84SEric Taylor 	}
391fac3008cSeschrock 
39214843421SMatthew Ahrens 	/*
39314843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
39414843421SMatthew Ahrens 	 * storing them in zfs_props?
39514843421SMatthew Ahrens 	 */
396fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
397fac3008cSeschrock 		nvlist_free(allprops);
398fac3008cSeschrock 		return (-1);
399fac3008cSeschrock 	}
400fac3008cSeschrock 
40199653d4eSeschrock 	nvlist_free(zhp->zfs_props);
402fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
40399653d4eSeschrock 
404fac3008cSeschrock 	zhp->zfs_props = allprops;
405fac3008cSeschrock 	zhp->zfs_user_props = userprops;
40699653d4eSeschrock 
407fa9e4066Sahrens 	return (0);
408fa9e4066Sahrens }
409fa9e4066Sahrens 
410ebedde84SEric Taylor static int
411ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
412ebedde84SEric Taylor {
413ebedde84SEric Taylor 	int rc = 0;
414ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
415ebedde84SEric Taylor 
416ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
417ebedde84SEric Taylor 		return (-1);
418ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
419ebedde84SEric Taylor 		rc = -1;
420ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
421ebedde84SEric Taylor 		rc = -1;
422ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
423ebedde84SEric Taylor 	return (rc);
424ebedde84SEric Taylor }
425ebedde84SEric Taylor 
426fa9e4066Sahrens /*
427fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
428fa9e4066Sahrens  */
429fa9e4066Sahrens void
430fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
431fa9e4066Sahrens {
432fa9e4066Sahrens 	(void) get_stats(zhp);
433fa9e4066Sahrens }
434fa9e4066Sahrens 
435fa9e4066Sahrens /*
436fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
437fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
438fa9e4066Sahrens  */
439ebedde84SEric Taylor static int
440ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
441fa9e4066Sahrens {
442503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
443ebedde84SEric Taylor 		return (-1);
44431fd60d3Sahrens 
445fa9e4066Sahrens 	/*
446fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
447fa9e4066Sahrens 	 * the high-level type.
448fa9e4066Sahrens 	 */
449a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
450a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
451a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
452a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
453a2eea2e1Sahrens 	else
454a2eea2e1Sahrens 		abort();
455a2eea2e1Sahrens 
456fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
457fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
458fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
459fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
460fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
461fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
462fa9e4066Sahrens 	else
46399653d4eSeschrock 		abort();	/* we should never see any other types */
464fa9e4066Sahrens 
4659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
468ebedde84SEric Taylor 	return (0);
469ebedde84SEric Taylor }
470ebedde84SEric Taylor 
471ebedde84SEric Taylor zfs_handle_t *
472ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
473ebedde84SEric Taylor {
474ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
475ebedde84SEric Taylor 
476ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
477ebedde84SEric Taylor 
478ebedde84SEric Taylor 	if (zhp == NULL)
479ebedde84SEric Taylor 		return (NULL);
480ebedde84SEric Taylor 
481ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
482ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
483ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
484ebedde84SEric Taylor 		free(zhp);
485ebedde84SEric Taylor 		return (NULL);
486ebedde84SEric Taylor 	}
487ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
488ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
489ebedde84SEric Taylor 		free(zhp);
490ebedde84SEric Taylor 		return (NULL);
491ebedde84SEric Taylor 	}
492ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
493ebedde84SEric Taylor 		free(zhp);
494ebedde84SEric Taylor 		zhp = NULL;
495ebedde84SEric Taylor 	}
496ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
497ebedde84SEric Taylor 	return (zhp);
498ebedde84SEric Taylor }
499ebedde84SEric Taylor 
50019b94df9SMatthew Ahrens zfs_handle_t *
501ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
502ebedde84SEric Taylor {
503ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
504ebedde84SEric Taylor 
505ebedde84SEric Taylor 	if (zhp == NULL)
506ebedde84SEric Taylor 		return (NULL);
507ebedde84SEric Taylor 
508ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
509ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
510ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
511ebedde84SEric Taylor 		free(zhp);
512ebedde84SEric Taylor 		return (NULL);
513ebedde84SEric Taylor 	}
514fa9e4066Sahrens 	return (zhp);
515fa9e4066Sahrens }
516fa9e4066Sahrens 
51719b94df9SMatthew Ahrens zfs_handle_t *
51819b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
51919b94df9SMatthew Ahrens {
52019b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
52119b94df9SMatthew Ahrens 
52219b94df9SMatthew Ahrens 	if (zhp == NULL)
52319b94df9SMatthew Ahrens 		return (NULL);
52419b94df9SMatthew Ahrens 
52519b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
52619b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
52719b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
52819b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
52919b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
53019b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
53119b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
53219b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
53319b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
53419b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
53519b94df9SMatthew Ahrens 			zfs_close(zhp);
53619b94df9SMatthew Ahrens 			return (NULL);
53719b94df9SMatthew Ahrens 		}
53819b94df9SMatthew Ahrens 	}
53919b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
54019b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
54119b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
54219b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54319b94df9SMatthew Ahrens 			zfs_close(zhp);
54419b94df9SMatthew Ahrens 			return (NULL);
54519b94df9SMatthew Ahrens 		}
54619b94df9SMatthew Ahrens 	}
54719b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
54819b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
54919b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
55019b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55119b94df9SMatthew Ahrens 			zfs_close(zhp);
55219b94df9SMatthew Ahrens 			return (NULL);
55319b94df9SMatthew Ahrens 		}
55419b94df9SMatthew Ahrens 	}
55519b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
55619b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
55719b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
55819b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
55919b94df9SMatthew Ahrens 	}
56019b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
56119b94df9SMatthew Ahrens 	return (zhp);
56219b94df9SMatthew Ahrens }
56319b94df9SMatthew Ahrens 
564fa9e4066Sahrens /*
565fa9e4066Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
566fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
567fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
568fa9e4066Sahrens  */
569fa9e4066Sahrens zfs_handle_t *
57099653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
571fa9e4066Sahrens {
572fa9e4066Sahrens 	zfs_handle_t *zhp;
57399653d4eSeschrock 	char errbuf[1024];
57499653d4eSeschrock 
57599653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
57699653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
577fa9e4066Sahrens 
578fa9e4066Sahrens 	/*
57999653d4eSeschrock 	 * Validate the name before we even try to open it.
580fa9e4066Sahrens 	 */
581f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
58299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
58399653d4eSeschrock 		    "invalid dataset name"));
58499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
585fa9e4066Sahrens 		return (NULL);
586fa9e4066Sahrens 	}
587fa9e4066Sahrens 
588fa9e4066Sahrens 	/*
589fa9e4066Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
590fa9e4066Sahrens 	 */
591fa9e4066Sahrens 	errno = 0;
59299653d4eSeschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
593ece3d9b3Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
594fa9e4066Sahrens 		return (NULL);
595fa9e4066Sahrens 	}
596fa9e4066Sahrens 
597fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
59899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
59994de1d4cSeschrock 		zfs_close(zhp);
600fa9e4066Sahrens 		return (NULL);
601fa9e4066Sahrens 	}
602fa9e4066Sahrens 
603fa9e4066Sahrens 	return (zhp);
604fa9e4066Sahrens }
605fa9e4066Sahrens 
606fa9e4066Sahrens /*
607fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
608fa9e4066Sahrens  */
609fa9e4066Sahrens void
610fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
611fa9e4066Sahrens {
612fa9e4066Sahrens 	if (zhp->zfs_mntopts)
613fa9e4066Sahrens 		free(zhp->zfs_mntopts);
61499653d4eSeschrock 	nvlist_free(zhp->zfs_props);
615e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
61692241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
617fa9e4066Sahrens 	free(zhp);
618fa9e4066Sahrens }
619fa9e4066Sahrens 
620ebedde84SEric Taylor typedef struct mnttab_node {
621ebedde84SEric Taylor 	struct mnttab mtn_mt;
622ebedde84SEric Taylor 	avl_node_t mtn_node;
623ebedde84SEric Taylor } mnttab_node_t;
624ebedde84SEric Taylor 
625ebedde84SEric Taylor static int
626ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
627ebedde84SEric Taylor {
628ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
629ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
630ebedde84SEric Taylor 	int rv;
631ebedde84SEric Taylor 
632ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
633ebedde84SEric Taylor 
634ebedde84SEric Taylor 	if (rv == 0)
635ebedde84SEric Taylor 		return (0);
636ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
637ebedde84SEric Taylor }
638ebedde84SEric Taylor 
639ebedde84SEric Taylor void
640ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
641ebedde84SEric Taylor {
642ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
643ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
644ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
645b2634b9cSEric Taylor }
646b2634b9cSEric Taylor 
647b2634b9cSEric Taylor void
648b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
649b2634b9cSEric Taylor {
650b2634b9cSEric Taylor 	struct mnttab entry;
651ebedde84SEric Taylor 
652ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
653ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
654ebedde84SEric Taylor 		mnttab_node_t *mtn;
655ebedde84SEric Taylor 
656ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
657ebedde84SEric Taylor 			continue;
658ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
659ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
660ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
661ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
662ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
663ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
664ebedde84SEric Taylor 	}
665ebedde84SEric Taylor }
666ebedde84SEric Taylor 
667ebedde84SEric Taylor void
668ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
669ebedde84SEric Taylor {
670ebedde84SEric Taylor 	void *cookie = NULL;
671ebedde84SEric Taylor 	mnttab_node_t *mtn;
672ebedde84SEric Taylor 
673ebedde84SEric Taylor 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
674ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
675ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
676ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
677ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
678ebedde84SEric Taylor 		free(mtn);
679ebedde84SEric Taylor 	}
680ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
681ebedde84SEric Taylor }
682ebedde84SEric Taylor 
683b2634b9cSEric Taylor void
684b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
685b2634b9cSEric Taylor {
686b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
687b2634b9cSEric Taylor }
688b2634b9cSEric Taylor 
689ebedde84SEric Taylor int
690ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
691ebedde84SEric Taylor     struct mnttab *entry)
692ebedde84SEric Taylor {
693ebedde84SEric Taylor 	mnttab_node_t find;
694ebedde84SEric Taylor 	mnttab_node_t *mtn;
695ebedde84SEric Taylor 
696b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
697b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
698b2634b9cSEric Taylor 
699b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
700b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
701b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
702b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
703b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
704b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
705b2634b9cSEric Taylor 			return (0);
706b2634b9cSEric Taylor 		else
707b2634b9cSEric Taylor 			return (ENOENT);
708b2634b9cSEric Taylor 	}
709b2634b9cSEric Taylor 
710ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
711b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
712ebedde84SEric Taylor 
713ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
714ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
715ebedde84SEric Taylor 	if (mtn) {
716ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
717ebedde84SEric Taylor 		return (0);
718ebedde84SEric Taylor 	}
719ebedde84SEric Taylor 	return (ENOENT);
720ebedde84SEric Taylor }
721ebedde84SEric Taylor 
722ebedde84SEric Taylor void
723ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
724ebedde84SEric Taylor     const char *mountp, const char *mntopts)
725ebedde84SEric Taylor {
726ebedde84SEric Taylor 	mnttab_node_t *mtn;
727ebedde84SEric Taylor 
728ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
729ebedde84SEric Taylor 		return;
730ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
731ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
732ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
733ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
734ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
735ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
736ebedde84SEric Taylor }
737ebedde84SEric Taylor 
738ebedde84SEric Taylor void
739ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
740ebedde84SEric Taylor {
741ebedde84SEric Taylor 	mnttab_node_t find;
742ebedde84SEric Taylor 	mnttab_node_t *ret;
743ebedde84SEric Taylor 
744ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
745ebedde84SEric Taylor 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
746ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
747ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
748ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
749ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
750ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
751ebedde84SEric Taylor 		free(ret);
752ebedde84SEric Taylor 	}
753ebedde84SEric Taylor }
754ebedde84SEric Taylor 
7557b97dc1aSrm160521 int
7567b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
7577b97dc1aSrm160521 {
75829ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
7597b97dc1aSrm160521 
7607b97dc1aSrm160521 	if (zpool_handle == NULL)
7617b97dc1aSrm160521 		return (-1);
7627b97dc1aSrm160521 
7637b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
7647b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
7657b97dc1aSrm160521 	return (0);
7667b97dc1aSrm160521 }
7677b97dc1aSrm160521 
7687b97dc1aSrm160521 /*
7697b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
7707b97dc1aSrm160521  */
7717b97dc1aSrm160521 static int
7727b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
7737b97dc1aSrm160521 {
7747b97dc1aSrm160521 	int spa_version;
7757b97dc1aSrm160521 
7767b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
7777b97dc1aSrm160521 		return (-1);
7787b97dc1aSrm160521 
7797b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
7807b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
7817b97dc1aSrm160521 	else
7827b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
7837b97dc1aSrm160521 
7847b97dc1aSrm160521 	return (0);
7857b97dc1aSrm160521 }
7867b97dc1aSrm160521 
787b1b8ab34Slling /*
788e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
789e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
790e9dbad6fSeschrock  * strings.
791fa9e4066Sahrens  */
7920a48a24eStimh nvlist_t *
7930a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
794990b4856Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
795fa9e4066Sahrens {
796e9dbad6fSeschrock 	nvpair_t *elem;
797e9dbad6fSeschrock 	uint64_t intval;
798e9dbad6fSeschrock 	char *strval;
799990b4856Slling 	zfs_prop_t prop;
800e9dbad6fSeschrock 	nvlist_t *ret;
801da6c28aaSamw 	int chosen_normal = -1;
802da6c28aaSamw 	int chosen_utf = -1;
803990b4856Slling 
804e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
805e9dbad6fSeschrock 		(void) no_memory(hdl);
806e9dbad6fSeschrock 		return (NULL);
807e9dbad6fSeschrock 	}
808fa9e4066Sahrens 
80914843421SMatthew Ahrens 	/*
81014843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
81114843421SMatthew Ahrens 	 */
81214843421SMatthew Ahrens 
813e9dbad6fSeschrock 	elem = NULL;
814e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
815990b4856Slling 		const char *propname = nvpair_name(elem);
81699653d4eSeschrock 
81714843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
81814843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
819fa9e4066Sahrens 			/*
82014843421SMatthew Ahrens 			 * This is a user property: make sure it's a
821990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
822990b4856Slling 			 */
823990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
824990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
825990b4856Slling 				    "'%s' must be a string"), propname);
826990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
827990b4856Slling 				goto error;
828990b4856Slling 			}
829990b4856Slling 
830990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
831e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
832e9dbad6fSeschrock 				    "property name '%s' is too long"),
833e9dbad6fSeschrock 				    propname);
834990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
835e9dbad6fSeschrock 				goto error;
836e9dbad6fSeschrock 			}
837e9dbad6fSeschrock 
838e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
839e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
840e9dbad6fSeschrock 				(void) no_memory(hdl);
841e9dbad6fSeschrock 				goto error;
842e9dbad6fSeschrock 			}
843e9dbad6fSeschrock 			continue;
844e9dbad6fSeschrock 		}
845fa9e4066Sahrens 
84614843421SMatthew Ahrens 		/*
84714843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
84814843421SMatthew Ahrens 		 * snapshots.
84914843421SMatthew Ahrens 		 */
850bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
851bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
852bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
853bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
854bb0ade09Sahrens 			goto error;
855bb0ade09Sahrens 		}
856bb0ade09Sahrens 
85714843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
85814843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
85914843421SMatthew Ahrens 			char newpropname[128];
86014843421SMatthew Ahrens 			char domain[128];
86114843421SMatthew Ahrens 			uint64_t rid;
86214843421SMatthew Ahrens 			uint64_t valary[3];
86314843421SMatthew Ahrens 
86414843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
86514843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
86614843421SMatthew Ahrens 				zfs_error_aux(hdl,
86714843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
86814843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
86914843421SMatthew Ahrens 				    propname);
87014843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
87114843421SMatthew Ahrens 				goto error;
87214843421SMatthew Ahrens 			}
87314843421SMatthew Ahrens 
87414843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
87514843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
87614843421SMatthew Ahrens 				zfs_error_aux(hdl,
87714843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
87814843421SMatthew Ahrens 				    propname);
87914843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
88014843421SMatthew Ahrens 				    errbuf);
88114843421SMatthew Ahrens 				goto error;
88214843421SMatthew Ahrens 			}
88314843421SMatthew Ahrens 
88414843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
88514843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
88614843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
88714843421SMatthew Ahrens 					intval = 0;
88814843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
88914843421SMatthew Ahrens 				    strval, &intval) != 0) {
89014843421SMatthew Ahrens 					(void) zfs_error(hdl,
89114843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
89214843421SMatthew Ahrens 					goto error;
89314843421SMatthew Ahrens 				}
89414843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
89514843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
89614843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
89714843421SMatthew Ahrens 				if (intval == 0) {
89814843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
89914843421SMatthew Ahrens 					    "use 'none' to disable "
90014843421SMatthew Ahrens 					    "userquota/groupquota"));
90114843421SMatthew Ahrens 					goto error;
90214843421SMatthew Ahrens 				}
90314843421SMatthew Ahrens 			} else {
90414843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90514843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
90614843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
90714843421SMatthew Ahrens 				goto error;
90814843421SMatthew Ahrens 			}
90914843421SMatthew Ahrens 
9102d5843dbSMatthew Ahrens 			/*
9112d5843dbSMatthew Ahrens 			 * Encode the prop name as
9122d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
9132d5843dbSMatthew Ahrens 			 * for the kernel to decode.
9142d5843dbSMatthew Ahrens 			 */
91514843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
9162d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
9172d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
91814843421SMatthew Ahrens 			valary[0] = uqtype;
91914843421SMatthew Ahrens 			valary[1] = rid;
92014843421SMatthew Ahrens 			valary[2] = intval;
92114843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
92214843421SMatthew Ahrens 			    valary, 3) != 0) {
92314843421SMatthew Ahrens 				(void) no_memory(hdl);
92414843421SMatthew Ahrens 				goto error;
92514843421SMatthew Ahrens 			}
92614843421SMatthew Ahrens 			continue;
92719b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
92819b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
92919b94df9SMatthew Ahrens 			    "'%s' is readonly"),
93019b94df9SMatthew Ahrens 			    propname);
93119b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
93219b94df9SMatthew Ahrens 			goto error;
93314843421SMatthew Ahrens 		}
93414843421SMatthew Ahrens 
93514843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
93614843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93714843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
93814843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
93914843421SMatthew Ahrens 			goto error;
94014843421SMatthew Ahrens 		}
94114843421SMatthew Ahrens 
942e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
943e9dbad6fSeschrock 			zfs_error_aux(hdl,
944e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
945e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
946e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
947e9dbad6fSeschrock 			goto error;
948e9dbad6fSeschrock 		}
949e9dbad6fSeschrock 
950e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
951da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
952e9dbad6fSeschrock 			zfs_error_aux(hdl,
953e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
954e9dbad6fSeschrock 			    propname);
955e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
956e9dbad6fSeschrock 			goto error;
957e9dbad6fSeschrock 		}
958e9dbad6fSeschrock 
959990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
960990b4856Slling 		    &strval, &intval, errbuf) != 0)
961e9dbad6fSeschrock 			goto error;
962e9dbad6fSeschrock 
963e9dbad6fSeschrock 		/*
964e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
965e9dbad6fSeschrock 		 */
966e9dbad6fSeschrock 		switch (prop) {
967e7437265Sahrens 		case ZFS_PROP_VERSION:
968e7437265Sahrens 		{
969e7437265Sahrens 			int version;
970e7437265Sahrens 
971e7437265Sahrens 			if (zhp == NULL)
972e7437265Sahrens 				break;
973e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
974e7437265Sahrens 			if (intval < version) {
975e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
976e7437265Sahrens 				    "Can not downgrade; already at version %u"),
977e7437265Sahrens 				    version);
978e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
979e7437265Sahrens 				goto error;
980e7437265Sahrens 			}
981e7437265Sahrens 			break;
982e7437265Sahrens 		}
983e7437265Sahrens 
984e9dbad6fSeschrock 		case ZFS_PROP_RECORDSIZE:
985e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
986e9dbad6fSeschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
987e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
988e9dbad6fSeschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
989e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990e9dbad6fSeschrock 				    "'%s' must be power of 2 from %u "
991e9dbad6fSeschrock 				    "to %uk"), propname,
992e9dbad6fSeschrock 				    (uint_t)SPA_MINBLOCKSIZE,
993e9dbad6fSeschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
994e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
995e9dbad6fSeschrock 				goto error;
996e9dbad6fSeschrock 			}
997e9dbad6fSeschrock 			break;
998e9dbad6fSeschrock 
9994201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
10004201a95eSRic Aleshire 		{
10014201a95eSRic Aleshire 			/*
10024201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
10034201a95eSRic Aleshire 			 * internal hex label string.
10044201a95eSRic Aleshire 			 */
10054201a95eSRic Aleshire 
10064201a95eSRic Aleshire 			m_label_t *new_sl;
10074201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
10084201a95eSRic Aleshire 
10094201a95eSRic Aleshire 			/* Default value is already OK. */
10104201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
10114201a95eSRic Aleshire 				break;
10124201a95eSRic Aleshire 
10134201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
10144201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
10154201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
10164201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
10174201a95eSRic Aleshire 				goto badlabel;
10184201a95eSRic Aleshire 			}
10194201a95eSRic Aleshire 
10204201a95eSRic Aleshire 			/* Now translate to hex internal label string */
10214201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
10224201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
10234201a95eSRic Aleshire 				if (hex)
10244201a95eSRic Aleshire 					free(hex);
10254201a95eSRic Aleshire 				goto badlabel;
10264201a95eSRic Aleshire 			}
10274201a95eSRic Aleshire 			m_label_free(new_sl);
10284201a95eSRic Aleshire 
10294201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
10304201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
10314201a95eSRic Aleshire 				free(hex);
10324201a95eSRic Aleshire 				break;
10334201a95eSRic Aleshire 			}
10344201a95eSRic Aleshire 
10354201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1036569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
10374201a95eSRic Aleshire 			    DATA_TYPE_STRING);
10384201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
10394201a95eSRic Aleshire 			    hex) == 0);
10404201a95eSRic Aleshire 			free(hex);
10414201a95eSRic Aleshire 
10424201a95eSRic Aleshire 			break;
10434201a95eSRic Aleshire 
10444201a95eSRic Aleshire badlabel:
10454201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10464201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
10474201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
10484201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
10494201a95eSRic Aleshire 			goto error;
10504201a95eSRic Aleshire 
10514201a95eSRic Aleshire 		}
10524201a95eSRic Aleshire 
1053e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
105489eef05eSrm160521 		{
105589eef05eSrm160521 			namecheck_err_t why;
105689eef05eSrm160521 
1057e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1058e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1059e9dbad6fSeschrock 				break;
1060e9dbad6fSeschrock 
106189eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
106289eef05eSrm160521 				switch (why) {
106389eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
106489eef05eSrm160521 					zfs_error_aux(hdl,
106589eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1066e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1067e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
106889eef05eSrm160521 					break;
106989eef05eSrm160521 				case NAME_ERR_TOOLONG:
107089eef05eSrm160521 					zfs_error_aux(hdl,
107189eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
107289eef05eSrm160521 					    "component of '%s' is too long"),
107389eef05eSrm160521 					    propname);
107489eef05eSrm160521 					break;
107589eef05eSrm160521 				}
1076e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1077e9dbad6fSeschrock 				goto error;
1078e9dbad6fSeschrock 			}
107989eef05eSrm160521 		}
108089eef05eSrm160521 
1081f3861e1aSahl 			/*FALLTHRU*/
1082e9dbad6fSeschrock 
1083da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1084f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1085e9dbad6fSeschrock 			/*
1086da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1087da6c28aaSamw 			 * properties, check if it can be set in a
1088da6c28aaSamw 			 * global/non-global zone based on
1089f3861e1aSahl 			 * the zoned property value:
1090fa9e4066Sahrens 			 *
1091fa9e4066Sahrens 			 *		global zone	    non-global zone
1092f3861e1aSahl 			 * --------------------------------------------------
1093fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1094fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1095da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1096fa9e4066Sahrens 			 *
1097fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1098fa9e4066Sahrens 			 *		sharenfs (yes)
1099da6c28aaSamw 			 *		sharesmb (yes)
1100fa9e4066Sahrens 			 */
1101e9dbad6fSeschrock 			if (zoned) {
1102fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
110399653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1104e9dbad6fSeschrock 					    "'%s' cannot be set on "
1105e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1106e9dbad6fSeschrock 					    propname);
1107e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1108e9dbad6fSeschrock 					    errbuf);
1109e9dbad6fSeschrock 					goto error;
1110da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1111da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
111299653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1113e9dbad6fSeschrock 					    "'%s' cannot be set in "
1114e9dbad6fSeschrock 					    "a non-global zone"), propname);
1115e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1116e9dbad6fSeschrock 					    errbuf);
1117e9dbad6fSeschrock 					goto error;
1118fa9e4066Sahrens 				}
1119fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1120fa9e4066Sahrens 				/*
1121fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
112214843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1123fa9e4066Sahrens 				 */
112499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1126e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1127e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1128e9dbad6fSeschrock 				goto error;
1129fa9e4066Sahrens 			}
1130f3861e1aSahl 
113167331909Sdougm 			/*
113267331909Sdougm 			 * At this point, it is legitimate to set the
113367331909Sdougm 			 * property. Now we want to make sure that the
113467331909Sdougm 			 * property value is valid if it is sharenfs.
113567331909Sdougm 			 */
1136da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1137da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
113867331909Sdougm 			    strcmp(strval, "on") != 0 &&
113967331909Sdougm 			    strcmp(strval, "off") != 0) {
1140da6c28aaSamw 				zfs_share_proto_t proto;
1141da6c28aaSamw 
1142da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1143da6c28aaSamw 					proto = PROTO_SMB;
1144da6c28aaSamw 				else
1145da6c28aaSamw 					proto = PROTO_NFS;
114667331909Sdougm 
114767331909Sdougm 				/*
1148da6c28aaSamw 				 * Must be an valid sharing protocol
1149da6c28aaSamw 				 * option string so init the libshare
1150da6c28aaSamw 				 * in order to enable the parser and
1151da6c28aaSamw 				 * then parse the options. We use the
1152da6c28aaSamw 				 * control API since we don't care about
1153da6c28aaSamw 				 * the current configuration and don't
115467331909Sdougm 				 * want the overhead of loading it
115567331909Sdougm 				 * until we actually do something.
115667331909Sdougm 				 */
115767331909Sdougm 
115867331909Sdougm 				if (zfs_init_libshare(hdl,
115967331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1160fac3008cSeschrock 					/*
1161fac3008cSeschrock 					 * An error occurred so we can't do
1162fac3008cSeschrock 					 * anything
1163fac3008cSeschrock 					 */
116467331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116567331909Sdougm 					    "'%s' cannot be set: problem "
116667331909Sdougm 					    "in share initialization"),
116767331909Sdougm 					    propname);
1168fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1169fac3008cSeschrock 					    errbuf);
117067331909Sdougm 					goto error;
117167331909Sdougm 				}
117267331909Sdougm 
1173da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
117467331909Sdougm 					/*
117567331909Sdougm 					 * There was an error in parsing so
117667331909Sdougm 					 * deal with it by issuing an error
117767331909Sdougm 					 * message and leaving after
117867331909Sdougm 					 * uninitializing the the libshare
117967331909Sdougm 					 * interface.
118067331909Sdougm 					 */
118167331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118267331909Sdougm 					    "'%s' cannot be set to invalid "
118367331909Sdougm 					    "options"), propname);
1184fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1185fac3008cSeschrock 					    errbuf);
118667331909Sdougm 					zfs_uninit_libshare(hdl);
118767331909Sdougm 					goto error;
118867331909Sdougm 				}
118967331909Sdougm 				zfs_uninit_libshare(hdl);
119067331909Sdougm 			}
119167331909Sdougm 
1192f3861e1aSahl 			break;
1193da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1194da6c28aaSamw 			chosen_utf = (int)intval;
1195da6c28aaSamw 			break;
1196da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1197da6c28aaSamw 			chosen_normal = (int)intval;
1198da6c28aaSamw 			break;
1199fa9e4066Sahrens 		}
1200fa9e4066Sahrens 
1201e9dbad6fSeschrock 		/*
1202e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1203e9dbad6fSeschrock 		 * checks to enforce.
1204e9dbad6fSeschrock 		 */
1205e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1206e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1207e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1208e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1209e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1210e9dbad6fSeschrock 			char buf[64];
1211e9dbad6fSeschrock 
1212e9dbad6fSeschrock 			switch (prop) {
1213e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1214a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
1215e9dbad6fSeschrock 				if (intval > volsize) {
1216e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1217e9dbad6fSeschrock 					    "'%s' is greater than current "
1218e9dbad6fSeschrock 					    "volume size"), propname);
1219e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1220e9dbad6fSeschrock 					    errbuf);
1221e9dbad6fSeschrock 					goto error;
1222e9dbad6fSeschrock 				}
1223e9dbad6fSeschrock 				break;
1224e9dbad6fSeschrock 
1225e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1226e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1227e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1228e9dbad6fSeschrock 					    sizeof (buf));
1229e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1230e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1231e9dbad6fSeschrock 					    "volume block size (%s)"),
1232e9dbad6fSeschrock 					    propname, buf);
1233e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1234e9dbad6fSeschrock 					    errbuf);
1235e9dbad6fSeschrock 					goto error;
1236e9dbad6fSeschrock 				}
1237e9dbad6fSeschrock 
1238e9dbad6fSeschrock 				if (intval == 0) {
1239e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1240e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1241e9dbad6fSeschrock 					    propname);
1242e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1243e9dbad6fSeschrock 					    errbuf);
1244e9dbad6fSeschrock 					goto error;
1245e9dbad6fSeschrock 				}
1246f3861e1aSahl 				break;
1247e9dbad6fSeschrock 			}
1248e9dbad6fSeschrock 		}
1249e9dbad6fSeschrock 	}
1250e9dbad6fSeschrock 
1251e9dbad6fSeschrock 	/*
1252da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1253da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1254da6c28aaSamw 	 *
1255da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1256da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1257da6c28aaSamw 	 */
1258de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1259da6c28aaSamw 		if (nvlist_add_uint64(ret,
1260da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1261da6c28aaSamw 			(void) no_memory(hdl);
1262da6c28aaSamw 			goto error;
1263da6c28aaSamw 		}
1264de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1265da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1266da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1267da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1268da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1269da6c28aaSamw 		goto error;
1270da6c28aaSamw 	}
1271e9dbad6fSeschrock 	return (ret);
1272e9dbad6fSeschrock 
1273e9dbad6fSeschrock error:
1274e9dbad6fSeschrock 	nvlist_free(ret);
1275e9dbad6fSeschrock 	return (NULL);
1276e9dbad6fSeschrock }
1277e9dbad6fSeschrock 
127836db6475SEric Taylor int
127936db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
128036db6475SEric Taylor {
128136db6475SEric Taylor 	uint64_t old_volsize;
128236db6475SEric Taylor 	uint64_t new_volsize;
128336db6475SEric Taylor 	uint64_t old_reservation;
128436db6475SEric Taylor 	uint64_t new_reservation;
128536db6475SEric Taylor 	zfs_prop_t resv_prop;
128636db6475SEric Taylor 
128736db6475SEric Taylor 	/*
128836db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
128936db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
129036db6475SEric Taylor 	 */
129136db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
129236db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
129336db6475SEric Taylor 		return (-1);
129436db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
129536db6475SEric Taylor 	if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
129636db6475SEric Taylor 	    old_reservation) || nvlist_lookup_uint64(nvl,
129736db6475SEric Taylor 	    zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
129836db6475SEric Taylor 		return (0);
129936db6475SEric Taylor 	}
130036db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
130136db6475SEric Taylor 	    &new_volsize) != 0)
130236db6475SEric Taylor 		return (-1);
130336db6475SEric Taylor 	new_reservation = zvol_volsize_to_reservation(new_volsize,
130436db6475SEric Taylor 	    zhp->zfs_props);
130536db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
130636db6475SEric Taylor 	    new_reservation) != 0) {
130736db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
130836db6475SEric Taylor 		return (-1);
130936db6475SEric Taylor 	}
131036db6475SEric Taylor 	return (1);
131136db6475SEric Taylor }
131236db6475SEric Taylor 
131392241e0bSTom Erickson void
131492241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
131592241e0bSTom Erickson     char *errbuf)
131692241e0bSTom Erickson {
131792241e0bSTom Erickson 	switch (err) {
131892241e0bSTom Erickson 
131992241e0bSTom Erickson 	case ENOSPC:
132092241e0bSTom Erickson 		/*
132192241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
132292241e0bSTom Erickson 		 * something different; setting a quota or reservation
132392241e0bSTom Erickson 		 * doesn't use any disk space.
132492241e0bSTom Erickson 		 */
132592241e0bSTom Erickson 		switch (prop) {
132692241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
132792241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
132892241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132992241e0bSTom Erickson 			    "size is less than current used or "
133092241e0bSTom Erickson 			    "reserved space"));
133192241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
133292241e0bSTom Erickson 			break;
133392241e0bSTom Erickson 
133492241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
133592241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
133692241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133792241e0bSTom Erickson 			    "size is greater than available space"));
133892241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
133992241e0bSTom Erickson 			break;
134092241e0bSTom Erickson 
134192241e0bSTom Erickson 		default:
134292241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
134392241e0bSTom Erickson 			break;
134492241e0bSTom Erickson 		}
134592241e0bSTom Erickson 		break;
134692241e0bSTom Erickson 
134792241e0bSTom Erickson 	case EBUSY:
134892241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
134992241e0bSTom Erickson 		break;
135092241e0bSTom Erickson 
135192241e0bSTom Erickson 	case EROFS:
135292241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
135392241e0bSTom Erickson 		break;
135492241e0bSTom Erickson 
135592241e0bSTom Erickson 	case ENOTSUP:
135692241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135792241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
135892241e0bSTom Erickson 		    "property or value"));
135992241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
136092241e0bSTom Erickson 		break;
136192241e0bSTom Erickson 
136292241e0bSTom Erickson 	case ERANGE:
136392241e0bSTom Erickson 		if (prop == ZFS_PROP_COMPRESSION) {
136492241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136592241e0bSTom Erickson 			    "property setting is not allowed on "
136692241e0bSTom Erickson 			    "bootable datasets"));
136792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
136892241e0bSTom Erickson 		} else {
136992241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
137092241e0bSTom Erickson 		}
137192241e0bSTom Erickson 		break;
137292241e0bSTom Erickson 
1373ab003da8SJim Dunham 	case EINVAL:
1374ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1375ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1376ab003da8SJim Dunham 		} else {
1377ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1378ab003da8SJim Dunham 		}
1379ab003da8SJim Dunham 		break;
1380ab003da8SJim Dunham 
138192241e0bSTom Erickson 	case EOVERFLOW:
138292241e0bSTom Erickson 		/*
138392241e0bSTom Erickson 		 * This platform can't address a volume this big.
138492241e0bSTom Erickson 		 */
138592241e0bSTom Erickson #ifdef _ILP32
138692241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
138792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
138892241e0bSTom Erickson 			break;
138992241e0bSTom Erickson 		}
139092241e0bSTom Erickson #endif
139192241e0bSTom Erickson 		/* FALLTHROUGH */
139292241e0bSTom Erickson 	default:
139392241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
139492241e0bSTom Erickson 	}
139592241e0bSTom Erickson }
139692241e0bSTom Erickson 
1397e9dbad6fSeschrock /*
1398e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1399e9dbad6fSeschrock  */
1400e9dbad6fSeschrock int
1401e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1402e9dbad6fSeschrock {
1403e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1404e9dbad6fSeschrock 	int ret = -1;
1405e9dbad6fSeschrock 	prop_changelist_t *cl = NULL;
1406e9dbad6fSeschrock 	char errbuf[1024];
1407e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1408e9dbad6fSeschrock 	nvlist_t *nvl = NULL, *realprops;
1409e9dbad6fSeschrock 	zfs_prop_t prop;
14100068372bSMark J Musante 	boolean_t do_prefix;
14110068372bSMark J Musante 	uint64_t idx;
141236db6475SEric Taylor 	int added_resv;
1413e9dbad6fSeschrock 
1414e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1415e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1416e9dbad6fSeschrock 	    zhp->zfs_name);
1417e9dbad6fSeschrock 
1418e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1419e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1420e9dbad6fSeschrock 		(void) no_memory(hdl);
1421e9dbad6fSeschrock 		goto error;
1422e9dbad6fSeschrock 	}
1423e9dbad6fSeschrock 
14240a48a24eStimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1425e9dbad6fSeschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1426e9dbad6fSeschrock 		goto error;
1427990b4856Slling 
1428e9dbad6fSeschrock 	nvlist_free(nvl);
1429e9dbad6fSeschrock 	nvl = realprops;
1430e9dbad6fSeschrock 
1431e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
1432e9dbad6fSeschrock 
143336db6475SEric Taylor 	if (prop == ZFS_PROP_VOLSIZE) {
143436db6475SEric Taylor 		if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
143536db6475SEric Taylor 			goto error;
143636db6475SEric Taylor 	}
143736db6475SEric Taylor 
14380069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1439e9dbad6fSeschrock 		goto error;
1440fa9e4066Sahrens 
1441fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
144299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1443fa9e4066Sahrens 		    "child dataset with inherited mountpoint is used "
144499653d4eSeschrock 		    "in a non-global zone"));
144599653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1446fa9e4066Sahrens 		goto error;
1447fa9e4066Sahrens 	}
1448fa9e4066Sahrens 
14490068372bSMark J Musante 	/*
14500068372bSMark J Musante 	 * If the dataset's canmount property is being set to noauto,
14510068372bSMark J Musante 	 * then we want to prevent unmounting & remounting it.
14520068372bSMark J Musante 	 */
14530068372bSMark J Musante 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
14540068372bSMark J Musante 	    (zprop_string_to_index(prop, propval, &idx,
14550068372bSMark J Musante 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1456a227b7f4Shs24103 
1457a227b7f4Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1458fa9e4066Sahrens 		goto error;
1459fa9e4066Sahrens 
1460fa9e4066Sahrens 	/*
1461fa9e4066Sahrens 	 * Execute the corresponding ioctl() to set this property.
1462fa9e4066Sahrens 	 */
1463fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1464fa9e4066Sahrens 
1465990b4856Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1466e9dbad6fSeschrock 		goto error;
1467e9dbad6fSeschrock 
1468ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1469743a77edSAlan Wright 
1470fa9e4066Sahrens 	if (ret != 0) {
147192241e0bSTom Erickson 		zfs_setprop_error(hdl, prop, errno, errbuf);
147236db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
147336db6475SEric Taylor 			/* clean up the volsize property we tried to set */
147436db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
147536db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
147636db6475SEric Taylor 			nvlist_free(nvl);
147736db6475SEric Taylor 			zcmd_free_nvlists(&zc);
147836db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
147936db6475SEric Taylor 				goto error;
148036db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
148136db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
148236db6475SEric Taylor 			    old_volsize) != 0)
148336db6475SEric Taylor 				goto error;
148436db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
148536db6475SEric Taylor 				goto error;
148636db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
148736db6475SEric Taylor 		}
1488fa9e4066Sahrens 	} else {
1489a227b7f4Shs24103 		if (do_prefix)
1490a227b7f4Shs24103 			ret = changelist_postfix(cl);
1491a227b7f4Shs24103 
1492fa9e4066Sahrens 		/*
1493fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1494fa9e4066Sahrens 		 * is reflected.
1495fa9e4066Sahrens 		 */
1496a227b7f4Shs24103 		if (ret == 0)
1497fa9e4066Sahrens 			(void) get_stats(zhp);
1498fa9e4066Sahrens 	}
1499fa9e4066Sahrens 
1500fa9e4066Sahrens error:
1501e9dbad6fSeschrock 	nvlist_free(nvl);
1502e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1503e9dbad6fSeschrock 	if (cl)
1504fa9e4066Sahrens 		changelist_free(cl);
1505fa9e4066Sahrens 	return (ret);
1506fa9e4066Sahrens }
1507fa9e4066Sahrens 
1508fa9e4066Sahrens /*
150992241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
151092241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1511fa9e4066Sahrens  */
1512fa9e4066Sahrens int
151392241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1514fa9e4066Sahrens {
1515fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1516fa9e4066Sahrens 	int ret;
1517fa9e4066Sahrens 	prop_changelist_t *cl;
151899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
151999653d4eSeschrock 	char errbuf[1024];
1520e9dbad6fSeschrock 	zfs_prop_t prop;
152199653d4eSeschrock 
152299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
152399653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1524fa9e4066Sahrens 
152592241e0bSTom Erickson 	zc.zc_cookie = received;
1526990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1527e9dbad6fSeschrock 		/*
1528e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1529e9dbad6fSeschrock 		 * small, so just do it here.
1530e9dbad6fSeschrock 		 */
1531e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1532e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1533e9dbad6fSeschrock 			    "invalid property"));
1534e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1535e9dbad6fSeschrock 		}
1536e9dbad6fSeschrock 
1537e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1538e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1539e9dbad6fSeschrock 
1540e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1541e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1542e9dbad6fSeschrock 
1543e9dbad6fSeschrock 		return (0);
1544e9dbad6fSeschrock 	}
1545e9dbad6fSeschrock 
1546fa9e4066Sahrens 	/*
1547fa9e4066Sahrens 	 * Verify that this property is inheritable.
1548fa9e4066Sahrens 	 */
154999653d4eSeschrock 	if (zfs_prop_readonly(prop))
155099653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1551fa9e4066Sahrens 
155292241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
155399653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1554fa9e4066Sahrens 
1555fa9e4066Sahrens 	/*
1556fa9e4066Sahrens 	 * Check to see if the value applies to this type
1557fa9e4066Sahrens 	 */
155899653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
155999653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1560fa9e4066Sahrens 
1561bf7c2d40Srm160521 	/*
156236db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1563bf7c2d40Srm160521 	 */
1564bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1565fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1566e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1567fa9e4066Sahrens 
1568fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1569fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
157099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157199653d4eSeschrock 		    "dataset is used in a non-global zone"));
157299653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1573fa9e4066Sahrens 	}
1574fa9e4066Sahrens 
1575fa9e4066Sahrens 	/*
1576fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1577fa9e4066Sahrens 	 */
15780069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1579fa9e4066Sahrens 		return (-1);
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
158299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158399653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
158499653d4eSeschrock 		    "in a non-global zone"));
158599653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1586fa9e4066Sahrens 		goto error;
1587fa9e4066Sahrens 	}
1588fa9e4066Sahrens 
1589fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1590fa9e4066Sahrens 		goto error;
1591fa9e4066Sahrens 
1592e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
159399653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1594fa9e4066Sahrens 	} else {
1595fa9e4066Sahrens 
1596efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1597fa9e4066Sahrens 			goto error;
1598fa9e4066Sahrens 
1599fa9e4066Sahrens 		/*
1600fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1601fa9e4066Sahrens 		 */
1602fa9e4066Sahrens 		(void) get_stats(zhp);
1603fa9e4066Sahrens 	}
1604fa9e4066Sahrens 
1605fa9e4066Sahrens error:
1606fa9e4066Sahrens 	changelist_free(cl);
1607fa9e4066Sahrens 	return (ret);
1608fa9e4066Sahrens }
1609fa9e4066Sahrens 
1610fa9e4066Sahrens /*
16117f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
16127f7322feSeschrock  * extract them appropriately.
16137f7322feSeschrock  */
16147f7322feSeschrock static uint64_t
16157f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16167f7322feSeschrock {
16177f7322feSeschrock 	nvlist_t *nv;
16187f7322feSeschrock 	uint64_t value;
16197f7322feSeschrock 
1620a2eea2e1Sahrens 	*source = NULL;
16217f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
16227f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1623990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1624990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
16257f7322feSeschrock 	} else {
16262e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
16272e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
16287f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
16297f7322feSeschrock 		*source = "";
16307f7322feSeschrock 	}
16317f7322feSeschrock 
16327f7322feSeschrock 	return (value);
16337f7322feSeschrock }
16347f7322feSeschrock 
16357f7322feSeschrock static char *
16367f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
16377f7322feSeschrock {
16387f7322feSeschrock 	nvlist_t *nv;
16397f7322feSeschrock 	char *value;
16407f7322feSeschrock 
1641a2eea2e1Sahrens 	*source = NULL;
16427f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
16437f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1644990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1645990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
16467f7322feSeschrock 	} else {
16472e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
16482e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
16497f7322feSeschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
16507f7322feSeschrock 			value = "";
16517f7322feSeschrock 		*source = "";
16527f7322feSeschrock 	}
16537f7322feSeschrock 
16547f7322feSeschrock 	return (value);
16557f7322feSeschrock }
16567f7322feSeschrock 
165792241e0bSTom Erickson static boolean_t
165892241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
165992241e0bSTom Erickson {
166092241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
166192241e0bSTom Erickson }
166292241e0bSTom Erickson 
166392241e0bSTom Erickson static void
166492241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
166592241e0bSTom Erickson {
166692241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
166792241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
166892241e0bSTom Erickson }
166992241e0bSTom Erickson 
167092241e0bSTom Erickson static void
167192241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
167292241e0bSTom Erickson {
167392241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
167492241e0bSTom Erickson 	*cookie = 0;
167592241e0bSTom Erickson }
167692241e0bSTom Erickson 
16777f7322feSeschrock /*
1678fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1679fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1680fa9e4066Sahrens  *
1681fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1682fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1683fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1684fa9e4066Sahrens  * the source "temporary".
1685fa9e4066Sahrens  */
168699653d4eSeschrock static int
1687990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
168899653d4eSeschrock     char **source, uint64_t *val)
1689fa9e4066Sahrens {
1690bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
169196510749Stimh 	nvlist_t *zplprops = NULL;
1692fa9e4066Sahrens 	struct mnttab mnt;
16933ccfa83cSahrens 	char *mntopt_on = NULL;
16943ccfa83cSahrens 	char *mntopt_off = NULL;
169592241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1696fa9e4066Sahrens 
1697fa9e4066Sahrens 	*source = NULL;
1698fa9e4066Sahrens 
16993ccfa83cSahrens 	switch (prop) {
17003ccfa83cSahrens 	case ZFS_PROP_ATIME:
17013ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
17023ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
17033ccfa83cSahrens 		break;
17043ccfa83cSahrens 
17053ccfa83cSahrens 	case ZFS_PROP_DEVICES:
17063ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
17073ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
17083ccfa83cSahrens 		break;
17093ccfa83cSahrens 
17103ccfa83cSahrens 	case ZFS_PROP_EXEC:
17113ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
17123ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
17133ccfa83cSahrens 		break;
17143ccfa83cSahrens 
17153ccfa83cSahrens 	case ZFS_PROP_READONLY:
17163ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
17173ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
17183ccfa83cSahrens 		break;
17193ccfa83cSahrens 
17203ccfa83cSahrens 	case ZFS_PROP_SETUID:
17213ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
17223ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
17233ccfa83cSahrens 		break;
17243ccfa83cSahrens 
17253ccfa83cSahrens 	case ZFS_PROP_XATTR:
17263ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
17273ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
17283ccfa83cSahrens 		break;
1729da6c28aaSamw 
1730da6c28aaSamw 	case ZFS_PROP_NBMAND:
1731da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
1732da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
1733da6c28aaSamw 		break;
17343ccfa83cSahrens 	}
17353ccfa83cSahrens 
17363bb79becSeschrock 	/*
17373bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
17383bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
17393bb79becSeschrock 	 * we're looking up a property which requires its presence.
17403bb79becSeschrock 	 */
17413bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
17423ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1743ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
1744ebedde84SEric Taylor 		struct mnttab entry;
17453bb79becSeschrock 
1746ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1747ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
17483ccfa83cSahrens 			    entry.mnt_mntopts);
17493ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
17503bb79becSeschrock 				return (-1);
17513ccfa83cSahrens 		}
17523bb79becSeschrock 
17533bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
17543bb79becSeschrock 	}
17553bb79becSeschrock 
1756fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
1757fa9e4066Sahrens 		mnt.mnt_mntopts = "";
1758fa9e4066Sahrens 	else
1759fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1760fa9e4066Sahrens 
1761fa9e4066Sahrens 	switch (prop) {
1762fa9e4066Sahrens 	case ZFS_PROP_ATIME:
1763fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
1764fa9e4066Sahrens 	case ZFS_PROP_EXEC:
17653ccfa83cSahrens 	case ZFS_PROP_READONLY:
17663ccfa83cSahrens 	case ZFS_PROP_SETUID:
17673ccfa83cSahrens 	case ZFS_PROP_XATTR:
1768da6c28aaSamw 	case ZFS_PROP_NBMAND:
176999653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
1770fa9e4066Sahrens 
177192241e0bSTom Erickson 		if (received)
177292241e0bSTom Erickson 			break;
177392241e0bSTom Erickson 
17743ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
177599653d4eSeschrock 			*val = B_TRUE;
1776fa9e4066Sahrens 			if (src)
1777990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
17783ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
177999653d4eSeschrock 			*val = B_FALSE;
1780fa9e4066Sahrens 			if (src)
1781990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
1782fa9e4066Sahrens 		}
178399653d4eSeschrock 		break;
1784fa9e4066Sahrens 
17853ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
1786d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
1787fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
1788a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
1789fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
1790a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
1791a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
179292241e0bSTom Erickson 
179392241e0bSTom Erickson 		if (*source == NULL) {
179492241e0bSTom Erickson 			/* not default, must be local */
1795fa9e4066Sahrens 			*source = zhp->zfs_name;
179692241e0bSTom Erickson 		}
179799653d4eSeschrock 		break;
1798fa9e4066Sahrens 
1799fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
180099653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
180199653d4eSeschrock 		break;
1802fa9e4066Sahrens 
180339c23413Seschrock 	case ZFS_PROP_NUMCLONES:
180439c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
180539c23413Seschrock 		break;
180639c23413Seschrock 
1807bd00f61bSrm160521 	case ZFS_PROP_VERSION:
1808de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
1809de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
1810de8267e0Stimh 	case ZFS_PROP_CASE:
1811de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1812de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
18133d7934e1Srm160521 			return (-1);
1814bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1815de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1816de8267e0Stimh 			zcmd_free_nvlists(&zc);
1817f4b94bdeSMatthew Ahrens 			return (-1);
1818bd00f61bSrm160521 		}
1819de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1820de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1821de8267e0Stimh 		    val) != 0) {
1822de8267e0Stimh 			zcmd_free_nvlists(&zc);
1823f4b94bdeSMatthew Ahrens 			return (-1);
1824de8267e0Stimh 		}
182596510749Stimh 		if (zplprops)
182696510749Stimh 			nvlist_free(zplprops);
1827de8267e0Stimh 		zcmd_free_nvlists(&zc);
1828bd00f61bSrm160521 		break;
1829bd00f61bSrm160521 
1830fa9e4066Sahrens 	default:
1831e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
183291ebeef5Sahrens 		case PROP_TYPE_NUMBER:
183391ebeef5Sahrens 		case PROP_TYPE_INDEX:
1834e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
183574e7dc98SMatthew Ahrens 			/*
183614843421SMatthew Ahrens 			 * If we tried to use a default value for a
183774e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
183831f572c2STom Erickson 			 * present.
183974e7dc98SMatthew Ahrens 			 */
184074e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
184131f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
184231f572c2STom Erickson 				*source = NULL;
184374e7dc98SMatthew Ahrens 			}
1844e7437265Sahrens 			break;
1845e7437265Sahrens 
184691ebeef5Sahrens 		case PROP_TYPE_STRING:
1847e7437265Sahrens 		default:
184899653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
184999653d4eSeschrock 			    "cannot get non-numeric property"));
185099653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
185199653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
1852fa9e4066Sahrens 		}
1853e7437265Sahrens 	}
1854fa9e4066Sahrens 
1855fa9e4066Sahrens 	return (0);
1856fa9e4066Sahrens }
1857fa9e4066Sahrens 
1858fa9e4066Sahrens /*
1859fa9e4066Sahrens  * Calculate the source type, given the raw source string.
1860fa9e4066Sahrens  */
1861fa9e4066Sahrens static void
1862990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1863fa9e4066Sahrens     char *statbuf, size_t statlen)
1864fa9e4066Sahrens {
1865990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1866fa9e4066Sahrens 		return;
1867fa9e4066Sahrens 
1868fa9e4066Sahrens 	if (source == NULL) {
1869990b4856Slling 		*srctype = ZPROP_SRC_NONE;
1870fa9e4066Sahrens 	} else if (source[0] == '\0') {
1871990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
187292241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
187392241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
1874fa9e4066Sahrens 	} else {
1875fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
1876990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
1877fa9e4066Sahrens 		} else {
1878fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
1879990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
1880fa9e4066Sahrens 		}
1881fa9e4066Sahrens 	}
1882fa9e4066Sahrens 
1883fa9e4066Sahrens }
1884fa9e4066Sahrens 
188592241e0bSTom Erickson int
188692241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
188792241e0bSTom Erickson     size_t proplen, boolean_t literal)
188892241e0bSTom Erickson {
188992241e0bSTom Erickson 	zfs_prop_t prop;
189092241e0bSTom Erickson 	int err = 0;
189192241e0bSTom Erickson 
189292241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
189392241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
189492241e0bSTom Erickson 			return (-1);
189592241e0bSTom Erickson 
189692241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
189792241e0bSTom Erickson 
189892241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
189992241e0bSTom Erickson 		uint64_t cookie;
190092241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
190192241e0bSTom Erickson 			return (-1);
190292241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
190392241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
190492241e0bSTom Erickson 		    NULL, NULL, 0, literal);
190592241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
190692241e0bSTom Erickson 	} else {
190792241e0bSTom Erickson 		nvlist_t *propval;
190892241e0bSTom Erickson 		char *recvdval;
190992241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
191092241e0bSTom Erickson 		    propname, &propval) != 0)
191192241e0bSTom Erickson 			return (-1);
191292241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
191392241e0bSTom Erickson 		    &recvdval) == 0);
191492241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
191592241e0bSTom Erickson 	}
191692241e0bSTom Erickson 
191792241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
191892241e0bSTom Erickson }
191992241e0bSTom Erickson 
192019b94df9SMatthew Ahrens static int
192119b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
192219b94df9SMatthew Ahrens {
192319b94df9SMatthew Ahrens 	nvlist_t *value;
192419b94df9SMatthew Ahrens 	nvpair_t *pair;
192519b94df9SMatthew Ahrens 
192619b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
192719b94df9SMatthew Ahrens 	if (value == NULL)
192819b94df9SMatthew Ahrens 		return (-1);
192919b94df9SMatthew Ahrens 
193019b94df9SMatthew Ahrens 	propbuf[0] = '\0';
193119b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
193219b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
193319b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
193419b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
193519b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
193619b94df9SMatthew Ahrens 	}
193719b94df9SMatthew Ahrens 
193819b94df9SMatthew Ahrens 	return (0);
193919b94df9SMatthew Ahrens }
194019b94df9SMatthew Ahrens 
194119b94df9SMatthew Ahrens struct get_clones_arg {
194219b94df9SMatthew Ahrens 	uint64_t numclones;
194319b94df9SMatthew Ahrens 	nvlist_t *value;
194419b94df9SMatthew Ahrens 	const char *origin;
194519b94df9SMatthew Ahrens 	char buf[ZFS_MAXNAMELEN];
194619b94df9SMatthew Ahrens };
194719b94df9SMatthew Ahrens 
194819b94df9SMatthew Ahrens int
194919b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
195019b94df9SMatthew Ahrens {
195119b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
195219b94df9SMatthew Ahrens 
195319b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
195419b94df9SMatthew Ahrens 		zfs_close(zhp);
195519b94df9SMatthew Ahrens 		return (0);
195619b94df9SMatthew Ahrens 	}
195719b94df9SMatthew Ahrens 
195819b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
195919b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
196019b94df9SMatthew Ahrens 		goto out;
196119b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
196219b94df9SMatthew Ahrens 		if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
196319b94df9SMatthew Ahrens 			zfs_close(zhp);
196419b94df9SMatthew Ahrens 			return (no_memory(zhp->zfs_hdl));
196519b94df9SMatthew Ahrens 		}
196619b94df9SMatthew Ahrens 		gca->numclones--;
196719b94df9SMatthew Ahrens 	}
196819b94df9SMatthew Ahrens 
196919b94df9SMatthew Ahrens out:
197019b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
197119b94df9SMatthew Ahrens 	zfs_close(zhp);
197219b94df9SMatthew Ahrens 	return (0);
197319b94df9SMatthew Ahrens }
197419b94df9SMatthew Ahrens 
197519b94df9SMatthew Ahrens nvlist_t *
197619b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
197719b94df9SMatthew Ahrens {
197819b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
197919b94df9SMatthew Ahrens 
198019b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
198119b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
198219b94df9SMatthew Ahrens 		struct get_clones_arg gca;
198319b94df9SMatthew Ahrens 
198419b94df9SMatthew Ahrens 		/*
198519b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
198619b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
198719b94df9SMatthew Ahrens 		 */
198819b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
198919b94df9SMatthew Ahrens 			return (NULL);
199019b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
199119b94df9SMatthew Ahrens 			return (NULL);
199219b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
199319b94df9SMatthew Ahrens 			nvlist_free(nv);
199419b94df9SMatthew Ahrens 			return (NULL);
199519b94df9SMatthew Ahrens 		}
199619b94df9SMatthew Ahrens 
199719b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
199819b94df9SMatthew Ahrens 		gca.value = value;
199919b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
200019b94df9SMatthew Ahrens 
200119b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
200219b94df9SMatthew Ahrens 			zfs_handle_t *root;
200319b94df9SMatthew Ahrens 			char pool[ZFS_MAXNAMELEN];
200419b94df9SMatthew Ahrens 			char *cp = pool;
200519b94df9SMatthew Ahrens 
200619b94df9SMatthew Ahrens 			/* get the pool name */
200719b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
200819b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
200919b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
201019b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
201119b94df9SMatthew Ahrens 
201219b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
201319b94df9SMatthew Ahrens 		}
201419b94df9SMatthew Ahrens 
201519b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
201619b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
201719b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
201819b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
201919b94df9SMatthew Ahrens 			nvlist_free(nv);
202019b94df9SMatthew Ahrens 			nvlist_free(value);
202119b94df9SMatthew Ahrens 			return (NULL);
202219b94df9SMatthew Ahrens 		}
202319b94df9SMatthew Ahrens 		nvlist_free(nv);
202419b94df9SMatthew Ahrens 		nvlist_free(value);
202519b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
202619b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
202719b94df9SMatthew Ahrens 	}
202819b94df9SMatthew Ahrens 
202919b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
203019b94df9SMatthew Ahrens 
203119b94df9SMatthew Ahrens 	return (value);
203219b94df9SMatthew Ahrens }
203319b94df9SMatthew Ahrens 
2034fa9e4066Sahrens /*
2035fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2036fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2037fa9e4066Sahrens  * human-readable form.
2038fa9e4066Sahrens  *
2039fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2040fa9e4066Sahrens  */
2041fa9e4066Sahrens int
2042fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2043990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2044fa9e4066Sahrens {
2045fa9e4066Sahrens 	char *source = NULL;
2046fa9e4066Sahrens 	uint64_t val;
2047fa9e4066Sahrens 	char *str;
2048e9dbad6fSeschrock 	const char *strval;
204992241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2050fa9e4066Sahrens 
2051fa9e4066Sahrens 	/*
2052fa9e4066Sahrens 	 * Check to see if this property applies to our object
2053fa9e4066Sahrens 	 */
2054fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2055fa9e4066Sahrens 		return (-1);
2056fa9e4066Sahrens 
205792241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
205892241e0bSTom Erickson 		return (-1);
205992241e0bSTom Erickson 
2060fa9e4066Sahrens 	if (src)
2061990b4856Slling 		*src = ZPROP_SRC_NONE;
2062fa9e4066Sahrens 
2063fa9e4066Sahrens 	switch (prop) {
2064fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2065fa9e4066Sahrens 		/*
2066fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2067fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2068fa9e4066Sahrens 		 */
2069fa9e4066Sahrens 		{
2070a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2071a2eea2e1Sahrens 			time_t time = (time_t)val;
2072fa9e4066Sahrens 			struct tm t;
2073fa9e4066Sahrens 
2074fa9e4066Sahrens 			if (literal ||
2075fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2076fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2077fa9e4066Sahrens 			    &t) == 0)
2078a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2079fa9e4066Sahrens 		}
2080fa9e4066Sahrens 		break;
2081fa9e4066Sahrens 
2082fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2083fa9e4066Sahrens 		/*
2084fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2085fa9e4066Sahrens 		 *
2086fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2087fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2088fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2089fa9e4066Sahrens 		 *
2090fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2091fa9e4066Sahrens 		 * root to any values we return.
2092fa9e4066Sahrens 		 */
209329ab75c9Srm160521 
20947f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2095fa9e4066Sahrens 
2096b21718f0Sgw25295 		if (str[0] == '/') {
209729ab75c9Srm160521 			char buf[MAXPATHLEN];
209829ab75c9Srm160521 			char *root = buf;
2099a79992aaSTom Erickson 			const char *relpath;
2100fa9e4066Sahrens 
2101a79992aaSTom Erickson 			/*
2102a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2103a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2104a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2105a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2106a79992aaSTom Erickson 			 * inherited.
2107a79992aaSTom Erickson 			 */
2108a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2109a79992aaSTom Erickson 				relpath = "";
2110a79992aaSTom Erickson 			} else {
2111a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2112fa9e4066Sahrens 				if (relpath[0] == '/')
2113fa9e4066Sahrens 					relpath++;
2114a79992aaSTom Erickson 			}
2115b21718f0Sgw25295 
211629ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
211729ab75c9Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
211829ab75c9Srm160521 			    (strcmp(root, "-") == 0))
211929ab75c9Srm160521 				root[0] = '\0';
2120b21718f0Sgw25295 			/*
2121b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2122b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2123b21718f0Sgw25295 			 * mountpoint path.
2124b21718f0Sgw25295 			 */
2125b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2126b21718f0Sgw25295 				root++;
2127b21718f0Sgw25295 
2128b21718f0Sgw25295 			/*
2129b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2130b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2131b21718f0Sgw25295 			 * an inherited mountpoint.
2132b21718f0Sgw25295 			 */
2133b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2134b21718f0Sgw25295 			    relpath[0] != '\0'))
21357f7322feSeschrock 				str++;
2136fa9e4066Sahrens 
2137fa9e4066Sahrens 			if (relpath[0] == '\0')
2138fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
21397f7322feSeschrock 				    root, str);
2140fa9e4066Sahrens 			else
2141fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
21427f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2143fa9e4066Sahrens 				    relpath);
2144fa9e4066Sahrens 		} else {
2145fa9e4066Sahrens 			/* 'legacy' or 'none' */
21467f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2147fa9e4066Sahrens 		}
2148fa9e4066Sahrens 
2149fa9e4066Sahrens 		break;
2150fa9e4066Sahrens 
2151fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
2152a2eea2e1Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2153fa9e4066Sahrens 		    proplen);
2154fa9e4066Sahrens 		/*
2155fa9e4066Sahrens 		 * If there is no parent at all, return failure to indicate that
2156fa9e4066Sahrens 		 * it doesn't apply to this dataset.
2157fa9e4066Sahrens 		 */
2158fa9e4066Sahrens 		if (propbuf[0] == '\0')
2159fa9e4066Sahrens 			return (-1);
2160fa9e4066Sahrens 		break;
2161fa9e4066Sahrens 
216219b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
216319b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
216419b94df9SMatthew Ahrens 			return (-1);
216519b94df9SMatthew Ahrens 		break;
216619b94df9SMatthew Ahrens 
2167fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2168a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2169fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2170a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2171a9799022Sck153898 
217299653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
217399653d4eSeschrock 			return (-1);
2174fa9e4066Sahrens 
2175fa9e4066Sahrens 		/*
2176fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2177fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2178fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2179fa9e4066Sahrens 		 * that its set locally.
2180fa9e4066Sahrens 		 */
2181fa9e4066Sahrens 		if (val == 0) {
2182fa9e4066Sahrens 			if (literal)
2183fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2184fa9e4066Sahrens 			else
2185fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2186fa9e4066Sahrens 		} else {
2187fa9e4066Sahrens 			if (literal)
21885ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
21895ad82045Snd150628 				    (u_longlong_t)val);
2190fa9e4066Sahrens 			else
2191fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2192fa9e4066Sahrens 		}
2193fa9e4066Sahrens 		break;
2194fa9e4066Sahrens 
2195187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2196fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
219799653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
219899653d4eSeschrock 			return (-1);
2199b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2200b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2201b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2202fa9e4066Sahrens 		break;
2203fa9e4066Sahrens 
2204fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2205fa9e4066Sahrens 		switch (zhp->zfs_type) {
2206fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2207fa9e4066Sahrens 			str = "filesystem";
2208fa9e4066Sahrens 			break;
2209fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2210fa9e4066Sahrens 			str = "volume";
2211fa9e4066Sahrens 			break;
2212fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2213fa9e4066Sahrens 			str = "snapshot";
2214fa9e4066Sahrens 			break;
2215fa9e4066Sahrens 		default:
221699653d4eSeschrock 			abort();
2217fa9e4066Sahrens 		}
2218fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2219fa9e4066Sahrens 		break;
2220fa9e4066Sahrens 
2221fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2222fa9e4066Sahrens 		/*
2223fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2224fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2225fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2226fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2227fa9e4066Sahrens 		 */
222899653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
222999653d4eSeschrock 		    src, &source, &val) != 0)
223099653d4eSeschrock 			return (-1);
223199653d4eSeschrock 		if (val)
2232fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2233fa9e4066Sahrens 		else
2234fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2235fa9e4066Sahrens 		break;
2236fa9e4066Sahrens 
2237fa9e4066Sahrens 	case ZFS_PROP_NAME:
2238fa9e4066Sahrens 		/*
2239fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2240fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2241fa9e4066Sahrens 		 * consumers.
2242fa9e4066Sahrens 		 */
2243fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2244fa9e4066Sahrens 		break;
2245fa9e4066Sahrens 
22464201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
22474201a95eSRic Aleshire 		{
22484201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
22494201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
22504201a95eSRic Aleshire 
22514201a95eSRic Aleshire 			(void) strlcpy(propbuf,
22524201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
22534201a95eSRic Aleshire 
22544201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
22554201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
22564201a95eSRic Aleshire 				break;
22574201a95eSRic Aleshire 
22584201a95eSRic Aleshire 			/*
22594201a95eSRic Aleshire 			 * Try to translate the internal hex string to
22604201a95eSRic Aleshire 			 * human-readable output.  If there are any
22614201a95eSRic Aleshire 			 * problems just use the hex string.
22624201a95eSRic Aleshire 			 */
22634201a95eSRic Aleshire 
22644201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
22654201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
22664201a95eSRic Aleshire 				m_label_free(new_sl);
22674201a95eSRic Aleshire 				break;
22684201a95eSRic Aleshire 			}
22694201a95eSRic Aleshire 
22704201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
22714201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
22724201a95eSRic Aleshire 				if (ascii)
22734201a95eSRic Aleshire 					free(ascii);
22744201a95eSRic Aleshire 				m_label_free(new_sl);
22754201a95eSRic Aleshire 				break;
22764201a95eSRic Aleshire 			}
22774201a95eSRic Aleshire 			m_label_free(new_sl);
22784201a95eSRic Aleshire 
22794201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
22804201a95eSRic Aleshire 			free(ascii);
22814201a95eSRic Aleshire 		}
22824201a95eSRic Aleshire 		break;
22834201a95eSRic Aleshire 
2284*f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2285*f0f3ef5aSGarrett D'Amore 		/*
2286*f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2287*f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2288*f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2289*f0f3ef5aSGarrett D'Amore 		 */
2290*f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2291*f0f3ef5aSGarrett D'Amore 			return (-1);
2292*f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2293*f0f3ef5aSGarrett D'Amore 		break;
2294*f0f3ef5aSGarrett D'Amore 
2295fa9e4066Sahrens 	default:
2296e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
229791ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2298e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2299e7437265Sahrens 			    &source, &val) != 0)
2300e7437265Sahrens 				return (-1);
2301e7437265Sahrens 			if (literal)
2302e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2303e7437265Sahrens 				    (u_longlong_t)val);
2304e7437265Sahrens 			else
2305e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2306e7437265Sahrens 			break;
2307e7437265Sahrens 
230891ebeef5Sahrens 		case PROP_TYPE_STRING:
2309e7437265Sahrens 			(void) strlcpy(propbuf,
2310e7437265Sahrens 			    getprop_string(zhp, prop, &source), proplen);
2311e7437265Sahrens 			break;
2312e7437265Sahrens 
231391ebeef5Sahrens 		case PROP_TYPE_INDEX:
2314fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2315fe192f76Sahrens 			    &source, &val) != 0)
2316fe192f76Sahrens 				return (-1);
2317fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2318e7437265Sahrens 				return (-1);
2319e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2320e7437265Sahrens 			break;
2321e7437265Sahrens 
2322e7437265Sahrens 		default:
232399653d4eSeschrock 			abort();
2324fa9e4066Sahrens 		}
2325e7437265Sahrens 	}
2326fa9e4066Sahrens 
2327fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2328fa9e4066Sahrens 
2329fa9e4066Sahrens 	return (0);
2330fa9e4066Sahrens }
2331fa9e4066Sahrens 
2332fa9e4066Sahrens /*
2333fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2334fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2335fa9e4066Sahrens  * hard-coded property types.
2336fa9e4066Sahrens  */
2337fa9e4066Sahrens uint64_t
2338fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2339fa9e4066Sahrens {
2340fa9e4066Sahrens 	char *source;
234199653d4eSeschrock 	uint64_t val;
2342fa9e4066Sahrens 
23433cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
234499653d4eSeschrock 
234599653d4eSeschrock 	return (val);
2346fa9e4066Sahrens }
2347fa9e4066Sahrens 
23487b97dc1aSrm160521 int
23497b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
23507b97dc1aSrm160521 {
23517b97dc1aSrm160521 	char buf[64];
23527b97dc1aSrm160521 
235314843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
23547b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
23557b97dc1aSrm160521 }
23567b97dc1aSrm160521 
2357fa9e4066Sahrens /*
2358fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2359fa9e4066Sahrens  */
2360fa9e4066Sahrens int
2361fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2362990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2363fa9e4066Sahrens {
2364fa9e4066Sahrens 	char *source;
2365fa9e4066Sahrens 
2366fa9e4066Sahrens 	/*
2367fa9e4066Sahrens 	 * Check to see if this property applies to our object
2368fa9e4066Sahrens 	 */
2369e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2370ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
237199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
237299653d4eSeschrock 		    zfs_prop_to_name(prop)));
2373e45ce728Sahrens 	}
2374fa9e4066Sahrens 
2375fa9e4066Sahrens 	if (src)
2376990b4856Slling 		*src = ZPROP_SRC_NONE;
2377fa9e4066Sahrens 
237899653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
237999653d4eSeschrock 		return (-1);
2380fa9e4066Sahrens 
2381fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2382fa9e4066Sahrens 
2383fa9e4066Sahrens 	return (0);
2384fa9e4066Sahrens }
2385fa9e4066Sahrens 
238614843421SMatthew Ahrens static int
238714843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
238814843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
238914843421SMatthew Ahrens {
239014843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
239114843421SMatthew Ahrens 	idmap_stat status;
239214843421SMatthew Ahrens 	int err = EINVAL;
239314843421SMatthew Ahrens 
23941fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
239514843421SMatthew Ahrens 		goto out;
239614843421SMatthew Ahrens 
239714843421SMatthew Ahrens 	if (isuser) {
239814843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
239914843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
240014843421SMatthew Ahrens 	} else {
240114843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
240214843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
240314843421SMatthew Ahrens 	}
240414843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
240514843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
240614843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
240714843421SMatthew Ahrens 		err = 0;
240814843421SMatthew Ahrens 	else
240914843421SMatthew Ahrens 		err = EINVAL;
241014843421SMatthew Ahrens out:
241114843421SMatthew Ahrens 	if (get_hdl)
241214843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
241314843421SMatthew Ahrens 	return (err);
241414843421SMatthew Ahrens }
241514843421SMatthew Ahrens 
241614843421SMatthew Ahrens /*
241714843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
241814843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
241914843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
242014843421SMatthew Ahrens  */
242114843421SMatthew Ahrens static int
242214843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
242314843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
242414843421SMatthew Ahrens {
242514843421SMatthew Ahrens 	zfs_userquota_prop_t type;
242614843421SMatthew Ahrens 	char *cp, *end;
24273b12c289SMatthew Ahrens 	char *numericsid = NULL;
242814843421SMatthew Ahrens 	boolean_t isuser;
242914843421SMatthew Ahrens 
243014843421SMatthew Ahrens 	domain[0] = '\0';
243114843421SMatthew Ahrens 
243214843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
243314843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
243414843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
243514843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
243614843421SMatthew Ahrens 			break;
243714843421SMatthew Ahrens 	}
243814843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
243914843421SMatthew Ahrens 		return (EINVAL);
244014843421SMatthew Ahrens 	*typep = type;
244114843421SMatthew Ahrens 
244214843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
244314843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
244414843421SMatthew Ahrens 
244514843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
244614843421SMatthew Ahrens 
244714843421SMatthew Ahrens 	if (strchr(cp, '@')) {
244814843421SMatthew Ahrens 		/*
244914843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
24503b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
245114843421SMatthew Ahrens 		 */
24523b12c289SMatthew Ahrens 		directory_error_t e;
245314843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
245414843421SMatthew Ahrens 			return (ENOENT);
24553b12c289SMatthew Ahrens 		if (isuser) {
24563b12c289SMatthew Ahrens 			e = directory_sid_from_user_name(NULL,
24573b12c289SMatthew Ahrens 			    cp, &numericsid);
24583b12c289SMatthew Ahrens 		} else {
24593b12c289SMatthew Ahrens 			e = directory_sid_from_group_name(NULL,
24603b12c289SMatthew Ahrens 			    cp, &numericsid);
24613b12c289SMatthew Ahrens 		}
24623b12c289SMatthew Ahrens 		if (e != NULL) {
24633b12c289SMatthew Ahrens 			directory_error_free(e);
246414843421SMatthew Ahrens 			return (ENOENT);
24653b12c289SMatthew Ahrens 		}
24663b12c289SMatthew Ahrens 		if (numericsid == NULL)
246714843421SMatthew Ahrens 			return (ENOENT);
24683b12c289SMatthew Ahrens 		cp = numericsid;
24693b12c289SMatthew Ahrens 		/* will be further decoded below */
24703b12c289SMatthew Ahrens 	}
24713b12c289SMatthew Ahrens 
24723b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
247314843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
24743b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
247514843421SMatthew Ahrens 		cp = strrchr(domain, '-');
247614843421SMatthew Ahrens 		*cp = '\0';
247714843421SMatthew Ahrens 		cp++;
247814843421SMatthew Ahrens 
247914843421SMatthew Ahrens 		errno = 0;
248014843421SMatthew Ahrens 		*ridp = strtoull(cp, &end, 10);
24813b12c289SMatthew Ahrens 		if (numericsid) {
24823b12c289SMatthew Ahrens 			free(numericsid);
24833b12c289SMatthew Ahrens 			numericsid = NULL;
24843b12c289SMatthew Ahrens 		}
2485777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
248614843421SMatthew Ahrens 			return (EINVAL);
248714843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
248814843421SMatthew Ahrens 		/*
248914843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
249014843421SMatthew Ahrens 		 * turned into a uid/gid
249114843421SMatthew Ahrens 		 */
249214843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
249314843421SMatthew Ahrens 			return (ENOENT);
249414843421SMatthew Ahrens 		if (isuser) {
249514843421SMatthew Ahrens 			struct passwd *pw;
249614843421SMatthew Ahrens 			pw = getpwnam(cp);
249714843421SMatthew Ahrens 			if (pw == NULL)
249814843421SMatthew Ahrens 				return (ENOENT);
249914843421SMatthew Ahrens 			*ridp = pw->pw_uid;
250014843421SMatthew Ahrens 		} else {
250114843421SMatthew Ahrens 			struct group *gr;
250214843421SMatthew Ahrens 			gr = getgrnam(cp);
250314843421SMatthew Ahrens 			if (gr == NULL)
250414843421SMatthew Ahrens 				return (ENOENT);
250514843421SMatthew Ahrens 			*ridp = gr->gr_gid;
250614843421SMatthew Ahrens 		}
250714843421SMatthew Ahrens 	} else {
250814843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
250914843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
251014843421SMatthew Ahrens 		idmap_rid_t rid;
251114843421SMatthew Ahrens 		char *mapdomain;
251214843421SMatthew Ahrens 
251314843421SMatthew Ahrens 		if (*end != '\0')
251414843421SMatthew Ahrens 			return (EINVAL);
251514843421SMatthew Ahrens 		if (id > MAXUID) {
251614843421SMatthew Ahrens 			/* It's an ephemeral ID. */
251714843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
251814843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
251914843421SMatthew Ahrens 				return (ENOENT);
25203b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
252114843421SMatthew Ahrens 			*ridp = rid;
252214843421SMatthew Ahrens 		} else {
252314843421SMatthew Ahrens 			*ridp = id;
252414843421SMatthew Ahrens 		}
252514843421SMatthew Ahrens 	}
252614843421SMatthew Ahrens 
25273b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
252814843421SMatthew Ahrens 	return (0);
252914843421SMatthew Ahrens }
253014843421SMatthew Ahrens 
2531edea4b55SLin Ling static int
2532edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2533edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
253414843421SMatthew Ahrens {
253514843421SMatthew Ahrens 	int err;
253614843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
253714843421SMatthew Ahrens 
253819b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
253914843421SMatthew Ahrens 
254014843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
254114843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2542edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2543edea4b55SLin Ling 	zc.zc_objset_type = *typep;
254414843421SMatthew Ahrens 	if (err)
254514843421SMatthew Ahrens 		return (err);
254614843421SMatthew Ahrens 
254714843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
254814843421SMatthew Ahrens 	if (err)
254914843421SMatthew Ahrens 		return (err);
255014843421SMatthew Ahrens 
2551edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2552edea4b55SLin Ling 	return (0);
2553edea4b55SLin Ling }
2554edea4b55SLin Ling 
2555edea4b55SLin Ling int
2556edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2557edea4b55SLin Ling     uint64_t *propvalue)
2558edea4b55SLin Ling {
2559edea4b55SLin Ling 	zfs_userquota_prop_t type;
2560edea4b55SLin Ling 
2561edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2562edea4b55SLin Ling 	    &type));
2563edea4b55SLin Ling }
2564edea4b55SLin Ling 
2565edea4b55SLin Ling int
2566edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2567edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2568edea4b55SLin Ling {
2569edea4b55SLin Ling 	int err;
2570edea4b55SLin Ling 	uint64_t propvalue;
2571edea4b55SLin Ling 	zfs_userquota_prop_t type;
2572edea4b55SLin Ling 
2573edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2574edea4b55SLin Ling 	    &type);
2575edea4b55SLin Ling 
2576edea4b55SLin Ling 	if (err)
2577edea4b55SLin Ling 		return (err);
2578edea4b55SLin Ling 
257914843421SMatthew Ahrens 	if (literal) {
2580edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2581edea4b55SLin Ling 	} else if (propvalue == 0 &&
258214843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
258314843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
258414843421SMatthew Ahrens 	} else {
2585edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
258614843421SMatthew Ahrens 	}
258714843421SMatthew Ahrens 	return (0);
258814843421SMatthew Ahrens }
258914843421SMatthew Ahrens 
259019b94df9SMatthew Ahrens int
259119b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
259219b94df9SMatthew Ahrens     uint64_t *propvalue)
259319b94df9SMatthew Ahrens {
259419b94df9SMatthew Ahrens 	int err;
259519b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
259619b94df9SMatthew Ahrens 	const char *snapname;
259719b94df9SMatthew Ahrens 
259819b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
259919b94df9SMatthew Ahrens 
260019b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
260119b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
260219b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
260319b94df9SMatthew Ahrens 	} else {
260419b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
260519b94df9SMatthew Ahrens 		char *cp;
260619b94df9SMatthew Ahrens 
260719b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
260819b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
260919b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
261019b94df9SMatthew Ahrens 		if (cp != NULL)
261119b94df9SMatthew Ahrens 			*cp = '\0';
261219b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
261319b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
261419b94df9SMatthew Ahrens 	}
261519b94df9SMatthew Ahrens 
261619b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
261719b94df9SMatthew Ahrens 	if (err)
261819b94df9SMatthew Ahrens 		return (err);
261919b94df9SMatthew Ahrens 
262019b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
262119b94df9SMatthew Ahrens 	return (0);
262219b94df9SMatthew Ahrens }
262319b94df9SMatthew Ahrens 
262419b94df9SMatthew Ahrens int
262519b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
262619b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
262719b94df9SMatthew Ahrens {
262819b94df9SMatthew Ahrens 	int err;
262919b94df9SMatthew Ahrens 	uint64_t propvalue;
263019b94df9SMatthew Ahrens 
263119b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
263219b94df9SMatthew Ahrens 
263319b94df9SMatthew Ahrens 	if (err)
263419b94df9SMatthew Ahrens 		return (err);
263519b94df9SMatthew Ahrens 
263619b94df9SMatthew Ahrens 	if (literal) {
263719b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
263819b94df9SMatthew Ahrens 	} else {
263919b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
264019b94df9SMatthew Ahrens 	}
264119b94df9SMatthew Ahrens 	return (0);
264219b94df9SMatthew Ahrens }
264319b94df9SMatthew Ahrens 
264419b94df9SMatthew Ahrens int
264519b94df9SMatthew Ahrens zfs_get_snapused_int(zfs_handle_t *firstsnap, zfs_handle_t *lastsnap,
264619b94df9SMatthew Ahrens     uint64_t *usedp)
264719b94df9SMatthew Ahrens {
264819b94df9SMatthew Ahrens 	int err;
264919b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
265019b94df9SMatthew Ahrens 
265119b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, lastsnap->zfs_name, sizeof (zc.zc_name));
265219b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_value, firstsnap->zfs_name, sizeof (zc.zc_value));
265319b94df9SMatthew Ahrens 
265419b94df9SMatthew Ahrens 	err = ioctl(lastsnap->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_SNAPS, &zc);
265519b94df9SMatthew Ahrens 	if (err)
265619b94df9SMatthew Ahrens 		return (err);
265719b94df9SMatthew Ahrens 
265819b94df9SMatthew Ahrens 	*usedp = zc.zc_cookie;
265919b94df9SMatthew Ahrens 
266019b94df9SMatthew Ahrens 	return (0);
266119b94df9SMatthew Ahrens }
266219b94df9SMatthew Ahrens 
2663fa9e4066Sahrens /*
2664fa9e4066Sahrens  * Returns the name of the given zfs handle.
2665fa9e4066Sahrens  */
2666fa9e4066Sahrens const char *
2667fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
2668fa9e4066Sahrens {
2669fa9e4066Sahrens 	return (zhp->zfs_name);
2670fa9e4066Sahrens }
2671fa9e4066Sahrens 
2672fa9e4066Sahrens /*
2673fa9e4066Sahrens  * Returns the type of the given zfs handle.
2674fa9e4066Sahrens  */
2675fa9e4066Sahrens zfs_type_t
2676fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
2677fa9e4066Sahrens {
2678fa9e4066Sahrens 	return (zhp->zfs_type);
2679fa9e4066Sahrens }
2680fa9e4066Sahrens 
26817f7322feSeschrock /*
2682d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
2683d41c4376SMark J Musante  *
2684d41c4376SMark J Musante  * Needs to handle these cases:
2685d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2686d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2687d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
2688d41c4376SMark J Musante  */
2689d41c4376SMark J Musante static boolean_t
2690d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
2691d41c4376SMark J Musante {
2692d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
2693d41c4376SMark J Musante 
2694d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
2695d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
2696d41c4376SMark J Musante 		return (B_FALSE);
2697d41c4376SMark J Musante 
2698d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
2699d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2700d41c4376SMark J Musante }
2701d41c4376SMark J Musante 
2702d41c4376SMark J Musante /*
2703fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
270419b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
270519b94df9SMatthew Ahrens  * pool).
2706fa9e4066Sahrens  */
2707fa9e4066Sahrens static int
2708fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
2709fa9e4066Sahrens {
271019b94df9SMatthew Ahrens 	char *slashp;
2711fa9e4066Sahrens 
271219b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
271319b94df9SMatthew Ahrens 
271419b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
2715fa9e4066Sahrens 		return (-1);
271619b94df9SMatthew Ahrens 	*slashp = '\0';
2717fa9e4066Sahrens 
2718fa9e4066Sahrens 	return (0);
2719fa9e4066Sahrens }
2720fa9e4066Sahrens 
2721fa9e4066Sahrens /*
27227f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
27237f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
27247f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
27257f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
27267f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
27277f1f55eaSvb160487  * new datasets.
2728fa9e4066Sahrens  */
2729fa9e4066Sahrens static int
27307f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27317f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
2732fa9e4066Sahrens {
2733fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2734fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
2735fa9e4066Sahrens 	char *slash;
27367f7322feSeschrock 	zfs_handle_t *zhp;
273799653d4eSeschrock 	char errbuf[1024];
2738d41c4376SMark J Musante 	uint64_t is_zoned;
273999653d4eSeschrock 
2740deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
2741deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2742fa9e4066Sahrens 
2743fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
2744fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
274599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
274699653d4eSeschrock 		    "missing dataset name"));
274799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2748fa9e4066Sahrens 	}
2749fa9e4066Sahrens 
2750fa9e4066Sahrens 	/* check to see if the pool exists */
2751fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2752fa9e4066Sahrens 		slash = parent + strlen(parent);
27538ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
2754fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
275599653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2756fa9e4066Sahrens 	    errno == ENOENT) {
275799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
275899653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
275999653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2760fa9e4066Sahrens 	}
2761fa9e4066Sahrens 
2762fa9e4066Sahrens 	/* check to see if the parent dataset exists */
27637f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27647f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
27657f1f55eaSvb160487 			/*
27667f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
27677f1f55eaSvb160487 			 */
27687f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27697f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27707f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
27717f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27727f1f55eaSvb160487 			}
27737f1f55eaSvb160487 		} else if (errno == ENOENT) {
277499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
277599653d4eSeschrock 			    "parent does not exist"));
277699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27777f1f55eaSvb160487 		} else
277899653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2779fa9e4066Sahrens 	}
2780fa9e4066Sahrens 
2781d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2782d41c4376SMark J Musante 	if (zoned != NULL)
2783d41c4376SMark J Musante 		*zoned = is_zoned;
2784d41c4376SMark J Musante 
2785fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
2786d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
278799653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27887f7322feSeschrock 		zfs_close(zhp);
2789fa9e4066Sahrens 		return (-1);
2790fa9e4066Sahrens 	}
2791fa9e4066Sahrens 
2792fa9e4066Sahrens 	/* make sure parent is a filesystem */
27937f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
279499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
279599653d4eSeschrock 		    "parent is not a filesystem"));
279699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27977f7322feSeschrock 		zfs_close(zhp);
2798fa9e4066Sahrens 		return (-1);
2799fa9e4066Sahrens 	}
2800fa9e4066Sahrens 
28017f7322feSeschrock 	zfs_close(zhp);
28027f1f55eaSvb160487 	if (prefixlen != NULL)
28037f1f55eaSvb160487 		*prefixlen = strlen(parent);
28047f1f55eaSvb160487 	return (0);
28057f1f55eaSvb160487 }
28067f1f55eaSvb160487 
28077f1f55eaSvb160487 /*
28087f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
28097f1f55eaSvb160487  */
28107f1f55eaSvb160487 boolean_t
28117f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
28127f1f55eaSvb160487 {
28137f1f55eaSvb160487 	zfs_handle_t *zhp;
28147f1f55eaSvb160487 
2815f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
28167f1f55eaSvb160487 		return (B_FALSE);
28177f1f55eaSvb160487 
28187f1f55eaSvb160487 	/*
28197f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
28207f1f55eaSvb160487 	 */
28217f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
28227f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
28237f1f55eaSvb160487 
28247f1f55eaSvb160487 		zfs_close(zhp);
28257f1f55eaSvb160487 		if (types & ds_type)
28267f1f55eaSvb160487 			return (B_TRUE);
28277f1f55eaSvb160487 	}
28287f1f55eaSvb160487 	return (B_FALSE);
28297f1f55eaSvb160487 }
28307f1f55eaSvb160487 
28317f1f55eaSvb160487 /*
28323cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
28333cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
28343cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
28353cb34c60Sahrens  */
28363cb34c60Sahrens int
28373cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
28383cb34c60Sahrens {
28393cb34c60Sahrens 	zfs_handle_t *h;
28403cb34c60Sahrens 	char *cp;
28413cb34c60Sahrens 	const char *opname;
28423cb34c60Sahrens 
28433cb34c60Sahrens 	/* make sure prefix exists */
28443cb34c60Sahrens 	cp = target + prefixlen;
28453cb34c60Sahrens 	if (*cp != '/') {
28463cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
28473cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28483cb34c60Sahrens 	} else {
28493cb34c60Sahrens 		*cp = '\0';
28503cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28513cb34c60Sahrens 		*cp = '/';
28523cb34c60Sahrens 	}
28533cb34c60Sahrens 	if (h == NULL)
28543cb34c60Sahrens 		return (-1);
28553cb34c60Sahrens 	zfs_close(h);
28563cb34c60Sahrens 
28573cb34c60Sahrens 	/*
28583cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28593cb34c60Sahrens 	 * up to the prefixlen-long one.
28603cb34c60Sahrens 	 */
28613cb34c60Sahrens 	for (cp = target + prefixlen + 1;
28623cb34c60Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28633cb34c60Sahrens 		char *logstr;
28643cb34c60Sahrens 
28653cb34c60Sahrens 		*cp = '\0';
28663cb34c60Sahrens 
28673cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
28683cb34c60Sahrens 		if (h) {
28693cb34c60Sahrens 			/* it already exists, nothing to do here */
28703cb34c60Sahrens 			zfs_close(h);
28713cb34c60Sahrens 			continue;
28723cb34c60Sahrens 		}
28733cb34c60Sahrens 
28743cb34c60Sahrens 		logstr = hdl->libzfs_log_str;
28753cb34c60Sahrens 		hdl->libzfs_log_str = NULL;
28763cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28773cb34c60Sahrens 		    NULL) != 0) {
28783cb34c60Sahrens 			hdl->libzfs_log_str = logstr;
28793cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28803cb34c60Sahrens 			goto ancestorerr;
28813cb34c60Sahrens 		}
28823cb34c60Sahrens 
28833cb34c60Sahrens 		hdl->libzfs_log_str = logstr;
28843cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28853cb34c60Sahrens 		if (h == NULL) {
28863cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28873cb34c60Sahrens 			goto ancestorerr;
28883cb34c60Sahrens 		}
28893cb34c60Sahrens 
28903cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28913cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28923cb34c60Sahrens 			goto ancestorerr;
28933cb34c60Sahrens 		}
28943cb34c60Sahrens 
28953cb34c60Sahrens 		if (zfs_share(h) != 0) {
28963cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28973cb34c60Sahrens 			goto ancestorerr;
28983cb34c60Sahrens 		}
28993cb34c60Sahrens 
29003cb34c60Sahrens 		zfs_close(h);
29013cb34c60Sahrens 	}
29023cb34c60Sahrens 
29033cb34c60Sahrens 	return (0);
29043cb34c60Sahrens 
29053cb34c60Sahrens ancestorerr:
29063cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29073cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
29083cb34c60Sahrens 	return (-1);
29093cb34c60Sahrens }
29103cb34c60Sahrens 
29113cb34c60Sahrens /*
29127f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
29137f1f55eaSvb160487  */
29147f1f55eaSvb160487 int
29157f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
29167f1f55eaSvb160487 {
29177f1f55eaSvb160487 	int prefix;
29187f1f55eaSvb160487 	char *path_copy;
29197f1f55eaSvb160487 	int rc;
29207f1f55eaSvb160487 
2921d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
29227f1f55eaSvb160487 		return (-1);
29237f1f55eaSvb160487 
29247f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
29257f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
29267f1f55eaSvb160487 		free(path_copy);
29277f1f55eaSvb160487 	}
29287f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
29297f1f55eaSvb160487 		return (-1);
29307f1f55eaSvb160487 
2931fa9e4066Sahrens 	return (0);
2932fa9e4066Sahrens }
2933fa9e4066Sahrens 
2934fa9e4066Sahrens /*
2935e9dbad6fSeschrock  * Create a new filesystem or volume.
2936fa9e4066Sahrens  */
2937fa9e4066Sahrens int
293899653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2939e9dbad6fSeschrock     nvlist_t *props)
2940fa9e4066Sahrens {
2941fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2942fa9e4066Sahrens 	int ret;
2943fa9e4066Sahrens 	uint64_t size = 0;
2944fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
294599653d4eSeschrock 	char errbuf[1024];
2946e9dbad6fSeschrock 	uint64_t zoned;
294799653d4eSeschrock 
294899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
294999653d4eSeschrock 	    "cannot create '%s'"), path);
2950fa9e4066Sahrens 
2951fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
2952f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
295399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2954fa9e4066Sahrens 
2955fa9e4066Sahrens 	/* validate parents exist */
29567f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2957fa9e4066Sahrens 		return (-1);
2958fa9e4066Sahrens 
2959fa9e4066Sahrens 	/*
2960fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
2961fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
2962fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2963fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2964fa9e4066Sahrens 	 * first try to see if the dataset exists.
2965fa9e4066Sahrens 	 */
2966fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2967990b4856Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
296899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
296999653d4eSeschrock 		    "dataset already exists"));
297099653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2971fa9e4066Sahrens 	}
2972fa9e4066Sahrens 
2973fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
2974fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2975fa9e4066Sahrens 	else
2976fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2977fa9e4066Sahrens 
29780a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
2979b1b8ab34Slling 	    zoned, NULL, errbuf)) == 0)
2980e9dbad6fSeschrock 		return (-1);
2981e9dbad6fSeschrock 
2982fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29835c5460e9Seschrock 		/*
29845c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
29855c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29865c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29875c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29885c5460e9Seschrock 		 * zero.
29895c5460e9Seschrock 		 */
2990e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
2991e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2992e9dbad6fSeschrock 			nvlist_free(props);
299399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2994e9dbad6fSeschrock 			    "missing volume size"));
2995e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2996fa9e4066Sahrens 		}
2997fa9e4066Sahrens 
2998e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
2999e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3000e9dbad6fSeschrock 		    &blocksize)) != 0) {
3001e9dbad6fSeschrock 			if (ret == ENOENT) {
3002e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3003e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3004e9dbad6fSeschrock 			} else {
3005e9dbad6fSeschrock 				nvlist_free(props);
300699653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3007e9dbad6fSeschrock 				    "missing volume block size"));
3008e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3009e9dbad6fSeschrock 			}
3010e9dbad6fSeschrock 		}
3011e9dbad6fSeschrock 
3012e9dbad6fSeschrock 		if (size == 0) {
3013e9dbad6fSeschrock 			nvlist_free(props);
3014e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3015e9dbad6fSeschrock 			    "volume size cannot be zero"));
3016e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30175c5460e9Seschrock 		}
30185c5460e9Seschrock 
30195c5460e9Seschrock 		if (size % blocksize != 0) {
3020e9dbad6fSeschrock 			nvlist_free(props);
302199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3022e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3023e9dbad6fSeschrock 			    "size"));
3024e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3025e9dbad6fSeschrock 		}
30265c5460e9Seschrock 	}
30275c5460e9Seschrock 
3028990b4856Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3029e9dbad6fSeschrock 		return (-1);
3030e9dbad6fSeschrock 	nvlist_free(props);
3031fa9e4066Sahrens 
3032fa9e4066Sahrens 	/* create the dataset */
3033ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3034fa9e4066Sahrens 
3035e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
3036e9dbad6fSeschrock 
3037fa9e4066Sahrens 	/* check for failure */
3038fa9e4066Sahrens 	if (ret != 0) {
3039fa9e4066Sahrens 		char parent[ZFS_MAXNAMELEN];
3040fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3041fa9e4066Sahrens 
3042fa9e4066Sahrens 		switch (errno) {
3043fa9e4066Sahrens 		case ENOENT:
304499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
304599653d4eSeschrock 			    "no such parent '%s'"), parent);
304699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3047fa9e4066Sahrens 
3048fa9e4066Sahrens 		case EINVAL:
304999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3050d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
305199653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3052fa9e4066Sahrens 
3053fa9e4066Sahrens 		case EDOM:
305499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3055e9dbad6fSeschrock 			    "volume block size must be power of 2 from "
3056e9dbad6fSeschrock 			    "%u to %uk"),
3057fa9e4066Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3058fa9e4066Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
305999653d4eSeschrock 
3060e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
306199653d4eSeschrock 
306240feaa91Sahrens 		case ENOTSUP:
306340feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
306440feaa91Sahrens 			    "pool must be upgraded to set this "
306540feaa91Sahrens 			    "property or value"));
306640feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3067fa9e4066Sahrens #ifdef _ILP32
3068fa9e4066Sahrens 		case EOVERFLOW:
3069fa9e4066Sahrens 			/*
3070fa9e4066Sahrens 			 * This platform can't address a volume this big.
3071fa9e4066Sahrens 			 */
307299653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
307399653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
307499653d4eSeschrock 				    errbuf));
3075fa9e4066Sahrens #endif
307699653d4eSeschrock 			/* FALLTHROUGH */
3077fa9e4066Sahrens 		default:
307899653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3079fa9e4066Sahrens 		}
3080fa9e4066Sahrens 	}
3081fa9e4066Sahrens 
3082fa9e4066Sahrens 	return (0);
3083fa9e4066Sahrens }
3084fa9e4066Sahrens 
3085fa9e4066Sahrens /*
3086fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3087fa9e4066Sahrens  * isn't mounted, and that there are no active dependents.
3088fa9e4066Sahrens  */
3089fa9e4066Sahrens int
3090842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3091fa9e4066Sahrens {
3092fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3093fa9e4066Sahrens 
3094fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3095fa9e4066Sahrens 
3096e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3097fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3098fa9e4066Sahrens 	} else {
3099fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3100fa9e4066Sahrens 	}
3101fa9e4066Sahrens 
3102842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
3103ecd6cf80Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3104ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
310599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
310699653d4eSeschrock 		    zhp->zfs_name));
31071d452cf5Sahrens 	}
3108fa9e4066Sahrens 
3109fa9e4066Sahrens 	remove_mountpoint(zhp);
3110fa9e4066Sahrens 
3111fa9e4066Sahrens 	return (0);
3112fa9e4066Sahrens }
3113fa9e4066Sahrens 
31141d452cf5Sahrens struct destroydata {
311519b94df9SMatthew Ahrens 	nvlist_t *nvl;
311619b94df9SMatthew Ahrens 	const char *snapname;
31171d452cf5Sahrens };
31181d452cf5Sahrens 
31191d452cf5Sahrens static int
3120681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
31211d452cf5Sahrens {
31221d452cf5Sahrens 	struct destroydata *dd = arg;
31231d452cf5Sahrens 	zfs_handle_t *szhp;
31241d452cf5Sahrens 	char name[ZFS_MAXNAMELEN];
3125681d9761SEric Taylor 	int rv = 0;
31261d452cf5Sahrens 
312719b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
312819b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
31291d452cf5Sahrens 
31301d452cf5Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31311d452cf5Sahrens 	if (szhp) {
313219b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
31331d452cf5Sahrens 		zfs_close(szhp);
31341d452cf5Sahrens 	}
31351d452cf5Sahrens 
313619b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
31373ccfa83cSahrens 	zfs_close(zhp);
31383ccfa83cSahrens 	return (rv);
31391d452cf5Sahrens }
31401d452cf5Sahrens 
31411d452cf5Sahrens /*
31421d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31431d452cf5Sahrens  */
31441d452cf5Sahrens int
3145842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
31461d452cf5Sahrens {
31471d452cf5Sahrens 	int ret;
31481d452cf5Sahrens 	struct destroydata dd = { 0 };
31491d452cf5Sahrens 
31501d452cf5Sahrens 	dd.snapname = snapname;
315119b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
315219b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
31531d452cf5Sahrens 
315419b94df9SMatthew Ahrens 	if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
315519b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31561d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
315719b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
315819b94df9SMatthew Ahrens 	} else {
315919b94df9SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
316019b94df9SMatthew Ahrens 	}
316119b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
316219b94df9SMatthew Ahrens 	return (ret);
3163e5351341SMatthew Ahrens }
3164e5351341SMatthew Ahrens 
316519b94df9SMatthew Ahrens /*
316619b94df9SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.  They must be underneath
316719b94df9SMatthew Ahrens  * the zhp (either snapshots of it, or snapshots of its descendants).
316819b94df9SMatthew Ahrens  */
316919b94df9SMatthew Ahrens int
317019b94df9SMatthew Ahrens zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
317119b94df9SMatthew Ahrens {
317219b94df9SMatthew Ahrens 	int ret;
317319b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
317419b94df9SMatthew Ahrens 
31751d452cf5Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
317619b94df9SMatthew Ahrens 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
317719b94df9SMatthew Ahrens 		return (-1);
3178842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
31791d452cf5Sahrens 
318019b94df9SMatthew Ahrens 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
31811d452cf5Sahrens 	if (ret != 0) {
31821d452cf5Sahrens 		char errbuf[1024];
31831d452cf5Sahrens 
31841d452cf5Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
318519b94df9SMatthew Ahrens 		    "cannot destroy snapshots in %s"), zc.zc_name);
31861d452cf5Sahrens 
31871d452cf5Sahrens 		switch (errno) {
31881d452cf5Sahrens 		case EEXIST:
31891d452cf5Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31901d452cf5Sahrens 			    "snapshot is cloned"));
31911d452cf5Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31921d452cf5Sahrens 
31931d452cf5Sahrens 		default:
31941d452cf5Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31951d452cf5Sahrens 			    errbuf));
31961d452cf5Sahrens 		}
31971d452cf5Sahrens 	}
31981d452cf5Sahrens 
31991d452cf5Sahrens 	return (0);
32001d452cf5Sahrens }
32011d452cf5Sahrens 
3202fa9e4066Sahrens /*
3203fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3204fa9e4066Sahrens  */
3205fa9e4066Sahrens int
3206e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3207fa9e4066Sahrens {
3208fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3209fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
3210fa9e4066Sahrens 	int ret;
321199653d4eSeschrock 	char errbuf[1024];
321299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3213e9dbad6fSeschrock 	zfs_type_t type;
3214e9dbad6fSeschrock 	uint64_t zoned;
3215fa9e4066Sahrens 
3216fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3217fa9e4066Sahrens 
321899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
321999653d4eSeschrock 	    "cannot create '%s'"), target);
322099653d4eSeschrock 
322119b94df9SMatthew Ahrens 	/* validate the target/clone name */
3222f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
322399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3224fa9e4066Sahrens 
3225fa9e4066Sahrens 	/* validate parents exist */
32267f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3227fa9e4066Sahrens 		return (-1);
3228fa9e4066Sahrens 
3229fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3230fa9e4066Sahrens 
3231fa9e4066Sahrens 	/* do the clone */
3232e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3233fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32345f8e1617Snn35248 		type = ZFS_TYPE_VOLUME;
3235e9dbad6fSeschrock 	} else {
3236fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32375f8e1617Snn35248 		type = ZFS_TYPE_FILESYSTEM;
3238e9dbad6fSeschrock 	}
3239e9dbad6fSeschrock 
3240e9dbad6fSeschrock 	if (props) {
32410a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
32420a48a24eStimh 		    zhp, errbuf)) == NULL)
3243e9dbad6fSeschrock 			return (-1);
3244e9dbad6fSeschrock 
3245990b4856Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3246e9dbad6fSeschrock 			nvlist_free(props);
3247e9dbad6fSeschrock 			return (-1);
3248e9dbad6fSeschrock 		}
3249e9dbad6fSeschrock 
3250e9dbad6fSeschrock 		nvlist_free(props);
3251e9dbad6fSeschrock 	}
3252fa9e4066Sahrens 
3253fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3254e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3255ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3256fa9e4066Sahrens 
3257e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
3258e9dbad6fSeschrock 
3259fa9e4066Sahrens 	if (ret != 0) {
3260fa9e4066Sahrens 		switch (errno) {
3261fa9e4066Sahrens 
3262fa9e4066Sahrens 		case ENOENT:
3263fa9e4066Sahrens 			/*
3264fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3265fa9e4066Sahrens 			 * above, but there may a race condition that has since
3266fa9e4066Sahrens 			 * destroyed the parent.
3267fa9e4066Sahrens 			 *
3268fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3269fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3270fa9e4066Sahrens 			 * dataset doesn't exist.
3271fa9e4066Sahrens 			 */
327299653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
327399653d4eSeschrock 			    "no such parent '%s'"), parent);
327499653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3275fa9e4066Sahrens 
327699653d4eSeschrock 		case EXDEV:
327799653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
327899653d4eSeschrock 			    "source and target pools differ"));
327999653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
328099653d4eSeschrock 			    errbuf));
328199653d4eSeschrock 
328299653d4eSeschrock 		default:
328399653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
328499653d4eSeschrock 			    errbuf));
328599653d4eSeschrock 		}
328699653d4eSeschrock 	}
328799653d4eSeschrock 
328899653d4eSeschrock 	return (ret);
328999653d4eSeschrock }
329099653d4eSeschrock 
329199653d4eSeschrock /*
329299653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
329399653d4eSeschrock  */
329499653d4eSeschrock int
329599653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
329699653d4eSeschrock {
329799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
329899653d4eSeschrock 	zfs_cmd_t zc = { 0 };
329999653d4eSeschrock 	char parent[MAXPATHLEN];
330099653d4eSeschrock 	int ret;
330199653d4eSeschrock 	char errbuf[1024];
330299653d4eSeschrock 
330399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
330499653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
330599653d4eSeschrock 
330699653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
330799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
330899653d4eSeschrock 		    "snapshots can not be promoted"));
330999653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
331099653d4eSeschrock 	}
331199653d4eSeschrock 
33123cb34c60Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
331399653d4eSeschrock 	if (parent[0] == '\0') {
331499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
331599653d4eSeschrock 		    "not a cloned filesystem"));
331699653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
331799653d4eSeschrock 	}
331899653d4eSeschrock 
33193cb34c60Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3320e9dbad6fSeschrock 	    sizeof (zc.zc_value));
332199653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3322ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
332399653d4eSeschrock 
332499653d4eSeschrock 	if (ret != 0) {
33250b69c2f0Sahrens 		int save_errno = errno;
3326fa9e4066Sahrens 
33270b69c2f0Sahrens 		switch (save_errno) {
3328fa9e4066Sahrens 		case EEXIST:
3329681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
333099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3331681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3332681d9761SEric Taylor 			    zc.zc_string, parent);
333399653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3334fa9e4066Sahrens 
3335fa9e4066Sahrens 		default:
33360b69c2f0Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3337fa9e4066Sahrens 		}
3338fa9e4066Sahrens 	}
3339e9dbad6fSeschrock 	return (ret);
33401d452cf5Sahrens }
33411d452cf5Sahrens 
3342fa9e4066Sahrens /*
334372bdce51Sahl  * Takes a snapshot of the given dataset.
3344fa9e4066Sahrens  */
3345fa9e4066Sahrens int
3346bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3347bb0ade09Sahrens     nvlist_t *props)
3348fa9e4066Sahrens {
3349fa9e4066Sahrens 	const char *delim;
3350bb0ade09Sahrens 	char parent[ZFS_MAXNAMELEN];
3351fa9e4066Sahrens 	zfs_handle_t *zhp;
3352fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3353fa9e4066Sahrens 	int ret;
335499653d4eSeschrock 	char errbuf[1024];
3355fa9e4066Sahrens 
335699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
335799653d4eSeschrock 	    "cannot snapshot '%s'"), path);
335899653d4eSeschrock 
335999653d4eSeschrock 	/* validate the target name */
3360f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
336199653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3362fa9e4066Sahrens 
3363bb0ade09Sahrens 	if (props) {
3364bb0ade09Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3365bb0ade09Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
3366bb0ade09Sahrens 			return (-1);
3367bb0ade09Sahrens 
3368bb0ade09Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3369bb0ade09Sahrens 			nvlist_free(props);
3370bb0ade09Sahrens 			return (-1);
3371bb0ade09Sahrens 		}
3372bb0ade09Sahrens 
3373bb0ade09Sahrens 		nvlist_free(props);
3374bb0ade09Sahrens 	}
3375bb0ade09Sahrens 
3376fa9e4066Sahrens 	/* make sure the parent exists and is of the appropriate type */
33771d452cf5Sahrens 	delim = strchr(path, '@');
33788ac09fceSRichard Lowe 	(void) strncpy(parent, path, delim - path);
3379fa9e4066Sahrens 	parent[delim - path] = '\0';
3380fa9e4066Sahrens 
338199653d4eSeschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3382fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3383bb0ade09Sahrens 		zcmd_free_nvlists(&zc);
3384fa9e4066Sahrens 		return (-1);
3385fa9e4066Sahrens 	}
3386fa9e4066Sahrens 
33871d452cf5Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3388e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3389ecd6cf80Smarks 	if (ZFS_IS_VOLUME(zhp))
3390ecd6cf80Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
3391ecd6cf80Smarks 	else
3392ecd6cf80Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
33931d452cf5Sahrens 	zc.zc_cookie = recursive;
3394ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3395fa9e4066Sahrens 
3396bb0ade09Sahrens 	zcmd_free_nvlists(&zc);
3397bb0ade09Sahrens 
33981d452cf5Sahrens 	/*
33991d452cf5Sahrens 	 * if it was recursive, the one that actually failed will be in
34001d452cf5Sahrens 	 * zc.zc_name.
34011d452cf5Sahrens 	 */
3402681d9761SEric Taylor 	if (ret != 0) {
34031d452cf5Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3404e9dbad6fSeschrock 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
340599653d4eSeschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3406681d9761SEric Taylor 	}
3407fa9e4066Sahrens 
3408fa9e4066Sahrens 	zfs_close(zhp);
3409fa9e4066Sahrens 
3410fa9e4066Sahrens 	return (ret);
3411fa9e4066Sahrens }
3412fa9e4066Sahrens 
3413fa9e4066Sahrens /*
3414b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3415b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3416b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3417b12a1c38Slling  * group.
3418fa9e4066Sahrens  */
3419b12a1c38Slling typedef struct rollback_data {
3420b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3421b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3422c391e322Sahrens 	boolean_t	cb_error;
342399653d4eSeschrock 	boolean_t	cb_dependent;
3424c391e322Sahrens 	boolean_t	cb_force;
3425b12a1c38Slling } rollback_data_t;
3426b12a1c38Slling 
3427b12a1c38Slling static int
3428b12a1c38Slling rollback_destroy(zfs_handle_t *zhp, void *data)
3429b12a1c38Slling {
3430b12a1c38Slling 	rollback_data_t *cbp = data;
3431b12a1c38Slling 
3432b12a1c38Slling 	if (!cbp->cb_dependent) {
3433b12a1c38Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3434b12a1c38Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3435b12a1c38Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3436b12a1c38Slling 		    cbp->cb_create) {
3437ecd6cf80Smarks 			char *logstr;
3438b12a1c38Slling 
343999653d4eSeschrock 			cbp->cb_dependent = B_TRUE;
34404ccbb6e7Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
34414ccbb6e7Sahrens 			    rollback_destroy, cbp);
344299653d4eSeschrock 			cbp->cb_dependent = B_FALSE;
3443b12a1c38Slling 
3444ecd6cf80Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
3445ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
3446842727c2SChris Kirby 			cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3447ecd6cf80Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3448b12a1c38Slling 		}
3449b12a1c38Slling 	} else {
3450c391e322Sahrens 		/* We must destroy this clone; first unmount it */
3451c391e322Sahrens 		prop_changelist_t *clp;
3452c391e322Sahrens 
34530069fd67STim Haley 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3454c391e322Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
3455c391e322Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
3456c391e322Sahrens 			cbp->cb_error = B_TRUE;
3457c391e322Sahrens 			zfs_close(zhp);
3458c391e322Sahrens 			return (0);
3459c391e322Sahrens 		}
3460842727c2SChris Kirby 		if (zfs_destroy(zhp, B_FALSE) != 0)
3461c391e322Sahrens 			cbp->cb_error = B_TRUE;
3462c391e322Sahrens 		else
3463c391e322Sahrens 			changelist_remove(clp, zhp->zfs_name);
3464ba7b046eSahrens 		(void) changelist_postfix(clp);
3465c391e322Sahrens 		changelist_free(clp);
3466b12a1c38Slling 	}
3467b12a1c38Slling 
3468b12a1c38Slling 	zfs_close(zhp);
3469b12a1c38Slling 	return (0);
3470b12a1c38Slling }
3471b12a1c38Slling 
3472b12a1c38Slling /*
34734ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
34744ccbb6e7Sahrens  * data changes since then and making it the active dataset.
34754ccbb6e7Sahrens  *
34764ccbb6e7Sahrens  * Any snapshots more recent than the target are destroyed, along with
34774ccbb6e7Sahrens  * their dependents.
3478b12a1c38Slling  */
34794ccbb6e7Sahrens int
3480c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3481fa9e4066Sahrens {
34824ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
34834ccbb6e7Sahrens 	int err;
3484fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
34857b97dc1aSrm160521 	boolean_t restore_resv = 0;
34867b97dc1aSrm160521 	uint64_t old_volsize, new_volsize;
34877b97dc1aSrm160521 	zfs_prop_t resv_prop;
3488fa9e4066Sahrens 
3489fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3490fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3491fa9e4066Sahrens 
34924ccbb6e7Sahrens 	/*
34934ccbb6e7Sahrens 	 * Destroy all recent snapshots and its dependends.
34944ccbb6e7Sahrens 	 */
3495c391e322Sahrens 	cb.cb_force = force;
34964ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
34974ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
34984ccbb6e7Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
34994ccbb6e7Sahrens 
3500c391e322Sahrens 	if (cb.cb_error)
3501c391e322Sahrens 		return (-1);
35024ccbb6e7Sahrens 
35034ccbb6e7Sahrens 	/*
35044ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
35054ccbb6e7Sahrens 	 * rollback to the given snapshot.
35064ccbb6e7Sahrens 	 */
35074ccbb6e7Sahrens 
35087b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
35097b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
35107b97dc1aSrm160521 			return (-1);
35117b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35127b97dc1aSrm160521 		restore_resv =
35137b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
35147b97dc1aSrm160521 	}
3515fa9e4066Sahrens 
3516fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3517fa9e4066Sahrens 
3518e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3519fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3520fa9e4066Sahrens 	else
3521fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3522fa9e4066Sahrens 
3523fa9e4066Sahrens 	/*
35244ccbb6e7Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
35254ccbb6e7Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
35264ccbb6e7Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
35274ccbb6e7Sahrens 	 * an unlikely race condition where the user has taken a
35284ccbb6e7Sahrens 	 * snapshot since we verified that this was the most recent.
35297b97dc1aSrm160521 	 *
3530fa9e4066Sahrens 	 */
35314ccbb6e7Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3532ece3d9b3Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
353399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
353499653d4eSeschrock 		    zhp->zfs_name);
3535b9415e83Srm160521 		return (err);
3536b9415e83Srm160521 	}
3537fa9e4066Sahrens 
35387b97dc1aSrm160521 	/*
35397b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
35407b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
35417b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
35427b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
35437b97dc1aSrm160521 	 */
3544b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3545b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
35467b97dc1aSrm160521 		if (restore_resv) {
35477b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35487b97dc1aSrm160521 			if (old_volsize != new_volsize)
3549b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
3550b9415e83Srm160521 				    new_volsize);
35517b97dc1aSrm160521 		}
35527b97dc1aSrm160521 		zfs_close(zhp);
35537b97dc1aSrm160521 	}
35544ccbb6e7Sahrens 	return (err);
3555b12a1c38Slling }
3556b12a1c38Slling 
3557b12a1c38Slling /*
3558fa9e4066Sahrens  * Renames the given dataset.
3559fa9e4066Sahrens  */
3560fa9e4066Sahrens int
35616a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
35626a9cb0eaSEric Schrock     boolean_t force_unmount)
3563fa9e4066Sahrens {
3564fa9e4066Sahrens 	int ret;
3565fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3566fa9e4066Sahrens 	char *delim;
3567cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
3568cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
3569cdf5b4caSmmusante 	char *parentname = NULL;
3570fa9e4066Sahrens 	char parent[ZFS_MAXNAMELEN];
357199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
357299653d4eSeschrock 	char errbuf[1024];
3573fa9e4066Sahrens 
3574fa9e4066Sahrens 	/* if we have the same exact name, just return success */
3575fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3576fa9e4066Sahrens 		return (0);
3577fa9e4066Sahrens 
357899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
357999653d4eSeschrock 	    "cannot rename to '%s'"), target);
358099653d4eSeschrock 
3581fa9e4066Sahrens 	/*
3582fa9e4066Sahrens 	 * Make sure the target name is valid
3583fa9e4066Sahrens 	 */
3584fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
358598579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
358698579b20Snd150628 		    *target == '@') {
358798579b20Snd150628 			/*
358898579b20Snd150628 			 * Snapshot target name is abbreviated,
358998579b20Snd150628 			 * reconstruct full dataset name
359098579b20Snd150628 			 */
359198579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
359298579b20Snd150628 			    sizeof (parent));
359398579b20Snd150628 			delim = strchr(parent, '@');
359498579b20Snd150628 			if (strchr(target, '@') == NULL)
359598579b20Snd150628 				*(++delim) = '\0';
359698579b20Snd150628 			else
359798579b20Snd150628 				*delim = '\0';
359898579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
359998579b20Snd150628 			target = parent;
360098579b20Snd150628 		} else {
3601fa9e4066Sahrens 			/*
3602fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
3603fa9e4066Sahrens 			 */
360498579b20Snd150628 			delim = strchr(target, '@');
360598579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
360698579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
360799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
360898579b20Snd150628 				    "snapshots must be part of same "
360998579b20Snd150628 				    "dataset"));
361098579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
361198579b20Snd150628 				    errbuf));
3612fa9e4066Sahrens 			}
361398579b20Snd150628 		}
3614f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
361598579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3616fa9e4066Sahrens 	} else {
3617cdf5b4caSmmusante 		if (recursive) {
3618cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3619cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
3620cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3621cdf5b4caSmmusante 		}
3622cdf5b4caSmmusante 
3623f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
362498579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3625e9dbad6fSeschrock 
3626fa9e4066Sahrens 		/* validate parents */
3627d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3628fa9e4066Sahrens 			return (-1);
3629fa9e4066Sahrens 
3630fa9e4066Sahrens 		/* make sure we're in the same pool */
3631fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3632fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3633fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
363499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
363599653d4eSeschrock 			    "datasets must be within same pool"));
363699653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3637fa9e4066Sahrens 		}
3638f2fdf992Snd150628 
3639f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
3640d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
3641f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3642d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
3643f2fdf992Snd150628 			    "current dataset name"));
3644f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3645f2fdf992Snd150628 		}
3646fa9e4066Sahrens 	}
3647fa9e4066Sahrens 
364899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
364999653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
365099653d4eSeschrock 
3651fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3652fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
365399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
365499653d4eSeschrock 		    "dataset is used in a non-global zone"));
365599653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3656fa9e4066Sahrens 	}
3657fa9e4066Sahrens 
3658cdf5b4caSmmusante 	if (recursive) {
3659cdf5b4caSmmusante 
3660f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3661f0c5ee21Smmusante 		if (parentname == NULL) {
3662f0c5ee21Smmusante 			ret = -1;
3663f0c5ee21Smmusante 			goto error;
3664f0c5ee21Smmusante 		}
3665cdf5b4caSmmusante 		delim = strchr(parentname, '@');
3666cdf5b4caSmmusante 		*delim = '\0';
3667990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3668cdf5b4caSmmusante 		if (zhrp == NULL) {
3669f0c5ee21Smmusante 			ret = -1;
3670f0c5ee21Smmusante 			goto error;
3671cdf5b4caSmmusante 		}
3672cdf5b4caSmmusante 
3673cdf5b4caSmmusante 	} else {
36746a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
36756a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
367699653d4eSeschrock 			return (-1);
3677fa9e4066Sahrens 
3678fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
367999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
368099653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
368199653d4eSeschrock 			    "in a non-global zone"));
3682e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3683fa9e4066Sahrens 			goto error;
3684fa9e4066Sahrens 		}
3685fa9e4066Sahrens 
3686fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
3687fa9e4066Sahrens 			goto error;
3688cdf5b4caSmmusante 	}
3689fa9e4066Sahrens 
3690e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
3691fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3692fa9e4066Sahrens 	else
3693fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3694fa9e4066Sahrens 
369598579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3696e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
369798579b20Snd150628 
3698cdf5b4caSmmusante 	zc.zc_cookie = recursive;
3699cdf5b4caSmmusante 
3700ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3701cdf5b4caSmmusante 		/*
3702cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
3703cdf5b4caSmmusante 		 * be in zc.zc_name
3704cdf5b4caSmmusante 		 */
3705cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37063cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
3707cdf5b4caSmmusante 
3708cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
3709cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3710cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
3711cdf5b4caSmmusante 			    "with the new name"));
3712a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3713cdf5b4caSmmusante 		} else {
371499653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3715cdf5b4caSmmusante 		}
3716fa9e4066Sahrens 
3717fa9e4066Sahrens 		/*
3718fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
3719fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
3720fa9e4066Sahrens 		 */
3721681d9761SEric Taylor 		if (!recursive)
3722fa9e4066Sahrens 			(void) changelist_postfix(cl);
3723cdf5b4caSmmusante 	} else {
3724681d9761SEric Taylor 		if (!recursive) {
3725fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
3726fa9e4066Sahrens 			ret = changelist_postfix(cl);
3727fa9e4066Sahrens 		}
3728cdf5b4caSmmusante 	}
3729fa9e4066Sahrens 
3730fa9e4066Sahrens error:
3731cdf5b4caSmmusante 	if (parentname) {
3732cdf5b4caSmmusante 		free(parentname);
3733cdf5b4caSmmusante 	}
3734cdf5b4caSmmusante 	if (zhrp) {
3735cdf5b4caSmmusante 		zfs_close(zhrp);
3736cdf5b4caSmmusante 	}
3737cdf5b4caSmmusante 	if (cl) {
3738fa9e4066Sahrens 		changelist_free(cl);
3739cdf5b4caSmmusante 	}
3740fa9e4066Sahrens 	return (ret);
3741fa9e4066Sahrens }
3742fa9e4066Sahrens 
3743e9dbad6fSeschrock nvlist_t *
3744e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
3745e9dbad6fSeschrock {
3746e9dbad6fSeschrock 	return (zhp->zfs_user_props);
3747e9dbad6fSeschrock }
3748e9dbad6fSeschrock 
374992241e0bSTom Erickson nvlist_t *
375092241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
375192241e0bSTom Erickson {
375292241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
375392241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
375492241e0bSTom Erickson 			return (NULL);
375592241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
375692241e0bSTom Erickson }
375792241e0bSTom Erickson 
3758e9dbad6fSeschrock /*
3759e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
3760e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
3761e9dbad6fSeschrock  *
3762e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
3763e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
3764e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
3765e9dbad6fSeschrock  *
3766e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
376792241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
376892241e0bSTom Erickson  *        requested received property values, we also need to compute the width
376992241e0bSTom Erickson  *        of the RECEIVED column.
3770e9dbad6fSeschrock  */
3771e9dbad6fSeschrock int
377292241e0bSTom Erickson zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3773e9dbad6fSeschrock {
3774e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3775990b4856Slling 	zprop_list_t *entry;
3776990b4856Slling 	zprop_list_t **last, **start;
3777e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
3778e9dbad6fSeschrock 	nvpair_t *elem;
3779e9dbad6fSeschrock 	char *strval;
3780e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
3781e9dbad6fSeschrock 
3782990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3783e9dbad6fSeschrock 		return (-1);
3784e9dbad6fSeschrock 
3785e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
3786e9dbad6fSeschrock 
3787e9dbad6fSeschrock 	entry = *plp;
3788e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3789e9dbad6fSeschrock 		/*
3790e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
3791e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
3792e9dbad6fSeschrock 		 * non-native property.
3793e9dbad6fSeschrock 		 */
3794e9dbad6fSeschrock 		start = plp;
3795e9dbad6fSeschrock 		while (*start != NULL) {
3796990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
3797e9dbad6fSeschrock 				break;
3798e9dbad6fSeschrock 			start = &(*start)->pl_next;
3799e9dbad6fSeschrock 		}
3800e9dbad6fSeschrock 
3801e9dbad6fSeschrock 		elem = NULL;
3802e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3803e9dbad6fSeschrock 			/*
3804e9dbad6fSeschrock 			 * See if we've already found this property in our list.
3805e9dbad6fSeschrock 			 */
3806e9dbad6fSeschrock 			for (last = start; *last != NULL;
3807e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
3808e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
3809e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
3810e9dbad6fSeschrock 					break;
3811e9dbad6fSeschrock 			}
3812e9dbad6fSeschrock 
3813e9dbad6fSeschrock 			if (*last == NULL) {
3814e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
3815990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
3816e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
3817e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
3818e9dbad6fSeschrock 					free(entry);
3819e9dbad6fSeschrock 					return (-1);
3820e9dbad6fSeschrock 				}
3821e9dbad6fSeschrock 
3822990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
3823e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
3824e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
3825e9dbad6fSeschrock 				*last = entry;
3826e9dbad6fSeschrock 			}
3827e9dbad6fSeschrock 		}
3828e9dbad6fSeschrock 	}
3829e9dbad6fSeschrock 
3830e9dbad6fSeschrock 	/*
3831e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
3832e9dbad6fSeschrock 	 */
3833e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3834e9dbad6fSeschrock 		if (entry->pl_fixed)
3835e9dbad6fSeschrock 			continue;
3836e9dbad6fSeschrock 
3837990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
3838e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
3839e9dbad6fSeschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3840e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
3841e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
3842e9dbad6fSeschrock 			}
384392241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
384492241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
384592241e0bSTom Erickson 			    buf, sizeof (buf), B_FALSE) == 0)
384692241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
384792241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
384892241e0bSTom Erickson 		} else {
384992241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
385092241e0bSTom Erickson 			    &propval) == 0) {
3851e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
3852990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
3853e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
3854e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
3855e9dbad6fSeschrock 			}
385692241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
385792241e0bSTom Erickson 			    entry->pl_user_prop,
385892241e0bSTom Erickson 			    buf, sizeof (buf), B_FALSE) == 0)
385992241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
386092241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
386192241e0bSTom Erickson 		}
3862e9dbad6fSeschrock 	}
3863e9dbad6fSeschrock 
3864e9dbad6fSeschrock 	return (0);
3865e9dbad6fSeschrock }
3866ecd6cf80Smarks 
3867ecd6cf80Smarks int
3868ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3869743a77edSAlan Wright     char *resource, void *export, void *sharetab,
3870743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
3871ecd6cf80Smarks {
3872ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
3873ecd6cf80Smarks 	int error;
3874ecd6cf80Smarks 
3875ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3876ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3877743a77edSAlan Wright 	if (resource)
3878743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3879ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3880ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3881da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
3882ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
3883ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3884ecd6cf80Smarks 	return (error);
3885ecd6cf80Smarks }
38862e5e9e19SSanjeev Bagewadi 
38872e5e9e19SSanjeev Bagewadi void
38882e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
38892e5e9e19SSanjeev Bagewadi {
38902e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
38912e5e9e19SSanjeev Bagewadi 
38922e5e9e19SSanjeev Bagewadi 	/*
38932e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
38942e5e9e19SSanjeev Bagewadi 	 * properties.
38952e5e9e19SSanjeev Bagewadi 	 */
38962e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
38972e5e9e19SSanjeev Bagewadi 
38982e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
38992e5e9e19SSanjeev Bagewadi 
39002e5e9e19SSanjeev Bagewadi 	while (curr) {
39012e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
39022e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
39032e5e9e19SSanjeev Bagewadi 
390414843421SMatthew Ahrens 		/*
3905faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
3906faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
3907faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
3908faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
3909faaa6415SEric Schrock 		 * software, for example).
391014843421SMatthew Ahrens 		 */
391114843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
39122e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
39132e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
39142e5e9e19SSanjeev Bagewadi 		curr = next;
39152e5e9e19SSanjeev Bagewadi 	}
39162e5e9e19SSanjeev Bagewadi }
3917743a77edSAlan Wright 
3918743a77edSAlan Wright static int
3919743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3920743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3921743a77edSAlan Wright {
3922743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
3923743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
3924743a77edSAlan Wright 	int error;
3925743a77edSAlan Wright 
3926743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3927743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3928743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
3929743a77edSAlan Wright 
3930743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
3931743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3932743a77edSAlan Wright 			(void) no_memory(hdl);
3933743a77edSAlan Wright 			return (NULL);
3934743a77edSAlan Wright 		}
3935743a77edSAlan Wright 	}
3936743a77edSAlan Wright 
3937743a77edSAlan Wright 	switch (cmd) {
3938743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
3939743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
3940743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3941743a77edSAlan Wright 		break;
3942743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
3943743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3944743a77edSAlan Wright 		    resource1) != 0) {
3945743a77edSAlan Wright 				(void) no_memory(hdl);
3946743a77edSAlan Wright 				return (-1);
3947743a77edSAlan Wright 		}
3948743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3949743a77edSAlan Wright 		    resource2) != 0) {
3950743a77edSAlan Wright 				(void) no_memory(hdl);
3951743a77edSAlan Wright 				return (-1);
3952743a77edSAlan Wright 		}
3953743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3954743a77edSAlan Wright 			nvlist_free(nvlist);
3955743a77edSAlan Wright 			return (-1);
3956743a77edSAlan Wright 		}
3957743a77edSAlan Wright 		break;
3958743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
3959743a77edSAlan Wright 		break;
3960743a77edSAlan Wright 	default:
3961743a77edSAlan Wright 		return (-1);
3962743a77edSAlan Wright 	}
3963743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3964743a77edSAlan Wright 	if (nvlist)
3965743a77edSAlan Wright 		nvlist_free(nvlist);
3966743a77edSAlan Wright 	return (error);
3967743a77edSAlan Wright }
3968743a77edSAlan Wright 
3969743a77edSAlan Wright int
3970743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3971743a77edSAlan Wright     char *path, char *resource)
3972743a77edSAlan Wright {
3973743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3974743a77edSAlan Wright 	    resource, NULL));
3975743a77edSAlan Wright }
3976743a77edSAlan Wright 
3977743a77edSAlan Wright int
3978743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3979743a77edSAlan Wright     char *path, char *resource)
3980743a77edSAlan Wright {
3981743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3982743a77edSAlan Wright 	    resource, NULL));
3983743a77edSAlan Wright }
3984743a77edSAlan Wright 
3985743a77edSAlan Wright int
3986743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3987743a77edSAlan Wright {
3988743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3989743a77edSAlan Wright 	    NULL, NULL));
3990743a77edSAlan Wright }
3991743a77edSAlan Wright 
3992743a77edSAlan Wright int
3993743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3994743a77edSAlan Wright     char *oldname, char *newname)
3995743a77edSAlan Wright {
3996743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3997743a77edSAlan Wright 	    oldname, newname));
3998743a77edSAlan Wright }
399914843421SMatthew Ahrens 
400014843421SMatthew Ahrens int
400114843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
400214843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
400314843421SMatthew Ahrens {
400414843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
400514843421SMatthew Ahrens 	int error;
400614843421SMatthew Ahrens 	zfs_useracct_t buf[100];
400714843421SMatthew Ahrens 
400819b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
400914843421SMatthew Ahrens 
401014843421SMatthew Ahrens 	zc.zc_objset_type = type;
401114843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
401214843421SMatthew Ahrens 
401314843421SMatthew Ahrens 	/* CONSTCOND */
401414843421SMatthew Ahrens 	while (1) {
401514843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
401614843421SMatthew Ahrens 
401714843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
401814843421SMatthew Ahrens 		error = ioctl(zhp->zfs_hdl->libzfs_fd,
401914843421SMatthew Ahrens 		    ZFS_IOC_USERSPACE_MANY, &zc);
402014843421SMatthew Ahrens 		if (error || zc.zc_nvlist_dst_size == 0)
402114843421SMatthew Ahrens 			break;
402214843421SMatthew Ahrens 
402314843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
40240aea4b19SMatthew Ahrens 			error = func(arg, zua->zu_domain, zua->zu_rid,
40250aea4b19SMatthew Ahrens 			    zua->zu_space);
40260aea4b19SMatthew Ahrens 			if (error != 0)
40270aea4b19SMatthew Ahrens 				return (error);
402814843421SMatthew Ahrens 			zua++;
402914843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
403014843421SMatthew Ahrens 		}
403114843421SMatthew Ahrens 	}
403214843421SMatthew Ahrens 
403314843421SMatthew Ahrens 	return (error);
403414843421SMatthew Ahrens }
4035842727c2SChris Kirby 
4036842727c2SChris Kirby int
4037842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4038c99e4bdcSChris Kirby     boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4039a7f53a56SChris Kirby     int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4040842727c2SChris Kirby {
4041842727c2SChris Kirby 	zfs_cmd_t zc = { 0 };
4042842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4043842727c2SChris Kirby 
4044a7f53a56SChris Kirby 	ASSERT(!recursive || dsobj == 0);
4045a7f53a56SChris Kirby 
4046842727c2SChris Kirby 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4047842727c2SChris Kirby 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4048ca45db41SChris Kirby 	if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4049ca45db41SChris Kirby 	    >= sizeof (zc.zc_string))
4050ca45db41SChris Kirby 		return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4051842727c2SChris Kirby 	zc.zc_cookie = recursive;
4052ca45db41SChris Kirby 	zc.zc_temphold = temphold;
4053c99e4bdcSChris Kirby 	zc.zc_cleanup_fd = cleanup_fd;
4054a7f53a56SChris Kirby 	zc.zc_sendobj = dsobj;
4055a7f53a56SChris Kirby 	zc.zc_createtxg = createtxg;
4056842727c2SChris Kirby 
4057842727c2SChris Kirby 	if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4058842727c2SChris Kirby 		char errbuf[ZFS_MAXNAMELEN+32];
4059842727c2SChris Kirby 
4060842727c2SChris Kirby 		/*
4061842727c2SChris Kirby 		 * if it was recursive, the one that actually failed will be in
4062842727c2SChris Kirby 		 * zc.zc_name.
4063842727c2SChris Kirby 		 */
4064842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4065842727c2SChris Kirby 		    "cannot hold '%s@%s'"), zc.zc_name, snapname);
4066842727c2SChris Kirby 		switch (errno) {
406715508ac0SChris Kirby 		case E2BIG:
406815508ac0SChris Kirby 			/*
406915508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
407015508ac0SChris Kirby 			 * prepended. So even if we passed the length check
407115508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
407215508ac0SChris Kirby 			 * up being slightly too long.
407315508ac0SChris Kirby 			 */
407415508ac0SChris Kirby 			return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4075842727c2SChris Kirby 		case ENOTSUP:
4076842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4077842727c2SChris Kirby 			    "pool must be upgraded"));
4078842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4079842727c2SChris Kirby 		case EINVAL:
4080842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4081842727c2SChris Kirby 		case EEXIST:
4082842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4083818119b8SChris Kirby 		case ENOENT:
4084818119b8SChris Kirby 			if (enoent_ok)
4085a7f53a56SChris Kirby 				return (ENOENT);
4086818119b8SChris Kirby 			/* FALLTHROUGH */
4087842727c2SChris Kirby 		default:
4088842727c2SChris Kirby 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4089842727c2SChris Kirby 		}
4090842727c2SChris Kirby 	}
4091842727c2SChris Kirby 
4092842727c2SChris Kirby 	return (0);
4093842727c2SChris Kirby }
4094842727c2SChris Kirby 
4095842727c2SChris Kirby int
4096842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4097842727c2SChris Kirby     boolean_t recursive)
4098842727c2SChris Kirby {
4099842727c2SChris Kirby 	zfs_cmd_t zc = { 0 };
4100842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4101842727c2SChris Kirby 
4102842727c2SChris Kirby 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4103842727c2SChris Kirby 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4104ca45db41SChris Kirby 	if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4105ca45db41SChris Kirby 	    >= sizeof (zc.zc_string))
4106ca45db41SChris Kirby 		return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4107842727c2SChris Kirby 	zc.zc_cookie = recursive;
4108842727c2SChris Kirby 
4109842727c2SChris Kirby 	if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4110842727c2SChris Kirby 		char errbuf[ZFS_MAXNAMELEN+32];
4111842727c2SChris Kirby 
4112842727c2SChris Kirby 		/*
4113842727c2SChris Kirby 		 * if it was recursive, the one that actually failed will be in
4114842727c2SChris Kirby 		 * zc.zc_name.
4115842727c2SChris Kirby 		 */
4116842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4117620252bcSChris Kirby 		    "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4118620252bcSChris Kirby 		    snapname);
4119842727c2SChris Kirby 		switch (errno) {
4120842727c2SChris Kirby 		case ESRCH:
4121842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4122842727c2SChris Kirby 		case ENOTSUP:
4123842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4124842727c2SChris Kirby 			    "pool must be upgraded"));
4125842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4126842727c2SChris Kirby 		case EINVAL:
4127842727c2SChris Kirby 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4128842727c2SChris Kirby 		default:
4129842727c2SChris Kirby 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4130842727c2SChris Kirby 		}
4131842727c2SChris Kirby 	}
4132842727c2SChris Kirby 
4133842727c2SChris Kirby 	return (0);
4134842727c2SChris Kirby }
4135ca45db41SChris Kirby 
41361af68beaSAlexander Stetsenko int
41371af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
41381af68beaSAlexander Stetsenko {
41391af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
41401af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
41411af68beaSAlexander Stetsenko 	int nvsz = 2048;
41421af68beaSAlexander Stetsenko 	void *nvbuf;
41431af68beaSAlexander Stetsenko 	int err = 0;
41441af68beaSAlexander Stetsenko 	char errbuf[ZFS_MAXNAMELEN+32];
41451af68beaSAlexander Stetsenko 
41461af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
41471af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
41481af68beaSAlexander Stetsenko 
41491af68beaSAlexander Stetsenko tryagain:
41501af68beaSAlexander Stetsenko 
41511af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
41521af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
41531af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
41541af68beaSAlexander Stetsenko 		goto out;
41551af68beaSAlexander Stetsenko 	}
41561af68beaSAlexander Stetsenko 
41571af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
41581af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
41591af68beaSAlexander Stetsenko 
41601af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
41611af68beaSAlexander Stetsenko 
4162d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
41631af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
41641af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
41651af68beaSAlexander Stetsenko 		    zc.zc_name);
41661af68beaSAlexander Stetsenko 		switch (errno) {
41671af68beaSAlexander Stetsenko 		case ENOMEM:
41681af68beaSAlexander Stetsenko 			free(nvbuf);
41691af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
41701af68beaSAlexander Stetsenko 			goto tryagain;
41711af68beaSAlexander Stetsenko 
41721af68beaSAlexander Stetsenko 		case ENOTSUP:
41731af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41741af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
41751af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
41761af68beaSAlexander Stetsenko 			break;
41771af68beaSAlexander Stetsenko 		case EINVAL:
41781af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
41791af68beaSAlexander Stetsenko 			break;
41801af68beaSAlexander Stetsenko 		case ENOENT:
41811af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
41821af68beaSAlexander Stetsenko 			break;
41831af68beaSAlexander Stetsenko 		default:
41841af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
41851af68beaSAlexander Stetsenko 			break;
41861af68beaSAlexander Stetsenko 		}
41871af68beaSAlexander Stetsenko 	} else {
41881af68beaSAlexander Stetsenko 		/* success */
41891af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
41901af68beaSAlexander Stetsenko 		if (rc) {
41911af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
41921af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
41931af68beaSAlexander Stetsenko 			    zc.zc_name);
41941af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
41951af68beaSAlexander Stetsenko 		}
41961af68beaSAlexander Stetsenko 	}
41971af68beaSAlexander Stetsenko 
41981af68beaSAlexander Stetsenko 	free(nvbuf);
41991af68beaSAlexander Stetsenko out:
42001af68beaSAlexander Stetsenko 	return (err);
42011af68beaSAlexander Stetsenko }
42021af68beaSAlexander Stetsenko 
42031af68beaSAlexander Stetsenko int
42041af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
42051af68beaSAlexander Stetsenko {
42061af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
42071af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
42081af68beaSAlexander Stetsenko 	char *nvbuf;
42091af68beaSAlexander Stetsenko 	char errbuf[ZFS_MAXNAMELEN+32];
42101af68beaSAlexander Stetsenko 	size_t nvsz;
42111af68beaSAlexander Stetsenko 	int err;
42121af68beaSAlexander Stetsenko 
42131af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
42141af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
42151af68beaSAlexander Stetsenko 
42161af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
42171af68beaSAlexander Stetsenko 	assert(err == 0);
42181af68beaSAlexander Stetsenko 
42191af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
42201af68beaSAlexander Stetsenko 
42211af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
42221af68beaSAlexander Stetsenko 	assert(err == 0);
42231af68beaSAlexander Stetsenko 
42241af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
42251af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
42261af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
42271af68beaSAlexander Stetsenko 
42281af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
42291af68beaSAlexander Stetsenko 
42301af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
42311af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
42321af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
42331af68beaSAlexander Stetsenko 		    zc.zc_name);
42341af68beaSAlexander Stetsenko 		switch (errno) {
42351af68beaSAlexander Stetsenko 		case ENOTSUP:
42361af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42371af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
42381af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
42391af68beaSAlexander Stetsenko 			break;
42401af68beaSAlexander Stetsenko 		case EINVAL:
42411af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
42421af68beaSAlexander Stetsenko 			break;
42431af68beaSAlexander Stetsenko 		case ENOENT:
42441af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
42451af68beaSAlexander Stetsenko 			break;
42461af68beaSAlexander Stetsenko 		default:
42471af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
42481af68beaSAlexander Stetsenko 			break;
42491af68beaSAlexander Stetsenko 		}
42501af68beaSAlexander Stetsenko 	}
42511af68beaSAlexander Stetsenko 
42521af68beaSAlexander Stetsenko 	free(nvbuf);
42531af68beaSAlexander Stetsenko 
42541af68beaSAlexander Stetsenko 	return (err);
42551af68beaSAlexander Stetsenko }
42561af68beaSAlexander Stetsenko 
42571af68beaSAlexander Stetsenko int
42581af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
42591af68beaSAlexander Stetsenko {
42601af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
42611af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
42621af68beaSAlexander Stetsenko 	int nvsz = 2048;
42631af68beaSAlexander Stetsenko 	void *nvbuf;
42641af68beaSAlexander Stetsenko 	int err = 0;
42651af68beaSAlexander Stetsenko 	char errbuf[ZFS_MAXNAMELEN+32];
42661af68beaSAlexander Stetsenko 
42671af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
42681af68beaSAlexander Stetsenko 
42691af68beaSAlexander Stetsenko tryagain:
42701af68beaSAlexander Stetsenko 
42711af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
42721af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
42731af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
42741af68beaSAlexander Stetsenko 		goto out;
42751af68beaSAlexander Stetsenko 	}
42761af68beaSAlexander Stetsenko 
42771af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
42781af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
42791af68beaSAlexander Stetsenko 
42801af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
42811af68beaSAlexander Stetsenko 
42821af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
42831af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
42841af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
42851af68beaSAlexander Stetsenko 		    zc.zc_name);
42861af68beaSAlexander Stetsenko 		switch (errno) {
42871af68beaSAlexander Stetsenko 		case ENOMEM:
42881af68beaSAlexander Stetsenko 			free(nvbuf);
42891af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
42901af68beaSAlexander Stetsenko 			goto tryagain;
42911af68beaSAlexander Stetsenko 
42921af68beaSAlexander Stetsenko 		case ENOTSUP:
42931af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42941af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
42951af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
42961af68beaSAlexander Stetsenko 			break;
42971af68beaSAlexander Stetsenko 		case EINVAL:
42981af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
42991af68beaSAlexander Stetsenko 			break;
43001af68beaSAlexander Stetsenko 		case ENOENT:
43011af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
43021af68beaSAlexander Stetsenko 			break;
43031af68beaSAlexander Stetsenko 		default:
43041af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
43051af68beaSAlexander Stetsenko 			break;
43061af68beaSAlexander Stetsenko 		}
43071af68beaSAlexander Stetsenko 	} else {
43081af68beaSAlexander Stetsenko 		/* success */
43091af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
43101af68beaSAlexander Stetsenko 		if (rc) {
43111af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf),
43121af68beaSAlexander Stetsenko 			    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
43131af68beaSAlexander Stetsenko 			    zc.zc_name);
43141af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
43151af68beaSAlexander Stetsenko 		}
43161af68beaSAlexander Stetsenko 	}
43171af68beaSAlexander Stetsenko 
43181af68beaSAlexander Stetsenko 	free(nvbuf);
43191af68beaSAlexander Stetsenko out:
43201af68beaSAlexander Stetsenko 	return (err);
43211af68beaSAlexander Stetsenko }
43221af68beaSAlexander Stetsenko 
4323c1449561SEric Taylor uint64_t
4324c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4325c1449561SEric Taylor {
4326c1449561SEric Taylor 	uint64_t numdb;
4327c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4328c1449561SEric Taylor 	int ncopies;
4329c1449561SEric Taylor 	char *strval;
4330c1449561SEric Taylor 
4331c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4332c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4333c1449561SEric Taylor 		ncopies = atoi(strval);
4334c1449561SEric Taylor 	else
4335c1449561SEric Taylor 		ncopies = 1;
4336c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4337c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4338c1449561SEric Taylor 	    &volblocksize) != 0)
4339c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4340c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4341c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4342c1449561SEric Taylor 	numdb = 7;
4343c1449561SEric Taylor 	/* calculate number of indirects */
4344c1449561SEric Taylor 	while (nblocks > 1) {
4345c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4346c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4347c1449561SEric Taylor 		numdb += nblocks;
4348c1449561SEric Taylor 	}
4349c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4350c1449561SEric Taylor 	volsize *= ncopies;
4351c1449561SEric Taylor 	/*
4352c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4353c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4354c1449561SEric Taylor 	 * 1100 bytes
4355c1449561SEric Taylor 	 */
4356c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4357c1449561SEric Taylor 	volsize += numdb;
4358c1449561SEric Taylor 	return (volsize);
4359c1449561SEric Taylor }
4360