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>
62*eb633035STom 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
104f18faf3fSek zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
105f18faf3fSek     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;
1445ad82045Snd 
1455ad82045Snd 			case NAME_ERR_NOLETTER:
1465ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475ad82045Snd 				    "pool doesn't begin with a letter"));
1485ad82045Snd 				break;
1495ad82045Snd 
1505ad82045Snd 			case NAME_ERR_RESERVED:
1515ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1525ad82045Snd 				    "name is reserved"));
1535ad82045Snd 				break;
1545ad82045Snd 
1555ad82045Snd 			case NAME_ERR_DISKLIKE:
1565ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1575ad82045Snd 				    "reserved disk name"));
1585ad82045Snd 				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 
198f18faf3fSek 	if (modifying && strchr(path, '%') != NULL) {
199f18faf3fSek 		if (hdl != NULL)
200f18faf3fSek 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
201f18faf3fSek 			    "invalid character %c in name"), '%');
202f18faf3fSek 		return (0);
203f18faf3fSek 	}
204f18faf3fSek 
20599653d4eSeschrock 	return (-1);
206fa9e4066Sahrens }
207fa9e4066Sahrens 
208fa9e4066Sahrens int
209fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
210fa9e4066Sahrens {
211e7cbe64fSgw 	if (type == ZFS_TYPE_POOL)
212e7cbe64fSgw 		return (zpool_name_valid(NULL, B_FALSE, name));
213f18faf3fSek 	return (zfs_validate_name(NULL, name, type, B_FALSE));
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216e9dbad6fSeschrock /*
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 
24929ab75c9Srm static zpool_handle_t *
25029ab75c9Srm zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
25129ab75c9Srm {
25229ab75c9Srm 	libzfs_handle_t *hdl = zhp->zfs_hdl;
25329ab75c9Srm 	zpool_handle_t *zph;
25429ab75c9Srm 
25529ab75c9Srm 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
25629ab75c9Srm 		if (hdl->libzfs_pool_handles != NULL)
25729ab75c9Srm 			zph->zpool_next = hdl->libzfs_pool_handles;
25829ab75c9Srm 		hdl->libzfs_pool_handles = zph;
25929ab75c9Srm 	}
26029ab75c9Srm 	return (zph);
26129ab75c9Srm }
26229ab75c9Srm 
26329ab75c9Srm static zpool_handle_t *
26429ab75c9Srm zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
26529ab75c9Srm {
26629ab75c9Srm 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26729ab75c9Srm 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
26829ab75c9Srm 
26929ab75c9Srm 	while ((zph != NULL) &&
27029ab75c9Srm 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
27129ab75c9Srm 		zph = zph->zpool_next;
27229ab75c9Srm 	return (zph);
27329ab75c9Srm }
27429ab75c9Srm 
27529ab75c9Srm /*
27629ab75c9Srm  * Returns a handle to the pool that contains the provided dataset.
27729ab75c9Srm  * If a handle to that pool already exists then that handle is returned.
27829ab75c9Srm  * Otherwise, a new handle is created and added to the list of handles.
27929ab75c9Srm  */
28029ab75c9Srm static zpool_handle_t *
28129ab75c9Srm zpool_handle(zfs_handle_t *zhp)
28229ab75c9Srm {
28329ab75c9Srm 	char *pool_name;
28429ab75c9Srm 	int len;
28529ab75c9Srm 	zpool_handle_t *zph;
28629ab75c9Srm 
28778f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
28829ab75c9Srm 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
28929ab75c9Srm 	(void) strlcpy(pool_name, zhp->zfs_name, len);
29029ab75c9Srm 
29129ab75c9Srm 	zph = zpool_find_handle(zhp, pool_name, len);
29229ab75c9Srm 	if (zph == NULL)
29329ab75c9Srm 		zph = zpool_add_handle(zhp, pool_name);
29429ab75c9Srm 
29529ab75c9Srm 	free(pool_name);
29629ab75c9Srm 	return (zph);
29729ab75c9Srm }
29829ab75c9Srm 
29929ab75c9Srm void
30029ab75c9Srm zpool_free_handles(libzfs_handle_t *hdl)
30129ab75c9Srm {
30229ab75c9Srm 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
30329ab75c9Srm 
30429ab75c9Srm 	while (zph != NULL) {
30529ab75c9Srm 		next = zph->zpool_next;
30629ab75c9Srm 		zpool_close(zph);
30729ab75c9Srm 		zph = next;
30829ab75c9Srm 	}
30929ab75c9Srm 	hdl->libzfs_pool_handles = NULL;
31029ab75c9Srm }
31129ab75c9Srm 
312fa9e4066Sahrens /*
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 {
3287f7322feSeschrock 			return (-1);
3297f7322feSeschrock 		}
3307f7322feSeschrock 	}
331ebedde84SEric Taylor 	return (0);
332ebedde84SEric Taylor }
333fa9e4066Sahrens 
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;
376fa9e4066Sahrens 
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) {
38099653d4eSeschrock 		return (-1);
38199653d4eSeschrock 	}
382fa9e4066Sahrens 
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);
389e9dbad6fSeschrock 		return (-1);
390fac3008cSeschrock 	}
391fac3008cSeschrock 
392fac3008cSeschrock 	nvlist_free(zhp->zfs_props);
393fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
394fac3008cSeschrock 
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 
5100d8fa8f8SMartin Matuska 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 
52619b94df9SMatthew Ahrens 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 
647edb901aaSMarcel Telka 	/*
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 
665fa9e4066Sahrens /*
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.
690fa9e4066Sahrens 	 */
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.
696edb901aaSMarcel Telka 		 */
697edb901aaSMarcel Telka 		errno = 0;
698edb901aaSMarcel Telka 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
699edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
700edb901aaSMarcel Telka 			return (NULL);
701edb901aaSMarcel Telka 		}
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);
745fa9e4066Sahrens 	}
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);
764e9dbad6fSeschrock 	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) {
885591e0e13SSebastien Roy 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
886591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
887591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
888591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
889591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
890591e0e13SSebastien Roy 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
891591e0e13SSebastien Roy 	}
892591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
893ebedde84SEric Taylor }
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 
9157b97dc1aSrm int
9167b97dc1aSrm zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9177b97dc1aSrm {
91829ab75c9Srm 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9197b97dc1aSrm 
9207b97dc1aSrm 	if (zpool_handle == NULL)
9217b97dc1aSrm 		return (-1);
9227b97dc1aSrm 
9237b97dc1aSrm 	*spa_version = zpool_get_prop_int(zpool_handle,
9247b97dc1aSrm 	    ZPOOL_PROP_VERSION, NULL);
9257b97dc1aSrm 	return (0);
9267b97dc1aSrm }
9277b97dc1aSrm 
9287b97dc1aSrm /*
9297b97dc1aSrm  * The choice of reservation property depends on the SPA version.
9307b97dc1aSrm  */
9317b97dc1aSrm static int
9327b97dc1aSrm zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9337b97dc1aSrm {
9347b97dc1aSrm 	int spa_version;
9357b97dc1aSrm 
9367b97dc1aSrm 	if (zfs_spa_version(zhp, &spa_version) < 0)
9377b97dc1aSrm 		return (-1);
9387b97dc1aSrm 
9397b97dc1aSrm 	if (spa_version >= SPA_VERSION_REFRESERVATION)
9407b97dc1aSrm 		*resv_prop = ZFS_PROP_REFRESERVATION;
9417b97dc1aSrm 	else
9427b97dc1aSrm 		*resv_prop = ZFS_PROP_RESERVATION;
9437b97dc1aSrm 
9447b97dc1aSrm 	return (0);
9457b97dc1aSrm }
9467b97dc1aSrm 
947e9dbad6fSeschrock /*
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.
951e9dbad6fSeschrock  */
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,
955*eb633035STom Caputi     boolean_t key_params_ok, const char *errbuf)
956e9dbad6fSeschrock {
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;
964e9dbad6fSeschrock 
965990b4856Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
966990b4856Slling 		(void) no_memory(hdl);
967990b4856Slling 		return (NULL);
968e9dbad6fSeschrock 	}
969e9dbad6fSeschrock 
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);
977e9dbad6fSeschrock 
97814843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
97914843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
980990b4856Slling 			/*
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) {
992990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
993990b4856Slling 				    "property name '%s' is too long"),
994990b4856Slling 				    propname);
995990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
996990b4856Slling 				goto error;
997fa9e4066Sahrens 			}
998fa9e4066Sahrens 
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 		}
1006e9dbad6fSeschrock 
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 &&
103614843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
103714843421SMatthew Ahrens 				zfs_error_aux(hdl,
103814843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
103914843421SMatthew Ahrens 				    propname);
104014843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
104114843421SMatthew Ahrens 				    errbuf);
104214843421SMatthew Ahrens 				goto error;
104314843421SMatthew Ahrens 			}
104414843421SMatthew Ahrens 
104514843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
104614843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
104714843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
104814843421SMatthew Ahrens 					intval = 0;
104914843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
105014843421SMatthew Ahrens 				    strval, &intval) != 0) {
105114843421SMatthew Ahrens 					(void) zfs_error(hdl,
105214843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
105314843421SMatthew Ahrens 					goto error;
105414843421SMatthew Ahrens 				}
105514843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
105614843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
105714843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
105814843421SMatthew Ahrens 				if (intval == 0) {
105914843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106014843421SMatthew Ahrens 					    "use 'none' to disable "
106114843421SMatthew Ahrens 					    "userquota/groupquota"));
106214843421SMatthew Ahrens 					goto error;
106314843421SMatthew Ahrens 				}
106414843421SMatthew Ahrens 			} else {
106514843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106614843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
106714843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
106814843421SMatthew Ahrens 				goto error;
106914843421SMatthew Ahrens 			}
107014843421SMatthew Ahrens 
10712d5843dbSMatthew Ahrens 			/*
10722d5843dbSMatthew Ahrens 			 * Encode the prop name as
10732d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
10742d5843dbSMatthew Ahrens 			 * for the kernel to decode.
10752d5843dbSMatthew Ahrens 			 */
107614843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
10772d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10782d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
107914843421SMatthew Ahrens 			valary[0] = uqtype;
108014843421SMatthew Ahrens 			valary[1] = rid;
108114843421SMatthew Ahrens 			valary[2] = intval;
108214843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
108314843421SMatthew Ahrens 			    valary, 3) != 0) {
108414843421SMatthew Ahrens 				(void) no_memory(hdl);
108514843421SMatthew Ahrens 				goto error;
108614843421SMatthew Ahrens 			}
108714843421SMatthew Ahrens 			continue;
108819b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
108919b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109019b94df9SMatthew Ahrens 			    "'%s' is readonly"),
109119b94df9SMatthew Ahrens 			    propname);
109219b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
109319b94df9SMatthew Ahrens 			goto error;
109414843421SMatthew Ahrens 		}
109514843421SMatthew Ahrens 
109614843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
109714843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109814843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
109914843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
110014843421SMatthew Ahrens 			goto error;
110114843421SMatthew Ahrens 		}
110214843421SMatthew Ahrens 
1103e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1104e9dbad6fSeschrock 			zfs_error_aux(hdl,
1105e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1106e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1107e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1108e9dbad6fSeschrock 			goto error;
1109e9dbad6fSeschrock 		}
1110e9dbad6fSeschrock 
1111e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1112*eb633035STom Caputi 		    !(zfs_prop_setonce(prop) && zhp == NULL) &&
1113*eb633035STom Caputi 		    !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1114e9dbad6fSeschrock 			zfs_error_aux(hdl,
1115e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1116e9dbad6fSeschrock 			    propname);
1117e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1118e9dbad6fSeschrock 			goto error;
1119e9dbad6fSeschrock 		}
1120e9dbad6fSeschrock 
1121990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1122990b4856Slling 		    &strval, &intval, errbuf) != 0)
1123e9dbad6fSeschrock 			goto error;
1124fa9e4066Sahrens 
1125e9dbad6fSeschrock 		/*
1126e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1127e9dbad6fSeschrock 		 */
1128e9dbad6fSeschrock 		switch (prop) {
1129e7437265Sahrens 		case ZFS_PROP_VERSION:
1130e7437265Sahrens 		{
1131e7437265Sahrens 			int version;
1132e7437265Sahrens 
1133e7437265Sahrens 			if (zhp == NULL)
1134e7437265Sahrens 				break;
1135e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1136e7437265Sahrens 			if (intval < version) {
1137e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1138e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1139e7437265Sahrens 				    version);
1140e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1141e7437265Sahrens 				goto error;
1142e7437265Sahrens 			}
1143e7437265Sahrens 			break;
1144e7437265Sahrens 		}
1145e7437265Sahrens 
1146e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1147b5152584SMatthew Ahrens 		case ZFS_PROP_RECORDSIZE:
1148b5152584SMatthew Ahrens 		{
1149b5152584SMatthew Ahrens 			int maxbs = SPA_MAXBLOCKSIZE;
1150e9316f76SJoe Stein 			if (zpool_hdl != NULL) {
1151e9316f76SJoe Stein 				maxbs = zpool_get_prop_int(zpool_hdl,
1152b5152584SMatthew Ahrens 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1153b5152584SMatthew Ahrens 			}
1154b5152584SMatthew Ahrens 			/*
1155b5152584SMatthew Ahrens 			 * Volumes are limited to a volblocksize of 128KB,
1156b5152584SMatthew Ahrens 			 * because they typically service workloads with
1157b5152584SMatthew Ahrens 			 * small random writes, which incur a large performance
1158b5152584SMatthew Ahrens 			 * penalty with large blocks.
1159b5152584SMatthew Ahrens 			 */
1160b5152584SMatthew Ahrens 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1161b5152584SMatthew Ahrens 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1162b5152584SMatthew Ahrens 			/*
1163b5152584SMatthew Ahrens 			 * The value must be a power of two between
1164b5152584SMatthew Ahrens 			 * SPA_MINBLOCKSIZE and maxbs.
1165b5152584SMatthew Ahrens 			 */
1166e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1167b5152584SMatthew Ahrens 			    intval > maxbs || !ISP2(intval)) {
116899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1169b5152584SMatthew Ahrens 				    "'%s' must be power of 2 from 512B "
1170b5152584SMatthew Ahrens 				    "to %uKB"), propname, maxbs >> 10);
1171e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1172e9dbad6fSeschrock 				goto error;
1173fa9e4066Sahrens 			}
1174fa9e4066Sahrens 			break;
1175b5152584SMatthew Ahrens 		}
1176663207adSDon Brady 
1177663207adSDon Brady 		case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1178663207adSDon Brady 			if (zpool_hdl != NULL) {
1179663207adSDon Brady 				char state[64] = "";
1180663207adSDon Brady 
1181663207adSDon Brady 				/*
1182663207adSDon Brady 				 * Issue a warning but do not fail so that
1183663207adSDon Brady 				 * tests for setable properties succeed.
1184663207adSDon Brady 				 */
1185663207adSDon Brady 				if (zpool_prop_get_feature(zpool_hdl,
1186663207adSDon Brady 				    "feature@allocation_classes", state,
1187663207adSDon Brady 				    sizeof (state)) != 0 ||
1188663207adSDon Brady 				    strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1189663207adSDon Brady 					(void) fprintf(stderr, gettext(
1190663207adSDon Brady 					    "%s: property requires a special "
1191663207adSDon Brady 					    "device in the pool\n"), propname);
1192663207adSDon Brady 				}
1193663207adSDon Brady 			}
1194663207adSDon Brady 			if (intval != 0 &&
1195663207adSDon Brady 			    (intval < SPA_MINBLOCKSIZE ||
1196663207adSDon Brady 			    intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) {
1197663207adSDon Brady 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198663207adSDon Brady 				    "invalid '%s=%d' property: must be zero or "
1199663207adSDon Brady 				    "a power of 2 from 512B to 128K"), propname,
1200663207adSDon Brady 				    intval);
1201663207adSDon Brady 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1202663207adSDon Brady 				goto error;
1203663207adSDon Brady 			}
1204663207adSDon Brady 			break;
1205663207adSDon Brady 
12064201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
12074201a95eSRic Aleshire 		{
12084201a95eSRic Aleshire 			/*
12094201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
12104201a95eSRic Aleshire 			 * internal hex label string.
12114201a95eSRic Aleshire 			 */
12124201a95eSRic Aleshire 
12134201a95eSRic Aleshire 			m_label_t *new_sl;
12144201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
12154201a95eSRic Aleshire 
12164201a95eSRic Aleshire 			/* Default value is already OK. */
12174201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
12184201a95eSRic Aleshire 				break;
12194201a95eSRic Aleshire 
12204201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
12214201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
12224201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
12234201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
12244201a95eSRic Aleshire 				goto badlabel;
12254201a95eSRic Aleshire 			}
12264201a95eSRic Aleshire 
12274201a95eSRic Aleshire 			/* Now translate to hex internal label string */
12284201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
12294201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
12304201a95eSRic Aleshire 				if (hex)
12314201a95eSRic Aleshire 					free(hex);
12324201a95eSRic Aleshire 				goto badlabel;
12334201a95eSRic Aleshire 			}
12344201a95eSRic Aleshire 			m_label_free(new_sl);
12354201a95eSRic Aleshire 
12364201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
12374201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
12384201a95eSRic Aleshire 				free(hex);
12394201a95eSRic Aleshire 				break;
12404201a95eSRic Aleshire 			}
12414201a95eSRic Aleshire 
12424201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1243569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
12444201a95eSRic Aleshire 			    DATA_TYPE_STRING);
12454201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12464201a95eSRic Aleshire 			    hex) == 0);
12474201a95eSRic Aleshire 			free(hex);
12484201a95eSRic Aleshire 
12494201a95eSRic Aleshire 			break;
12504201a95eSRic Aleshire 
12514201a95eSRic Aleshire badlabel:
12524201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12534201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
12544201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12554201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
12564201a95eSRic Aleshire 			goto error;
12574201a95eSRic Aleshire 
12584201a95eSRic Aleshire 		}
12594201a95eSRic Aleshire 
1260e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
126189eef05eSrm 		{
126289eef05eSrm 			namecheck_err_t why;
126389eef05eSrm 
1264e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1265e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1266e9dbad6fSeschrock 				break;
1267fa9e4066Sahrens 
126889eef05eSrm 			if (mountpoint_namecheck(strval, &why)) {
126989eef05eSrm 				switch (why) {
127089eef05eSrm 				case NAME_ERR_LEADING_SLASH:
127189eef05eSrm 					zfs_error_aux(hdl,
127289eef05eSrm 					    dgettext(TEXT_DOMAIN,
127389eef05eSrm 					    "'%s' must be an absolute path, "
127489eef05eSrm 					    "'none', or 'legacy'"), propname);
127589eef05eSrm 					break;
127689eef05eSrm 				case NAME_ERR_TOOLONG:
127789eef05eSrm 					zfs_error_aux(hdl,
127889eef05eSrm 					    dgettext(TEXT_DOMAIN,
127989eef05eSrm 					    "component of '%s' is too long"),
128089eef05eSrm 					    propname);
128189eef05eSrm 					break;
128288f61deeSIgor Kozhukhov 
128388f61deeSIgor Kozhukhov 				default:
128488f61deeSIgor Kozhukhov 					zfs_error_aux(hdl,
128588f61deeSIgor Kozhukhov 					    dgettext(TEXT_DOMAIN,
128688f61deeSIgor Kozhukhov 					    "(%d) not defined"),
128788f61deeSIgor Kozhukhov 					    why);
128888f61deeSIgor Kozhukhov 					break;
128989eef05eSrm 				}
1290e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1291e9dbad6fSeschrock 				goto error;
1292fa9e4066Sahrens 			}
129389eef05eSrm 		}
129489eef05eSrm 
1295f3861e1aSahl 			/*FALLTHRU*/
1296fa9e4066Sahrens 
1297da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1298f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1299f3861e1aSahl 			/*
1300da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1301da6c28aaSamw 			 * properties, check if it can be set in a
1302da6c28aaSamw 			 * global/non-global zone based on
1303f3861e1aSahl 			 * the zoned property value:
1304f3861e1aSahl 			 *
1305f3861e1aSahl 			 *		global zone	    non-global zone
1306f3861e1aSahl 			 * --------------------------------------------------
1307f3861e1aSahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1308f3861e1aSahl 			 *		sharenfs (no)	    sharenfs (no)
1309da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1310f3861e1aSahl 			 *
1311f3861e1aSahl 			 * zoned=off	mountpoint (yes)	N/A
1312f3861e1aSahl 			 *		sharenfs (yes)
1313da6c28aaSamw 			 *		sharesmb (yes)
1314f3861e1aSahl 			 */
1315e9dbad6fSeschrock 			if (zoned) {
1316e9dbad6fSeschrock 				if (getzoneid() == GLOBAL_ZONEID) {
1317e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1318e9dbad6fSeschrock 					    "'%s' cannot be set on "
1319e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1320e9dbad6fSeschrock 					    propname);
1321e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1322e9dbad6fSeschrock 					    errbuf);
1323e9dbad6fSeschrock 					goto error;
1324da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1325da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
1326e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1327e9dbad6fSeschrock 					    "'%s' cannot be set in "
1328e9dbad6fSeschrock 					    "a non-global zone"), propname);
1329e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1330e9dbad6fSeschrock 					    errbuf);
1331e9dbad6fSeschrock 					goto error;
1332fa9e4066Sahrens 				}
1333e9dbad6fSeschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
1334e9dbad6fSeschrock 				/*
1335e9dbad6fSeschrock 				 * If zoned property is 'off', this must be in
133614843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1337e9dbad6fSeschrock 				 */
1338e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1340e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1341e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1342e9dbad6fSeschrock 				goto error;
1343fa9e4066Sahrens 			}
1344f3861e1aSahl 
134567331909Sdougm 			/*
134667331909Sdougm 			 * At this point, it is legitimate to set the
134767331909Sdougm 			 * property. Now we want to make sure that the
134867331909Sdougm 			 * property value is valid if it is sharenfs.
134967331909Sdougm 			 */
1350da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1351da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
1352fac3008cSeschrock 			    strcmp(strval, "on") != 0 &&
1353fac3008cSeschrock 			    strcmp(strval, "off") != 0) {
1354da6c28aaSamw 				zfs_share_proto_t proto;
1355da6c28aaSamw 
1356da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1357da6c28aaSamw 					proto = PROTO_SMB;
1358da6c28aaSamw 				else
1359da6c28aaSamw 					proto = PROTO_NFS;
136067331909Sdougm 
136167331909Sdougm 				/*
1362da6c28aaSamw 				 * Must be an valid sharing protocol
1363da6c28aaSamw 				 * option string so init the libshare
1364da6c28aaSamw 				 * in order to enable the parser and
1365da6c28aaSamw 				 * then parse the options. We use the
1366da6c28aaSamw 				 * control API since we don't care about
1367da6c28aaSamw 				 * the current configuration and don't
136867331909Sdougm 				 * want the overhead of loading it
136967331909Sdougm 				 * until we actually do something.
137067331909Sdougm 				 */
137167331909Sdougm 
1372fac3008cSeschrock 				if (zfs_init_libshare(hdl,
1373fac3008cSeschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
1374fac3008cSeschrock 					/*
1375fac3008cSeschrock 					 * An error occurred so we can't do
1376fac3008cSeschrock 					 * anything
1377fac3008cSeschrock 					 */
1378fac3008cSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1379fac3008cSeschrock 					    "'%s' cannot be set: problem "
1380fac3008cSeschrock 					    "in share initialization"),
1381fac3008cSeschrock 					    propname);
1382fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1383fac3008cSeschrock 					    errbuf);
1384fac3008cSeschrock 					goto error;
1385fac3008cSeschrock 				}
138667331909Sdougm 
1387da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
1388fac3008cSeschrock 					/*
1389fac3008cSeschrock 					 * There was an error in parsing so
1390fac3008cSeschrock 					 * deal with it by issuing an error
1391fac3008cSeschrock 					 * message and leaving after
1392fac3008cSeschrock 					 * uninitializing the the libshare
1393fac3008cSeschrock 					 * interface.
1394fac3008cSeschrock 					 */
1395fac3008cSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1396fac3008cSeschrock 					    "'%s' cannot be set to invalid "
1397fac3008cSeschrock 					    "options"), propname);
1398fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1399fac3008cSeschrock 					    errbuf);
1400fac3008cSeschrock 					zfs_uninit_libshare(hdl);
1401fac3008cSeschrock 					goto error;
1402fac3008cSeschrock 				}
140367331909Sdougm 				zfs_uninit_libshare(hdl);
140467331909Sdougm 			}
140567331909Sdougm 
1406da6c28aaSamw 			break;
140788f61deeSIgor Kozhukhov 
1408*eb633035STom Caputi 		case ZFS_PROP_KEYLOCATION:
1409*eb633035STom Caputi 			if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1410*eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1411*eb633035STom Caputi 				    "invalid keylocation"));
1412*eb633035STom Caputi 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1413*eb633035STom Caputi 				goto error;
1414*eb633035STom Caputi 			}
1415*eb633035STom Caputi 
1416*eb633035STom Caputi 			if (zhp != NULL) {
1417*eb633035STom Caputi 				uint64_t crypt =
1418*eb633035STom Caputi 				    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1419*eb633035STom Caputi 
1420*eb633035STom Caputi 				if (crypt == ZIO_CRYPT_OFF &&
1421*eb633035STom Caputi 				    strcmp(strval, "none") != 0) {
1422*eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1423*eb633035STom Caputi 					    "keylocation must be 'none' "
1424*eb633035STom Caputi 					    "for unencrypted datasets"));
1425*eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1426*eb633035STom Caputi 					    errbuf);
1427*eb633035STom Caputi 					goto error;
1428*eb633035STom Caputi 				} else if (crypt != ZIO_CRYPT_OFF &&
1429*eb633035STom Caputi 				    strcmp(strval, "none") == 0) {
1430*eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1431*eb633035STom Caputi 					    "keylocation must not be 'none' "
1432*eb633035STom Caputi 					    "for encrypted datasets"));
1433*eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1434*eb633035STom Caputi 					    errbuf);
1435*eb633035STom Caputi 					goto error;
1436*eb633035STom Caputi 				}
1437*eb633035STom Caputi 			}
1438*eb633035STom Caputi 			break;
1439*eb633035STom Caputi 
1440*eb633035STom Caputi 		case ZFS_PROP_PBKDF2_ITERS:
1441*eb633035STom Caputi 			if (intval < MIN_PBKDF2_ITERATIONS) {
1442*eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1443*eb633035STom Caputi 				    "minimum pbkdf2 iterations is %u"),
1444*eb633035STom Caputi 				    MIN_PBKDF2_ITERATIONS);
1445*eb633035STom Caputi 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1446*eb633035STom Caputi 				goto error;
1447*eb633035STom Caputi 			}
1448*eb633035STom Caputi 			break;
1449*eb633035STom Caputi 
1450da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1451da6c28aaSamw 			chosen_utf = (int)intval;
1452da6c28aaSamw 			break;
145388f61deeSIgor Kozhukhov 
1454da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1455da6c28aaSamw 			chosen_normal = (int)intval;
1456f3861e1aSahl 			break;
145788f61deeSIgor Kozhukhov 
145888f61deeSIgor Kozhukhov 		default:
145988f61deeSIgor Kozhukhov 			break;
1460e9dbad6fSeschrock 		}
1461fa9e4066Sahrens 
1462e9dbad6fSeschrock 		/*
1463e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1464e9dbad6fSeschrock 		 * checks to enforce.
1465e9dbad6fSeschrock 		 */
1466e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1467e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1468e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1469e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1470e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1471e9dbad6fSeschrock 			char buf[64];
1472e9dbad6fSeschrock 
1473e9dbad6fSeschrock 			switch (prop) {
1474e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1475e9dbad6fSeschrock 				if (intval > volsize) {
1476e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1477e9dbad6fSeschrock 					    "'%s' is greater than current "
1478e9dbad6fSeschrock 					    "volume size"), propname);
1479e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1480e9dbad6fSeschrock 					    errbuf);
1481e9dbad6fSeschrock 					goto error;
1482e9dbad6fSeschrock 				}
1483e9dbad6fSeschrock 				break;
1484e9dbad6fSeschrock 
14851c10ae76SMike Gerdts 			case ZFS_PROP_REFRESERVATION:
14861c10ae76SMike Gerdts 				if (intval > volsize && intval != UINT64_MAX) {
14871c10ae76SMike Gerdts 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14881c10ae76SMike Gerdts 					    "'%s' is greater than current "
14891c10ae76SMike Gerdts 					    "volume size"), propname);
14901c10ae76SMike Gerdts 					(void) zfs_error(hdl, EZFS_BADPROP,
14911c10ae76SMike Gerdts 					    errbuf);
14921c10ae76SMike Gerdts 					goto error;
14931c10ae76SMike Gerdts 				}
14941c10ae76SMike Gerdts 				break;
14951c10ae76SMike Gerdts 
1496e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1497e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1498e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1499e9dbad6fSeschrock 					    sizeof (buf));
1500e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1501e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1502e9dbad6fSeschrock 					    "volume block size (%s)"),
1503e9dbad6fSeschrock 					    propname, buf);
1504e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1505e9dbad6fSeschrock 					    errbuf);
1506e9dbad6fSeschrock 					goto error;
1507e9dbad6fSeschrock 				}
1508e9dbad6fSeschrock 
1509e9dbad6fSeschrock 				if (intval == 0) {
1510e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1512e9dbad6fSeschrock 					    propname);
1513e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1514e9dbad6fSeschrock 					    errbuf);
1515e9dbad6fSeschrock 					goto error;
1516e9dbad6fSeschrock 				}
1517f3861e1aSahl 				break;
151888f61deeSIgor Kozhukhov 
151988f61deeSIgor Kozhukhov 			default:
152088f61deeSIgor Kozhukhov 				break;
1521fa9e4066Sahrens 			}
1522e9dbad6fSeschrock 		}
1523*eb633035STom Caputi 
1524*eb633035STom Caputi 		/* check encryption properties */
1525*eb633035STom Caputi 		if (zhp != NULL) {
1526*eb633035STom Caputi 			int64_t crypt = zfs_prop_get_int(zhp,
1527*eb633035STom Caputi 			    ZFS_PROP_ENCRYPTION);
1528*eb633035STom Caputi 
1529*eb633035STom Caputi 			switch (prop) {
1530*eb633035STom Caputi 			case ZFS_PROP_COPIES:
1531*eb633035STom Caputi 				if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1532*eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1533*eb633035STom Caputi 					    "encrypted datasets cannot have "
1534*eb633035STom Caputi 					    "3 copies"));
1535*eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1536*eb633035STom Caputi 					    errbuf);
1537*eb633035STom Caputi 					goto error;
1538*eb633035STom Caputi 				}
1539*eb633035STom Caputi 				break;
1540*eb633035STom Caputi 			default:
1541*eb633035STom Caputi 				break;
1542*eb633035STom Caputi 			}
1543*eb633035STom Caputi 		}
1544e9dbad6fSeschrock 	}
1545fa9e4066Sahrens 
1546da6c28aaSamw 	/*
1547da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1548da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1549da6c28aaSamw 	 *
1550da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1551da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1552da6c28aaSamw 	 */
1553de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1554da6c28aaSamw 		if (nvlist_add_uint64(ret,
1555da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1556da6c28aaSamw 			(void) no_memory(hdl);
1557da6c28aaSamw 			goto error;
1558da6c28aaSamw 		}
1559de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1560da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1561da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1562da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1563da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1564da6c28aaSamw 		goto error;
1565da6c28aaSamw 	}
156636db6475SEric Taylor 	return (ret);
156736db6475SEric Taylor 
156836db6475SEric Taylor error:
156936db6475SEric Taylor 	nvlist_free(ret);
157036db6475SEric Taylor 	return (NULL);
157136db6475SEric Taylor }
157236db6475SEric Taylor 
157336db6475SEric Taylor int
157436db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
157536db6475SEric Taylor {
157636db6475SEric Taylor 	uint64_t old_volsize;
157736db6475SEric Taylor 	uint64_t new_volsize;
157836db6475SEric Taylor 	uint64_t old_reservation;
157936db6475SEric Taylor 	uint64_t new_reservation;
158036db6475SEric Taylor 	zfs_prop_t resv_prop;
1581c61ea566SGeorge Wilson 	nvlist_t *props;
1582b73ccab0SMike Gerdts 	zpool_handle_t *zph = zpool_handle(zhp);
1583da6c28aaSamw 
1584e9dbad6fSeschrock 	/*
1585e9dbad6fSeschrock 	 * If this is an existing volume, and someone is setting the volsize,
1586e9dbad6fSeschrock 	 * make sure that it matches the reservation, or add it if necessary.
1587e9dbad6fSeschrock 	 */
158836db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
158936db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
159036db6475SEric Taylor 		return (-1);
159136db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1592c61ea566SGeorge Wilson 
1593c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1594c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1595c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1596c61ea566SGeorge Wilson 
1597b73ccab0SMike Gerdts 	if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1598c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1599c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1600c61ea566SGeorge Wilson 		fnvlist_free(props);
160136db6475SEric Taylor 		return (0);
1602fa9e4066Sahrens 	}
160336db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1604c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1605c61ea566SGeorge Wilson 		fnvlist_free(props);
160636db6475SEric Taylor 		return (-1);
1607c61ea566SGeorge Wilson 	}
1608b73ccab0SMike Gerdts 	new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1609c61ea566SGeorge Wilson 	fnvlist_free(props);
1610c61ea566SGeorge Wilson 
161136db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
161236db6475SEric Taylor 	    new_reservation) != 0) {
161336db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
161436db6475SEric Taylor 		return (-1);
161536db6475SEric Taylor 	}
161636db6475SEric Taylor 	return (1);
1617fa9e4066Sahrens }
1618fa9e4066Sahrens 
16191c10ae76SMike Gerdts /*
16201c10ae76SMike Gerdts  * Helper for 'zfs {set|clone} refreservation=auto'.  Must be called after
16211c10ae76SMike Gerdts  * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
16221c10ae76SMike Gerdts  * Return codes must match zfs_add_synthetic_resv().
16231c10ae76SMike Gerdts  */
16241c10ae76SMike Gerdts static int
16251c10ae76SMike Gerdts zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
16261c10ae76SMike Gerdts {
16271c10ae76SMike Gerdts 	uint64_t volsize;
16281c10ae76SMike Gerdts 	uint64_t resvsize;
16291c10ae76SMike Gerdts 	zfs_prop_t prop;
16301c10ae76SMike Gerdts 	nvlist_t *props;
16311c10ae76SMike Gerdts 
16321c10ae76SMike Gerdts 	if (!ZFS_IS_VOLUME(zhp)) {
16331c10ae76SMike Gerdts 		return (0);
16341c10ae76SMike Gerdts 	}
16351c10ae76SMike Gerdts 
16361c10ae76SMike Gerdts 	if (zfs_which_resv_prop(zhp, &prop) != 0) {
16371c10ae76SMike Gerdts 		return (-1);
16381c10ae76SMike Gerdts 	}
16391c10ae76SMike Gerdts 
16401c10ae76SMike Gerdts 	if (prop != ZFS_PROP_REFRESERVATION) {
16411c10ae76SMike Gerdts 		return (0);
16421c10ae76SMike Gerdts 	}
16431c10ae76SMike Gerdts 
16441c10ae76SMike Gerdts 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
16451c10ae76SMike Gerdts 		/* No value being set, so it can't be "auto" */
16461c10ae76SMike Gerdts 		return (0);
16471c10ae76SMike Gerdts 	}
16481c10ae76SMike Gerdts 	if (resvsize != UINT64_MAX) {
16491c10ae76SMike Gerdts 		/* Being set to a value other than "auto" */
16501c10ae76SMike Gerdts 		return (0);
16511c10ae76SMike Gerdts 	}
16521c10ae76SMike Gerdts 
16531c10ae76SMike Gerdts 	props = fnvlist_alloc();
16541c10ae76SMike Gerdts 
16551c10ae76SMike Gerdts 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
16561c10ae76SMike Gerdts 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
16571c10ae76SMike Gerdts 
16581c10ae76SMike Gerdts 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
16591c10ae76SMike Gerdts 	    &volsize) != 0) {
16601c10ae76SMike Gerdts 		volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
16611c10ae76SMike Gerdts 	}
16621c10ae76SMike Gerdts 
1663b73ccab0SMike Gerdts 	resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1664b73ccab0SMike Gerdts 	    props);
16651c10ae76SMike Gerdts 	fnvlist_free(props);
16661c10ae76SMike Gerdts 
16671c10ae76SMike Gerdts 	(void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
16681c10ae76SMike Gerdts 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
16691c10ae76SMike Gerdts 		(void) no_memory(zhp->zfs_hdl);
16701c10ae76SMike Gerdts 		return (-1);
16711c10ae76SMike Gerdts 	}
16721c10ae76SMike Gerdts 	return (1);
16731c10ae76SMike Gerdts }
16741c10ae76SMike Gerdts 
167592241e0bSTom Erickson void
167692241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
167792241e0bSTom Erickson     char *errbuf)
167892241e0bSTom Erickson {
167992241e0bSTom Erickson 	switch (err) {
168092241e0bSTom Erickson 
168192241e0bSTom Erickson 	case ENOSPC:
168292241e0bSTom Erickson 		/*
168392241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
168492241e0bSTom Erickson 		 * something different; setting a quota or reservation
168592241e0bSTom Erickson 		 * doesn't use any disk space.
168692241e0bSTom Erickson 		 */
168792241e0bSTom Erickson 		switch (prop) {
168892241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
168992241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
169092241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169192241e0bSTom Erickson 			    "size is less than current used or "
169292241e0bSTom Erickson 			    "reserved space"));
169392241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
169492241e0bSTom Erickson 			break;
169592241e0bSTom Erickson 
169692241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
169792241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
169892241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169992241e0bSTom Erickson 			    "size is greater than available space"));
170092241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
170192241e0bSTom Erickson 			break;
170292241e0bSTom Erickson 
170392241e0bSTom Erickson 		default:
170492241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
170592241e0bSTom Erickson 			break;
170692241e0bSTom Erickson 		}
170792241e0bSTom Erickson 		break;
170892241e0bSTom Erickson 
170992241e0bSTom Erickson 	case EBUSY:
171092241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
171192241e0bSTom Erickson 		break;
171292241e0bSTom Erickson 
171392241e0bSTom Erickson 	case EROFS:
171492241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
171592241e0bSTom Erickson 		break;
171692241e0bSTom Erickson 
17176fdcb3d1SWill Andrews 	case E2BIG:
17186fdcb3d1SWill Andrews 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17196fdcb3d1SWill Andrews 		    "property value too long"));
17206fdcb3d1SWill Andrews 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
17216fdcb3d1SWill Andrews 		break;
17226fdcb3d1SWill Andrews 
172392241e0bSTom Erickson 	case ENOTSUP:
172492241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172592241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
172692241e0bSTom Erickson 		    "property or value"));
172792241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
172892241e0bSTom Erickson 		break;
172992241e0bSTom Erickson 
173092241e0bSTom Erickson 	case ERANGE:
1731b5152584SMatthew Ahrens 		if (prop == ZFS_PROP_COMPRESSION ||
1732b5152584SMatthew Ahrens 		    prop == ZFS_PROP_RECORDSIZE) {
173392241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173492241e0bSTom Erickson 			    "property setting is not allowed on "
173592241e0bSTom Erickson 			    "bootable datasets"));
173692241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
173745818ee1SMatthew Ahrens 		} else if (prop == ZFS_PROP_CHECKSUM ||
173845818ee1SMatthew Ahrens 		    prop == ZFS_PROP_DEDUP) {
173945818ee1SMatthew Ahrens 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174045818ee1SMatthew Ahrens 			    "property setting is not allowed on "
174145818ee1SMatthew Ahrens 			    "root pools"));
174245818ee1SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
174392241e0bSTom Erickson 		} else {
174492241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
174592241e0bSTom Erickson 		}
174692241e0bSTom Erickson 		break;
174792241e0bSTom Erickson 
1748ab003da8SJim Dunham 	case EINVAL:
1749ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1750ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1751ab003da8SJim Dunham 		} else {
1752ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1753ab003da8SJim Dunham 		}
1754ab003da8SJim Dunham 		break;
1755ab003da8SJim Dunham 
1756*eb633035STom Caputi 	case EACCES:
1757*eb633035STom Caputi 		if (prop == ZFS_PROP_KEYLOCATION) {
1758*eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1759*eb633035STom Caputi 			    "keylocation may only be set on encryption roots"));
1760*eb633035STom Caputi 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1761*eb633035STom Caputi 		} else {
1762*eb633035STom Caputi 			(void) zfs_standard_error(hdl, err, errbuf);
1763*eb633035STom Caputi 		}
1764*eb633035STom Caputi 		break;
1765*eb633035STom Caputi 
176692241e0bSTom Erickson 	case EOVERFLOW:
176792241e0bSTom Erickson 		/*
176892241e0bSTom Erickson 		 * This platform can't address a volume this big.
176992241e0bSTom Erickson 		 */
177092241e0bSTom Erickson #ifdef _ILP32
177192241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
177292241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
177392241e0bSTom Erickson 			break;
177492241e0bSTom Erickson 		}
177592241e0bSTom Erickson #endif
177692241e0bSTom Erickson 		/* FALLTHROUGH */
177792241e0bSTom Erickson 	default:
177892241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
177992241e0bSTom Erickson 	}
178092241e0bSTom Erickson }
178192241e0bSTom Erickson 
1782fa9e4066Sahrens /*
1783fa9e4066Sahrens  * Given a property name and value, set the property for the given dataset.
1784fa9e4066Sahrens  */
1785fa9e4066Sahrens int
1786e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1787fa9e4066Sahrens {
1788e9dbad6fSeschrock 	int ret = -1;
178999653d4eSeschrock 	char errbuf[1024];
179099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
179130925561SChris Williamson 	nvlist_t *nvl = NULL;
179299653d4eSeschrock 
179399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1794e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
179599653d4eSeschrock 	    zhp->zfs_name);
179699653d4eSeschrock 
1797e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1798e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1799e9dbad6fSeschrock 		(void) no_memory(hdl);
1800e9dbad6fSeschrock 		goto error;
1801fa9e4066Sahrens 	}
1802fa9e4066Sahrens 
180330925561SChris Williamson 	ret = zfs_prop_set_list(zhp, nvl);
1804990b4856Slling 
180530925561SChris Williamson error:
1806e9dbad6fSeschrock 	nvlist_free(nvl);
180730925561SChris Williamson 	return (ret);
180830925561SChris Williamson }
1809e9dbad6fSeschrock 
1810e9dbad6fSeschrock 
181136db6475SEric Taylor 
181230925561SChris Williamson /*
181330925561SChris Williamson  * Given an nvlist of property names and values, set the properties for the
181430925561SChris Williamson  * given dataset.
181530925561SChris Williamson  */
181630925561SChris Williamson int
181730925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
181830925561SChris Williamson {
181930925561SChris Williamson 	zfs_cmd_t zc = { 0 };
182030925561SChris Williamson 	int ret = -1;
182130925561SChris Williamson 	prop_changelist_t **cls = NULL;
182230925561SChris Williamson 	int cl_idx;
182330925561SChris Williamson 	char errbuf[1024];
182430925561SChris Williamson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
182530925561SChris Williamson 	nvlist_t *nvl;
182630925561SChris Williamson 	int nvl_len;
1827f83b46baSPaul Dagnelie 	int added_resv = 0;
1828fa9e4066Sahrens 
182930925561SChris Williamson 	(void) snprintf(errbuf, sizeof (errbuf),
183030925561SChris Williamson 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
183130925561SChris Williamson 	    zhp->zfs_name);
183230925561SChris Williamson 
183330925561SChris Williamson 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1834e9316f76SJoe Stein 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1835*eb633035STom Caputi 	    B_FALSE, errbuf)) == NULL)
1836fa9e4066Sahrens 		goto error;
1837fa9e4066Sahrens 
18380068372bSMark J Musante 	/*
183930925561SChris Williamson 	 * We have to check for any extra properties which need to be added
184030925561SChris Williamson 	 * before computing the length of the nvlist.
18410068372bSMark J Musante 	 */
184230925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
184330925561SChris Williamson 	    elem != NULL;
184430925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
184530925561SChris Williamson 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
184630925561SChris Williamson 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
184730925561SChris Williamson 			goto error;
184830925561SChris Williamson 		}
18494445fffbSMatthew Ahrens 	}
18501c10ae76SMike Gerdts 
18511c10ae76SMike Gerdts 	if (added_resv != 1 &&
18521c10ae76SMike Gerdts 	    (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
18531c10ae76SMike Gerdts 		goto error;
18541c10ae76SMike Gerdts 	}
18551c10ae76SMike Gerdts 
185630925561SChris Williamson 	/*
185730925561SChris Williamson 	 * Check how many properties we're setting and allocate an array to
185830925561SChris Williamson 	 * store changelist pointers for postfix().
185930925561SChris Williamson 	 */
186030925561SChris Williamson 	nvl_len = 0;
186130925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
186230925561SChris Williamson 	    elem != NULL;
186330925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem))
186430925561SChris Williamson 		nvl_len++;
186530925561SChris Williamson 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
18660068372bSMark J Musante 		goto error;
1867fa9e4066Sahrens 
186830925561SChris Williamson 	cl_idx = 0;
186930925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
187030925561SChris Williamson 	    elem != NULL;
187130925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
187230925561SChris Williamson 
187330925561SChris Williamson 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
187430925561SChris Williamson 
187530925561SChris Williamson 		assert(cl_idx < nvl_len);
187630925561SChris Williamson 		/*
187730925561SChris Williamson 		 * We don't want to unmount & remount the dataset when changing
187830925561SChris Williamson 		 * its canmount property to 'on' or 'noauto'.  We only use
187930925561SChris Williamson 		 * the changelist logic to unmount when setting canmount=off.
188030925561SChris Williamson 		 */
1881c079fa4dSAndriy Gapon 		if (prop != ZFS_PROP_CANMOUNT ||
1882c079fa4dSAndriy Gapon 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1883c079fa4dSAndriy Gapon 		    zfs_is_mounted(zhp, NULL))) {
188430925561SChris Williamson 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
188530925561SChris Williamson 			if (cls[cl_idx] == NULL)
188630925561SChris Williamson 				goto error;
188730925561SChris Williamson 		}
188830925561SChris Williamson 
188930925561SChris Williamson 		if (prop == ZFS_PROP_MOUNTPOINT &&
189030925561SChris Williamson 		    changelist_haszonedchild(cls[cl_idx])) {
189130925561SChris Williamson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
189230925561SChris Williamson 			    "child dataset with inherited mountpoint is used "
189330925561SChris Williamson 			    "in a non-global zone"));
189430925561SChris Williamson 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
189530925561SChris Williamson 			goto error;
189630925561SChris Williamson 		}
189730925561SChris Williamson 
189830925561SChris Williamson 		if (cls[cl_idx] != NULL &&
189930925561SChris Williamson 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
190030925561SChris Williamson 			goto error;
190130925561SChris Williamson 
190230925561SChris Williamson 		cl_idx++;
190330925561SChris Williamson 	}
190430925561SChris Williamson 	assert(cl_idx == nvl_len);
190530925561SChris Williamson 
1906fa9e4066Sahrens 	/*
190730925561SChris Williamson 	 * Execute the corresponding ioctl() to set this list of properties.
1908fa9e4066Sahrens 	 */
1909fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1910fa9e4066Sahrens 
191130925561SChris Williamson 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
191230925561SChris Williamson 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1913e9dbad6fSeschrock 		goto error;
1914e9dbad6fSeschrock 
1915ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1916743a77edSAlan Wright 
1917fa9e4066Sahrens 	if (ret != 0) {
1918f62db44dSAndrew Stormont 		if (zc.zc_nvlist_dst_filled == B_FALSE) {
1919f62db44dSAndrew Stormont 			(void) zfs_standard_error(hdl, errno, errbuf);
1920f62db44dSAndrew Stormont 			goto error;
1921f62db44dSAndrew Stormont 		}
1922f62db44dSAndrew Stormont 
192330925561SChris Williamson 		/* Get the list of unset properties back and report them. */
192430925561SChris Williamson 		nvlist_t *errorprops = NULL;
192530925561SChris Williamson 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
192630925561SChris Williamson 			goto error;
1927f62db44dSAndrew Stormont 		for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
192830925561SChris Williamson 		    elem != NULL;
1929f62db44dSAndrew Stormont 		    elem = nvlist_next_nvpair(errorprops, elem)) {
193030925561SChris Williamson 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
193130925561SChris Williamson 			zfs_setprop_error(hdl, prop, errno, errbuf);
193230925561SChris Williamson 		}
193330925561SChris Williamson 		nvlist_free(errorprops);
193430925561SChris Williamson 
193536db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
193636db6475SEric Taylor 			/* clean up the volsize property we tried to set */
193736db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
193836db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
193936db6475SEric Taylor 			nvlist_free(nvl);
194030925561SChris Williamson 			nvl = NULL;
194136db6475SEric Taylor 			zcmd_free_nvlists(&zc);
194230925561SChris Williamson 
194336db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
194436db6475SEric Taylor 				goto error;
194536db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
194636db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
194736db6475SEric Taylor 			    old_volsize) != 0)
194836db6475SEric Taylor 				goto error;
194936db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
195036db6475SEric Taylor 				goto error;
195136db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
195236db6475SEric Taylor 		}
1953fa9e4066Sahrens 	} else {
195430925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
195530925561SChris Williamson 			if (cls[cl_idx] != NULL) {
195630925561SChris Williamson 				int clp_err = changelist_postfix(cls[cl_idx]);
195730925561SChris Williamson 				if (clp_err != 0)
195830925561SChris Williamson 					ret = clp_err;
195930925561SChris Williamson 			}
196030925561SChris Williamson 		}
1961a227b7f4Shs 
1962fa9e4066Sahrens 		/*
1963fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1964fa9e4066Sahrens 		 * is reflected.
1965fa9e4066Sahrens 		 */
1966a227b7f4Shs 		if (ret == 0)
1967e9dbad6fSeschrock 			(void) get_stats(zhp);
1968fa9e4066Sahrens 	}
1969fa9e4066Sahrens 
1970fa9e4066Sahrens error:
1971e9dbad6fSeschrock 	nvlist_free(nvl);
1972e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
197330925561SChris Williamson 	if (cls != NULL) {
197430925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
197530925561SChris Williamson 			if (cls[cl_idx] != NULL)
197630925561SChris Williamson 				changelist_free(cls[cl_idx]);
197730925561SChris Williamson 		}
197830925561SChris Williamson 		free(cls);
197930925561SChris Williamson 	}
1980fa9e4066Sahrens 	return (ret);
1981fa9e4066Sahrens }
1982fa9e4066Sahrens 
1983fa9e4066Sahrens /*
198492241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
198592241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1986fa9e4066Sahrens  */
1987fa9e4066Sahrens int
198892241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1989fa9e4066Sahrens {
1990fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1991fa9e4066Sahrens 	int ret;
1992fa9e4066Sahrens 	prop_changelist_t *cl;
199399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
199499653d4eSeschrock 	char errbuf[1024];
1995e9dbad6fSeschrock 	zfs_prop_t prop;
199699653d4eSeschrock 
199799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
199899653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1999fa9e4066Sahrens 
200092241e0bSTom Erickson 	zc.zc_cookie = received;
2001990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2002e9dbad6fSeschrock 		/*
2003e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
2004e9dbad6fSeschrock 		 * small, so just do it here.
2005e9dbad6fSeschrock 		 */
2006e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
2007e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2008e9dbad6fSeschrock 			    "invalid property"));
2009e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2010e9dbad6fSeschrock 		}
2011e9dbad6fSeschrock 
2012e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2013e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2014e9dbad6fSeschrock 
2015e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2016e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2017e9dbad6fSeschrock 
2018e9dbad6fSeschrock 		return (0);
2019e9dbad6fSeschrock 	}
2020e9dbad6fSeschrock 
2021fa9e4066Sahrens 	/*
2022fa9e4066Sahrens 	 * Verify that this property is inheritable.
2023fa9e4066Sahrens 	 */
202499653d4eSeschrock 	if (zfs_prop_readonly(prop))
202599653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2026fa9e4066Sahrens 
202792241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
202899653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2029fa9e4066Sahrens 
2030fa9e4066Sahrens 	/*
2031fa9e4066Sahrens 	 * Check to see if the value applies to this type
2032fa9e4066Sahrens 	 */
203399653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
203499653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2035fa9e4066Sahrens 
2036bf7c2d40Srm 	/*
203736db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
2038bf7c2d40Srm 	 */
2039bf7c2d40Srm 	propname = zfs_prop_to_name(prop);
2040fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2041e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2042fa9e4066Sahrens 
2043fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2044fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
204599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
204699653d4eSeschrock 		    "dataset is used in a non-global zone"));
204799653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2048fa9e4066Sahrens 	}
2049fa9e4066Sahrens 
2050fa9e4066Sahrens 	/*
2051fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
2052fa9e4066Sahrens 	 */
20530069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2054fa9e4066Sahrens 		return (-1);
2055fa9e4066Sahrens 
2056fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
205799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205899653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
205999653d4eSeschrock 		    "in a non-global zone"));
206099653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2061fa9e4066Sahrens 		goto error;
2062fa9e4066Sahrens 	}
2063fa9e4066Sahrens 
2064fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
2065fa9e4066Sahrens 		goto error;
2066fa9e4066Sahrens 
2067e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
206899653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2069fa9e4066Sahrens 	} else {
2070fa9e4066Sahrens 
2071efc555ebSnd 		if ((ret = changelist_postfix(cl)) != 0)
2072fa9e4066Sahrens 			goto error;
2073fa9e4066Sahrens 
2074fa9e4066Sahrens 		/*
2075fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
2076fa9e4066Sahrens 		 */
2077fa9e4066Sahrens 		(void) get_stats(zhp);
2078fa9e4066Sahrens 	}
2079fa9e4066Sahrens 
2080fa9e4066Sahrens error:
2081fa9e4066Sahrens 	changelist_free(cl);
2082fa9e4066Sahrens 	return (ret);
2083fa9e4066Sahrens }
2084fa9e4066Sahrens 
20857f7322feSeschrock /*
20867f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
20877f7322feSeschrock  * extract them appropriately.
20887f7322feSeschrock  */
20897f7322feSeschrock static uint64_t
20907f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20917f7322feSeschrock {
20927f7322feSeschrock 	nvlist_t *nv;
20937f7322feSeschrock 	uint64_t value;
20947f7322feSeschrock 
2095a2eea2e1Sahrens 	*source = NULL;
20967f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20977f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
2098990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2099990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21007f7322feSeschrock 	} else {
21012e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
21022e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
21037f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
21047f7322feSeschrock 		*source = "";
21057f7322feSeschrock 	}
21067f7322feSeschrock 
21077f7322feSeschrock 	return (value);
21087f7322feSeschrock }
21097f7322feSeschrock 
21109c3fd121SMatthew Ahrens static const char *
21117f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21127f7322feSeschrock {
21137f7322feSeschrock 	nvlist_t *nv;
21149c3fd121SMatthew Ahrens 	const char *value;
21157f7322feSeschrock 
2116a2eea2e1Sahrens 	*source = NULL;
21177f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21187f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21199c3fd121SMatthew Ahrens 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2120990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21217f7322feSeschrock 	} else {
21222e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
21232e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
21249c3fd121SMatthew Ahrens 		value = zfs_prop_default_string(prop);
21257f7322feSeschrock 		*source = "";
21267f7322feSeschrock 	}
21277f7322feSeschrock 
21287f7322feSeschrock 	return (value);
21297f7322feSeschrock }
21307f7322feSeschrock 
213192241e0bSTom Erickson static boolean_t
213292241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
213392241e0bSTom Erickson {
213492241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
213592241e0bSTom Erickson }
213692241e0bSTom Erickson 
213792241e0bSTom Erickson static void
213892241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
213992241e0bSTom Erickson {
214092241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
214192241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
214292241e0bSTom Erickson }
214392241e0bSTom Erickson 
214492241e0bSTom Erickson static void
214592241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
214692241e0bSTom Erickson {
214792241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
214892241e0bSTom Erickson 	*cookie = 0;
214992241e0bSTom Erickson }
215092241e0bSTom Erickson 
2151fa9e4066Sahrens /*
2152fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2153fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
2154fa9e4066Sahrens  *
2155fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2156fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2157fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
2158fa9e4066Sahrens  * the source "temporary".
2159fa9e4066Sahrens  */
216099653d4eSeschrock static int
2161990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
216299653d4eSeschrock     char **source, uint64_t *val)
2163fa9e4066Sahrens {
2164bd00f61bSrm 	zfs_cmd_t zc = { 0 };
216596510749Stimh 	nvlist_t *zplprops = NULL;
2166fa9e4066Sahrens 	struct mnttab mnt;
21673ccfa83cSahrens 	char *mntopt_on = NULL;
21683ccfa83cSahrens 	char *mntopt_off = NULL;
216992241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2170fa9e4066Sahrens 
2171fa9e4066Sahrens 	*source = NULL;
2172fa9e4066Sahrens 
21733ccfa83cSahrens 	switch (prop) {
21743ccfa83cSahrens 	case ZFS_PROP_ATIME:
21753ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
21763ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
21773ccfa83cSahrens 		break;
21783ccfa83cSahrens 
21793ccfa83cSahrens 	case ZFS_PROP_DEVICES:
21803ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
21813ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
21823ccfa83cSahrens 		break;
21833ccfa83cSahrens 
21843ccfa83cSahrens 	case ZFS_PROP_EXEC:
21853ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
21863ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
21873ccfa83cSahrens 		break;
21883ccfa83cSahrens 
21893ccfa83cSahrens 	case ZFS_PROP_READONLY:
21903ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
21913ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
21923ccfa83cSahrens 		break;
21933ccfa83cSahrens 
21943ccfa83cSahrens 	case ZFS_PROP_SETUID:
21953ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
21963ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
21973ccfa83cSahrens 		break;
21983ccfa83cSahrens 
21993ccfa83cSahrens 	case ZFS_PROP_XATTR:
22003ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
22013ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
22023ccfa83cSahrens 		break;
2203da6c28aaSamw 
2204da6c28aaSamw 	case ZFS_PROP_NBMAND:
2205da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
2206da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2207da6c28aaSamw 		break;
220888f61deeSIgor Kozhukhov 
220988f61deeSIgor Kozhukhov 	default:
221088f61deeSIgor Kozhukhov 		break;
22113ccfa83cSahrens 	}
22123ccfa83cSahrens 
22133bb79becSeschrock 	/*
22143bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
22153bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
22163bb79becSeschrock 	 * we're looking up a property which requires its presence.
22173bb79becSeschrock 	 */
22183bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
22193ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2220ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2221ebedde84SEric Taylor 		struct mnttab entry;
22223bb79becSeschrock 
2223ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2224ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
22253ccfa83cSahrens 			    entry.mnt_mntopts);
22263ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
22273ccfa83cSahrens 				return (-1);
22283ccfa83cSahrens 		}
22293bb79becSeschrock 
22303bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
22313bb79becSeschrock 	}
22323bb79becSeschrock 
2233fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2234fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2235fa9e4066Sahrens 	else
2236fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2237fa9e4066Sahrens 
2238fa9e4066Sahrens 	switch (prop) {
2239fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2240fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2241fa9e4066Sahrens 	case ZFS_PROP_EXEC:
22423ccfa83cSahrens 	case ZFS_PROP_READONLY:
22433ccfa83cSahrens 	case ZFS_PROP_SETUID:
22443ccfa83cSahrens 	case ZFS_PROP_XATTR:
2245da6c28aaSamw 	case ZFS_PROP_NBMAND:
224699653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2247fa9e4066Sahrens 
224892241e0bSTom Erickson 		if (received)
224992241e0bSTom Erickson 			break;
225092241e0bSTom Erickson 
22513ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
225299653d4eSeschrock 			*val = B_TRUE;
2253fa9e4066Sahrens 			if (src)
2254990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
22553ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
225699653d4eSeschrock 			*val = B_FALSE;
2257fa9e4066Sahrens 			if (src)
2258990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2259fa9e4066Sahrens 		}
226099653d4eSeschrock 		break;
2261fa9e4066Sahrens 
22623ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
2263d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
2264fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2265a9799022Sck 	case ZFS_PROP_REFQUOTA:
2266fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2267a9799022Sck 	case ZFS_PROP_REFRESERVATION:
2268a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2269a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2270a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2271a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2272a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
227392241e0bSTom Erickson 
227492241e0bSTom Erickson 		if (*source == NULL) {
227592241e0bSTom Erickson 			/* not default, must be local */
2276fa9e4066Sahrens 			*source = zhp->zfs_name;
227792241e0bSTom Erickson 		}
227899653d4eSeschrock 		break;
2279fa9e4066Sahrens 
2280fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
228199653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
228299653d4eSeschrock 		break;
2283fa9e4066Sahrens 
228439c23413Seschrock 	case ZFS_PROP_NUMCLONES:
228539c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
228639c23413Seschrock 		break;
228739c23413Seschrock 
2288bd00f61bSrm 	case ZFS_PROP_VERSION:
2289de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2290de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2291de8267e0Stimh 	case ZFS_PROP_CASE:
2292de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2293de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22943d7934e1Srm 			return (-1);
2295bd00f61bSrm 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2296de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2297de8267e0Stimh 			zcmd_free_nvlists(&zc);
2298f4b94bdeSMatthew Ahrens 			return (-1);
2299bd00f61bSrm 		}
2300de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2301de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2302de8267e0Stimh 		    val) != 0) {
2303de8267e0Stimh 			zcmd_free_nvlists(&zc);
2304f4b94bdeSMatthew Ahrens 			return (-1);
2305de8267e0Stimh 		}
2306aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(zplprops);
2307de8267e0Stimh 		zcmd_free_nvlists(&zc);
2308bd00f61bSrm 		break;
2309bd00f61bSrm 
2310ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
2311ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
2312ca48f36fSKeith M Wesolowski 		break;
2313ca48f36fSKeith M Wesolowski 
2314fa9e4066Sahrens 	default:
2315e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
231691ebeef5Sahrens 		case PROP_TYPE_NUMBER:
231791ebeef5Sahrens 		case PROP_TYPE_INDEX:
2318e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
231974e7dc98SMatthew Ahrens 			/*
232014843421SMatthew Ahrens 			 * If we tried to use a default value for a
232174e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
23224d86c0eaSMatthew Ahrens 			 * present.  Note this only applies to "truly"
23234d86c0eaSMatthew Ahrens 			 * readonly properties, not set-once properties
23244d86c0eaSMatthew Ahrens 			 * like volblocksize.
232574e7dc98SMatthew Ahrens 			 */
232674e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
23274d86c0eaSMatthew Ahrens 			    !zfs_prop_setonce(prop) &&
232831f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
232931f572c2STom Erickson 				*source = NULL;
2330ad2760acSMatthew Ahrens 				return (-1);
233174e7dc98SMatthew Ahrens 			}
2332e7437265Sahrens 			break;
2333e7437265Sahrens 
233491ebeef5Sahrens 		case PROP_TYPE_STRING:
2335e7437265Sahrens 		default:
2336e7437265Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2337e7437265Sahrens 			    "cannot get non-numeric property"));
2338e7437265Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2339e7437265Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
2340e7437265Sahrens 		}
2341fa9e4066Sahrens 	}
2342fa9e4066Sahrens 
2343fa9e4066Sahrens 	return (0);
2344fa9e4066Sahrens }
2345fa9e4066Sahrens 
2346fa9e4066Sahrens /*
2347fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2348fa9e4066Sahrens  */
2349fa9e4066Sahrens static void
2350990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2351fa9e4066Sahrens     char *statbuf, size_t statlen)
2352fa9e4066Sahrens {
2353990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2354fa9e4066Sahrens 		return;
2355fa9e4066Sahrens 
2356fa9e4066Sahrens 	if (source == NULL) {
2357990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2358fa9e4066Sahrens 	} else if (source[0] == '\0') {
2359990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
236092241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
236192241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
2362fa9e4066Sahrens 	} else {
2363fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2364990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2365fa9e4066Sahrens 		} else {
2366fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2367990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2368fa9e4066Sahrens 		}
2369fa9e4066Sahrens 	}
2370fa9e4066Sahrens 
2371fa9e4066Sahrens }
2372fa9e4066Sahrens 
237392241e0bSTom Erickson int
237492241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
237592241e0bSTom Erickson     size_t proplen, boolean_t literal)
237692241e0bSTom Erickson {
237792241e0bSTom Erickson 	zfs_prop_t prop;
237892241e0bSTom Erickson 	int err = 0;
237992241e0bSTom Erickson 
238092241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
238192241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
238292241e0bSTom Erickson 			return (-1);
238392241e0bSTom Erickson 
238492241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
238592241e0bSTom Erickson 
238692241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
238792241e0bSTom Erickson 		uint64_t cookie;
238892241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
238992241e0bSTom Erickson 			return (-1);
239092241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
239192241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
239292241e0bSTom Erickson 		    NULL, NULL, 0, literal);
239392241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
239492241e0bSTom Erickson 	} else {
239592241e0bSTom Erickson 		nvlist_t *propval;
239692241e0bSTom Erickson 		char *recvdval;
239792241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
239892241e0bSTom Erickson 		    propname, &propval) != 0)
239992241e0bSTom Erickson 			return (-1);
240092241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
240192241e0bSTom Erickson 		    &recvdval) == 0);
240292241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
240392241e0bSTom Erickson 	}
240492241e0bSTom Erickson 
240592241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
240692241e0bSTom Erickson }
240792241e0bSTom Erickson 
240819b94df9SMatthew Ahrens static int
240919b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
241019b94df9SMatthew Ahrens {
241119b94df9SMatthew Ahrens 	nvlist_t *value;
241219b94df9SMatthew Ahrens 	nvpair_t *pair;
241319b94df9SMatthew Ahrens 
241419b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
241519b94df9SMatthew Ahrens 	if (value == NULL)
241619b94df9SMatthew Ahrens 		return (-1);
241719b94df9SMatthew Ahrens 
241819b94df9SMatthew Ahrens 	propbuf[0] = '\0';
241919b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
242019b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
242119b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
242219b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
242319b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
242419b94df9SMatthew Ahrens 	}
242519b94df9SMatthew Ahrens 
242619b94df9SMatthew Ahrens 	return (0);
242719b94df9SMatthew Ahrens }
242819b94df9SMatthew Ahrens 
242919b94df9SMatthew Ahrens struct get_clones_arg {
243019b94df9SMatthew Ahrens 	uint64_t numclones;
243119b94df9SMatthew Ahrens 	nvlist_t *value;
243219b94df9SMatthew Ahrens 	const char *origin;
24339adfa60dSMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
243419b94df9SMatthew Ahrens };
243519b94df9SMatthew Ahrens 
243619b94df9SMatthew Ahrens int
243719b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
243819b94df9SMatthew Ahrens {
243919b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
244019b94df9SMatthew Ahrens 
244119b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
244219b94df9SMatthew Ahrens 		zfs_close(zhp);
244319b94df9SMatthew Ahrens 		return (0);
244419b94df9SMatthew Ahrens 	}
244519b94df9SMatthew Ahrens 
244619b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
244719b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
244819b94df9SMatthew Ahrens 		goto out;
244919b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
24503b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
245119b94df9SMatthew Ahrens 		gca->numclones--;
245219b94df9SMatthew Ahrens 	}
245319b94df9SMatthew Ahrens 
245419b94df9SMatthew Ahrens out:
245519b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
245619b94df9SMatthew Ahrens 	zfs_close(zhp);
245719b94df9SMatthew Ahrens 	return (0);
245819b94df9SMatthew Ahrens }
245919b94df9SMatthew Ahrens 
246019b94df9SMatthew Ahrens nvlist_t *
246119b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
246219b94df9SMatthew Ahrens {
246319b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
246419b94df9SMatthew Ahrens 
246519b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
246619b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
246719b94df9SMatthew Ahrens 		struct get_clones_arg gca;
246819b94df9SMatthew Ahrens 
246919b94df9SMatthew Ahrens 		/*
247019b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
247119b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
247219b94df9SMatthew Ahrens 		 */
247319b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
247419b94df9SMatthew Ahrens 			return (NULL);
247519b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
247619b94df9SMatthew Ahrens 			return (NULL);
247719b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
247819b94df9SMatthew Ahrens 			nvlist_free(nv);
247919b94df9SMatthew Ahrens 			return (NULL);
248019b94df9SMatthew Ahrens 		}
248119b94df9SMatthew Ahrens 
248219b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
248319b94df9SMatthew Ahrens 		gca.value = value;
248419b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
248519b94df9SMatthew Ahrens 
248619b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
248719b94df9SMatthew Ahrens 			zfs_handle_t *root;
24889adfa60dSMatthew Ahrens 			char pool[ZFS_MAX_DATASET_NAME_LEN];
248919b94df9SMatthew Ahrens 			char *cp = pool;
249019b94df9SMatthew Ahrens 
249119b94df9SMatthew Ahrens 			/* get the pool name */
249219b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
249319b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
249419b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
249519b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
249619b94df9SMatthew Ahrens 
249719b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
249819b94df9SMatthew Ahrens 		}
249919b94df9SMatthew Ahrens 
250019b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
250119b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
250219b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
250319b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
250419b94df9SMatthew Ahrens 			nvlist_free(nv);
250519b94df9SMatthew Ahrens 			nvlist_free(value);
250619b94df9SMatthew Ahrens 			return (NULL);
250719b94df9SMatthew Ahrens 		}
250819b94df9SMatthew Ahrens 		nvlist_free(nv);
250919b94df9SMatthew Ahrens 		nvlist_free(value);
251019b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
251119b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
251219b94df9SMatthew Ahrens 	}
251319b94df9SMatthew Ahrens 
251419b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
251519b94df9SMatthew Ahrens 
251619b94df9SMatthew Ahrens 	return (value);
251719b94df9SMatthew Ahrens }
251819b94df9SMatthew Ahrens 
2519dfc11533SChris Williamson /*
2520dfc11533SChris Williamson  * Accepts a property and value and checks that the value
2521dfc11533SChris Williamson  * matches the one found by the channel program. If they are
2522dfc11533SChris Williamson  * not equal, print both of them.
2523dfc11533SChris Williamson  */
2524dfc11533SChris Williamson void
2525dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2526dfc11533SChris Williamson     const char *strval)
2527dfc11533SChris Williamson {
2528dfc11533SChris Williamson 	if (!zhp->zfs_hdl->libzfs_prop_debug)
2529dfc11533SChris Williamson 		return;
2530dfc11533SChris Williamson 	int error;
2531dfc11533SChris Williamson 	char *poolname = zhp->zpool_hdl->zpool_name;
2532dfc11533SChris Williamson 	const char *program =
2533dfc11533SChris Williamson 	    "args = ...\n"
2534dfc11533SChris Williamson 	    "ds = args['dataset']\n"
2535dfc11533SChris Williamson 	    "prop = args['property']\n"
2536dfc11533SChris Williamson 	    "value, setpoint = zfs.get_prop(ds, prop)\n"
2537dfc11533SChris Williamson 	    "return {value=value, setpoint=setpoint}\n";
2538dfc11533SChris Williamson 	nvlist_t *outnvl;
2539dfc11533SChris Williamson 	nvlist_t *retnvl;
2540dfc11533SChris Williamson 	nvlist_t *argnvl = fnvlist_alloc();
2541dfc11533SChris Williamson 
2542dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2543dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2544dfc11533SChris Williamson 
2545a3b28680SSerapheim Dimitropoulos 	error = lzc_channel_program_nosync(poolname, program,
2546dfc11533SChris Williamson 	    10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2547dfc11533SChris Williamson 
2548dfc11533SChris Williamson 	if (error == 0) {
2549dfc11533SChris Williamson 		retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2550dfc11533SChris Williamson 		if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2551dfc11533SChris Williamson 			int64_t ans;
2552dfc11533SChris Williamson 			error = nvlist_lookup_int64(retnvl, "value", &ans);
2553dfc11533SChris Williamson 			if (error != 0) {
2554dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2555dfc11533SChris Williamson 				    error);
2556dfc11533SChris Williamson 				return;
2557dfc11533SChris Williamson 			}
2558dfc11533SChris Williamson 			if (ans != intval) {
2559dfc11533SChris Williamson 				(void) fprintf(stderr,
2560dfc11533SChris Williamson 				    "%s: zfs found %lld, but zcp found %lld\n",
2561dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2562dfc11533SChris Williamson 				    (longlong_t)intval, (longlong_t)ans);
2563dfc11533SChris Williamson 			}
2564dfc11533SChris Williamson 		} else {
2565dfc11533SChris Williamson 			char *str_ans;
2566dfc11533SChris Williamson 			error = nvlist_lookup_string(retnvl, "value", &str_ans);
2567dfc11533SChris Williamson 			if (error != 0) {
2568dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2569dfc11533SChris Williamson 				    error);
2570dfc11533SChris Williamson 				return;
2571dfc11533SChris Williamson 			}
2572dfc11533SChris Williamson 			if (strcmp(strval, str_ans) != 0) {
2573dfc11533SChris Williamson 				(void) fprintf(stderr,
2574dfc11533SChris Williamson 				    "%s: zfs found %s, but zcp found %s\n",
2575dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2576dfc11533SChris Williamson 				    strval, str_ans);
2577dfc11533SChris Williamson 			}
2578dfc11533SChris Williamson 		}
2579dfc11533SChris Williamson 	} else {
2580dfc11533SChris Williamson 		(void) fprintf(stderr,
2581dfc11533SChris Williamson 		    "zcp check failed, channel program error: %u\n", error);
2582dfc11533SChris Williamson 	}
2583dfc11533SChris Williamson 	nvlist_free(argnvl);
2584dfc11533SChris Williamson 	nvlist_free(outnvl);
2585dfc11533SChris Williamson }
2586dfc11533SChris Williamson 
2587fa9e4066Sahrens /*
2588fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2589fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2590fa9e4066Sahrens  * human-readable form.
2591fa9e4066Sahrens  *
2592fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2593fa9e4066Sahrens  */
2594fa9e4066Sahrens int
2595fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2596990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2597fa9e4066Sahrens {
2598fa9e4066Sahrens 	char *source = NULL;
2599fa9e4066Sahrens 	uint64_t val;
26009c3fd121SMatthew Ahrens 	const char *str;
2601e9dbad6fSeschrock 	const char *strval;
260292241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2603fa9e4066Sahrens 
2604fa9e4066Sahrens 	/*
2605fa9e4066Sahrens 	 * Check to see if this property applies to our object
2606fa9e4066Sahrens 	 */
2607fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2608fa9e4066Sahrens 		return (-1);
2609fa9e4066Sahrens 
261092241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
261192241e0bSTom Erickson 		return (-1);
261292241e0bSTom Erickson 
2613fa9e4066Sahrens 	if (src)
2614990b4856Slling 		*src = ZPROP_SRC_NONE;
2615fa9e4066Sahrens 
2616fa9e4066Sahrens 	switch (prop) {
2617fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2618fa9e4066Sahrens 		/*
2619fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2620fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2621fa9e4066Sahrens 		 */
2622fa9e4066Sahrens 		{
2623a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2624a2eea2e1Sahrens 			time_t time = (time_t)val;
2625fa9e4066Sahrens 			struct tm t;
2626fa9e4066Sahrens 
2627fa9e4066Sahrens 			if (literal ||
2628fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2629fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2630fa9e4066Sahrens 			    &t) == 0)
2631a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2632fa9e4066Sahrens 		}
2633dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2634fa9e4066Sahrens 		break;
2635fa9e4066Sahrens 
2636fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2637fa9e4066Sahrens 		/*
2638fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2639fa9e4066Sahrens 		 *
2640fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2641fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2642fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2643fa9e4066Sahrens 		 *
2644fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2645fa9e4066Sahrens 		 * root to any values we return.
2646fa9e4066Sahrens 		 */
264729ab75c9Srm 
26487f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2649fa9e4066Sahrens 
2650b21718f0Sgw 		if (str[0] == '/') {
265129ab75c9Srm 			char buf[MAXPATHLEN];
265229ab75c9Srm 			char *root = buf;
2653a79992aaSTom Erickson 			const char *relpath;
2654fa9e4066Sahrens 
2655a79992aaSTom Erickson 			/*
2656a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2657a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2658a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2659a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2660a79992aaSTom Erickson 			 * inherited.
2661a79992aaSTom Erickson 			 */
2662a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2663a79992aaSTom Erickson 				relpath = "";
2664a79992aaSTom Erickson 			} else {
2665a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2666a79992aaSTom Erickson 				if (relpath[0] == '/')
2667a79992aaSTom Erickson 					relpath++;
2668a79992aaSTom Erickson 			}
2669b21718f0Sgw 
267029ab75c9Srm 			if ((zpool_get_prop(zhp->zpool_hdl,
2671c58b3526SAdam Stevko 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2672c58b3526SAdam Stevko 			    B_FALSE)) || (strcmp(root, "-") == 0))
267329ab75c9Srm 				root[0] = '\0';
2674b21718f0Sgw 			/*
2675b21718f0Sgw 			 * Special case an alternate root of '/'. This will
2676b21718f0Sgw 			 * avoid having multiple leading slashes in the
2677b21718f0Sgw 			 * mountpoint path.
2678b21718f0Sgw 			 */
2679b21718f0Sgw 			if (strcmp(root, "/") == 0)
2680b21718f0Sgw 				root++;
2681b21718f0Sgw 
2682b21718f0Sgw 			/*
2683b21718f0Sgw 			 * If the mountpoint is '/' then skip over this
2684b21718f0Sgw 			 * if we are obtaining either an alternate root or
2685b21718f0Sgw 			 * an inherited mountpoint.
2686b21718f0Sgw 			 */
2687b21718f0Sgw 			if (str[1] == '\0' && (root[0] != '\0' ||
2688b21718f0Sgw 			    relpath[0] != '\0'))
26897f7322feSeschrock 				str++;
2690fa9e4066Sahrens 
2691fa9e4066Sahrens 			if (relpath[0] == '\0')
2692fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
26937f7322feSeschrock 				    root, str);
2694fa9e4066Sahrens 			else
2695fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
26967f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2697fa9e4066Sahrens 				    relpath);
2698fa9e4066Sahrens 		} else {
2699fa9e4066Sahrens 			/* 'legacy' or 'none' */
27007f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2701fa9e4066Sahrens 		}
2702dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, propbuf);
2703fa9e4066Sahrens 		break;
2704fa9e4066Sahrens 
2705fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
27069c3fd121SMatthew Ahrens 		str = getprop_string(zhp, prop, &source);
27079c3fd121SMatthew Ahrens 		if (str == NULL)
2708fa9e4066Sahrens 			return (-1);
27099c3fd121SMatthew Ahrens 		(void) strlcpy(propbuf, str, proplen);
2710dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, str);
2711fa9e4066Sahrens 		break;
2712fa9e4066Sahrens 
271319b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
271419b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
271519b94df9SMatthew Ahrens 			return (-1);
271619b94df9SMatthew Ahrens 		break;
271719b94df9SMatthew Ahrens 
2718fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2719a9799022Sck 	case ZFS_PROP_REFQUOTA:
2720fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2721a9799022Sck 	case ZFS_PROP_REFRESERVATION:
2722a9799022Sck 
272399653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
272499653d4eSeschrock 			return (-1);
2725fa9e4066Sahrens 		/*
2726fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2727fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2728fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2729fa9e4066Sahrens 		 * that its set locally.
2730fa9e4066Sahrens 		 */
2731fa9e4066Sahrens 		if (val == 0) {
2732fa9e4066Sahrens 			if (literal)
2733fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2734fa9e4066Sahrens 			else
2735fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2736fa9e4066Sahrens 		} else {
2737fa9e4066Sahrens 			if (literal)
27385ad82045Snd 				(void) snprintf(propbuf, proplen, "%llu",
2739b1b8ab34Slling 				    (u_longlong_t)val);
2740fa9e4066Sahrens 			else
2741fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2742fa9e4066Sahrens 		}
2743dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2744fa9e4066Sahrens 		break;
2745fa9e4066Sahrens 
2746a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2747a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2748a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2749a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2750a2afb611SJerry Jelinek 
2751a2afb611SJerry Jelinek 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2752a2afb611SJerry Jelinek 			return (-1);
2753a2afb611SJerry Jelinek 
2754a2afb611SJerry Jelinek 		/*
2755a2afb611SJerry Jelinek 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2756a2afb611SJerry Jelinek 		 * literal is set), and indicate that it's the default value.
2757a2afb611SJerry Jelinek 		 * Otherwise, we print the number nicely and indicate that it's
2758a2afb611SJerry Jelinek 		 * set locally.
2759a2afb611SJerry Jelinek 		 */
2760a2afb611SJerry Jelinek 		if (literal) {
2761a2afb611SJerry Jelinek 			(void) snprintf(propbuf, proplen, "%llu",
2762a2afb611SJerry Jelinek 			    (u_longlong_t)val);
2763a2afb611SJerry Jelinek 		} else if (val == UINT64_MAX) {
2764a2afb611SJerry Jelinek 			(void) strlcpy(propbuf, "none", proplen);
2765a2afb611SJerry Jelinek 		} else {
2766a2afb611SJerry Jelinek 			zfs_nicenum(val, propbuf, proplen);
2767a2afb611SJerry Jelinek 		}
2768dfc11533SChris Williamson 
2769dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2770a2afb611SJerry Jelinek 		break;
2771a2afb611SJerry Jelinek 
2772187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2773fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
277499653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
277599653d4eSeschrock 			return (-1);
2776b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2777b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2778b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2779dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2780fa9e4066Sahrens 		break;
2781fa9e4066Sahrens 
2782fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2783fa9e4066Sahrens 		switch (zhp->zfs_type) {
2784fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2785fa9e4066Sahrens 			str = "filesystem";
2786fa9e4066Sahrens 			break;
2787fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2788fa9e4066Sahrens 			str = "volume";
2789fa9e4066Sahrens 			break;
2790fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2791fa9e4066Sahrens 			str = "snapshot";
2792fa9e4066Sahrens 			break;
279378f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
279478f17100SMatthew Ahrens 			str = "bookmark";
279578f17100SMatthew Ahrens 			break;
2796fa9e4066Sahrens 		default:
279799653d4eSeschrock 			abort();
2798fa9e4066Sahrens 		}
2799fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2800dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, propbuf);
2801fa9e4066Sahrens 		break;
2802fa9e4066Sahrens 
2803fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2804fa9e4066Sahrens 		/*
2805fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2806fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2807fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2808fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2809fa9e4066Sahrens 		 */
281099653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
281199653d4eSeschrock 		    src, &source, &val) != 0)
281299653d4eSeschrock 			return (-1);
281399653d4eSeschrock 		if (val)
2814fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2815fa9e4066Sahrens 		else
2816fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2817fa9e4066Sahrens 		break;
2818fa9e4066Sahrens 
2819fa9e4066Sahrens 	case ZFS_PROP_NAME:
2820fa9e4066Sahrens 		/*
2821fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2822fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2823fa9e4066Sahrens 		 * consumers.
2824fa9e4066Sahrens 		 */
2825fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2826dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, propbuf);
2827fa9e4066Sahrens 		break;
2828fa9e4066Sahrens 
28294201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
28304201a95eSRic Aleshire 		{
28314201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
28324201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
28334201a95eSRic Aleshire 
28344201a95eSRic Aleshire 			(void) strlcpy(propbuf,
28354201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
28364201a95eSRic Aleshire 
28374201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
28384201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
28394201a95eSRic Aleshire 				break;
28404201a95eSRic Aleshire 
28414201a95eSRic Aleshire 			/*
28424201a95eSRic Aleshire 			 * Try to translate the internal hex string to
28434201a95eSRic Aleshire 			 * human-readable output.  If there are any
28444201a95eSRic Aleshire 			 * problems just use the hex string.
28454201a95eSRic Aleshire 			 */
28464201a95eSRic Aleshire 
28474201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
28484201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
28494201a95eSRic Aleshire 				m_label_free(new_sl);
28504201a95eSRic Aleshire 				break;
28514201a95eSRic Aleshire 			}
28524201a95eSRic Aleshire 
28534201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
28544201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
28554201a95eSRic Aleshire 				if (ascii)
28564201a95eSRic Aleshire 					free(ascii);
28574201a95eSRic Aleshire 				m_label_free(new_sl);
28584201a95eSRic Aleshire 				break;
28594201a95eSRic Aleshire 			}
28604201a95eSRic Aleshire 			m_label_free(new_sl);
28614201a95eSRic Aleshire 
28624201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
28634201a95eSRic Aleshire 			free(ascii);
28644201a95eSRic Aleshire 		}
28654201a95eSRic Aleshire 		break;
28664201a95eSRic Aleshire 
2867f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2868e8d4a73cSJosh Paetzel 	case ZFS_PROP_CREATETXG:
2869f0f3ef5aSGarrett D'Amore 		/*
2870f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2871f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2872f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2873f0f3ef5aSGarrett D'Amore 		 */
2874f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2875f0f3ef5aSGarrett D'Amore 			return (-1);
2876f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2877dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2878f0f3ef5aSGarrett D'Amore 		break;
2879f0f3ef5aSGarrett D'Amore 
2880fa9e4066Sahrens 	default:
2881e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
288291ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2883e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2884dfc11533SChris Williamson 			    &source, &val) != 0) {
2885e7437265Sahrens 				return (-1);
2886dfc11533SChris Williamson 			}
2887dfc11533SChris Williamson 
2888dfc11533SChris Williamson 			if (literal) {
2889e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2890e7437265Sahrens 				    (u_longlong_t)val);
2891dfc11533SChris Williamson 			} else {
2892e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2893dfc11533SChris Williamson 			}
2894dfc11533SChris Williamson 			zcp_check(zhp, prop, val, NULL);
2895e7437265Sahrens 			break;
2896e7437265Sahrens 
289791ebeef5Sahrens 		case PROP_TYPE_STRING:
28989c3fd121SMatthew Ahrens 			str = getprop_string(zhp, prop, &source);
28999c3fd121SMatthew Ahrens 			if (str == NULL)
29009c3fd121SMatthew Ahrens 				return (-1);
2901dfc11533SChris Williamson 
29029c3fd121SMatthew Ahrens 			(void) strlcpy(propbuf, str, proplen);
2903dfc11533SChris Williamson 			zcp_check(zhp, prop, NULL, str);
2904e7437265Sahrens 			break;
2905e7437265Sahrens 
290691ebeef5Sahrens 		case PROP_TYPE_INDEX:
2907fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2908fe192f76Sahrens 			    &source, &val) != 0)
2909fe192f76Sahrens 				return (-1);
2910fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2911e7437265Sahrens 				return (-1);
2912dfc11533SChris Williamson 
2913e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2914dfc11533SChris Williamson 			zcp_check(zhp, prop, NULL, strval);
2915e7437265Sahrens 			break;
2916e7437265Sahrens 
2917e7437265Sahrens 		default:
2918e7437265Sahrens 			abort();
2919e7437265Sahrens 		}
2920fa9e4066Sahrens 	}
2921fa9e4066Sahrens 
2922fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2923fa9e4066Sahrens 
2924fa9e4066Sahrens 	return (0);
2925fa9e4066Sahrens }
2926fa9e4066Sahrens 
2927fa9e4066Sahrens /*
2928fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2929fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2930fa9e4066Sahrens  * hard-coded property types.
2931fa9e4066Sahrens  */
2932fa9e4066Sahrens uint64_t
2933fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2934fa9e4066Sahrens {
2935fa9e4066Sahrens 	char *source;
293699653d4eSeschrock 	uint64_t val;
293799653d4eSeschrock 
29383cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2939fa9e4066Sahrens 
294099653d4eSeschrock 	return (val);
2941fa9e4066Sahrens }
2942fa9e4066Sahrens 
29437b97dc1aSrm int
29447b97dc1aSrm zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
29457b97dc1aSrm {
29467b97dc1aSrm 	char buf[64];
29477b97dc1aSrm 
294814843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
29497b97dc1aSrm 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
29507b97dc1aSrm }
29517b97dc1aSrm 
2952fa9e4066Sahrens /*
2953fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2954fa9e4066Sahrens  */
2955fa9e4066Sahrens int
2956fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2957990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2958fa9e4066Sahrens {
2959fa9e4066Sahrens 	char *source;
2960fa9e4066Sahrens 
2961fa9e4066Sahrens 	/*
2962fa9e4066Sahrens 	 * Check to see if this property applies to our object
2963fa9e4066Sahrens 	 */
2964e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2965ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
296699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
296799653d4eSeschrock 		    zfs_prop_to_name(prop)));
2968e45ce728Sahrens 	}
2969fa9e4066Sahrens 
2970fa9e4066Sahrens 	if (src)
2971990b4856Slling 		*src = ZPROP_SRC_NONE;
2972fa9e4066Sahrens 
297399653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
297499653d4eSeschrock 		return (-1);
2975fa9e4066Sahrens 
2976fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2977fa9e4066Sahrens 
2978fa9e4066Sahrens 	return (0);
2979fa9e4066Sahrens }
2980fa9e4066Sahrens 
298114843421SMatthew Ahrens static int
298214843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
298314843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
298414843421SMatthew Ahrens {
298514843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
298614843421SMatthew Ahrens 	idmap_stat status;
298714843421SMatthew Ahrens 	int err = EINVAL;
298814843421SMatthew Ahrens 
29891fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
299014843421SMatthew Ahrens 		goto out;
299114843421SMatthew Ahrens 
299214843421SMatthew Ahrens 	if (isuser) {
299314843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
299414843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
299514843421SMatthew Ahrens 	} else {
299614843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
299714843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
299814843421SMatthew Ahrens 	}
299914843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
300014843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
300114843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
300214843421SMatthew Ahrens 		err = 0;
300314843421SMatthew Ahrens 	else
300414843421SMatthew Ahrens 		err = EINVAL;
300514843421SMatthew Ahrens out:
300614843421SMatthew Ahrens 	if (get_hdl)
300714843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
300814843421SMatthew Ahrens 	return (err);
300914843421SMatthew Ahrens }
301014843421SMatthew Ahrens 
301114843421SMatthew Ahrens /*
301214843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
301314843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
301414843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
301514843421SMatthew Ahrens  */
301614843421SMatthew Ahrens static int
301714843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
301814843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
301914843421SMatthew Ahrens {
302014843421SMatthew Ahrens 	zfs_userquota_prop_t type;
302114843421SMatthew Ahrens 	char *cp, *end;
30223b12c289SMatthew Ahrens 	char *numericsid = NULL;
302314843421SMatthew Ahrens 	boolean_t isuser;
302414843421SMatthew Ahrens 
302514843421SMatthew Ahrens 	domain[0] = '\0';
30261ed6b69aSGordon Ross 	*ridp = 0;
302714843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
302814843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
302914843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
303014843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
303114843421SMatthew Ahrens 			break;
303214843421SMatthew Ahrens 	}
303314843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
303414843421SMatthew Ahrens 		return (EINVAL);
303514843421SMatthew Ahrens 	*typep = type;
303614843421SMatthew Ahrens 
303714843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
303814843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
303914843421SMatthew Ahrens 
304014843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
304114843421SMatthew Ahrens 
304214843421SMatthew Ahrens 	if (strchr(cp, '@')) {
304314843421SMatthew Ahrens 		/*
304414843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
30453b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
304614843421SMatthew Ahrens 		 */
30471ed6b69aSGordon Ross 		int flag = 0;
30481ed6b69aSGordon Ross 		idmap_stat stat, map_stat;
30491ed6b69aSGordon Ross 		uid_t pid;
30501ed6b69aSGordon Ross 		idmap_rid_t rid;
30511ed6b69aSGordon Ross 		idmap_get_handle_t *gh = NULL;
30521ed6b69aSGordon Ross 
30531ed6b69aSGordon Ross 		stat = idmap_get_create(&gh);
30541ed6b69aSGordon Ross 		if (stat != IDMAP_SUCCESS) {
30551ed6b69aSGordon Ross 			idmap_get_destroy(gh);
30561ed6b69aSGordon Ross 			return (ENOMEM);
30571ed6b69aSGordon Ross 		}
305814843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
305914843421SMatthew Ahrens 			return (ENOENT);
30603b12c289SMatthew Ahrens 		if (isuser) {
30611ed6b69aSGordon Ross 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
30621ed6b69aSGordon Ross 			if (stat < 0)
30631ed6b69aSGordon Ross 				return (ENOENT);
30641ed6b69aSGordon Ross 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
30651ed6b69aSGordon Ross 			    &rid, &map_stat);
30663b12c289SMatthew Ahrens 		} else {
30671ed6b69aSGordon Ross 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
30681ed6b69aSGordon Ross 			if (stat < 0)
30691ed6b69aSGordon Ross 				return (ENOENT);
30701ed6b69aSGordon Ross 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
30711ed6b69aSGordon Ross 			    &rid, &map_stat);
30721ed6b69aSGordon Ross 		}
30731ed6b69aSGordon Ross 		if (stat < 0) {
30741ed6b69aSGordon Ross 			idmap_get_destroy(gh);
30751ed6b69aSGordon Ross 			return (ENOENT);
30763b12c289SMatthew Ahrens 		}
30771ed6b69aSGordon Ross 		stat = idmap_get_mappings(gh);
30781ed6b69aSGordon Ross 		idmap_get_destroy(gh);
30791ed6b69aSGordon Ross 
30801ed6b69aSGordon Ross 		if (stat < 0) {
308114843421SMatthew Ahrens 			return (ENOENT);
30823b12c289SMatthew Ahrens 		}
30833b12c289SMatthew Ahrens 		if (numericsid == NULL)
308414843421SMatthew Ahrens 			return (ENOENT);
30853b12c289SMatthew Ahrens 		cp = numericsid;
30861ed6b69aSGordon Ross 		*ridp = rid;
30873b12c289SMatthew Ahrens 		/* will be further decoded below */
30883b12c289SMatthew Ahrens 	}
30893b12c289SMatthew Ahrens 
30903b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
309114843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
30923b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
309314843421SMatthew Ahrens 		errno = 0;
30941ed6b69aSGordon Ross 		if (*ridp == 0) {
30951ed6b69aSGordon Ross 			cp = strrchr(domain, '-');
30961ed6b69aSGordon Ross 			*cp = '\0';
30971ed6b69aSGordon Ross 			cp++;
30981ed6b69aSGordon Ross 			*ridp = strtoull(cp, &end, 10);
30991ed6b69aSGordon Ross 		} else {
31001ed6b69aSGordon Ross 			end = "";
31011ed6b69aSGordon Ross 		}
31023b12c289SMatthew Ahrens 		if (numericsid) {
31033b12c289SMatthew Ahrens 			free(numericsid);
31043b12c289SMatthew Ahrens 			numericsid = NULL;
31053b12c289SMatthew Ahrens 		}
3106777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
310714843421SMatthew Ahrens 			return (EINVAL);
310814843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
310914843421SMatthew Ahrens 		/*
311014843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
311114843421SMatthew Ahrens 		 * turned into a uid/gid
311214843421SMatthew Ahrens 		 */
311314843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
311414843421SMatthew Ahrens 			return (ENOENT);
311514843421SMatthew Ahrens 		if (isuser) {
311614843421SMatthew Ahrens 			struct passwd *pw;
311714843421SMatthew Ahrens 			pw = getpwnam(cp);
311814843421SMatthew Ahrens 			if (pw == NULL)
311914843421SMatthew Ahrens 				return (ENOENT);
312014843421SMatthew Ahrens 			*ridp = pw->pw_uid;
312114843421SMatthew Ahrens 		} else {
312214843421SMatthew Ahrens 			struct group *gr;
312314843421SMatthew Ahrens 			gr = getgrnam(cp);
312414843421SMatthew Ahrens 			if (gr == NULL)
312514843421SMatthew Ahrens 				return (ENOENT);
312614843421SMatthew Ahrens 			*ridp = gr->gr_gid;
312714843421SMatthew Ahrens 		}
312814843421SMatthew Ahrens 	} else {
312914843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
313014843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
313114843421SMatthew Ahrens 		idmap_rid_t rid;
313214843421SMatthew Ahrens 		char *mapdomain;
313314843421SMatthew Ahrens 
313414843421SMatthew Ahrens 		if (*end != '\0')
313514843421SMatthew Ahrens 			return (EINVAL);
313614843421SMatthew Ahrens 		if (id > MAXUID) {
313714843421SMatthew Ahrens 			/* It's an ephemeral ID. */
313814843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
313914843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
314014843421SMatthew Ahrens 				return (ENOENT);
31413b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
314214843421SMatthew Ahrens 			*ridp = rid;
314314843421SMatthew Ahrens 		} else {
314414843421SMatthew Ahrens 			*ridp = id;
314514843421SMatthew Ahrens 		}
314614843421SMatthew Ahrens 	}
314714843421SMatthew Ahrens 
31483b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
314914843421SMatthew Ahrens 	return (0);
315014843421SMatthew Ahrens }
315114843421SMatthew Ahrens 
3152edea4b55SLin Ling static int
3153edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3154edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
315514843421SMatthew Ahrens {
315614843421SMatthew Ahrens 	int err;
315714843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
315814843421SMatthew Ahrens 
315919b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
316014843421SMatthew Ahrens 
316114843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
316214843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3163edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3164edea4b55SLin Ling 	zc.zc_objset_type = *typep;
316514843421SMatthew Ahrens 	if (err)
316614843421SMatthew Ahrens 		return (err);
316714843421SMatthew Ahrens 
316814843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
316914843421SMatthew Ahrens 	if (err)
317014843421SMatthew Ahrens 		return (err);
317114843421SMatthew Ahrens 
3172edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
3173edea4b55SLin Ling 	return (0);
3174edea4b55SLin Ling }
3175edea4b55SLin Ling 
3176edea4b55SLin Ling int
3177edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3178edea4b55SLin Ling     uint64_t *propvalue)
3179edea4b55SLin Ling {
3180edea4b55SLin Ling 	zfs_userquota_prop_t type;
3181edea4b55SLin Ling 
3182edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3183edea4b55SLin Ling 	    &type));
3184edea4b55SLin Ling }
3185edea4b55SLin Ling 
3186edea4b55SLin Ling int
3187edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3188edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
3189edea4b55SLin Ling {
3190edea4b55SLin Ling 	int err;
3191edea4b55SLin Ling 	uint64_t propvalue;
3192edea4b55SLin Ling 	zfs_userquota_prop_t type;
3193edea4b55SLin Ling 
3194edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3195edea4b55SLin Ling 	    &type);
3196edea4b55SLin Ling 
3197edea4b55SLin Ling 	if (err)
3198edea4b55SLin Ling 		return (err);
3199edea4b55SLin Ling 
320014843421SMatthew Ahrens 	if (literal) {
3201edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3202edea4b55SLin Ling 	} else if (propvalue == 0 &&
320314843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
320414843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
320514843421SMatthew Ahrens 	} else {
3206edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
320714843421SMatthew Ahrens 	}
320814843421SMatthew Ahrens 	return (0);
320914843421SMatthew Ahrens }
321014843421SMatthew Ahrens 
321119b94df9SMatthew Ahrens int
321219b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
321319b94df9SMatthew Ahrens     uint64_t *propvalue)
32148ac09fceSRichard Lowe {
321519b94df9SMatthew Ahrens 	int err;
321619b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
321719b94df9SMatthew Ahrens 	const char *snapname;
3218ebedde84SEric Taylor 
321919b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3220e5351341SMatthew Ahrens 
322119b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
322219b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
322319b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
322419b94df9SMatthew Ahrens 	} else {
322519b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
322619b94df9SMatthew Ahrens 		char *cp;
3227e5351341SMatthew Ahrens 
322819b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
322919b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
323019b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
323119b94df9SMatthew Ahrens 		if (cp != NULL)
323219b94df9SMatthew Ahrens 			*cp = '\0';
323319b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
323419b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
32358ac09fceSRichard Lowe 	}
323619b94df9SMatthew Ahrens 
323719b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
323819b94df9SMatthew Ahrens 	if (err)
323919b94df9SMatthew Ahrens 		return (err);
324019b94df9SMatthew Ahrens 
324119b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
324219b94df9SMatthew Ahrens 	return (0);
3243ebedde84SEric Taylor }
3244ebedde84SEric Taylor 
3245fa9e4066Sahrens int
324619b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
324719b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
3248fa9e4066Sahrens {
324919b94df9SMatthew Ahrens 	int err;
325019b94df9SMatthew Ahrens 	uint64_t propvalue;
32513cb34c60Sahrens 
325219b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3253ebedde84SEric Taylor 
325419b94df9SMatthew Ahrens 	if (err)
325519b94df9SMatthew Ahrens 		return (err);
32568ac09fceSRichard Lowe 
325719b94df9SMatthew Ahrens 	if (literal) {
325819b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
325919b94df9SMatthew Ahrens 	} else {
326019b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
3261fa9e4066Sahrens 	}
326219b94df9SMatthew Ahrens 	return (0);
32637f7322feSeschrock }
32647f7322feSeschrock 
32657f7322feSeschrock /*
326619b94df9SMatthew Ahrens  * Returns the name of the given zfs handle.
32677f7322feSeschrock  */
326819b94df9SMatthew Ahrens const char *
326919b94df9SMatthew Ahrens zfs_get_name(const zfs_handle_t *zhp)
32707f7322feSeschrock {
327119b94df9SMatthew Ahrens 	return (zhp->zfs_name);
327219b94df9SMatthew Ahrens }
32738ac09fceSRichard Lowe 
32748808ac5dSYuri Pankov /*
32758808ac5dSYuri Pankov  * Returns the name of the parent pool for the given zfs handle.
32768808ac5dSYuri Pankov  */
32778808ac5dSYuri Pankov const char *
32788808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp)
32798808ac5dSYuri Pankov {
32808808ac5dSYuri Pankov 	return (zhp->zpool_hdl->zpool_name);
32818808ac5dSYuri Pankov }
32828808ac5dSYuri Pankov 
328319b94df9SMatthew Ahrens /*
328419b94df9SMatthew Ahrens  * Returns the type of the given zfs handle.
328519b94df9SMatthew Ahrens  */
328619b94df9SMatthew Ahrens zfs_type_t
328719b94df9SMatthew Ahrens zfs_get_type(const zfs_handle_t *zhp)
328819b94df9SMatthew Ahrens {
328919b94df9SMatthew Ahrens 	return (zhp->zfs_type);
32907f7322feSeschrock }
32917f7322feSeschrock 
3292d41c4376SMark J Musante /*
3293d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
3294d41c4376SMark J Musante  *
3295d41c4376SMark J Musante  * Needs to handle these cases:
3296d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3297d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3298d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
3299d41c4376SMark J Musante  */
3300d41c4376SMark J Musante static boolean_t
3301d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
3302d41c4376SMark J Musante {
3303d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
3304d41c4376SMark J Musante 
3305d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
3306d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
3307d41c4376SMark J Musante 		return (B_FALSE);
3308d41c4376SMark J Musante 
3309d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
3310d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3311d41c4376SMark J Musante }
3312d41c4376SMark J Musante 
3313fa9e4066Sahrens /*
3314fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
331519b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
331619b94df9SMatthew Ahrens  * pool).
3317fa9e4066Sahrens  */
3318fa9e4066Sahrens static int
3319fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3320fa9e4066Sahrens {
332119b94df9SMatthew Ahrens 	char *slashp;
3322fa9e4066Sahrens 
332319b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
3324fa9e4066Sahrens 
332519b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
332619b94df9SMatthew Ahrens 		return (-1);
332719b94df9SMatthew Ahrens 	*slashp = '\0';
3328fa9e4066Sahrens 
3329fa9e4066Sahrens 	return (0);
3330fa9e4066Sahrens }
3331fa9e4066Sahrens 
3332*eb633035STom Caputi int
3333*eb633035STom Caputi zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3334*eb633035STom Caputi {
3335*eb633035STom Caputi 	return (parent_name(zfs_get_name(zhp), buf, buflen));
3336*eb633035STom Caputi }
3337*eb633035STom Caputi 
3338fa9e4066Sahrens /*
33397f1f55eaSvb  * If accept_ancestor is false, then check to make sure that the given path has
33407f1f55eaSvb  * a parent, and that it exists.  If accept_ancestor is true, then find the
33417f1f55eaSvb  * closest existing ancestor for the given path.  In prefixlen return the
33427f1f55eaSvb  * length of already existing prefix of the given path.  We also fetch the
33437f1f55eaSvb  * 'zoned' property, which is used to validate property settings when creating
33447f1f55eaSvb  * new datasets.
3345fa9e4066Sahrens  */
3346fa9e4066Sahrens static int
33477f1f55eaSvb check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
33487f1f55eaSvb     boolean_t accept_ancestor, int *prefixlen)
3349fa9e4066Sahrens {
3350fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
33519adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3352fa9e4066Sahrens 	char *slash;
33537f7322feSeschrock 	zfs_handle_t *zhp;
335499653d4eSeschrock 	char errbuf[1024];
3355d41c4376SMark J Musante 	uint64_t is_zoned;
335699653d4eSeschrock 
3357deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
3358deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3359fa9e4066Sahrens 
3360fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
3361fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
336299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
336399653d4eSeschrock 		    "missing dataset name"));
336499653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3365fa9e4066Sahrens 	}
3366fa9e4066Sahrens 
3367fa9e4066Sahrens 	/* check to see if the pool exists */
3368fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
3369fa9e4066Sahrens 		slash = parent + strlen(parent);
33708ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
3371fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
337299653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3373fa9e4066Sahrens 	    errno == ENOENT) {
337499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
337599653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
337699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3377fa9e4066Sahrens 	}
3378fa9e4066Sahrens 
3379fa9e4066Sahrens 	/* check to see if the parent dataset exists */
33807f1f55eaSvb 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
33817f1f55eaSvb 		if (errno == ENOENT && accept_ancestor) {
33827f1f55eaSvb 			/*
33837f1f55eaSvb 			 * Go deeper to find an ancestor, give up on top level.
33847f1f55eaSvb 			 */
33857f1f55eaSvb 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
33867f1f55eaSvb 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33877f1f55eaSvb 				    "no such pool '%s'"), zc.zc_name);
33887f1f55eaSvb 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
33897f1f55eaSvb 			}
33907f1f55eaSvb 		} else if (errno == ENOENT) {
339199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
339299653d4eSeschrock 			    "parent does not exist"));
339399653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
33947f1f55eaSvb 		} else
339599653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3396fa9e4066Sahrens 	}
3397fa9e4066Sahrens 
3398d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3399d41c4376SMark J Musante 	if (zoned != NULL)
3400d41c4376SMark J Musante 		*zoned = is_zoned;
3401d41c4376SMark J Musante 
3402fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
3403d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
340499653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
34057f7322feSeschrock 		zfs_close(zhp);
3406fa9e4066Sahrens 		return (-1);
3407fa9e4066Sahrens 	}
3408fa9e4066Sahrens 
3409fa9e4066Sahrens 	/* make sure parent is a filesystem */
34107f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
341199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
341299653d4eSeschrock 		    "parent is not a filesystem"));
341399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
34147f7322feSeschrock 		zfs_close(zhp);
3415fa9e4066Sahrens 		return (-1);
3416fa9e4066Sahrens 	}
3417fa9e4066Sahrens 
34187f7322feSeschrock 	zfs_close(zhp);
34197f1f55eaSvb 	if (prefixlen != NULL)
34207f1f55eaSvb 		*prefixlen = strlen(parent);
34217f1f55eaSvb 	return (0);
34227f1f55eaSvb }
34237f1f55eaSvb 
34247f1f55eaSvb /*
34257f1f55eaSvb  * Finds whether the dataset of the given type(s) exists.
34267f1f55eaSvb  */
34277f1f55eaSvb boolean_t
34287f1f55eaSvb zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
34297f1f55eaSvb {
34307f1f55eaSvb 	zfs_handle_t *zhp;
34317f1f55eaSvb 
3432f18faf3fSek 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
34337f1f55eaSvb 		return (B_FALSE);
34347f1f55eaSvb 
34357f1f55eaSvb 	/*
34367f1f55eaSvb 	 * Try to get stats for the dataset, which will tell us if it exists.
34377f1f55eaSvb 	 */
34387f1f55eaSvb 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
34397f1f55eaSvb 		int ds_type = zhp->zfs_type;
34407f1f55eaSvb 
34417f1f55eaSvb 		zfs_close(zhp);
34427f1f55eaSvb 		if (types & ds_type)
34437f1f55eaSvb 			return (B_TRUE);
34447f1f55eaSvb 	}
34457f1f55eaSvb 	return (B_FALSE);
34467f1f55eaSvb }
34477f1f55eaSvb 
34483cb34c60Sahrens /*
34493cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
34503cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
34513cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
34523cb34c60Sahrens  */
34533cb34c60Sahrens int
34543cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
34553cb34c60Sahrens {
34563cb34c60Sahrens 	zfs_handle_t *h;
34573cb34c60Sahrens 	char *cp;
34583cb34c60Sahrens 	const char *opname;
34593cb34c60Sahrens 
34603cb34c60Sahrens 	/* make sure prefix exists */
34613cb34c60Sahrens 	cp = target + prefixlen;
34623cb34c60Sahrens 	if (*cp != '/') {
34633cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
34643cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34653cb34c60Sahrens 	} else {
34663cb34c60Sahrens 		*cp = '\0';
34673cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34683cb34c60Sahrens 		*cp = '/';
34693cb34c60Sahrens 	}
34703cb34c60Sahrens 	if (h == NULL)
34713cb34c60Sahrens 		return (-1);
34723cb34c60Sahrens 	zfs_close(h);
34733cb34c60Sahrens 
34743cb34c60Sahrens 	/*
34753cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
34763cb34c60Sahrens 	 * up to the prefixlen-long one.
34773cb34c60Sahrens 	 */
34783cb34c60Sahrens 	for (cp = target + prefixlen + 1;
347988f61deeSIgor Kozhukhov 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
34803cb34c60Sahrens 
34813cb34c60Sahrens 		*cp = '\0';
34823cb34c60Sahrens 
34833cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
34843cb34c60Sahrens 		if (h) {
34853cb34c60Sahrens 			/* it already exists, nothing to do here */
34863cb34c60Sahrens 			zfs_close(h);
34873cb34c60Sahrens 			continue;
34883cb34c60Sahrens 		}
34893cb34c60Sahrens 
34903cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
34913cb34c60Sahrens 		    NULL) != 0) {
34923cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
34933cb34c60Sahrens 			goto ancestorerr;
34943cb34c60Sahrens 		}
34953cb34c60Sahrens 
34963cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34973cb34c60Sahrens 		if (h == NULL) {
34983cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
34993cb34c60Sahrens 			goto ancestorerr;
35003cb34c60Sahrens 		}
35013cb34c60Sahrens 
35023cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
35033cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
35043cb34c60Sahrens 			goto ancestorerr;
35053cb34c60Sahrens 		}
35063cb34c60Sahrens 
35073cb34c60Sahrens 		if (zfs_share(h) != 0) {
35083cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
35093cb34c60Sahrens 			goto ancestorerr;
35103cb34c60Sahrens 		}
35113cb34c60Sahrens 
35123cb34c60Sahrens 		zfs_close(h);
35133cb34c60Sahrens 	}
35143cb34c60Sahrens 
35153cb34c60Sahrens 	return (0);
35163cb34c60Sahrens 
35173cb34c60Sahrens ancestorerr:
35183cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35193cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
35203cb34c60Sahrens 	return (-1);
35213cb34c60Sahrens }
35223cb34c60Sahrens 
35237f1f55eaSvb /*
35247f1f55eaSvb  * Creates non-existing ancestors of the given path.
35257f1f55eaSvb  */
35267f1f55eaSvb int
35277f1f55eaSvb zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
35287f1f55eaSvb {
35297f1f55eaSvb 	int prefix;
35307f1f55eaSvb 	char *path_copy;
35315ac95da7SSerapheim Dimitropoulos 	char errbuf[1024];
3532f83b46baSPaul Dagnelie 	int rc = 0;
35337f1f55eaSvb 
35345ac95da7SSerapheim Dimitropoulos 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35355ac95da7SSerapheim Dimitropoulos 	    "cannot create '%s'"), path);
35365ac95da7SSerapheim Dimitropoulos 
35375ac95da7SSerapheim Dimitropoulos 	/*
35385ac95da7SSerapheim Dimitropoulos 	 * Check that we are not passing the nesting limit
35395ac95da7SSerapheim Dimitropoulos 	 * before we start creating any ancestors.
35405ac95da7SSerapheim Dimitropoulos 	 */
35415ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(path) != 0) {
35425ac95da7SSerapheim Dimitropoulos 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35435ac95da7SSerapheim Dimitropoulos 		    "maximum name nesting depth exceeded"));
35445ac95da7SSerapheim Dimitropoulos 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35455ac95da7SSerapheim Dimitropoulos 	}
35465ac95da7SSerapheim Dimitropoulos 
3547d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
35487f1f55eaSvb 		return (-1);
35497f1f55eaSvb 
35507f1f55eaSvb 	if ((path_copy = strdup(path)) != NULL) {
35517f1f55eaSvb 		rc = create_parents(hdl, path_copy, prefix);
35527f1f55eaSvb 		free(path_copy);
35537f1f55eaSvb 	}
35547f1f55eaSvb 	if (path_copy == NULL || rc != 0)
35557f1f55eaSvb 		return (-1);
35567f1f55eaSvb 
3557fa9e4066Sahrens 	return (0);
3558fa9e4066Sahrens }
3559fa9e4066Sahrens 
3560fa9e4066Sahrens /*
3561e9dbad6fSeschrock  * Create a new filesystem or volume.
3562fa9e4066Sahrens  */
3563fa9e4066Sahrens int
356499653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3565e9dbad6fSeschrock     nvlist_t *props)
3566fa9e4066Sahrens {
3567fa9e4066Sahrens 	int ret;
3568fa9e4066Sahrens 	uint64_t size = 0;
3569fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3570*eb633035STom Caputi 	uint8_t *wkeydata = NULL;
3571*eb633035STom Caputi 	uint_t wkeylen = 0;
357299653d4eSeschrock 	char errbuf[1024];
3573*eb633035STom Caputi 	char parent[MAXNAMELEN];
3574e9dbad6fSeschrock 	uint64_t zoned;
357526455f9eSAndriy Gapon 	enum lzc_dataset_type ost;
3576690031d3Sloli 	zpool_handle_t *zpool_handle;
357799653d4eSeschrock 
357899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
357999653d4eSeschrock 	    "cannot create '%s'"), path);
3580fa9e4066Sahrens 
3581fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3582f18faf3fSek 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
358399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3584fa9e4066Sahrens 
35855ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(path) != 0) {
35865ac95da7SSerapheim Dimitropoulos 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35875ac95da7SSerapheim Dimitropoulos 		    "maximum name nesting depth exceeded"));
35885ac95da7SSerapheim Dimitropoulos 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35895ac95da7SSerapheim Dimitropoulos 	}
35905ac95da7SSerapheim Dimitropoulos 
3591fa9e4066Sahrens 	/* validate parents exist */
35927f1f55eaSvb 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3593fa9e4066Sahrens 		return (-1);
3594fa9e4066Sahrens 
3595fa9e4066Sahrens 	/*
3596fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3597fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3598fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3599fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3600fa9e4066Sahrens 	 * first try to see if the dataset exists.
3601fa9e4066Sahrens 	 */
36024445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
360399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
360499653d4eSeschrock 		    "dataset already exists"));
360599653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3606fa9e4066Sahrens 	}
3607fa9e4066Sahrens 
3608fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
360926455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZVOL;
3610fa9e4066Sahrens 	else
361126455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZFS;
3612fa9e4066Sahrens 
3613e9316f76SJoe Stein 	/* open zpool handle for prop validation */
36149adfa60dSMatthew Ahrens 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3615e9316f76SJoe Stein 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3616e9316f76SJoe Stein 
3617e9316f76SJoe Stein 	/* truncate pool_path at first slash */
3618e9316f76SJoe Stein 	char *p = strchr(pool_path, '/');
3619e9316f76SJoe Stein 	if (p != NULL)
3620e9316f76SJoe Stein 		*p = '\0';
3621e9316f76SJoe Stein 
3622690031d3Sloli 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3623690031d3Sloli 		return (-1);
3624e9316f76SJoe Stein 
36250a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3626*eb633035STom Caputi 	    zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3627e9316f76SJoe Stein 		zpool_close(zpool_handle);
3628e9dbad6fSeschrock 		return (-1);
3629e9316f76SJoe Stein 	}
3630e9316f76SJoe Stein 	zpool_close(zpool_handle);
3631e9dbad6fSeschrock 
3632fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
36335c5460e9Seschrock 		/*
36345c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
36355c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
36365c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
36375c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
36385c5460e9Seschrock 		 * zero.
36395c5460e9Seschrock 		 */
3640e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3641e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3642e9dbad6fSeschrock 			nvlist_free(props);
364399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3644e9dbad6fSeschrock 			    "missing volume size"));
3645e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3646e9dbad6fSeschrock 		}
3647e9dbad6fSeschrock 
3648e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3649e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3650e9dbad6fSeschrock 		    &blocksize)) != 0) {
3651e9dbad6fSeschrock 			if (ret == ENOENT) {
3652e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3653e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3654e9dbad6fSeschrock 			} else {
3655e9dbad6fSeschrock 				nvlist_free(props);
3656e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3657e9dbad6fSeschrock 				    "missing volume block size"));
3658e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3659e9dbad6fSeschrock 			}
3660fa9e4066Sahrens 		}
3661fa9e4066Sahrens 
3662e9dbad6fSeschrock 		if (size == 0) {
3663e9dbad6fSeschrock 			nvlist_free(props);
366499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3665e9dbad6fSeschrock 			    "volume size cannot be zero"));
3666e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36675c5460e9Seschrock 		}
36685c5460e9Seschrock 
36695c5460e9Seschrock 		if (size % blocksize != 0) {
3670e9dbad6fSeschrock 			nvlist_free(props);
367199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3672e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3673e9dbad6fSeschrock 			    "size"));
3674e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36755c5460e9Seschrock 		}
3676fa9e4066Sahrens 	}
3677fa9e4066Sahrens 
3678*eb633035STom Caputi 	(void) parent_name(path, parent, sizeof (parent));
3679*eb633035STom Caputi 	if (zfs_crypto_create(hdl, parent, props, NULL, &wkeydata,
3680*eb633035STom Caputi 	    &wkeylen) != 0) {
3681*eb633035STom Caputi 		nvlist_free(props);
3682*eb633035STom Caputi 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3683*eb633035STom Caputi 	}
3684*eb633035STom Caputi 
3685fa9e4066Sahrens 	/* create the dataset */
3686*eb633035STom Caputi 	ret = lzc_create(path, ost, props, wkeydata, wkeylen);
36874445fffbSMatthew Ahrens 	nvlist_free(props);
3688*eb633035STom Caputi 	if (wkeydata != NULL)
3689*eb633035STom Caputi 		free(wkeydata);
3690e9dbad6fSeschrock 
3691fa9e4066Sahrens 	/* check for failure */
3692fa9e4066Sahrens 	if (ret != 0) {
3693fa9e4066Sahrens 		switch (errno) {
3694fa9e4066Sahrens 		case ENOENT:
369599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
369699653d4eSeschrock 			    "no such parent '%s'"), parent);
369799653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3698fa9e4066Sahrens 
3699fa9e4066Sahrens 		case EINVAL:
370099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3701d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
370299653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3703fa9e4066Sahrens 
370440feaa91Sahrens 		case ENOTSUP:
370540feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
370640feaa91Sahrens 			    "pool must be upgraded to set this "
370740feaa91Sahrens 			    "property or value"));
370840feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
37099fa2266dSYuri Pankov 		case ERANGE:
37109fa2266dSYuri Pankov 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37119fa2266dSYuri Pankov 			    "invalid property value(s) specified"));
37129fa2266dSYuri Pankov 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3713*eb633035STom Caputi 		case EACCES:
3714*eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3715*eb633035STom Caputi 			    "encryption root's key is not loaded "
3716*eb633035STom Caputi 			    "or provided"));
3717*eb633035STom Caputi 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3718*eb633035STom Caputi 
3719fa9e4066Sahrens #ifdef _ILP32
3720fa9e4066Sahrens 		case EOVERFLOW:
3721fa9e4066Sahrens 			/*
3722fa9e4066Sahrens 			 * This platform can't address a volume this big.
3723fa9e4066Sahrens 			 */
372499653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
372599653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
372699653d4eSeschrock 				    errbuf));
3727fa9e4066Sahrens #endif
372899653d4eSeschrock 			/* FALLTHROUGH */
3729fa9e4066Sahrens 		default:
373099653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3731fa9e4066Sahrens 		}
3732fa9e4066Sahrens 	}
3733fa9e4066Sahrens 
3734fa9e4066Sahrens 	return (0);
3735fa9e4066Sahrens }
3736fa9e4066Sahrens 
3737fa9e4066Sahrens /*
3738fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
373965fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
374065fec9f6SChristopher Siden  * does not exist this function does nothing.
3741fa9e4066Sahrens  */
3742fa9e4066Sahrens int
3743842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3744fa9e4066Sahrens {
3745049ba636SAndriy Gapon 	int error;
3746049ba636SAndriy Gapon 
3747049ba636SAndriy Gapon 	if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3748049ba636SAndriy Gapon 		return (EINVAL);
3749fa9e4066Sahrens 
375078f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
375178f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
375278f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
3753049ba636SAndriy Gapon 		error = lzc_destroy_bookmarks(nv, NULL);
375478f17100SMatthew Ahrens 		fnvlist_free(nv);
375578f17100SMatthew Ahrens 		if (error != 0) {
3756049ba636SAndriy Gapon 			return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
375778f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
375878f17100SMatthew Ahrens 			    zhp->zfs_name));
375978f17100SMatthew Ahrens 		}
376078f17100SMatthew Ahrens 		return (0);
376178f17100SMatthew Ahrens 	}
376278f17100SMatthew Ahrens 
3763049ba636SAndriy Gapon 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3764049ba636SAndriy Gapon 		nvlist_t *nv = fnvlist_alloc();
3765049ba636SAndriy Gapon 		fnvlist_add_boolean(nv, zhp->zfs_name);
3766049ba636SAndriy Gapon 		error = lzc_destroy_snaps(nv, defer, NULL);
3767049ba636SAndriy Gapon 		fnvlist_free(nv);
3768fa9e4066Sahrens 	} else {
3769049ba636SAndriy Gapon 		error = lzc_destroy(zhp->zfs_name);
3770fa9e4066Sahrens 	}
3771fa9e4066Sahrens 
3772049ba636SAndriy Gapon 	if (error != 0 && error != ENOENT) {
3773ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
377499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
377599653d4eSeschrock 		    zhp->zfs_name));
37761d452cf5Sahrens 	}
3777fa9e4066Sahrens 
3778fa9e4066Sahrens 	remove_mountpoint(zhp);
3779fa9e4066Sahrens 
3780fa9e4066Sahrens 	return (0);
3781fa9e4066Sahrens }
3782fa9e4066Sahrens 
37831d452cf5Sahrens struct destroydata {
378419b94df9SMatthew Ahrens 	nvlist_t *nvl;
378519b94df9SMatthew Ahrens 	const char *snapname;
37861d452cf5Sahrens };
37871d452cf5Sahrens 
37881d452cf5Sahrens static int
3789681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
37901d452cf5Sahrens {
37911d452cf5Sahrens 	struct destroydata *dd = arg;
37929adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3793681d9761SEric Taylor 	int rv = 0;
37941d452cf5Sahrens 
379519b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
379619b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
37971d452cf5Sahrens 
3798a7a845e4SSteven Hartland 	if (lzc_exists(name))
379919b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
38001d452cf5Sahrens 
380119b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
380219b94df9SMatthew Ahrens 	zfs_close(zhp);
38033ccfa83cSahrens 	return (rv);
38041d452cf5Sahrens }
38051d452cf5Sahrens 
38061d452cf5Sahrens /*
38071d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
38081d452cf5Sahrens  */
38091d452cf5Sahrens int
3810842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
38111d452cf5Sahrens {
38121d452cf5Sahrens 	int ret;
38131d452cf5Sahrens 	struct destroydata dd = { 0 };
38141d452cf5Sahrens 
38151d452cf5Sahrens 	dd.snapname = snapname;
381619b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
381719b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
38181d452cf5Sahrens 
3819a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
382019b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
38211d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
382219b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
382319b94df9SMatthew Ahrens 	} else {
38243b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
38251d452cf5Sahrens 	}
382619b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
382719b94df9SMatthew Ahrens 	return (ret);
382819b94df9SMatthew Ahrens }
382919b94df9SMatthew Ahrens 
383019b94df9SMatthew Ahrens /*
38313b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
383219b94df9SMatthew Ahrens  */
383319b94df9SMatthew Ahrens int
38343b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
383519b94df9SMatthew Ahrens {
383619b94df9SMatthew Ahrens 	int ret;
38374cde22c2SChris Williamson 	nvlist_t *errlist = NULL;
38381d452cf5Sahrens 
38394445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
38401d452cf5Sahrens 
38414cde22c2SChris Williamson 	if (ret == 0) {
38424cde22c2SChris Williamson 		nvlist_free(errlist);
38433b2aab18SMatthew Ahrens 		return (0);
38444cde22c2SChris Williamson 	}
38454445fffbSMatthew Ahrens 
3846a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
38473b2aab18SMatthew Ahrens 		char errbuf[1024];
38483b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
38493b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
38503b2aab18SMatthew Ahrens 
38513b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
38523b2aab18SMatthew Ahrens 	}
38533b2aab18SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
38543b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
38553b2aab18SMatthew Ahrens 		char errbuf[1024];
38563b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
38573b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
38583b2aab18SMatthew Ahrens 		    nvpair_name(pair));
38593b2aab18SMatthew Ahrens 
38603b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
38613b2aab18SMatthew Ahrens 		case EEXIST:
38623b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
38633b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
38643b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
38653b2aab18SMatthew Ahrens 			break;
38663b2aab18SMatthew Ahrens 		default:
38673b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
38683b2aab18SMatthew Ahrens 			break;
38691d452cf5Sahrens 		}
38701d452cf5Sahrens 	}
38711d452cf5Sahrens 
38724cde22c2SChris Williamson 	nvlist_free(errlist);
38734445fffbSMatthew Ahrens 	return (ret);
38741d452cf5Sahrens }
38751d452cf5Sahrens 
3876fa9e4066Sahrens /*
3877fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3878fa9e4066Sahrens  */
3879fa9e4066Sahrens int
3880e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3881fa9e4066Sahrens {
38829adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3883fa9e4066Sahrens 	int ret;
388499653d4eSeschrock 	char errbuf[1024];
388599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3886e9dbad6fSeschrock 	uint64_t zoned;
3887fa9e4066Sahrens 
3888fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3889fa9e4066Sahrens 
389099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
389199653d4eSeschrock 	    "cannot create '%s'"), target);
389299653d4eSeschrock 
389319b94df9SMatthew Ahrens 	/* validate the target/clone name */
3894f18faf3fSek 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
389599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3896fa9e4066Sahrens 
3897fa9e4066Sahrens 	/* validate parents exist */
38987f1f55eaSvb 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3899fa9e4066Sahrens 		return (-1);
3900fa9e4066Sahrens 
3901fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3902fa9e4066Sahrens 
3903fa9e4066Sahrens 	/* do the clone */
3904e9dbad6fSeschrock 
3905e9dbad6fSeschrock 	if (props) {
39064445fffbSMatthew Ahrens 		zfs_type_t type;
39071c10ae76SMike Gerdts 
39084445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
39094445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
39104445fffbSMatthew Ahrens 		} else {
39114445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
39124445fffbSMatthew Ahrens 		}
39130a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3914*eb633035STom Caputi 		    zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
3915e9dbad6fSeschrock 			return (-1);
39161c10ae76SMike Gerdts 		if (zfs_fix_auto_resv(zhp, props) == -1) {
39171c10ae76SMike Gerdts 			nvlist_free(props);
39181c10ae76SMike Gerdts 			return (-1);
39191c10ae76SMike Gerdts 		}
3920e9dbad6fSeschrock 	}
3921fa9e4066Sahrens 
3922*eb633035STom Caputi 	if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
3923*eb633035STom Caputi 		nvlist_free(props);
3924*eb633035STom Caputi 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3925*eb633035STom Caputi 	}
3926*eb633035STom Caputi 
39274445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
39284445fffbSMatthew Ahrens 	nvlist_free(props);
3929e9dbad6fSeschrock 
3930fa9e4066Sahrens 	if (ret != 0) {
3931fa9e4066Sahrens 		switch (errno) {
3932fa9e4066Sahrens 
3933fa9e4066Sahrens 		case ENOENT:
3934fa9e4066Sahrens 			/*
3935fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3936fa9e4066Sahrens 			 * above, but there may a race condition that has since
3937fa9e4066Sahrens 			 * destroyed the parent.
3938fa9e4066Sahrens 			 *
3939fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3940fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3941fa9e4066Sahrens 			 * dataset doesn't exist.
3942fa9e4066Sahrens 			 */
394399653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
394499653d4eSeschrock 			    "no such parent '%s'"), parent);
394599653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3946fa9e4066Sahrens 
394799653d4eSeschrock 		case EXDEV:
394899653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
394999653d4eSeschrock 			    "source and target pools differ"));
395099653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
395199653d4eSeschrock 			    errbuf));
3952fa9e4066Sahrens 
395399653d4eSeschrock 		default:
395499653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
395599653d4eSeschrock 			    errbuf));
395699653d4eSeschrock 		}
395799653d4eSeschrock 	}
3958fa9e4066Sahrens 
395999653d4eSeschrock 	return (ret);
396099653d4eSeschrock }
396199653d4eSeschrock 
396299653d4eSeschrock /*
396399653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
396499653d4eSeschrock  */
396599653d4eSeschrock int
396699653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
396799653d4eSeschrock {
396899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3969a4b8c9aaSAndrew Stormont 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
397099653d4eSeschrock 	int ret;
397199653d4eSeschrock 	char errbuf[1024];
397299653d4eSeschrock 
397399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
397499653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
397599653d4eSeschrock 
397699653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
397799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
397899653d4eSeschrock 		    "snapshots can not be promoted"));
397999653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
398099653d4eSeschrock 	}
398199653d4eSeschrock 
3982a4b8c9aaSAndrew Stormont 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
398399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
398499653d4eSeschrock 		    "not a cloned filesystem"));
398599653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
398699653d4eSeschrock 	}
398799653d4eSeschrock 
3988add927f8Sloli 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3989add927f8Sloli 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3990add927f8Sloli 
3991a4b8c9aaSAndrew Stormont 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
399299653d4eSeschrock 
399399653d4eSeschrock 	if (ret != 0) {
3994a4b8c9aaSAndrew Stormont 		switch (ret) {
399599653d4eSeschrock 		case EEXIST:
3996681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
399799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3998681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3999a4b8c9aaSAndrew Stormont 			    snapname, zhp->zfs_dmustats.dds_origin);
400099653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4001fa9e4066Sahrens 
4002fa9e4066Sahrens 		default:
4003a4b8c9aaSAndrew Stormont 			return (zfs_standard_error(hdl, ret, errbuf));
4004fa9e4066Sahrens 		}
4005fa9e4066Sahrens 	}
4006e9dbad6fSeschrock 	return (ret);
40071d452cf5Sahrens }
40081d452cf5Sahrens 
40094445fffbSMatthew Ahrens typedef struct snapdata {
40104445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
40114445fffbSMatthew Ahrens 	const char *sd_snapname;
40124445fffbSMatthew Ahrens } snapdata_t;
40134445fffbSMatthew Ahrens 
40144445fffbSMatthew Ahrens static int
40154445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
40164445fffbSMatthew Ahrens {
40174445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
40189adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
40194445fffbSMatthew Ahrens 	int rv = 0;
40204445fffbSMatthew Ahrens 
4021ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4022ca48f36fSKeith M Wesolowski 		(void) snprintf(name, sizeof (name),
4023ca48f36fSKeith M Wesolowski 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
40244445fffbSMatthew Ahrens 
4025ca48f36fSKeith M Wesolowski 		fnvlist_add_boolean(sd->sd_nvl, name);
40264445fffbSMatthew Ahrens 
4027ca48f36fSKeith M Wesolowski 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4028ca48f36fSKeith M Wesolowski 	}
40294445fffbSMatthew Ahrens 	zfs_close(zhp);
4030ca48f36fSKeith M Wesolowski 
40314445fffbSMatthew Ahrens 	return (rv);
40324445fffbSMatthew Ahrens }
40334445fffbSMatthew Ahrens 
40345cabbc6bSPrashanth Sreenivasa int
40355cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
40365cabbc6bSPrashanth Sreenivasa {
40375cabbc6bSPrashanth Sreenivasa 	int err;
40385cabbc6bSPrashanth Sreenivasa 	char errbuf[1024];
40395cabbc6bSPrashanth Sreenivasa 
40405cabbc6bSPrashanth Sreenivasa 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40410b2e8253Sloli 	    "cannot remap dataset '%s'"), fs);
40425cabbc6bSPrashanth Sreenivasa 
40435cabbc6bSPrashanth Sreenivasa 	err = lzc_remap(fs);
40445cabbc6bSPrashanth Sreenivasa 
40455cabbc6bSPrashanth Sreenivasa 	if (err != 0) {
40460b2e8253Sloli 		switch (err) {
40470b2e8253Sloli 		case ENOTSUP:
40480b2e8253Sloli 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40490b2e8253Sloli 			    "pool must be upgraded"));
40500b2e8253Sloli 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
40510b2e8253Sloli 			break;
40520b2e8253Sloli 		case EINVAL:
40530b2e8253Sloli 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
40540b2e8253Sloli 			break;
40550b2e8253Sloli 		default:
40560b2e8253Sloli 			(void) zfs_standard_error(hdl, err, errbuf);
40570b2e8253Sloli 			break;
40580b2e8253Sloli 		}
40595cabbc6bSPrashanth Sreenivasa 	}
40605cabbc6bSPrashanth Sreenivasa 
40615cabbc6bSPrashanth Sreenivasa 	return (err);
40625cabbc6bSPrashanth Sreenivasa }
40635cabbc6bSPrashanth Sreenivasa 
4064fa9e4066Sahrens /*
40654445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
40664445fffbSMatthew Ahrens  * created.
4067fa9e4066Sahrens  */
4068fa9e4066Sahrens int
40694445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4070fa9e4066Sahrens {
4071fa9e4066Sahrens 	int ret;
407299653d4eSeschrock 	char errbuf[1024];
40734445fffbSMatthew Ahrens 	nvpair_t *elem;
40744445fffbSMatthew Ahrens 	nvlist_t *errors;
4075fa9e4066Sahrens 
407699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40774445fffbSMatthew Ahrens 	    "cannot create snapshots "));
407899653d4eSeschrock 
40794445fffbSMatthew Ahrens 	elem = NULL;
40804445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
40814445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
40824445fffbSMatthew Ahrens 
40834445fffbSMatthew Ahrens 		/* validate the target name */
40844445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
40854445fffbSMatthew Ahrens 		    B_TRUE)) {
40864445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
40874445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
40884445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
40894445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
40904445fffbSMatthew Ahrens 		}
40914445fffbSMatthew Ahrens 	}
4092fa9e4066Sahrens 
4093e9316f76SJoe Stein 	/*
4094e9316f76SJoe Stein 	 * get pool handle for prop validation. assumes all snaps are in the
4095e9316f76SJoe Stein 	 * same pool, as does lzc_snapshot (below).
4096e9316f76SJoe Stein 	 */
40979adfa60dSMatthew Ahrens 	char pool[ZFS_MAX_DATASET_NAME_LEN];
4098e9316f76SJoe Stein 	elem = nvlist_next_nvpair(snaps, NULL);
4099e9316f76SJoe Stein 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4100e9316f76SJoe Stein 	pool[strcspn(pool, "/@")] = '\0';
4101e9316f76SJoe Stein 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4102e9316f76SJoe Stein 
41034445fffbSMatthew Ahrens 	if (props != NULL &&
41044445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4105*eb633035STom Caputi 	    props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4106e9316f76SJoe Stein 		zpool_close(zpool_hdl);
41074445fffbSMatthew Ahrens 		return (-1);
41084445fffbSMatthew Ahrens 	}
4109e9316f76SJoe Stein 	zpool_close(zpool_hdl);
4110bb0ade09Sahrens 
41114445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
41124445fffbSMatthew Ahrens 
41134445fffbSMatthew Ahrens 	if (ret != 0) {
41144445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
41154445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
41164445fffbSMatthew Ahrens 		    elem != NULL;
41174445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
41184445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
41194445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
41204445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
41214445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
41224445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
41234445fffbSMatthew Ahrens 			printed = B_TRUE;
4124bb0ade09Sahrens 		}
41254445fffbSMatthew Ahrens 		if (!printed) {
41264445fffbSMatthew Ahrens 			switch (ret) {
41274445fffbSMatthew Ahrens 			case EXDEV:
41284445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41294445fffbSMatthew Ahrens 				    "multiple snapshots of same "
41304445fffbSMatthew Ahrens 				    "fs not allowed"));
41314445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4132bb0ade09Sahrens 
41334445fffbSMatthew Ahrens 				break;
41344445fffbSMatthew Ahrens 			default:
41354445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
41364445fffbSMatthew Ahrens 			}
41374445fffbSMatthew Ahrens 		}
4138bb0ade09Sahrens 	}
4139bb0ade09Sahrens 
41404445fffbSMatthew Ahrens 	nvlist_free(props);
41414445fffbSMatthew Ahrens 	nvlist_free(errors);
41424445fffbSMatthew Ahrens 	return (ret);
41434445fffbSMatthew Ahrens }
41444445fffbSMatthew Ahrens 
41454445fffbSMatthew Ahrens int
41464445fffbSMatthew Ahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
41474445fffbSMatthew Ahrens     nvlist_t *props)
41484445fffbSMatthew Ahrens {
41494445fffbSMatthew Ahrens 	int ret;
41504445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
41519adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
41524445fffbSMatthew Ahrens 	char *cp;
41534445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
41544445fffbSMatthew Ahrens 	char errbuf[1024];
41554445fffbSMatthew Ahrens 
41564445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
41574445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
41584445fffbSMatthew Ahrens 
41594445fffbSMatthew Ahrens 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
41604445fffbSMatthew Ahrens 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41614445fffbSMatthew Ahrens 
41624445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
41634445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
41644445fffbSMatthew Ahrens 	*cp = '\0';
41654445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
4166fa9e4066Sahrens 
41674445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4168fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
4169fa9e4066Sahrens 		return (-1);
4170fa9e4066Sahrens 	}
4171fa9e4066Sahrens 
41724445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
41734445fffbSMatthew Ahrens 	if (recursive) {
41744445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
41754445fffbSMatthew Ahrens 	} else {
41764445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
4177681d9761SEric Taylor 	}
4178fa9e4066Sahrens 
41794445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
41804445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
4181fa9e4066Sahrens 	zfs_close(zhp);
4182fa9e4066Sahrens 	return (ret);
4183fa9e4066Sahrens }
4184fa9e4066Sahrens 
4185fa9e4066Sahrens /*
4186b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
4187b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
4188b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
4189b12a1c38Slling  * group.
4190fa9e4066Sahrens  */
4191b12a1c38Slling typedef struct rollback_data {
4192b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
4193b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
4194c391e322Sahrens 	boolean_t	cb_error;
4195c391e322Sahrens 	boolean_t	cb_force;
4196b12a1c38Slling } rollback_data_t;
4197b12a1c38Slling 
4198b12a1c38Slling static int
419978f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4200b12a1c38Slling {
4201b12a1c38Slling 	rollback_data_t *cbp = data;
420278f17100SMatthew Ahrens 	prop_changelist_t *clp;
420378f17100SMatthew Ahrens 
420478f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
420578f17100SMatthew Ahrens 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
420678f17100SMatthew Ahrens 	    cbp->cb_force ? MS_FORCE: 0);
420778f17100SMatthew Ahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
420878f17100SMatthew Ahrens 		cbp->cb_error = B_TRUE;
420978f17100SMatthew Ahrens 		zfs_close(zhp);
421078f17100SMatthew Ahrens 		return (0);
421178f17100SMatthew Ahrens 	}
421278f17100SMatthew Ahrens 	if (zfs_destroy(zhp, B_FALSE) != 0)
421378f17100SMatthew Ahrens 		cbp->cb_error = B_TRUE;
421478f17100SMatthew Ahrens 	else
421578f17100SMatthew Ahrens 		changelist_remove(clp, zhp->zfs_name);
421678f17100SMatthew Ahrens 	(void) changelist_postfix(clp);
421778f17100SMatthew Ahrens 	changelist_free(clp);
4218b12a1c38Slling 
421978f17100SMatthew Ahrens 	zfs_close(zhp);
422078f17100SMatthew Ahrens 	return (0);
422178f17100SMatthew Ahrens }
4222b12a1c38Slling 
422378f17100SMatthew Ahrens static int
422478f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
422578f17100SMatthew Ahrens {
422678f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
4227b12a1c38Slling 
422878f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
422978f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
423078f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
4231c391e322Sahrens 
423278f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4233b12a1c38Slling 	}
4234b12a1c38Slling 
4235b12a1c38Slling 	zfs_close(zhp);
4236b12a1c38Slling 	return (0);
4237b12a1c38Slling }
4238b12a1c38Slling 
4239b12a1c38Slling /*
4240b12a1c38Slling  * Given a dataset, rollback to a specific snapshot, discarding any
4241b12a1c38Slling  * data changes since then and making it the active dataset.
4242b12a1c38Slling  *
424378f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
424478f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
4245b12a1c38Slling  */
4246b12a1c38Slling int
4247c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4248b12a1c38Slling {
4249b12a1c38Slling 	rollback_data_t cb = { 0 };
42504ccbb6e7Sahrens 	int err;
42517b97dc1aSrm 	boolean_t restore_resv = 0;
4252f83b46baSPaul Dagnelie 	uint64_t old_volsize = 0, new_volsize;
42537b97dc1aSrm 	zfs_prop_t resv_prop;
4254b12a1c38Slling 
42554ccbb6e7Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
42564ccbb6e7Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
4257b12a1c38Slling 
4258b12a1c38Slling 	/*
42592e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
4260b12a1c38Slling 	 */
4261c391e322Sahrens 	cb.cb_force = force;
4262b12a1c38Slling 	cb.cb_target = snap->zfs_name;
4263b12a1c38Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
42640d8fa8f8SMartin Matuska 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
426578f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4266b12a1c38Slling 
4267c391e322Sahrens 	if (cb.cb_error)
4268c391e322Sahrens 		return (-1);
4269b12a1c38Slling 
4270b12a1c38Slling 	/*
4271b12a1c38Slling 	 * Now that we have verified that the snapshot is the latest,
4272b12a1c38Slling 	 * rollback to the given snapshot.
4273b12a1c38Slling 	 */
4274b12a1c38Slling 
42757b97dc1aSrm 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
42767b97dc1aSrm 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
42777b97dc1aSrm 			return (-1);
42787b97dc1aSrm 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
42797b97dc1aSrm 		restore_resv =
42807b97dc1aSrm 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
42817b97dc1aSrm 	}
42824ccbb6e7Sahrens 
4283b12a1c38Slling 	/*
428477b17137SAndriy Gapon 	 * Pass both the filesystem and the wanted snapshot names,
428577b17137SAndriy Gapon 	 * we would get an error back if the snapshot is destroyed or
428677b17137SAndriy Gapon 	 * a new snapshot is created before this request is processed.
4287b12a1c38Slling 	 */
428877b17137SAndriy Gapon 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
428995643f75SAndriy Gapon 	if (err != 0) {
429095643f75SAndriy Gapon 		char errbuf[1024];
429195643f75SAndriy Gapon 
429295643f75SAndriy Gapon 		(void) snprintf(errbuf, sizeof (errbuf),
42934ccbb6e7Sahrens 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
42944ccbb6e7Sahrens 		    zhp->zfs_name);
429595643f75SAndriy Gapon 		switch (err) {
429695643f75SAndriy Gapon 		case EEXIST:
429795643f75SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
429895643f75SAndriy Gapon 			    "there is a snapshot or bookmark more recent "
429995643f75SAndriy Gapon 			    "than '%s'"), snap->zfs_name);
430095643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
430195643f75SAndriy Gapon 			break;
430295643f75SAndriy Gapon 		case ESRCH:
430395643f75SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
430495643f75SAndriy Gapon 			    "'%s' is not found among snapshots of '%s'"),
430595643f75SAndriy Gapon 			    snap->zfs_name, zhp->zfs_name);
430695643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
430795643f75SAndriy Gapon 			break;
430895643f75SAndriy Gapon 		case EINVAL:
430995643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
431095643f75SAndriy Gapon 			break;
431195643f75SAndriy Gapon 		default:
431295643f75SAndriy Gapon 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
431395643f75SAndriy Gapon 		}
4314b9415e83Srm 		return (err);
4315b9415e83Srm 	}
43167b97dc1aSrm 
43177b97dc1aSrm 	/*
43187b97dc1aSrm 	 * For volumes, if the pre-rollback volsize matched the pre-
43197b97dc1aSrm 	 * rollback reservation and the volsize has changed then set
43207b97dc1aSrm 	 * the reservation property to the post-rollback volsize.
43217b97dc1aSrm 	 * Make a new handle since the rollback closed the dataset.
43227b97dc1aSrm 	 */
4323b9415e83Srm 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4324b9415e83Srm 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
43257b97dc1aSrm 		if (restore_resv) {
43267b97dc1aSrm 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
43277b97dc1aSrm 			if (old_volsize != new_volsize)
4328b9415e83Srm 				err = zfs_prop_set_int(zhp, resv_prop,
4329b9415e83Srm 				    new_volsize);
43307b97dc1aSrm 		}
43317b97dc1aSrm 		zfs_close(zhp);
43324ccbb6e7Sahrens 	}
43334ccbb6e7Sahrens 	return (err);
4334b12a1c38Slling }
4335b12a1c38Slling 
4336fa9e4066Sahrens /*
4337fa9e4066Sahrens  * Renames the given dataset.
4338fa9e4066Sahrens  */
4339fa9e4066Sahrens int
43406a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
43416a9cb0eaSEric Schrock     boolean_t force_unmount)
4342fa9e4066Sahrens {
434388f61deeSIgor Kozhukhov 	int ret = 0;
4344fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
4345fa9e4066Sahrens 	char *delim;
4346cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
4347cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
4348cdf5b4caSmmusante 	char *parentname = NULL;
43499adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
435099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
435199653d4eSeschrock 	char errbuf[1024];
4352fa9e4066Sahrens 
4353fa9e4066Sahrens 	/* if we have the same exact name, just return success */
4354fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
4355fa9e4066Sahrens 		return (0);
4356fa9e4066Sahrens 
435799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
435899653d4eSeschrock 	    "cannot rename to '%s'"), target);
435999653d4eSeschrock 
4360add927f8Sloli 	/* make sure source name is valid */
4361add927f8Sloli 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4362add927f8Sloli 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4363add927f8Sloli 
4364fa9e4066Sahrens 	/*
4365fa9e4066Sahrens 	 * Make sure the target name is valid
4366fa9e4066Sahrens 	 */
4367fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
436898579b20Snd 		if ((strchr(target, '@') == NULL) ||
436998579b20Snd 		    *target == '@') {
437098579b20Snd 			/*
437198579b20Snd 			 * Snapshot target name is abbreviated,
437298579b20Snd 			 * reconstruct full dataset name
437398579b20Snd 			 */
437498579b20Snd 			(void) strlcpy(parent, zhp->zfs_name,
437598579b20Snd 			    sizeof (parent));
437698579b20Snd 			delim = strchr(parent, '@');
437798579b20Snd 			if (strchr(target, '@') == NULL)
437898579b20Snd 				*(++delim) = '\0';
437998579b20Snd 			else
438098579b20Snd 				*delim = '\0';
438198579b20Snd 			(void) strlcat(parent, target, sizeof (parent));
438298579b20Snd 			target = parent;
438398579b20Snd 		} else {
438498579b20Snd 			/*
438598579b20Snd 			 * Make sure we're renaming within the same dataset.
438698579b20Snd 			 */
438798579b20Snd 			delim = strchr(target, '@');
438898579b20Snd 			if (strncmp(zhp->zfs_name, target, delim - target)
438998579b20Snd 			    != 0 || zhp->zfs_name[delim - target] != '@') {
439098579b20Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439198579b20Snd 				    "snapshots must be part of same "
439298579b20Snd 				    "dataset"));
439398579b20Snd 				return (zfs_error(hdl, EZFS_CROSSTARGET,
4394b1b8ab34Slling 				    errbuf));
439598579b20Snd 			}
4396fa9e4066Sahrens 		}
43975ac95da7SSerapheim Dimitropoulos 
4398f18faf3fSek 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
439998579b20Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4400fa9e4066Sahrens 	} else {
4401cdf5b4caSmmusante 		if (recursive) {
4402cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4403cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
4404cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4405cdf5b4caSmmusante 		}
4406cdf5b4caSmmusante 
4407f18faf3fSek 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
440898579b20Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4409e9dbad6fSeschrock 
4410fa9e4066Sahrens 		/* validate parents */
4411d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4412fa9e4066Sahrens 			return (-1);
4413fa9e4066Sahrens 
4414fa9e4066Sahrens 		/* make sure we're in the same pool */
4415fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4416fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4417fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
441899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
441999653d4eSeschrock 			    "datasets must be within same pool"));
442099653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4421fa9e4066Sahrens 		}
4422f2fdf992Snd 
4423f2fdf992Snd 		/* new name cannot be a child of the current dataset name */
4424d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
4425f2fdf992Snd 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4426d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
4427f2fdf992Snd 			    "current dataset name"));
4428f2fdf992Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4429f2fdf992Snd 		}
4430fa9e4066Sahrens 	}
4431fa9e4066Sahrens 
443299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
443399653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
443499653d4eSeschrock 
4435fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4436fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
443799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
443899653d4eSeschrock 		    "dataset is used in a non-global zone"));
443999653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4440fa9e4066Sahrens 	}
4441fa9e4066Sahrens 
4442cdf5b4caSmmusante 	if (recursive) {
4443f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4444f0c5ee21Smmusante 		if (parentname == NULL) {
4445f0c5ee21Smmusante 			ret = -1;
4446f0c5ee21Smmusante 			goto error;
4447f0c5ee21Smmusante 		}
4448cdf5b4caSmmusante 		delim = strchr(parentname, '@');
4449cdf5b4caSmmusante 		*delim = '\0';
4450990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4451cdf5b4caSmmusante 		if (zhrp == NULL) {
4452f0c5ee21Smmusante 			ret = -1;
4453f0c5ee21Smmusante 			goto error;
4454cdf5b4caSmmusante 		}
445533cde0d0SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
44566a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
44576a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
4458cdf5b4caSmmusante 			return (-1);
4459cdf5b4caSmmusante 
4460cdf5b4caSmmusante 		if (changelist_haszonedchild(cl)) {
4461cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4462cdf5b4caSmmusante 			    "child dataset with inherited mountpoint is used "
4463cdf5b4caSmmusante 			    "in a non-global zone"));
4464cdf5b4caSmmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4465f83b46baSPaul Dagnelie 			ret = -1;
4466cdf5b4caSmmusante 			goto error;
4467cdf5b4caSmmusante 		}
4468cdf5b4caSmmusante 
4469cdf5b4caSmmusante 		if ((ret = changelist_prefix(cl)) != 0)
4470cdf5b4caSmmusante 			goto error;
4471cdf5b4caSmmusante 	}
4472fa9e4066Sahrens 
4473e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
4474fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4475fa9e4066Sahrens 	else
4476fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4477fa9e4066Sahrens 
447898579b20Snd 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4479e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
448098579b20Snd 
4481cdf5b4caSmmusante 	zc.zc_cookie = recursive;
4482cdf5b4caSmmusante 
4483ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4484cdf5b4caSmmusante 		/*
4485cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
4486cdf5b4caSmmusante 		 * be in zc.zc_name
4487cdf5b4caSmmusante 		 */
4488cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
44893cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
4490cdf5b4caSmmusante 
4491cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
4492cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4493cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
4494cdf5b4caSmmusante 			    "with the new name"));
4495a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4496*eb633035STom Caputi 		} else if (errno == EACCES) {
4497*eb633035STom Caputi 			if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) ==
4498*eb633035STom Caputi 			    ZIO_CRYPT_OFF) {
4499*eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4500*eb633035STom Caputi 				    "cannot rename an unencrypted dataset to "
4501*eb633035STom Caputi 				    "be a decendent of an encrypted one"));
4502*eb633035STom Caputi 			} else {
4503*eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4504*eb633035STom Caputi 				    "cannot move encryption child outside of "
4505*eb633035STom Caputi 				    "its encryption root"));
4506*eb633035STom Caputi 			}
4507*eb633035STom Caputi 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4508cdf5b4caSmmusante 		} else {
4509cdf5b4caSmmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4510cdf5b4caSmmusante 		}
4511fa9e4066Sahrens 
4512fa9e4066Sahrens 		/*
4513fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
4514fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
4515fa9e4066Sahrens 		 */
451633cde0d0SMatthew Ahrens 		if (cl != NULL)
4517cdf5b4caSmmusante 			(void) changelist_postfix(cl);
4518fa9e4066Sahrens 	} else {
451933cde0d0SMatthew Ahrens 		if (cl != NULL) {
4520cdf5b4caSmmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
4521cdf5b4caSmmusante 			ret = changelist_postfix(cl);
4522cdf5b4caSmmusante 		}
4523fa9e4066Sahrens 	}
4524fa9e4066Sahrens 
4525fa9e4066Sahrens error:
452633cde0d0SMatthew Ahrens 	if (parentname != NULL) {
4527cdf5b4caSmmusante 		free(parentname);
4528cdf5b4caSmmusante 	}
452933cde0d0SMatthew Ahrens 	if (zhrp != NULL) {
4530cdf5b4caSmmusante 		zfs_close(zhrp);
4531cdf5b4caSmmusante 	}
453233cde0d0SMatthew Ahrens 	if (cl != NULL) {
4533cdf5b4caSmmusante 		changelist_free(cl);
4534cdf5b4caSmmusante 	}
4535fa9e4066Sahrens 	return (ret);
4536fa9e4066Sahrens }
4537fa9e4066Sahrens 
4538e9dbad6fSeschrock nvlist_t *
4539e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4540e9dbad6fSeschrock {
4541e9dbad6fSeschrock 	return (zhp->zfs_user_props);
4542e9dbad6fSeschrock }
4543e9dbad6fSeschrock 
454492241e0bSTom Erickson nvlist_t *
454592241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
454692241e0bSTom Erickson {
454792241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
454892241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
454992241e0bSTom Erickson 			return (NULL);
455092241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
455192241e0bSTom Erickson }
455292241e0bSTom Erickson 
4553b1b8ab34Slling /*
4554b1b8ab34Slling  * This function is used by 'zfs list' to determine the exact set of columns to
4555b1b8ab34Slling  * display, and their maximum widths.  This does two main things:
4556b1b8ab34Slling  *
4557b1b8ab34Slling  *      - If this is a list of all properties, then expand the list to include
4558b1b8ab34Slling  *        all native properties, and set a flag so that for each dataset we look
4559b1b8ab34Slling  *        for new unique user properties and add them to the list.
4560b1b8ab34Slling  *
4561b1b8ab34Slling  *      - For non fixed-width properties, keep track of the maximum width seen
456292241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
456392241e0bSTom Erickson  *        requested received property values, we also need to compute the width
456492241e0bSTom Erickson  *        of the RECEIVED column.
4565b1b8ab34Slling  */
4566b1b8ab34Slling int
456743d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
456843d68d68SYuri Pankov     boolean_t literal)
4569b1b8ab34Slling {
4570b1b8ab34Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4571990b4856Slling 	zprop_list_t *entry;
4572990b4856Slling 	zprop_list_t **last, **start;
4573b1b8ab34Slling 	nvlist_t *userprops, *propval;
4574b1b8ab34Slling 	nvpair_t *elem;
4575b1b8ab34Slling 	char *strval;
4576b1b8ab34Slling 	char buf[ZFS_MAXPROPLEN];
4577b1b8ab34Slling 
4578990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4579b1b8ab34Slling 		return (-1);
4580e9dbad6fSeschrock 
4581e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4582e9dbad6fSeschrock 
4583e9dbad6fSeschrock 	entry = *plp;
4584e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4585e9dbad6fSeschrock 		/*
4586e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4587e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4588e9dbad6fSeschrock 		 * non-native property.
4589e9dbad6fSeschrock 		 */
4590e9dbad6fSeschrock 		start = plp;
4591e9dbad6fSeschrock 		while (*start != NULL) {
4592990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4593e9dbad6fSeschrock 				break;
4594e9dbad6fSeschrock 			start = &(*start)->pl_next;
4595e9dbad6fSeschrock 		}
4596e9dbad6fSeschrock 
4597e9dbad6fSeschrock 		elem = NULL;
4598e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4599e9dbad6fSeschrock 			/*
4600e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4601e9dbad6fSeschrock 			 */
4602e9dbad6fSeschrock 			for (last = start; *last != NULL;
4603e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4604e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4605e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4606e9dbad6fSeschrock 					break;
4607e9dbad6fSeschrock 			}
4608e9dbad6fSeschrock 
4609e9dbad6fSeschrock 			if (*last == NULL) {
4610e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4611990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4612e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4613e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4614e9dbad6fSeschrock 					free(entry);
4615e9dbad6fSeschrock 					return (-1);
4616e9dbad6fSeschrock 				}
4617e9dbad6fSeschrock 
4618990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4619e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4620e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4621e9dbad6fSeschrock 				*last = entry;
4622e9dbad6fSeschrock 			}
4623e9dbad6fSeschrock 		}
4624e9dbad6fSeschrock 	}
4625e9dbad6fSeschrock 
4626e9dbad6fSeschrock 	/*
4627e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4628e9dbad6fSeschrock 	 */
4629e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
463043d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
4631e9dbad6fSeschrock 			continue;
4632e9dbad6fSeschrock 
4633990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4634e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
463543d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4636e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4637e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4638e9dbad6fSeschrock 			}
463992241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
464092241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
464143d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
464292241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
464392241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
464492241e0bSTom Erickson 		} else {
464592241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
464692241e0bSTom Erickson 			    &propval) == 0) {
464792241e0bSTom Erickson 				verify(nvlist_lookup_string(propval,
464892241e0bSTom Erickson 				    ZPROP_VALUE, &strval) == 0);
464992241e0bSTom Erickson 				if (strlen(strval) > entry->pl_width)
465092241e0bSTom Erickson 					entry->pl_width = strlen(strval);
465192241e0bSTom Erickson 			}
465292241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
465392241e0bSTom Erickson 			    entry->pl_user_prop,
465443d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
465592241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
465692241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
4657e9dbad6fSeschrock 		}
4658e9dbad6fSeschrock 	}
4659e9dbad6fSeschrock 
4660e9dbad6fSeschrock 	return (0);
4661e9dbad6fSeschrock }
4662ecd6cf80Smarks 
4663ecd6cf80Smarks int
4664ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4665743a77edSAlan Wright     char *resource, void *export, void *sharetab,
4666743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
4667ecd6cf80Smarks {
4668ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4669ecd6cf80Smarks 	int error;
4670ecd6cf80Smarks 
4671ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4672ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4673743a77edSAlan Wright 	if (resource)
4674743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4675ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4676ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4677da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4678ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4679ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4680ecd6cf80Smarks 	return (error);
4681ecd6cf80Smarks }
46822e5e9e19SSanjeev Bagewadi 
46832e5e9e19SSanjeev Bagewadi void
46842e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
46852e5e9e19SSanjeev Bagewadi {
46862e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
46872e5e9e19SSanjeev Bagewadi 
46882e5e9e19SSanjeev Bagewadi 	/*
46892e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
46902e5e9e19SSanjeev Bagewadi 	 * properties.
46912e5e9e19SSanjeev Bagewadi 	 */
46922e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
46932e5e9e19SSanjeev Bagewadi 
46942e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
46952e5e9e19SSanjeev Bagewadi 
46962e5e9e19SSanjeev Bagewadi 	while (curr) {
46972e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
46982e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
46992e5e9e19SSanjeev Bagewadi 
470014843421SMatthew Ahrens 		/*
4701faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4702faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4703faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4704faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4705faaa6415SEric Schrock 		 * software, for example).
470614843421SMatthew Ahrens 		 */
470714843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
47082e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
47092e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
47102e5e9e19SSanjeev Bagewadi 		curr = next;
47112e5e9e19SSanjeev Bagewadi 	}
47122e5e9e19SSanjeev Bagewadi }
4713743a77edSAlan Wright 
4714743a77edSAlan Wright static int
4715743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4716743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4717743a77edSAlan Wright {
4718743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4719743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4720743a77edSAlan Wright 	int error;
4721743a77edSAlan Wright 
4722743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4723743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4724743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4725743a77edSAlan Wright 
4726743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4727743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4728743a77edSAlan Wright 			(void) no_memory(hdl);
472930925561SChris Williamson 			return (0);
4730743a77edSAlan Wright 		}
4731743a77edSAlan Wright 	}
4732743a77edSAlan Wright 
4733743a77edSAlan Wright 	switch (cmd) {
4734743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4735743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4736743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4737743a77edSAlan Wright 		break;
4738743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4739743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4740743a77edSAlan Wright 		    resource1) != 0) {
4741743a77edSAlan Wright 				(void) no_memory(hdl);
4742743a77edSAlan Wright 				return (-1);
4743743a77edSAlan Wright 		}
4744743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4745743a77edSAlan Wright 		    resource2) != 0) {
4746743a77edSAlan Wright 				(void) no_memory(hdl);
4747743a77edSAlan Wright 				return (-1);
4748743a77edSAlan Wright 		}
4749743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4750743a77edSAlan Wright 			nvlist_free(nvlist);
4751743a77edSAlan Wright 			return (-1);
4752743a77edSAlan Wright 		}
4753743a77edSAlan Wright 		break;
4754743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4755743a77edSAlan Wright 		break;
4756743a77edSAlan Wright 	default:
4757743a77edSAlan Wright 		return (-1);
4758743a77edSAlan Wright 	}
4759743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4760aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlist);
4761743a77edSAlan Wright 	return (error);
4762743a77edSAlan Wright }
4763743a77edSAlan Wright 
4764743a77edSAlan Wright int
4765743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4766743a77edSAlan Wright     char *path, char *resource)
4767743a77edSAlan Wright {
4768743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4769743a77edSAlan Wright 	    resource, NULL));
4770743a77edSAlan Wright }
4771743a77edSAlan Wright 
4772743a77edSAlan Wright int
4773743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4774743a77edSAlan Wright     char *path, char *resource)
4775743a77edSAlan Wright {
4776743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4777743a77edSAlan Wright 	    resource, NULL));
4778743a77edSAlan Wright }
4779743a77edSAlan Wright 
4780743a77edSAlan Wright int
4781743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4782743a77edSAlan Wright {
4783743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4784743a77edSAlan Wright 	    NULL, NULL));
4785743a77edSAlan Wright }
4786743a77edSAlan Wright 
4787743a77edSAlan Wright int
4788743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4789743a77edSAlan Wright     char *oldname, char *newname)
4790743a77edSAlan Wright {
4791743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4792743a77edSAlan Wright 	    oldname, newname));
4793743a77edSAlan Wright }
479414843421SMatthew Ahrens 
479514843421SMatthew Ahrens int
479614843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
479714843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
479814843421SMatthew Ahrens {
479914843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
480014843421SMatthew Ahrens 	zfs_useracct_t buf[100];
480170f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
480270f56fa6SYuri Pankov 	int ret;
480314843421SMatthew Ahrens 
480419b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
480514843421SMatthew Ahrens 
480614843421SMatthew Ahrens 	zc.zc_objset_type = type;
480714843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
480814843421SMatthew Ahrens 
480970f56fa6SYuri Pankov 	for (;;) {
481014843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
481114843421SMatthew Ahrens 
481214843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
481370f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
48143b2aab18SMatthew Ahrens 			char errbuf[1024];
481570f56fa6SYuri Pankov 
481670f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
481770f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
481870f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
481970f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
482070f56fa6SYuri Pankov 		}
482170f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
482214843421SMatthew Ahrens 			break;
482314843421SMatthew Ahrens 
482414843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
482570f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
482670f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
482770f56fa6SYuri Pankov 				return (ret);
482814843421SMatthew Ahrens 			zua++;
482914843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
483014843421SMatthew Ahrens 		}
483114843421SMatthew Ahrens 	}
483214843421SMatthew Ahrens 
483370f56fa6SYuri Pankov 	return (0);
483414843421SMatthew Ahrens }
4835842727c2SChris Kirby 
48363b2aab18SMatthew Ahrens struct holdarg {
48373b2aab18SMatthew Ahrens 	nvlist_t *nvl;
48383b2aab18SMatthew Ahrens 	const char *snapname;
48393b2aab18SMatthew Ahrens 	const char *tag;
48403b2aab18SMatthew Ahrens 	boolean_t recursive;
4841bb6e7075SMatthew Ahrens 	int error;
48423b2aab18SMatthew Ahrens };
48433b2aab18SMatthew Ahrens 
48443b2aab18SMatthew Ahrens static int
48453b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
48463b2aab18SMatthew Ahrens {
48473b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
48489adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
48493b2aab18SMatthew Ahrens 	int rv = 0;
48503b2aab18SMatthew Ahrens 
48513b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
48523b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
48533b2aab18SMatthew Ahrens 
4854a7a845e4SSteven Hartland 	if (lzc_exists(name))
48553b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
48563b2aab18SMatthew Ahrens 
48573b2aab18SMatthew Ahrens 	if (ha->recursive)
48583b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
48593b2aab18SMatthew Ahrens 	zfs_close(zhp);
48603b2aab18SMatthew Ahrens 	return (rv);
48613b2aab18SMatthew Ahrens }
48623b2aab18SMatthew Ahrens 
4863842727c2SChris Kirby int
4864842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4865a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4866842727c2SChris Kirby {
48673b2aab18SMatthew Ahrens 	int ret;
48683b2aab18SMatthew Ahrens 	struct holdarg ha;
4869842727c2SChris Kirby 
48703b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
48713b2aab18SMatthew Ahrens 	ha.snapname = snapname;
48723b2aab18SMatthew Ahrens 	ha.tag = tag;
48733b2aab18SMatthew Ahrens 	ha.recursive = recursive;
48743b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4875013023d4SMartin Matuska 
4876a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4877a7a845e4SSteven Hartland 		char errbuf[1024];
4878a7a845e4SSteven Hartland 
4879013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4880013023d4SMartin Matuska 		ret = ENOENT;
4881a7a845e4SSteven Hartland 		(void) snprintf(errbuf, sizeof (errbuf),
4882a7a845e4SSteven Hartland 		    dgettext(TEXT_DOMAIN,
4883a7a845e4SSteven Hartland 		    "cannot hold snapshot '%s@%s'"),
4884a7a845e4SSteven Hartland 		    zhp->zfs_name, snapname);
4885a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4886013023d4SMartin Matuska 		return (ret);
4887013023d4SMartin Matuska 	}
4888013023d4SMartin Matuska 
4889a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
48903b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4891a7f53a56SChris Kirby 
4892a7a845e4SSteven Hartland 	return (ret);
4893a7a845e4SSteven Hartland }
4894a7a845e4SSteven Hartland 
4895a7a845e4SSteven Hartland int
4896a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4897a7a845e4SSteven Hartland {
4898a7a845e4SSteven Hartland 	int ret;
4899a7a845e4SSteven Hartland 	nvlist_t *errors;
4900a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4901a7a845e4SSteven Hartland 	char errbuf[1024];
4902a7a845e4SSteven Hartland 	nvpair_t *elem;
4903a7a845e4SSteven Hartland 
4904a7a845e4SSteven Hartland 	errors = NULL;
4905a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4906a7a845e4SSteven Hartland 
4907a7a845e4SSteven Hartland 	if (ret == 0) {
4908a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4909a7a845e4SSteven Hartland 		fnvlist_free(errors);
49103b2aab18SMatthew Ahrens 		return (0);
4911a7a845e4SSteven Hartland 	}
4912842727c2SChris Kirby 
4913a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
49143b2aab18SMatthew Ahrens 		/* no hold-specific errors */
49153b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
49163b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
49173b2aab18SMatthew Ahrens 		switch (ret) {
49183b2aab18SMatthew Ahrens 		case ENOTSUP:
49193b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49203b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
49213b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
49223b2aab18SMatthew Ahrens 			break;
49233b2aab18SMatthew Ahrens 		case EINVAL:
49243b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49253b2aab18SMatthew Ahrens 			break;
49263b2aab18SMatthew Ahrens 		default:
49273b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
49283b2aab18SMatthew Ahrens 		}
49293b2aab18SMatthew Ahrens 	}
4930842727c2SChris Kirby 
49313b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
49323b2aab18SMatthew Ahrens 	    elem != NULL;
49333b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
49343b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
49353b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
49363b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
49373b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
493815508ac0SChris Kirby 		case E2BIG:
493915508ac0SChris Kirby 			/*
494015508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
494115508ac0SChris Kirby 			 * prepended. So even if we passed the length check
494215508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
494315508ac0SChris Kirby 			 * up being slightly too long.
494415508ac0SChris Kirby 			 */
49453b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
49463b2aab18SMatthew Ahrens 			break;
4947842727c2SChris Kirby 		case EINVAL:
49483b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49493b2aab18SMatthew Ahrens 			break;
4950842727c2SChris Kirby 		case EEXIST:
49513b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
49523b2aab18SMatthew Ahrens 			break;
4953842727c2SChris Kirby 		default:
49543b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
49553b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4956842727c2SChris Kirby 		}
4957842727c2SChris Kirby 	}
4958842727c2SChris Kirby 
49593b2aab18SMatthew Ahrens 	fnvlist_free(errors);
49603b2aab18SMatthew Ahrens 	return (ret);
49613b2aab18SMatthew Ahrens }
49623b2aab18SMatthew Ahrens 
49633b2aab18SMatthew Ahrens static int
49643b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
49653b2aab18SMatthew Ahrens {
49663b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
49679adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
49683b2aab18SMatthew Ahrens 	int rv = 0;
4969bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
49703b2aab18SMatthew Ahrens 
49713b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
49723b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
49733b2aab18SMatthew Ahrens 
4974bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4975bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4976bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4977bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4978bb6e7075SMatthew Ahrens 	} else {
4979bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4980bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4981bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4982bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
49833b2aab18SMatthew Ahrens 	}
49843b2aab18SMatthew Ahrens 
49853b2aab18SMatthew Ahrens 	if (ha->recursive)
49863b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
49873b2aab18SMatthew Ahrens 	zfs_close(zhp);
49883b2aab18SMatthew Ahrens 	return (rv);
4989842727c2SChris Kirby }
4990842727c2SChris Kirby 
4991842727c2SChris Kirby int
4992842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4993842727c2SChris Kirby     boolean_t recursive)
4994842727c2SChris Kirby {
49953b2aab18SMatthew Ahrens 	int ret;
49963b2aab18SMatthew Ahrens 	struct holdarg ha;
4997a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
49983b2aab18SMatthew Ahrens 	nvpair_t *elem;
4999842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
5000013023d4SMartin Matuska 	char errbuf[1024];
5001842727c2SChris Kirby 
50023b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
50033b2aab18SMatthew Ahrens 	ha.snapname = snapname;
50043b2aab18SMatthew Ahrens 	ha.tag = tag;
50053b2aab18SMatthew Ahrens 	ha.recursive = recursive;
5006bb6e7075SMatthew Ahrens 	ha.error = 0;
50073b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
5008013023d4SMartin Matuska 
5009a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
5010013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
5011bb6e7075SMatthew Ahrens 		ret = ha.error;
5012013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
5013013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
5014013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
5015013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
5016bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
5017bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5018bb6e7075SMatthew Ahrens 		} else {
5019bb6e7075SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
5020bb6e7075SMatthew Ahrens 		}
5021013023d4SMartin Matuska 		return (ret);
5022013023d4SMartin Matuska 	}
5023013023d4SMartin Matuska 
50243b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
50253b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
5026842727c2SChris Kirby 
5027a7a845e4SSteven Hartland 	if (ret == 0) {
5028a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
5029a7a845e4SSteven Hartland 		fnvlist_free(errors);
50303b2aab18SMatthew Ahrens 		return (0);
5031a7a845e4SSteven Hartland 	}
50323b2aab18SMatthew Ahrens 
5033a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
50343b2aab18SMatthew Ahrens 		/* no hold-specific errors */
5035842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
50363b2aab18SMatthew Ahrens 		    "cannot release"));
5037842727c2SChris Kirby 		switch (errno) {
5038842727c2SChris Kirby 		case ENOTSUP:
5039842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5040842727c2SChris Kirby 			    "pool must be upgraded"));
50413b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
50423b2aab18SMatthew Ahrens 			break;
50433b2aab18SMatthew Ahrens 		default:
50443b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
50453b2aab18SMatthew Ahrens 		}
50463b2aab18SMatthew Ahrens 	}
50473b2aab18SMatthew Ahrens 
50483b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
50493b2aab18SMatthew Ahrens 	    elem != NULL;
50503b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
50513b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
50523b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
50533b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
50543b2aab18SMatthew Ahrens 		    nvpair_name(elem));
50553b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
50563b2aab18SMatthew Ahrens 		case ESRCH:
50573b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
50583b2aab18SMatthew Ahrens 			break;
5059842727c2SChris Kirby 		case EINVAL:
50603b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
50613b2aab18SMatthew Ahrens 			break;
5062842727c2SChris Kirby 		default:
50633b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
50643b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
5065842727c2SChris Kirby 		}
5066842727c2SChris Kirby 	}
5067842727c2SChris Kirby 
50683b2aab18SMatthew Ahrens 	fnvlist_free(errors);
50693b2aab18SMatthew Ahrens 	return (ret);
5070842727c2SChris Kirby }
5071ca45db41SChris Kirby 
50721af68beaSAlexander Stetsenko int
50731af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
50741af68beaSAlexander Stetsenko {
50751af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
50761af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
50771af68beaSAlexander Stetsenko 	int nvsz = 2048;
50781af68beaSAlexander Stetsenko 	void *nvbuf;
50791af68beaSAlexander Stetsenko 	int err = 0;
50803b2aab18SMatthew Ahrens 	char errbuf[1024];
50811af68beaSAlexander Stetsenko 
50821af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
50831af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
50841af68beaSAlexander Stetsenko 
50851af68beaSAlexander Stetsenko tryagain:
50861af68beaSAlexander Stetsenko 
50871af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
50881af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
50891af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
50901af68beaSAlexander Stetsenko 		goto out;
50911af68beaSAlexander Stetsenko 	}
50921af68beaSAlexander Stetsenko 
50931af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
50941af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
50951af68beaSAlexander Stetsenko 
50969adfa60dSMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
50971af68beaSAlexander Stetsenko 
5098d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
50991af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
51001af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
51011af68beaSAlexander Stetsenko 		    zc.zc_name);
51021af68beaSAlexander Stetsenko 		switch (errno) {
51031af68beaSAlexander Stetsenko 		case ENOMEM:
51041af68beaSAlexander Stetsenko 			free(nvbuf);
51051af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
51061af68beaSAlexander Stetsenko 			goto tryagain;
51071af68beaSAlexander Stetsenko 
51081af68beaSAlexander Stetsenko 		case ENOTSUP:
51091af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51101af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
51111af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51121af68beaSAlexander Stetsenko 			break;
51131af68beaSAlexander Stetsenko 		case EINVAL:
51141af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51151af68beaSAlexander Stetsenko 			break;
51161af68beaSAlexander Stetsenko 		case ENOENT:
51171af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
51181af68beaSAlexander Stetsenko 			break;
51191af68beaSAlexander Stetsenko 		default:
51201af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
51211af68beaSAlexander Stetsenko 			break;
51221af68beaSAlexander Stetsenko 		}
51231af68beaSAlexander Stetsenko 	} else {
51241af68beaSAlexander Stetsenko 		/* success */
51251af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
51261af68beaSAlexander Stetsenko 		if (rc) {
51271af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
51281af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
51291af68beaSAlexander Stetsenko 			    zc.zc_name);
51301af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
51311af68beaSAlexander Stetsenko 		}
51321af68beaSAlexander Stetsenko 	}
51331af68beaSAlexander Stetsenko 
51341af68beaSAlexander Stetsenko 	free(nvbuf);
51351af68beaSAlexander Stetsenko out:
51361af68beaSAlexander Stetsenko 	return (err);
51371af68beaSAlexander Stetsenko }
51381af68beaSAlexander Stetsenko 
51391af68beaSAlexander Stetsenko int
51401af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
51411af68beaSAlexander Stetsenko {
51421af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
51431af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
51441af68beaSAlexander Stetsenko 	char *nvbuf;
51453b2aab18SMatthew Ahrens 	char errbuf[1024];
51461af68beaSAlexander Stetsenko 	size_t nvsz;
51471af68beaSAlexander Stetsenko 	int err;
51481af68beaSAlexander Stetsenko 
51491af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
51501af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
51511af68beaSAlexander Stetsenko 
51521af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
51531af68beaSAlexander Stetsenko 	assert(err == 0);
51541af68beaSAlexander Stetsenko 
51551af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
51561af68beaSAlexander Stetsenko 
51571af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
51581af68beaSAlexander Stetsenko 	assert(err == 0);
51591af68beaSAlexander Stetsenko 
51601af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
51611af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
51621af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
51631af68beaSAlexander Stetsenko 
51641af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
51651af68beaSAlexander Stetsenko 
51661af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
51671af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
51681af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
51691af68beaSAlexander Stetsenko 		    zc.zc_name);
51701af68beaSAlexander Stetsenko 		switch (errno) {
51711af68beaSAlexander Stetsenko 		case ENOTSUP:
51721af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51731af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
51741af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51751af68beaSAlexander Stetsenko 			break;
51761af68beaSAlexander Stetsenko 		case EINVAL:
51771af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51781af68beaSAlexander Stetsenko 			break;
51791af68beaSAlexander Stetsenko 		case ENOENT:
51801af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
51811af68beaSAlexander Stetsenko 			break;
51821af68beaSAlexander Stetsenko 		default:
51831af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
51841af68beaSAlexander Stetsenko 			break;
51851af68beaSAlexander Stetsenko 		}
51861af68beaSAlexander Stetsenko 	}
51871af68beaSAlexander Stetsenko 
51881af68beaSAlexander Stetsenko 	free(nvbuf);
51891af68beaSAlexander Stetsenko 
51901af68beaSAlexander Stetsenko 	return (err);
51911af68beaSAlexander Stetsenko }
51921af68beaSAlexander Stetsenko 
51931af68beaSAlexander Stetsenko int
51941af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
51951af68beaSAlexander Stetsenko {
51963b2aab18SMatthew Ahrens 	int err;
51973b2aab18SMatthew Ahrens 	char errbuf[1024];
51981af68beaSAlexander Stetsenko 
51993b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
52001af68beaSAlexander Stetsenko 
52013b2aab18SMatthew Ahrens 	if (err != 0) {
52023b2aab18SMatthew Ahrens 		libzfs_handle_t *hdl = zhp->zfs_hdl;
52031af68beaSAlexander Stetsenko 
52041af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
52051af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
52063b2aab18SMatthew Ahrens 		    zhp->zfs_name);
52073b2aab18SMatthew Ahrens 		switch (err) {
52081af68beaSAlexander Stetsenko 		case ENOTSUP:
52091af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52101af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
52111af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
52121af68beaSAlexander Stetsenko 			break;
52131af68beaSAlexander Stetsenko 		case EINVAL:
52141af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
52151af68beaSAlexander Stetsenko 			break;
52161af68beaSAlexander Stetsenko 		case ENOENT:
52171af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
52181af68beaSAlexander Stetsenko 			break;
52191af68beaSAlexander Stetsenko 		default:
52201af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
52211af68beaSAlexander Stetsenko 			break;
52221af68beaSAlexander Stetsenko 		}
52231af68beaSAlexander Stetsenko 	}
52241af68beaSAlexander Stetsenko 
52251af68beaSAlexander Stetsenko 	return (err);
52261af68beaSAlexander Stetsenko }
52271af68beaSAlexander Stetsenko 
52283e30c24aSWill Andrews /*
5229b73ccab0SMike Gerdts  * The theory of raidz space accounting
5230b73ccab0SMike Gerdts  *
5231b73ccab0SMike Gerdts  * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5232b73ccab0SMike Gerdts  * will "reference" 128KB, even though it allocates more than that, to store the
5233b73ccab0SMike Gerdts  * parity information (and perhaps skip sectors). This concept of the
5234b73ccab0SMike Gerdts  * "referenced" (and other DMU space accounting) being lower than the allocated
5235b73ccab0SMike Gerdts  * space by a constant factor is called "raidz deflation."
5236b73ccab0SMike Gerdts  *
5237b73ccab0SMike Gerdts  * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5238b73ccab0SMike Gerdts  * block size. However, zvols typically have a much smaller block size (default
5239b73ccab0SMike Gerdts  * 8KB). These smaller blocks may require proportionally much more parity
5240b73ccab0SMike Gerdts  * information (and perhaps skip sectors). In this case, the change to the
5241b73ccab0SMike Gerdts  * "referenced" property may be much more than the logical block size.
5242b73ccab0SMike Gerdts  *
5243b73ccab0SMike Gerdts  * Suppose a raidz vdev has 5 disks with ashift=12.  A 128k block may be written
5244b73ccab0SMike Gerdts  * as follows.
5245b73ccab0SMike Gerdts  *
5246b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5247b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5248b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5249b73ccab0SMike Gerdts  * |  P0   |  D0   |  D8   |  D16  |  D24  |
5250b73ccab0SMike Gerdts  * |  P1   |  D1   |  D9   |  D17  |  D25  |
5251b73ccab0SMike Gerdts  * |  P2   |  D2   |  D10  |  D18  |  D26  |
5252b73ccab0SMike Gerdts  * |  P3   |  D3   |  D11  |  D19  |  D27  |
5253b73ccab0SMike Gerdts  * |  P4   |  D4   |  D12  |  D20  |  D28  |
5254b73ccab0SMike Gerdts  * |  P5   |  D5   |  D13  |  D21  |  D29  |
5255b73ccab0SMike Gerdts  * |  P6   |  D6   |  D14  |  D22  |  D30  |
5256b73ccab0SMike Gerdts  * |  P7   |  D7   |  D15  |  D23  |  D31  |
5257b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5258b73ccab0SMike Gerdts  *
5259b73ccab0SMike Gerdts  * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5260b73ccab0SMike Gerdts  * sectors.  The dataset's referenced will increase by 128k and the pool's
5261b73ccab0SMike Gerdts  * allocated and free properties will be adjusted by 160k.
5262b73ccab0SMike Gerdts  *
5263b73ccab0SMike Gerdts  * A 4k block written to the same raidz vdev will require two 4k sectors.  The
5264b73ccab0SMike Gerdts  * blank cells represent unallocated space.
5265b73ccab0SMike Gerdts  *
5266b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5267b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5268b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5269b73ccab0SMike Gerdts  * |  P0   |  D0   |       |       |       |
5270b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5271b73ccab0SMike Gerdts  *
5272b73ccab0SMike Gerdts  * Above, notice that the 4k block required one sector for parity and another
5273b73ccab0SMike Gerdts  * for data.  vdev_raidz_asize() will return 8k and as such the pool's allocated
5274b73ccab0SMike Gerdts  * and free properties will be adjusted by 8k.  The dataset will not be charged
5275b73ccab0SMike Gerdts  * 8k.  Rather, it will be charged a value that is scaled according to the
5276b73ccab0SMike Gerdts  * overhead of the 128k block on the same vdev.  This 8k allocation will be
5277b73ccab0SMike Gerdts  * charged 8k * 128k / 160k.  128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5278b73ccab0SMike Gerdts  * calculated in the 128k block example above.
5279b73ccab0SMike Gerdts  *
5280b73ccab0SMike Gerdts  * Every raidz allocation is sized to be a multiple of nparity+1 sectors.  That
5281b73ccab0SMike Gerdts  * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5282b73ccab0SMike Gerdts  * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5283b73ccab0SMike Gerdts  * multiple of of 4 sectors.  When a block does not fill the required number of
5284b73ccab0SMike Gerdts  * sectors, skip blocks (sectors) are used.
5285b73ccab0SMike Gerdts  *
5286b73ccab0SMike Gerdts  * An 8k block being written to a raidz vdev may be written as follows:
5287b73ccab0SMike Gerdts  *
5288b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5289b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5290b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5291b73ccab0SMike Gerdts  * |  P0   |  D0   |  D1   |  S0   |       |
5292b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5293b73ccab0SMike Gerdts  *
5294b73ccab0SMike Gerdts  * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5295b73ccab0SMike Gerdts  * added.  For this 8k block, the pool's allocated and free properties are
5296b73ccab0SMike Gerdts  * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5297b73ccab0SMike Gerdts  * 160k.  Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5298b73ccab0SMike Gerdts  * the 128k block example above.
5299b73ccab0SMike Gerdts  *
5300b73ccab0SMike Gerdts  * Compression may lead to a variety of block sizes being written for the same
5301b73ccab0SMike Gerdts  * volume or file.  There is no clear way to reserve just the amount of space
5302b73ccab0SMike Gerdts  * that will be required, so the worst case (no compression) is assumed.
5303b73ccab0SMike Gerdts  * Note that metadata blocks will typically be compressed, so the reservation
5304b73ccab0SMike Gerdts  * size returned by zvol_volsize_to_reservation() will generally be slightly
5305b73ccab0SMike Gerdts  * larger than the maximum that the volume can reference.
5306b73ccab0SMike Gerdts  */
5307b73ccab0SMike Gerdts 
5308b73ccab0SMike Gerdts /*
5309b73ccab0SMike Gerdts  * Derived from function of same name in uts/common/fs/zfs/vdev_raidz.c.
5310b73ccab0SMike Gerdts  * Returns the amount of space (in bytes) that will be allocated for the
5311b73ccab0SMike Gerdts  * specified block size. Note that the "referenced" space accounted will be less
5312b73ccab0SMike Gerdts  * than this, but not necessarily equal to "blksize", due to RAIDZ deflation.
5313b73ccab0SMike Gerdts  */
5314b73ccab0SMike Gerdts static uint64_t
5315b73ccab0SMike Gerdts vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5316b73ccab0SMike Gerdts     uint64_t blksize)
5317b73ccab0SMike Gerdts {
5318b73ccab0SMike Gerdts 	uint64_t asize, ndata;
5319b73ccab0SMike Gerdts 
5320b73ccab0SMike Gerdts 	ASSERT3U(ndisks, >, nparity);
5321b73ccab0SMike Gerdts 	ndata = ndisks - nparity;
5322b73ccab0SMike Gerdts 	asize = ((blksize - 1) >> ashift) + 1;
5323b73ccab0SMike Gerdts 	asize += nparity * ((asize + ndata - 1) / ndata);
5324b73ccab0SMike Gerdts 	asize = roundup(asize, nparity + 1) << ashift;
5325b73ccab0SMike Gerdts 
5326b73ccab0SMike Gerdts 	return (asize);
5327b73ccab0SMike Gerdts }
5328b73ccab0SMike Gerdts 
5329b73ccab0SMike Gerdts /*
5330b73ccab0SMike Gerdts  * Determine how much space will be allocated if it lands on the most space-
5331b73ccab0SMike Gerdts  * inefficient top-level vdev.  Returns the size in bytes required to store one
5332b73ccab0SMike Gerdts  * copy of the volume data.  See theory comment above.
5333b73ccab0SMike Gerdts  */
5334b73ccab0SMike Gerdts static uint64_t
5335b73ccab0SMike Gerdts volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5336b73ccab0SMike Gerdts {
5337b73ccab0SMike Gerdts 	nvlist_t *config, *tree, **vdevs;
5338b73ccab0SMike Gerdts 	uint_t nvdevs, v;
5339b73ccab0SMike Gerdts 	uint64_t ret = 0;
5340b73ccab0SMike Gerdts 
5341b73ccab0SMike Gerdts 	config = zpool_get_config(zhp, NULL);
5342b73ccab0SMike Gerdts 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5343b73ccab0SMike Gerdts 	    nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5344b73ccab0SMike Gerdts 	    &vdevs, &nvdevs) != 0) {
5345b73ccab0SMike Gerdts 		return (nblocks * blksize);
5346b73ccab0SMike Gerdts 	}
5347b73ccab0SMike Gerdts 
5348b73ccab0SMike Gerdts 	for (v = 0; v < nvdevs; v++) {
5349b73ccab0SMike Gerdts 		char *type;
5350b73ccab0SMike Gerdts 		uint64_t nparity, ashift, asize, tsize;
5351b73ccab0SMike Gerdts 		nvlist_t **disks;
5352b73ccab0SMike Gerdts 		uint_t ndisks;
5353b73ccab0SMike Gerdts 		uint64_t volsize;
5354b73ccab0SMike Gerdts 
5355b73ccab0SMike Gerdts 		if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5356b73ccab0SMike Gerdts 		    &type) != 0 || strcmp(type, VDEV_TYPE_RAIDZ) != 0 ||
5357b73ccab0SMike Gerdts 		    nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_NPARITY,
5358b73ccab0SMike Gerdts 		    &nparity) != 0 ||
5359b73ccab0SMike Gerdts 		    nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_ASHIFT,
5360b73ccab0SMike Gerdts 		    &ashift) != 0 ||
5361b73ccab0SMike Gerdts 		    nvlist_lookup_nvlist_array(vdevs[v], ZPOOL_CONFIG_CHILDREN,
5362b73ccab0SMike Gerdts 		    &disks, &ndisks) != 0) {
5363b73ccab0SMike Gerdts 			continue;
5364b73ccab0SMike Gerdts 		}
5365b73ccab0SMike Gerdts 
5366b73ccab0SMike Gerdts 		/* allocation size for the "typical" 128k block */
5367b73ccab0SMike Gerdts 		tsize = vdev_raidz_asize(ndisks, nparity, ashift,
5368b73ccab0SMike Gerdts 		    SPA_OLD_MAXBLOCKSIZE);
5369b73ccab0SMike Gerdts 		/* allocation size for the blksize block */
5370b73ccab0SMike Gerdts 		asize = vdev_raidz_asize(ndisks, nparity, ashift, blksize);
5371b73ccab0SMike Gerdts 
5372b73ccab0SMike Gerdts 		/*
5373b73ccab0SMike Gerdts 		 * Scale this size down as a ratio of 128k / tsize.  See theory
5374b73ccab0SMike Gerdts 		 * statement above.
5375b73ccab0SMike Gerdts 		 */
5376b73ccab0SMike Gerdts 		volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize;
5377b73ccab0SMike Gerdts 		if (volsize > ret) {
5378b73ccab0SMike Gerdts 			ret = volsize;
5379b73ccab0SMike Gerdts 		}
5380b73ccab0SMike Gerdts 	}
5381b73ccab0SMike Gerdts 
5382b73ccab0SMike Gerdts 	if (ret == 0) {
5383b73ccab0SMike Gerdts 		ret = nblocks * blksize;
5384b73ccab0SMike Gerdts 	}
5385b73ccab0SMike Gerdts 
5386b73ccab0SMike Gerdts 	return (ret);
5387b73ccab0SMike Gerdts }
5388b73ccab0SMike Gerdts 
5389b73ccab0SMike Gerdts /*
5390b73ccab0SMike Gerdts  * Convert the zvol's volume size to an appropriate reservation.  See theory
5391b73ccab0SMike Gerdts  * comment above.
5392b73ccab0SMike Gerdts  *
53933e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
5394b73ccab0SMike Gerdts  * suite's shell version in reservation.shlib.
53953e30c24aSWill Andrews  */
5396c1449561SEric Taylor uint64_t
5397b73ccab0SMike Gerdts zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5398b73ccab0SMike Gerdts     nvlist_t *props)
5399c1449561SEric Taylor {
5400c1449561SEric Taylor 	uint64_t numdb;
5401c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
5402c1449561SEric Taylor 	int ncopies;
5403c1449561SEric Taylor 	char *strval;
5404c1449561SEric Taylor 
5405c1449561SEric Taylor 	if (nvlist_lookup_string(props,
5406c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5407c1449561SEric Taylor 		ncopies = atoi(strval);
5408c1449561SEric Taylor 	else
5409c1449561SEric Taylor 		ncopies = 1;
5410c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
5411c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5412c1449561SEric Taylor 	    &volblocksize) != 0)
5413c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5414b73ccab0SMike Gerdts 
5415b73ccab0SMike Gerdts 	nblocks = volsize / volblocksize;
5416b73ccab0SMike Gerdts 	/*
5417b73ccab0SMike Gerdts 	 * Metadata defaults to using 128k blocks, not volblocksize blocks.  For
5418b73ccab0SMike Gerdts 	 * this reason, only the data blocks are scaled based on vdev config.
5419b73ccab0SMike Gerdts 	 */
5420b73ccab0SMike Gerdts 	volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5421b73ccab0SMike Gerdts 
5422c1449561SEric Taylor 	/* start with metadnode L0-L6 */
5423c1449561SEric Taylor 	numdb = 7;
5424c1449561SEric Taylor 	/* calculate number of indirects */
5425c1449561SEric Taylor 	while (nblocks > 1) {
5426c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
5427c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
5428c1449561SEric Taylor 		numdb += nblocks;
5429c1449561SEric Taylor 	}
5430c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5431c1449561SEric Taylor 	volsize *= ncopies;
5432c1449561SEric Taylor 	/*
5433c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5434c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
5435c1449561SEric Taylor 	 * 1100 bytes
5436c1449561SEric Taylor 	 */
5437c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5438c1449561SEric Taylor 	volsize += numdb;
5439c1449561SEric Taylor 	return (volsize);
5440c1449561SEric Taylor }
5441