xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision f67950b21e185934ccabe311516f4dcbdb00ef79)
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.
24b73ccab0SMike Gerdts  * Copyright 2019 Joyent, Inc.
25ad2760acSMatthew Ahrens  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
270d8fa8f8SMartin Matuska  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
29a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
30c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
319fa2266dSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
3288f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33f62db44dSAndrew Stormont  * Copyright 2017-2018 RackTop Systems.
34fa9e4066Sahrens  */
35fa9e4066Sahrens 
36fa9e4066Sahrens #include <ctype.h>
37fa9e4066Sahrens #include <errno.h>
38fa9e4066Sahrens #include <libintl.h>
39fa9e4066Sahrens #include <math.h>
40fa9e4066Sahrens #include <stdio.h>
41fa9e4066Sahrens #include <stdlib.h>
42fa9e4066Sahrens #include <strings.h>
43fa9e4066Sahrens #include <unistd.h>
443cb34c60Sahrens #include <stddef.h>
45fa9e4066Sahrens #include <zone.h>
4699653d4eSeschrock #include <fcntl.h>
47fa9e4066Sahrens #include <sys/mntent.h>
48b12a1c38Slling #include <sys/mount.h>
49ecd6cf80Smarks #include <priv.h>
50ecd6cf80Smarks #include <pwd.h>
51ecd6cf80Smarks #include <grp.h>
52ecd6cf80Smarks #include <stddef.h>
53ecd6cf80Smarks #include <ucred.h>
5414843421SMatthew Ahrens #include <idmap.h>
5514843421SMatthew Ahrens #include <aclutils.h>
563b12c289SMatthew Ahrens #include <directory.h>
57591e0e13SSebastien Roy #include <time.h>
58fa9e4066Sahrens 
59c1449561SEric Taylor #include <sys/dnode.h>
60fa9e4066Sahrens #include <sys/spa.h>
61e9dbad6fSeschrock #include <sys/zap.h>
62eb633035STom Caputi #include <sys/dsl_crypt.h>
63fa9e4066Sahrens #include <libzfs.h>
64fa9e4066Sahrens 
65fa9e4066Sahrens #include "zfs_namecheck.h"
66fa9e4066Sahrens #include "zfs_prop.h"
67fa9e4066Sahrens #include "libzfs_impl.h"
68ecd6cf80Smarks #include "zfs_deleg.h"
69fa9e4066Sahrens 
7014843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
7114843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
72cdf5b4caSmmusante 
73fa9e4066Sahrens /*
74fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
75fa9e4066Sahrens  * readable form.
76fa9e4066Sahrens  */
77fa9e4066Sahrens const char *
78fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
79fa9e4066Sahrens {
80fa9e4066Sahrens 	switch (type) {
81fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
82fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
83fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
84fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
85fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
86fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
8788f61deeSIgor Kozhukhov 	case ZFS_TYPE_POOL:
8888f61deeSIgor Kozhukhov 		return (dgettext(TEXT_DOMAIN, "pool"));
8988f61deeSIgor Kozhukhov 	case ZFS_TYPE_BOOKMARK:
9088f61deeSIgor Kozhukhov 		return (dgettext(TEXT_DOMAIN, "bookmark"));
9188f61deeSIgor Kozhukhov 	default:
9288f61deeSIgor Kozhukhov 		assert(!"unhandled zfs_type_t");
93fa9e4066Sahrens 	}
94fa9e4066Sahrens 
95fa9e4066Sahrens 	return (NULL);
96fa9e4066Sahrens }
97fa9e4066Sahrens 
98fa9e4066Sahrens /*
99fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
10014843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
10114843421SMatthew Ahrens  * explain exactly why the name was not valid.
102fa9e4066Sahrens  */
10399d5e173STim Haley int
104f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
105f18faf3fSek110237     boolean_t modifying)
106fa9e4066Sahrens {
107fa9e4066Sahrens 	namecheck_err_t why;
108fa9e4066Sahrens 	char what;
109fa9e4066Sahrens 
110edb901aaSMarcel Telka 	if (entity_namecheck(path, &why, &what) != 0) {
11199653d4eSeschrock 		if (hdl != NULL) {
112fa9e4066Sahrens 			switch (why) {
113b81d61a6Slling 			case NAME_ERR_TOOLONG:
11499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11599653d4eSeschrock 				    "name is too long"));
116b81d61a6Slling 				break;
117b81d61a6Slling 
118fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
11999653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12099653d4eSeschrock 				    "leading slash in name"));
121fa9e4066Sahrens 				break;
122fa9e4066Sahrens 
123fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
12499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12599653d4eSeschrock 				    "empty component in name"));
126fa9e4066Sahrens 				break;
127fa9e4066Sahrens 
128fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
12999653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13099653d4eSeschrock 				    "trailing slash in name"));
131fa9e4066Sahrens 				break;
132fa9e4066Sahrens 
133fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
13499653d4eSeschrock 				zfs_error_aux(hdl,
135fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
13699653d4eSeschrock 				    "'%c' in name"), what);
137fa9e4066Sahrens 				break;
138fa9e4066Sahrens 
139edb901aaSMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
14099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141edb901aaSMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
142edb901aaSMarcel Telka 				    "name"));
143fa9e4066Sahrens 				break;
1445ad82045Snd150628 
1455ad82045Snd150628 			case NAME_ERR_NOLETTER:
1465ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475ad82045Snd150628 				    "pool doesn't begin with a letter"));
1485ad82045Snd150628 				break;
1495ad82045Snd150628 
1505ad82045Snd150628 			case NAME_ERR_RESERVED:
1515ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1525ad82045Snd150628 				    "name is reserved"));
1535ad82045Snd150628 				break;
1545ad82045Snd150628 
1555ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1565ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1575ad82045Snd150628 				    "reserved disk name"));
1585ad82045Snd150628 				break;
15988f61deeSIgor Kozhukhov 
16088f61deeSIgor Kozhukhov 			default:
16188f61deeSIgor Kozhukhov 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16288f61deeSIgor Kozhukhov 				    "(%d) not defined"), why);
16388f61deeSIgor Kozhukhov 				break;
164fa9e4066Sahrens 			}
165fa9e4066Sahrens 		}
166fa9e4066Sahrens 
167fa9e4066Sahrens 		return (0);
168fa9e4066Sahrens 	}
169fa9e4066Sahrens 
170fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
17199653d4eSeschrock 		if (hdl != NULL)
17299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173edb901aaSMarcel Telka 			    "snapshot delimiter '@' is not expected here"));
174fa9e4066Sahrens 		return (0);
175fa9e4066Sahrens 	}
176fa9e4066Sahrens 
1771d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1781d452cf5Sahrens 		if (hdl != NULL)
1791d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
180d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
1811d452cf5Sahrens 		return (0);
1821d452cf5Sahrens 	}
1831d452cf5Sahrens 
184edb901aaSMarcel Telka 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
185edb901aaSMarcel Telka 		if (hdl != NULL)
186edb901aaSMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
187edb901aaSMarcel Telka 			    "bookmark delimiter '#' is not expected here"));
188edb901aaSMarcel Telka 		return (0);
189edb901aaSMarcel Telka 	}
190edb901aaSMarcel Telka 
191edb901aaSMarcel Telka 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
192edb901aaSMarcel Telka 		if (hdl != NULL)
193edb901aaSMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
194edb901aaSMarcel Telka 			    "missing '#' delimiter in bookmark name"));
195edb901aaSMarcel Telka 		return (0);
196edb901aaSMarcel Telka 	}
197edb901aaSMarcel Telka 
198f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
199f18faf3fSek110237 		if (hdl != NULL)
200f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
201f18faf3fSek110237 			    "invalid character %c in name"), '%');
202f18faf3fSek110237 		return (0);
203f18faf3fSek110237 	}
204f18faf3fSek110237 
20599653d4eSeschrock 	return (-1);
206fa9e4066Sahrens }
207fa9e4066Sahrens 
208fa9e4066Sahrens int
209fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
210fa9e4066Sahrens {
211e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
212e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
213f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216fa9e4066Sahrens /*
217e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
218e9dbad6fSeschrock  * properties into a separate nvlist.
219e9dbad6fSeschrock  */
220fac3008cSeschrock static nvlist_t *
221fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
222e9dbad6fSeschrock {
223e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
224e9dbad6fSeschrock 	nvpair_t *elem;
225e9dbad6fSeschrock 	nvlist_t *propval;
226fac3008cSeschrock 	nvlist_t *nvl;
227e9dbad6fSeschrock 
228fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
229fac3008cSeschrock 		(void) no_memory(hdl);
230fac3008cSeschrock 		return (NULL);
231fac3008cSeschrock 	}
232e9dbad6fSeschrock 
233e9dbad6fSeschrock 	elem = NULL;
234fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
235e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
236e9dbad6fSeschrock 			continue;
237e9dbad6fSeschrock 
238e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
239fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
240fac3008cSeschrock 			nvlist_free(nvl);
241fac3008cSeschrock 			(void) no_memory(hdl);
242fac3008cSeschrock 			return (NULL);
243fac3008cSeschrock 		}
244e9dbad6fSeschrock 	}
245e9dbad6fSeschrock 
246fac3008cSeschrock 	return (nvl);
247e9dbad6fSeschrock }
248e9dbad6fSeschrock 
24929ab75c9Srm160521 static zpool_handle_t *
25029ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
25129ab75c9Srm160521 {
25229ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
25329ab75c9Srm160521 	zpool_handle_t *zph;
25429ab75c9Srm160521 
25529ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
25629ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
25729ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
25829ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
25929ab75c9Srm160521 	}
26029ab75c9Srm160521 	return (zph);
26129ab75c9Srm160521 }
26229ab75c9Srm160521 
26329ab75c9Srm160521 static zpool_handle_t *
26429ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
26529ab75c9Srm160521 {
26629ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26729ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
26829ab75c9Srm160521 
26929ab75c9Srm160521 	while ((zph != NULL) &&
27029ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
27129ab75c9Srm160521 		zph = zph->zpool_next;
27229ab75c9Srm160521 	return (zph);
27329ab75c9Srm160521 }
27429ab75c9Srm160521 
27529ab75c9Srm160521 /*
27629ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
27729ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
27829ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
27929ab75c9Srm160521  */
28029ab75c9Srm160521 static zpool_handle_t *
28129ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
28229ab75c9Srm160521 {
28329ab75c9Srm160521 	char *pool_name;
28429ab75c9Srm160521 	int len;
28529ab75c9Srm160521 	zpool_handle_t *zph;
28629ab75c9Srm160521 
28778f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
28829ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
28929ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
29029ab75c9Srm160521 
29129ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
29229ab75c9Srm160521 	if (zph == NULL)
29329ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
29429ab75c9Srm160521 
29529ab75c9Srm160521 	free(pool_name);
29629ab75c9Srm160521 	return (zph);
29729ab75c9Srm160521 }
29829ab75c9Srm160521 
29929ab75c9Srm160521 void
30029ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
30129ab75c9Srm160521 {
30229ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
30329ab75c9Srm160521 
30429ab75c9Srm160521 	while (zph != NULL) {
30529ab75c9Srm160521 		next = zph->zpool_next;
30629ab75c9Srm160521 		zpool_close(zph);
30729ab75c9Srm160521 		zph = next;
30829ab75c9Srm160521 	}
30929ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
31029ab75c9Srm160521 }
31129ab75c9Srm160521 
312e9dbad6fSeschrock /*
313fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
314fa9e4066Sahrens  */
315fa9e4066Sahrens static int
316ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
317fa9e4066Sahrens {
318e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
319fa9e4066Sahrens 
320ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
321fa9e4066Sahrens 
322ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3237f7322feSeschrock 		if (errno == ENOMEM) {
324ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
32599653d4eSeschrock 				return (-1);
326e9dbad6fSeschrock 			}
3277f7322feSeschrock 		} else {
328fa9e4066Sahrens 			return (-1);
3297f7322feSeschrock 		}
3307f7322feSeschrock 	}
331ebedde84SEric Taylor 	return (0);
332fac3008cSeschrock }
333fac3008cSeschrock 
33492241e0bSTom Erickson /*
33592241e0bSTom Erickson  * Utility function to get the received properties of the given object.
33692241e0bSTom Erickson  */
33792241e0bSTom Erickson static int
33892241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
33992241e0bSTom Erickson {
34092241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34192241e0bSTom Erickson 	nvlist_t *recvdprops;
34292241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
34392241e0bSTom Erickson 	int err;
34492241e0bSTom Erickson 
34592241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
34692241e0bSTom Erickson 		return (-1);
34792241e0bSTom Erickson 
34892241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34992241e0bSTom Erickson 
35092241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
35192241e0bSTom Erickson 		if (errno == ENOMEM) {
35292241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
35392241e0bSTom Erickson 				return (-1);
35492241e0bSTom Erickson 			}
35592241e0bSTom Erickson 		} else {
35692241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
35792241e0bSTom Erickson 			return (-1);
35892241e0bSTom Erickson 		}
35992241e0bSTom Erickson 	}
36092241e0bSTom Erickson 
36192241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
36292241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
36392241e0bSTom Erickson 	if (err != 0)
36492241e0bSTom Erickson 		return (-1);
36592241e0bSTom Erickson 
36692241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
36792241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
36892241e0bSTom Erickson 
36992241e0bSTom Erickson 	return (0);
37092241e0bSTom Erickson }
37192241e0bSTom Erickson 
372ebedde84SEric Taylor static int
373ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
374ebedde84SEric Taylor {
375ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
376ebedde84SEric Taylor 
377ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
378ebedde84SEric Taylor 
379ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
380ebedde84SEric Taylor 		return (-1);
381ebedde84SEric Taylor 	}
382fac3008cSeschrock 
38314843421SMatthew Ahrens 	/*
38414843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
38514843421SMatthew Ahrens 	 * storing them in zfs_props?
38614843421SMatthew Ahrens 	 */
387fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
388fac3008cSeschrock 		nvlist_free(allprops);
389fac3008cSeschrock 		return (-1);
390fac3008cSeschrock 	}
391fac3008cSeschrock 
39299653d4eSeschrock 	nvlist_free(zhp->zfs_props);
393fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
39499653d4eSeschrock 
395fac3008cSeschrock 	zhp->zfs_props = allprops;
396fac3008cSeschrock 	zhp->zfs_user_props = userprops;
39799653d4eSeschrock 
398fa9e4066Sahrens 	return (0);
399fa9e4066Sahrens }
400fa9e4066Sahrens 
401ebedde84SEric Taylor static int
402ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
403ebedde84SEric Taylor {
404ebedde84SEric Taylor 	int rc = 0;
405ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
406ebedde84SEric Taylor 
407ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
408ebedde84SEric Taylor 		return (-1);
409ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
410ebedde84SEric Taylor 		rc = -1;
411ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
412ebedde84SEric Taylor 		rc = -1;
413ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
414ebedde84SEric Taylor 	return (rc);
415ebedde84SEric Taylor }
416ebedde84SEric Taylor 
417fa9e4066Sahrens /*
418fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
419fa9e4066Sahrens  */
420fa9e4066Sahrens void
421fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
422fa9e4066Sahrens {
423fa9e4066Sahrens 	(void) get_stats(zhp);
424fa9e4066Sahrens }
425fa9e4066Sahrens 
426fa9e4066Sahrens /*
427fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
428fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
429fa9e4066Sahrens  */
430ebedde84SEric Taylor static int
431ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
432fa9e4066Sahrens {
433503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
434ebedde84SEric Taylor 		return (-1);
43531fd60d3Sahrens 
436fa9e4066Sahrens 	/*
437fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
438fa9e4066Sahrens 	 * the high-level type.
439fa9e4066Sahrens 	 */
440a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
441a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
442a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
443a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
444e0f1c0afSOlaf Faaland 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
445e0f1c0afSOlaf Faaland 		return (-1);
446a2eea2e1Sahrens 	else
447a2eea2e1Sahrens 		abort();
448a2eea2e1Sahrens 
449fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
450fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
451fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
452fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
453fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
454fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
455fa9e4066Sahrens 	else
45699653d4eSeschrock 		abort();	/* we should never see any other types */
457fa9e4066Sahrens 
4589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
461ebedde84SEric Taylor 	return (0);
462ebedde84SEric Taylor }
463ebedde84SEric Taylor 
464ebedde84SEric Taylor zfs_handle_t *
465ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
466ebedde84SEric Taylor {
467ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
468ebedde84SEric Taylor 
469ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
470ebedde84SEric Taylor 
471ebedde84SEric Taylor 	if (zhp == NULL)
472ebedde84SEric Taylor 		return (NULL);
473ebedde84SEric Taylor 
474ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
475ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
476ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
477ebedde84SEric Taylor 		free(zhp);
478ebedde84SEric Taylor 		return (NULL);
479ebedde84SEric Taylor 	}
480ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
481ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
482ebedde84SEric Taylor 		free(zhp);
483ebedde84SEric Taylor 		return (NULL);
484ebedde84SEric Taylor 	}
485ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
486ebedde84SEric Taylor 		free(zhp);
487ebedde84SEric Taylor 		zhp = NULL;
488ebedde84SEric Taylor 	}
489ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
490ebedde84SEric Taylor 	return (zhp);
491ebedde84SEric Taylor }
492ebedde84SEric Taylor 
49319b94df9SMatthew Ahrens zfs_handle_t *
494ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
495ebedde84SEric Taylor {
496ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
497ebedde84SEric Taylor 
498ebedde84SEric Taylor 	if (zhp == NULL)
499ebedde84SEric Taylor 		return (NULL);
500ebedde84SEric Taylor 
501ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
502ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
503ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
504ebedde84SEric Taylor 		free(zhp);
505ebedde84SEric Taylor 		return (NULL);
506ebedde84SEric Taylor 	}
507fa9e4066Sahrens 	return (zhp);
508fa9e4066Sahrens }
509fa9e4066Sahrens 
51019b94df9SMatthew Ahrens zfs_handle_t *
5110d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
5120d8fa8f8SMartin Matuska {
5130d8fa8f8SMartin Matuska 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5140d8fa8f8SMartin Matuska 
5150d8fa8f8SMartin Matuska 	if (zhp == NULL)
5160d8fa8f8SMartin Matuska 		return (NULL);
5170d8fa8f8SMartin Matuska 
5180d8fa8f8SMartin Matuska 	zhp->zfs_hdl = pzhp->zfs_hdl;
5190d8fa8f8SMartin Matuska 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5200d8fa8f8SMartin Matuska 	zhp->zfs_head_type = pzhp->zfs_type;
5210d8fa8f8SMartin Matuska 	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
5220d8fa8f8SMartin Matuska 	zhp->zpool_hdl = zpool_handle(zhp);
5230d8fa8f8SMartin Matuska 	return (zhp);
5240d8fa8f8SMartin Matuska }
5250d8fa8f8SMartin Matuska 
5260d8fa8f8SMartin Matuska zfs_handle_t *
52719b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
52819b94df9SMatthew Ahrens {
52919b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
53019b94df9SMatthew Ahrens 
53119b94df9SMatthew Ahrens 	if (zhp == NULL)
53219b94df9SMatthew Ahrens 		return (NULL);
53319b94df9SMatthew Ahrens 
53419b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
53519b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
53619b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
53719b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
53819b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
53919b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
54019b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
54119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
54219b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
54319b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54419b94df9SMatthew Ahrens 			zfs_close(zhp);
54519b94df9SMatthew Ahrens 			return (NULL);
54619b94df9SMatthew Ahrens 		}
54719b94df9SMatthew Ahrens 	}
54819b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
54919b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
55019b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
55119b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55219b94df9SMatthew Ahrens 			zfs_close(zhp);
55319b94df9SMatthew Ahrens 			return (NULL);
55419b94df9SMatthew Ahrens 		}
55519b94df9SMatthew Ahrens 	}
55619b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
55719b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
55819b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
55919b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
56019b94df9SMatthew Ahrens 			zfs_close(zhp);
56119b94df9SMatthew Ahrens 			return (NULL);
56219b94df9SMatthew Ahrens 		}
56319b94df9SMatthew Ahrens 	}
56419b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
56519b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
56619b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
56719b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
56819b94df9SMatthew Ahrens 	}
56919b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
57019b94df9SMatthew Ahrens 	return (zhp);
57119b94df9SMatthew Ahrens }
57219b94df9SMatthew Ahrens 
57378f17100SMatthew Ahrens boolean_t
57478f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
57578f17100SMatthew Ahrens {
57678f17100SMatthew Ahrens 	nvlist_t *bmarks;
57778f17100SMatthew Ahrens 	nvlist_t *props;
5789adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
57978f17100SMatthew Ahrens 	char *bmark_name;
58078f17100SMatthew Ahrens 	char *pound;
58178f17100SMatthew Ahrens 	int err;
58278f17100SMatthew Ahrens 	boolean_t rv;
58378f17100SMatthew Ahrens 
58478f17100SMatthew Ahrens 
58578f17100SMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
58678f17100SMatthew Ahrens 	pound = strchr(fsname, '#');
58778f17100SMatthew Ahrens 	if (pound == NULL)
58878f17100SMatthew Ahrens 		return (B_FALSE);
58978f17100SMatthew Ahrens 
59078f17100SMatthew Ahrens 	*pound = '\0';
59178f17100SMatthew Ahrens 	bmark_name = pound + 1;
59278f17100SMatthew Ahrens 	props = fnvlist_alloc();
59378f17100SMatthew Ahrens 	err = lzc_get_bookmarks(fsname, props, &bmarks);
59478f17100SMatthew Ahrens 	nvlist_free(props);
59578f17100SMatthew Ahrens 	if (err != 0) {
59678f17100SMatthew Ahrens 		nvlist_free(bmarks);
59778f17100SMatthew Ahrens 		return (B_FALSE);
59878f17100SMatthew Ahrens 	}
59978f17100SMatthew Ahrens 
60078f17100SMatthew Ahrens 	rv = nvlist_exists(bmarks, bmark_name);
60178f17100SMatthew Ahrens 	nvlist_free(bmarks);
60278f17100SMatthew Ahrens 	return (rv);
60378f17100SMatthew Ahrens }
60478f17100SMatthew Ahrens 
60578f17100SMatthew Ahrens zfs_handle_t *
60678f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
60778f17100SMatthew Ahrens     nvlist_t *bmark_props)
60878f17100SMatthew Ahrens {
60978f17100SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
61078f17100SMatthew Ahrens 
61178f17100SMatthew Ahrens 	if (zhp == NULL)
61278f17100SMatthew Ahrens 		return (NULL);
61378f17100SMatthew Ahrens 
61478f17100SMatthew Ahrens 	/* Fill in the name. */
61578f17100SMatthew Ahrens 	zhp->zfs_hdl = parent->zfs_hdl;
61678f17100SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
61778f17100SMatthew Ahrens 
61878f17100SMatthew Ahrens 	/* Set the property lists. */
61978f17100SMatthew Ahrens 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
62078f17100SMatthew Ahrens 		free(zhp);
62178f17100SMatthew Ahrens 		return (NULL);
62278f17100SMatthew Ahrens 	}
62378f17100SMatthew Ahrens 
62478f17100SMatthew Ahrens 	/* Set the types. */
62578f17100SMatthew Ahrens 	zhp->zfs_head_type = parent->zfs_head_type;
62678f17100SMatthew Ahrens 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
62778f17100SMatthew Ahrens 
62878f17100SMatthew Ahrens 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
62978f17100SMatthew Ahrens 		nvlist_free(zhp->zfs_props);
63078f17100SMatthew Ahrens 		free(zhp);
63178f17100SMatthew Ahrens 		return (NULL);
63278f17100SMatthew Ahrens 	}
63378f17100SMatthew Ahrens 
63478f17100SMatthew Ahrens 	return (zhp);
63578f17100SMatthew Ahrens }
63678f17100SMatthew Ahrens 
637edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data {
638edb901aaSMarcel Telka 	const char *path;
639edb901aaSMarcel Telka 	zfs_handle_t *zhp;
640edb901aaSMarcel Telka };
641edb901aaSMarcel Telka 
642edb901aaSMarcel Telka static int
643edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
644edb901aaSMarcel Telka {
645edb901aaSMarcel Telka 	struct zfs_open_bookmarks_cb_data *dp = data;
646edb901aaSMarcel Telka 
647fa9e4066Sahrens 	/*
648edb901aaSMarcel Telka 	 * Is it the one we are looking for?
649edb901aaSMarcel Telka 	 */
650edb901aaSMarcel Telka 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
651edb901aaSMarcel Telka 		/*
652edb901aaSMarcel Telka 		 * We found it.  Save it and let the caller know we are done.
653edb901aaSMarcel Telka 		 */
654edb901aaSMarcel Telka 		dp->zhp = zhp;
655edb901aaSMarcel Telka 		return (EEXIST);
656edb901aaSMarcel Telka 	}
657edb901aaSMarcel Telka 
658edb901aaSMarcel Telka 	/*
659edb901aaSMarcel Telka 	 * Not found.  Close the handle and ask for another one.
660edb901aaSMarcel Telka 	 */
661edb901aaSMarcel Telka 	zfs_close(zhp);
662edb901aaSMarcel Telka 	return (0);
663edb901aaSMarcel Telka }
664edb901aaSMarcel Telka 
665edb901aaSMarcel Telka /*
666edb901aaSMarcel Telka  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
667fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
668fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
669fa9e4066Sahrens  */
670fa9e4066Sahrens zfs_handle_t *
67199653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
672fa9e4066Sahrens {
673fa9e4066Sahrens 	zfs_handle_t *zhp;
67499653d4eSeschrock 	char errbuf[1024];
675edb901aaSMarcel Telka 	char *bookp;
67699653d4eSeschrock 
67799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
67899653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
679fa9e4066Sahrens 
680fa9e4066Sahrens 	/*
68199653d4eSeschrock 	 * Validate the name before we even try to open it.
682fa9e4066Sahrens 	 */
683edb901aaSMarcel Telka 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
68499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
685fa9e4066Sahrens 		return (NULL);
686fa9e4066Sahrens 	}
687fa9e4066Sahrens 
688fa9e4066Sahrens 	/*
689edb901aaSMarcel Telka 	 * Bookmarks needs to be handled separately.
690edb901aaSMarcel Telka 	 */
691edb901aaSMarcel Telka 	bookp = strchr(path, '#');
692edb901aaSMarcel Telka 	if (bookp == NULL) {
693edb901aaSMarcel Telka 		/*
694edb901aaSMarcel Telka 		 * Try to get stats for the dataset, which will tell us if it
695edb901aaSMarcel Telka 		 * exists.
696fa9e4066Sahrens 		 */
697fa9e4066Sahrens 		errno = 0;
69899653d4eSeschrock 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
699ece3d9b3Slling 			(void) zfs_standard_error(hdl, errno, errbuf);
700fa9e4066Sahrens 			return (NULL);
701fa9e4066Sahrens 		}
702edb901aaSMarcel Telka 	} else {
703edb901aaSMarcel Telka 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
704edb901aaSMarcel Telka 		zfs_handle_t *pzhp;
705edb901aaSMarcel Telka 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
706edb901aaSMarcel Telka 
707edb901aaSMarcel Telka 		/*
708edb901aaSMarcel Telka 		 * We need to cut out '#' and everything after '#'
709edb901aaSMarcel Telka 		 * to get the parent dataset name only.
710edb901aaSMarcel Telka 		 */
711edb901aaSMarcel Telka 		assert(bookp - path < sizeof (dsname));
712edb901aaSMarcel Telka 		(void) strncpy(dsname, path, bookp - path);
713edb901aaSMarcel Telka 		dsname[bookp - path] = '\0';
714edb901aaSMarcel Telka 
715edb901aaSMarcel Telka 		/*
716edb901aaSMarcel Telka 		 * Create handle for the parent dataset.
717edb901aaSMarcel Telka 		 */
718edb901aaSMarcel Telka 		errno = 0;
719edb901aaSMarcel Telka 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
720edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
721edb901aaSMarcel Telka 			return (NULL);
722edb901aaSMarcel Telka 		}
723edb901aaSMarcel Telka 
724edb901aaSMarcel Telka 		/*
725edb901aaSMarcel Telka 		 * Iterate bookmarks to find the right one.
726edb901aaSMarcel Telka 		 */
727edb901aaSMarcel Telka 		errno = 0;
728edb901aaSMarcel Telka 		if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
729edb901aaSMarcel Telka 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
730edb901aaSMarcel Telka 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
731edb901aaSMarcel Telka 			zfs_close(pzhp);
732edb901aaSMarcel Telka 			return (NULL);
733edb901aaSMarcel Telka 		}
734edb901aaSMarcel Telka 		if (cb_data.zhp == NULL) {
735edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
736edb901aaSMarcel Telka 			zfs_close(pzhp);
737edb901aaSMarcel Telka 			return (NULL);
738edb901aaSMarcel Telka 		}
739edb901aaSMarcel Telka 		zhp = cb_data.zhp;
740edb901aaSMarcel Telka 
741edb901aaSMarcel Telka 		/*
742edb901aaSMarcel Telka 		 * Cleanup.
743edb901aaSMarcel Telka 		 */
744edb901aaSMarcel Telka 		zfs_close(pzhp);
745edb901aaSMarcel Telka 	}
746fa9e4066Sahrens 
747fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
74899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
74994de1d4cSeschrock 		zfs_close(zhp);
750fa9e4066Sahrens 		return (NULL);
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 
753fa9e4066Sahrens 	return (zhp);
754fa9e4066Sahrens }
755fa9e4066Sahrens 
756fa9e4066Sahrens /*
757fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
758fa9e4066Sahrens  */
759fa9e4066Sahrens void
760fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
761fa9e4066Sahrens {
762fa9e4066Sahrens 	if (zhp->zfs_mntopts)
763fa9e4066Sahrens 		free(zhp->zfs_mntopts);
76499653d4eSeschrock 	nvlist_free(zhp->zfs_props);
765e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
76692241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
767fa9e4066Sahrens 	free(zhp);
768fa9e4066Sahrens }
769fa9e4066Sahrens 
770ebedde84SEric Taylor typedef struct mnttab_node {
771ebedde84SEric Taylor 	struct mnttab mtn_mt;
772ebedde84SEric Taylor 	avl_node_t mtn_node;
773ebedde84SEric Taylor } mnttab_node_t;
774ebedde84SEric Taylor 
775ebedde84SEric Taylor static int
776ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
777ebedde84SEric Taylor {
778c4ab0d3fSGvozden Neskovic 	const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
779c4ab0d3fSGvozden Neskovic 	const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
780ebedde84SEric Taylor 	int rv;
781ebedde84SEric Taylor 
782ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
783ebedde84SEric Taylor 
784c4ab0d3fSGvozden Neskovic 	return (AVL_ISIGN(rv));
785ebedde84SEric Taylor }
786ebedde84SEric Taylor 
787ebedde84SEric Taylor void
788ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
789ebedde84SEric Taylor {
790591e0e13SSebastien Roy 	(void) mutex_init(&hdl->libzfs_mnttab_cache_lock,
791591e0e13SSebastien Roy 	    LOCK_NORMAL | LOCK_ERRORCHECK, NULL);
792ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
793ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
794ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
795b2634b9cSEric Taylor }
796b2634b9cSEric Taylor 
797b2634b9cSEric Taylor void
798b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
799b2634b9cSEric Taylor {
800b2634b9cSEric Taylor 	struct mnttab entry;
801ebedde84SEric Taylor 
802ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
803ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
804ebedde84SEric Taylor 		mnttab_node_t *mtn;
805ebedde84SEric Taylor 
806ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
807ebedde84SEric Taylor 			continue;
808ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
809ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
810ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
811ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
812ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
813ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
814ebedde84SEric Taylor 	}
815ebedde84SEric Taylor }
816ebedde84SEric Taylor 
817ebedde84SEric Taylor void
818ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
819ebedde84SEric Taylor {
820ebedde84SEric Taylor 	void *cookie = NULL;
821ebedde84SEric Taylor 	mnttab_node_t *mtn;
822ebedde84SEric Taylor 
82388f61deeSIgor Kozhukhov 	while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
82488f61deeSIgor Kozhukhov 	    != NULL) {
825ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
826ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
827ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
828ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
829ebedde84SEric Taylor 		free(mtn);
830ebedde84SEric Taylor 	}
831ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
832591e0e13SSebastien Roy 	(void) mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
833ebedde84SEric Taylor }
834ebedde84SEric Taylor 
835b2634b9cSEric Taylor void
836b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
837b2634b9cSEric Taylor {
838b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
839b2634b9cSEric Taylor }
840b2634b9cSEric Taylor 
841ebedde84SEric Taylor int
842ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
843ebedde84SEric Taylor     struct mnttab *entry)
844ebedde84SEric Taylor {
845ebedde84SEric Taylor 	mnttab_node_t find;
846ebedde84SEric Taylor 	mnttab_node_t *mtn;
847591e0e13SSebastien Roy 	int ret = ENOENT;
848ebedde84SEric Taylor 
849b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
850b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
851b2634b9cSEric Taylor 
852b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
853b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
854b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
855b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
856b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
857b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
858b2634b9cSEric Taylor 			return (0);
859b2634b9cSEric Taylor 		else
860b2634b9cSEric Taylor 			return (ENOENT);
861b2634b9cSEric Taylor 	}
862b2634b9cSEric Taylor 
863591e0e13SSebastien Roy 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
864ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
865b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
866ebedde84SEric Taylor 
867ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
868ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
869ebedde84SEric Taylor 	if (mtn) {
870ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
871591e0e13SSebastien Roy 		ret = 0;
872ebedde84SEric Taylor 	}
873591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
874591e0e13SSebastien Roy 	return (ret);
875ebedde84SEric Taylor }
876ebedde84SEric Taylor 
877ebedde84SEric Taylor void
878ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
879ebedde84SEric Taylor     const char *mountp, const char *mntopts)
880ebedde84SEric Taylor {
881ebedde84SEric Taylor 	mnttab_node_t *mtn;
882ebedde84SEric Taylor 
883591e0e13SSebastien Roy 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
884591e0e13SSebastien Roy 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
885ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
886ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
887ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
888ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
889ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
890ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
891ebedde84SEric Taylor 	}
892591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
893591e0e13SSebastien Roy }
894ebedde84SEric Taylor 
895ebedde84SEric Taylor void
896ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
897ebedde84SEric Taylor {
898ebedde84SEric Taylor 	mnttab_node_t find;
899ebedde84SEric Taylor 	mnttab_node_t *ret;
900ebedde84SEric Taylor 
901591e0e13SSebastien Roy 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
902ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
90388f61deeSIgor Kozhukhov 	if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
90488f61deeSIgor Kozhukhov 	    != NULL) {
905ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
906ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
907ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
908ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
909ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
910ebedde84SEric Taylor 		free(ret);
911ebedde84SEric Taylor 	}
912591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
913ebedde84SEric Taylor }
914ebedde84SEric Taylor 
9157b97dc1aSrm160521 int
9167b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9177b97dc1aSrm160521 {
91829ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9197b97dc1aSrm160521 
9207b97dc1aSrm160521 	if (zpool_handle == NULL)
9217b97dc1aSrm160521 		return (-1);
9227b97dc1aSrm160521 
9237b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
9247b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
9257b97dc1aSrm160521 	return (0);
9267b97dc1aSrm160521 }
9277b97dc1aSrm160521 
9287b97dc1aSrm160521 /*
9297b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
9307b97dc1aSrm160521  */
9317b97dc1aSrm160521 static int
9327b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9337b97dc1aSrm160521 {
9347b97dc1aSrm160521 	int spa_version;
9357b97dc1aSrm160521 
9367b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
9377b97dc1aSrm160521 		return (-1);
9387b97dc1aSrm160521 
9397b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
9407b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
9417b97dc1aSrm160521 	else
9427b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
9437b97dc1aSrm160521 
9447b97dc1aSrm160521 	return (0);
9457b97dc1aSrm160521 }
9467b97dc1aSrm160521 
947b1b8ab34Slling /*
948e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
949e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
950e9dbad6fSeschrock  * strings.
951fa9e4066Sahrens  */
9520a48a24eStimh nvlist_t *
9530a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
954e9316f76SJoe Stein     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
955eb633035STom Caputi     boolean_t key_params_ok, const char *errbuf)
956fa9e4066Sahrens {
957e9dbad6fSeschrock 	nvpair_t *elem;
958e9dbad6fSeschrock 	uint64_t intval;
959e9dbad6fSeschrock 	char *strval;
960990b4856Slling 	zfs_prop_t prop;
961e9dbad6fSeschrock 	nvlist_t *ret;
962da6c28aaSamw 	int chosen_normal = -1;
963da6c28aaSamw 	int chosen_utf = -1;
964990b4856Slling 
965e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
966e9dbad6fSeschrock 		(void) no_memory(hdl);
967e9dbad6fSeschrock 		return (NULL);
968e9dbad6fSeschrock 	}
969fa9e4066Sahrens 
97014843421SMatthew Ahrens 	/*
97114843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
97214843421SMatthew Ahrens 	 */
97314843421SMatthew Ahrens 
974e9dbad6fSeschrock 	elem = NULL;
975e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
976990b4856Slling 		const char *propname = nvpair_name(elem);
97799653d4eSeschrock 
97814843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
97914843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
980fa9e4066Sahrens 			/*
98114843421SMatthew Ahrens 			 * This is a user property: make sure it's a
982990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
983990b4856Slling 			 */
984990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
985990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986990b4856Slling 				    "'%s' must be a string"), propname);
987990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
988990b4856Slling 				goto error;
989990b4856Slling 			}
990990b4856Slling 
991990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
992e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
993e9dbad6fSeschrock 				    "property name '%s' is too long"),
994e9dbad6fSeschrock 				    propname);
995990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
996e9dbad6fSeschrock 				goto error;
997e9dbad6fSeschrock 			}
998e9dbad6fSeschrock 
999e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
1000e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
1001e9dbad6fSeschrock 				(void) no_memory(hdl);
1002e9dbad6fSeschrock 				goto error;
1003e9dbad6fSeschrock 			}
1004e9dbad6fSeschrock 			continue;
1005e9dbad6fSeschrock 		}
1006fa9e4066Sahrens 
100714843421SMatthew Ahrens 		/*
100814843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
100914843421SMatthew Ahrens 		 * snapshots.
101014843421SMatthew Ahrens 		 */
1011bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
1012bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
1014bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1015bb0ade09Sahrens 			goto error;
1016bb0ade09Sahrens 		}
1017bb0ade09Sahrens 
101814843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
101914843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
102014843421SMatthew Ahrens 			char newpropname[128];
102114843421SMatthew Ahrens 			char domain[128];
102214843421SMatthew Ahrens 			uint64_t rid;
102314843421SMatthew Ahrens 			uint64_t valary[3];
102414843421SMatthew Ahrens 
102514843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
102614843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
102714843421SMatthew Ahrens 				zfs_error_aux(hdl,
102814843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
102914843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
103014843421SMatthew Ahrens 				    propname);
103114843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
103214843421SMatthew Ahrens 				goto error;
103314843421SMatthew Ahrens 			}
103414843421SMatthew Ahrens 
103514843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
1036*f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_GROUPQUOTA &&
1037*f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_USEROBJQUOTA &&
1038*f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1039*f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_PROJECTQUOTA &&
1040*f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
104114843421SMatthew Ahrens 				zfs_error_aux(hdl,
104214843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
104314843421SMatthew Ahrens 				    propname);
104414843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
104514843421SMatthew Ahrens 				    errbuf);
104614843421SMatthew Ahrens 				goto error;
104714843421SMatthew Ahrens 			}
104814843421SMatthew Ahrens 
104914843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
105014843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
105114843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
105214843421SMatthew Ahrens 					intval = 0;
105314843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
105414843421SMatthew Ahrens 				    strval, &intval) != 0) {
105514843421SMatthew Ahrens 					(void) zfs_error(hdl,
105614843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
105714843421SMatthew Ahrens 					goto error;
105814843421SMatthew Ahrens 				}
105914843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
106014843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
106114843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
106214843421SMatthew Ahrens 				if (intval == 0) {
106314843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106414843421SMatthew Ahrens 					    "use 'none' to disable "
1065*f67950b2SNasf-Fan 					    "{user|group|project}quota"));
106614843421SMatthew Ahrens 					goto error;
106714843421SMatthew Ahrens 				}
106814843421SMatthew Ahrens 			} else {
106914843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107014843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
107114843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
107214843421SMatthew Ahrens 				goto error;
107314843421SMatthew Ahrens 			}
107414843421SMatthew Ahrens 
10752d5843dbSMatthew Ahrens 			/*
10762d5843dbSMatthew Ahrens 			 * Encode the prop name as
10772d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
10782d5843dbSMatthew Ahrens 			 * for the kernel to decode.
10792d5843dbSMatthew Ahrens 			 */
108014843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
10812d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10822d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
108314843421SMatthew Ahrens 			valary[0] = uqtype;
108414843421SMatthew Ahrens 			valary[1] = rid;
108514843421SMatthew Ahrens 			valary[2] = intval;
108614843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
108714843421SMatthew Ahrens 			    valary, 3) != 0) {
108814843421SMatthew Ahrens 				(void) no_memory(hdl);
108914843421SMatthew Ahrens 				goto error;
109014843421SMatthew Ahrens 			}
109114843421SMatthew Ahrens 			continue;
109219b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
109319b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109419b94df9SMatthew Ahrens 			    "'%s' is readonly"),
109519b94df9SMatthew Ahrens 			    propname);
109619b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
109719b94df9SMatthew Ahrens 			goto error;
109814843421SMatthew Ahrens 		}
109914843421SMatthew Ahrens 
110014843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
110114843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
110214843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
110314843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
110414843421SMatthew Ahrens 			goto error;
110514843421SMatthew Ahrens 		}
110614843421SMatthew Ahrens 
1107e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1108e9dbad6fSeschrock 			zfs_error_aux(hdl,
1109e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1110e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1111e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1112e9dbad6fSeschrock 			goto error;
1113e9dbad6fSeschrock 		}
1114e9dbad6fSeschrock 
1115e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1116eb633035STom Caputi 		    !(zfs_prop_setonce(prop) && zhp == NULL) &&
1117eb633035STom Caputi 		    !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1118e9dbad6fSeschrock 			zfs_error_aux(hdl,
1119e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1120e9dbad6fSeschrock 			    propname);
1121e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1122e9dbad6fSeschrock 			goto error;
1123e9dbad6fSeschrock 		}
1124e9dbad6fSeschrock 
1125990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1126990b4856Slling 		    &strval, &intval, errbuf) != 0)
1127e9dbad6fSeschrock 			goto error;
1128e9dbad6fSeschrock 
1129e9dbad6fSeschrock 		/*
1130e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1131e9dbad6fSeschrock 		 */
1132e9dbad6fSeschrock 		switch (prop) {
1133e7437265Sahrens 		case ZFS_PROP_VERSION:
1134e7437265Sahrens 		{
1135e7437265Sahrens 			int version;
1136e7437265Sahrens 
1137e7437265Sahrens 			if (zhp == NULL)
1138e7437265Sahrens 				break;
1139e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1140e7437265Sahrens 			if (intval < version) {
1141e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1142e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1143e7437265Sahrens 				    version);
1144e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1145e7437265Sahrens 				goto error;
1146e7437265Sahrens 			}
1147e7437265Sahrens 			break;
1148e7437265Sahrens 		}
1149e7437265Sahrens 
1150e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1151b5152584SMatthew Ahrens 		case ZFS_PROP_RECORDSIZE:
1152b5152584SMatthew Ahrens 		{
1153b5152584SMatthew Ahrens 			int maxbs = SPA_MAXBLOCKSIZE;
1154e9316f76SJoe Stein 			if (zpool_hdl != NULL) {
1155e9316f76SJoe Stein 				maxbs = zpool_get_prop_int(zpool_hdl,
1156b5152584SMatthew Ahrens 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1157b5152584SMatthew Ahrens 			}
1158b5152584SMatthew Ahrens 			/*
1159b5152584SMatthew Ahrens 			 * Volumes are limited to a volblocksize of 128KB,
1160b5152584SMatthew Ahrens 			 * because they typically service workloads with
1161b5152584SMatthew Ahrens 			 * small random writes, which incur a large performance
1162b5152584SMatthew Ahrens 			 * penalty with large blocks.
1163b5152584SMatthew Ahrens 			 */
1164b5152584SMatthew Ahrens 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1165b5152584SMatthew Ahrens 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1166b5152584SMatthew Ahrens 			/*
1167b5152584SMatthew Ahrens 			 * The value must be a power of two between
1168b5152584SMatthew Ahrens 			 * SPA_MINBLOCKSIZE and maxbs.
1169b5152584SMatthew Ahrens 			 */
1170e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1171b5152584SMatthew Ahrens 			    intval > maxbs || !ISP2(intval)) {
1172e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1173b5152584SMatthew Ahrens 				    "'%s' must be power of 2 from 512B "
1174b5152584SMatthew Ahrens 				    "to %uKB"), propname, maxbs >> 10);
1175e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1176e9dbad6fSeschrock 				goto error;
1177e9dbad6fSeschrock 			}
1178e9dbad6fSeschrock 			break;
1179b5152584SMatthew Ahrens 		}
1180663207adSDon Brady 
1181663207adSDon Brady 		case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1182663207adSDon Brady 			if (zpool_hdl != NULL) {
1183663207adSDon Brady 				char state[64] = "";
1184663207adSDon Brady 
1185663207adSDon Brady 				/*
1186663207adSDon Brady 				 * Issue a warning but do not fail so that
1187663207adSDon Brady 				 * tests for setable properties succeed.
1188663207adSDon Brady 				 */
1189663207adSDon Brady 				if (zpool_prop_get_feature(zpool_hdl,
1190663207adSDon Brady 				    "feature@allocation_classes", state,
1191663207adSDon Brady 				    sizeof (state)) != 0 ||
1192663207adSDon Brady 				    strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1193663207adSDon Brady 					(void) fprintf(stderr, gettext(
1194663207adSDon Brady 					    "%s: property requires a special "
1195663207adSDon Brady 					    "device in the pool\n"), propname);
1196663207adSDon Brady 				}
1197663207adSDon Brady 			}
1198663207adSDon Brady 			if (intval != 0 &&
1199663207adSDon Brady 			    (intval < SPA_MINBLOCKSIZE ||
1200663207adSDon Brady 			    intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) {
1201663207adSDon Brady 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1202663207adSDon Brady 				    "invalid '%s=%d' property: must be zero or "
1203663207adSDon Brady 				    "a power of 2 from 512B to 128K"), propname,
1204663207adSDon Brady 				    intval);
1205663207adSDon Brady 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1206663207adSDon Brady 				goto error;
1207663207adSDon Brady 			}
1208663207adSDon Brady 			break;
1209663207adSDon Brady 
12104201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
12114201a95eSRic Aleshire 		{
12124201a95eSRic Aleshire 			/*
12134201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
12144201a95eSRic Aleshire 			 * internal hex label string.
12154201a95eSRic Aleshire 			 */
12164201a95eSRic Aleshire 
12174201a95eSRic Aleshire 			m_label_t *new_sl;
12184201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
12194201a95eSRic Aleshire 
12204201a95eSRic Aleshire 			/* Default value is already OK. */
12214201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
12224201a95eSRic Aleshire 				break;
12234201a95eSRic Aleshire 
12244201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
12254201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
12264201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
12274201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
12284201a95eSRic Aleshire 				goto badlabel;
12294201a95eSRic Aleshire 			}
12304201a95eSRic Aleshire 
12314201a95eSRic Aleshire 			/* Now translate to hex internal label string */
12324201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
12334201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
12344201a95eSRic Aleshire 				if (hex)
12354201a95eSRic Aleshire 					free(hex);
12364201a95eSRic Aleshire 				goto badlabel;
12374201a95eSRic Aleshire 			}
12384201a95eSRic Aleshire 			m_label_free(new_sl);
12394201a95eSRic Aleshire 
12404201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
12414201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
12424201a95eSRic Aleshire 				free(hex);
12434201a95eSRic Aleshire 				break;
12444201a95eSRic Aleshire 			}
12454201a95eSRic Aleshire 
12464201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1247569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
12484201a95eSRic Aleshire 			    DATA_TYPE_STRING);
12494201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12504201a95eSRic Aleshire 			    hex) == 0);
12514201a95eSRic Aleshire 			free(hex);
12524201a95eSRic Aleshire 
12534201a95eSRic Aleshire 			break;
12544201a95eSRic Aleshire 
12554201a95eSRic Aleshire badlabel:
12564201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12574201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
12584201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12594201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
12604201a95eSRic Aleshire 			goto error;
12614201a95eSRic Aleshire 
12624201a95eSRic Aleshire 		}
12634201a95eSRic Aleshire 
1264e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
126589eef05eSrm160521 		{
126689eef05eSrm160521 			namecheck_err_t why;
126789eef05eSrm160521 
1268e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1269e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1270e9dbad6fSeschrock 				break;
1271e9dbad6fSeschrock 
127289eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
127389eef05eSrm160521 				switch (why) {
127489eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
127589eef05eSrm160521 					zfs_error_aux(hdl,
127689eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1277e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1278e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
127989eef05eSrm160521 					break;
128089eef05eSrm160521 				case NAME_ERR_TOOLONG:
128189eef05eSrm160521 					zfs_error_aux(hdl,
128289eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
128389eef05eSrm160521 					    "component of '%s' is too long"),
128489eef05eSrm160521 					    propname);
128589eef05eSrm160521 					break;
128688f61deeSIgor Kozhukhov 
128788f61deeSIgor Kozhukhov 				default:
128888f61deeSIgor Kozhukhov 					zfs_error_aux(hdl,
128988f61deeSIgor Kozhukhov 					    dgettext(TEXT_DOMAIN,
129088f61deeSIgor Kozhukhov 					    "(%d) not defined"),
129188f61deeSIgor Kozhukhov 					    why);
129288f61deeSIgor Kozhukhov 					break;
129389eef05eSrm160521 				}
1294e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1295e9dbad6fSeschrock 				goto error;
1296e9dbad6fSeschrock 			}
129789eef05eSrm160521 		}
129889eef05eSrm160521 
1299f3861e1aSahl 			/*FALLTHRU*/
1300e9dbad6fSeschrock 
1301da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1302f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1303e9dbad6fSeschrock 			/*
1304da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1305da6c28aaSamw 			 * properties, check if it can be set in a
1306da6c28aaSamw 			 * global/non-global zone based on
1307f3861e1aSahl 			 * the zoned property value:
1308fa9e4066Sahrens 			 *
1309fa9e4066Sahrens 			 *		global zone	    non-global zone
1310f3861e1aSahl 			 * --------------------------------------------------
1311fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1312fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1313da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1314fa9e4066Sahrens 			 *
1315fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1316fa9e4066Sahrens 			 *		sharenfs (yes)
1317da6c28aaSamw 			 *		sharesmb (yes)
1318fa9e4066Sahrens 			 */
1319e9dbad6fSeschrock 			if (zoned) {
1320fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
132199653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1322e9dbad6fSeschrock 					    "'%s' cannot be set on "
1323e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1324e9dbad6fSeschrock 					    propname);
1325e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1326e9dbad6fSeschrock 					    errbuf);
1327e9dbad6fSeschrock 					goto error;
1328da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1329da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
133099653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1331e9dbad6fSeschrock 					    "'%s' cannot be set in "
1332e9dbad6fSeschrock 					    "a non-global zone"), propname);
1333e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1334e9dbad6fSeschrock 					    errbuf);
1335e9dbad6fSeschrock 					goto error;
1336fa9e4066Sahrens 				}
1337fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1338fa9e4066Sahrens 				/*
1339fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
134014843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1341fa9e4066Sahrens 				 */
134299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1343e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1344e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1345e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1346e9dbad6fSeschrock 				goto error;
1347fa9e4066Sahrens 			}
1348f3861e1aSahl 
134967331909Sdougm 			/*
135067331909Sdougm 			 * At this point, it is legitimate to set the
135167331909Sdougm 			 * property. Now we want to make sure that the
135267331909Sdougm 			 * property value is valid if it is sharenfs.
135367331909Sdougm 			 */
1354da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1355da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
135667331909Sdougm 			    strcmp(strval, "on") != 0 &&
135767331909Sdougm 			    strcmp(strval, "off") != 0) {
1358da6c28aaSamw 				zfs_share_proto_t proto;
1359da6c28aaSamw 
1360da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1361da6c28aaSamw 					proto = PROTO_SMB;
1362da6c28aaSamw 				else
1363da6c28aaSamw 					proto = PROTO_NFS;
136467331909Sdougm 
136567331909Sdougm 				/*
1366da6c28aaSamw 				 * Must be an valid sharing protocol
1367da6c28aaSamw 				 * option string so init the libshare
1368da6c28aaSamw 				 * in order to enable the parser and
1369da6c28aaSamw 				 * then parse the options. We use the
1370da6c28aaSamw 				 * control API since we don't care about
1371da6c28aaSamw 				 * the current configuration and don't
137267331909Sdougm 				 * want the overhead of loading it
137367331909Sdougm 				 * until we actually do something.
137467331909Sdougm 				 */
137567331909Sdougm 
137667331909Sdougm 				if (zfs_init_libshare(hdl,
137767331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1378fac3008cSeschrock 					/*
1379fac3008cSeschrock 					 * An error occurred so we can't do
1380fac3008cSeschrock 					 * anything
1381fac3008cSeschrock 					 */
138267331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
138367331909Sdougm 					    "'%s' cannot be set: problem "
138467331909Sdougm 					    "in share initialization"),
138567331909Sdougm 					    propname);
1386fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1387fac3008cSeschrock 					    errbuf);
138867331909Sdougm 					goto error;
138967331909Sdougm 				}
139067331909Sdougm 
1391da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
139267331909Sdougm 					/*
139367331909Sdougm 					 * There was an error in parsing so
139467331909Sdougm 					 * deal with it by issuing an error
139567331909Sdougm 					 * message and leaving after
139667331909Sdougm 					 * uninitializing the the libshare
139767331909Sdougm 					 * interface.
139867331909Sdougm 					 */
139967331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140067331909Sdougm 					    "'%s' cannot be set to invalid "
140167331909Sdougm 					    "options"), propname);
1402fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1403fac3008cSeschrock 					    errbuf);
140467331909Sdougm 					zfs_uninit_libshare(hdl);
140567331909Sdougm 					goto error;
140667331909Sdougm 				}
140767331909Sdougm 				zfs_uninit_libshare(hdl);
140867331909Sdougm 			}
140967331909Sdougm 
1410f3861e1aSahl 			break;
141188f61deeSIgor Kozhukhov 
1412eb633035STom Caputi 		case ZFS_PROP_KEYLOCATION:
1413eb633035STom Caputi 			if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1414eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1415eb633035STom Caputi 				    "invalid keylocation"));
1416eb633035STom Caputi 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1417eb633035STom Caputi 				goto error;
1418eb633035STom Caputi 			}
1419eb633035STom Caputi 
1420eb633035STom Caputi 			if (zhp != NULL) {
1421eb633035STom Caputi 				uint64_t crypt =
1422eb633035STom Caputi 				    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1423eb633035STom Caputi 
1424eb633035STom Caputi 				if (crypt == ZIO_CRYPT_OFF &&
1425eb633035STom Caputi 				    strcmp(strval, "none") != 0) {
1426eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1427eb633035STom Caputi 					    "keylocation must be 'none' "
1428eb633035STom Caputi 					    "for unencrypted datasets"));
1429eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1430eb633035STom Caputi 					    errbuf);
1431eb633035STom Caputi 					goto error;
1432eb633035STom Caputi 				} else if (crypt != ZIO_CRYPT_OFF &&
1433eb633035STom Caputi 				    strcmp(strval, "none") == 0) {
1434eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1435eb633035STom Caputi 					    "keylocation must not be 'none' "
1436eb633035STom Caputi 					    "for encrypted datasets"));
1437eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1438eb633035STom Caputi 					    errbuf);
1439eb633035STom Caputi 					goto error;
1440eb633035STom Caputi 				}
1441eb633035STom Caputi 			}
1442eb633035STom Caputi 			break;
1443eb633035STom Caputi 
1444eb633035STom Caputi 		case ZFS_PROP_PBKDF2_ITERS:
1445eb633035STom Caputi 			if (intval < MIN_PBKDF2_ITERATIONS) {
1446eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1447eb633035STom Caputi 				    "minimum pbkdf2 iterations is %u"),
1448eb633035STom Caputi 				    MIN_PBKDF2_ITERATIONS);
1449eb633035STom Caputi 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1450eb633035STom Caputi 				goto error;
1451eb633035STom Caputi 			}
1452eb633035STom Caputi 			break;
1453eb633035STom Caputi 
1454da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1455da6c28aaSamw 			chosen_utf = (int)intval;
1456da6c28aaSamw 			break;
145788f61deeSIgor Kozhukhov 
1458da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1459da6c28aaSamw 			chosen_normal = (int)intval;
1460da6c28aaSamw 			break;
146188f61deeSIgor Kozhukhov 
146288f61deeSIgor Kozhukhov 		default:
146388f61deeSIgor Kozhukhov 			break;
1464fa9e4066Sahrens 		}
1465fa9e4066Sahrens 
1466e9dbad6fSeschrock 		/*
1467e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1468e9dbad6fSeschrock 		 * checks to enforce.
1469e9dbad6fSeschrock 		 */
1470e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1471e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1472e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1473e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1474e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1475e9dbad6fSeschrock 			char buf[64];
1476e9dbad6fSeschrock 
1477e9dbad6fSeschrock 			switch (prop) {
1478e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1479e9dbad6fSeschrock 				if (intval > volsize) {
1480e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1481e9dbad6fSeschrock 					    "'%s' is greater than current "
1482e9dbad6fSeschrock 					    "volume size"), propname);
1483e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1484e9dbad6fSeschrock 					    errbuf);
1485e9dbad6fSeschrock 					goto error;
1486e9dbad6fSeschrock 				}
1487e9dbad6fSeschrock 				break;
1488e9dbad6fSeschrock 
14891c10ae76SMike Gerdts 			case ZFS_PROP_REFRESERVATION:
14901c10ae76SMike Gerdts 				if (intval > volsize && intval != UINT64_MAX) {
14911c10ae76SMike Gerdts 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14921c10ae76SMike Gerdts 					    "'%s' is greater than current "
14931c10ae76SMike Gerdts 					    "volume size"), propname);
14941c10ae76SMike Gerdts 					(void) zfs_error(hdl, EZFS_BADPROP,
14951c10ae76SMike Gerdts 					    errbuf);
14961c10ae76SMike Gerdts 					goto error;
14971c10ae76SMike Gerdts 				}
14981c10ae76SMike Gerdts 				break;
14991c10ae76SMike Gerdts 
1500e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1501e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1502e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1503e9dbad6fSeschrock 					    sizeof (buf));
1504e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1506e9dbad6fSeschrock 					    "volume block size (%s)"),
1507e9dbad6fSeschrock 					    propname, buf);
1508e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1509e9dbad6fSeschrock 					    errbuf);
1510e9dbad6fSeschrock 					goto error;
1511e9dbad6fSeschrock 				}
1512e9dbad6fSeschrock 
1513e9dbad6fSeschrock 				if (intval == 0) {
1514e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1515e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1516e9dbad6fSeschrock 					    propname);
1517e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1518e9dbad6fSeschrock 					    errbuf);
1519e9dbad6fSeschrock 					goto error;
1520e9dbad6fSeschrock 				}
1521f3861e1aSahl 				break;
152288f61deeSIgor Kozhukhov 
152388f61deeSIgor Kozhukhov 			default:
152488f61deeSIgor Kozhukhov 				break;
1525e9dbad6fSeschrock 			}
1526e9dbad6fSeschrock 		}
1527eb633035STom Caputi 
1528eb633035STom Caputi 		/* check encryption properties */
1529eb633035STom Caputi 		if (zhp != NULL) {
1530eb633035STom Caputi 			int64_t crypt = zfs_prop_get_int(zhp,
1531eb633035STom Caputi 			    ZFS_PROP_ENCRYPTION);
1532eb633035STom Caputi 
1533eb633035STom Caputi 			switch (prop) {
1534eb633035STom Caputi 			case ZFS_PROP_COPIES:
1535eb633035STom Caputi 				if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1536eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1537eb633035STom Caputi 					    "encrypted datasets cannot have "
1538eb633035STom Caputi 					    "3 copies"));
1539eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1540eb633035STom Caputi 					    errbuf);
1541eb633035STom Caputi 					goto error;
1542eb633035STom Caputi 				}
1543eb633035STom Caputi 				break;
1544eb633035STom Caputi 			default:
1545eb633035STom Caputi 				break;
1546eb633035STom Caputi 			}
1547eb633035STom Caputi 		}
1548e9dbad6fSeschrock 	}
1549e9dbad6fSeschrock 
1550e9dbad6fSeschrock 	/*
1551da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1552da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1553da6c28aaSamw 	 *
1554da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1555da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1556da6c28aaSamw 	 */
1557de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1558da6c28aaSamw 		if (nvlist_add_uint64(ret,
1559da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1560da6c28aaSamw 			(void) no_memory(hdl);
1561da6c28aaSamw 			goto error;
1562da6c28aaSamw 		}
1563de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1564da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1565da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1566da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1567da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1568da6c28aaSamw 		goto error;
1569da6c28aaSamw 	}
1570e9dbad6fSeschrock 	return (ret);
1571e9dbad6fSeschrock 
1572e9dbad6fSeschrock error:
1573e9dbad6fSeschrock 	nvlist_free(ret);
1574e9dbad6fSeschrock 	return (NULL);
1575e9dbad6fSeschrock }
1576e9dbad6fSeschrock 
157736db6475SEric Taylor int
157836db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
157936db6475SEric Taylor {
158036db6475SEric Taylor 	uint64_t old_volsize;
158136db6475SEric Taylor 	uint64_t new_volsize;
158236db6475SEric Taylor 	uint64_t old_reservation;
158336db6475SEric Taylor 	uint64_t new_reservation;
158436db6475SEric Taylor 	zfs_prop_t resv_prop;
1585c61ea566SGeorge Wilson 	nvlist_t *props;
1586b73ccab0SMike Gerdts 	zpool_handle_t *zph = zpool_handle(zhp);
158736db6475SEric Taylor 
158836db6475SEric Taylor 	/*
158936db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
159036db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
159136db6475SEric Taylor 	 */
159236db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
159336db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
159436db6475SEric Taylor 		return (-1);
159536db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1596c61ea566SGeorge Wilson 
1597c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1598c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1599c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1600c61ea566SGeorge Wilson 
1601b73ccab0SMike Gerdts 	if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1602c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1603c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1604c61ea566SGeorge Wilson 		fnvlist_free(props);
160536db6475SEric Taylor 		return (0);
160636db6475SEric Taylor 	}
160736db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1608c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1609c61ea566SGeorge Wilson 		fnvlist_free(props);
161036db6475SEric Taylor 		return (-1);
1611c61ea566SGeorge Wilson 	}
1612b73ccab0SMike Gerdts 	new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1613c61ea566SGeorge Wilson 	fnvlist_free(props);
1614c61ea566SGeorge Wilson 
161536db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
161636db6475SEric Taylor 	    new_reservation) != 0) {
161736db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
161836db6475SEric Taylor 		return (-1);
161936db6475SEric Taylor 	}
162036db6475SEric Taylor 	return (1);
162136db6475SEric Taylor }
162236db6475SEric Taylor 
16231c10ae76SMike Gerdts /*
16241c10ae76SMike Gerdts  * Helper for 'zfs {set|clone} refreservation=auto'.  Must be called after
16251c10ae76SMike Gerdts  * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
16261c10ae76SMike Gerdts  * Return codes must match zfs_add_synthetic_resv().
16271c10ae76SMike Gerdts  */
16281c10ae76SMike Gerdts static int
16291c10ae76SMike Gerdts zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
16301c10ae76SMike Gerdts {
16311c10ae76SMike Gerdts 	uint64_t volsize;
16321c10ae76SMike Gerdts 	uint64_t resvsize;
16331c10ae76SMike Gerdts 	zfs_prop_t prop;
16341c10ae76SMike Gerdts 	nvlist_t *props;
16351c10ae76SMike Gerdts 
16361c10ae76SMike Gerdts 	if (!ZFS_IS_VOLUME(zhp)) {
16371c10ae76SMike Gerdts 		return (0);
16381c10ae76SMike Gerdts 	}
16391c10ae76SMike Gerdts 
16401c10ae76SMike Gerdts 	if (zfs_which_resv_prop(zhp, &prop) != 0) {
16411c10ae76SMike Gerdts 		return (-1);
16421c10ae76SMike Gerdts 	}
16431c10ae76SMike Gerdts 
16441c10ae76SMike Gerdts 	if (prop != ZFS_PROP_REFRESERVATION) {
16451c10ae76SMike Gerdts 		return (0);
16461c10ae76SMike Gerdts 	}
16471c10ae76SMike Gerdts 
16481c10ae76SMike Gerdts 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
16491c10ae76SMike Gerdts 		/* No value being set, so it can't be "auto" */
16501c10ae76SMike Gerdts 		return (0);
16511c10ae76SMike Gerdts 	}
16521c10ae76SMike Gerdts 	if (resvsize != UINT64_MAX) {
16531c10ae76SMike Gerdts 		/* Being set to a value other than "auto" */
16541c10ae76SMike Gerdts 		return (0);
16551c10ae76SMike Gerdts 	}
16561c10ae76SMike Gerdts 
16571c10ae76SMike Gerdts 	props = fnvlist_alloc();
16581c10ae76SMike Gerdts 
16591c10ae76SMike Gerdts 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
16601c10ae76SMike Gerdts 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
16611c10ae76SMike Gerdts 
16621c10ae76SMike Gerdts 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
16631c10ae76SMike Gerdts 	    &volsize) != 0) {
16641c10ae76SMike Gerdts 		volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
16651c10ae76SMike Gerdts 	}
16661c10ae76SMike Gerdts 
1667b73ccab0SMike Gerdts 	resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1668b73ccab0SMike Gerdts 	    props);
16691c10ae76SMike Gerdts 	fnvlist_free(props);
16701c10ae76SMike Gerdts 
16711c10ae76SMike Gerdts 	(void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
16721c10ae76SMike Gerdts 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
16731c10ae76SMike Gerdts 		(void) no_memory(zhp->zfs_hdl);
16741c10ae76SMike Gerdts 		return (-1);
16751c10ae76SMike Gerdts 	}
16761c10ae76SMike Gerdts 	return (1);
16771c10ae76SMike Gerdts }
16781c10ae76SMike Gerdts 
167992241e0bSTom Erickson void
168092241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
168192241e0bSTom Erickson     char *errbuf)
168292241e0bSTom Erickson {
168392241e0bSTom Erickson 	switch (err) {
168492241e0bSTom Erickson 
168592241e0bSTom Erickson 	case ENOSPC:
168692241e0bSTom Erickson 		/*
168792241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
168892241e0bSTom Erickson 		 * something different; setting a quota or reservation
168992241e0bSTom Erickson 		 * doesn't use any disk space.
169092241e0bSTom Erickson 		 */
169192241e0bSTom Erickson 		switch (prop) {
169292241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
169392241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
169492241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169592241e0bSTom Erickson 			    "size is less than current used or "
169692241e0bSTom Erickson 			    "reserved space"));
169792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
169892241e0bSTom Erickson 			break;
169992241e0bSTom Erickson 
170092241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
170192241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
170292241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170392241e0bSTom Erickson 			    "size is greater than available space"));
170492241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
170592241e0bSTom Erickson 			break;
170692241e0bSTom Erickson 
170792241e0bSTom Erickson 		default:
170892241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
170992241e0bSTom Erickson 			break;
171092241e0bSTom Erickson 		}
171192241e0bSTom Erickson 		break;
171292241e0bSTom Erickson 
171392241e0bSTom Erickson 	case EBUSY:
171492241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
171592241e0bSTom Erickson 		break;
171692241e0bSTom Erickson 
171792241e0bSTom Erickson 	case EROFS:
171892241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
171992241e0bSTom Erickson 		break;
172092241e0bSTom Erickson 
17216fdcb3d1SWill Andrews 	case E2BIG:
17226fdcb3d1SWill Andrews 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17236fdcb3d1SWill Andrews 		    "property value too long"));
17246fdcb3d1SWill Andrews 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
17256fdcb3d1SWill Andrews 		break;
17266fdcb3d1SWill Andrews 
172792241e0bSTom Erickson 	case ENOTSUP:
172892241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172992241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
173092241e0bSTom Erickson 		    "property or value"));
173192241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
173292241e0bSTom Erickson 		break;
173392241e0bSTom Erickson 
173492241e0bSTom Erickson 	case ERANGE:
1735b5152584SMatthew Ahrens 		if (prop == ZFS_PROP_COMPRESSION ||
1736b5152584SMatthew Ahrens 		    prop == ZFS_PROP_RECORDSIZE) {
173792241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173892241e0bSTom Erickson 			    "property setting is not allowed on "
173992241e0bSTom Erickson 			    "bootable datasets"));
174092241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
174145818ee1SMatthew Ahrens 		} else if (prop == ZFS_PROP_CHECKSUM ||
174245818ee1SMatthew Ahrens 		    prop == ZFS_PROP_DEDUP) {
174345818ee1SMatthew Ahrens 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174445818ee1SMatthew Ahrens 			    "property setting is not allowed on "
174545818ee1SMatthew Ahrens 			    "root pools"));
174645818ee1SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
174792241e0bSTom Erickson 		} else {
174892241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
174992241e0bSTom Erickson 		}
175092241e0bSTom Erickson 		break;
175192241e0bSTom Erickson 
1752ab003da8SJim Dunham 	case EINVAL:
1753ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1754ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1755ab003da8SJim Dunham 		} else {
1756ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1757ab003da8SJim Dunham 		}
1758ab003da8SJim Dunham 		break;
1759ab003da8SJim Dunham 
1760eb633035STom Caputi 	case EACCES:
1761eb633035STom Caputi 		if (prop == ZFS_PROP_KEYLOCATION) {
1762eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1763eb633035STom Caputi 			    "keylocation may only be set on encryption roots"));
1764eb633035STom Caputi 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1765eb633035STom Caputi 		} else {
1766eb633035STom Caputi 			(void) zfs_standard_error(hdl, err, errbuf);
1767eb633035STom Caputi 		}
1768eb633035STom Caputi 		break;
1769eb633035STom Caputi 
177092241e0bSTom Erickson 	case EOVERFLOW:
177192241e0bSTom Erickson 		/*
177292241e0bSTom Erickson 		 * This platform can't address a volume this big.
177392241e0bSTom Erickson 		 */
177492241e0bSTom Erickson #ifdef _ILP32
177592241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
177692241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
177792241e0bSTom Erickson 			break;
177892241e0bSTom Erickson 		}
177992241e0bSTom Erickson #endif
178092241e0bSTom Erickson 		/* FALLTHROUGH */
178192241e0bSTom Erickson 	default:
178292241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
178392241e0bSTom Erickson 	}
178492241e0bSTom Erickson }
178592241e0bSTom Erickson 
1786e9dbad6fSeschrock /*
1787e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1788e9dbad6fSeschrock  */
1789e9dbad6fSeschrock int
1790e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1791e9dbad6fSeschrock {
1792e9dbad6fSeschrock 	int ret = -1;
1793e9dbad6fSeschrock 	char errbuf[1024];
1794e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
179530925561SChris Williamson 	nvlist_t *nvl = NULL;
1796e9dbad6fSeschrock 
1797e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1798e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1799e9dbad6fSeschrock 	    zhp->zfs_name);
1800e9dbad6fSeschrock 
1801e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1802e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1803e9dbad6fSeschrock 		(void) no_memory(hdl);
1804e9dbad6fSeschrock 		goto error;
1805e9dbad6fSeschrock 	}
1806e9dbad6fSeschrock 
180730925561SChris Williamson 	ret = zfs_prop_set_list(zhp, nvl);
180830925561SChris Williamson 
180930925561SChris Williamson error:
181030925561SChris Williamson 	nvlist_free(nvl);
181130925561SChris Williamson 	return (ret);
181230925561SChris Williamson }
181330925561SChris Williamson 
181430925561SChris Williamson 
181530925561SChris Williamson 
181630925561SChris Williamson /*
181730925561SChris Williamson  * Given an nvlist of property names and values, set the properties for the
181830925561SChris Williamson  * given dataset.
181930925561SChris Williamson  */
182030925561SChris Williamson int
182130925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
182230925561SChris Williamson {
182330925561SChris Williamson 	zfs_cmd_t zc = { 0 };
182430925561SChris Williamson 	int ret = -1;
182530925561SChris Williamson 	prop_changelist_t **cls = NULL;
182630925561SChris Williamson 	int cl_idx;
182730925561SChris Williamson 	char errbuf[1024];
182830925561SChris Williamson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
182930925561SChris Williamson 	nvlist_t *nvl;
183030925561SChris Williamson 	int nvl_len;
1831f83b46baSPaul Dagnelie 	int added_resv = 0;
183230925561SChris Williamson 
183330925561SChris Williamson 	(void) snprintf(errbuf, sizeof (errbuf),
183430925561SChris Williamson 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
183530925561SChris Williamson 	    zhp->zfs_name);
183630925561SChris Williamson 
183730925561SChris Williamson 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1838e9316f76SJoe Stein 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1839eb633035STom Caputi 	    B_FALSE, errbuf)) == NULL)
1840e9dbad6fSeschrock 		goto error;
1841990b4856Slling 
184230925561SChris Williamson 	/*
184330925561SChris Williamson 	 * We have to check for any extra properties which need to be added
184430925561SChris Williamson 	 * before computing the length of the nvlist.
184530925561SChris Williamson 	 */
184630925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
184730925561SChris Williamson 	    elem != NULL;
184830925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
184930925561SChris Williamson 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
185030925561SChris Williamson 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
185130925561SChris Williamson 			goto error;
185230925561SChris Williamson 		}
185330925561SChris Williamson 	}
18541c10ae76SMike Gerdts 
18551c10ae76SMike Gerdts 	if (added_resv != 1 &&
18561c10ae76SMike Gerdts 	    (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
18571c10ae76SMike Gerdts 		goto error;
18581c10ae76SMike Gerdts 	}
18591c10ae76SMike Gerdts 
186030925561SChris Williamson 	/*
186130925561SChris Williamson 	 * Check how many properties we're setting and allocate an array to
186230925561SChris Williamson 	 * store changelist pointers for postfix().
186330925561SChris Williamson 	 */
186430925561SChris Williamson 	nvl_len = 0;
186530925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
186630925561SChris Williamson 	    elem != NULL;
186730925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem))
186830925561SChris Williamson 		nvl_len++;
186930925561SChris Williamson 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
187030925561SChris Williamson 		goto error;
1871e9dbad6fSeschrock 
187230925561SChris Williamson 	cl_idx = 0;
187330925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
187430925561SChris Williamson 	    elem != NULL;
187530925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
1876e9dbad6fSeschrock 
187730925561SChris Williamson 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
187830925561SChris Williamson 
187930925561SChris Williamson 		assert(cl_idx < nvl_len);
188030925561SChris Williamson 		/*
188130925561SChris Williamson 		 * We don't want to unmount & remount the dataset when changing
188230925561SChris Williamson 		 * its canmount property to 'on' or 'noauto'.  We only use
188330925561SChris Williamson 		 * the changelist logic to unmount when setting canmount=off.
188430925561SChris Williamson 		 */
1885c079fa4dSAndriy Gapon 		if (prop != ZFS_PROP_CANMOUNT ||
1886c079fa4dSAndriy Gapon 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1887c079fa4dSAndriy Gapon 		    zfs_is_mounted(zhp, NULL))) {
188830925561SChris Williamson 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
188930925561SChris Williamson 			if (cls[cl_idx] == NULL)
189036db6475SEric Taylor 				goto error;
189136db6475SEric Taylor 		}
189236db6475SEric Taylor 
189330925561SChris Williamson 		if (prop == ZFS_PROP_MOUNTPOINT &&
189430925561SChris Williamson 		    changelist_haszonedchild(cls[cl_idx])) {
189599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1896fa9e4066Sahrens 			    "child dataset with inherited mountpoint is used "
189799653d4eSeschrock 			    "in a non-global zone"));
189899653d4eSeschrock 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1899fa9e4066Sahrens 			goto error;
1900fa9e4066Sahrens 		}
1901fa9e4066Sahrens 
190230925561SChris Williamson 		if (cls[cl_idx] != NULL &&
190330925561SChris Williamson 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1904fa9e4066Sahrens 			goto error;
1905fa9e4066Sahrens 
190630925561SChris Williamson 		cl_idx++;
190730925561SChris Williamson 	}
190830925561SChris Williamson 	assert(cl_idx == nvl_len);
190930925561SChris Williamson 
1910fa9e4066Sahrens 	/*
191130925561SChris Williamson 	 * Execute the corresponding ioctl() to set this list of properties.
1912fa9e4066Sahrens 	 */
1913fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1914fa9e4066Sahrens 
191530925561SChris Williamson 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
191630925561SChris Williamson 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1917e9dbad6fSeschrock 		goto error;
1918e9dbad6fSeschrock 
1919ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1920743a77edSAlan Wright 
1921fa9e4066Sahrens 	if (ret != 0) {
1922f62db44dSAndrew Stormont 		if (zc.zc_nvlist_dst_filled == B_FALSE) {
1923f62db44dSAndrew Stormont 			(void) zfs_standard_error(hdl, errno, errbuf);
1924f62db44dSAndrew Stormont 			goto error;
1925f62db44dSAndrew Stormont 		}
1926f62db44dSAndrew Stormont 
192730925561SChris Williamson 		/* Get the list of unset properties back and report them. */
192830925561SChris Williamson 		nvlist_t *errorprops = NULL;
192930925561SChris Williamson 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
193030925561SChris Williamson 			goto error;
1931f62db44dSAndrew Stormont 		for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
193230925561SChris Williamson 		    elem != NULL;
1933f62db44dSAndrew Stormont 		    elem = nvlist_next_nvpair(errorprops, elem)) {
193430925561SChris Williamson 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
193592241e0bSTom Erickson 			zfs_setprop_error(hdl, prop, errno, errbuf);
193630925561SChris Williamson 		}
193730925561SChris Williamson 		nvlist_free(errorprops);
193830925561SChris Williamson 
193936db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
194036db6475SEric Taylor 			/* clean up the volsize property we tried to set */
194136db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
194236db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
194336db6475SEric Taylor 			nvlist_free(nvl);
194430925561SChris Williamson 			nvl = NULL;
194536db6475SEric Taylor 			zcmd_free_nvlists(&zc);
194630925561SChris Williamson 
194736db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
194836db6475SEric Taylor 				goto error;
194936db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
195036db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
195136db6475SEric Taylor 			    old_volsize) != 0)
195236db6475SEric Taylor 				goto error;
195336db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
195436db6475SEric Taylor 				goto error;
195536db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
195636db6475SEric Taylor 		}
1957fa9e4066Sahrens 	} else {
195830925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
195930925561SChris Williamson 			if (cls[cl_idx] != NULL) {
196030925561SChris Williamson 				int clp_err = changelist_postfix(cls[cl_idx]);
196130925561SChris Williamson 				if (clp_err != 0)
196230925561SChris Williamson 					ret = clp_err;
196330925561SChris Williamson 			}
196430925561SChris Williamson 		}
1965a227b7f4Shs24103 
1966fa9e4066Sahrens 		/*
1967fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1968fa9e4066Sahrens 		 * is reflected.
1969fa9e4066Sahrens 		 */
1970a227b7f4Shs24103 		if (ret == 0)
1971fa9e4066Sahrens 			(void) get_stats(zhp);
1972fa9e4066Sahrens 	}
1973fa9e4066Sahrens 
1974fa9e4066Sahrens error:
1975e9dbad6fSeschrock 	nvlist_free(nvl);
1976e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
197730925561SChris Williamson 	if (cls != NULL) {
197830925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
197930925561SChris Williamson 			if (cls[cl_idx] != NULL)
198030925561SChris Williamson 				changelist_free(cls[cl_idx]);
198130925561SChris Williamson 		}
198230925561SChris Williamson 		free(cls);
198330925561SChris Williamson 	}
1984fa9e4066Sahrens 	return (ret);
1985fa9e4066Sahrens }
1986fa9e4066Sahrens 
1987fa9e4066Sahrens /*
198892241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
198992241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1990fa9e4066Sahrens  */
1991fa9e4066Sahrens int
199292241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1993fa9e4066Sahrens {
1994fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1995fa9e4066Sahrens 	int ret;
1996fa9e4066Sahrens 	prop_changelist_t *cl;
199799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
199899653d4eSeschrock 	char errbuf[1024];
1999e9dbad6fSeschrock 	zfs_prop_t prop;
200099653d4eSeschrock 
200199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
200299653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2003fa9e4066Sahrens 
200492241e0bSTom Erickson 	zc.zc_cookie = received;
2005990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2006e9dbad6fSeschrock 		/*
2007e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
2008e9dbad6fSeschrock 		 * small, so just do it here.
2009e9dbad6fSeschrock 		 */
2010e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
2011e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2012e9dbad6fSeschrock 			    "invalid property"));
2013e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2014e9dbad6fSeschrock 		}
2015e9dbad6fSeschrock 
2016e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2017e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2018e9dbad6fSeschrock 
2019e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2020e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2021e9dbad6fSeschrock 
2022e9dbad6fSeschrock 		return (0);
2023e9dbad6fSeschrock 	}
2024e9dbad6fSeschrock 
2025fa9e4066Sahrens 	/*
2026fa9e4066Sahrens 	 * Verify that this property is inheritable.
2027fa9e4066Sahrens 	 */
202899653d4eSeschrock 	if (zfs_prop_readonly(prop))
202999653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2030fa9e4066Sahrens 
203192241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
203299653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2033fa9e4066Sahrens 
2034fa9e4066Sahrens 	/*
2035fa9e4066Sahrens 	 * Check to see if the value applies to this type
2036fa9e4066Sahrens 	 */
203799653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
203899653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2039fa9e4066Sahrens 
2040bf7c2d40Srm160521 	/*
204136db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
2042bf7c2d40Srm160521 	 */
2043bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
2044fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2045e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2046fa9e4066Sahrens 
2047fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2048fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
204999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205099653d4eSeschrock 		    "dataset is used in a non-global zone"));
205199653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2052fa9e4066Sahrens 	}
2053fa9e4066Sahrens 
2054fa9e4066Sahrens 	/*
2055fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
2056fa9e4066Sahrens 	 */
20570069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2058fa9e4066Sahrens 		return (-1);
2059fa9e4066Sahrens 
2060fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
206199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
206299653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
206399653d4eSeschrock 		    "in a non-global zone"));
206499653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2065fa9e4066Sahrens 		goto error;
2066fa9e4066Sahrens 	}
2067fa9e4066Sahrens 
2068fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
2069fa9e4066Sahrens 		goto error;
2070fa9e4066Sahrens 
2071e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
207299653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2073fa9e4066Sahrens 	} else {
2074fa9e4066Sahrens 
2075efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
2076fa9e4066Sahrens 			goto error;
2077fa9e4066Sahrens 
2078fa9e4066Sahrens 		/*
2079fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
2080fa9e4066Sahrens 		 */
2081fa9e4066Sahrens 		(void) get_stats(zhp);
2082fa9e4066Sahrens 	}
2083fa9e4066Sahrens 
2084fa9e4066Sahrens error:
2085fa9e4066Sahrens 	changelist_free(cl);
2086fa9e4066Sahrens 	return (ret);
2087fa9e4066Sahrens }
2088fa9e4066Sahrens 
2089fa9e4066Sahrens /*
20907f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
20917f7322feSeschrock  * extract them appropriately.
20927f7322feSeschrock  */
20937f7322feSeschrock static uint64_t
20947f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20957f7322feSeschrock {
20967f7322feSeschrock 	nvlist_t *nv;
20977f7322feSeschrock 	uint64_t value;
20987f7322feSeschrock 
2099a2eea2e1Sahrens 	*source = NULL;
21007f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21017f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
2102990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2103990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21047f7322feSeschrock 	} else {
21052e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
21062e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
21077f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
21087f7322feSeschrock 		*source = "";
21097f7322feSeschrock 	}
21107f7322feSeschrock 
21117f7322feSeschrock 	return (value);
21127f7322feSeschrock }
21137f7322feSeschrock 
21149c3fd121SMatthew Ahrens static const char *
21157f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21167f7322feSeschrock {
21177f7322feSeschrock 	nvlist_t *nv;
21189c3fd121SMatthew Ahrens 	const char *value;
21197f7322feSeschrock 
2120a2eea2e1Sahrens 	*source = NULL;
21217f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21227f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21239c3fd121SMatthew Ahrens 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2124990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21257f7322feSeschrock 	} else {
21262e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
21272e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
21289c3fd121SMatthew Ahrens 		value = zfs_prop_default_string(prop);
21297f7322feSeschrock 		*source = "";
21307f7322feSeschrock 	}
21317f7322feSeschrock 
21327f7322feSeschrock 	return (value);
21337f7322feSeschrock }
21347f7322feSeschrock 
213592241e0bSTom Erickson static boolean_t
213692241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
213792241e0bSTom Erickson {
213892241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
213992241e0bSTom Erickson }
214092241e0bSTom Erickson 
214192241e0bSTom Erickson static void
214292241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
214392241e0bSTom Erickson {
214492241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
214592241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
214692241e0bSTom Erickson }
214792241e0bSTom Erickson 
214892241e0bSTom Erickson static void
214992241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
215092241e0bSTom Erickson {
215192241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
215292241e0bSTom Erickson 	*cookie = 0;
215392241e0bSTom Erickson }
215492241e0bSTom Erickson 
21557f7322feSeschrock /*
2156fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2157fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
2158fa9e4066Sahrens  *
2159fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2160fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2161fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
2162fa9e4066Sahrens  * the source "temporary".
2163fa9e4066Sahrens  */
216499653d4eSeschrock static int
2165990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
216699653d4eSeschrock     char **source, uint64_t *val)
2167fa9e4066Sahrens {
2168bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
216996510749Stimh 	nvlist_t *zplprops = NULL;
2170fa9e4066Sahrens 	struct mnttab mnt;
21713ccfa83cSahrens 	char *mntopt_on = NULL;
21723ccfa83cSahrens 	char *mntopt_off = NULL;
217392241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2174fa9e4066Sahrens 
2175fa9e4066Sahrens 	*source = NULL;
2176fa9e4066Sahrens 
21773ccfa83cSahrens 	switch (prop) {
21783ccfa83cSahrens 	case ZFS_PROP_ATIME:
21793ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
21803ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
21813ccfa83cSahrens 		break;
21823ccfa83cSahrens 
21833ccfa83cSahrens 	case ZFS_PROP_DEVICES:
21843ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
21853ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
21863ccfa83cSahrens 		break;
21873ccfa83cSahrens 
21883ccfa83cSahrens 	case ZFS_PROP_EXEC:
21893ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
21903ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
21913ccfa83cSahrens 		break;
21923ccfa83cSahrens 
21933ccfa83cSahrens 	case ZFS_PROP_READONLY:
21943ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
21953ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
21963ccfa83cSahrens 		break;
21973ccfa83cSahrens 
21983ccfa83cSahrens 	case ZFS_PROP_SETUID:
21993ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
22003ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
22013ccfa83cSahrens 		break;
22023ccfa83cSahrens 
22033ccfa83cSahrens 	case ZFS_PROP_XATTR:
22043ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
22053ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
22063ccfa83cSahrens 		break;
2207da6c28aaSamw 
2208da6c28aaSamw 	case ZFS_PROP_NBMAND:
2209da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
2210da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2211da6c28aaSamw 		break;
221288f61deeSIgor Kozhukhov 
221388f61deeSIgor Kozhukhov 	default:
221488f61deeSIgor Kozhukhov 		break;
22153ccfa83cSahrens 	}
22163ccfa83cSahrens 
22173bb79becSeschrock 	/*
22183bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
22193bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
22203bb79becSeschrock 	 * we're looking up a property which requires its presence.
22213bb79becSeschrock 	 */
22223bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
22233ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2224ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2225ebedde84SEric Taylor 		struct mnttab entry;
22263bb79becSeschrock 
2227ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2228ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
22293ccfa83cSahrens 			    entry.mnt_mntopts);
22303ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
22313bb79becSeschrock 				return (-1);
22323ccfa83cSahrens 		}
22333bb79becSeschrock 
22343bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
22353bb79becSeschrock 	}
22363bb79becSeschrock 
2237fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2238fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2239fa9e4066Sahrens 	else
2240fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2241fa9e4066Sahrens 
2242fa9e4066Sahrens 	switch (prop) {
2243fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2244fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2245fa9e4066Sahrens 	case ZFS_PROP_EXEC:
22463ccfa83cSahrens 	case ZFS_PROP_READONLY:
22473ccfa83cSahrens 	case ZFS_PROP_SETUID:
22483ccfa83cSahrens 	case ZFS_PROP_XATTR:
2249da6c28aaSamw 	case ZFS_PROP_NBMAND:
225099653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2251fa9e4066Sahrens 
225292241e0bSTom Erickson 		if (received)
225392241e0bSTom Erickson 			break;
225492241e0bSTom Erickson 
22553ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
225699653d4eSeschrock 			*val = B_TRUE;
2257fa9e4066Sahrens 			if (src)
2258990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
22593ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
226099653d4eSeschrock 			*val = B_FALSE;
2261fa9e4066Sahrens 			if (src)
2262990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2263fa9e4066Sahrens 		}
226499653d4eSeschrock 		break;
2265fa9e4066Sahrens 
22663ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
2267d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
2268fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2269a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2270fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2271a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2272a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2273a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2274a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2275a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2276a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
227792241e0bSTom Erickson 
227892241e0bSTom Erickson 		if (*source == NULL) {
227992241e0bSTom Erickson 			/* not default, must be local */
2280fa9e4066Sahrens 			*source = zhp->zfs_name;
228192241e0bSTom Erickson 		}
228299653d4eSeschrock 		break;
2283fa9e4066Sahrens 
2284fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
228599653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
228699653d4eSeschrock 		break;
2287fa9e4066Sahrens 
228839c23413Seschrock 	case ZFS_PROP_NUMCLONES:
228939c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
229039c23413Seschrock 		break;
229139c23413Seschrock 
2292bd00f61bSrm160521 	case ZFS_PROP_VERSION:
2293de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2294de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2295de8267e0Stimh 	case ZFS_PROP_CASE:
2296de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2297de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22983d7934e1Srm160521 			return (-1);
2299bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2300de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2301de8267e0Stimh 			zcmd_free_nvlists(&zc);
2302f4b94bdeSMatthew Ahrens 			return (-1);
2303bd00f61bSrm160521 		}
2304de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2305de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2306de8267e0Stimh 		    val) != 0) {
2307de8267e0Stimh 			zcmd_free_nvlists(&zc);
2308f4b94bdeSMatthew Ahrens 			return (-1);
2309de8267e0Stimh 		}
231096510749Stimh 		nvlist_free(zplprops);
2311de8267e0Stimh 		zcmd_free_nvlists(&zc);
2312bd00f61bSrm160521 		break;
2313bd00f61bSrm160521 
2314ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
2315ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
2316ca48f36fSKeith M Wesolowski 		break;
2317ca48f36fSKeith M Wesolowski 
2318fa9e4066Sahrens 	default:
2319e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
232091ebeef5Sahrens 		case PROP_TYPE_NUMBER:
232191ebeef5Sahrens 		case PROP_TYPE_INDEX:
2322e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
232374e7dc98SMatthew Ahrens 			/*
232414843421SMatthew Ahrens 			 * If we tried to use a default value for a
232574e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
23264d86c0eaSMatthew Ahrens 			 * present.  Note this only applies to "truly"
23274d86c0eaSMatthew Ahrens 			 * readonly properties, not set-once properties
23284d86c0eaSMatthew Ahrens 			 * like volblocksize.
232974e7dc98SMatthew Ahrens 			 */
233074e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
23314d86c0eaSMatthew Ahrens 			    !zfs_prop_setonce(prop) &&
233231f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
233331f572c2STom Erickson 				*source = NULL;
2334ad2760acSMatthew Ahrens 				return (-1);
233574e7dc98SMatthew Ahrens 			}
2336e7437265Sahrens 			break;
2337e7437265Sahrens 
233891ebeef5Sahrens 		case PROP_TYPE_STRING:
2339e7437265Sahrens 		default:
234099653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
234199653d4eSeschrock 			    "cannot get non-numeric property"));
234299653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
234399653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
2344fa9e4066Sahrens 		}
2345e7437265Sahrens 	}
2346fa9e4066Sahrens 
2347fa9e4066Sahrens 	return (0);
2348fa9e4066Sahrens }
2349fa9e4066Sahrens 
2350fa9e4066Sahrens /*
2351fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2352fa9e4066Sahrens  */
2353fa9e4066Sahrens static void
2354990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2355fa9e4066Sahrens     char *statbuf, size_t statlen)
2356fa9e4066Sahrens {
2357990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2358fa9e4066Sahrens 		return;
2359fa9e4066Sahrens 
2360fa9e4066Sahrens 	if (source == NULL) {
2361990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2362fa9e4066Sahrens 	} else if (source[0] == '\0') {
2363990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
236492241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
236592241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
2366fa9e4066Sahrens 	} else {
2367fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2368990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2369fa9e4066Sahrens 		} else {
2370fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2371990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2372fa9e4066Sahrens 		}
2373fa9e4066Sahrens 	}
2374fa9e4066Sahrens 
2375fa9e4066Sahrens }
2376fa9e4066Sahrens 
237792241e0bSTom Erickson int
237892241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
237992241e0bSTom Erickson     size_t proplen, boolean_t literal)
238092241e0bSTom Erickson {
238192241e0bSTom Erickson 	zfs_prop_t prop;
238292241e0bSTom Erickson 	int err = 0;
238392241e0bSTom Erickson 
238492241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
238592241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
238692241e0bSTom Erickson 			return (-1);
238792241e0bSTom Erickson 
238892241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
238992241e0bSTom Erickson 
239092241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
239192241e0bSTom Erickson 		uint64_t cookie;
239292241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
239392241e0bSTom Erickson 			return (-1);
239492241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
239592241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
239692241e0bSTom Erickson 		    NULL, NULL, 0, literal);
239792241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
239892241e0bSTom Erickson 	} else {
239992241e0bSTom Erickson 		nvlist_t *propval;
240092241e0bSTom Erickson 		char *recvdval;
240192241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
240292241e0bSTom Erickson 		    propname, &propval) != 0)
240392241e0bSTom Erickson 			return (-1);
240492241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
240592241e0bSTom Erickson 		    &recvdval) == 0);
240692241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
240792241e0bSTom Erickson 	}
240892241e0bSTom Erickson 
240992241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
241092241e0bSTom Erickson }
241192241e0bSTom Erickson 
241219b94df9SMatthew Ahrens static int
241319b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
241419b94df9SMatthew Ahrens {
241519b94df9SMatthew Ahrens 	nvlist_t *value;
241619b94df9SMatthew Ahrens 	nvpair_t *pair;
241719b94df9SMatthew Ahrens 
241819b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
241919b94df9SMatthew Ahrens 	if (value == NULL)
242019b94df9SMatthew Ahrens 		return (-1);
242119b94df9SMatthew Ahrens 
242219b94df9SMatthew Ahrens 	propbuf[0] = '\0';
242319b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
242419b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
242519b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
242619b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
242719b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
242819b94df9SMatthew Ahrens 	}
242919b94df9SMatthew Ahrens 
243019b94df9SMatthew Ahrens 	return (0);
243119b94df9SMatthew Ahrens }
243219b94df9SMatthew Ahrens 
243319b94df9SMatthew Ahrens struct get_clones_arg {
243419b94df9SMatthew Ahrens 	uint64_t numclones;
243519b94df9SMatthew Ahrens 	nvlist_t *value;
243619b94df9SMatthew Ahrens 	const char *origin;
24379adfa60dSMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
243819b94df9SMatthew Ahrens };
243919b94df9SMatthew Ahrens 
244019b94df9SMatthew Ahrens int
244119b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
244219b94df9SMatthew Ahrens {
244319b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
244419b94df9SMatthew Ahrens 
244519b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
244619b94df9SMatthew Ahrens 		zfs_close(zhp);
244719b94df9SMatthew Ahrens 		return (0);
244819b94df9SMatthew Ahrens 	}
244919b94df9SMatthew Ahrens 
245019b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
245119b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
245219b94df9SMatthew Ahrens 		goto out;
245319b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
24543b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
245519b94df9SMatthew Ahrens 		gca->numclones--;
245619b94df9SMatthew Ahrens 	}
245719b94df9SMatthew Ahrens 
245819b94df9SMatthew Ahrens out:
245919b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
246019b94df9SMatthew Ahrens 	zfs_close(zhp);
246119b94df9SMatthew Ahrens 	return (0);
246219b94df9SMatthew Ahrens }
246319b94df9SMatthew Ahrens 
246419b94df9SMatthew Ahrens nvlist_t *
246519b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
246619b94df9SMatthew Ahrens {
246719b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
246819b94df9SMatthew Ahrens 
246919b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
247019b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
247119b94df9SMatthew Ahrens 		struct get_clones_arg gca;
247219b94df9SMatthew Ahrens 
247319b94df9SMatthew Ahrens 		/*
247419b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
247519b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
247619b94df9SMatthew Ahrens 		 */
247719b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
247819b94df9SMatthew Ahrens 			return (NULL);
247919b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
248019b94df9SMatthew Ahrens 			return (NULL);
248119b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
248219b94df9SMatthew Ahrens 			nvlist_free(nv);
248319b94df9SMatthew Ahrens 			return (NULL);
248419b94df9SMatthew Ahrens 		}
248519b94df9SMatthew Ahrens 
248619b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
248719b94df9SMatthew Ahrens 		gca.value = value;
248819b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
248919b94df9SMatthew Ahrens 
249019b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
249119b94df9SMatthew Ahrens 			zfs_handle_t *root;
24929adfa60dSMatthew Ahrens 			char pool[ZFS_MAX_DATASET_NAME_LEN];
249319b94df9SMatthew Ahrens 			char *cp = pool;
249419b94df9SMatthew Ahrens 
249519b94df9SMatthew Ahrens 			/* get the pool name */
249619b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
249719b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
249819b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
249919b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
250019b94df9SMatthew Ahrens 
250119b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
250219b94df9SMatthew Ahrens 		}
250319b94df9SMatthew Ahrens 
250419b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
250519b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
250619b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
250719b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
250819b94df9SMatthew Ahrens 			nvlist_free(nv);
250919b94df9SMatthew Ahrens 			nvlist_free(value);
251019b94df9SMatthew Ahrens 			return (NULL);
251119b94df9SMatthew Ahrens 		}
251219b94df9SMatthew Ahrens 		nvlist_free(nv);
251319b94df9SMatthew Ahrens 		nvlist_free(value);
251419b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
251519b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
251619b94df9SMatthew Ahrens 	}
251719b94df9SMatthew Ahrens 
251819b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
251919b94df9SMatthew Ahrens 
252019b94df9SMatthew Ahrens 	return (value);
252119b94df9SMatthew Ahrens }
252219b94df9SMatthew Ahrens 
2523fa9e4066Sahrens /*
2524dfc11533SChris Williamson  * Accepts a property and value and checks that the value
2525dfc11533SChris Williamson  * matches the one found by the channel program. If they are
2526dfc11533SChris Williamson  * not equal, print both of them.
2527dfc11533SChris Williamson  */
2528dfc11533SChris Williamson void
2529dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2530dfc11533SChris Williamson     const char *strval)
2531dfc11533SChris Williamson {
2532dfc11533SChris Williamson 	if (!zhp->zfs_hdl->libzfs_prop_debug)
2533dfc11533SChris Williamson 		return;
2534dfc11533SChris Williamson 	int error;
2535dfc11533SChris Williamson 	char *poolname = zhp->zpool_hdl->zpool_name;
2536dfc11533SChris Williamson 	const char *program =
2537dfc11533SChris Williamson 	    "args = ...\n"
2538dfc11533SChris Williamson 	    "ds = args['dataset']\n"
2539dfc11533SChris Williamson 	    "prop = args['property']\n"
2540dfc11533SChris Williamson 	    "value, setpoint = zfs.get_prop(ds, prop)\n"
2541dfc11533SChris Williamson 	    "return {value=value, setpoint=setpoint}\n";
2542dfc11533SChris Williamson 	nvlist_t *outnvl;
2543dfc11533SChris Williamson 	nvlist_t *retnvl;
2544dfc11533SChris Williamson 	nvlist_t *argnvl = fnvlist_alloc();
2545dfc11533SChris Williamson 
2546dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2547dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2548dfc11533SChris Williamson 
2549a3b28680SSerapheim Dimitropoulos 	error = lzc_channel_program_nosync(poolname, program,
2550dfc11533SChris Williamson 	    10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2551dfc11533SChris Williamson 
2552dfc11533SChris Williamson 	if (error == 0) {
2553dfc11533SChris Williamson 		retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2554dfc11533SChris Williamson 		if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2555dfc11533SChris Williamson 			int64_t ans;
2556dfc11533SChris Williamson 			error = nvlist_lookup_int64(retnvl, "value", &ans);
2557dfc11533SChris Williamson 			if (error != 0) {
2558dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2559dfc11533SChris Williamson 				    error);
2560dfc11533SChris Williamson 				return;
2561dfc11533SChris Williamson 			}
2562dfc11533SChris Williamson 			if (ans != intval) {
2563dfc11533SChris Williamson 				(void) fprintf(stderr,
2564dfc11533SChris Williamson 				    "%s: zfs found %lld, but zcp found %lld\n",
2565dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2566dfc11533SChris Williamson 				    (longlong_t)intval, (longlong_t)ans);
2567dfc11533SChris Williamson 			}
2568dfc11533SChris Williamson 		} else {
2569dfc11533SChris Williamson 			char *str_ans;
2570dfc11533SChris Williamson 			error = nvlist_lookup_string(retnvl, "value", &str_ans);
2571dfc11533SChris Williamson 			if (error != 0) {
2572dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2573dfc11533SChris Williamson 				    error);
2574dfc11533SChris Williamson 				return;
2575dfc11533SChris Williamson 			}
2576dfc11533SChris Williamson 			if (strcmp(strval, str_ans) != 0) {
2577dfc11533SChris Williamson 				(void) fprintf(stderr,
2578dfc11533SChris Williamson 				    "%s: zfs found %s, but zcp found %s\n",
2579dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2580dfc11533SChris Williamson 				    strval, str_ans);
2581dfc11533SChris Williamson 			}
2582dfc11533SChris Williamson 		}
2583dfc11533SChris Williamson 	} else {
2584dfc11533SChris Williamson 		(void) fprintf(stderr,
2585dfc11533SChris Williamson 		    "zcp check failed, channel program error: %u\n", error);
2586dfc11533SChris Williamson 	}
2587dfc11533SChris Williamson 	nvlist_free(argnvl);
2588dfc11533SChris Williamson 	nvlist_free(outnvl);
2589dfc11533SChris Williamson }
2590dfc11533SChris Williamson 
2591dfc11533SChris Williamson /*
2592fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2593fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2594fa9e4066Sahrens  * human-readable form.
2595fa9e4066Sahrens  *
2596fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2597fa9e4066Sahrens  */
2598fa9e4066Sahrens int
2599fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2600990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2601fa9e4066Sahrens {
2602fa9e4066Sahrens 	char *source = NULL;
2603fa9e4066Sahrens 	uint64_t val;
26049c3fd121SMatthew Ahrens 	const char *str;
2605e9dbad6fSeschrock 	const char *strval;
260692241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2607fa9e4066Sahrens 
2608fa9e4066Sahrens 	/*
2609fa9e4066Sahrens 	 * Check to see if this property applies to our object
2610fa9e4066Sahrens 	 */
2611fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2612fa9e4066Sahrens 		return (-1);
2613fa9e4066Sahrens 
261492241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
261592241e0bSTom Erickson 		return (-1);
261692241e0bSTom Erickson 
2617fa9e4066Sahrens 	if (src)
2618990b4856Slling 		*src = ZPROP_SRC_NONE;
2619fa9e4066Sahrens 
2620fa9e4066Sahrens 	switch (prop) {
2621fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2622fa9e4066Sahrens 		/*
2623fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2624fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2625fa9e4066Sahrens 		 */
2626fa9e4066Sahrens 		{
2627a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2628a2eea2e1Sahrens 			time_t time = (time_t)val;
2629fa9e4066Sahrens 			struct tm t;
2630fa9e4066Sahrens 
2631fa9e4066Sahrens 			if (literal ||
2632fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2633fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2634fa9e4066Sahrens 			    &t) == 0)
2635a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2636fa9e4066Sahrens 		}
2637dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2638fa9e4066Sahrens 		break;
2639fa9e4066Sahrens 
2640fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2641fa9e4066Sahrens 		/*
2642fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2643fa9e4066Sahrens 		 *
2644fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2645fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2646fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2647fa9e4066Sahrens 		 *
2648fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2649fa9e4066Sahrens 		 * root to any values we return.
2650fa9e4066Sahrens 		 */
265129ab75c9Srm160521 
26527f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2653fa9e4066Sahrens 
2654b21718f0Sgw25295 		if (str[0] == '/') {
265529ab75c9Srm160521 			char buf[MAXPATHLEN];
265629ab75c9Srm160521 			char *root = buf;
2657a79992aaSTom Erickson 			const char *relpath;
2658fa9e4066Sahrens 
2659a79992aaSTom Erickson 			/*
2660a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2661a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2662a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2663a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2664a79992aaSTom Erickson 			 * inherited.
2665a79992aaSTom Erickson 			 */
2666a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2667a79992aaSTom Erickson 				relpath = "";
2668a79992aaSTom Erickson 			} else {
2669a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2670fa9e4066Sahrens 				if (relpath[0] == '/')
2671fa9e4066Sahrens 					relpath++;
2672a79992aaSTom Erickson 			}
2673b21718f0Sgw25295 
267429ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
2675c58b3526SAdam Stevko 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2676c58b3526SAdam Stevko 			    B_FALSE)) || (strcmp(root, "-") == 0))
267729ab75c9Srm160521 				root[0] = '\0';
2678b21718f0Sgw25295 			/*
2679b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2680b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2681b21718f0Sgw25295 			 * mountpoint path.
2682b21718f0Sgw25295 			 */
2683b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2684b21718f0Sgw25295 				root++;
2685b21718f0Sgw25295 
2686b21718f0Sgw25295 			/*
2687b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2688b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2689b21718f0Sgw25295 			 * an inherited mountpoint.
2690b21718f0Sgw25295 			 */
2691b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2692b21718f0Sgw25295 			    relpath[0] != '\0'))
26937f7322feSeschrock 				str++;
2694fa9e4066Sahrens 
2695fa9e4066Sahrens 			if (relpath[0] == '\0')
2696fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
26977f7322feSeschrock 				    root, str);
2698fa9e4066Sahrens 			else
2699fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
27007f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2701fa9e4066Sahrens 				    relpath);
2702fa9e4066Sahrens 		} else {
2703fa9e4066Sahrens 			/* 'legacy' or 'none' */
27047f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2705fa9e4066Sahrens 		}
27065b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, propbuf);
2707fa9e4066Sahrens 		break;
2708fa9e4066Sahrens 
2709fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
27109c3fd121SMatthew Ahrens 		str = getprop_string(zhp, prop, &source);
27119c3fd121SMatthew Ahrens 		if (str == NULL)
2712fa9e4066Sahrens 			return (-1);
27139c3fd121SMatthew Ahrens 		(void) strlcpy(propbuf, str, proplen);
27145b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, str);
2715fa9e4066Sahrens 		break;
2716fa9e4066Sahrens 
271719b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
271819b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
271919b94df9SMatthew Ahrens 			return (-1);
272019b94df9SMatthew Ahrens 		break;
272119b94df9SMatthew Ahrens 
2722fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2723a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2724fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2725a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2726a9799022Sck153898 
272799653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
272899653d4eSeschrock 			return (-1);
2729fa9e4066Sahrens 		/*
2730fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2731fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2732fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2733fa9e4066Sahrens 		 * that its set locally.
2734fa9e4066Sahrens 		 */
2735fa9e4066Sahrens 		if (val == 0) {
2736fa9e4066Sahrens 			if (literal)
2737fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2738fa9e4066Sahrens 			else
2739fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2740fa9e4066Sahrens 		} else {
2741fa9e4066Sahrens 			if (literal)
27425ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
27435ad82045Snd150628 				    (u_longlong_t)val);
2744fa9e4066Sahrens 			else
2745fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2746fa9e4066Sahrens 		}
2747dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2748fa9e4066Sahrens 		break;
2749fa9e4066Sahrens 
2750a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2751a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2752a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2753a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2754a2afb611SJerry Jelinek 
2755a2afb611SJerry Jelinek 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2756a2afb611SJerry Jelinek 			return (-1);
2757a2afb611SJerry Jelinek 
2758a2afb611SJerry Jelinek 		/*
2759a2afb611SJerry Jelinek 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2760a2afb611SJerry Jelinek 		 * literal is set), and indicate that it's the default value.
2761a2afb611SJerry Jelinek 		 * Otherwise, we print the number nicely and indicate that it's
2762a2afb611SJerry Jelinek 		 * set locally.
2763a2afb611SJerry Jelinek 		 */
2764a2afb611SJerry Jelinek 		if (literal) {
2765a2afb611SJerry Jelinek 			(void) snprintf(propbuf, proplen, "%llu",
2766a2afb611SJerry Jelinek 			    (u_longlong_t)val);
2767a2afb611SJerry Jelinek 		} else if (val == UINT64_MAX) {
2768a2afb611SJerry Jelinek 			(void) strlcpy(propbuf, "none", proplen);
2769a2afb611SJerry Jelinek 		} else {
2770a2afb611SJerry Jelinek 			zfs_nicenum(val, propbuf, proplen);
2771a2afb611SJerry Jelinek 		}
2772dfc11533SChris Williamson 
2773dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2774a2afb611SJerry Jelinek 		break;
2775a2afb611SJerry Jelinek 
2776187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2777fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
277899653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
277999653d4eSeschrock 			return (-1);
2780b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2781b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2782b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2783dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2784fa9e4066Sahrens 		break;
2785fa9e4066Sahrens 
2786fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2787fa9e4066Sahrens 		switch (zhp->zfs_type) {
2788fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2789fa9e4066Sahrens 			str = "filesystem";
2790fa9e4066Sahrens 			break;
2791fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2792fa9e4066Sahrens 			str = "volume";
2793fa9e4066Sahrens 			break;
2794fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2795fa9e4066Sahrens 			str = "snapshot";
2796fa9e4066Sahrens 			break;
279778f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
279878f17100SMatthew Ahrens 			str = "bookmark";
279978f17100SMatthew Ahrens 			break;
2800fa9e4066Sahrens 		default:
280199653d4eSeschrock 			abort();
2802fa9e4066Sahrens 		}
2803fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
28045b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, propbuf);
2805fa9e4066Sahrens 		break;
2806fa9e4066Sahrens 
2807fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2808fa9e4066Sahrens 		/*
2809fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2810fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2811fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2812fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2813fa9e4066Sahrens 		 */
281499653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
281599653d4eSeschrock 		    src, &source, &val) != 0)
281699653d4eSeschrock 			return (-1);
281799653d4eSeschrock 		if (val)
2818fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2819fa9e4066Sahrens 		else
2820fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2821fa9e4066Sahrens 		break;
2822fa9e4066Sahrens 
2823fa9e4066Sahrens 	case ZFS_PROP_NAME:
2824fa9e4066Sahrens 		/*
2825fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2826fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2827fa9e4066Sahrens 		 * consumers.
2828fa9e4066Sahrens 		 */
2829fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
28305b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, propbuf);
2831fa9e4066Sahrens 		break;
2832fa9e4066Sahrens 
28334201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
28344201a95eSRic Aleshire 		{
28354201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
28364201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
28374201a95eSRic Aleshire 
28384201a95eSRic Aleshire 			(void) strlcpy(propbuf,
28394201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
28404201a95eSRic Aleshire 
28414201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
28424201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
28434201a95eSRic Aleshire 				break;
28444201a95eSRic Aleshire 
28454201a95eSRic Aleshire 			/*
28464201a95eSRic Aleshire 			 * Try to translate the internal hex string to
28474201a95eSRic Aleshire 			 * human-readable output.  If there are any
28484201a95eSRic Aleshire 			 * problems just use the hex string.
28494201a95eSRic Aleshire 			 */
28504201a95eSRic Aleshire 
28514201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
28524201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
28534201a95eSRic Aleshire 				m_label_free(new_sl);
28544201a95eSRic Aleshire 				break;
28554201a95eSRic Aleshire 			}
28564201a95eSRic Aleshire 
28574201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
28584201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
28594201a95eSRic Aleshire 				if (ascii)
28604201a95eSRic Aleshire 					free(ascii);
28614201a95eSRic Aleshire 				m_label_free(new_sl);
28624201a95eSRic Aleshire 				break;
28634201a95eSRic Aleshire 			}
28644201a95eSRic Aleshire 			m_label_free(new_sl);
28654201a95eSRic Aleshire 
28664201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
28674201a95eSRic Aleshire 			free(ascii);
28684201a95eSRic Aleshire 		}
28694201a95eSRic Aleshire 		break;
28704201a95eSRic Aleshire 
2871f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2872e8d4a73cSJosh Paetzel 	case ZFS_PROP_CREATETXG:
2873f0f3ef5aSGarrett D'Amore 		/*
2874f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2875f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2876f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2877f0f3ef5aSGarrett D'Amore 		 */
2878f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2879f0f3ef5aSGarrett D'Amore 			return (-1);
2880f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2881dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2882f0f3ef5aSGarrett D'Amore 		break;
2883f0f3ef5aSGarrett D'Amore 
2884fa9e4066Sahrens 	default:
2885e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
288691ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2887e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2888dfc11533SChris Williamson 			    &source, &val) != 0) {
2889e7437265Sahrens 				return (-1);
2890dfc11533SChris Williamson 			}
2891dfc11533SChris Williamson 
2892dfc11533SChris Williamson 			if (literal) {
2893e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2894e7437265Sahrens 				    (u_longlong_t)val);
2895dfc11533SChris Williamson 			} else {
2896e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2897dfc11533SChris Williamson 			}
2898dfc11533SChris Williamson 			zcp_check(zhp, prop, val, NULL);
2899e7437265Sahrens 			break;
2900e7437265Sahrens 
290191ebeef5Sahrens 		case PROP_TYPE_STRING:
29029c3fd121SMatthew Ahrens 			str = getprop_string(zhp, prop, &source);
29039c3fd121SMatthew Ahrens 			if (str == NULL)
29049c3fd121SMatthew Ahrens 				return (-1);
2905dfc11533SChris Williamson 
29069c3fd121SMatthew Ahrens 			(void) strlcpy(propbuf, str, proplen);
29075b8cbb8eSToomas Soome 			zcp_check(zhp, prop, 0, str);
2908e7437265Sahrens 			break;
2909e7437265Sahrens 
291091ebeef5Sahrens 		case PROP_TYPE_INDEX:
2911fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2912fe192f76Sahrens 			    &source, &val) != 0)
2913fe192f76Sahrens 				return (-1);
2914fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2915e7437265Sahrens 				return (-1);
2916dfc11533SChris Williamson 
2917e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
29185b8cbb8eSToomas Soome 			zcp_check(zhp, prop, 0, strval);
2919e7437265Sahrens 			break;
2920e7437265Sahrens 
2921e7437265Sahrens 		default:
292299653d4eSeschrock 			abort();
2923fa9e4066Sahrens 		}
2924e7437265Sahrens 	}
2925fa9e4066Sahrens 
2926fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2927fa9e4066Sahrens 
2928fa9e4066Sahrens 	return (0);
2929fa9e4066Sahrens }
2930fa9e4066Sahrens 
2931fa9e4066Sahrens /*
2932fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2933fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2934fa9e4066Sahrens  * hard-coded property types.
2935fa9e4066Sahrens  */
2936fa9e4066Sahrens uint64_t
2937fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2938fa9e4066Sahrens {
2939fa9e4066Sahrens 	char *source;
294099653d4eSeschrock 	uint64_t val;
2941fa9e4066Sahrens 
29423cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
294399653d4eSeschrock 
294499653d4eSeschrock 	return (val);
2945fa9e4066Sahrens }
2946fa9e4066Sahrens 
29477b97dc1aSrm160521 int
29487b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
29497b97dc1aSrm160521 {
29507b97dc1aSrm160521 	char buf[64];
29517b97dc1aSrm160521 
295214843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
29537b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
29547b97dc1aSrm160521 }
29557b97dc1aSrm160521 
2956fa9e4066Sahrens /*
2957fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2958fa9e4066Sahrens  */
2959fa9e4066Sahrens int
2960fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2961990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2962fa9e4066Sahrens {
2963fa9e4066Sahrens 	char *source;
2964fa9e4066Sahrens 
2965fa9e4066Sahrens 	/*
2966fa9e4066Sahrens 	 * Check to see if this property applies to our object
2967fa9e4066Sahrens 	 */
2968e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2969ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
297099653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
297199653d4eSeschrock 		    zfs_prop_to_name(prop)));
2972e45ce728Sahrens 	}
2973fa9e4066Sahrens 
2974fa9e4066Sahrens 	if (src)
2975990b4856Slling 		*src = ZPROP_SRC_NONE;
2976fa9e4066Sahrens 
297799653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
297899653d4eSeschrock 		return (-1);
2979fa9e4066Sahrens 
2980fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2981fa9e4066Sahrens 
2982fa9e4066Sahrens 	return (0);
2983fa9e4066Sahrens }
2984fa9e4066Sahrens 
298514843421SMatthew Ahrens static int
298614843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
298714843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
298814843421SMatthew Ahrens {
298914843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
299014843421SMatthew Ahrens 	idmap_stat status;
299114843421SMatthew Ahrens 	int err = EINVAL;
299214843421SMatthew Ahrens 
29931fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
299414843421SMatthew Ahrens 		goto out;
299514843421SMatthew Ahrens 
299614843421SMatthew Ahrens 	if (isuser) {
299714843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
299814843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
299914843421SMatthew Ahrens 	} else {
300014843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
300114843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
300214843421SMatthew Ahrens 	}
300314843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
300414843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
300514843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
300614843421SMatthew Ahrens 		err = 0;
300714843421SMatthew Ahrens 	else
300814843421SMatthew Ahrens 		err = EINVAL;
300914843421SMatthew Ahrens out:
301014843421SMatthew Ahrens 	if (get_hdl)
301114843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
301214843421SMatthew Ahrens 	return (err);
301314843421SMatthew Ahrens }
301414843421SMatthew Ahrens 
301514843421SMatthew Ahrens /*
301614843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
301714843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
301814843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3019*f67950b2SNasf-Fan  * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3020*f67950b2SNasf-Fan  * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3021*f67950b2SNasf-Fan  * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3022*f67950b2SNasf-Fan  * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
302314843421SMatthew Ahrens  */
302414843421SMatthew Ahrens static int
302514843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
302614843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
302714843421SMatthew Ahrens {
302814843421SMatthew Ahrens 	zfs_userquota_prop_t type;
3029*f67950b2SNasf-Fan 	char *cp;
303014843421SMatthew Ahrens 	boolean_t isuser;
3031*f67950b2SNasf-Fan 	boolean_t isgroup;
3032*f67950b2SNasf-Fan 	boolean_t isproject;
3033*f67950b2SNasf-Fan 	struct passwd *pw;
3034*f67950b2SNasf-Fan 	struct group *gr;
303514843421SMatthew Ahrens 
303614843421SMatthew Ahrens 	domain[0] = '\0';
3037*f67950b2SNasf-Fan 
3038*f67950b2SNasf-Fan 	/* Figure out the property type ({user|group|project}{quota|space}) */
303914843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
304014843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
304114843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
304214843421SMatthew Ahrens 			break;
304314843421SMatthew Ahrens 	}
304414843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
304514843421SMatthew Ahrens 		return (EINVAL);
304614843421SMatthew Ahrens 	*typep = type;
304714843421SMatthew Ahrens 
3048*f67950b2SNasf-Fan 	isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3049*f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJQUOTA ||
3050*f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJUSED);
3051*f67950b2SNasf-Fan 	isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3052*f67950b2SNasf-Fan 	    type == ZFS_PROP_GROUPOBJQUOTA ||
3053*f67950b2SNasf-Fan 	    type == ZFS_PROP_GROUPOBJUSED);
3054*f67950b2SNasf-Fan 	isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3055*f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3056*f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED);
305714843421SMatthew Ahrens 
305814843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
305914843421SMatthew Ahrens 
3060*f67950b2SNasf-Fan 	if (isuser && (pw = getpwnam(cp)) != NULL) {
3061*f67950b2SNasf-Fan 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3062*f67950b2SNasf-Fan 			return (ENOENT);
3063*f67950b2SNasf-Fan 		*ridp = pw->pw_uid;
3064*f67950b2SNasf-Fan 	} else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3065*f67950b2SNasf-Fan 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3066*f67950b2SNasf-Fan 			return (ENOENT);
3067*f67950b2SNasf-Fan 		*ridp = gr->gr_gid;
3068*f67950b2SNasf-Fan 	} else if (!isproject && strchr(cp, '@')) {
306914843421SMatthew Ahrens 		/*
307014843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
30713b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
307214843421SMatthew Ahrens 		 */
3073*f67950b2SNasf-Fan 		directory_error_t e;
3074*f67950b2SNasf-Fan 		char *numericsid = NULL;
3075*f67950b2SNasf-Fan 		char *end;
30761ed6b69aSGordon Ross 
307714843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
307814843421SMatthew Ahrens 			return (ENOENT);
30793b12c289SMatthew Ahrens 		if (isuser) {
3080*f67950b2SNasf-Fan 			e = directory_sid_from_user_name(NULL,
3081*f67950b2SNasf-Fan 			    cp, &numericsid);
30823b12c289SMatthew Ahrens 		} else {
3083*f67950b2SNasf-Fan 			e = directory_sid_from_group_name(NULL,
3084*f67950b2SNasf-Fan 			    cp, &numericsid);
30853b12c289SMatthew Ahrens 		}
3086*f67950b2SNasf-Fan 		if (e != NULL) {
3087*f67950b2SNasf-Fan 			directory_error_free(e);
308814843421SMatthew Ahrens 			return (ENOENT);
30893b12c289SMatthew Ahrens 		}
30903b12c289SMatthew Ahrens 		if (numericsid == NULL)
309114843421SMatthew Ahrens 			return (ENOENT);
30923b12c289SMatthew Ahrens 		cp = numericsid;
30933b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
309414843421SMatthew Ahrens 		cp = strrchr(domain, '-');
309514843421SMatthew Ahrens 		*cp = '\0';
309614843421SMatthew Ahrens 		cp++;
3097*f67950b2SNasf-Fan 
3098*f67950b2SNasf-Fan 		errno = 0;
309914843421SMatthew Ahrens 		*ridp = strtoull(cp, &end, 10);
31003b12c289SMatthew Ahrens 		free(numericsid);
3101*f67950b2SNasf-Fan 
3102777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
310314843421SMatthew Ahrens 			return (EINVAL);
310414843421SMatthew Ahrens 	} else {
3105*f67950b2SNasf-Fan 		/* It's a user/group/project ID (eg "12345"). */
3106*f67950b2SNasf-Fan 		char *end;
310714843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
3108*f67950b2SNasf-Fan 		if (*end != '\0')
3109*f67950b2SNasf-Fan 			return (EINVAL);
3110*f67950b2SNasf-Fan 		if (id > MAXUID && !isproject) {
3111*f67950b2SNasf-Fan 			/* It's an ephemeral ID. */
311214843421SMatthew Ahrens 			idmap_rid_t rid;
311314843421SMatthew Ahrens 			char *mapdomain;
311414843421SMatthew Ahrens 
311514843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
311614843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
311714843421SMatthew Ahrens 				return (ENOENT);
31183b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
311914843421SMatthew Ahrens 			*ridp = rid;
312014843421SMatthew Ahrens 		} else {
312114843421SMatthew Ahrens 			*ridp = id;
312214843421SMatthew Ahrens 		}
312314843421SMatthew Ahrens 	}
312414843421SMatthew Ahrens 
312514843421SMatthew Ahrens 	return (0);
312614843421SMatthew Ahrens }
312714843421SMatthew Ahrens 
3128edea4b55SLin Ling static int
3129edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3130edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
313114843421SMatthew Ahrens {
313214843421SMatthew Ahrens 	int err;
313314843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
313414843421SMatthew Ahrens 
313519b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
313614843421SMatthew Ahrens 
313714843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
313814843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3139edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3140edea4b55SLin Ling 	zc.zc_objset_type = *typep;
314114843421SMatthew Ahrens 	if (err)
314214843421SMatthew Ahrens 		return (err);
314314843421SMatthew Ahrens 
314414843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
314514843421SMatthew Ahrens 	if (err)
314614843421SMatthew Ahrens 		return (err);
314714843421SMatthew Ahrens 
3148edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
3149edea4b55SLin Ling 	return (0);
3150edea4b55SLin Ling }
3151edea4b55SLin Ling 
3152edea4b55SLin Ling int
3153edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3154edea4b55SLin Ling     uint64_t *propvalue)
3155edea4b55SLin Ling {
3156edea4b55SLin Ling 	zfs_userquota_prop_t type;
3157edea4b55SLin Ling 
3158edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3159edea4b55SLin Ling 	    &type));
3160edea4b55SLin Ling }
3161edea4b55SLin Ling 
3162edea4b55SLin Ling int
3163edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3164edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
3165edea4b55SLin Ling {
3166edea4b55SLin Ling 	int err;
3167edea4b55SLin Ling 	uint64_t propvalue;
3168edea4b55SLin Ling 	zfs_userquota_prop_t type;
3169edea4b55SLin Ling 
3170edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3171edea4b55SLin Ling 	    &type);
3172edea4b55SLin Ling 
3173edea4b55SLin Ling 	if (err)
3174edea4b55SLin Ling 		return (err);
3175edea4b55SLin Ling 
317614843421SMatthew Ahrens 	if (literal) {
3177edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3178edea4b55SLin Ling 	} else if (propvalue == 0 &&
3179*f67950b2SNasf-Fan 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3180*f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3181*f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTQUOTA || ZFS_PROP_PROJECTOBJQUOTA)) {
318214843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
3183*f67950b2SNasf-Fan 	} else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3184*f67950b2SNasf-Fan 	    type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3185*f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3186*f67950b2SNasf-Fan 		zfs_nicenum(propvalue, propbuf, proplen);
318714843421SMatthew Ahrens 	} else {
3188edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
318914843421SMatthew Ahrens 	}
319014843421SMatthew Ahrens 	return (0);
319114843421SMatthew Ahrens }
319214843421SMatthew Ahrens 
319319b94df9SMatthew Ahrens int
319419b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
319519b94df9SMatthew Ahrens     uint64_t *propvalue)
319619b94df9SMatthew Ahrens {
319719b94df9SMatthew Ahrens 	int err;
319819b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
319919b94df9SMatthew Ahrens 	const char *snapname;
320019b94df9SMatthew Ahrens 
320119b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
320219b94df9SMatthew Ahrens 
320319b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
320419b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
320519b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
320619b94df9SMatthew Ahrens 	} else {
320719b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
320819b94df9SMatthew Ahrens 		char *cp;
320919b94df9SMatthew Ahrens 
321019b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
321119b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
321219b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
321319b94df9SMatthew Ahrens 		if (cp != NULL)
321419b94df9SMatthew Ahrens 			*cp = '\0';
321519b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
321619b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
321719b94df9SMatthew Ahrens 	}
321819b94df9SMatthew Ahrens 
321919b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
322019b94df9SMatthew Ahrens 	if (err)
322119b94df9SMatthew Ahrens 		return (err);
322219b94df9SMatthew Ahrens 
322319b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
322419b94df9SMatthew Ahrens 	return (0);
322519b94df9SMatthew Ahrens }
322619b94df9SMatthew Ahrens 
322719b94df9SMatthew Ahrens int
322819b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
322919b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
323019b94df9SMatthew Ahrens {
323119b94df9SMatthew Ahrens 	int err;
323219b94df9SMatthew Ahrens 	uint64_t propvalue;
323319b94df9SMatthew Ahrens 
323419b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
323519b94df9SMatthew Ahrens 
323619b94df9SMatthew Ahrens 	if (err)
323719b94df9SMatthew Ahrens 		return (err);
323819b94df9SMatthew Ahrens 
323919b94df9SMatthew Ahrens 	if (literal) {
324019b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
324119b94df9SMatthew Ahrens 	} else {
324219b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
324319b94df9SMatthew Ahrens 	}
324419b94df9SMatthew Ahrens 	return (0);
324519b94df9SMatthew Ahrens }
324619b94df9SMatthew Ahrens 
3247fa9e4066Sahrens /*
3248fa9e4066Sahrens  * Returns the name of the given zfs handle.
3249fa9e4066Sahrens  */
3250fa9e4066Sahrens const char *
3251fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
3252fa9e4066Sahrens {
3253fa9e4066Sahrens 	return (zhp->zfs_name);
3254fa9e4066Sahrens }
3255fa9e4066Sahrens 
3256fa9e4066Sahrens /*
32578808ac5dSYuri Pankov  * Returns the name of the parent pool for the given zfs handle.
32588808ac5dSYuri Pankov  */
32598808ac5dSYuri Pankov const char *
32608808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp)
32618808ac5dSYuri Pankov {
32628808ac5dSYuri Pankov 	return (zhp->zpool_hdl->zpool_name);
32638808ac5dSYuri Pankov }
32648808ac5dSYuri Pankov 
32658808ac5dSYuri Pankov /*
3266fa9e4066Sahrens  * Returns the type of the given zfs handle.
3267fa9e4066Sahrens  */
3268fa9e4066Sahrens zfs_type_t
3269fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
3270fa9e4066Sahrens {
3271fa9e4066Sahrens 	return (zhp->zfs_type);
3272fa9e4066Sahrens }
3273fa9e4066Sahrens 
32747f7322feSeschrock /*
3275d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
3276d41c4376SMark J Musante  *
3277d41c4376SMark J Musante  * Needs to handle these cases:
3278d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3279d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3280d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
3281d41c4376SMark J Musante  */
3282d41c4376SMark J Musante static boolean_t
3283d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
3284d41c4376SMark J Musante {
3285d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
3286d41c4376SMark J Musante 
3287d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
3288d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
3289d41c4376SMark J Musante 		return (B_FALSE);
3290d41c4376SMark J Musante 
3291d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
3292d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3293d41c4376SMark J Musante }
3294d41c4376SMark J Musante 
3295d41c4376SMark J Musante /*
3296fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
329719b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
329819b94df9SMatthew Ahrens  * pool).
3299fa9e4066Sahrens  */
3300fa9e4066Sahrens static int
3301fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3302fa9e4066Sahrens {
330319b94df9SMatthew Ahrens 	char *slashp;
3304fa9e4066Sahrens 
330519b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
330619b94df9SMatthew Ahrens 
330719b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
3308fa9e4066Sahrens 		return (-1);
330919b94df9SMatthew Ahrens 	*slashp = '\0';
3310fa9e4066Sahrens 
3311fa9e4066Sahrens 	return (0);
3312fa9e4066Sahrens }
3313fa9e4066Sahrens 
3314eb633035STom Caputi int
3315eb633035STom Caputi zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3316eb633035STom Caputi {
3317eb633035STom Caputi 	return (parent_name(zfs_get_name(zhp), buf, buflen));
3318eb633035STom Caputi }
3319eb633035STom Caputi 
3320fa9e4066Sahrens /*
33217f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
33227f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
33237f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
33247f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
33257f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
33267f1f55eaSvb160487  * new datasets.
3327fa9e4066Sahrens  */
3328fa9e4066Sahrens static int
33297f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
33307f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
3331fa9e4066Sahrens {
3332fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
33339adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3334fa9e4066Sahrens 	char *slash;
33357f7322feSeschrock 	zfs_handle_t *zhp;
333699653d4eSeschrock 	char errbuf[1024];
3337d41c4376SMark J Musante 	uint64_t is_zoned;
333899653d4eSeschrock 
3339deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
3340deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3341fa9e4066Sahrens 
3342fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
3343fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
334499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
334599653d4eSeschrock 		    "missing dataset name"));
334699653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3347fa9e4066Sahrens 	}
3348fa9e4066Sahrens 
3349fa9e4066Sahrens 	/* check to see if the pool exists */
3350fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
3351fa9e4066Sahrens 		slash = parent + strlen(parent);
33528ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
3353fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
335499653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3355fa9e4066Sahrens 	    errno == ENOENT) {
335699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
335799653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
335899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3359fa9e4066Sahrens 	}
3360fa9e4066Sahrens 
3361fa9e4066Sahrens 	/* check to see if the parent dataset exists */
33627f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
33637f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
33647f1f55eaSvb160487 			/*
33657f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
33667f1f55eaSvb160487 			 */
33677f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
33687f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33697f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
33707f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
33717f1f55eaSvb160487 			}
33727f1f55eaSvb160487 		} else if (errno == ENOENT) {
337399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
337499653d4eSeschrock 			    "parent does not exist"));
337599653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
33767f1f55eaSvb160487 		} else
337799653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3378fa9e4066Sahrens 	}
3379fa9e4066Sahrens 
3380d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3381d41c4376SMark J Musante 	if (zoned != NULL)
3382d41c4376SMark J Musante 		*zoned = is_zoned;
3383d41c4376SMark J Musante 
3384fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
3385d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
338699653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
33877f7322feSeschrock 		zfs_close(zhp);
3388fa9e4066Sahrens 		return (-1);
3389fa9e4066Sahrens 	}
3390fa9e4066Sahrens 
3391fa9e4066Sahrens 	/* make sure parent is a filesystem */
33927f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
339399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
339499653d4eSeschrock 		    "parent is not a filesystem"));
339599653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
33967f7322feSeschrock 		zfs_close(zhp);
3397fa9e4066Sahrens 		return (-1);
3398fa9e4066Sahrens 	}
3399fa9e4066Sahrens 
34007f7322feSeschrock 	zfs_close(zhp);
34017f1f55eaSvb160487 	if (prefixlen != NULL)
34027f1f55eaSvb160487 		*prefixlen = strlen(parent);
34037f1f55eaSvb160487 	return (0);
34047f1f55eaSvb160487 }
34057f1f55eaSvb160487 
34067f1f55eaSvb160487 /*
34077f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
34087f1f55eaSvb160487  */
34097f1f55eaSvb160487 boolean_t
34107f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
34117f1f55eaSvb160487 {
34127f1f55eaSvb160487 	zfs_handle_t *zhp;
34137f1f55eaSvb160487 
3414f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
34157f1f55eaSvb160487 		return (B_FALSE);
34167f1f55eaSvb160487 
34177f1f55eaSvb160487 	/*
34187f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
34197f1f55eaSvb160487 	 */
34207f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
34217f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
34227f1f55eaSvb160487 
34237f1f55eaSvb160487 		zfs_close(zhp);
34247f1f55eaSvb160487 		if (types & ds_type)
34257f1f55eaSvb160487 			return (B_TRUE);
34267f1f55eaSvb160487 	}
34277f1f55eaSvb160487 	return (B_FALSE);
34287f1f55eaSvb160487 }
34297f1f55eaSvb160487 
34307f1f55eaSvb160487 /*
34313cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
34323cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
34333cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
34343cb34c60Sahrens  */
34353cb34c60Sahrens int
34363cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
34373cb34c60Sahrens {
34383cb34c60Sahrens 	zfs_handle_t *h;
34393cb34c60Sahrens 	char *cp;
34403cb34c60Sahrens 	const char *opname;
34413cb34c60Sahrens 
34423cb34c60Sahrens 	/* make sure prefix exists */
34433cb34c60Sahrens 	cp = target + prefixlen;
34443cb34c60Sahrens 	if (*cp != '/') {
34453cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
34463cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34473cb34c60Sahrens 	} else {
34483cb34c60Sahrens 		*cp = '\0';
34493cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34503cb34c60Sahrens 		*cp = '/';
34513cb34c60Sahrens 	}
34523cb34c60Sahrens 	if (h == NULL)
34533cb34c60Sahrens 		return (-1);
34543cb34c60Sahrens 	zfs_close(h);
34553cb34c60Sahrens 
34563cb34c60Sahrens 	/*
34573cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
34583cb34c60Sahrens 	 * up to the prefixlen-long one.
34593cb34c60Sahrens 	 */
34603cb34c60Sahrens 	for (cp = target + prefixlen + 1;
346188f61deeSIgor Kozhukhov 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
34623cb34c60Sahrens 
34633cb34c60Sahrens 		*cp = '\0';
34643cb34c60Sahrens 
34653cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
34663cb34c60Sahrens 		if (h) {
34673cb34c60Sahrens 			/* it already exists, nothing to do here */
34683cb34c60Sahrens 			zfs_close(h);
34693cb34c60Sahrens 			continue;
34703cb34c60Sahrens 		}
34713cb34c60Sahrens 
34723cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
34733cb34c60Sahrens 		    NULL) != 0) {
34743cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
34753cb34c60Sahrens 			goto ancestorerr;
34763cb34c60Sahrens 		}
34773cb34c60Sahrens 
34783cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34793cb34c60Sahrens 		if (h == NULL) {
34803cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
34813cb34c60Sahrens 			goto ancestorerr;
34823cb34c60Sahrens 		}
34833cb34c60Sahrens 
34843cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
34853cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
34863cb34c60Sahrens 			goto ancestorerr;
34873cb34c60Sahrens 		}
34883cb34c60Sahrens 
34893cb34c60Sahrens 		if (zfs_share(h) != 0) {
34903cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
34913cb34c60Sahrens 			goto ancestorerr;
34923cb34c60Sahrens 		}
34933cb34c60Sahrens 
34943cb34c60Sahrens 		zfs_close(h);
34953cb34c60Sahrens 	}
34963cb34c60Sahrens 
34973cb34c60Sahrens 	return (0);
34983cb34c60Sahrens 
34993cb34c60Sahrens ancestorerr:
35003cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35013cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
35023cb34c60Sahrens 	return (-1);
35033cb34c60Sahrens }
35043cb34c60Sahrens 
35053cb34c60Sahrens /*
35067f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
35077f1f55eaSvb160487  */
35087f1f55eaSvb160487 int
35097f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
35107f1f55eaSvb160487 {
35117f1f55eaSvb160487 	int prefix;
35127f1f55eaSvb160487 	char *path_copy;
35135ac95da7SSerapheim Dimitropoulos 	char errbuf[1024];
3514f83b46baSPaul Dagnelie 	int rc = 0;
35157f1f55eaSvb160487 
35165ac95da7SSerapheim Dimitropoulos 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35175ac95da7SSerapheim Dimitropoulos 	    "cannot create '%s'"), path);
35185ac95da7SSerapheim Dimitropoulos 
35195ac95da7SSerapheim Dimitropoulos 	/*
35205ac95da7SSerapheim Dimitropoulos 	 * Check that we are not passing the nesting limit
35215ac95da7SSerapheim Dimitropoulos 	 * before we start creating any ancestors.
35225ac95da7SSerapheim Dimitropoulos 	 */
35235ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(path) != 0) {
35245ac95da7SSerapheim Dimitropoulos 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35255ac95da7SSerapheim Dimitropoulos 		    "maximum name nesting depth exceeded"));
35265ac95da7SSerapheim Dimitropoulos 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35275ac95da7SSerapheim Dimitropoulos 	}
35285ac95da7SSerapheim Dimitropoulos 
3529d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
35307f1f55eaSvb160487 		return (-1);
35317f1f55eaSvb160487 
35327f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
35337f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
35347f1f55eaSvb160487 		free(path_copy);
35357f1f55eaSvb160487 	}
35367f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
35377f1f55eaSvb160487 		return (-1);
35387f1f55eaSvb160487 
3539fa9e4066Sahrens 	return (0);
3540fa9e4066Sahrens }
3541fa9e4066Sahrens 
3542fa9e4066Sahrens /*
3543e9dbad6fSeschrock  * Create a new filesystem or volume.
3544fa9e4066Sahrens  */
3545fa9e4066Sahrens int
354699653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3547e9dbad6fSeschrock     nvlist_t *props)
3548fa9e4066Sahrens {
3549fa9e4066Sahrens 	int ret;
3550fa9e4066Sahrens 	uint64_t size = 0;
3551fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3552eb633035STom Caputi 	uint8_t *wkeydata = NULL;
3553eb633035STom Caputi 	uint_t wkeylen = 0;
355499653d4eSeschrock 	char errbuf[1024];
3555eb633035STom Caputi 	char parent[MAXNAMELEN];
3556e9dbad6fSeschrock 	uint64_t zoned;
355726455f9eSAndriy Gapon 	enum lzc_dataset_type ost;
3558690031d3Sloli10K 	zpool_handle_t *zpool_handle;
355999653d4eSeschrock 
356099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
356199653d4eSeschrock 	    "cannot create '%s'"), path);
3562fa9e4066Sahrens 
3563fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3564f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
356599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3566fa9e4066Sahrens 
35675ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(path) != 0) {
35685ac95da7SSerapheim Dimitropoulos 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35695ac95da7SSerapheim Dimitropoulos 		    "maximum name nesting depth exceeded"));
35705ac95da7SSerapheim Dimitropoulos 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35715ac95da7SSerapheim Dimitropoulos 	}
35725ac95da7SSerapheim Dimitropoulos 
3573fa9e4066Sahrens 	/* validate parents exist */
35747f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3575fa9e4066Sahrens 		return (-1);
3576fa9e4066Sahrens 
3577fa9e4066Sahrens 	/*
3578fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3579fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3580fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3581fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3582fa9e4066Sahrens 	 * first try to see if the dataset exists.
3583fa9e4066Sahrens 	 */
35844445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
358599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358699653d4eSeschrock 		    "dataset already exists"));
358799653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3588fa9e4066Sahrens 	}
3589fa9e4066Sahrens 
3590fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
359126455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZVOL;
3592fa9e4066Sahrens 	else
359326455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZFS;
3594fa9e4066Sahrens 
3595e9316f76SJoe Stein 	/* open zpool handle for prop validation */
35969adfa60dSMatthew Ahrens 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3597e9316f76SJoe Stein 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3598e9316f76SJoe Stein 
3599e9316f76SJoe Stein 	/* truncate pool_path at first slash */
3600e9316f76SJoe Stein 	char *p = strchr(pool_path, '/');
3601e9316f76SJoe Stein 	if (p != NULL)
3602e9316f76SJoe Stein 		*p = '\0';
3603e9316f76SJoe Stein 
3604690031d3Sloli10K 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3605690031d3Sloli10K 		return (-1);
3606e9316f76SJoe Stein 
36070a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3608eb633035STom Caputi 	    zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3609e9316f76SJoe Stein 		zpool_close(zpool_handle);
3610e9dbad6fSeschrock 		return (-1);
3611e9316f76SJoe Stein 	}
3612e9316f76SJoe Stein 	zpool_close(zpool_handle);
3613e9dbad6fSeschrock 
3614fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
36155c5460e9Seschrock 		/*
36165c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
36175c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
36185c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
36195c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
36205c5460e9Seschrock 		 * zero.
36215c5460e9Seschrock 		 */
3622e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3623e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3624e9dbad6fSeschrock 			nvlist_free(props);
362599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3626e9dbad6fSeschrock 			    "missing volume size"));
3627e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3628fa9e4066Sahrens 		}
3629fa9e4066Sahrens 
3630e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3631e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3632e9dbad6fSeschrock 		    &blocksize)) != 0) {
3633e9dbad6fSeschrock 			if (ret == ENOENT) {
3634e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3635e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3636e9dbad6fSeschrock 			} else {
3637e9dbad6fSeschrock 				nvlist_free(props);
363899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3639e9dbad6fSeschrock 				    "missing volume block size"));
3640e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3641e9dbad6fSeschrock 			}
3642e9dbad6fSeschrock 		}
3643e9dbad6fSeschrock 
3644e9dbad6fSeschrock 		if (size == 0) {
3645e9dbad6fSeschrock 			nvlist_free(props);
3646e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3647e9dbad6fSeschrock 			    "volume size cannot be zero"));
3648e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36495c5460e9Seschrock 		}
36505c5460e9Seschrock 
36515c5460e9Seschrock 		if (size % blocksize != 0) {
3652e9dbad6fSeschrock 			nvlist_free(props);
365399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3654e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3655e9dbad6fSeschrock 			    "size"));
3656e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3657e9dbad6fSeschrock 		}
36585c5460e9Seschrock 	}
36595c5460e9Seschrock 
3660eb633035STom Caputi 	(void) parent_name(path, parent, sizeof (parent));
36616ccda740Sloli10K 	if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
36626ccda740Sloli10K 	    &wkeydata, &wkeylen) != 0) {
36634445fffbSMatthew Ahrens 		nvlist_free(props);
3664eb633035STom Caputi 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3665eb633035STom Caputi 	}
3666eb633035STom Caputi 
3667eb633035STom Caputi 	/* create the dataset */
3668eb633035STom Caputi 	ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3669eb633035STom Caputi 	nvlist_free(props);
3670eb633035STom Caputi 	if (wkeydata != NULL)
3671eb633035STom Caputi 		free(wkeydata);
3672e9dbad6fSeschrock 
3673fa9e4066Sahrens 	/* check for failure */
3674fa9e4066Sahrens 	if (ret != 0) {
3675fa9e4066Sahrens 		switch (errno) {
3676fa9e4066Sahrens 		case ENOENT:
367799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
367899653d4eSeschrock 			    "no such parent '%s'"), parent);
367999653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3680fa9e4066Sahrens 
3681fa9e4066Sahrens 		case EINVAL:
368299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3683d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
368499653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3685fa9e4066Sahrens 
368640feaa91Sahrens 		case ENOTSUP:
368740feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
368840feaa91Sahrens 			    "pool must be upgraded to set this "
368940feaa91Sahrens 			    "property or value"));
369040feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
36919fa2266dSYuri Pankov 		case ERANGE:
36929fa2266dSYuri Pankov 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36939fa2266dSYuri Pankov 			    "invalid property value(s) specified"));
36949fa2266dSYuri Pankov 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3695eb633035STom Caputi 		case EACCES:
3696eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3697eb633035STom Caputi 			    "encryption root's key is not loaded "
3698eb633035STom Caputi 			    "or provided"));
3699eb633035STom Caputi 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3700eb633035STom Caputi 
3701fa9e4066Sahrens #ifdef _ILP32
3702fa9e4066Sahrens 		case EOVERFLOW:
3703fa9e4066Sahrens 			/*
3704fa9e4066Sahrens 			 * This platform can't address a volume this big.
3705fa9e4066Sahrens 			 */
370699653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
370799653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
370899653d4eSeschrock 				    errbuf));
3709fa9e4066Sahrens #endif
371099653d4eSeschrock 			/* FALLTHROUGH */
3711fa9e4066Sahrens 		default:
371299653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3713fa9e4066Sahrens 		}
3714fa9e4066Sahrens 	}
3715fa9e4066Sahrens 
3716fa9e4066Sahrens 	return (0);
3717fa9e4066Sahrens }
3718fa9e4066Sahrens 
3719fa9e4066Sahrens /*
3720fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
372165fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
372265fec9f6SChristopher Siden  * does not exist this function does nothing.
3723fa9e4066Sahrens  */
3724fa9e4066Sahrens int
3725842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3726fa9e4066Sahrens {
3727049ba636SAndriy Gapon 	int error;
3728049ba636SAndriy Gapon 
3729049ba636SAndriy Gapon 	if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3730049ba636SAndriy Gapon 		return (EINVAL);
3731fa9e4066Sahrens 
373278f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
373378f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
373478f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
3735049ba636SAndriy Gapon 		error = lzc_destroy_bookmarks(nv, NULL);
373678f17100SMatthew Ahrens 		fnvlist_free(nv);
373778f17100SMatthew Ahrens 		if (error != 0) {
3738049ba636SAndriy Gapon 			return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
373978f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
374078f17100SMatthew Ahrens 			    zhp->zfs_name));
374178f17100SMatthew Ahrens 		}
374278f17100SMatthew Ahrens 		return (0);
374378f17100SMatthew Ahrens 	}
374478f17100SMatthew Ahrens 
3745049ba636SAndriy Gapon 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3746049ba636SAndriy Gapon 		nvlist_t *nv = fnvlist_alloc();
3747049ba636SAndriy Gapon 		fnvlist_add_boolean(nv, zhp->zfs_name);
3748049ba636SAndriy Gapon 		error = lzc_destroy_snaps(nv, defer, NULL);
3749049ba636SAndriy Gapon 		fnvlist_free(nv);
3750fa9e4066Sahrens 	} else {
3751049ba636SAndriy Gapon 		error = lzc_destroy(zhp->zfs_name);
3752fa9e4066Sahrens 	}
3753fa9e4066Sahrens 
3754049ba636SAndriy Gapon 	if (error != 0 && error != ENOENT) {
3755ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
375699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
375799653d4eSeschrock 		    zhp->zfs_name));
37581d452cf5Sahrens 	}
3759fa9e4066Sahrens 
3760fa9e4066Sahrens 	remove_mountpoint(zhp);
3761fa9e4066Sahrens 
3762fa9e4066Sahrens 	return (0);
3763fa9e4066Sahrens }
3764fa9e4066Sahrens 
37651d452cf5Sahrens struct destroydata {
376619b94df9SMatthew Ahrens 	nvlist_t *nvl;
376719b94df9SMatthew Ahrens 	const char *snapname;
37681d452cf5Sahrens };
37691d452cf5Sahrens 
37701d452cf5Sahrens static int
3771681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
37721d452cf5Sahrens {
37731d452cf5Sahrens 	struct destroydata *dd = arg;
37749adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3775681d9761SEric Taylor 	int rv = 0;
37761d452cf5Sahrens 
377719b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
377819b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
37791d452cf5Sahrens 
3780a7a845e4SSteven Hartland 	if (lzc_exists(name))
378119b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
37821d452cf5Sahrens 
378319b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
37843ccfa83cSahrens 	zfs_close(zhp);
37853ccfa83cSahrens 	return (rv);
37861d452cf5Sahrens }
37871d452cf5Sahrens 
37881d452cf5Sahrens /*
37891d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
37901d452cf5Sahrens  */
37911d452cf5Sahrens int
3792842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
37931d452cf5Sahrens {
37941d452cf5Sahrens 	int ret;
37951d452cf5Sahrens 	struct destroydata dd = { 0 };
37961d452cf5Sahrens 
37971d452cf5Sahrens 	dd.snapname = snapname;
379819b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
379919b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
38001d452cf5Sahrens 
3801a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
380219b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
38031d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
380419b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
380519b94df9SMatthew Ahrens 	} else {
38063b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
380719b94df9SMatthew Ahrens 	}
380819b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
380919b94df9SMatthew Ahrens 	return (ret);
3810e5351341SMatthew Ahrens }
3811e5351341SMatthew Ahrens 
381219b94df9SMatthew Ahrens /*
38133b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
381419b94df9SMatthew Ahrens  */
381519b94df9SMatthew Ahrens int
38163b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
381719b94df9SMatthew Ahrens {
381819b94df9SMatthew Ahrens 	int ret;
38194cde22c2SChris Williamson 	nvlist_t *errlist = NULL;
382019b94df9SMatthew Ahrens 
38214445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
38221d452cf5Sahrens 
38234cde22c2SChris Williamson 	if (ret == 0) {
38244cde22c2SChris Williamson 		nvlist_free(errlist);
38253b2aab18SMatthew Ahrens 		return (0);
38264cde22c2SChris Williamson 	}
38273b2aab18SMatthew Ahrens 
3828a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
38293b2aab18SMatthew Ahrens 		char errbuf[1024];
38303b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
38313b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
38323b2aab18SMatthew Ahrens 
38333b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
38343b2aab18SMatthew Ahrens 	}
38354445fffbSMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
38364445fffbSMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
38371d452cf5Sahrens 		char errbuf[1024];
38384445fffbSMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
38394445fffbSMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
38404445fffbSMatthew Ahrens 		    nvpair_name(pair));
38411d452cf5Sahrens 
38424445fffbSMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
38431d452cf5Sahrens 		case EEXIST:
38443b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
38453b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
38463b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
38474445fffbSMatthew Ahrens 			break;
38481d452cf5Sahrens 		default:
38493b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
38504445fffbSMatthew Ahrens 			break;
38514445fffbSMatthew Ahrens 		}
38521d452cf5Sahrens 	}
38531d452cf5Sahrens 
38544cde22c2SChris Williamson 	nvlist_free(errlist);
38554445fffbSMatthew Ahrens 	return (ret);
38561d452cf5Sahrens }
38571d452cf5Sahrens 
3858fa9e4066Sahrens /*
3859fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3860fa9e4066Sahrens  */
3861fa9e4066Sahrens int
3862e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3863fa9e4066Sahrens {
38649adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3865fa9e4066Sahrens 	int ret;
386699653d4eSeschrock 	char errbuf[1024];
386799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3868e9dbad6fSeschrock 	uint64_t zoned;
3869fa9e4066Sahrens 
3870fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3871fa9e4066Sahrens 
387299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
387399653d4eSeschrock 	    "cannot create '%s'"), target);
387499653d4eSeschrock 
387519b94df9SMatthew Ahrens 	/* validate the target/clone name */
3876f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
387799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3878fa9e4066Sahrens 
3879fa9e4066Sahrens 	/* validate parents exist */
38807f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3881fa9e4066Sahrens 		return (-1);
3882fa9e4066Sahrens 
3883fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3884fa9e4066Sahrens 
3885fa9e4066Sahrens 	/* do the clone */
3886e9dbad6fSeschrock 
3887e9dbad6fSeschrock 	if (props) {
38884445fffbSMatthew Ahrens 		zfs_type_t type;
38891c10ae76SMike Gerdts 
38904445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
38914445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
38924445fffbSMatthew Ahrens 		} else {
38934445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
38944445fffbSMatthew Ahrens 		}
38950a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3896eb633035STom Caputi 		    zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
3897e9dbad6fSeschrock 			return (-1);
38981c10ae76SMike Gerdts 		if (zfs_fix_auto_resv(zhp, props) == -1) {
38991c10ae76SMike Gerdts 			nvlist_free(props);
39001c10ae76SMike Gerdts 			return (-1);
39011c10ae76SMike Gerdts 		}
3902e9dbad6fSeschrock 	}
3903e9dbad6fSeschrock 
3904eb633035STom Caputi 	if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
3905eb633035STom Caputi 		nvlist_free(props);
3906eb633035STom Caputi 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3907eb633035STom Caputi 	}
3908eb633035STom Caputi 
39094445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
3910e9dbad6fSeschrock 	nvlist_free(props);
3911e9dbad6fSeschrock 
3912fa9e4066Sahrens 	if (ret != 0) {
3913fa9e4066Sahrens 		switch (errno) {
3914fa9e4066Sahrens 
3915fa9e4066Sahrens 		case ENOENT:
3916fa9e4066Sahrens 			/*
3917fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3918fa9e4066Sahrens 			 * above, but there may a race condition that has since
3919fa9e4066Sahrens 			 * destroyed the parent.
3920fa9e4066Sahrens 			 *
3921fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3922fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3923fa9e4066Sahrens 			 * dataset doesn't exist.
3924fa9e4066Sahrens 			 */
392599653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
392699653d4eSeschrock 			    "no such parent '%s'"), parent);
392799653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3928fa9e4066Sahrens 
392999653d4eSeschrock 		case EXDEV:
393099653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
393199653d4eSeschrock 			    "source and target pools differ"));
393299653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
393399653d4eSeschrock 			    errbuf));
393499653d4eSeschrock 
393599653d4eSeschrock 		default:
393699653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
393799653d4eSeschrock 			    errbuf));
393899653d4eSeschrock 		}
393999653d4eSeschrock 	}
394099653d4eSeschrock 
394199653d4eSeschrock 	return (ret);
394299653d4eSeschrock }
394399653d4eSeschrock 
394499653d4eSeschrock /*
394599653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
394699653d4eSeschrock  */
394799653d4eSeschrock int
394899653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
394999653d4eSeschrock {
395099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3951a4b8c9aaSAndrew Stormont 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
395299653d4eSeschrock 	int ret;
395399653d4eSeschrock 	char errbuf[1024];
395499653d4eSeschrock 
395599653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
395699653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
395799653d4eSeschrock 
395899653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
395999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396099653d4eSeschrock 		    "snapshots can not be promoted"));
396199653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
396299653d4eSeschrock 	}
396399653d4eSeschrock 
3964a4b8c9aaSAndrew Stormont 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
396599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396699653d4eSeschrock 		    "not a cloned filesystem"));
396799653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
396899653d4eSeschrock 	}
396999653d4eSeschrock 
3970add927f8Sloli10K 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3971add927f8Sloli10K 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3972add927f8Sloli10K 
3973a4b8c9aaSAndrew Stormont 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
397499653d4eSeschrock 
397599653d4eSeschrock 	if (ret != 0) {
3976a4b8c9aaSAndrew Stormont 		switch (ret) {
3977fa9e4066Sahrens 		case EEXIST:
3978681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
397999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3980681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3981a4b8c9aaSAndrew Stormont 			    snapname, zhp->zfs_dmustats.dds_origin);
398299653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3983fa9e4066Sahrens 
3984fa9e4066Sahrens 		default:
3985a4b8c9aaSAndrew Stormont 			return (zfs_standard_error(hdl, ret, errbuf));
3986fa9e4066Sahrens 		}
3987fa9e4066Sahrens 	}
3988e9dbad6fSeschrock 	return (ret);
39891d452cf5Sahrens }
39901d452cf5Sahrens 
39914445fffbSMatthew Ahrens typedef struct snapdata {
39924445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
39934445fffbSMatthew Ahrens 	const char *sd_snapname;
39944445fffbSMatthew Ahrens } snapdata_t;
39954445fffbSMatthew Ahrens 
39964445fffbSMatthew Ahrens static int
39974445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
39984445fffbSMatthew Ahrens {
39994445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
40009adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
40014445fffbSMatthew Ahrens 	int rv = 0;
40024445fffbSMatthew Ahrens 
4003ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
40044445fffbSMatthew Ahrens 		(void) snprintf(name, sizeof (name),
40054445fffbSMatthew Ahrens 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
40064445fffbSMatthew Ahrens 
40074445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd->sd_nvl, name);
40084445fffbSMatthew Ahrens 
40094445fffbSMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4010ca48f36fSKeith M Wesolowski 	}
40114445fffbSMatthew Ahrens 	zfs_close(zhp);
4012ca48f36fSKeith M Wesolowski 
40134445fffbSMatthew Ahrens 	return (rv);
40144445fffbSMatthew Ahrens }
40154445fffbSMatthew Ahrens 
40165cabbc6bSPrashanth Sreenivasa int
40175cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
40185cabbc6bSPrashanth Sreenivasa {
40195cabbc6bSPrashanth Sreenivasa 	int err;
40205cabbc6bSPrashanth Sreenivasa 	char errbuf[1024];
40215cabbc6bSPrashanth Sreenivasa 
40225cabbc6bSPrashanth Sreenivasa 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40230b2e8253Sloli10K 	    "cannot remap dataset '%s'"), fs);
40245cabbc6bSPrashanth Sreenivasa 
40255cabbc6bSPrashanth Sreenivasa 	err = lzc_remap(fs);
40265cabbc6bSPrashanth Sreenivasa 
40275cabbc6bSPrashanth Sreenivasa 	if (err != 0) {
40280b2e8253Sloli10K 		switch (err) {
40290b2e8253Sloli10K 		case ENOTSUP:
40300b2e8253Sloli10K 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40310b2e8253Sloli10K 			    "pool must be upgraded"));
40320b2e8253Sloli10K 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
40330b2e8253Sloli10K 			break;
40340b2e8253Sloli10K 		case EINVAL:
40350b2e8253Sloli10K 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
40360b2e8253Sloli10K 			break;
40370b2e8253Sloli10K 		default:
40385cabbc6bSPrashanth Sreenivasa 			(void) zfs_standard_error(hdl, err, errbuf);
40390b2e8253Sloli10K 			break;
40400b2e8253Sloli10K 		}
40415cabbc6bSPrashanth Sreenivasa 	}
40425cabbc6bSPrashanth Sreenivasa 
40435cabbc6bSPrashanth Sreenivasa 	return (err);
40445cabbc6bSPrashanth Sreenivasa }
40455cabbc6bSPrashanth Sreenivasa 
4046fa9e4066Sahrens /*
40474445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
40484445fffbSMatthew Ahrens  * created.
4049fa9e4066Sahrens  */
4050fa9e4066Sahrens int
40514445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
40524445fffbSMatthew Ahrens {
40534445fffbSMatthew Ahrens 	int ret;
40544445fffbSMatthew Ahrens 	char errbuf[1024];
40554445fffbSMatthew Ahrens 	nvpair_t *elem;
40564445fffbSMatthew Ahrens 	nvlist_t *errors;
40574445fffbSMatthew Ahrens 
40584445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40594445fffbSMatthew Ahrens 	    "cannot create snapshots "));
40604445fffbSMatthew Ahrens 
40614445fffbSMatthew Ahrens 	elem = NULL;
40624445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
40634445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
40644445fffbSMatthew Ahrens 
40654445fffbSMatthew Ahrens 		/* validate the target name */
40664445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
40674445fffbSMatthew Ahrens 		    B_TRUE)) {
40684445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
40694445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
40704445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
40714445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
40724445fffbSMatthew Ahrens 		}
40734445fffbSMatthew Ahrens 	}
40744445fffbSMatthew Ahrens 
4075e9316f76SJoe Stein 	/*
4076e9316f76SJoe Stein 	 * get pool handle for prop validation. assumes all snaps are in the
4077e9316f76SJoe Stein 	 * same pool, as does lzc_snapshot (below).
4078e9316f76SJoe Stein 	 */
40799adfa60dSMatthew Ahrens 	char pool[ZFS_MAX_DATASET_NAME_LEN];
4080e9316f76SJoe Stein 	elem = nvlist_next_nvpair(snaps, NULL);
4081e9316f76SJoe Stein 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4082e9316f76SJoe Stein 	pool[strcspn(pool, "/@")] = '\0';
4083e9316f76SJoe Stein 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4084e9316f76SJoe Stein 
40854445fffbSMatthew Ahrens 	if (props != NULL &&
40864445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4087eb633035STom Caputi 	    props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4088e9316f76SJoe Stein 		zpool_close(zpool_hdl);
40894445fffbSMatthew Ahrens 		return (-1);
40904445fffbSMatthew Ahrens 	}
4091e9316f76SJoe Stein 	zpool_close(zpool_hdl);
40924445fffbSMatthew Ahrens 
40934445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
40944445fffbSMatthew Ahrens 
40954445fffbSMatthew Ahrens 	if (ret != 0) {
40964445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
40974445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
40984445fffbSMatthew Ahrens 		    elem != NULL;
40994445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
41004445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
41014445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
41024445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
41034445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
41044445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
41054445fffbSMatthew Ahrens 			printed = B_TRUE;
41064445fffbSMatthew Ahrens 		}
41074445fffbSMatthew Ahrens 		if (!printed) {
41084445fffbSMatthew Ahrens 			switch (ret) {
41094445fffbSMatthew Ahrens 			case EXDEV:
41104445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41114445fffbSMatthew Ahrens 				    "multiple snapshots of same "
41124445fffbSMatthew Ahrens 				    "fs not allowed"));
41134445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
41144445fffbSMatthew Ahrens 
41154445fffbSMatthew Ahrens 				break;
41164445fffbSMatthew Ahrens 			default:
41174445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
41184445fffbSMatthew Ahrens 			}
41194445fffbSMatthew Ahrens 		}
41204445fffbSMatthew Ahrens 	}
41214445fffbSMatthew Ahrens 
41224445fffbSMatthew Ahrens 	nvlist_free(props);
41234445fffbSMatthew Ahrens 	nvlist_free(errors);
41244445fffbSMatthew Ahrens 	return (ret);
41254445fffbSMatthew Ahrens }
41264445fffbSMatthew Ahrens 
41274445fffbSMatthew Ahrens int
4128bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4129bb0ade09Sahrens     nvlist_t *props)
4130fa9e4066Sahrens {
4131fa9e4066Sahrens 	int ret;
41324445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
41339adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
41344445fffbSMatthew Ahrens 	char *cp;
41354445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
413699653d4eSeschrock 	char errbuf[1024];
4137fa9e4066Sahrens 
413899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
41394445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
414099653d4eSeschrock 
4141f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
414299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4143fa9e4066Sahrens 
41444445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
41454445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
41464445fffbSMatthew Ahrens 	*cp = '\0';
41474445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
4148bb0ade09Sahrens 
41494445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4150fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
4151fa9e4066Sahrens 		return (-1);
4152fa9e4066Sahrens 	}
4153fa9e4066Sahrens 
41544445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
41554445fffbSMatthew Ahrens 	if (recursive) {
41564445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
41574445fffbSMatthew Ahrens 	} else {
41584445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
4159681d9761SEric Taylor 	}
4160fa9e4066Sahrens 
41614445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
41624445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
4163fa9e4066Sahrens 	zfs_close(zhp);
4164fa9e4066Sahrens 	return (ret);
4165fa9e4066Sahrens }
4166fa9e4066Sahrens 
4167fa9e4066Sahrens /*
4168b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
4169b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
4170b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
4171b12a1c38Slling  * group.
4172fa9e4066Sahrens  */
4173b12a1c38Slling typedef struct rollback_data {
4174b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
4175b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
4176c391e322Sahrens 	boolean_t	cb_error;
4177c391e322Sahrens 	boolean_t	cb_force;
4178b12a1c38Slling } rollback_data_t;
4179b12a1c38Slling 
4180b12a1c38Slling static int
418178f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4182b12a1c38Slling {
4183b12a1c38Slling 	rollback_data_t *cbp = data;
4184c391e322Sahrens 	prop_changelist_t *clp;
4185c391e322Sahrens 
418678f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
41870069fd67STim Haley 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4188c391e322Sahrens 	    cbp->cb_force ? MS_FORCE: 0);
4189c391e322Sahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
4190c391e322Sahrens 		cbp->cb_error = B_TRUE;
4191c391e322Sahrens 		zfs_close(zhp);
4192c391e322Sahrens 		return (0);
4193c391e322Sahrens 	}
4194842727c2SChris Kirby 	if (zfs_destroy(zhp, B_FALSE) != 0)
4195c391e322Sahrens 		cbp->cb_error = B_TRUE;
4196c391e322Sahrens 	else
4197c391e322Sahrens 		changelist_remove(clp, zhp->zfs_name);
4198ba7b046eSahrens 	(void) changelist_postfix(clp);
4199c391e322Sahrens 	changelist_free(clp);
420078f17100SMatthew Ahrens 
420178f17100SMatthew Ahrens 	zfs_close(zhp);
420278f17100SMatthew Ahrens 	return (0);
420378f17100SMatthew Ahrens }
420478f17100SMatthew Ahrens 
420578f17100SMatthew Ahrens static int
420678f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
420778f17100SMatthew Ahrens {
420878f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
420978f17100SMatthew Ahrens 
421078f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
421178f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
421278f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
421378f17100SMatthew Ahrens 
421478f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4215b12a1c38Slling 	}
4216b12a1c38Slling 
4217b12a1c38Slling 	zfs_close(zhp);
4218b12a1c38Slling 	return (0);
4219b12a1c38Slling }
4220b12a1c38Slling 
4221b12a1c38Slling /*
42224ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
42234ccbb6e7Sahrens  * data changes since then and making it the active dataset.
42244ccbb6e7Sahrens  *
422578f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
422678f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
4227b12a1c38Slling  */
42284ccbb6e7Sahrens int
4229c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4230fa9e4066Sahrens {
42314ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
42324ccbb6e7Sahrens 	int err;
42337b97dc1aSrm160521 	boolean_t restore_resv = 0;
4234f83b46baSPaul Dagnelie 	uint64_t old_volsize = 0, new_volsize;
42357b97dc1aSrm160521 	zfs_prop_t resv_prop;
4236fa9e4066Sahrens 
4237fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4238fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
4239fa9e4066Sahrens 
42404ccbb6e7Sahrens 	/*
42412e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
42424ccbb6e7Sahrens 	 */
4243c391e322Sahrens 	cb.cb_force = force;
42444ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
42454ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
42460d8fa8f8SMartin Matuska 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
424778f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
42484ccbb6e7Sahrens 
4249c391e322Sahrens 	if (cb.cb_error)
4250c391e322Sahrens 		return (-1);
42514ccbb6e7Sahrens 
42524ccbb6e7Sahrens 	/*
42534ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
42544ccbb6e7Sahrens 	 * rollback to the given snapshot.
42554ccbb6e7Sahrens 	 */
42564ccbb6e7Sahrens 
42577b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
42587b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
42597b97dc1aSrm160521 			return (-1);
42607b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
42617b97dc1aSrm160521 		restore_resv =
42627b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
42637b97dc1aSrm160521 	}
4264fa9e4066Sahrens 
4265fa9e4066Sahrens 	/*
426677b17137SAndriy Gapon 	 * Pass both the filesystem and the wanted snapshot names,
426777b17137SAndriy Gapon 	 * we would get an error back if the snapshot is destroyed or
426877b17137SAndriy Gapon 	 * a new snapshot is created before this request is processed.
4269fa9e4066Sahrens 	 */
427077b17137SAndriy Gapon 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
427195643f75SAndriy Gapon 	if (err != 0) {
427295643f75SAndriy Gapon 		char errbuf[1024];
427395643f75SAndriy Gapon 
427495643f75SAndriy Gapon 		(void) snprintf(errbuf, sizeof (errbuf),
427595643f75SAndriy Gapon 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
427695643f75SAndriy Gapon 		    zhp->zfs_name);
427795643f75SAndriy Gapon 		switch (err) {
427895643f75SAndriy Gapon 		case EEXIST:
427977b17137SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
428095643f75SAndriy Gapon 			    "there is a snapshot or bookmark more recent "
428195643f75SAndriy Gapon 			    "than '%s'"), snap->zfs_name);
428295643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
428395643f75SAndriy Gapon 			break;
428495643f75SAndriy Gapon 		case ESRCH:
428595643f75SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
428695643f75SAndriy Gapon 			    "'%s' is not found among snapshots of '%s'"),
428795643f75SAndriy Gapon 			    snap->zfs_name, zhp->zfs_name);
428895643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
428995643f75SAndriy Gapon 			break;
429095643f75SAndriy Gapon 		case EINVAL:
429195643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
429295643f75SAndriy Gapon 			break;
429395643f75SAndriy Gapon 		default:
429495643f75SAndriy Gapon 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
429595643f75SAndriy Gapon 		}
4296b9415e83Srm160521 		return (err);
4297b9415e83Srm160521 	}
4298fa9e4066Sahrens 
42997b97dc1aSrm160521 	/*
43007b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
43017b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
43027b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
43037b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
43047b97dc1aSrm160521 	 */
4305b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4306b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
43077b97dc1aSrm160521 		if (restore_resv) {
43087b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
43097b97dc1aSrm160521 			if (old_volsize != new_volsize)
4310b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
4311b9415e83Srm160521 				    new_volsize);
43127b97dc1aSrm160521 		}
43137b97dc1aSrm160521 		zfs_close(zhp);
43147b97dc1aSrm160521 	}
43154ccbb6e7Sahrens 	return (err);
4316b12a1c38Slling }
4317b12a1c38Slling 
4318b12a1c38Slling /*
4319fa9e4066Sahrens  * Renames the given dataset.
4320fa9e4066Sahrens  */
4321fa9e4066Sahrens int
43226a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
43236a9cb0eaSEric Schrock     boolean_t force_unmount)
4324fa9e4066Sahrens {
432588f61deeSIgor Kozhukhov 	int ret = 0;
4326fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
4327fa9e4066Sahrens 	char *delim;
4328cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
4329cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
4330cdf5b4caSmmusante 	char *parentname = NULL;
43319adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
433299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
433399653d4eSeschrock 	char errbuf[1024];
4334fa9e4066Sahrens 
4335fa9e4066Sahrens 	/* if we have the same exact name, just return success */
4336fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
4337fa9e4066Sahrens 		return (0);
4338fa9e4066Sahrens 
433999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
434099653d4eSeschrock 	    "cannot rename to '%s'"), target);
434199653d4eSeschrock 
4342add927f8Sloli10K 	/* make sure source name is valid */
4343add927f8Sloli10K 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4344add927f8Sloli10K 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4345add927f8Sloli10K 
4346fa9e4066Sahrens 	/*
4347fa9e4066Sahrens 	 * Make sure the target name is valid
4348fa9e4066Sahrens 	 */
4349fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
435098579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
435198579b20Snd150628 		    *target == '@') {
435298579b20Snd150628 			/*
435398579b20Snd150628 			 * Snapshot target name is abbreviated,
435498579b20Snd150628 			 * reconstruct full dataset name
435598579b20Snd150628 			 */
435698579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
435798579b20Snd150628 			    sizeof (parent));
435898579b20Snd150628 			delim = strchr(parent, '@');
435998579b20Snd150628 			if (strchr(target, '@') == NULL)
436098579b20Snd150628 				*(++delim) = '\0';
436198579b20Snd150628 			else
436298579b20Snd150628 				*delim = '\0';
436398579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
436498579b20Snd150628 			target = parent;
436598579b20Snd150628 		} else {
4366fa9e4066Sahrens 			/*
4367fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
4368fa9e4066Sahrens 			 */
436998579b20Snd150628 			delim = strchr(target, '@');
437098579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
437198579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
437299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437398579b20Snd150628 				    "snapshots must be part of same "
437498579b20Snd150628 				    "dataset"));
437598579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
437698579b20Snd150628 				    errbuf));
4377fa9e4066Sahrens 			}
437898579b20Snd150628 		}
43795ac95da7SSerapheim Dimitropoulos 
4380f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
438198579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4382fa9e4066Sahrens 	} else {
4383cdf5b4caSmmusante 		if (recursive) {
4384cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4385cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
4386cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4387cdf5b4caSmmusante 		}
4388cdf5b4caSmmusante 
4389f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
439098579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4391e9dbad6fSeschrock 
4392fa9e4066Sahrens 		/* validate parents */
4393d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4394fa9e4066Sahrens 			return (-1);
4395fa9e4066Sahrens 
4396fa9e4066Sahrens 		/* make sure we're in the same pool */
4397fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4398fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4399fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
440099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440199653d4eSeschrock 			    "datasets must be within same pool"));
440299653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4403fa9e4066Sahrens 		}
4404f2fdf992Snd150628 
4405f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
4406d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
4407f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4408d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
4409f2fdf992Snd150628 			    "current dataset name"));
4410f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4411f2fdf992Snd150628 		}
4412fa9e4066Sahrens 	}
4413fa9e4066Sahrens 
441499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
441599653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
441699653d4eSeschrock 
4417fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4418fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
441999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
442099653d4eSeschrock 		    "dataset is used in a non-global zone"));
442199653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4422fa9e4066Sahrens 	}
4423fa9e4066Sahrens 
4424cdf5b4caSmmusante 	if (recursive) {
4425f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4426f0c5ee21Smmusante 		if (parentname == NULL) {
4427f0c5ee21Smmusante 			ret = -1;
4428f0c5ee21Smmusante 			goto error;
4429f0c5ee21Smmusante 		}
4430cdf5b4caSmmusante 		delim = strchr(parentname, '@');
4431cdf5b4caSmmusante 		*delim = '\0';
4432990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4433cdf5b4caSmmusante 		if (zhrp == NULL) {
4434f0c5ee21Smmusante 			ret = -1;
4435f0c5ee21Smmusante 			goto error;
4436cdf5b4caSmmusante 		}
443733cde0d0SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
44386a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
44396a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
444099653d4eSeschrock 			return (-1);
4441fa9e4066Sahrens 
4442fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
444399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
444499653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
444599653d4eSeschrock 			    "in a non-global zone"));
4446e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4447f83b46baSPaul Dagnelie 			ret = -1;
4448fa9e4066Sahrens 			goto error;
4449fa9e4066Sahrens 		}
4450fa9e4066Sahrens 
4451fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
4452fa9e4066Sahrens 			goto error;
4453cdf5b4caSmmusante 	}
4454fa9e4066Sahrens 
4455e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
4456fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4457fa9e4066Sahrens 	else
4458fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4459fa9e4066Sahrens 
446098579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4461e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
446298579b20Snd150628 
4463cdf5b4caSmmusante 	zc.zc_cookie = recursive;
4464cdf5b4caSmmusante 
4465ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4466cdf5b4caSmmusante 		/*
4467cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
4468cdf5b4caSmmusante 		 * be in zc.zc_name
4469cdf5b4caSmmusante 		 */
4470cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
44713cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
4472cdf5b4caSmmusante 
4473cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
4474cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4475cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
4476cdf5b4caSmmusante 			    "with the new name"));
4477a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4478eb633035STom Caputi 		} else if (errno == EACCES) {
4479eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4480a60ca23dSTom Caputi 			    "cannot move encrypted child outside of "
4481eb633035STom Caputi 			    "its encryption root"));
4482eb633035STom Caputi 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4483cdf5b4caSmmusante 		} else {
448499653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4485cdf5b4caSmmusante 		}
4486fa9e4066Sahrens 
4487fa9e4066Sahrens 		/*
4488fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
4489fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
4490fa9e4066Sahrens 		 */
449133cde0d0SMatthew Ahrens 		if (cl != NULL)
4492fa9e4066Sahrens 			(void) changelist_postfix(cl);
4493cdf5b4caSmmusante 	} else {
449433cde0d0SMatthew Ahrens 		if (cl != NULL) {
4495fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
4496fa9e4066Sahrens 			ret = changelist_postfix(cl);
4497fa9e4066Sahrens 		}
4498cdf5b4caSmmusante 	}
4499fa9e4066Sahrens 
4500fa9e4066Sahrens error:
450133cde0d0SMatthew Ahrens 	if (parentname != NULL) {
4502cdf5b4caSmmusante 		free(parentname);
4503cdf5b4caSmmusante 	}
450433cde0d0SMatthew Ahrens 	if (zhrp != NULL) {
4505cdf5b4caSmmusante 		zfs_close(zhrp);
4506cdf5b4caSmmusante 	}
450733cde0d0SMatthew Ahrens 	if (cl != NULL) {
4508fa9e4066Sahrens 		changelist_free(cl);
4509cdf5b4caSmmusante 	}
4510fa9e4066Sahrens 	return (ret);
4511fa9e4066Sahrens }
4512fa9e4066Sahrens 
4513e9dbad6fSeschrock nvlist_t *
4514e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4515e9dbad6fSeschrock {
4516e9dbad6fSeschrock 	return (zhp->zfs_user_props);
4517e9dbad6fSeschrock }
4518e9dbad6fSeschrock 
451992241e0bSTom Erickson nvlist_t *
452092241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
452192241e0bSTom Erickson {
452292241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
452392241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
452492241e0bSTom Erickson 			return (NULL);
452592241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
452692241e0bSTom Erickson }
452792241e0bSTom Erickson 
4528e9dbad6fSeschrock /*
4529e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
4530e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
4531e9dbad6fSeschrock  *
4532e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
4533e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
4534e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
4535e9dbad6fSeschrock  *
4536e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
453792241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
453892241e0bSTom Erickson  *        requested received property values, we also need to compute the width
453992241e0bSTom Erickson  *        of the RECEIVED column.
4540e9dbad6fSeschrock  */
4541e9dbad6fSeschrock int
454243d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
454343d68d68SYuri Pankov     boolean_t literal)
4544e9dbad6fSeschrock {
4545e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4546990b4856Slling 	zprop_list_t *entry;
4547990b4856Slling 	zprop_list_t **last, **start;
4548e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
4549e9dbad6fSeschrock 	nvpair_t *elem;
4550e9dbad6fSeschrock 	char *strval;
4551e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
4552e9dbad6fSeschrock 
4553990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4554e9dbad6fSeschrock 		return (-1);
4555e9dbad6fSeschrock 
4556e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4557e9dbad6fSeschrock 
4558e9dbad6fSeschrock 	entry = *plp;
4559e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4560e9dbad6fSeschrock 		/*
4561e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4562e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4563e9dbad6fSeschrock 		 * non-native property.
4564e9dbad6fSeschrock 		 */
4565e9dbad6fSeschrock 		start = plp;
4566e9dbad6fSeschrock 		while (*start != NULL) {
4567990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4568e9dbad6fSeschrock 				break;
4569e9dbad6fSeschrock 			start = &(*start)->pl_next;
4570e9dbad6fSeschrock 		}
4571e9dbad6fSeschrock 
4572e9dbad6fSeschrock 		elem = NULL;
4573e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4574e9dbad6fSeschrock 			/*
4575e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4576e9dbad6fSeschrock 			 */
4577e9dbad6fSeschrock 			for (last = start; *last != NULL;
4578e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4579e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4580e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4581e9dbad6fSeschrock 					break;
4582e9dbad6fSeschrock 			}
4583e9dbad6fSeschrock 
4584e9dbad6fSeschrock 			if (*last == NULL) {
4585e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4586990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4587e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4588e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4589e9dbad6fSeschrock 					free(entry);
4590e9dbad6fSeschrock 					return (-1);
4591e9dbad6fSeschrock 				}
4592e9dbad6fSeschrock 
4593990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4594e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4595e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4596e9dbad6fSeschrock 				*last = entry;
4597e9dbad6fSeschrock 			}
4598e9dbad6fSeschrock 		}
4599e9dbad6fSeschrock 	}
4600e9dbad6fSeschrock 
4601e9dbad6fSeschrock 	/*
4602e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4603e9dbad6fSeschrock 	 */
4604e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
460543d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
4606e9dbad6fSeschrock 			continue;
4607e9dbad6fSeschrock 
4608990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4609e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
461043d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4611e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4612e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4613e9dbad6fSeschrock 			}
461492241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
461592241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
461643d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
461792241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
461892241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
461992241e0bSTom Erickson 		} else {
462092241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
462192241e0bSTom Erickson 			    &propval) == 0) {
4622e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
4623990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
4624e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
4625e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
4626e9dbad6fSeschrock 			}
462792241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
462892241e0bSTom Erickson 			    entry->pl_user_prop,
462943d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
463092241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
463192241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
463292241e0bSTom Erickson 		}
4633e9dbad6fSeschrock 	}
4634e9dbad6fSeschrock 
4635e9dbad6fSeschrock 	return (0);
4636e9dbad6fSeschrock }
4637ecd6cf80Smarks 
4638ecd6cf80Smarks int
4639ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4640743a77edSAlan Wright     char *resource, void *export, void *sharetab,
4641743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
4642ecd6cf80Smarks {
4643ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4644ecd6cf80Smarks 	int error;
4645ecd6cf80Smarks 
4646ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4647ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4648743a77edSAlan Wright 	if (resource)
4649743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4650ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4651ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4652da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4653ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4654ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4655ecd6cf80Smarks 	return (error);
4656ecd6cf80Smarks }
46572e5e9e19SSanjeev Bagewadi 
46582e5e9e19SSanjeev Bagewadi void
46592e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
46602e5e9e19SSanjeev Bagewadi {
46612e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
46622e5e9e19SSanjeev Bagewadi 
46632e5e9e19SSanjeev Bagewadi 	/*
46642e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
46652e5e9e19SSanjeev Bagewadi 	 * properties.
46662e5e9e19SSanjeev Bagewadi 	 */
46672e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
46682e5e9e19SSanjeev Bagewadi 
46692e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
46702e5e9e19SSanjeev Bagewadi 
46712e5e9e19SSanjeev Bagewadi 	while (curr) {
46722e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
46732e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
46742e5e9e19SSanjeev Bagewadi 
467514843421SMatthew Ahrens 		/*
4676faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4677faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4678faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4679faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4680faaa6415SEric Schrock 		 * software, for example).
468114843421SMatthew Ahrens 		 */
468214843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
46832e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
46842e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
46852e5e9e19SSanjeev Bagewadi 		curr = next;
46862e5e9e19SSanjeev Bagewadi 	}
46872e5e9e19SSanjeev Bagewadi }
4688743a77edSAlan Wright 
4689743a77edSAlan Wright static int
4690743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4691743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4692743a77edSAlan Wright {
4693743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4694743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4695743a77edSAlan Wright 	int error;
4696743a77edSAlan Wright 
4697743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4698743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4699743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4700743a77edSAlan Wright 
4701743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4702743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4703743a77edSAlan Wright 			(void) no_memory(hdl);
470430925561SChris Williamson 			return (0);
4705743a77edSAlan Wright 		}
4706743a77edSAlan Wright 	}
4707743a77edSAlan Wright 
4708743a77edSAlan Wright 	switch (cmd) {
4709743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4710743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4711743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4712743a77edSAlan Wright 		break;
4713743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4714743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4715743a77edSAlan Wright 		    resource1) != 0) {
4716743a77edSAlan Wright 				(void) no_memory(hdl);
4717743a77edSAlan Wright 				return (-1);
4718743a77edSAlan Wright 		}
4719743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4720743a77edSAlan Wright 		    resource2) != 0) {
4721743a77edSAlan Wright 				(void) no_memory(hdl);
4722743a77edSAlan Wright 				return (-1);
4723743a77edSAlan Wright 		}
4724743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4725743a77edSAlan Wright 			nvlist_free(nvlist);
4726743a77edSAlan Wright 			return (-1);
4727743a77edSAlan Wright 		}
4728743a77edSAlan Wright 		break;
4729743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4730743a77edSAlan Wright 		break;
4731743a77edSAlan Wright 	default:
4732743a77edSAlan Wright 		return (-1);
4733743a77edSAlan Wright 	}
4734743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4735743a77edSAlan Wright 	nvlist_free(nvlist);
4736743a77edSAlan Wright 	return (error);
4737743a77edSAlan Wright }
4738743a77edSAlan Wright 
4739743a77edSAlan Wright int
4740743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4741743a77edSAlan Wright     char *path, char *resource)
4742743a77edSAlan Wright {
4743743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4744743a77edSAlan Wright 	    resource, NULL));
4745743a77edSAlan Wright }
4746743a77edSAlan Wright 
4747743a77edSAlan Wright int
4748743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4749743a77edSAlan Wright     char *path, char *resource)
4750743a77edSAlan Wright {
4751743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4752743a77edSAlan Wright 	    resource, NULL));
4753743a77edSAlan Wright }
4754743a77edSAlan Wright 
4755743a77edSAlan Wright int
4756743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4757743a77edSAlan Wright {
4758743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4759743a77edSAlan Wright 	    NULL, NULL));
4760743a77edSAlan Wright }
4761743a77edSAlan Wright 
4762743a77edSAlan Wright int
4763743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4764743a77edSAlan Wright     char *oldname, char *newname)
4765743a77edSAlan Wright {
4766743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4767743a77edSAlan Wright 	    oldname, newname));
4768743a77edSAlan Wright }
476914843421SMatthew Ahrens 
477014843421SMatthew Ahrens int
477114843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
477214843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
477314843421SMatthew Ahrens {
477414843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
477514843421SMatthew Ahrens 	zfs_useracct_t buf[100];
477670f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
477770f56fa6SYuri Pankov 	int ret;
477814843421SMatthew Ahrens 
477919b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
478014843421SMatthew Ahrens 
478114843421SMatthew Ahrens 	zc.zc_objset_type = type;
478214843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
478314843421SMatthew Ahrens 
478470f56fa6SYuri Pankov 	for (;;) {
478514843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
478614843421SMatthew Ahrens 
478714843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
478870f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
47893b2aab18SMatthew Ahrens 			char errbuf[1024];
479070f56fa6SYuri Pankov 
4791*f67950b2SNasf-Fan 			if ((errno == ENOTSUP &&
4792*f67950b2SNasf-Fan 			    (type == ZFS_PROP_USEROBJUSED ||
4793*f67950b2SNasf-Fan 			    type == ZFS_PROP_GROUPOBJUSED ||
4794*f67950b2SNasf-Fan 			    type == ZFS_PROP_USEROBJQUOTA ||
4795*f67950b2SNasf-Fan 			    type == ZFS_PROP_GROUPOBJQUOTA ||
4796*f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTOBJUSED ||
4797*f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTOBJQUOTA ||
4798*f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTUSED ||
4799*f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTQUOTA)))
4800*f67950b2SNasf-Fan 				break;
4801*f67950b2SNasf-Fan 
480270f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
480370f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
480470f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
480570f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
480670f56fa6SYuri Pankov 		}
480770f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
480814843421SMatthew Ahrens 			break;
480914843421SMatthew Ahrens 
481014843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
481170f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
481270f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
481370f56fa6SYuri Pankov 				return (ret);
481414843421SMatthew Ahrens 			zua++;
481514843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
481614843421SMatthew Ahrens 		}
481714843421SMatthew Ahrens 	}
481814843421SMatthew Ahrens 
481970f56fa6SYuri Pankov 	return (0);
482014843421SMatthew Ahrens }
4821842727c2SChris Kirby 
48223b2aab18SMatthew Ahrens struct holdarg {
48233b2aab18SMatthew Ahrens 	nvlist_t *nvl;
48243b2aab18SMatthew Ahrens 	const char *snapname;
48253b2aab18SMatthew Ahrens 	const char *tag;
48263b2aab18SMatthew Ahrens 	boolean_t recursive;
4827bb6e7075SMatthew Ahrens 	int error;
48283b2aab18SMatthew Ahrens };
48293b2aab18SMatthew Ahrens 
48303b2aab18SMatthew Ahrens static int
48313b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
48323b2aab18SMatthew Ahrens {
48333b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
48349adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
48353b2aab18SMatthew Ahrens 	int rv = 0;
48363b2aab18SMatthew Ahrens 
48373b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
48383b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
48393b2aab18SMatthew Ahrens 
4840a7a845e4SSteven Hartland 	if (lzc_exists(name))
48413b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
48423b2aab18SMatthew Ahrens 
48433b2aab18SMatthew Ahrens 	if (ha->recursive)
48443b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
48453b2aab18SMatthew Ahrens 	zfs_close(zhp);
48463b2aab18SMatthew Ahrens 	return (rv);
48473b2aab18SMatthew Ahrens }
48483b2aab18SMatthew Ahrens 
4849842727c2SChris Kirby int
4850842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4851a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4852842727c2SChris Kirby {
48533b2aab18SMatthew Ahrens 	int ret;
48543b2aab18SMatthew Ahrens 	struct holdarg ha;
4855842727c2SChris Kirby 
48563b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
48573b2aab18SMatthew Ahrens 	ha.snapname = snapname;
48583b2aab18SMatthew Ahrens 	ha.tag = tag;
48593b2aab18SMatthew Ahrens 	ha.recursive = recursive;
48603b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4861013023d4SMartin Matuska 
4862a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4863a7a845e4SSteven Hartland 		char errbuf[1024];
4864a7a845e4SSteven Hartland 
4865013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4866013023d4SMartin Matuska 		ret = ENOENT;
4867013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4868013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4869013023d4SMartin Matuska 		    "cannot hold snapshot '%s@%s'"),
4870013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4871a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4872013023d4SMartin Matuska 		return (ret);
4873013023d4SMartin Matuska 	}
4874013023d4SMartin Matuska 
4875a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
48763b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4877a7f53a56SChris Kirby 
4878a7a845e4SSteven Hartland 	return (ret);
4879a7a845e4SSteven Hartland }
4880842727c2SChris Kirby 
4881a7a845e4SSteven Hartland int
4882a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4883a7a845e4SSteven Hartland {
4884a7a845e4SSteven Hartland 	int ret;
4885a7a845e4SSteven Hartland 	nvlist_t *errors;
4886a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4887a7a845e4SSteven Hartland 	char errbuf[1024];
4888a7a845e4SSteven Hartland 	nvpair_t *elem;
4889a7a845e4SSteven Hartland 
4890a7a845e4SSteven Hartland 	errors = NULL;
4891a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4892a7a845e4SSteven Hartland 
4893a7a845e4SSteven Hartland 	if (ret == 0) {
4894a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4895a7a845e4SSteven Hartland 		fnvlist_free(errors);
4896a7a845e4SSteven Hartland 		return (0);
4897a7a845e4SSteven Hartland 	}
4898a7a845e4SSteven Hartland 
4899a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
49003b2aab18SMatthew Ahrens 		/* no hold-specific errors */
49013b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
49023b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
49033b2aab18SMatthew Ahrens 		switch (ret) {
49043b2aab18SMatthew Ahrens 		case ENOTSUP:
49053b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49063b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
49073b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
49083b2aab18SMatthew Ahrens 			break;
49093b2aab18SMatthew Ahrens 		case EINVAL:
49103b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49113b2aab18SMatthew Ahrens 			break;
49123b2aab18SMatthew Ahrens 		default:
49133b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
49143b2aab18SMatthew Ahrens 		}
49153b2aab18SMatthew Ahrens 	}
4916842727c2SChris Kirby 
49173b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
49183b2aab18SMatthew Ahrens 	    elem != NULL;
49193b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
49203b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
49213b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
49223b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
49233b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
492415508ac0SChris Kirby 		case E2BIG:
492515508ac0SChris Kirby 			/*
492615508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
492715508ac0SChris Kirby 			 * prepended. So even if we passed the length check
492815508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
492915508ac0SChris Kirby 			 * up being slightly too long.
493015508ac0SChris Kirby 			 */
49313b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
49323b2aab18SMatthew Ahrens 			break;
4933842727c2SChris Kirby 		case EINVAL:
49343b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49353b2aab18SMatthew Ahrens 			break;
4936842727c2SChris Kirby 		case EEXIST:
49373b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
49383b2aab18SMatthew Ahrens 			break;
4939842727c2SChris Kirby 		default:
49403b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
49413b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4942842727c2SChris Kirby 		}
4943842727c2SChris Kirby 	}
4944842727c2SChris Kirby 
49453b2aab18SMatthew Ahrens 	fnvlist_free(errors);
49463b2aab18SMatthew Ahrens 	return (ret);
49473b2aab18SMatthew Ahrens }
49483b2aab18SMatthew Ahrens 
49493b2aab18SMatthew Ahrens static int
49503b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
49513b2aab18SMatthew Ahrens {
49523b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
49539adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
49543b2aab18SMatthew Ahrens 	int rv = 0;
4955bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
49563b2aab18SMatthew Ahrens 
49573b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
49583b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
49593b2aab18SMatthew Ahrens 
4960bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4961bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4962bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4963bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4964bb6e7075SMatthew Ahrens 	} else {
4965bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4966bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4967bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4968bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
49693b2aab18SMatthew Ahrens 	}
49703b2aab18SMatthew Ahrens 
49713b2aab18SMatthew Ahrens 	if (ha->recursive)
49723b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
49733b2aab18SMatthew Ahrens 	zfs_close(zhp);
49743b2aab18SMatthew Ahrens 	return (rv);
4975842727c2SChris Kirby }
4976842727c2SChris Kirby 
4977842727c2SChris Kirby int
4978842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4979842727c2SChris Kirby     boolean_t recursive)
4980842727c2SChris Kirby {
49813b2aab18SMatthew Ahrens 	int ret;
49823b2aab18SMatthew Ahrens 	struct holdarg ha;
4983a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
49843b2aab18SMatthew Ahrens 	nvpair_t *elem;
4985842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4986013023d4SMartin Matuska 	char errbuf[1024];
4987842727c2SChris Kirby 
49883b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
49893b2aab18SMatthew Ahrens 	ha.snapname = snapname;
49903b2aab18SMatthew Ahrens 	ha.tag = tag;
49913b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4992bb6e7075SMatthew Ahrens 	ha.error = 0;
49933b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4994013023d4SMartin Matuska 
4995a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4996013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4997bb6e7075SMatthew Ahrens 		ret = ha.error;
4998013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4999013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
5000013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
5001013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
5002bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
5003bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5004bb6e7075SMatthew Ahrens 		} else {
5005013023d4SMartin Matuska 			(void) zfs_standard_error(hdl, ret, errbuf);
5006bb6e7075SMatthew Ahrens 		}
5007013023d4SMartin Matuska 		return (ret);
5008013023d4SMartin Matuska 	}
5009013023d4SMartin Matuska 
50103b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
50113b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
5012842727c2SChris Kirby 
5013a7a845e4SSteven Hartland 	if (ret == 0) {
5014a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
5015a7a845e4SSteven Hartland 		fnvlist_free(errors);
50163b2aab18SMatthew Ahrens 		return (0);
5017a7a845e4SSteven Hartland 	}
5018842727c2SChris Kirby 
5019a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
50203b2aab18SMatthew Ahrens 		/* no hold-specific errors */
5021842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
50223b2aab18SMatthew Ahrens 		    "cannot release"));
5023842727c2SChris Kirby 		switch (errno) {
5024842727c2SChris Kirby 		case ENOTSUP:
5025842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5026842727c2SChris Kirby 			    "pool must be upgraded"));
50273b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
50283b2aab18SMatthew Ahrens 			break;
5029842727c2SChris Kirby 		default:
50303b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
5031842727c2SChris Kirby 		}
5032842727c2SChris Kirby 	}
5033842727c2SChris Kirby 
50343b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
50353b2aab18SMatthew Ahrens 	    elem != NULL;
50363b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
50373b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
50383b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
50393b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
50403b2aab18SMatthew Ahrens 		    nvpair_name(elem));
50413b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
50423b2aab18SMatthew Ahrens 		case ESRCH:
50433b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
50443b2aab18SMatthew Ahrens 			break;
50453b2aab18SMatthew Ahrens 		case EINVAL:
50463b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
50473b2aab18SMatthew Ahrens 			break;
50483b2aab18SMatthew Ahrens 		default:
50493b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
50503b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
50513b2aab18SMatthew Ahrens 		}
50523b2aab18SMatthew Ahrens 	}
50533b2aab18SMatthew Ahrens 
50543b2aab18SMatthew Ahrens 	fnvlist_free(errors);
50553b2aab18SMatthew Ahrens 	return (ret);
5056842727c2SChris Kirby }
5057ca45db41SChris Kirby 
50581af68beaSAlexander Stetsenko int
50591af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
50601af68beaSAlexander Stetsenko {
50611af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
50621af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
50631af68beaSAlexander Stetsenko 	int nvsz = 2048;
50641af68beaSAlexander Stetsenko 	void *nvbuf;
50651af68beaSAlexander Stetsenko 	int err = 0;
50663b2aab18SMatthew Ahrens 	char errbuf[1024];
50671af68beaSAlexander Stetsenko 
50681af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
50691af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
50701af68beaSAlexander Stetsenko 
50711af68beaSAlexander Stetsenko tryagain:
50721af68beaSAlexander Stetsenko 
50731af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
50741af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
50751af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
50761af68beaSAlexander Stetsenko 		goto out;
50771af68beaSAlexander Stetsenko 	}
50781af68beaSAlexander Stetsenko 
50791af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
50801af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
50811af68beaSAlexander Stetsenko 
50829adfa60dSMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
50831af68beaSAlexander Stetsenko 
5084d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
50851af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
50861af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
50871af68beaSAlexander Stetsenko 		    zc.zc_name);
50881af68beaSAlexander Stetsenko 		switch (errno) {
50891af68beaSAlexander Stetsenko 		case ENOMEM:
50901af68beaSAlexander Stetsenko 			free(nvbuf);
50911af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
50921af68beaSAlexander Stetsenko 			goto tryagain;
50931af68beaSAlexander Stetsenko 
50941af68beaSAlexander Stetsenko 		case ENOTSUP:
50951af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50961af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
50971af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
50981af68beaSAlexander Stetsenko 			break;
50991af68beaSAlexander Stetsenko 		case EINVAL:
51001af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51011af68beaSAlexander Stetsenko 			break;
51021af68beaSAlexander Stetsenko 		case ENOENT:
51031af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
51041af68beaSAlexander Stetsenko 			break;
51051af68beaSAlexander Stetsenko 		default:
51061af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
51071af68beaSAlexander Stetsenko 			break;
51081af68beaSAlexander Stetsenko 		}
51091af68beaSAlexander Stetsenko 	} else {
51101af68beaSAlexander Stetsenko 		/* success */
51111af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
51121af68beaSAlexander Stetsenko 		if (rc) {
51131af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
51141af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
51151af68beaSAlexander Stetsenko 			    zc.zc_name);
51161af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
51171af68beaSAlexander Stetsenko 		}
51181af68beaSAlexander Stetsenko 	}
51191af68beaSAlexander Stetsenko 
51201af68beaSAlexander Stetsenko 	free(nvbuf);
51211af68beaSAlexander Stetsenko out:
51221af68beaSAlexander Stetsenko 	return (err);
51231af68beaSAlexander Stetsenko }
51241af68beaSAlexander Stetsenko 
51251af68beaSAlexander Stetsenko int
51261af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
51271af68beaSAlexander Stetsenko {
51281af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
51291af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
51301af68beaSAlexander Stetsenko 	char *nvbuf;
51313b2aab18SMatthew Ahrens 	char errbuf[1024];
51321af68beaSAlexander Stetsenko 	size_t nvsz;
51331af68beaSAlexander Stetsenko 	int err;
51341af68beaSAlexander Stetsenko 
51351af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
51361af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
51371af68beaSAlexander Stetsenko 
51381af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
51391af68beaSAlexander Stetsenko 	assert(err == 0);
51401af68beaSAlexander Stetsenko 
51411af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
51421af68beaSAlexander Stetsenko 
51431af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
51441af68beaSAlexander Stetsenko 	assert(err == 0);
51451af68beaSAlexander Stetsenko 
51461af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
51471af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
51481af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
51491af68beaSAlexander Stetsenko 
51501af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
51511af68beaSAlexander Stetsenko 
51521af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
51531af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
51541af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
51551af68beaSAlexander Stetsenko 		    zc.zc_name);
51561af68beaSAlexander Stetsenko 		switch (errno) {
51571af68beaSAlexander Stetsenko 		case ENOTSUP:
51581af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51591af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
51601af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51611af68beaSAlexander Stetsenko 			break;
51621af68beaSAlexander Stetsenko 		case EINVAL:
51631af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51641af68beaSAlexander Stetsenko 			break;
51651af68beaSAlexander Stetsenko 		case ENOENT:
51661af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
51671af68beaSAlexander Stetsenko 			break;
51681af68beaSAlexander Stetsenko 		default:
51691af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
51701af68beaSAlexander Stetsenko 			break;
51711af68beaSAlexander Stetsenko 		}
51721af68beaSAlexander Stetsenko 	}
51731af68beaSAlexander Stetsenko 
51741af68beaSAlexander Stetsenko 	free(nvbuf);
51751af68beaSAlexander Stetsenko 
51761af68beaSAlexander Stetsenko 	return (err);
51771af68beaSAlexander Stetsenko }
51781af68beaSAlexander Stetsenko 
51791af68beaSAlexander Stetsenko int
51801af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
51811af68beaSAlexander Stetsenko {
51823b2aab18SMatthew Ahrens 	int err;
51833b2aab18SMatthew Ahrens 	char errbuf[1024];
51843b2aab18SMatthew Ahrens 
51853b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
51863b2aab18SMatthew Ahrens 
51873b2aab18SMatthew Ahrens 	if (err != 0) {
51881af68beaSAlexander Stetsenko 		libzfs_handle_t *hdl = zhp->zfs_hdl;
51891af68beaSAlexander Stetsenko 
51901af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
51911af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
51923b2aab18SMatthew Ahrens 		    zhp->zfs_name);
51933b2aab18SMatthew Ahrens 		switch (err) {
51941af68beaSAlexander Stetsenko 		case ENOTSUP:
51951af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51961af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
51971af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51981af68beaSAlexander Stetsenko 			break;
51991af68beaSAlexander Stetsenko 		case EINVAL:
52001af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
52011af68beaSAlexander Stetsenko 			break;
52021af68beaSAlexander Stetsenko 		case ENOENT:
52031af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
52041af68beaSAlexander Stetsenko 			break;
52051af68beaSAlexander Stetsenko 		default:
52061af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
52071af68beaSAlexander Stetsenko 			break;
52081af68beaSAlexander Stetsenko 		}
52091af68beaSAlexander Stetsenko 	}
52101af68beaSAlexander Stetsenko 
52111af68beaSAlexander Stetsenko 	return (err);
52121af68beaSAlexander Stetsenko }
52131af68beaSAlexander Stetsenko 
52143e30c24aSWill Andrews /*
5215b73ccab0SMike Gerdts  * The theory of raidz space accounting
5216b73ccab0SMike Gerdts  *
5217b73ccab0SMike Gerdts  * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5218b73ccab0SMike Gerdts  * will "reference" 128KB, even though it allocates more than that, to store the
5219b73ccab0SMike Gerdts  * parity information (and perhaps skip sectors). This concept of the
5220b73ccab0SMike Gerdts  * "referenced" (and other DMU space accounting) being lower than the allocated
5221b73ccab0SMike Gerdts  * space by a constant factor is called "raidz deflation."
5222b73ccab0SMike Gerdts  *
5223b73ccab0SMike Gerdts  * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5224b73ccab0SMike Gerdts  * block size. However, zvols typically have a much smaller block size (default
5225b73ccab0SMike Gerdts  * 8KB). These smaller blocks may require proportionally much more parity
5226b73ccab0SMike Gerdts  * information (and perhaps skip sectors). In this case, the change to the
5227b73ccab0SMike Gerdts  * "referenced" property may be much more than the logical block size.
5228b73ccab0SMike Gerdts  *
5229b73ccab0SMike Gerdts  * Suppose a raidz vdev has 5 disks with ashift=12.  A 128k block may be written
5230b73ccab0SMike Gerdts  * as follows.
5231b73ccab0SMike Gerdts  *
5232b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5233b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5234b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5235b73ccab0SMike Gerdts  * |  P0   |  D0   |  D8   |  D16  |  D24  |
5236b73ccab0SMike Gerdts  * |  P1   |  D1   |  D9   |  D17  |  D25  |
5237b73ccab0SMike Gerdts  * |  P2   |  D2   |  D10  |  D18  |  D26  |
5238b73ccab0SMike Gerdts  * |  P3   |  D3   |  D11  |  D19  |  D27  |
5239b73ccab0SMike Gerdts  * |  P4   |  D4   |  D12  |  D20  |  D28  |
5240b73ccab0SMike Gerdts  * |  P5   |  D5   |  D13  |  D21  |  D29  |
5241b73ccab0SMike Gerdts  * |  P6   |  D6   |  D14  |  D22  |  D30  |
5242b73ccab0SMike Gerdts  * |  P7   |  D7   |  D15  |  D23  |  D31  |
5243b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5244b73ccab0SMike Gerdts  *
5245b73ccab0SMike Gerdts  * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5246b73ccab0SMike Gerdts  * sectors.  The dataset's referenced will increase by 128k and the pool's
5247b73ccab0SMike Gerdts  * allocated and free properties will be adjusted by 160k.
5248b73ccab0SMike Gerdts  *
5249b73ccab0SMike Gerdts  * A 4k block written to the same raidz vdev will require two 4k sectors.  The
5250b73ccab0SMike Gerdts  * blank cells represent unallocated space.
5251b73ccab0SMike Gerdts  *
5252b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5253b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5254b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5255b73ccab0SMike Gerdts  * |  P0   |  D0   |       |       |       |
5256b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5257b73ccab0SMike Gerdts  *
5258b73ccab0SMike Gerdts  * Above, notice that the 4k block required one sector for parity and another
5259b73ccab0SMike Gerdts  * for data.  vdev_raidz_asize() will return 8k and as such the pool's allocated
5260b73ccab0SMike Gerdts  * and free properties will be adjusted by 8k.  The dataset will not be charged
5261b73ccab0SMike Gerdts  * 8k.  Rather, it will be charged a value that is scaled according to the
5262b73ccab0SMike Gerdts  * overhead of the 128k block on the same vdev.  This 8k allocation will be
5263b73ccab0SMike Gerdts  * charged 8k * 128k / 160k.  128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5264b73ccab0SMike Gerdts  * calculated in the 128k block example above.
5265b73ccab0SMike Gerdts  *
5266b73ccab0SMike Gerdts  * Every raidz allocation is sized to be a multiple of nparity+1 sectors.  That
5267b73ccab0SMike Gerdts  * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5268b73ccab0SMike Gerdts  * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5269b73ccab0SMike Gerdts  * multiple of of 4 sectors.  When a block does not fill the required number of
5270b73ccab0SMike Gerdts  * sectors, skip blocks (sectors) are used.
5271b73ccab0SMike Gerdts  *
5272b73ccab0SMike Gerdts  * An 8k block being written to a raidz vdev may be written as follows:
5273b73ccab0SMike Gerdts  *
5274b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5275b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5276b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5277b73ccab0SMike Gerdts  * |  P0   |  D0   |  D1   |  S0   |       |
5278b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5279b73ccab0SMike Gerdts  *
5280b73ccab0SMike Gerdts  * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5281b73ccab0SMike Gerdts  * added.  For this 8k block, the pool's allocated and free properties are
5282b73ccab0SMike Gerdts  * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5283b73ccab0SMike Gerdts  * 160k.  Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5284b73ccab0SMike Gerdts  * the 128k block example above.
5285b73ccab0SMike Gerdts  *
5286b73ccab0SMike Gerdts  * Compression may lead to a variety of block sizes being written for the same
5287b73ccab0SMike Gerdts  * volume or file.  There is no clear way to reserve just the amount of space
5288b73ccab0SMike Gerdts  * that will be required, so the worst case (no compression) is assumed.
5289b73ccab0SMike Gerdts  * Note that metadata blocks will typically be compressed, so the reservation
5290b73ccab0SMike Gerdts  * size returned by zvol_volsize_to_reservation() will generally be slightly
5291b73ccab0SMike Gerdts  * larger than the maximum that the volume can reference.
5292b73ccab0SMike Gerdts  */
5293b73ccab0SMike Gerdts 
5294b73ccab0SMike Gerdts /*
5295b73ccab0SMike Gerdts  * Derived from function of same name in uts/common/fs/zfs/vdev_raidz.c.
5296b73ccab0SMike Gerdts  * Returns the amount of space (in bytes) that will be allocated for the
5297b73ccab0SMike Gerdts  * specified block size. Note that the "referenced" space accounted will be less
5298b73ccab0SMike Gerdts  * than this, but not necessarily equal to "blksize", due to RAIDZ deflation.
5299b73ccab0SMike Gerdts  */
5300b73ccab0SMike Gerdts static uint64_t
5301b73ccab0SMike Gerdts vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5302b73ccab0SMike Gerdts     uint64_t blksize)
5303b73ccab0SMike Gerdts {
5304b73ccab0SMike Gerdts 	uint64_t asize, ndata;
5305b73ccab0SMike Gerdts 
5306b73ccab0SMike Gerdts 	ASSERT3U(ndisks, >, nparity);
5307b73ccab0SMike Gerdts 	ndata = ndisks - nparity;
5308b73ccab0SMike Gerdts 	asize = ((blksize - 1) >> ashift) + 1;
5309b73ccab0SMike Gerdts 	asize += nparity * ((asize + ndata - 1) / ndata);
5310b73ccab0SMike Gerdts 	asize = roundup(asize, nparity + 1) << ashift;
5311b73ccab0SMike Gerdts 
5312b73ccab0SMike Gerdts 	return (asize);
5313b73ccab0SMike Gerdts }
5314b73ccab0SMike Gerdts 
5315b73ccab0SMike Gerdts /*
5316b73ccab0SMike Gerdts  * Determine how much space will be allocated if it lands on the most space-
5317b73ccab0SMike Gerdts  * inefficient top-level vdev.  Returns the size in bytes required to store one
5318b73ccab0SMike Gerdts  * copy of the volume data.  See theory comment above.
5319b73ccab0SMike Gerdts  */
5320b73ccab0SMike Gerdts static uint64_t
5321b73ccab0SMike Gerdts volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5322b73ccab0SMike Gerdts {
5323b73ccab0SMike Gerdts 	nvlist_t *config, *tree, **vdevs;
5324b73ccab0SMike Gerdts 	uint_t nvdevs, v;
5325b73ccab0SMike Gerdts 	uint64_t ret = 0;
5326b73ccab0SMike Gerdts 
5327b73ccab0SMike Gerdts 	config = zpool_get_config(zhp, NULL);
5328b73ccab0SMike Gerdts 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5329b73ccab0SMike Gerdts 	    nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5330b73ccab0SMike Gerdts 	    &vdevs, &nvdevs) != 0) {
5331b73ccab0SMike Gerdts 		return (nblocks * blksize);
5332b73ccab0SMike Gerdts 	}
5333b73ccab0SMike Gerdts 
5334b73ccab0SMike Gerdts 	for (v = 0; v < nvdevs; v++) {
5335b73ccab0SMike Gerdts 		char *type;
5336b73ccab0SMike Gerdts 		uint64_t nparity, ashift, asize, tsize;
5337b73ccab0SMike Gerdts 		nvlist_t **disks;
5338b73ccab0SMike Gerdts 		uint_t ndisks;
5339b73ccab0SMike Gerdts 		uint64_t volsize;
5340b73ccab0SMike Gerdts 
5341b73ccab0SMike Gerdts 		if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5342b73ccab0SMike Gerdts 		    &type) != 0 || strcmp(type, VDEV_TYPE_RAIDZ) != 0 ||
5343b73ccab0SMike Gerdts 		    nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_NPARITY,
5344b73ccab0SMike Gerdts 		    &nparity) != 0 ||
5345b73ccab0SMike Gerdts 		    nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_ASHIFT,
5346b73ccab0SMike Gerdts 		    &ashift) != 0 ||
5347b73ccab0SMike Gerdts 		    nvlist_lookup_nvlist_array(vdevs[v], ZPOOL_CONFIG_CHILDREN,
5348b73ccab0SMike Gerdts 		    &disks, &ndisks) != 0) {
5349b73ccab0SMike Gerdts 			continue;
5350b73ccab0SMike Gerdts 		}
5351b73ccab0SMike Gerdts 
5352b73ccab0SMike Gerdts 		/* allocation size for the "typical" 128k block */
5353b73ccab0SMike Gerdts 		tsize = vdev_raidz_asize(ndisks, nparity, ashift,
5354b73ccab0SMike Gerdts 		    SPA_OLD_MAXBLOCKSIZE);
5355b73ccab0SMike Gerdts 		/* allocation size for the blksize block */
5356b73ccab0SMike Gerdts 		asize = vdev_raidz_asize(ndisks, nparity, ashift, blksize);
5357b73ccab0SMike Gerdts 
5358b73ccab0SMike Gerdts 		/*
5359b73ccab0SMike Gerdts 		 * Scale this size down as a ratio of 128k / tsize.  See theory
5360b73ccab0SMike Gerdts 		 * statement above.
5361b73ccab0SMike Gerdts 		 */
5362b73ccab0SMike Gerdts 		volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize;
5363b73ccab0SMike Gerdts 		if (volsize > ret) {
5364b73ccab0SMike Gerdts 			ret = volsize;
5365b73ccab0SMike Gerdts 		}
5366b73ccab0SMike Gerdts 	}
5367b73ccab0SMike Gerdts 
5368b73ccab0SMike Gerdts 	if (ret == 0) {
5369b73ccab0SMike Gerdts 		ret = nblocks * blksize;
5370b73ccab0SMike Gerdts 	}
5371b73ccab0SMike Gerdts 
5372b73ccab0SMike Gerdts 	return (ret);
5373b73ccab0SMike Gerdts }
5374b73ccab0SMike Gerdts 
5375b73ccab0SMike Gerdts /*
5376b73ccab0SMike Gerdts  * Convert the zvol's volume size to an appropriate reservation.  See theory
5377b73ccab0SMike Gerdts  * comment above.
5378b73ccab0SMike Gerdts  *
53793e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
5380b73ccab0SMike Gerdts  * suite's shell version in reservation.shlib.
53813e30c24aSWill Andrews  */
5382c1449561SEric Taylor uint64_t
5383b73ccab0SMike Gerdts zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5384b73ccab0SMike Gerdts     nvlist_t *props)
5385c1449561SEric Taylor {
5386c1449561SEric Taylor 	uint64_t numdb;
5387c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
5388c1449561SEric Taylor 	int ncopies;
5389c1449561SEric Taylor 	char *strval;
5390c1449561SEric Taylor 
5391c1449561SEric Taylor 	if (nvlist_lookup_string(props,
5392c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5393c1449561SEric Taylor 		ncopies = atoi(strval);
5394c1449561SEric Taylor 	else
5395c1449561SEric Taylor 		ncopies = 1;
5396c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
5397c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5398c1449561SEric Taylor 	    &volblocksize) != 0)
5399c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5400b73ccab0SMike Gerdts 
5401c1449561SEric Taylor 	nblocks = volsize / volblocksize;
5402b73ccab0SMike Gerdts 	/*
5403b73ccab0SMike Gerdts 	 * Metadata defaults to using 128k blocks, not volblocksize blocks.  For
5404b73ccab0SMike Gerdts 	 * this reason, only the data blocks are scaled based on vdev config.
5405b73ccab0SMike Gerdts 	 */
5406b73ccab0SMike Gerdts 	volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5407b73ccab0SMike Gerdts 
5408c1449561SEric Taylor 	/* start with metadnode L0-L6 */
5409c1449561SEric Taylor 	numdb = 7;
5410c1449561SEric Taylor 	/* calculate number of indirects */
5411c1449561SEric Taylor 	while (nblocks > 1) {
5412c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
5413c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
5414c1449561SEric Taylor 		numdb += nblocks;
5415c1449561SEric Taylor 	}
5416c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5417c1449561SEric Taylor 	volsize *= ncopies;
5418c1449561SEric Taylor 	/*
5419c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5420c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
5421c1449561SEric Taylor 	 * 1100 bytes
5422c1449561SEric Taylor 	 */
5423c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5424c1449561SEric Taylor 	volsize += numdb;
5425c1449561SEric Taylor 	return (volsize);
5426c1449561SEric Taylor }
5427