1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21f3861e1aSahl 
22fa9e4066Sahrens /*
2336db6475SEric Taylor  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*0dfe541eSEvan Layton  */
25*0dfe541eSEvan Layton 
26*0dfe541eSEvan Layton /*
27*0dfe541eSEvan Layton  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28ad2760acSMatthew Ahrens  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
29f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
300d8fa8f8SMartin Matuska  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
31013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
32a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
33c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
34*0dfe541eSEvan Layton  * Copyright 2018 Nexenta Systems, Inc.
3588f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
36f62db44dSAndrew Stormont  * Copyright 2017-2018 RackTop Systems.
37fa9e4066Sahrens  */
38fa9e4066Sahrens 
39fa9e4066Sahrens #include <ctype.h>
40fa9e4066Sahrens #include <errno.h>
41fa9e4066Sahrens #include <libintl.h>
42fa9e4066Sahrens #include <stdio.h>
43fa9e4066Sahrens #include <stdlib.h>
44fa9e4066Sahrens #include <strings.h>
45fa9e4066Sahrens #include <unistd.h>
463cb34c60Sahrens #include <stddef.h>
47fa9e4066Sahrens #include <zone.h>
4899653d4eSeschrock #include <fcntl.h>
49fa9e4066Sahrens #include <sys/mntent.h>
50b12a1c38Slling #include <sys/mount.h>
51ecd6cf80Smarks #include <priv.h>
52ecd6cf80Smarks #include <pwd.h>
53ecd6cf80Smarks #include <grp.h>
54ecd6cf80Smarks #include <stddef.h>
55ecd6cf80Smarks #include <ucred.h>
5614843421SMatthew Ahrens #include <idmap.h>
5714843421SMatthew Ahrens #include <aclutils.h>
583b12c289SMatthew Ahrens #include <directory.h>
59591e0e13SSebastien Roy #include <time.h>
60fa9e4066Sahrens 
61c1449561SEric Taylor #include <sys/dnode.h>
62fa9e4066Sahrens #include <sys/spa.h>
63e9dbad6fSeschrock #include <sys/zap.h>
64eb633035STom Caputi #include <sys/dsl_crypt.h>
65fa9e4066Sahrens #include <libzfs.h>
66d8ab6e12SDon Brady #include <libzutil.h>
67fa9e4066Sahrens 
68fa9e4066Sahrens #include "zfs_namecheck.h"
69fa9e4066Sahrens #include "zfs_prop.h"
70fa9e4066Sahrens #include "libzfs_impl.h"
71ecd6cf80Smarks #include "zfs_deleg.h"
72fa9e4066Sahrens 
7314843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
7414843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
75cdf5b4caSmmusante 
76fa9e4066Sahrens /*
77fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
78fa9e4066Sahrens  * readable form.
79fa9e4066Sahrens  */
80fa9e4066Sahrens const char *
81fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
82fa9e4066Sahrens {
83fa9e4066Sahrens 	switch (type) {
84fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
85fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
86fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
87fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
88fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
89fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
9088f61deeSIgor Kozhukhov 	case ZFS_TYPE_POOL:
9188f61deeSIgor Kozhukhov 		return (dgettext(TEXT_DOMAIN, "pool"));
9288f61deeSIgor Kozhukhov 	case ZFS_TYPE_BOOKMARK:
9388f61deeSIgor Kozhukhov 		return (dgettext(TEXT_DOMAIN, "bookmark"));
9488f61deeSIgor Kozhukhov 	default:
9588f61deeSIgor Kozhukhov 		assert(!"unhandled zfs_type_t");
96fa9e4066Sahrens 	}
97fa9e4066Sahrens 
98fa9e4066Sahrens 	return (NULL);
99fa9e4066Sahrens }
100fa9e4066Sahrens 
101fa9e4066Sahrens /*
102fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
10314843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
10414843421SMatthew Ahrens  * explain exactly why the name was not valid.
105fa9e4066Sahrens  */
10699d5e173STim Haley int
107f18faf3fSek zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
108f18faf3fSek     boolean_t modifying)
109fa9e4066Sahrens {
110fa9e4066Sahrens 	namecheck_err_t why;
111fa9e4066Sahrens 	char what;
112fa9e4066Sahrens 
113edb901aaSMarcel Telka 	if (entity_namecheck(path, &why, &what) != 0) {
11499653d4eSeschrock 		if (hdl != NULL) {
115fa9e4066Sahrens 			switch (why) {
116b81d61a6Slling 			case NAME_ERR_TOOLONG:
11799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11899653d4eSeschrock 				    "name is too long"));
119b81d61a6Slling 				break;
120b81d61a6Slling 
121fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
12299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12399653d4eSeschrock 				    "leading slash in name"));
124fa9e4066Sahrens 				break;
125fa9e4066Sahrens 
126fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
12799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12899653d4eSeschrock 				    "empty component in name"));
129fa9e4066Sahrens 				break;
130fa9e4066Sahrens 
131fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
13299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13399653d4eSeschrock 				    "trailing slash in name"));
134fa9e4066Sahrens 				break;
135fa9e4066Sahrens 
136fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
13799653d4eSeschrock 				zfs_error_aux(hdl,
138fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
13999653d4eSeschrock 				    "'%c' in name"), what);
140fa9e4066Sahrens 				break;
141fa9e4066Sahrens 
142edb901aaSMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
14399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144edb901aaSMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
145edb901aaSMarcel Telka 				    "name"));
146fa9e4066Sahrens 				break;
1475ad82045Snd 
1485ad82045Snd 			case NAME_ERR_NOLETTER:
1495ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505ad82045Snd 				    "pool doesn't begin with a letter"));
1515ad82045Snd 				break;
1525ad82045Snd 
1535ad82045Snd 			case NAME_ERR_RESERVED:
1545ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1555ad82045Snd 				    "name is reserved"));
1565ad82045Snd 				break;
1575ad82045Snd 
1585ad82045Snd 			case NAME_ERR_DISKLIKE:
1595ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1605ad82045Snd 				    "reserved disk name"));
1615ad82045Snd 				break;
16288f61deeSIgor Kozhukhov 
16388f61deeSIgor Kozhukhov 			default:
16488f61deeSIgor Kozhukhov 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16588f61deeSIgor Kozhukhov 				    "(%d) not defined"), why);
16688f61deeSIgor Kozhukhov 				break;
167fa9e4066Sahrens 			}
168fa9e4066Sahrens 		}
169fa9e4066Sahrens 
170fa9e4066Sahrens 		return (0);
171fa9e4066Sahrens 	}
172fa9e4066Sahrens 
173fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
17499653d4eSeschrock 		if (hdl != NULL)
17599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176edb901aaSMarcel Telka 			    "snapshot delimiter '@' is not expected here"));
177fa9e4066Sahrens 		return (0);
178fa9e4066Sahrens 	}
179fa9e4066Sahrens 
1801d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1811d452cf5Sahrens 		if (hdl != NULL)
1821d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
1841d452cf5Sahrens 		return (0);
1851d452cf5Sahrens 	}
1861d452cf5Sahrens 
187edb901aaSMarcel Telka 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
188edb901aaSMarcel Telka 		if (hdl != NULL)
189edb901aaSMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
190edb901aaSMarcel Telka 			    "bookmark delimiter '#' is not expected here"));
191edb901aaSMarcel Telka 		return (0);
192edb901aaSMarcel Telka 	}
193edb901aaSMarcel Telka 
194edb901aaSMarcel Telka 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
195edb901aaSMarcel Telka 		if (hdl != NULL)
196edb901aaSMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
197edb901aaSMarcel Telka 			    "missing '#' delimiter in bookmark name"));
198edb901aaSMarcel Telka 		return (0);
199edb901aaSMarcel Telka 	}
200edb901aaSMarcel Telka 
201f18faf3fSek 	if (modifying && strchr(path, '%') != NULL) {
202f18faf3fSek 		if (hdl != NULL)
203f18faf3fSek 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
204f18faf3fSek 			    "invalid character %c in name"), '%');
205f18faf3fSek 		return (0);
206f18faf3fSek 	}
207f18faf3fSek 
20899653d4eSeschrock 	return (-1);
209fa9e4066Sahrens }
210fa9e4066Sahrens 
211fa9e4066Sahrens int
212fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
213fa9e4066Sahrens {
214e7cbe64fSgw 	if (type == ZFS_TYPE_POOL)
215e7cbe64fSgw 		return (zpool_name_valid(NULL, B_FALSE, name));
216f18faf3fSek 	return (zfs_validate_name(NULL, name, type, B_FALSE));
217fa9e4066Sahrens }
218fa9e4066Sahrens 
219e9dbad6fSeschrock /*
220e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
221e9dbad6fSeschrock  * properties into a separate nvlist.
222e9dbad6fSeschrock  */
223fac3008cSeschrock static nvlist_t *
224fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
225e9dbad6fSeschrock {
226e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
227e9dbad6fSeschrock 	nvpair_t *elem;
228e9dbad6fSeschrock 	nvlist_t *propval;
229fac3008cSeschrock 	nvlist_t *nvl;
230e9dbad6fSeschrock 
231fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
232fac3008cSeschrock 		(void) no_memory(hdl);
233fac3008cSeschrock 		return (NULL);
234fac3008cSeschrock 	}
235e9dbad6fSeschrock 
236e9dbad6fSeschrock 	elem = NULL;
237fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
238e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
239e9dbad6fSeschrock 			continue;
240e9dbad6fSeschrock 
241e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
242fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
243fac3008cSeschrock 			nvlist_free(nvl);
244fac3008cSeschrock 			(void) no_memory(hdl);
245fac3008cSeschrock 			return (NULL);
246fac3008cSeschrock 		}
247e9dbad6fSeschrock 	}
248e9dbad6fSeschrock 
249fac3008cSeschrock 	return (nvl);
250e9dbad6fSeschrock }
251e9dbad6fSeschrock 
25229ab75c9Srm static zpool_handle_t *
25329ab75c9Srm zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
25429ab75c9Srm {
25529ab75c9Srm 	libzfs_handle_t *hdl = zhp->zfs_hdl;
25629ab75c9Srm 	zpool_handle_t *zph;
25729ab75c9Srm 
25829ab75c9Srm 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
25929ab75c9Srm 		if (hdl->libzfs_pool_handles != NULL)
26029ab75c9Srm 			zph->zpool_next = hdl->libzfs_pool_handles;
26129ab75c9Srm 		hdl->libzfs_pool_handles = zph;
26229ab75c9Srm 	}
26329ab75c9Srm 	return (zph);
26429ab75c9Srm }
26529ab75c9Srm 
26629ab75c9Srm static zpool_handle_t *
26729ab75c9Srm zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
26829ab75c9Srm {
26929ab75c9Srm 	libzfs_handle_t *hdl = zhp->zfs_hdl;
27029ab75c9Srm 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
27129ab75c9Srm 
27229ab75c9Srm 	while ((zph != NULL) &&
27329ab75c9Srm 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
27429ab75c9Srm 		zph = zph->zpool_next;
27529ab75c9Srm 	return (zph);
27629ab75c9Srm }
27729ab75c9Srm 
27829ab75c9Srm /*
27929ab75c9Srm  * Returns a handle to the pool that contains the provided dataset.
28029ab75c9Srm  * If a handle to that pool already exists then that handle is returned.
28129ab75c9Srm  * Otherwise, a new handle is created and added to the list of handles.
28229ab75c9Srm  */
28329ab75c9Srm static zpool_handle_t *
28429ab75c9Srm zpool_handle(zfs_handle_t *zhp)
28529ab75c9Srm {
28629ab75c9Srm 	char *pool_name;
28729ab75c9Srm 	int len;
28829ab75c9Srm 	zpool_handle_t *zph;
28929ab75c9Srm 
29078f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
29129ab75c9Srm 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
29229ab75c9Srm 	(void) strlcpy(pool_name, zhp->zfs_name, len);
29329ab75c9Srm 
29429ab75c9Srm 	zph = zpool_find_handle(zhp, pool_name, len);
29529ab75c9Srm 	if (zph == NULL)
29629ab75c9Srm 		zph = zpool_add_handle(zhp, pool_name);
29729ab75c9Srm 
29829ab75c9Srm 	free(pool_name);
29929ab75c9Srm 	return (zph);
30029ab75c9Srm }
30129ab75c9Srm 
30229ab75c9Srm void
30329ab75c9Srm zpool_free_handles(libzfs_handle_t *hdl)
30429ab75c9Srm {
30529ab75c9Srm 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
30629ab75c9Srm 
30729ab75c9Srm 	while (zph != NULL) {
30829ab75c9Srm 		next = zph->zpool_next;
30929ab75c9Srm 		zpool_close(zph);
31029ab75c9Srm 		zph = next;
31129ab75c9Srm 	}
31229ab75c9Srm 	hdl->libzfs_pool_handles = NULL;
31329ab75c9Srm }
31429ab75c9Srm 
315fa9e4066Sahrens /*
316fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
317fa9e4066Sahrens  */
318fa9e4066Sahrens static int
319ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
320fa9e4066Sahrens {
321e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
322fa9e4066Sahrens 
323ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
324fa9e4066Sahrens 
325ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3267f7322feSeschrock 		if (errno == ENOMEM) {
327ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
32899653d4eSeschrock 				return (-1);
329e9dbad6fSeschrock 			}
3307f7322feSeschrock 		} else {
3317f7322feSeschrock 			return (-1);
3327f7322feSeschrock 		}
3337f7322feSeschrock 	}
334ebedde84SEric Taylor 	return (0);
335ebedde84SEric Taylor }
336fa9e4066Sahrens 
33792241e0bSTom Erickson /*
33892241e0bSTom Erickson  * Utility function to get the received properties of the given object.
33992241e0bSTom Erickson  */
34092241e0bSTom Erickson static int
34192241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
34292241e0bSTom Erickson {
34392241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34492241e0bSTom Erickson 	nvlist_t *recvdprops;
34592241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
34692241e0bSTom Erickson 	int err;
34792241e0bSTom Erickson 
34892241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
34992241e0bSTom Erickson 		return (-1);
35092241e0bSTom Erickson 
35192241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35292241e0bSTom Erickson 
35392241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
35492241e0bSTom Erickson 		if (errno == ENOMEM) {
35592241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
35692241e0bSTom Erickson 				return (-1);
35792241e0bSTom Erickson 			}
35892241e0bSTom Erickson 		} else {
35992241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
36092241e0bSTom Erickson 			return (-1);
36192241e0bSTom Erickson 		}
36292241e0bSTom Erickson 	}
36392241e0bSTom Erickson 
36492241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
36592241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
36692241e0bSTom Erickson 	if (err != 0)
36792241e0bSTom Erickson 		return (-1);
36892241e0bSTom Erickson 
36992241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
37092241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
37192241e0bSTom Erickson 
37292241e0bSTom Erickson 	return (0);
37392241e0bSTom Erickson }
37492241e0bSTom Erickson 
375ebedde84SEric Taylor static int
376ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
377ebedde84SEric Taylor {
378ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
379fa9e4066Sahrens 
380ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
381ebedde84SEric Taylor 
382ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
38399653d4eSeschrock 		return (-1);
38499653d4eSeschrock 	}
385fa9e4066Sahrens 
38614843421SMatthew Ahrens 	/*
38714843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
38814843421SMatthew Ahrens 	 * storing them in zfs_props?
38914843421SMatthew Ahrens 	 */
390fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
391fac3008cSeschrock 		nvlist_free(allprops);
392e9dbad6fSeschrock 		return (-1);
393fac3008cSeschrock 	}
394fac3008cSeschrock 
395fac3008cSeschrock 	nvlist_free(zhp->zfs_props);
396fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
397fac3008cSeschrock 
398fac3008cSeschrock 	zhp->zfs_props = allprops;
399fac3008cSeschrock 	zhp->zfs_user_props = userprops;
40099653d4eSeschrock 
401fa9e4066Sahrens 	return (0);
402fa9e4066Sahrens }
403fa9e4066Sahrens 
404ebedde84SEric Taylor static int
405ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
406ebedde84SEric Taylor {
407ebedde84SEric Taylor 	int rc = 0;
408ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
409ebedde84SEric Taylor 
410ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
411ebedde84SEric Taylor 		return (-1);
412ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
413ebedde84SEric Taylor 		rc = -1;
414ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
415ebedde84SEric Taylor 		rc = -1;
416ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
417ebedde84SEric Taylor 	return (rc);
418ebedde84SEric Taylor }
419ebedde84SEric Taylor 
420fa9e4066Sahrens /*
421fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
422fa9e4066Sahrens  */
423fa9e4066Sahrens void
424fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
425fa9e4066Sahrens {
426fa9e4066Sahrens 	(void) get_stats(zhp);
427fa9e4066Sahrens }
428fa9e4066Sahrens 
429fa9e4066Sahrens /*
430fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
431fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
432fa9e4066Sahrens  */
433ebedde84SEric Taylor static int
434ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
435fa9e4066Sahrens {
436503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
437ebedde84SEric Taylor 		return (-1);
43831fd60d3Sahrens 
439fa9e4066Sahrens 	/*
440fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
441fa9e4066Sahrens 	 * the high-level type.
442fa9e4066Sahrens 	 */
443a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
444a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
445a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
446a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
447e0f1c0afSOlaf Faaland 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
448e0f1c0afSOlaf Faaland 		return (-1);
449a2eea2e1Sahrens 	else
450a2eea2e1Sahrens 		abort();
451a2eea2e1Sahrens 
452fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
453fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
454fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
455fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
456fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
457fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
458fa9e4066Sahrens 	else
45999653d4eSeschrock 		abort();	/* we should never see any other types */
460fa9e4066Sahrens 
4619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
464ebedde84SEric Taylor 	return (0);
465ebedde84SEric Taylor }
466ebedde84SEric Taylor 
467ebedde84SEric Taylor zfs_handle_t *
468ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
469ebedde84SEric Taylor {
470ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
471ebedde84SEric Taylor 
472ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
473ebedde84SEric Taylor 
474ebedde84SEric Taylor 	if (zhp == NULL)
475ebedde84SEric Taylor 		return (NULL);
476ebedde84SEric Taylor 
477ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
478ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
479ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
480ebedde84SEric Taylor 		free(zhp);
481ebedde84SEric Taylor 		return (NULL);
482ebedde84SEric Taylor 	}
483ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
484ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
485ebedde84SEric Taylor 		free(zhp);
486ebedde84SEric Taylor 		return (NULL);
487ebedde84SEric Taylor 	}
488ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
489ebedde84SEric Taylor 		free(zhp);
490ebedde84SEric Taylor 		zhp = NULL;
491ebedde84SEric Taylor 	}
492ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
493ebedde84SEric Taylor 	return (zhp);
494ebedde84SEric Taylor }
495ebedde84SEric Taylor 
49619b94df9SMatthew Ahrens zfs_handle_t *
497ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
498ebedde84SEric Taylor {
499ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
500ebedde84SEric Taylor 
501ebedde84SEric Taylor 	if (zhp == NULL)
502ebedde84SEric Taylor 		return (NULL);
503ebedde84SEric Taylor 
504ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
505ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
506ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
507ebedde84SEric Taylor 		free(zhp);
508ebedde84SEric Taylor 		return (NULL);
509ebedde84SEric Taylor 	}
510fa9e4066Sahrens 	return (zhp);
511fa9e4066Sahrens }
512fa9e4066Sahrens 
5130d8fa8f8SMartin Matuska zfs_handle_t *
5140d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
5150d8fa8f8SMartin Matuska {
5160d8fa8f8SMartin Matuska 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5170d8fa8f8SMartin Matuska 
5180d8fa8f8SMartin Matuska 	if (zhp == NULL)
5190d8fa8f8SMartin Matuska 		return (NULL);
5200d8fa8f8SMartin Matuska 
5210d8fa8f8SMartin Matuska 	zhp->zfs_hdl = pzhp->zfs_hdl;
5220d8fa8f8SMartin Matuska 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5230d8fa8f8SMartin Matuska 	zhp->zfs_head_type = pzhp->zfs_type;
5240d8fa8f8SMartin Matuska 	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
5250d8fa8f8SMartin Matuska 	zhp->zpool_hdl = zpool_handle(zhp);
5260d8fa8f8SMartin Matuska 	return (zhp);
5270d8fa8f8SMartin Matuska }
5280d8fa8f8SMartin Matuska 
52919b94df9SMatthew Ahrens zfs_handle_t *
53019b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
53119b94df9SMatthew Ahrens {
53219b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
53319b94df9SMatthew Ahrens 
53419b94df9SMatthew Ahrens 	if (zhp == NULL)
53519b94df9SMatthew Ahrens 		return (NULL);
53619b94df9SMatthew Ahrens 
53719b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
53819b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
53919b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
54019b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
54119b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
54219b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
54319b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
54419b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
54519b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
54619b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54719b94df9SMatthew Ahrens 			zfs_close(zhp);
54819b94df9SMatthew Ahrens 			return (NULL);
54919b94df9SMatthew Ahrens 		}
55019b94df9SMatthew Ahrens 	}
55119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
55219b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
55319b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
55419b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55519b94df9SMatthew Ahrens 			zfs_close(zhp);
55619b94df9SMatthew Ahrens 			return (NULL);
55719b94df9SMatthew Ahrens 		}
55819b94df9SMatthew Ahrens 	}
55919b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
56019b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
56119b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
56219b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
56319b94df9SMatthew Ahrens 			zfs_close(zhp);
56419b94df9SMatthew Ahrens 			return (NULL);
56519b94df9SMatthew Ahrens 		}
56619b94df9SMatthew Ahrens 	}
56719b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
56819b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
56919b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
57019b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
57119b94df9SMatthew Ahrens 	}
57219b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
57319b94df9SMatthew Ahrens 	return (zhp);
57419b94df9SMatthew Ahrens }
57519b94df9SMatthew Ahrens 
57678f17100SMatthew Ahrens boolean_t
57778f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
57878f17100SMatthew Ahrens {
57978f17100SMatthew Ahrens 	nvlist_t *bmarks;
58078f17100SMatthew Ahrens 	nvlist_t *props;
5819adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
58278f17100SMatthew Ahrens 	char *bmark_name;
58378f17100SMatthew Ahrens 	char *pound;
58478f17100SMatthew Ahrens 	int err;
58578f17100SMatthew Ahrens 	boolean_t rv;
58678f17100SMatthew Ahrens 
58778f17100SMatthew Ahrens 
58878f17100SMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
58978f17100SMatthew Ahrens 	pound = strchr(fsname, '#');
59078f17100SMatthew Ahrens 	if (pound == NULL)
59178f17100SMatthew Ahrens 		return (B_FALSE);
59278f17100SMatthew Ahrens 
59378f17100SMatthew Ahrens 	*pound = '\0';
59478f17100SMatthew Ahrens 	bmark_name = pound + 1;
59578f17100SMatthew Ahrens 	props = fnvlist_alloc();
59678f17100SMatthew Ahrens 	err = lzc_get_bookmarks(fsname, props, &bmarks);
59778f17100SMatthew Ahrens 	nvlist_free(props);
59878f17100SMatthew Ahrens 	if (err != 0) {
59978f17100SMatthew Ahrens 		nvlist_free(bmarks);
60078f17100SMatthew Ahrens 		return (B_FALSE);
60178f17100SMatthew Ahrens 	}
60278f17100SMatthew Ahrens 
60378f17100SMatthew Ahrens 	rv = nvlist_exists(bmarks, bmark_name);
60478f17100SMatthew Ahrens 	nvlist_free(bmarks);
60578f17100SMatthew Ahrens 	return (rv);
60678f17100SMatthew Ahrens }
60778f17100SMatthew Ahrens 
60878f17100SMatthew Ahrens zfs_handle_t *
60978f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
61078f17100SMatthew Ahrens     nvlist_t *bmark_props)
61178f17100SMatthew Ahrens {
61278f17100SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
61378f17100SMatthew Ahrens 
61478f17100SMatthew Ahrens 	if (zhp == NULL)
61578f17100SMatthew Ahrens 		return (NULL);
61678f17100SMatthew Ahrens 
61778f17100SMatthew Ahrens 	/* Fill in the name. */
61878f17100SMatthew Ahrens 	zhp->zfs_hdl = parent->zfs_hdl;
61978f17100SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
62078f17100SMatthew Ahrens 
62178f17100SMatthew Ahrens 	/* Set the property lists. */
62278f17100SMatthew Ahrens 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
62378f17100SMatthew Ahrens 		free(zhp);
62478f17100SMatthew Ahrens 		return (NULL);
62578f17100SMatthew Ahrens 	}
62678f17100SMatthew Ahrens 
62778f17100SMatthew Ahrens 	/* Set the types. */
62878f17100SMatthew Ahrens 	zhp->zfs_head_type = parent->zfs_head_type;
62978f17100SMatthew Ahrens 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
63078f17100SMatthew Ahrens 
63178f17100SMatthew Ahrens 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
63278f17100SMatthew Ahrens 		nvlist_free(zhp->zfs_props);
63378f17100SMatthew Ahrens 		free(zhp);
63478f17100SMatthew Ahrens 		return (NULL);
63578f17100SMatthew Ahrens 	}
63678f17100SMatthew Ahrens 
63778f17100SMatthew Ahrens 	return (zhp);
63878f17100SMatthew Ahrens }
63978f17100SMatthew Ahrens 
640edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data {
641edb901aaSMarcel Telka 	const char *path;
642edb901aaSMarcel Telka 	zfs_handle_t *zhp;
643edb901aaSMarcel Telka };
644edb901aaSMarcel Telka 
645edb901aaSMarcel Telka static int
646edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
647edb901aaSMarcel Telka {
648edb901aaSMarcel Telka 	struct zfs_open_bookmarks_cb_data *dp = data;
649edb901aaSMarcel Telka 
650edb901aaSMarcel Telka 	/*
651edb901aaSMarcel Telka 	 * Is it the one we are looking for?
652edb901aaSMarcel Telka 	 */
653edb901aaSMarcel Telka 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
654edb901aaSMarcel Telka 		/*
655edb901aaSMarcel Telka 		 * We found it.  Save it and let the caller know we are done.
656edb901aaSMarcel Telka 		 */
657edb901aaSMarcel Telka 		dp->zhp = zhp;
658edb901aaSMarcel Telka 		return (EEXIST);
659edb901aaSMarcel Telka 	}
660edb901aaSMarcel Telka 
661edb901aaSMarcel Telka 	/*
662edb901aaSMarcel Telka 	 * Not found.  Close the handle and ask for another one.
663edb901aaSMarcel Telka 	 */
664edb901aaSMarcel Telka 	zfs_close(zhp);
665edb901aaSMarcel Telka 	return (0);
666edb901aaSMarcel Telka }
667edb901aaSMarcel Telka 
668fa9e4066Sahrens /*
669edb901aaSMarcel Telka  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
670fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
671fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
672fa9e4066Sahrens  */
673fa9e4066Sahrens zfs_handle_t *
67499653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
675fa9e4066Sahrens {
676fa9e4066Sahrens 	zfs_handle_t *zhp;
67799653d4eSeschrock 	char errbuf[1024];
678edb901aaSMarcel Telka 	char *bookp;
67999653d4eSeschrock 
68099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
68199653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
682fa9e4066Sahrens 
683fa9e4066Sahrens 	/*
68499653d4eSeschrock 	 * Validate the name before we even try to open it.
685fa9e4066Sahrens 	 */
686edb901aaSMarcel Telka 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
68799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
688fa9e4066Sahrens 		return (NULL);
689fa9e4066Sahrens 	}
690fa9e4066Sahrens 
691fa9e4066Sahrens 	/*
692edb901aaSMarcel Telka 	 * Bookmarks needs to be handled separately.
693fa9e4066Sahrens 	 */
694edb901aaSMarcel Telka 	bookp = strchr(path, '#');
695edb901aaSMarcel Telka 	if (bookp == NULL) {
696edb901aaSMarcel Telka 		/*
697edb901aaSMarcel Telka 		 * Try to get stats for the dataset, which will tell us if it
698edb901aaSMarcel Telka 		 * exists.
699edb901aaSMarcel Telka 		 */
700edb901aaSMarcel Telka 		errno = 0;
701edb901aaSMarcel Telka 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
702edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
703edb901aaSMarcel Telka 			return (NULL);
704edb901aaSMarcel Telka 		}
705edb901aaSMarcel Telka 	} else {
706edb901aaSMarcel Telka 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
707edb901aaSMarcel Telka 		zfs_handle_t *pzhp;
708edb901aaSMarcel Telka 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
709edb901aaSMarcel Telka 
710edb901aaSMarcel Telka 		/*
711edb901aaSMarcel Telka 		 * We need to cut out '#' and everything after '#'
712edb901aaSMarcel Telka 		 * to get the parent dataset name only.
713edb901aaSMarcel Telka 		 */
714edb901aaSMarcel Telka 		assert(bookp - path < sizeof (dsname));
715edb901aaSMarcel Telka 		(void) strncpy(dsname, path, bookp - path);
716edb901aaSMarcel Telka 		dsname[bookp - path] = '\0';
717edb901aaSMarcel Telka 
718edb901aaSMarcel Telka 		/*
719edb901aaSMarcel Telka 		 * Create handle for the parent dataset.
720edb901aaSMarcel Telka 		 */
721edb901aaSMarcel Telka 		errno = 0;
722edb901aaSMarcel Telka 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
723edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
724edb901aaSMarcel Telka 			return (NULL);
725edb901aaSMarcel Telka 		}
726edb901aaSMarcel Telka 
727edb901aaSMarcel Telka 		/*
728edb901aaSMarcel Telka 		 * Iterate bookmarks to find the right one.
729edb901aaSMarcel Telka 		 */
730edb901aaSMarcel Telka 		errno = 0;
731edb901aaSMarcel Telka 		if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
732edb901aaSMarcel Telka 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
733edb901aaSMarcel Telka 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
734edb901aaSMarcel Telka 			zfs_close(pzhp);
735edb901aaSMarcel Telka 			return (NULL);
736edb901aaSMarcel Telka 		}
737edb901aaSMarcel Telka 		if (cb_data.zhp == NULL) {
738edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
739edb901aaSMarcel Telka 			zfs_close(pzhp);
740edb901aaSMarcel Telka 			return (NULL);
741edb901aaSMarcel Telka 		}
742edb901aaSMarcel Telka 		zhp = cb_data.zhp;
743edb901aaSMarcel Telka 
744edb901aaSMarcel Telka 		/*
745edb901aaSMarcel Telka 		 * Cleanup.
746edb901aaSMarcel Telka 		 */
747edb901aaSMarcel Telka 		zfs_close(pzhp);
748fa9e4066Sahrens 	}
749fa9e4066Sahrens 
750fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
75199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
75294de1d4cSeschrock 		zfs_close(zhp);
753fa9e4066Sahrens 		return (NULL);
754fa9e4066Sahrens 	}
755fa9e4066Sahrens 
756fa9e4066Sahrens 	return (zhp);
757fa9e4066Sahrens }
758fa9e4066Sahrens 
759fa9e4066Sahrens /*
760fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
761fa9e4066Sahrens  */
762fa9e4066Sahrens void
763fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
764fa9e4066Sahrens {
765fa9e4066Sahrens 	if (zhp->zfs_mntopts)
766fa9e4066Sahrens 		free(zhp->zfs_mntopts);
767e9dbad6fSeschrock 	nvlist_free(zhp->zfs_props);
768e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
76992241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
770fa9e4066Sahrens 	free(zhp);
771fa9e4066Sahrens }
772fa9e4066Sahrens 
773ebedde84SEric Taylor typedef struct mnttab_node {
774ebedde84SEric Taylor 	struct mnttab mtn_mt;
775ebedde84SEric Taylor 	avl_node_t mtn_node;
776ebedde84SEric Taylor } mnttab_node_t;
777ebedde84SEric Taylor 
778ebedde84SEric Taylor static int
779ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
780ebedde84SEric Taylor {
781c4ab0d3fSGvozden Neskovic 	const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
782c4ab0d3fSGvozden Neskovic 	const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
783ebedde84SEric Taylor 	int rv;
784ebedde84SEric Taylor 
785ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
786ebedde84SEric Taylor 
7874d7988d6SPaul Dagnelie 	if (rv == 0)
7884d7988d6SPaul Dagnelie 		return (0);
7894d7988d6SPaul Dagnelie 	return (rv > 0 ? 1 : -1);
790ebedde84SEric Taylor }
791ebedde84SEric Taylor 
792ebedde84SEric Taylor void
793ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
794ebedde84SEric Taylor {
795591e0e13SSebastien Roy 	(void) mutex_init(&hdl->libzfs_mnttab_cache_lock,
796591e0e13SSebastien Roy 	    LOCK_NORMAL | LOCK_ERRORCHECK, NULL);
797ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
798ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
799ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
800b2634b9cSEric Taylor }
801b2634b9cSEric Taylor 
802b2634b9cSEric Taylor void
803b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
804b2634b9cSEric Taylor {
805b2634b9cSEric Taylor 	struct mnttab entry;
806ebedde84SEric Taylor 
807ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
808ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
809ebedde84SEric Taylor 		mnttab_node_t *mtn;
810ebedde84SEric Taylor 
811ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
812ebedde84SEric Taylor 			continue;
813ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
814ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
815ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
816ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
817ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
818ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
819ebedde84SEric Taylor 	}
820ebedde84SEric Taylor }
821ebedde84SEric Taylor 
822ebedde84SEric Taylor void
823ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
824ebedde84SEric Taylor {
825ebedde84SEric Taylor 	void *cookie = NULL;
826ebedde84SEric Taylor 	mnttab_node_t *mtn;
827ebedde84SEric Taylor 
82888f61deeSIgor Kozhukhov 	while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
82988f61deeSIgor Kozhukhov 	    != NULL) {
830ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
831ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
832ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
833ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
834ebedde84SEric Taylor 		free(mtn);
835ebedde84SEric Taylor 	}
836ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
837591e0e13SSebastien Roy 	(void) mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
838ebedde84SEric Taylor }
839ebedde84SEric Taylor 
840b2634b9cSEric Taylor void
841b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
842b2634b9cSEric Taylor {
843b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
844b2634b9cSEric Taylor }
845b2634b9cSEric Taylor 
846ebedde84SEric Taylor int
847ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
848ebedde84SEric Taylor     struct mnttab *entry)
849ebedde84SEric Taylor {
850ebedde84SEric Taylor 	mnttab_node_t find;
851ebedde84SEric Taylor 	mnttab_node_t *mtn;
852591e0e13SSebastien Roy 	int ret = ENOENT;
853ebedde84SEric Taylor 
854b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
855b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
856b2634b9cSEric Taylor 
857b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
858b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
859b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
860b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
861b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
862b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
863b2634b9cSEric Taylor 			return (0);
864b2634b9cSEric Taylor 		else
865b2634b9cSEric Taylor 			return (ENOENT);
866b2634b9cSEric Taylor 	}
867b2634b9cSEric Taylor 
868591e0e13SSebastien Roy 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
869ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
870b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
871ebedde84SEric Taylor 
872ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
873ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
874ebedde84SEric Taylor 	if (mtn) {
875ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
876591e0e13SSebastien Roy 		ret = 0;
877ebedde84SEric Taylor 	}
878591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
879591e0e13SSebastien Roy 	return (ret);
880ebedde84SEric Taylor }
881ebedde84SEric Taylor 
882ebedde84SEric Taylor void
883ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
884ebedde84SEric Taylor     const char *mountp, const char *mntopts)
885ebedde84SEric Taylor {
886ebedde84SEric Taylor 	mnttab_node_t *mtn;
887ebedde84SEric Taylor 
888591e0e13SSebastien Roy 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
889591e0e13SSebastien Roy 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
890591e0e13SSebastien Roy 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
891591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
892591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
893591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
894591e0e13SSebastien Roy 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
895591e0e13SSebastien Roy 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
896591e0e13SSebastien Roy 	}
897591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
898ebedde84SEric Taylor }
899ebedde84SEric Taylor 
900ebedde84SEric Taylor void
901ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
902ebedde84SEric Taylor {
903ebedde84SEric Taylor 	mnttab_node_t find;
904ebedde84SEric Taylor 	mnttab_node_t *ret;
905ebedde84SEric Taylor 
906591e0e13SSebastien Roy 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
907ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
90888f61deeSIgor Kozhukhov 	if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
90988f61deeSIgor Kozhukhov 	    != NULL) {
910ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
911ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
912ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
913ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
914ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
915ebedde84SEric Taylor 		free(ret);
916ebedde84SEric Taylor 	}
917591e0e13SSebastien Roy 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
918ebedde84SEric Taylor }
919ebedde84SEric Taylor 
9207b97dc1aSrm int
9217b97dc1aSrm zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9227b97dc1aSrm {
92329ab75c9Srm 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9247b97dc1aSrm 
9257b97dc1aSrm 	if (zpool_handle == NULL)
9267b97dc1aSrm 		return (-1);
9277b97dc1aSrm 
9287b97dc1aSrm 	*spa_version = zpool_get_prop_int(zpool_handle,
9297b97dc1aSrm 	    ZPOOL_PROP_VERSION, NULL);
9307b97dc1aSrm 	return (0);
9317b97dc1aSrm }
9327b97dc1aSrm 
9337b97dc1aSrm /*
9347b97dc1aSrm  * The choice of reservation property depends on the SPA version.
9357b97dc1aSrm  */
9367b97dc1aSrm static int
9377b97dc1aSrm zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9387b97dc1aSrm {
9397b97dc1aSrm 	int spa_version;
9407b97dc1aSrm 
9417b97dc1aSrm 	if (zfs_spa_version(zhp, &spa_version) < 0)
9427b97dc1aSrm 		return (-1);
9437b97dc1aSrm 
9447b97dc1aSrm 	if (spa_version >= SPA_VERSION_REFRESERVATION)
9457b97dc1aSrm 		*resv_prop = ZFS_PROP_REFRESERVATION;
9467b97dc1aSrm 	else
9477b97dc1aSrm 		*resv_prop = ZFS_PROP_RESERVATION;
9487b97dc1aSrm 
9497b97dc1aSrm 	return (0);
9507b97dc1aSrm }
9517b97dc1aSrm 
952e9dbad6fSeschrock /*
953e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
954e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
955e9dbad6fSeschrock  * strings.
956e9dbad6fSeschrock  */
9570a48a24eStimh nvlist_t *
9580a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
959e9316f76SJoe Stein     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
960eb633035STom Caputi     boolean_t key_params_ok, const char *errbuf)
961e9dbad6fSeschrock {
962e9dbad6fSeschrock 	nvpair_t *elem;
963e9dbad6fSeschrock 	uint64_t intval;
964e9dbad6fSeschrock 	char *strval;
965990b4856Slling 	zfs_prop_t prop;
966e9dbad6fSeschrock 	nvlist_t *ret;
967da6c28aaSamw 	int chosen_normal = -1;
968da6c28aaSamw 	int chosen_utf = -1;
969e9dbad6fSeschrock 
970990b4856Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
971990b4856Slling 		(void) no_memory(hdl);
972990b4856Slling 		return (NULL);
973e9dbad6fSeschrock 	}
974e9dbad6fSeschrock 
97514843421SMatthew Ahrens 	/*
97614843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
97714843421SMatthew Ahrens 	 */
97814843421SMatthew Ahrens 
979e9dbad6fSeschrock 	elem = NULL;
980e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
981990b4856Slling 		const char *propname = nvpair_name(elem);
982e9dbad6fSeschrock 
98314843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
98414843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
985990b4856Slling 			/*
98614843421SMatthew Ahrens 			 * This is a user property: make sure it's a
987990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
988990b4856Slling 			 */
989990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
990990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991990b4856Slling 				    "'%s' must be a string"), propname);
992990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
993990b4856Slling 				goto error;
994990b4856Slling 			}
995990b4856Slling 
996990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
997990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998990b4856Slling 				    "property name '%s' is too long"),
999990b4856Slling 				    propname);
1000990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1001990b4856Slling 				goto error;
1002fa9e4066Sahrens 			}
1003fa9e4066Sahrens 
1004e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
1005e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
1006e9dbad6fSeschrock 				(void) no_memory(hdl);
1007e9dbad6fSeschrock 				goto error;
1008e9dbad6fSeschrock 			}
1009e9dbad6fSeschrock 			continue;
1010e9dbad6fSeschrock 		}
1011e9dbad6fSeschrock 
101214843421SMatthew Ahrens 		/*
101314843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
101414843421SMatthew Ahrens 		 * snapshots.
101514843421SMatthew Ahrens 		 */
1016bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
1017bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
1019bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1020bb0ade09Sahrens 			goto error;
1021bb0ade09Sahrens 		}
1022bb0ade09Sahrens 
102314843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
102414843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
102514843421SMatthew Ahrens 			char newpropname[128];
102614843421SMatthew Ahrens 			char domain[128];
102714843421SMatthew Ahrens 			uint64_t rid;
102814843421SMatthew Ahrens 			uint64_t valary[3];
102914843421SMatthew Ahrens 
103014843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
103114843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
103214843421SMatthew Ahrens 				zfs_error_aux(hdl,
103314843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
103414843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
103514843421SMatthew Ahrens 				    propname);
103614843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
103714843421SMatthew Ahrens 				goto error;
103814843421SMatthew Ahrens 			}
103914843421SMatthew Ahrens 
104014843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
1041f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_GROUPQUOTA &&
1042f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_USEROBJQUOTA &&
1043f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1044f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_PROJECTQUOTA &&
1045f67950b2SNasf-Fan 			    uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
104614843421SMatthew Ahrens 				zfs_error_aux(hdl,
104714843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
104814843421SMatthew Ahrens 				    propname);
104914843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
105014843421SMatthew Ahrens 				    errbuf);
105114843421SMatthew Ahrens 				goto error;
105214843421SMatthew Ahrens 			}
105314843421SMatthew Ahrens 
105414843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
105514843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
105614843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
105714843421SMatthew Ahrens 					intval = 0;
105814843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
105914843421SMatthew Ahrens 				    strval, &intval) != 0) {
106014843421SMatthew Ahrens 					(void) zfs_error(hdl,
106114843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
106214843421SMatthew Ahrens 					goto error;
106314843421SMatthew Ahrens 				}
106414843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
106514843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
106614843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
106714843421SMatthew Ahrens 				if (intval == 0) {
106814843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106914843421SMatthew Ahrens 					    "use 'none' to disable "
1070f67950b2SNasf-Fan 					    "{user|group|project}quota"));
107114843421SMatthew Ahrens 					goto error;
107214843421SMatthew Ahrens 				}
107314843421SMatthew Ahrens 			} else {
107414843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107514843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
107614843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
107714843421SMatthew Ahrens 				goto error;
107814843421SMatthew Ahrens 			}
107914843421SMatthew Ahrens 
10802d5843dbSMatthew Ahrens 			/*
10812d5843dbSMatthew Ahrens 			 * Encode the prop name as
10822d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
10832d5843dbSMatthew Ahrens 			 * for the kernel to decode.
10842d5843dbSMatthew Ahrens 			 */
108514843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
10862d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10872d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
108814843421SMatthew Ahrens 			valary[0] = uqtype;
108914843421SMatthew Ahrens 			valary[1] = rid;
109014843421SMatthew Ahrens 			valary[2] = intval;
109114843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
109214843421SMatthew Ahrens 			    valary, 3) != 0) {
109314843421SMatthew Ahrens 				(void) no_memory(hdl);
109414843421SMatthew Ahrens 				goto error;
109514843421SMatthew Ahrens 			}
109614843421SMatthew Ahrens 			continue;
109719b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
109819b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109919b94df9SMatthew Ahrens 			    "'%s' is readonly"),
110019b94df9SMatthew Ahrens 			    propname);
110119b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
110219b94df9SMatthew Ahrens 			goto error;
110314843421SMatthew Ahrens 		}
110414843421SMatthew Ahrens 
110514843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
110614843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
110714843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
110814843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
110914843421SMatthew Ahrens 			goto error;
111014843421SMatthew Ahrens 		}
111114843421SMatthew Ahrens 
1112e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1113e9dbad6fSeschrock 			zfs_error_aux(hdl,
1114e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1115e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1116e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1117e9dbad6fSeschrock 			goto error;
1118e9dbad6fSeschrock 		}
1119e9dbad6fSeschrock 
1120e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1121eb633035STom Caputi 		    !(zfs_prop_setonce(prop) && zhp == NULL) &&
1122eb633035STom Caputi 		    !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1123e9dbad6fSeschrock 			zfs_error_aux(hdl,
1124e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1125e9dbad6fSeschrock 			    propname);
1126e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1127e9dbad6fSeschrock 			goto error;
1128e9dbad6fSeschrock 		}
1129e9dbad6fSeschrock 
1130990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1131990b4856Slling 		    &strval, &intval, errbuf) != 0)
1132e9dbad6fSeschrock 			goto error;
1133fa9e4066Sahrens 
1134e9dbad6fSeschrock 		/*
1135e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1136e9dbad6fSeschrock 		 */
1137e9dbad6fSeschrock 		switch (prop) {
1138e7437265Sahrens 		case ZFS_PROP_VERSION:
1139e7437265Sahrens 		{
1140e7437265Sahrens 			int version;
1141e7437265Sahrens 
1142e7437265Sahrens 			if (zhp == NULL)
1143e7437265Sahrens 				break;
1144e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1145e7437265Sahrens 			if (intval < version) {
1146e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1147e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1148e7437265Sahrens 				    version);
1149e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1150e7437265Sahrens 				goto error;
1151e7437265Sahrens 			}
1152e7437265Sahrens 			break;
1153e7437265Sahrens 		}
1154e7437265Sahrens 
1155e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1156b5152584SMatthew Ahrens 		case ZFS_PROP_RECORDSIZE:
1157b5152584SMatthew Ahrens 		{
1158b5152584SMatthew Ahrens 			int maxbs = SPA_MAXBLOCKSIZE;
1159e9316f76SJoe Stein 			if (zpool_hdl != NULL) {
1160e9316f76SJoe Stein 				maxbs = zpool_get_prop_int(zpool_hdl,
1161b5152584SMatthew Ahrens 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1162b5152584SMatthew Ahrens 			}
1163b5152584SMatthew Ahrens 			/*
1164b5152584SMatthew Ahrens 			 * Volumes are limited to a volblocksize of 128KB,
1165b5152584SMatthew Ahrens 			 * because they typically service workloads with
1166b5152584SMatthew Ahrens 			 * small random writes, which incur a large performance
1167b5152584SMatthew Ahrens 			 * penalty with large blocks.
1168b5152584SMatthew Ahrens 			 */
1169b5152584SMatthew Ahrens 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1170b5152584SMatthew Ahrens 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1171b5152584SMatthew Ahrens 			/*
1172b5152584SMatthew Ahrens 			 * The value must be a power of two between
1173b5152584SMatthew Ahrens 			 * SPA_MINBLOCKSIZE and maxbs.
1174b5152584SMatthew Ahrens 			 */
1175e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1176b5152584SMatthew Ahrens 			    intval > maxbs || !ISP2(intval)) {
117799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1178b5152584SMatthew Ahrens 				    "'%s' must be power of 2 from 512B "
1179b5152584SMatthew Ahrens 				    "to %uKB"), propname, maxbs >> 10);
1180e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1181e9dbad6fSeschrock 				goto error;
1182fa9e4066Sahrens 			}
1183fa9e4066Sahrens 			break;
1184b5152584SMatthew Ahrens 		}
1185663207adSDon Brady 
1186663207adSDon Brady 		case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1187663207adSDon Brady 			if (zpool_hdl != NULL) {
1188663207adSDon Brady 				char state[64] = "";
1189663207adSDon Brady 
1190663207adSDon Brady 				/*
1191663207adSDon Brady 				 * Issue a warning but do not fail so that
1192663207adSDon Brady 				 * tests for setable properties succeed.
1193663207adSDon Brady 				 */
1194663207adSDon Brady 				if (zpool_prop_get_feature(zpool_hdl,
1195663207adSDon Brady 				    "feature@allocation_classes", state,
1196663207adSDon Brady 				    sizeof (state)) != 0 ||
1197663207adSDon Brady 				    strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1198663207adSDon Brady 					(void) fprintf(stderr, gettext(
1199663207adSDon Brady 					    "%s: property requires a special "
1200663207adSDon Brady 					    "device in the pool\n"), propname);
1201663207adSDon Brady 				}
1202663207adSDon Brady 			}
1203663207adSDon Brady 			if (intval != 0 &&
1204663207adSDon Brady 			    (intval < SPA_MINBLOCKSIZE ||
1205663207adSDon Brady 			    intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) {
1206663207adSDon Brady 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1207663207adSDon Brady 				    "invalid '%s=%d' property: must be zero or "
1208663207adSDon Brady 				    "a power of 2 from 512B to 128K"), propname,
1209663207adSDon Brady 				    intval);
1210663207adSDon Brady 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1211663207adSDon Brady 				goto error;
1212663207adSDon Brady 			}
1213663207adSDon Brady 			break;
1214663207adSDon Brady 
12154201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
12164201a95eSRic Aleshire 		{
12174201a95eSRic Aleshire 			/*
12184201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
12194201a95eSRic Aleshire 			 * internal hex label string.
12204201a95eSRic Aleshire 			 */
12214201a95eSRic Aleshire 
12224201a95eSRic Aleshire 			m_label_t *new_sl;
12234201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
12244201a95eSRic Aleshire 
12254201a95eSRic Aleshire 			/* Default value is already OK. */
12264201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
12274201a95eSRic Aleshire 				break;
12284201a95eSRic Aleshire 
12294201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
12304201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
12314201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
12324201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
12334201a95eSRic Aleshire 				goto badlabel;
12344201a95eSRic Aleshire 			}
12354201a95eSRic Aleshire 
12364201a95eSRic Aleshire 			/* Now translate to hex internal label string */
12374201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
12384201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
12394201a95eSRic Aleshire 				if (hex)
12404201a95eSRic Aleshire 					free(hex);
12414201a95eSRic Aleshire 				goto badlabel;
12424201a95eSRic Aleshire 			}
12434201a95eSRic Aleshire 			m_label_free(new_sl);
12444201a95eSRic Aleshire 
12454201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
12464201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
12474201a95eSRic Aleshire 				free(hex);
12484201a95eSRic Aleshire 				break;
12494201a95eSRic Aleshire 			}
12504201a95eSRic Aleshire 
12514201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1252569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
12534201a95eSRic Aleshire 			    DATA_TYPE_STRING);
12544201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12554201a95eSRic Aleshire 			    hex) == 0);
12564201a95eSRic Aleshire 			free(hex);
12574201a95eSRic Aleshire 
12584201a95eSRic Aleshire 			break;
12594201a95eSRic Aleshire 
12604201a95eSRic Aleshire badlabel:
12614201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12624201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
12634201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12644201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
12654201a95eSRic Aleshire 			goto error;
12664201a95eSRic Aleshire 
12674201a95eSRic Aleshire 		}
12684201a95eSRic Aleshire 
1269e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
127089eef05eSrm 		{
127189eef05eSrm 			namecheck_err_t why;
127289eef05eSrm 
1273e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1274e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1275e9dbad6fSeschrock 				break;
1276fa9e4066Sahrens 
127789eef05eSrm 			if (mountpoint_namecheck(strval, &why)) {
127889eef05eSrm 				switch (why) {
127989eef05eSrm 				case NAME_ERR_LEADING_SLASH:
128089eef05eSrm 					zfs_error_aux(hdl,
128189eef05eSrm 					    dgettext(TEXT_DOMAIN,
128289eef05eSrm 					    "'%s' must be an absolute path, "
128389eef05eSrm 					    "'none', or 'legacy'"), propname);
128489eef05eSrm 					break;
128589eef05eSrm 				case NAME_ERR_TOOLONG:
128689eef05eSrm 					zfs_error_aux(hdl,
128789eef05eSrm 					    dgettext(TEXT_DOMAIN,
128889eef05eSrm 					    "component of '%s' is too long"),
128989eef05eSrm 					    propname);
129089eef05eSrm 					break;
129188f61deeSIgor Kozhukhov 
129288f61deeSIgor Kozhukhov 				default:
129388f61deeSIgor Kozhukhov 					zfs_error_aux(hdl,
129488f61deeSIgor Kozhukhov 					    dgettext(TEXT_DOMAIN,
129588f61deeSIgor Kozhukhov 					    "(%d) not defined"),
129688f61deeSIgor Kozhukhov 					    why);
129788f61deeSIgor Kozhukhov 					break;
129889eef05eSrm 				}
1299e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1300e9dbad6fSeschrock 				goto error;
1301fa9e4066Sahrens 			}
130289eef05eSrm 		}
130389eef05eSrm 
1304f3861e1aSahl 			/*FALLTHRU*/
1305fa9e4066Sahrens 
1306da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1307f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1308f3861e1aSahl 			/*
1309da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1310da6c28aaSamw 			 * properties, check if it can be set in a
1311da6c28aaSamw 			 * global/non-global zone based on
1312f3861e1aSahl 			 * the zoned property value:
1313f3861e1aSahl 			 *
1314f3861e1aSahl 			 *		global zone	    non-global zone
1315f3861e1aSahl 			 * --------------------------------------------------
1316f3861e1aSahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1317*0dfe541eSEvan Layton 			 *		sharenfs (no)	    sharenfs (yes)
1318*0dfe541eSEvan Layton 			 *		sharesmb (no)	    sharesmb (yes)
1319f3861e1aSahl 			 *
1320f3861e1aSahl 			 * zoned=off	mountpoint (yes)	N/A
1321f3861e1aSahl 			 *		sharenfs (yes)
1322da6c28aaSamw 			 *		sharesmb (yes)
1323f3861e1aSahl 			 */
1324e9dbad6fSeschrock 			if (zoned) {
1325e9dbad6fSeschrock 				if (getzoneid() == GLOBAL_ZONEID) {
1326e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1327e9dbad6fSeschrock 					    "'%s' cannot be set on "
1328e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1329e9dbad6fSeschrock 					    propname);
1330e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1331e9dbad6fSeschrock 					    errbuf);
1332e9dbad6fSeschrock 					goto error;
1333fa9e4066Sahrens 				}
1334e9dbad6fSeschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
1335e9dbad6fSeschrock 				/*
1336e9dbad6fSeschrock 				 * If zoned property is 'off', this must be in
133714843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1338e9dbad6fSeschrock 				 */
1339e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1340e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1341e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1342e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1343e9dbad6fSeschrock 				goto error;
1344fa9e4066Sahrens 			}
1345f3861e1aSahl 
134667331909Sdougm 			/*
134767331909Sdougm 			 * At this point, it is legitimate to set the
134867331909Sdougm 			 * property. Now we want to make sure that the
134967331909Sdougm 			 * property value is valid if it is sharenfs.
135067331909Sdougm 			 */
1351da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1352da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
1353fac3008cSeschrock 			    strcmp(strval, "on") != 0 &&
1354fac3008cSeschrock 			    strcmp(strval, "off") != 0) {
1355da6c28aaSamw 				zfs_share_proto_t proto;
1356da6c28aaSamw 
1357da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1358da6c28aaSamw 					proto = PROTO_SMB;
1359da6c28aaSamw 				else
1360da6c28aaSamw 					proto = PROTO_NFS;
136167331909Sdougm 
136267331909Sdougm 				/*
1363da6c28aaSamw 				 * Must be an valid sharing protocol
1364da6c28aaSamw 				 * option string so init the libshare
1365da6c28aaSamw 				 * in order to enable the parser and
1366da6c28aaSamw 				 * then parse the options. We use the
1367da6c28aaSamw 				 * control API since we don't care about
1368da6c28aaSamw 				 * the current configuration and don't
136967331909Sdougm 				 * want the overhead of loading it
137067331909Sdougm 				 * until we actually do something.
137167331909Sdougm 				 */
137267331909Sdougm 
1373fac3008cSeschrock 				if (zfs_init_libshare(hdl,
1374fac3008cSeschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
1375fac3008cSeschrock 					/*
1376fac3008cSeschrock 					 * An error occurred so we can't do
1377fac3008cSeschrock 					 * anything
1378fac3008cSeschrock 					 */
1379fac3008cSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1380fac3008cSeschrock 					    "'%s' cannot be set: problem "
1381fac3008cSeschrock 					    "in share initialization"),
1382fac3008cSeschrock 					    propname);
1383fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1384fac3008cSeschrock 					    errbuf);
1385fac3008cSeschrock 					goto error;
1386fac3008cSeschrock 				}
138767331909Sdougm 
1388da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
1389fac3008cSeschrock 					/*
1390fac3008cSeschrock 					 * There was an error in parsing so
1391fac3008cSeschrock 					 * deal with it by issuing an error
1392fac3008cSeschrock 					 * message and leaving after
1393fac3008cSeschrock 					 * uninitializing the the libshare
1394fac3008cSeschrock 					 * interface.
1395fac3008cSeschrock 					 */
1396fac3008cSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1397fac3008cSeschrock 					    "'%s' cannot be set to invalid "
1398fac3008cSeschrock 					    "options"), propname);
1399fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1400fac3008cSeschrock 					    errbuf);
1401fac3008cSeschrock 					zfs_uninit_libshare(hdl);
1402fac3008cSeschrock 					goto error;
1403fac3008cSeschrock 				}
140467331909Sdougm 				zfs_uninit_libshare(hdl);
140567331909Sdougm 			}
140667331909Sdougm 
1407da6c28aaSamw 			break;
140888f61deeSIgor Kozhukhov 
1409eb633035STom Caputi 		case ZFS_PROP_KEYLOCATION:
1410eb633035STom Caputi 			if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1411eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1412eb633035STom Caputi 				    "invalid keylocation"));
1413eb633035STom Caputi 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1414eb633035STom Caputi 				goto error;
1415eb633035STom Caputi 			}
1416eb633035STom Caputi 
1417eb633035STom Caputi 			if (zhp != NULL) {
1418eb633035STom Caputi 				uint64_t crypt =
1419eb633035STom Caputi 				    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1420eb633035STom Caputi 
1421eb633035STom Caputi 				if (crypt == ZIO_CRYPT_OFF &&
1422eb633035STom Caputi 				    strcmp(strval, "none") != 0) {
1423eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1424eb633035STom Caputi 					    "keylocation must be 'none' "
1425eb633035STom Caputi 					    "for unencrypted datasets"));
1426eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1427eb633035STom Caputi 					    errbuf);
1428eb633035STom Caputi 					goto error;
1429eb633035STom Caputi 				} else if (crypt != ZIO_CRYPT_OFF &&
1430eb633035STom Caputi 				    strcmp(strval, "none") == 0) {
1431eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432eb633035STom Caputi 					    "keylocation must not be 'none' "
1433eb633035STom Caputi 					    "for encrypted datasets"));
1434eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1435eb633035STom Caputi 					    errbuf);
1436eb633035STom Caputi 					goto error;
1437eb633035STom Caputi 				}
1438eb633035STom Caputi 			}
1439eb633035STom Caputi 			break;
1440eb633035STom Caputi 
1441eb633035STom Caputi 		case ZFS_PROP_PBKDF2_ITERS:
1442eb633035STom Caputi 			if (intval < MIN_PBKDF2_ITERATIONS) {
1443eb633035STom Caputi 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1444eb633035STom Caputi 				    "minimum pbkdf2 iterations is %u"),
1445eb633035STom Caputi 				    MIN_PBKDF2_ITERATIONS);
1446eb633035STom Caputi 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1447eb633035STom Caputi 				goto error;
1448eb633035STom Caputi 			}
1449eb633035STom Caputi 			break;
1450eb633035STom Caputi 
1451da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1452da6c28aaSamw 			chosen_utf = (int)intval;
1453da6c28aaSamw 			break;
145488f61deeSIgor Kozhukhov 
1455da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1456da6c28aaSamw 			chosen_normal = (int)intval;
1457f3861e1aSahl 			break;
145888f61deeSIgor Kozhukhov 
145988f61deeSIgor Kozhukhov 		default:
146088f61deeSIgor Kozhukhov 			break;
1461e9dbad6fSeschrock 		}
1462fa9e4066Sahrens 
1463e9dbad6fSeschrock 		/*
1464e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1465e9dbad6fSeschrock 		 * checks to enforce.
1466e9dbad6fSeschrock 		 */
1467e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1468e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1469e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1470e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1471e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1472e9dbad6fSeschrock 			char buf[64];
1473e9dbad6fSeschrock 
1474e9dbad6fSeschrock 			switch (prop) {
1475e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1476e9dbad6fSeschrock 				if (intval > volsize) {
1477e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1478e9dbad6fSeschrock 					    "'%s' is greater than current "
1479e9dbad6fSeschrock 					    "volume size"), propname);
1480e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1481e9dbad6fSeschrock 					    errbuf);
1482e9dbad6fSeschrock 					goto error;
1483e9dbad6fSeschrock 				}
1484e9dbad6fSeschrock 				break;
1485e9dbad6fSeschrock 
14861c10ae76SMike Gerdts 			case ZFS_PROP_REFRESERVATION:
14871c10ae76SMike Gerdts 				if (intval > volsize && intval != UINT64_MAX) {
14881c10ae76SMike Gerdts 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14891c10ae76SMike Gerdts 					    "'%s' is greater than current "
14901c10ae76SMike Gerdts 					    "volume size"), propname);
14911c10ae76SMike Gerdts 					(void) zfs_error(hdl, EZFS_BADPROP,
14921c10ae76SMike Gerdts 					    errbuf);
14931c10ae76SMike Gerdts 					goto error;
14941c10ae76SMike Gerdts 				}
14951c10ae76SMike Gerdts 				break;
14961c10ae76SMike Gerdts 
1497e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1498e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1499e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1500e9dbad6fSeschrock 					    sizeof (buf));
1501e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1502e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1503e9dbad6fSeschrock 					    "volume block size (%s)"),
1504e9dbad6fSeschrock 					    propname, buf);
1505e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1506e9dbad6fSeschrock 					    errbuf);
1507e9dbad6fSeschrock 					goto error;
1508e9dbad6fSeschrock 				}
1509e9dbad6fSeschrock 
1510e9dbad6fSeschrock 				if (intval == 0) {
1511e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1513e9dbad6fSeschrock 					    propname);
1514e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1515e9dbad6fSeschrock 					    errbuf);
1516e9dbad6fSeschrock 					goto error;
1517e9dbad6fSeschrock 				}
1518f3861e1aSahl 				break;
151988f61deeSIgor Kozhukhov 
152088f61deeSIgor Kozhukhov 			default:
152188f61deeSIgor Kozhukhov 				break;
1522fa9e4066Sahrens 			}
1523e9dbad6fSeschrock 		}
1524eb633035STom Caputi 
1525eb633035STom Caputi 		/* check encryption properties */
1526eb633035STom Caputi 		if (zhp != NULL) {
1527eb633035STom Caputi 			int64_t crypt = zfs_prop_get_int(zhp,
1528eb633035STom Caputi 			    ZFS_PROP_ENCRYPTION);
1529eb633035STom Caputi 
1530eb633035STom Caputi 			switch (prop) {
1531eb633035STom Caputi 			case ZFS_PROP_COPIES:
1532eb633035STom Caputi 				if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1533eb633035STom Caputi 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1534eb633035STom Caputi 					    "encrypted datasets cannot have "
1535eb633035STom Caputi 					    "3 copies"));
1536eb633035STom Caputi 					(void) zfs_error(hdl, EZFS_BADPROP,
1537eb633035STom Caputi 					    errbuf);
1538eb633035STom Caputi 					goto error;
1539eb633035STom Caputi 				}
1540eb633035STom Caputi 				break;
1541eb633035STom Caputi 			default:
1542eb633035STom Caputi 				break;
1543eb633035STom Caputi 			}
1544eb633035STom Caputi 		}
1545e9dbad6fSeschrock 	}
1546fa9e4066Sahrens 
1547da6c28aaSamw 	/*
1548da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1549da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1550da6c28aaSamw 	 *
1551da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1552da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1553da6c28aaSamw 	 */
1554de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1555da6c28aaSamw 		if (nvlist_add_uint64(ret,
1556da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1557da6c28aaSamw 			(void) no_memory(hdl);
1558da6c28aaSamw 			goto error;
1559da6c28aaSamw 		}
1560de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1561da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1563da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1564da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1565da6c28aaSamw 		goto error;
1566da6c28aaSamw 	}
156736db6475SEric Taylor 	return (ret);
156836db6475SEric Taylor 
156936db6475SEric Taylor error:
157036db6475SEric Taylor 	nvlist_free(ret);
157136db6475SEric Taylor 	return (NULL);
157236db6475SEric Taylor }
157336db6475SEric Taylor 
157436db6475SEric Taylor int
157536db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
157636db6475SEric Taylor {
157736db6475SEric Taylor 	uint64_t old_volsize;
157836db6475SEric Taylor 	uint64_t new_volsize;
157936db6475SEric Taylor 	uint64_t old_reservation;
158036db6475SEric Taylor 	uint64_t new_reservation;
158136db6475SEric Taylor 	zfs_prop_t resv_prop;
1582c61ea566SGeorge Wilson 	nvlist_t *props;
1583b73ccab0SMike Gerdts 	zpool_handle_t *zph = zpool_handle(zhp);
1584da6c28aaSamw 
1585e9dbad6fSeschrock 	/*
1586e9dbad6fSeschrock 	 * If this is an existing volume, and someone is setting the volsize,
1587e9dbad6fSeschrock 	 * make sure that it matches the reservation, or add it if necessary.
1588e9dbad6fSeschrock 	 */
158936db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
159036db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
159136db6475SEric Taylor 		return (-1);
159236db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1593c61ea566SGeorge Wilson 
1594c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1595c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1596c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1597c61ea566SGeorge Wilson 
1598b73ccab0SMike Gerdts 	if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1599c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1600c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1601c61ea566SGeorge Wilson 		fnvlist_free(props);
160236db6475SEric Taylor 		return (0);
1603fa9e4066Sahrens 	}
160436db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1605c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1606c61ea566SGeorge Wilson 		fnvlist_free(props);
160736db6475SEric Taylor 		return (-1);
1608c61ea566SGeorge Wilson 	}
1609b73ccab0SMike Gerdts 	new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1610c61ea566SGeorge Wilson 	fnvlist_free(props);
1611c61ea566SGeorge Wilson 
161236db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
161336db6475SEric Taylor 	    new_reservation) != 0) {
161436db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
161536db6475SEric Taylor 		return (-1);
161636db6475SEric Taylor 	}
161736db6475SEric Taylor 	return (1);
1618fa9e4066Sahrens }
1619fa9e4066Sahrens 
16201c10ae76SMike Gerdts /*
16211c10ae76SMike Gerdts  * Helper for 'zfs {set|clone} refreservation=auto'.  Must be called after
16221c10ae76SMike Gerdts  * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
16231c10ae76SMike Gerdts  * Return codes must match zfs_add_synthetic_resv().
16241c10ae76SMike Gerdts  */
16251c10ae76SMike Gerdts static int
16261c10ae76SMike Gerdts zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
16271c10ae76SMike Gerdts {
16281c10ae76SMike Gerdts 	uint64_t volsize;
16291c10ae76SMike Gerdts 	uint64_t resvsize;
16301c10ae76SMike Gerdts 	zfs_prop_t prop;
16311c10ae76SMike Gerdts 	nvlist_t *props;
16321c10ae76SMike Gerdts 
16331c10ae76SMike Gerdts 	if (!ZFS_IS_VOLUME(zhp)) {
16341c10ae76SMike Gerdts 		return (0);
16351c10ae76SMike Gerdts 	}
16361c10ae76SMike Gerdts 
16371c10ae76SMike Gerdts 	if (zfs_which_resv_prop(zhp, &prop) != 0) {
16381c10ae76SMike Gerdts 		return (-1);
16391c10ae76SMike Gerdts 	}
16401c10ae76SMike Gerdts 
16411c10ae76SMike Gerdts 	if (prop != ZFS_PROP_REFRESERVATION) {
16421c10ae76SMike Gerdts 		return (0);
16431c10ae76SMike Gerdts 	}
16441c10ae76SMike Gerdts 
16451c10ae76SMike Gerdts 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
16461c10ae76SMike Gerdts 		/* No value being set, so it can't be "auto" */
16471c10ae76SMike Gerdts 		return (0);
16481c10ae76SMike Gerdts 	}
16491c10ae76SMike Gerdts 	if (resvsize != UINT64_MAX) {
16501c10ae76SMike Gerdts 		/* Being set to a value other than "auto" */
16511c10ae76SMike Gerdts 		return (0);
16521c10ae76SMike Gerdts 	}
16531c10ae76SMike Gerdts 
16541c10ae76SMike Gerdts 	props = fnvlist_alloc();
16551c10ae76SMike Gerdts 
16561c10ae76SMike Gerdts 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
16571c10ae76SMike Gerdts 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
16581c10ae76SMike Gerdts 
16591c10ae76SMike Gerdts 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
16601c10ae76SMike Gerdts 	    &volsize) != 0) {
16611c10ae76SMike Gerdts 		volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
16621c10ae76SMike Gerdts 	}
16631c10ae76SMike Gerdts 
1664b73ccab0SMike Gerdts 	resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1665b73ccab0SMike Gerdts 	    props);
16661c10ae76SMike Gerdts 	fnvlist_free(props);
16671c10ae76SMike Gerdts 
16681c10ae76SMike Gerdts 	(void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
16691c10ae76SMike Gerdts 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
16701c10ae76SMike Gerdts 		(void) no_memory(zhp->zfs_hdl);
16711c10ae76SMike Gerdts 		return (-1);
16721c10ae76SMike Gerdts 	}
16731c10ae76SMike Gerdts 	return (1);
16741c10ae76SMike Gerdts }
16751c10ae76SMike Gerdts 
167692241e0bSTom Erickson void
167792241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
167892241e0bSTom Erickson     char *errbuf)
167992241e0bSTom Erickson {
168092241e0bSTom Erickson 	switch (err) {
168192241e0bSTom Erickson 
168292241e0bSTom Erickson 	case ENOSPC:
168392241e0bSTom Erickson 		/*
168492241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
168592241e0bSTom Erickson 		 * something different; setting a quota or reservation
168692241e0bSTom Erickson 		 * doesn't use any disk space.
168792241e0bSTom Erickson 		 */
168892241e0bSTom Erickson 		switch (prop) {
168992241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
169092241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
169192241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169292241e0bSTom Erickson 			    "size is less than current used or "
169392241e0bSTom Erickson 			    "reserved space"));
169492241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
169592241e0bSTom Erickson 			break;
169692241e0bSTom Erickson 
169792241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
169892241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
169992241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170092241e0bSTom Erickson 			    "size is greater than available space"));
170192241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
170292241e0bSTom Erickson 			break;
170392241e0bSTom Erickson 
170492241e0bSTom Erickson 		default:
170592241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
170692241e0bSTom Erickson 			break;
170792241e0bSTom Erickson 		}
170892241e0bSTom Erickson 		break;
170992241e0bSTom Erickson 
171092241e0bSTom Erickson 	case EBUSY:
171192241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
171292241e0bSTom Erickson 		break;
171392241e0bSTom Erickson 
171492241e0bSTom Erickson 	case EROFS:
171592241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
171692241e0bSTom Erickson 		break;
171792241e0bSTom Erickson 
17186fdcb3d1SWill Andrews 	case E2BIG:
17196fdcb3d1SWill Andrews 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17206fdcb3d1SWill Andrews 		    "property value too long"));
17216fdcb3d1SWill Andrews 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
17226fdcb3d1SWill Andrews 		break;
17236fdcb3d1SWill Andrews 
172492241e0bSTom Erickson 	case ENOTSUP:
172592241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172692241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
172792241e0bSTom Erickson 		    "property or value"));
172892241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
172992241e0bSTom Erickson 		break;
173092241e0bSTom Erickson 
173192241e0bSTom Erickson 	case ERANGE:
1732b5152584SMatthew Ahrens 		if (prop == ZFS_PROP_COMPRESSION ||
1733b5152584SMatthew Ahrens 		    prop == ZFS_PROP_RECORDSIZE) {
173492241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173592241e0bSTom Erickson 			    "property setting is not allowed on "
173692241e0bSTom Erickson 			    "bootable datasets"));
173792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
173845818ee1SMatthew Ahrens 		} else if (prop == ZFS_PROP_CHECKSUM ||
173945818ee1SMatthew Ahrens 		    prop == ZFS_PROP_DEDUP) {
174045818ee1SMatthew Ahrens 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174145818ee1SMatthew Ahrens 			    "property setting is not allowed on "
174245818ee1SMatthew Ahrens 			    "root pools"));
174345818ee1SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
174492241e0bSTom Erickson 		} else {
174592241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
174692241e0bSTom Erickson 		}
174792241e0bSTom Erickson 		break;
174892241e0bSTom Erickson 
1749ab003da8SJim Dunham 	case EINVAL:
1750ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1751ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1752ab003da8SJim Dunham 		} else {
1753ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1754ab003da8SJim Dunham 		}
1755ab003da8SJim Dunham 		break;
1756ab003da8SJim Dunham 
1757eb633035STom Caputi 	case EACCES:
1758eb633035STom Caputi 		if (prop == ZFS_PROP_KEYLOCATION) {
1759eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1760eb633035STom Caputi 			    "keylocation may only be set on encryption roots"));
1761eb633035STom Caputi 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1762eb633035STom Caputi 		} else {
1763eb633035STom Caputi 			(void) zfs_standard_error(hdl, err, errbuf);
1764eb633035STom Caputi 		}
1765eb633035STom Caputi 		break;
1766eb633035STom Caputi 
176792241e0bSTom Erickson 	case EOVERFLOW:
176892241e0bSTom Erickson 		/*
176992241e0bSTom Erickson 		 * This platform can't address a volume this big.
177092241e0bSTom Erickson 		 */
177192241e0bSTom Erickson #ifdef _ILP32
177292241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
177392241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
177492241e0bSTom Erickson 			break;
177592241e0bSTom Erickson 		}
177692241e0bSTom Erickson #endif
177792241e0bSTom Erickson 		/* FALLTHROUGH */
177892241e0bSTom Erickson 	default:
177992241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
178092241e0bSTom Erickson 	}
178192241e0bSTom Erickson }
178292241e0bSTom Erickson 
1783fa9e4066Sahrens /*
1784fa9e4066Sahrens  * Given a property name and value, set the property for the given dataset.
1785fa9e4066Sahrens  */
1786fa9e4066Sahrens int
1787e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1788fa9e4066Sahrens {
1789e9dbad6fSeschrock 	int ret = -1;
179099653d4eSeschrock 	char errbuf[1024];
179199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
179230925561SChris Williamson 	nvlist_t *nvl = NULL;
179399653d4eSeschrock 
179499653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1795e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
179699653d4eSeschrock 	    zhp->zfs_name);
179799653d4eSeschrock 
1798e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1799e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1800e9dbad6fSeschrock 		(void) no_memory(hdl);
1801e9dbad6fSeschrock 		goto error;
1802fa9e4066Sahrens 	}
1803fa9e4066Sahrens 
180430925561SChris Williamson 	ret = zfs_prop_set_list(zhp, nvl);
1805990b4856Slling 
180630925561SChris Williamson error:
1807e9dbad6fSeschrock 	nvlist_free(nvl);
180830925561SChris Williamson 	return (ret);
180930925561SChris Williamson }
1810e9dbad6fSeschrock 
1811e9dbad6fSeschrock 
181236db6475SEric Taylor 
181330925561SChris Williamson /*
181430925561SChris Williamson  * Given an nvlist of property names and values, set the properties for the
181530925561SChris Williamson  * given dataset.
181630925561SChris Williamson  */
181730925561SChris Williamson int
181830925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
181930925561SChris Williamson {
182030925561SChris Williamson 	zfs_cmd_t zc = { 0 };
182130925561SChris Williamson 	int ret = -1;
182230925561SChris Williamson 	prop_changelist_t **cls = NULL;
182330925561SChris Williamson 	int cl_idx;
182430925561SChris Williamson 	char errbuf[1024];
182530925561SChris Williamson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
182630925561SChris Williamson 	nvlist_t *nvl;
182730925561SChris Williamson 	int nvl_len;
1828f83b46baSPaul Dagnelie 	int added_resv = 0;
1829fa9e4066Sahrens 
183030925561SChris Williamson 	(void) snprintf(errbuf, sizeof (errbuf),
183130925561SChris Williamson 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
183230925561SChris Williamson 	    zhp->zfs_name);
183330925561SChris Williamson 
183430925561SChris Williamson 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1835e9316f76SJoe Stein 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1836eb633035STom Caputi 	    B_FALSE, errbuf)) == NULL)
1837fa9e4066Sahrens 		goto error;
1838fa9e4066Sahrens 
18390068372bSMark J Musante 	/*
184030925561SChris Williamson 	 * We have to check for any extra properties which need to be added
184130925561SChris Williamson 	 * before computing the length of the nvlist.
18420068372bSMark J Musante 	 */
184330925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
184430925561SChris Williamson 	    elem != NULL;
184530925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
184630925561SChris Williamson 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
184730925561SChris Williamson 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
184830925561SChris Williamson 			goto error;
184930925561SChris Williamson 		}
18504445fffbSMatthew Ahrens 	}
18511c10ae76SMike Gerdts 
18521c10ae76SMike Gerdts 	if (added_resv != 1 &&
18531c10ae76SMike Gerdts 	    (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
18541c10ae76SMike Gerdts 		goto error;
18551c10ae76SMike Gerdts 	}
18561c10ae76SMike Gerdts 
185730925561SChris Williamson 	/*
185830925561SChris Williamson 	 * Check how many properties we're setting and allocate an array to
185930925561SChris Williamson 	 * store changelist pointers for postfix().
186030925561SChris Williamson 	 */
186130925561SChris Williamson 	nvl_len = 0;
186230925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
186330925561SChris Williamson 	    elem != NULL;
186430925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem))
186530925561SChris Williamson 		nvl_len++;
186630925561SChris Williamson 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
18670068372bSMark J Musante 		goto error;
1868fa9e4066Sahrens 
186930925561SChris Williamson 	cl_idx = 0;
187030925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
187130925561SChris Williamson 	    elem != NULL;
187230925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
187330925561SChris Williamson 
187430925561SChris Williamson 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
187530925561SChris Williamson 
187630925561SChris Williamson 		assert(cl_idx < nvl_len);
187730925561SChris Williamson 		/*
187830925561SChris Williamson 		 * We don't want to unmount & remount the dataset when changing
187930925561SChris Williamson 		 * its canmount property to 'on' or 'noauto'.  We only use
188030925561SChris Williamson 		 * the changelist logic to unmount when setting canmount=off.
188130925561SChris Williamson 		 */
1882c079fa4dSAndriy Gapon 		if (prop != ZFS_PROP_CANMOUNT ||
1883c079fa4dSAndriy Gapon 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1884c079fa4dSAndriy Gapon 		    zfs_is_mounted(zhp, NULL))) {
188530925561SChris Williamson 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
188630925561SChris Williamson 			if (cls[cl_idx] == NULL)
188730925561SChris Williamson 				goto error;
188830925561SChris Williamson 		}
188930925561SChris Williamson 
189030925561SChris Williamson 		if (prop == ZFS_PROP_MOUNTPOINT &&
189130925561SChris Williamson 		    changelist_haszonedchild(cls[cl_idx])) {
189230925561SChris Williamson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
189330925561SChris Williamson 			    "child dataset with inherited mountpoint is used "
189430925561SChris Williamson 			    "in a non-global zone"));
189530925561SChris Williamson 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
189630925561SChris Williamson 			goto error;
189730925561SChris Williamson 		}
189830925561SChris Williamson 
189930925561SChris Williamson 		if (cls[cl_idx] != NULL &&
190030925561SChris Williamson 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
190130925561SChris Williamson 			goto error;
190230925561SChris Williamson 
190330925561SChris Williamson 		cl_idx++;
190430925561SChris Williamson 	}
190530925561SChris Williamson 	assert(cl_idx == nvl_len);
190630925561SChris Williamson 
1907fa9e4066Sahrens 	/*
190830925561SChris Williamson 	 * Execute the corresponding ioctl() to set this list of properties.
1909fa9e4066Sahrens 	 */
1910fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1911fa9e4066Sahrens 
191230925561SChris Williamson 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
191330925561SChris Williamson 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1914e9dbad6fSeschrock 		goto error;
1915e9dbad6fSeschrock 
1916ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1917743a77edSAlan Wright 
1918fa9e4066Sahrens 	if (ret != 0) {
1919f62db44dSAndrew Stormont 		if (zc.zc_nvlist_dst_filled == B_FALSE) {
1920f62db44dSAndrew Stormont 			(void) zfs_standard_error(hdl, errno, errbuf);
1921f62db44dSAndrew Stormont 			goto error;
1922f62db44dSAndrew Stormont 		}
1923f62db44dSAndrew Stormont 
192430925561SChris Williamson 		/* Get the list of unset properties back and report them. */
192530925561SChris Williamson 		nvlist_t *errorprops = NULL;
192630925561SChris Williamson 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
192730925561SChris Williamson 			goto error;
1928f62db44dSAndrew Stormont 		for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
192930925561SChris Williamson 		    elem != NULL;
1930f62db44dSAndrew Stormont 		    elem = nvlist_next_nvpair(errorprops, elem)) {
193130925561SChris Williamson 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
193230925561SChris Williamson 			zfs_setprop_error(hdl, prop, errno, errbuf);
193330925561SChris Williamson 		}
193430925561SChris Williamson 		nvlist_free(errorprops);
193530925561SChris Williamson 
193636db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
193736db6475SEric Taylor 			/* clean up the volsize property we tried to set */
193836db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
193936db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
194036db6475SEric Taylor 			nvlist_free(nvl);
194130925561SChris Williamson 			nvl = NULL;
194236db6475SEric Taylor 			zcmd_free_nvlists(&zc);
194330925561SChris Williamson 
194436db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
194536db6475SEric Taylor 				goto error;
194636db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
194736db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
194836db6475SEric Taylor 			    old_volsize) != 0)
194936db6475SEric Taylor 				goto error;
195036db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
195136db6475SEric Taylor 				goto error;
195236db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
195336db6475SEric Taylor 		}
1954fa9e4066Sahrens 	} else {
195530925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
195630925561SChris Williamson 			if (cls[cl_idx] != NULL) {
195730925561SChris Williamson 				int clp_err = changelist_postfix(cls[cl_idx]);
195830925561SChris Williamson 				if (clp_err != 0)
195930925561SChris Williamson 					ret = clp_err;
196030925561SChris Williamson 			}
196130925561SChris Williamson 		}
1962a227b7f4Shs 
1963fa9e4066Sahrens 		/*
1964fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1965fa9e4066Sahrens 		 * is reflected.
1966fa9e4066Sahrens 		 */
1967a227b7f4Shs 		if (ret == 0)
1968e9dbad6fSeschrock 			(void) get_stats(zhp);
1969fa9e4066Sahrens 	}
1970fa9e4066Sahrens 
1971fa9e4066Sahrens error:
1972e9dbad6fSeschrock 	nvlist_free(nvl);
1973e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
197430925561SChris Williamson 	if (cls != NULL) {
197530925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
197630925561SChris Williamson 			if (cls[cl_idx] != NULL)
197730925561SChris Williamson 				changelist_free(cls[cl_idx]);
197830925561SChris Williamson 		}
197930925561SChris Williamson 		free(cls);
198030925561SChris Williamson 	}
1981fa9e4066Sahrens 	return (ret);
1982fa9e4066Sahrens }
1983fa9e4066Sahrens 
1984fa9e4066Sahrens /*
198592241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
198692241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1987fa9e4066Sahrens  */
1988fa9e4066Sahrens int
198992241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1990fa9e4066Sahrens {
1991fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1992fa9e4066Sahrens 	int ret;
1993fa9e4066Sahrens 	prop_changelist_t *cl;
199499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
199599653d4eSeschrock 	char errbuf[1024];
1996e9dbad6fSeschrock 	zfs_prop_t prop;
199799653d4eSeschrock 
199899653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
199999653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2000fa9e4066Sahrens 
200192241e0bSTom Erickson 	zc.zc_cookie = received;
2002990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2003e9dbad6fSeschrock 		/*
2004e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
2005e9dbad6fSeschrock 		 * small, so just do it here.
2006e9dbad6fSeschrock 		 */
2007e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
2008e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2009e9dbad6fSeschrock 			    "invalid property"));
2010e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2011e9dbad6fSeschrock 		}
2012e9dbad6fSeschrock 
2013e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2014e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2015e9dbad6fSeschrock 
2016e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2017e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2018e9dbad6fSeschrock 
2019e9dbad6fSeschrock 		return (0);
2020e9dbad6fSeschrock 	}
2021e9dbad6fSeschrock 
2022fa9e4066Sahrens 	/*
2023fa9e4066Sahrens 	 * Verify that this property is inheritable.
2024fa9e4066Sahrens 	 */
202599653d4eSeschrock 	if (zfs_prop_readonly(prop))
202699653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2027fa9e4066Sahrens 
202892241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
202999653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2030fa9e4066Sahrens 
2031fa9e4066Sahrens 	/*
2032fa9e4066Sahrens 	 * Check to see if the value applies to this type
2033fa9e4066Sahrens 	 */
203499653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
203599653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2036fa9e4066Sahrens 
2037bf7c2d40Srm 	/*
203836db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
2039bf7c2d40Srm 	 */
2040bf7c2d40Srm 	propname = zfs_prop_to_name(prop);
2041fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2042e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2043fa9e4066Sahrens 
2044fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2045fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
204699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
204799653d4eSeschrock 		    "dataset is used in a non-global zone"));
204899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2049fa9e4066Sahrens 	}
2050fa9e4066Sahrens 
2051fa9e4066Sahrens 	/*
2052fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
2053fa9e4066Sahrens 	 */
20540069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2055fa9e4066Sahrens 		return (-1);
2056fa9e4066Sahrens 
2057fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
205899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205999653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
206099653d4eSeschrock 		    "in a non-global zone"));
206199653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2062fa9e4066Sahrens 		goto error;
2063fa9e4066Sahrens 	}
2064fa9e4066Sahrens 
2065fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
2066fa9e4066Sahrens 		goto error;
2067fa9e4066Sahrens 
2068e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
206999653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2070fa9e4066Sahrens 	} else {
2071fa9e4066Sahrens 
2072efc555ebSnd 		if ((ret = changelist_postfix(cl)) != 0)
2073fa9e4066Sahrens 			goto error;
2074fa9e4066Sahrens 
2075fa9e4066Sahrens 		/*
2076fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
2077fa9e4066Sahrens 		 */
2078fa9e4066Sahrens 		(void) get_stats(zhp);
2079fa9e4066Sahrens 	}
2080fa9e4066Sahrens 
2081fa9e4066Sahrens error:
2082fa9e4066Sahrens 	changelist_free(cl);
2083fa9e4066Sahrens 	return (ret);
2084fa9e4066Sahrens }
2085fa9e4066Sahrens 
20867f7322feSeschrock /*
20877f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
20887f7322feSeschrock  * extract them appropriately.
20897f7322feSeschrock  */
20907f7322feSeschrock static uint64_t
20917f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20927f7322feSeschrock {
20937f7322feSeschrock 	nvlist_t *nv;
20947f7322feSeschrock 	uint64_t value;
20957f7322feSeschrock 
2096a2eea2e1Sahrens 	*source = NULL;
20977f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20987f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
2099990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2100990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21017f7322feSeschrock 	} else {
21022e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
21032e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
21047f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
21057f7322feSeschrock 		*source = "";
21067f7322feSeschrock 	}
21077f7322feSeschrock 
21087f7322feSeschrock 	return (value);
21097f7322feSeschrock }
21107f7322feSeschrock 
21119c3fd121SMatthew Ahrens static const char *
21127f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21137f7322feSeschrock {
21147f7322feSeschrock 	nvlist_t *nv;
21159c3fd121SMatthew Ahrens 	const char *value;
21167f7322feSeschrock 
2117a2eea2e1Sahrens 	*source = NULL;
21187f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
21197f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
21209c3fd121SMatthew Ahrens 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2121990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21227f7322feSeschrock 	} else {
21232e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
21242e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
21259c3fd121SMatthew Ahrens 		value = zfs_prop_default_string(prop);
21267f7322feSeschrock 		*source = "";
21277f7322feSeschrock 	}
21287f7322feSeschrock 
21297f7322feSeschrock 	return (value);
21307f7322feSeschrock }
21317f7322feSeschrock 
213292241e0bSTom Erickson static boolean_t
213392241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
213492241e0bSTom Erickson {
213592241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
213692241e0bSTom Erickson }
213792241e0bSTom Erickson 
213892241e0bSTom Erickson static void
213992241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
214092241e0bSTom Erickson {
214192241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
214292241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
214392241e0bSTom Erickson }
214492241e0bSTom Erickson 
214592241e0bSTom Erickson static void
214692241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
214792241e0bSTom Erickson {
214892241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
214992241e0bSTom Erickson 	*cookie = 0;
215092241e0bSTom Erickson }
215192241e0bSTom Erickson 
2152fa9e4066Sahrens /*
2153fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2154fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
2155fa9e4066Sahrens  *
2156fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2157fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2158fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
2159fa9e4066Sahrens  * the source "temporary".
2160fa9e4066Sahrens  */
216199653d4eSeschrock static int
2162990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
216399653d4eSeschrock     char **source, uint64_t *val)
2164fa9e4066Sahrens {
2165bd00f61bSrm 	zfs_cmd_t zc = { 0 };
216696510749Stimh 	nvlist_t *zplprops = NULL;
2167fa9e4066Sahrens 	struct mnttab mnt;
21683ccfa83cSahrens 	char *mntopt_on = NULL;
21693ccfa83cSahrens 	char *mntopt_off = NULL;
217092241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2171fa9e4066Sahrens 
2172fa9e4066Sahrens 	*source = NULL;
2173fa9e4066Sahrens 
21743ccfa83cSahrens 	switch (prop) {
21753ccfa83cSahrens 	case ZFS_PROP_ATIME:
21763ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
21773ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
21783ccfa83cSahrens 		break;
21793ccfa83cSahrens 
21803ccfa83cSahrens 	case ZFS_PROP_DEVICES:
21813ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
21823ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
21833ccfa83cSahrens 		break;
21843ccfa83cSahrens 
21853ccfa83cSahrens 	case ZFS_PROP_EXEC:
21863ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
21873ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
21883ccfa83cSahrens 		break;
21893ccfa83cSahrens 
21903ccfa83cSahrens 	case ZFS_PROP_READONLY:
21913ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
21923ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
21933ccfa83cSahrens 		break;
21943ccfa83cSahrens 
21953ccfa83cSahrens 	case ZFS_PROP_SETUID:
21963ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
21973ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
21983ccfa83cSahrens 		break;
21993ccfa83cSahrens 
22003ccfa83cSahrens 	case ZFS_PROP_XATTR:
22013ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
22023ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
22033ccfa83cSahrens 		break;
2204da6c28aaSamw 
2205da6c28aaSamw 	case ZFS_PROP_NBMAND:
2206da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
2207da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2208da6c28aaSamw 		break;
220988f61deeSIgor Kozhukhov 
221088f61deeSIgor Kozhukhov 	default:
221188f61deeSIgor Kozhukhov 		break;
22123ccfa83cSahrens 	}
22133ccfa83cSahrens 
22143bb79becSeschrock 	/*
22153bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
22163bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
22173bb79becSeschrock 	 * we're looking up a property which requires its presence.
22183bb79becSeschrock 	 */
22193bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
22203ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2221ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2222ebedde84SEric Taylor 		struct mnttab entry;
22233bb79becSeschrock 
2224ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2225ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
22263ccfa83cSahrens 			    entry.mnt_mntopts);
22273ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
22283ccfa83cSahrens 				return (-1);
22293ccfa83cSahrens 		}
22303bb79becSeschrock 
22313bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
22323bb79becSeschrock 	}
22333bb79becSeschrock 
2234fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2235fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2236fa9e4066Sahrens 	else
2237fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2238fa9e4066Sahrens 
2239fa9e4066Sahrens 	switch (prop) {
2240fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2241fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2242fa9e4066Sahrens 	case ZFS_PROP_EXEC:
22433ccfa83cSahrens 	case ZFS_PROP_READONLY:
22443ccfa83cSahrens 	case ZFS_PROP_SETUID:
22453ccfa83cSahrens 	case ZFS_PROP_XATTR:
2246da6c28aaSamw 	case ZFS_PROP_NBMAND:
224799653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2248fa9e4066Sahrens 
224992241e0bSTom Erickson 		if (received)
225092241e0bSTom Erickson 			break;
225192241e0bSTom Erickson 
22523ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
225399653d4eSeschrock 			*val = B_TRUE;
2254fa9e4066Sahrens 			if (src)
2255990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
22563ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
225799653d4eSeschrock 			*val = B_FALSE;
2258fa9e4066Sahrens 			if (src)
2259990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2260fa9e4066Sahrens 		}
226199653d4eSeschrock 		break;
2262fa9e4066Sahrens 
22633ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
2264d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
2265fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2266a9799022Sck 	case ZFS_PROP_REFQUOTA:
2267fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2268a9799022Sck 	case ZFS_PROP_REFRESERVATION:
2269a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2270a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2271a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2272a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2273a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
227492241e0bSTom Erickson 
227592241e0bSTom Erickson 		if (*source == NULL) {
227692241e0bSTom Erickson 			/* not default, must be local */
2277fa9e4066Sahrens 			*source = zhp->zfs_name;
227892241e0bSTom Erickson 		}
227999653d4eSeschrock 		break;
2280fa9e4066Sahrens 
2281fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
228299653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
228399653d4eSeschrock 		break;
2284fa9e4066Sahrens 
228539c23413Seschrock 	case ZFS_PROP_NUMCLONES:
228639c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
228739c23413Seschrock 		break;
228839c23413Seschrock 
2289bd00f61bSrm 	case ZFS_PROP_VERSION:
2290de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2291de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2292de8267e0Stimh 	case ZFS_PROP_CASE:
2293de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2294de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22953d7934e1Srm 			return (-1);
2296bd00f61bSrm 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2297de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2298de8267e0Stimh 			zcmd_free_nvlists(&zc);
2299f4b94bdeSMatthew Ahrens 			return (-1);
2300bd00f61bSrm 		}
2301de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2302de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2303de8267e0Stimh 		    val) != 0) {
2304de8267e0Stimh 			zcmd_free_nvlists(&zc);
2305f4b94bdeSMatthew Ahrens 			return (-1);
2306de8267e0Stimh 		}
2307aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(zplprops);
2308de8267e0Stimh 		zcmd_free_nvlists(&zc);
2309bd00f61bSrm 		break;
2310bd00f61bSrm 
2311ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
2312ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
2313ca48f36fSKeith M Wesolowski 		break;
2314ca48f36fSKeith M Wesolowski 
2315fa9e4066Sahrens 	default:
2316e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
231791ebeef5Sahrens 		case PROP_TYPE_NUMBER:
231891ebeef5Sahrens 		case PROP_TYPE_INDEX:
2319e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
232074e7dc98SMatthew Ahrens 			/*
232114843421SMatthew Ahrens 			 * If we tried to use a default value for a
232274e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
23234d86c0eaSMatthew Ahrens 			 * present.  Note this only applies to "truly"
23244d86c0eaSMatthew Ahrens 			 * readonly properties, not set-once properties
23254d86c0eaSMatthew Ahrens 			 * like volblocksize.
232674e7dc98SMatthew Ahrens 			 */
232774e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
23284d86c0eaSMatthew Ahrens 			    !zfs_prop_setonce(prop) &&
232931f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
233031f572c2STom Erickson 				*source = NULL;
2331ad2760acSMatthew Ahrens 				return (-1);
233274e7dc98SMatthew Ahrens 			}
2333e7437265Sahrens 			break;
2334e7437265Sahrens 
233591ebeef5Sahrens 		case PROP_TYPE_STRING:
2336e7437265Sahrens 		default:
2337e7437265Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2338e7437265Sahrens 			    "cannot get non-numeric property"));
2339e7437265Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2340e7437265Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
2341e7437265Sahrens 		}
2342fa9e4066Sahrens 	}
2343fa9e4066Sahrens 
2344fa9e4066Sahrens 	return (0);
2345fa9e4066Sahrens }
2346fa9e4066Sahrens 
2347fa9e4066Sahrens /*
2348fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2349fa9e4066Sahrens  */
2350fa9e4066Sahrens static void
2351990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2352fa9e4066Sahrens     char *statbuf, size_t statlen)
2353fa9e4066Sahrens {
2354990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2355fa9e4066Sahrens 		return;
2356fa9e4066Sahrens 
2357fa9e4066Sahrens 	if (source == NULL) {
2358990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2359fa9e4066Sahrens 	} else if (source[0] == '\0') {
2360990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
236192241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
236292241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
2363fa9e4066Sahrens 	} else {
2364fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2365990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2366fa9e4066Sahrens 		} else {
2367fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2368990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2369fa9e4066Sahrens 		}
2370fa9e4066Sahrens 	}
2371fa9e4066Sahrens 
2372fa9e4066Sahrens }
2373fa9e4066Sahrens 
237492241e0bSTom Erickson int
237592241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
237692241e0bSTom Erickson     size_t proplen, boolean_t literal)
237792241e0bSTom Erickson {
237892241e0bSTom Erickson 	zfs_prop_t prop;
237992241e0bSTom Erickson 	int err = 0;
238092241e0bSTom Erickson 
238192241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
238292241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
238392241e0bSTom Erickson 			return (-1);
238492241e0bSTom Erickson 
238592241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
238692241e0bSTom Erickson 
238792241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
238892241e0bSTom Erickson 		uint64_t cookie;
238992241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
239092241e0bSTom Erickson 			return (-1);
239192241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
239292241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
239392241e0bSTom Erickson 		    NULL, NULL, 0, literal);
239492241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
239592241e0bSTom Erickson 	} else {
239692241e0bSTom Erickson 		nvlist_t *propval;
239792241e0bSTom Erickson 		char *recvdval;
239892241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
239992241e0bSTom Erickson 		    propname, &propval) != 0)
240092241e0bSTom Erickson 			return (-1);
240192241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
240292241e0bSTom Erickson 		    &recvdval) == 0);
240392241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
240492241e0bSTom Erickson 	}
240592241e0bSTom Erickson 
240692241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
240792241e0bSTom Erickson }
240892241e0bSTom Erickson 
240919b94df9SMatthew Ahrens static int
241019b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
241119b94df9SMatthew Ahrens {
241219b94df9SMatthew Ahrens 	nvlist_t *value;
241319b94df9SMatthew Ahrens 	nvpair_t *pair;
241419b94df9SMatthew Ahrens 
241519b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
241619b94df9SMatthew Ahrens 	if (value == NULL)
241719b94df9SMatthew Ahrens 		return (-1);
241819b94df9SMatthew Ahrens 
241919b94df9SMatthew Ahrens 	propbuf[0] = '\0';
242019b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
242119b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
242219b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
242319b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
242419b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
242519b94df9SMatthew Ahrens 	}
242619b94df9SMatthew Ahrens 
242719b94df9SMatthew Ahrens 	return (0);
242819b94df9SMatthew Ahrens }
242919b94df9SMatthew Ahrens 
243019b94df9SMatthew Ahrens struct get_clones_arg {
243119b94df9SMatthew Ahrens 	uint64_t numclones;
243219b94df9SMatthew Ahrens 	nvlist_t *value;
243319b94df9SMatthew Ahrens 	const char *origin;
24349adfa60dSMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
243519b94df9SMatthew Ahrens };
243619b94df9SMatthew Ahrens 
243719b94df9SMatthew Ahrens int
243819b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
243919b94df9SMatthew Ahrens {
244019b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
244119b94df9SMatthew Ahrens 
244219b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
244319b94df9SMatthew Ahrens 		zfs_close(zhp);
244419b94df9SMatthew Ahrens 		return (0);
244519b94df9SMatthew Ahrens 	}
244619b94df9SMatthew Ahrens 
244719b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
244819b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
244919b94df9SMatthew Ahrens 		goto out;
245019b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
24513b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
245219b94df9SMatthew Ahrens 		gca->numclones--;
245319b94df9SMatthew Ahrens 	}
245419b94df9SMatthew Ahrens 
245519b94df9SMatthew Ahrens out:
245619b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
245719b94df9SMatthew Ahrens 	zfs_close(zhp);
245819b94df9SMatthew Ahrens 	return (0);
245919b94df9SMatthew Ahrens }
246019b94df9SMatthew Ahrens 
246119b94df9SMatthew Ahrens nvlist_t *
246219b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
246319b94df9SMatthew Ahrens {
246419b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
246519b94df9SMatthew Ahrens 
246619b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
246719b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
246819b94df9SMatthew Ahrens 		struct get_clones_arg gca;
246919b94df9SMatthew Ahrens 
247019b94df9SMatthew Ahrens 		/*
247119b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
247219b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
247319b94df9SMatthew Ahrens 		 */
247419b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
247519b94df9SMatthew Ahrens 			return (NULL);
247619b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
247719b94df9SMatthew Ahrens 			return (NULL);
247819b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
247919b94df9SMatthew Ahrens 			nvlist_free(nv);
248019b94df9SMatthew Ahrens 			return (NULL);
248119b94df9SMatthew Ahrens 		}
248219b94df9SMatthew Ahrens 
248319b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
248419b94df9SMatthew Ahrens 		gca.value = value;
248519b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
248619b94df9SMatthew Ahrens 
248719b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
248819b94df9SMatthew Ahrens 			zfs_handle_t *root;
24899adfa60dSMatthew Ahrens 			char pool[ZFS_MAX_DATASET_NAME_LEN];
249019b94df9SMatthew Ahrens 			char *cp = pool;
249119b94df9SMatthew Ahrens 
249219b94df9SMatthew Ahrens 			/* get the pool name */
249319b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
249419b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
249519b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
249619b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
249719b94df9SMatthew Ahrens 
249819b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
249919b94df9SMatthew Ahrens 		}
250019b94df9SMatthew Ahrens 
250119b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
250219b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
250319b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
250419b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
250519b94df9SMatthew Ahrens 			nvlist_free(nv);
250619b94df9SMatthew Ahrens 			nvlist_free(value);
250719b94df9SMatthew Ahrens 			return (NULL);
250819b94df9SMatthew Ahrens 		}
250919b94df9SMatthew Ahrens 		nvlist_free(nv);
251019b94df9SMatthew Ahrens 		nvlist_free(value);
251119b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
251219b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
251319b94df9SMatthew Ahrens 	}
251419b94df9SMatthew Ahrens 
251519b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
251619b94df9SMatthew Ahrens 
251719b94df9SMatthew Ahrens 	return (value);
251819b94df9SMatthew Ahrens }
251919b94df9SMatthew Ahrens 
2520dfc11533SChris Williamson /*
2521dfc11533SChris Williamson  * Accepts a property and value and checks that the value
2522dfc11533SChris Williamson  * matches the one found by the channel program. If they are
2523dfc11533SChris Williamson  * not equal, print both of them.
2524dfc11533SChris Williamson  */
2525dfc11533SChris Williamson void
2526dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2527dfc11533SChris Williamson     const char *strval)
2528dfc11533SChris Williamson {
2529dfc11533SChris Williamson 	if (!zhp->zfs_hdl->libzfs_prop_debug)
2530dfc11533SChris Williamson 		return;
2531dfc11533SChris Williamson 	int error;
2532dfc11533SChris Williamson 	char *poolname = zhp->zpool_hdl->zpool_name;
2533dfc11533SChris Williamson 	const char *program =
2534dfc11533SChris Williamson 	    "args = ...\n"
2535dfc11533SChris Williamson 	    "ds = args['dataset']\n"
2536dfc11533SChris Williamson 	    "prop = args['property']\n"
2537dfc11533SChris Williamson 	    "value, setpoint = zfs.get_prop(ds, prop)\n"
2538dfc11533SChris Williamson 	    "return {value=value, setpoint=setpoint}\n";
2539dfc11533SChris Williamson 	nvlist_t *outnvl;
2540dfc11533SChris Williamson 	nvlist_t *retnvl;
2541dfc11533SChris Williamson 	nvlist_t *argnvl = fnvlist_alloc();
2542dfc11533SChris Williamson 
2543dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2544dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2545dfc11533SChris Williamson 
2546a3b28680SSerapheim Dimitropoulos 	error = lzc_channel_program_nosync(poolname, program,
2547dfc11533SChris Williamson 	    10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2548dfc11533SChris Williamson 
2549dfc11533SChris Williamson 	if (error == 0) {
2550dfc11533SChris Williamson 		retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2551dfc11533SChris Williamson 		if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2552dfc11533SChris Williamson 			int64_t ans;
2553dfc11533SChris Williamson 			error = nvlist_lookup_int64(retnvl, "value", &ans);
2554dfc11533SChris Williamson 			if (error != 0) {
2555dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2556dfc11533SChris Williamson 				    error);
2557dfc11533SChris Williamson 				return;
2558dfc11533SChris Williamson 			}
2559dfc11533SChris Williamson 			if (ans != intval) {
2560dfc11533SChris Williamson 				(void) fprintf(stderr,
2561dfc11533SChris Williamson 				    "%s: zfs found %lld, but zcp found %lld\n",
2562dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2563dfc11533SChris Williamson 				    (longlong_t)intval, (longlong_t)ans);
2564dfc11533SChris Williamson 			}
2565dfc11533SChris Williamson 		} else {
2566dfc11533SChris Williamson 			char *str_ans;
2567dfc11533SChris Williamson 			error = nvlist_lookup_string(retnvl, "value", &str_ans);
2568dfc11533SChris Williamson 			if (error != 0) {
2569dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2570dfc11533SChris Williamson 				    error);
2571dfc11533SChris Williamson 				return;
2572dfc11533SChris Williamson 			}
2573dfc11533SChris Williamson 			if (strcmp(strval, str_ans) != 0) {
2574dfc11533SChris Williamson 				(void) fprintf(stderr,
2575dfc11533SChris Williamson 				    "%s: zfs found %s, but zcp found %s\n",
2576dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2577dfc11533SChris Williamson 				    strval, str_ans);
2578dfc11533SChris Williamson 			}
2579dfc11533SChris Williamson 		}
2580dfc11533SChris Williamson 	} else {
2581dfc11533SChris Williamson 		(void) fprintf(stderr,
2582dfc11533SChris Williamson 		    "zcp check failed, channel program error: %u\n", error);
2583dfc11533SChris Williamson 	}
2584dfc11533SChris Williamson 	nvlist_free(argnvl);
2585dfc11533SChris Williamson 	nvlist_free(outnvl);
2586dfc11533SChris Williamson }
2587dfc11533SChris Williamson 
2588fa9e4066Sahrens /*
2589fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2590fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2591fa9e4066Sahrens  * human-readable form.
2592fa9e4066Sahrens  *
2593fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2594fa9e4066Sahrens  */
2595fa9e4066Sahrens int
2596fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2597990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2598fa9e4066Sahrens {
2599fa9e4066Sahrens 	char *source = NULL;
2600fa9e4066Sahrens 	uint64_t val;
26019c3fd121SMatthew Ahrens 	const char *str;
2602e9dbad6fSeschrock 	const char *strval;
260392241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2604fa9e4066Sahrens 
2605fa9e4066Sahrens 	/*
2606fa9e4066Sahrens 	 * Check to see if this property applies to our object
2607fa9e4066Sahrens 	 */
2608fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2609fa9e4066Sahrens 		return (-1);
2610fa9e4066Sahrens 
261192241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
261292241e0bSTom Erickson 		return (-1);
261392241e0bSTom Erickson 
2614fa9e4066Sahrens 	if (src)
2615990b4856Slling 		*src = ZPROP_SRC_NONE;
2616fa9e4066Sahrens 
2617fa9e4066Sahrens 	switch (prop) {
2618fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2619fa9e4066Sahrens 		/*
2620fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2621fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2622fa9e4066Sahrens 		 */
2623fa9e4066Sahrens 		{
2624a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2625a2eea2e1Sahrens 			time_t time = (time_t)val;
2626fa9e4066Sahrens 			struct tm t;
2627fa9e4066Sahrens 
2628fa9e4066Sahrens 			if (literal ||
2629fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2630fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2631fa9e4066Sahrens 			    &t) == 0)
2632a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2633fa9e4066Sahrens 		}
2634dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2635fa9e4066Sahrens 		break;
2636fa9e4066Sahrens 
2637fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2638fa9e4066Sahrens 		/*
2639fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2640fa9e4066Sahrens 		 *
2641fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2642fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2643fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2644fa9e4066Sahrens 		 *
2645fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2646fa9e4066Sahrens 		 * root to any values we return.
2647fa9e4066Sahrens 		 */
264829ab75c9Srm 
26497f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2650fa9e4066Sahrens 
2651b21718f0Sgw 		if (str[0] == '/') {
265229ab75c9Srm 			char buf[MAXPATHLEN];
265329ab75c9Srm 			char *root = buf;
2654a79992aaSTom Erickson 			const char *relpath;
2655fa9e4066Sahrens 
2656a79992aaSTom Erickson 			/*
2657a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2658a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2659a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2660a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2661a79992aaSTom Erickson 			 * inherited.
2662a79992aaSTom Erickson 			 */
2663a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2664a79992aaSTom Erickson 				relpath = "";
2665a79992aaSTom Erickson 			} else {
2666a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2667a79992aaSTom Erickson 				if (relpath[0] == '/')
2668a79992aaSTom Erickson 					relpath++;
2669a79992aaSTom Erickson 			}
2670b21718f0Sgw 
267129ab75c9Srm 			if ((zpool_get_prop(zhp->zpool_hdl,
2672c58b3526SAdam Stevko 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2673c58b3526SAdam Stevko 			    B_FALSE)) || (strcmp(root, "-") == 0))
267429ab75c9Srm 				root[0] = '\0';
2675b21718f0Sgw 			/*
2676b21718f0Sgw 			 * Special case an alternate root of '/'. This will
2677b21718f0Sgw 			 * avoid having multiple leading slashes in the
2678b21718f0Sgw 			 * mountpoint path.
2679b21718f0Sgw 			 */
2680b21718f0Sgw 			if (strcmp(root, "/") == 0)
2681b21718f0Sgw 				root++;
2682b21718f0Sgw 
2683b21718f0Sgw 			/*
2684b21718f0Sgw 			 * If the mountpoint is '/' then skip over this
2685b21718f0Sgw 			 * if we are obtaining either an alternate root or
2686b21718f0Sgw 			 * an inherited mountpoint.
2687b21718f0Sgw 			 */
2688b21718f0Sgw 			if (str[1] == '\0' && (root[0] != '\0' ||
2689b21718f0Sgw 			    relpath[0] != '\0'))
26907f7322feSeschrock 				str++;
2691fa9e4066Sahrens 
2692fa9e4066Sahrens 			if (relpath[0] == '\0')
2693fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
26947f7322feSeschrock 				    root, str);
2695fa9e4066Sahrens 			else
2696fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
26977f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2698fa9e4066Sahrens 				    relpath);
2699fa9e4066Sahrens 		} else {
2700fa9e4066Sahrens 			/* 'legacy' or 'none' */
27017f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2702fa9e4066Sahrens 		}
27035b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, propbuf);
2704fa9e4066Sahrens 		break;
2705fa9e4066Sahrens 
2706fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
27079c3fd121SMatthew Ahrens 		str = getprop_string(zhp, prop, &source);
27089c3fd121SMatthew Ahrens 		if (str == NULL)
2709fa9e4066Sahrens 			return (-1);
27109c3fd121SMatthew Ahrens 		(void) strlcpy(propbuf, str, proplen);
27115b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, str);
2712fa9e4066Sahrens 		break;
2713fa9e4066Sahrens 
271419b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
271519b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
271619b94df9SMatthew Ahrens 			return (-1);
271719b94df9SMatthew Ahrens 		break;
271819b94df9SMatthew Ahrens 
2719fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2720a9799022Sck 	case ZFS_PROP_REFQUOTA:
2721fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2722a9799022Sck 	case ZFS_PROP_REFRESERVATION:
2723a9799022Sck 
272499653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
272599653d4eSeschrock 			return (-1);
2726fa9e4066Sahrens 		/*
2727fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2728fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2729fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2730fa9e4066Sahrens 		 * that its set locally.
2731fa9e4066Sahrens 		 */
2732fa9e4066Sahrens 		if (val == 0) {
2733fa9e4066Sahrens 			if (literal)
2734fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2735fa9e4066Sahrens 			else
2736fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2737fa9e4066Sahrens 		} else {
2738fa9e4066Sahrens 			if (literal)
27395ad82045Snd 				(void) snprintf(propbuf, proplen, "%llu",
2740b1b8ab34Slling 				    (u_longlong_t)val);
2741fa9e4066Sahrens 			else
2742fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2743fa9e4066Sahrens 		}
2744dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2745fa9e4066Sahrens 		break;
2746fa9e4066Sahrens 
2747a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2748a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2749a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2750a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2751a2afb611SJerry Jelinek 
2752a2afb611SJerry Jelinek 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2753a2afb611SJerry Jelinek 			return (-1);
2754a2afb611SJerry Jelinek 
2755a2afb611SJerry Jelinek 		/*
2756a2afb611SJerry Jelinek 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2757a2afb611SJerry Jelinek 		 * literal is set), and indicate that it's the default value.
2758a2afb611SJerry Jelinek 		 * Otherwise, we print the number nicely and indicate that it's
2759a2afb611SJerry Jelinek 		 * set locally.
2760a2afb611SJerry Jelinek 		 */
2761a2afb611SJerry Jelinek 		if (literal) {
2762a2afb611SJerry Jelinek 			(void) snprintf(propbuf, proplen, "%llu",
2763a2afb611SJerry Jelinek 			    (u_longlong_t)val);
2764a2afb611SJerry Jelinek 		} else if (val == UINT64_MAX) {
2765a2afb611SJerry Jelinek 			(void) strlcpy(propbuf, "none", proplen);
2766a2afb611SJerry Jelinek 		} else {
2767a2afb611SJerry Jelinek 			zfs_nicenum(val, propbuf, proplen);
2768a2afb611SJerry Jelinek 		}
2769dfc11533SChris Williamson 
2770dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2771a2afb611SJerry Jelinek 		break;
2772a2afb611SJerry Jelinek 
2773187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2774fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
277599653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
277699653d4eSeschrock 			return (-1);
2777b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2778b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2779b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2780dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2781fa9e4066Sahrens 		break;
2782fa9e4066Sahrens 
2783fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2784fa9e4066Sahrens 		switch (zhp->zfs_type) {
2785fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2786fa9e4066Sahrens 			str = "filesystem";
2787fa9e4066Sahrens 			break;
2788fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2789fa9e4066Sahrens 			str = "volume";
2790fa9e4066Sahrens 			break;
2791fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2792fa9e4066Sahrens 			str = "snapshot";
2793fa9e4066Sahrens 			break;
279478f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
279578f17100SMatthew Ahrens 			str = "bookmark";
279678f17100SMatthew Ahrens 			break;
2797fa9e4066Sahrens 		default:
279899653d4eSeschrock 			abort();
2799fa9e4066Sahrens 		}
2800fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
28015b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, propbuf);
2802fa9e4066Sahrens 		break;
2803fa9e4066Sahrens 
2804fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2805fa9e4066Sahrens 		/*
2806fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2807fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2808fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2809fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2810fa9e4066Sahrens 		 */
281199653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
281299653d4eSeschrock 		    src, &source, &val) != 0)
281399653d4eSeschrock 			return (-1);
281499653d4eSeschrock 		if (val)
2815fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2816fa9e4066Sahrens 		else
2817fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2818fa9e4066Sahrens 		break;
2819fa9e4066Sahrens 
2820fa9e4066Sahrens 	case ZFS_PROP_NAME:
2821fa9e4066Sahrens 		/*
2822fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2823fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2824fa9e4066Sahrens 		 * consumers.
2825fa9e4066Sahrens 		 */
2826fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
28275b8cbb8eSToomas Soome 		zcp_check(zhp, prop, 0, propbuf);
2828fa9e4066Sahrens 		break;
2829fa9e4066Sahrens 
28304201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
28314201a95eSRic Aleshire 		{
28324201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
28334201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
28344201a95eSRic Aleshire 
28354201a95eSRic Aleshire 			(void) strlcpy(propbuf,
28364201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
28374201a95eSRic Aleshire 
28384201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
28394201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
28404201a95eSRic Aleshire 				break;
28414201a95eSRic Aleshire 
28424201a95eSRic Aleshire 			/*
28434201a95eSRic Aleshire 			 * Try to translate the internal hex string to
28444201a95eSRic Aleshire 			 * human-readable output.  If there are any
28454201a95eSRic Aleshire 			 * problems just use the hex string.
28464201a95eSRic Aleshire 			 */
28474201a95eSRic Aleshire 
28484201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
28494201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
28504201a95eSRic Aleshire 				m_label_free(new_sl);
28514201a95eSRic Aleshire 				break;
28524201a95eSRic Aleshire 			}
28534201a95eSRic Aleshire 
28544201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
28554201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
28564201a95eSRic Aleshire 				if (ascii)
28574201a95eSRic Aleshire 					free(ascii);
28584201a95eSRic Aleshire 				m_label_free(new_sl);
28594201a95eSRic Aleshire 				break;
28604201a95eSRic Aleshire 			}
28614201a95eSRic Aleshire 			m_label_free(new_sl);
28624201a95eSRic Aleshire 
28634201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
28644201a95eSRic Aleshire 			free(ascii);
28654201a95eSRic Aleshire 		}
28664201a95eSRic Aleshire 		break;
28674201a95eSRic Aleshire 
2868f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2869e8d4a73cSJosh Paetzel 	case ZFS_PROP_CREATETXG:
2870f0f3ef5aSGarrett D'Amore 		/*
2871f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2872f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2873f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2874f0f3ef5aSGarrett D'Amore 		 */
2875f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2876f0f3ef5aSGarrett D'Amore 			return (-1);
2877f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2878dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2879f0f3ef5aSGarrett D'Amore 		break;
2880f0f3ef5aSGarrett D'Amore 
2881fa9e4066Sahrens 	default:
2882e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
288391ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2884e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2885dfc11533SChris Williamson 			    &source, &val) != 0) {
2886e7437265Sahrens 				return (-1);
2887dfc11533SChris Williamson 			}
2888dfc11533SChris Williamson 
2889dfc11533SChris Williamson 			if (literal) {
2890e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2891e7437265Sahrens 				    (u_longlong_t)val);
2892dfc11533SChris Williamson 			} else {
2893e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2894dfc11533SChris Williamson 			}
2895dfc11533SChris Williamson 			zcp_check(zhp, prop, val, NULL);
2896e7437265Sahrens 			break;
2897e7437265Sahrens 
289891ebeef5Sahrens 		case PROP_TYPE_STRING:
28999c3fd121SMatthew Ahrens 			str = getprop_string(zhp, prop, &source);
29009c3fd121SMatthew Ahrens 			if (str == NULL)
29019c3fd121SMatthew Ahrens 				return (-1);
2902dfc11533SChris Williamson 
29039c3fd121SMatthew Ahrens 			(void) strlcpy(propbuf, str, proplen);
29045b8cbb8eSToomas Soome 			zcp_check(zhp, prop, 0, str);
2905e7437265Sahrens 			break;
2906e7437265Sahrens 
290791ebeef5Sahrens 		case PROP_TYPE_INDEX:
2908fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2909fe192f76Sahrens 			    &source, &val) != 0)
2910fe192f76Sahrens 				return (-1);
2911fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2912e7437265Sahrens 				return (-1);
2913dfc11533SChris Williamson 
2914e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
29155b8cbb8eSToomas Soome 			zcp_check(zhp, prop, 0, strval);
2916e7437265Sahrens 			break;
2917e7437265Sahrens 
2918e7437265Sahrens 		default:
2919e7437265Sahrens 			abort();
2920e7437265Sahrens 		}
2921fa9e4066Sahrens 	}
2922fa9e4066Sahrens 
2923fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2924fa9e4066Sahrens 
2925fa9e4066Sahrens 	return (0);
2926fa9e4066Sahrens }
2927fa9e4066Sahrens 
2928fa9e4066Sahrens /*
2929fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2930fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2931fa9e4066Sahrens  * hard-coded property types.
2932fa9e4066Sahrens  */
2933fa9e4066Sahrens uint64_t
2934fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2935fa9e4066Sahrens {
2936fa9e4066Sahrens 	char *source;
293799653d4eSeschrock 	uint64_t val;
293899653d4eSeschrock 
29393cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2940fa9e4066Sahrens 
294199653d4eSeschrock 	return (val);
2942fa9e4066Sahrens }
2943fa9e4066Sahrens 
29447b97dc1aSrm int
29457b97dc1aSrm zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
29467b97dc1aSrm {
29477b97dc1aSrm 	char buf[64];
29487b97dc1aSrm 
294914843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
29507b97dc1aSrm 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
29517b97dc1aSrm }
29527b97dc1aSrm 
2953fa9e4066Sahrens /*
2954fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2955fa9e4066Sahrens  */
2956fa9e4066Sahrens int
2957fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2958990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2959fa9e4066Sahrens {
2960fa9e4066Sahrens 	char *source;
2961fa9e4066Sahrens 
2962fa9e4066Sahrens 	/*
2963fa9e4066Sahrens 	 * Check to see if this property applies to our object
2964fa9e4066Sahrens 	 */
2965e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2966ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
296799653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
296899653d4eSeschrock 		    zfs_prop_to_name(prop)));
2969e45ce728Sahrens 	}
2970fa9e4066Sahrens 
2971fa9e4066Sahrens 	if (src)
2972990b4856Slling 		*src = ZPROP_SRC_NONE;
2973fa9e4066Sahrens 
297499653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
297599653d4eSeschrock 		return (-1);
2976fa9e4066Sahrens 
2977fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2978fa9e4066Sahrens 
2979fa9e4066Sahrens 	return (0);
2980fa9e4066Sahrens }
2981fa9e4066Sahrens 
298214843421SMatthew Ahrens static int
298314843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
298414843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
298514843421SMatthew Ahrens {
298614843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
298714843421SMatthew Ahrens 	idmap_stat status;
298814843421SMatthew Ahrens 	int err = EINVAL;
298914843421SMatthew Ahrens 
29901fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
299114843421SMatthew Ahrens 		goto out;
299214843421SMatthew Ahrens 
299314843421SMatthew Ahrens 	if (isuser) {
299414843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
299514843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
299614843421SMatthew Ahrens 	} else {
299714843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
299814843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
299914843421SMatthew Ahrens 	}
300014843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
300114843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
300214843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
300314843421SMatthew Ahrens 		err = 0;
300414843421SMatthew Ahrens 	else
300514843421SMatthew Ahrens 		err = EINVAL;
300614843421SMatthew Ahrens out:
300714843421SMatthew Ahrens 	if (get_hdl)
300814843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
300914843421SMatthew Ahrens 	return (err);
301014843421SMatthew Ahrens }
301114843421SMatthew Ahrens 
301214843421SMatthew Ahrens /*
301314843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
301414843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
301514843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3016f67950b2SNasf-Fan  * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3017f67950b2SNasf-Fan  * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3018f67950b2SNasf-Fan  * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3019f67950b2SNasf-Fan  * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
302014843421SMatthew Ahrens  */
302114843421SMatthew Ahrens static int
302214843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
302314843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
302414843421SMatthew Ahrens {
302514843421SMatthew Ahrens 	zfs_userquota_prop_t type;
3026f67950b2SNasf-Fan 	char *cp;
302714843421SMatthew Ahrens 	boolean_t isuser;
3028f67950b2SNasf-Fan 	boolean_t isgroup;
3029f67950b2SNasf-Fan 	boolean_t isproject;
3030f67950b2SNasf-Fan 	struct passwd *pw;
3031f67950b2SNasf-Fan 	struct group *gr;
303214843421SMatthew Ahrens 
303314843421SMatthew Ahrens 	domain[0] = '\0';
3034f67950b2SNasf-Fan 
3035f67950b2SNasf-Fan 	/* Figure out the property type ({user|group|project}{quota|space}) */
303614843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
303714843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
303814843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
303914843421SMatthew Ahrens 			break;
304014843421SMatthew Ahrens 	}
304114843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
304214843421SMatthew Ahrens 		return (EINVAL);
304314843421SMatthew Ahrens 	*typep = type;
304414843421SMatthew Ahrens 
3045f67950b2SNasf-Fan 	isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3046f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJQUOTA ||
3047f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJUSED);
3048f67950b2SNasf-Fan 	isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3049f67950b2SNasf-Fan 	    type == ZFS_PROP_GROUPOBJQUOTA ||
3050f67950b2SNasf-Fan 	    type == ZFS_PROP_GROUPOBJUSED);
3051f67950b2SNasf-Fan 	isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3052f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3053f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED);
305414843421SMatthew Ahrens 
305514843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
305614843421SMatthew Ahrens 
3057f67950b2SNasf-Fan 	if (isuser && (pw = getpwnam(cp)) != NULL) {
3058f67950b2SNasf-Fan 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3059f67950b2SNasf-Fan 			return (ENOENT);
3060f67950b2SNasf-Fan 		*ridp = pw->pw_uid;
3061f67950b2SNasf-Fan 	} else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3062f67950b2SNasf-Fan 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3063f67950b2SNasf-Fan 			return (ENOENT);
3064f67950b2SNasf-Fan 		*ridp = gr->gr_gid;
3065f67950b2SNasf-Fan 	} else if (!isproject && strchr(cp, '@')) {
306614843421SMatthew Ahrens 		/*
306714843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
30683b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
306914843421SMatthew Ahrens 		 */
3070f67950b2SNasf-Fan 		directory_error_t e;
3071f67950b2SNasf-Fan 		char *numericsid = NULL;
3072f67950b2SNasf-Fan 		char *end;
3073f67950b2SNasf-Fan 
307414843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
307514843421SMatthew Ahrens 			return (ENOENT);
30763b12c289SMatthew Ahrens 		if (isuser) {
3077f67950b2SNasf-Fan 			e = directory_sid_from_user_name(NULL,
3078f67950b2SNasf-Fan 			    cp, &numericsid);
30793b12c289SMatthew Ahrens 		} else {
3080f67950b2SNasf-Fan 			e = directory_sid_from_group_name(NULL,
3081f67950b2SNasf-Fan 			    cp, &numericsid);
30821ed6b69aSGordon Ross 		}
3083f67950b2SNasf-Fan 		if (e != NULL) {
3084f67950b2SNasf-Fan 			directory_error_free(e);
308514843421SMatthew Ahrens 			return (ENOENT);
30863b12c289SMatthew Ahrens 		}
30873b12c289SMatthew Ahrens 		if (numericsid == NULL)
308814843421SMatthew Ahrens 			return (ENOENT);
30893b12c289SMatthew Ahrens 		cp = numericsid;
30903b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
3091f67950b2SNasf-Fan 		cp = strrchr(domain, '-');
3092f67950b2SNasf-Fan 		*cp = '\0';
3093f67950b2SNasf-Fan 		cp++;
3094f67950b2SNasf-Fan 
309514843421SMatthew Ahrens 		errno = 0;
3096f67950b2SNasf-Fan 		*ridp = strtoull(cp, &end, 10);
3097f67950b2SNasf-Fan 		free(numericsid);
3098f67950b2SNasf-Fan 
3099777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
310014843421SMatthew Ahrens 			return (EINVAL);
310114843421SMatthew Ahrens 	} else {
3102f67950b2SNasf-Fan 		/* It's a user/group/project ID (eg "12345"). */
3103f67950b2SNasf-Fan 		char *end;
310414843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
310514843421SMatthew Ahrens 		if (*end != '\0')
310614843421SMatthew Ahrens 			return (EINVAL);
3107f67950b2SNasf-Fan 		if (id > MAXUID && !isproject) {
310814843421SMatthew Ahrens 			/* It's an ephemeral ID. */
3109f67950b2SNasf-Fan 			idmap_rid_t rid;
3110f67950b2SNasf-Fan 			char *mapdomain;
3111f67950b2SNasf-Fan 
311214843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
311314843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
311414843421SMatthew Ahrens 				return (ENOENT);
31153b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
311614843421SMatthew Ahrens 			*ridp = rid;
311714843421SMatthew Ahrens 		} else {
311814843421SMatthew Ahrens 			*ridp = id;
311914843421SMatthew Ahrens 		}
312014843421SMatthew Ahrens 	}
312114843421SMatthew Ahrens 
312214843421SMatthew Ahrens 	return (0);
312314843421SMatthew Ahrens }
312414843421SMatthew Ahrens 
3125edea4b55SLin Ling static int
3126edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3127edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
312814843421SMatthew Ahrens {
312914843421SMatthew Ahrens 	int err;
313014843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
313114843421SMatthew Ahrens 
313219b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
313314843421SMatthew Ahrens 
313414843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
313514843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3136edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3137edea4b55SLin Ling 	zc.zc_objset_type = *typep;
313814843421SMatthew Ahrens 	if (err)
313914843421SMatthew Ahrens 		return (err);
314014843421SMatthew Ahrens 
314114843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
314214843421SMatthew Ahrens 	if (err)
314314843421SMatthew Ahrens 		return (err);
314414843421SMatthew Ahrens 
3145edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
3146edea4b55SLin Ling 	return (0);
3147edea4b55SLin Ling }
3148edea4b55SLin Ling 
3149edea4b55SLin Ling int
3150edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3151edea4b55SLin Ling     uint64_t *propvalue)
3152edea4b55SLin Ling {
3153edea4b55SLin Ling 	zfs_userquota_prop_t type;
3154edea4b55SLin Ling 
3155edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3156edea4b55SLin Ling 	    &type));
3157edea4b55SLin Ling }
3158edea4b55SLin Ling 
3159edea4b55SLin Ling int
3160edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3161edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
3162edea4b55SLin Ling {
3163edea4b55SLin Ling 	int err;
3164edea4b55SLin Ling 	uint64_t propvalue;
3165edea4b55SLin Ling 	zfs_userquota_prop_t type;
3166edea4b55SLin Ling 
3167edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3168edea4b55SLin Ling 	    &type);
3169edea4b55SLin Ling 
3170edea4b55SLin Ling 	if (err)
3171edea4b55SLin Ling 		return (err);
3172edea4b55SLin Ling 
317314843421SMatthew Ahrens 	if (literal) {
3174edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3175edea4b55SLin Ling 	} else if (propvalue == 0 &&
3176f67950b2SNasf-Fan 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3177f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3178f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTQUOTA || ZFS_PROP_PROJECTOBJQUOTA)) {
317914843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
3180f67950b2SNasf-Fan 	} else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3181f67950b2SNasf-Fan 	    type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3182f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3183f67950b2SNasf-Fan 		zfs_nicenum(propvalue, propbuf, proplen);
318414843421SMatthew Ahrens 	} else {
3185edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
318614843421SMatthew Ahrens 	}
318714843421SMatthew Ahrens 	return (0);
318814843421SMatthew Ahrens }
318914843421SMatthew Ahrens 
319019b94df9SMatthew Ahrens int
319119b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
319219b94df9SMatthew Ahrens     uint64_t *propvalue)
31938ac09fceSRichard Lowe {
319419b94df9SMatthew Ahrens 	int err;
319519b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
319619b94df9SMatthew Ahrens 	const char *snapname;
3197ebedde84SEric Taylor 
319819b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3199e5351341SMatthew Ahrens 
320019b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
320119b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
320219b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
320319b94df9SMatthew Ahrens 	} else {
320419b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
320519b94df9SMatthew Ahrens 		char *cp;
3206e5351341SMatthew Ahrens 
320719b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
320819b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
320919b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
321019b94df9SMatthew Ahrens 		if (cp != NULL)
321119b94df9SMatthew Ahrens 			*cp = '\0';
321219b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
321319b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
32148ac09fceSRichard Lowe 	}
321519b94df9SMatthew Ahrens 
321619b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
321719b94df9SMatthew Ahrens 	if (err)
321819b94df9SMatthew Ahrens 		return (err);
321919b94df9SMatthew Ahrens 
322019b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
322119b94df9SMatthew Ahrens 	return (0);
3222ebedde84SEric Taylor }
3223ebedde84SEric Taylor 
3224fa9e4066Sahrens int
322519b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
322619b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
3227fa9e4066Sahrens {
322819b94df9SMatthew Ahrens 	int err;
322919b94df9SMatthew Ahrens 	uint64_t propvalue;
32303cb34c60Sahrens 
323119b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3232ebedde84SEric Taylor 
323319b94df9SMatthew Ahrens 	if (err)
323419b94df9SMatthew Ahrens 		return (err);
32358ac09fceSRichard Lowe 
323619b94df9SMatthew Ahrens 	if (literal) {
323719b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
323819b94df9SMatthew Ahrens 	} else {
323919b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
3240fa9e4066Sahrens 	}
324119b94df9SMatthew Ahrens 	return (0);
32427f7322feSeschrock }
32437f7322feSeschrock 
32447f7322feSeschrock /*
324519b94df9SMatthew Ahrens  * Returns the name of the given zfs handle.
32467f7322feSeschrock  */
324719b94df9SMatthew Ahrens const char *
324819b94df9SMatthew Ahrens zfs_get_name(const zfs_handle_t *zhp)
32497f7322feSeschrock {
325019b94df9SMatthew Ahrens 	return (zhp->zfs_name);
325119b94df9SMatthew Ahrens }
32528ac09fceSRichard Lowe 
32538808ac5dSYuri Pankov /*
32548808ac5dSYuri Pankov  * Returns the name of the parent pool for the given zfs handle.
32558808ac5dSYuri Pankov  */
32568808ac5dSYuri Pankov const char *
32578808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp)
32588808ac5dSYuri Pankov {
32598808ac5dSYuri Pankov 	return (zhp->zpool_hdl->zpool_name);
32608808ac5dSYuri Pankov }
32618808ac5dSYuri Pankov 
326219b94df9SMatthew Ahrens /*
326319b94df9SMatthew Ahrens  * Returns the type of the given zfs handle.
326419b94df9SMatthew Ahrens  */
326519b94df9SMatthew Ahrens zfs_type_t
326619b94df9SMatthew Ahrens zfs_get_type(const zfs_handle_t *zhp)
326719b94df9SMatthew Ahrens {
326819b94df9SMatthew Ahrens 	return (zhp->zfs_type);
32697f7322feSeschrock }
32707f7322feSeschrock 
3271d41c4376SMark J Musante /*
3272d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
3273d41c4376SMark J Musante  *
3274d41c4376SMark J Musante  * Needs to handle these cases:
3275d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3276d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3277d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
3278d41c4376SMark J Musante  */
3279d41c4376SMark J Musante static boolean_t
3280d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
3281d41c4376SMark J Musante {
3282d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
3283d41c4376SMark J Musante 
3284d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
3285d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
3286d41c4376SMark J Musante 		return (B_FALSE);
3287d41c4376SMark J Musante 
3288d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
3289d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3290d41c4376SMark J Musante }
3291d41c4376SMark J Musante 
3292fa9e4066Sahrens /*
3293fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
329419b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
329519b94df9SMatthew Ahrens  * pool).
3296fa9e4066Sahrens  */
3297fa9e4066Sahrens static int
3298fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3299fa9e4066Sahrens {
330019b94df9SMatthew Ahrens 	char *slashp;
3301fa9e4066Sahrens 
330219b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
3303fa9e4066Sahrens 
330419b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
330519b94df9SMatthew Ahrens 		return (-1);
330619b94df9SMatthew Ahrens 	*slashp = '\0';
3307fa9e4066Sahrens 
3308fa9e4066Sahrens 	return (0);
3309fa9e4066Sahrens }
3310fa9e4066Sahrens 
3311eb633035STom Caputi int
3312eb633035STom Caputi zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3313eb633035STom Caputi {
3314eb633035STom Caputi 	return (parent_name(zfs_get_name(zhp), buf, buflen));
3315eb633035STom Caputi }
3316eb633035STom Caputi 
3317fa9e4066Sahrens /*
33187f1f55eaSvb  * If accept_ancestor is false, then check to make sure that the given path has
33197f1f55eaSvb  * a parent, and that it exists.  If accept_ancestor is true, then find the
33207f1f55eaSvb  * closest existing ancestor for the given path.  In prefixlen return the
33217f1f55eaSvb  * length of already existing prefix of the given path.  We also fetch the
33227f1f55eaSvb  * 'zoned' property, which is used to validate property settings when creating
33237f1f55eaSvb  * new datasets.
3324fa9e4066Sahrens  */
3325fa9e4066Sahrens static int
33267f1f55eaSvb check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
33277f1f55eaSvb     boolean_t accept_ancestor, int *prefixlen)
3328fa9e4066Sahrens {
3329fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
33309adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3331fa9e4066Sahrens 	char *slash;
33327f7322feSeschrock 	zfs_handle_t *zhp;
333399653d4eSeschrock 	char errbuf[1024];
3334d41c4376SMark J Musante 	uint64_t is_zoned;
333599653d4eSeschrock 
3336deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
3337deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3338fa9e4066Sahrens 
3339fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
3340fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
334199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
334299653d4eSeschrock 		    "missing dataset name"));
334399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3344fa9e4066Sahrens 	}
3345fa9e4066Sahrens 
3346fa9e4066Sahrens 	/* check to see if the pool exists */
3347fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
3348fa9e4066Sahrens 		slash = parent + strlen(parent);
33498ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
3350fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
335199653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3352fa9e4066Sahrens 	    errno == ENOENT) {
335399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
335499653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
335599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3356fa9e4066Sahrens 	}
3357fa9e4066Sahrens 
3358fa9e4066Sahrens 	/* check to see if the parent dataset exists */
33597f1f55eaSvb 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
33607f1f55eaSvb 		if (errno == ENOENT && accept_ancestor) {
33617f1f55eaSvb 			/*
33627f1f55eaSvb 			 * Go deeper to find an ancestor, give up on top level.
33637f1f55eaSvb 			 */
33647f1f55eaSvb 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
33657f1f55eaSvb 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33667f1f55eaSvb 				    "no such pool '%s'"), zc.zc_name);
33677f1f55eaSvb 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
33687f1f55eaSvb 			}
33697f1f55eaSvb 		} else if (errno == ENOENT) {
337099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
337199653d4eSeschrock 			    "parent does not exist"));
337299653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
33737f1f55eaSvb 		} else
337499653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3375fa9e4066Sahrens 	}
3376fa9e4066Sahrens 
3377d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3378d41c4376SMark J Musante 	if (zoned != NULL)
3379d41c4376SMark J Musante 		*zoned = is_zoned;
3380d41c4376SMark J Musante 
3381fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
3382d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
338399653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
33847f7322feSeschrock 		zfs_close(zhp);
3385fa9e4066Sahrens 		return (-1);
3386fa9e4066Sahrens 	}
3387fa9e4066Sahrens 
3388fa9e4066Sahrens 	/* make sure parent is a filesystem */
33897f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
339099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
339199653d4eSeschrock 		    "parent is not a filesystem"));
339299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
33937f7322feSeschrock 		zfs_close(zhp);
3394fa9e4066Sahrens 		return (-1);
3395fa9e4066Sahrens 	}
3396fa9e4066Sahrens 
33977f7322feSeschrock 	zfs_close(zhp);
33987f1f55eaSvb 	if (prefixlen != NULL)
33997f1f55eaSvb 		*prefixlen = strlen(parent);
34007f1f55eaSvb 	return (0);
34017f1f55eaSvb }
34027f1f55eaSvb 
34037f1f55eaSvb /*
34047f1f55eaSvb  * Finds whether the dataset of the given type(s) exists.
34057f1f55eaSvb  */
34067f1f55eaSvb boolean_t
34077f1f55eaSvb zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
34087f1f55eaSvb {
34097f1f55eaSvb 	zfs_handle_t *zhp;
34107f1f55eaSvb 
3411f18faf3fSek 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
34127f1f55eaSvb 		return (B_FALSE);
34137f1f55eaSvb 
34147f1f55eaSvb 	/*
34157f1f55eaSvb 	 * Try to get stats for the dataset, which will tell us if it exists.
34167f1f55eaSvb 	 */
34177f1f55eaSvb 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
34187f1f55eaSvb 		int ds_type = zhp->zfs_type;
34197f1f55eaSvb 
34207f1f55eaSvb 		zfs_close(zhp);
34217f1f55eaSvb 		if (types & ds_type)
34227f1f55eaSvb 			return (B_TRUE);
34237f1f55eaSvb 	}
34247f1f55eaSvb 	return (B_FALSE);
34257f1f55eaSvb }
34267f1f55eaSvb 
34273cb34c60Sahrens /*
34283cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
34293cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
34303cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
34313cb34c60Sahrens  */
34323cb34c60Sahrens int
34333cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
34343cb34c60Sahrens {
34353cb34c60Sahrens 	zfs_handle_t *h;
34363cb34c60Sahrens 	char *cp;
34373cb34c60Sahrens 	const char *opname;
34383cb34c60Sahrens 
34393cb34c60Sahrens 	/* make sure prefix exists */
34403cb34c60Sahrens 	cp = target + prefixlen;
34413cb34c60Sahrens 	if (*cp != '/') {
34423cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
34433cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34443cb34c60Sahrens 	} else {
34453cb34c60Sahrens 		*cp = '\0';
34463cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34473cb34c60Sahrens 		*cp = '/';
34483cb34c60Sahrens 	}
34493cb34c60Sahrens 	if (h == NULL)
34503cb34c60Sahrens 		return (-1);
34513cb34c60Sahrens 	zfs_close(h);
34523cb34c60Sahrens 
34533cb34c60Sahrens 	/*
34543cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
34553cb34c60Sahrens 	 * up to the prefixlen-long one.
34563cb34c60Sahrens 	 */
34573cb34c60Sahrens 	for (cp = target + prefixlen + 1;
345888f61deeSIgor Kozhukhov 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
34593cb34c60Sahrens 
34603cb34c60Sahrens 		*cp = '\0';
34613cb34c60Sahrens 
34623cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
34633cb34c60Sahrens 		if (h) {
34643cb34c60Sahrens 			/* it already exists, nothing to do here */
34653cb34c60Sahrens 			zfs_close(h);
34663cb34c60Sahrens 			continue;
34673cb34c60Sahrens 		}
34683cb34c60Sahrens 
34693cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
34703cb34c60Sahrens 		    NULL) != 0) {
34713cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
34723cb34c60Sahrens 			goto ancestorerr;
34733cb34c60Sahrens 		}
34743cb34c60Sahrens 
34753cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34763cb34c60Sahrens 		if (h == NULL) {
34773cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
34783cb34c60Sahrens 			goto ancestorerr;
34793cb34c60Sahrens 		}
34803cb34c60Sahrens 
34813cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
34823cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
34833cb34c60Sahrens 			goto ancestorerr;
34843cb34c60Sahrens 		}
34853cb34c60Sahrens 
34863cb34c60Sahrens 		if (zfs_share(h) != 0) {
34873cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
34883cb34c60Sahrens 			goto ancestorerr;
34893cb34c60Sahrens 		}
34903cb34c60Sahrens 
34913cb34c60Sahrens 		zfs_close(h);
34923cb34c60Sahrens 	}
34933cb34c60Sahrens 
34943cb34c60Sahrens 	return (0);
34953cb34c60Sahrens 
34963cb34c60Sahrens ancestorerr:
34973cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34983cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
34993cb34c60Sahrens 	return (-1);
35003cb34c60Sahrens }
35013cb34c60Sahrens 
35027f1f55eaSvb /*
35037f1f55eaSvb  * Creates non-existing ancestors of the given path.
35047f1f55eaSvb  */
35057f1f55eaSvb int
35067f1f55eaSvb zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
35077f1f55eaSvb {
35087f1f55eaSvb 	int prefix;
35097f1f55eaSvb 	char *path_copy;
35105ac95da7SSerapheim Dimitropoulos 	char errbuf[1024];
3511f83b46baSPaul Dagnelie 	int rc = 0;
35127f1f55eaSvb 
35135ac95da7SSerapheim Dimitropoulos 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35145ac95da7SSerapheim Dimitropoulos 	    "cannot create '%s'"), path);
35155ac95da7SSerapheim Dimitropoulos 
35165ac95da7SSerapheim Dimitropoulos 	/*
35175ac95da7SSerapheim Dimitropoulos 	 * Check that we are not passing the nesting limit
35185ac95da7SSerapheim Dimitropoulos 	 * before we start creating any ancestors.
35195ac95da7SSerapheim Dimitropoulos 	 */
35205ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(path) != 0) {
35215ac95da7SSerapheim Dimitropoulos 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35225ac95da7SSerapheim Dimitropoulos 		    "maximum name nesting depth exceeded"));
35235ac95da7SSerapheim Dimitropoulos 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35245ac95da7SSerapheim Dimitropoulos 	}
35255ac95da7SSerapheim Dimitropoulos 
3526d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
35277f1f55eaSvb 		return (-1);
35287f1f55eaSvb 
35297f1f55eaSvb 	if ((path_copy = strdup(path)) != NULL) {
35307f1f55eaSvb 		rc = create_parents(hdl, path_copy, prefix);
35317f1f55eaSvb 		free(path_copy);
35327f1f55eaSvb 	}
35337f1f55eaSvb 	if (path_copy == NULL || rc != 0)
35347f1f55eaSvb 		return (-1);
35357f1f55eaSvb 
3536fa9e4066Sahrens 	return (0);
3537fa9e4066Sahrens }
3538fa9e4066Sahrens 
3539fa9e4066Sahrens /*
3540e9dbad6fSeschrock  * Create a new filesystem or volume.
3541fa9e4066Sahrens  */
3542fa9e4066Sahrens int
354399653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3544e9dbad6fSeschrock     nvlist_t *props)
3545fa9e4066Sahrens {
3546fa9e4066Sahrens 	int ret;
3547fa9e4066Sahrens 	uint64_t size = 0;
3548fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3549eb633035STom Caputi 	uint8_t *wkeydata = NULL;
3550eb633035STom Caputi 	uint_t wkeylen = 0;
355199653d4eSeschrock 	char errbuf[1024];
3552eb633035STom Caputi 	char parent[MAXNAMELEN];
3553e9dbad6fSeschrock 	uint64_t zoned;
355426455f9eSAndriy Gapon 	enum lzc_dataset_type ost;
3555690031d3Sloli 	zpool_handle_t *zpool_handle;
355699653d4eSeschrock 
355799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
355899653d4eSeschrock 	    "cannot create '%s'"), path);
3559fa9e4066Sahrens 
3560fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3561f18faf3fSek 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
356299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3563fa9e4066Sahrens 
35645ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(path) != 0) {
35655ac95da7SSerapheim Dimitropoulos 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35665ac95da7SSerapheim Dimitropoulos 		    "maximum name nesting depth exceeded"));
35675ac95da7SSerapheim Dimitropoulos 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35685ac95da7SSerapheim Dimitropoulos 	}
35695ac95da7SSerapheim Dimitropoulos 
3570fa9e4066Sahrens 	/* validate parents exist */
35717f1f55eaSvb 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3572fa9e4066Sahrens 		return (-1);
3573fa9e4066Sahrens 
3574fa9e4066Sahrens 	/*
3575fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3576fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3577fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3578fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3579fa9e4066Sahrens 	 * first try to see if the dataset exists.
3580fa9e4066Sahrens 	 */
35814445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
358299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358399653d4eSeschrock 		    "dataset already exists"));
358499653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3585fa9e4066Sahrens 	}
3586fa9e4066Sahrens 
3587fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
358826455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZVOL;
3589fa9e4066Sahrens 	else
359026455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZFS;
3591fa9e4066Sahrens 
3592e9316f76SJoe Stein 	/* open zpool handle for prop validation */
35939adfa60dSMatthew Ahrens 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3594e9316f76SJoe Stein 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3595e9316f76SJoe Stein 
3596e9316f76SJoe Stein 	/* truncate pool_path at first slash */
3597e9316f76SJoe Stein 	char *p = strchr(pool_path, '/');
3598e9316f76SJoe Stein 	if (p != NULL)
3599e9316f76SJoe Stein 		*p = '\0';
3600e9316f76SJoe Stein 
3601690031d3Sloli 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3602690031d3Sloli 		return (-1);
3603e9316f76SJoe Stein 
36040a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3605eb633035STom Caputi 	    zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3606e9316f76SJoe Stein 		zpool_close(zpool_handle);
3607e9dbad6fSeschrock 		return (-1);
3608e9316f76SJoe Stein 	}
3609e9316f76SJoe Stein 	zpool_close(zpool_handle);
3610e9dbad6fSeschrock 
3611fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
36125c5460e9Seschrock 		/*
36135c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
36145c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
36155c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
36165c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
36175c5460e9Seschrock 		 * zero.
36185c5460e9Seschrock 		 */
3619e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3620e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3621e9dbad6fSeschrock 			nvlist_free(props);
362299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3623e9dbad6fSeschrock 			    "missing volume size"));
3624e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3625e9dbad6fSeschrock 		}
3626e9dbad6fSeschrock 
3627e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3628e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3629e9dbad6fSeschrock 		    &blocksize)) != 0) {
3630e9dbad6fSeschrock 			if (ret == ENOENT) {
3631e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3632e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3633e9dbad6fSeschrock 			} else {
3634e9dbad6fSeschrock 				nvlist_free(props);
3635e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3636e9dbad6fSeschrock 				    "missing volume block size"));
3637e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3638e9dbad6fSeschrock 			}
3639fa9e4066Sahrens 		}
3640fa9e4066Sahrens 
3641e9dbad6fSeschrock 		if (size == 0) {
3642e9dbad6fSeschrock 			nvlist_free(props);
364399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3644e9dbad6fSeschrock 			    "volume size cannot be zero"));
3645e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36465c5460e9Seschrock 		}
36475c5460e9Seschrock 
36485c5460e9Seschrock 		if (size % blocksize != 0) {
3649e9dbad6fSeschrock 			nvlist_free(props);
365099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3651e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3652e9dbad6fSeschrock 			    "size"));
3653e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36545c5460e9Seschrock 		}
3655fa9e4066Sahrens 	}
3656fa9e4066Sahrens 
3657eb633035STom Caputi 	(void) parent_name(path, parent, sizeof (parent));
36586ccda740Sloli 	if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
36596ccda740Sloli 	    &wkeydata, &wkeylen) != 0) {
3660eb633035STom Caputi 		nvlist_free(props);
3661eb633035STom Caputi 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3662eb633035STom Caputi 	}
3663eb633035STom Caputi 
3664fa9e4066Sahrens 	/* create the dataset */
3665eb633035STom Caputi 	ret = lzc_create(path, ost, props, wkeydata, wkeylen);
36664445fffbSMatthew Ahrens 	nvlist_free(props);
3667eb633035STom Caputi 	if (wkeydata != NULL)
3668eb633035STom Caputi 		free(wkeydata);
3669e9dbad6fSeschrock 
3670fa9e4066Sahrens 	/* check for failure */
3671fa9e4066Sahrens 	if (ret != 0) {
3672fa9e4066Sahrens 		switch (errno) {
3673fa9e4066Sahrens 		case ENOENT:
367499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
367599653d4eSeschrock 			    "no such parent '%s'"), parent);
367699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3677fa9e4066Sahrens 
3678fa9e4066Sahrens 		case EINVAL:
367999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3680d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
368199653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3682fa9e4066Sahrens 
368340feaa91Sahrens 		case ENOTSUP:
368440feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
368540feaa91Sahrens 			    "pool must be upgraded to set this "
368640feaa91Sahrens 			    "property or value"));
368740feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
36889fa2266dSYuri Pankov 		case ERANGE:
36899fa2266dSYuri Pankov 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36909fa2266dSYuri Pankov 			    "invalid property value(s) specified"));
36919fa2266dSYuri Pankov 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3692eb633035STom Caputi 		case EACCES:
3693eb633035STom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3694eb633035STom Caputi 			    "encryption root's key is not loaded "
3695eb633035STom Caputi 			    "or provided"));
3696eb633035STom Caputi 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3697eb633035STom Caputi 
3698fa9e4066Sahrens #ifdef _ILP32
3699fa9e4066Sahrens 		case EOVERFLOW:
3700fa9e4066Sahrens 			/*
3701fa9e4066Sahrens 			 * This platform can't address a volume this big.
3702fa9e4066Sahrens 			 */
370399653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
370499653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
370599653d4eSeschrock 				    errbuf));
3706fa9e4066Sahrens #endif
370799653d4eSeschrock 			/* FALLTHROUGH */
3708fa9e4066Sahrens 		default:
370999653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3710fa9e4066Sahrens 		}
3711fa9e4066Sahrens 	}
3712fa9e4066Sahrens 
3713fa9e4066Sahrens 	return (0);
3714fa9e4066Sahrens }
3715fa9e4066Sahrens 
3716fa9e4066Sahrens /*
3717fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
371865fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
371965fec9f6SChristopher Siden  * does not exist this function does nothing.
3720fa9e4066Sahrens  */
3721fa9e4066Sahrens int
3722842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3723fa9e4066Sahrens {
3724049ba636SAndriy Gapon 	int error;
3725049ba636SAndriy Gapon 
3726049ba636SAndriy Gapon 	if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3727049ba636SAndriy Gapon 		return (EINVAL);
3728fa9e4066Sahrens 
372978f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
373078f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
373178f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
3732049ba636SAndriy Gapon 		error = lzc_destroy_bookmarks(nv, NULL);
373378f17100SMatthew Ahrens 		fnvlist_free(nv);
373478f17100SMatthew Ahrens 		if (error != 0) {
3735049ba636SAndriy Gapon 			return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
373678f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
373778f17100SMatthew Ahrens 			    zhp->zfs_name));
373878f17100SMatthew Ahrens 		}
373978f17100SMatthew Ahrens 		return (0);
374078f17100SMatthew Ahrens 	}
374178f17100SMatthew Ahrens 
3742049ba636SAndriy Gapon 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3743049ba636SAndriy Gapon 		nvlist_t *nv = fnvlist_alloc();
3744049ba636SAndriy Gapon 		fnvlist_add_boolean(nv, zhp->zfs_name);
3745049ba636SAndriy Gapon 		error = lzc_destroy_snaps(nv, defer, NULL);
3746049ba636SAndriy Gapon 		fnvlist_free(nv);
3747fa9e4066Sahrens 	} else {
3748049ba636SAndriy Gapon 		error = lzc_destroy(zhp->zfs_name);
3749fa9e4066Sahrens 	}
3750fa9e4066Sahrens 
3751049ba636SAndriy Gapon 	if (error != 0 && error != ENOENT) {
3752ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
375399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
375499653d4eSeschrock 		    zhp->zfs_name));
37551d452cf5Sahrens 	}
3756fa9e4066Sahrens 
3757fa9e4066Sahrens 	remove_mountpoint(zhp);
3758fa9e4066Sahrens 
3759fa9e4066Sahrens 	return (0);
3760fa9e4066Sahrens }
3761fa9e4066Sahrens 
37621d452cf5Sahrens struct destroydata {
376319b94df9SMatthew Ahrens 	nvlist_t *nvl;
376419b94df9SMatthew Ahrens 	const char *snapname;
37651d452cf5Sahrens };
37661d452cf5Sahrens 
37671d452cf5Sahrens static int
3768681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
37691d452cf5Sahrens {
37701d452cf5Sahrens 	struct destroydata *dd = arg;
37719adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3772681d9761SEric Taylor 	int rv = 0;
37731d452cf5Sahrens 
377419b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
377519b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
37761d452cf5Sahrens 
3777a7a845e4SSteven Hartland 	if (lzc_exists(name))
377819b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
37791d452cf5Sahrens 
378019b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
378119b94df9SMatthew Ahrens 	zfs_close(zhp);
37823ccfa83cSahrens 	return (rv);
37831d452cf5Sahrens }
37841d452cf5Sahrens 
37851d452cf5Sahrens /*
37861d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
37871d452cf5Sahrens  */
37881d452cf5Sahrens int
3789842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
37901d452cf5Sahrens {
37911d452cf5Sahrens 	int ret;
37921d452cf5Sahrens 	struct destroydata dd = { 0 };
37931d452cf5Sahrens 
37941d452cf5Sahrens 	dd.snapname = snapname;
379519b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
379619b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
37971d452cf5Sahrens 
3798a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
379919b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
38001d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
380119b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
380219b94df9SMatthew Ahrens 	} else {
38033b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
38041d452cf5Sahrens 	}
380519b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
380619b94df9SMatthew Ahrens 	return (ret);
380719b94df9SMatthew Ahrens }
380819b94df9SMatthew Ahrens 
380919b94df9SMatthew Ahrens /*
38103b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
381119b94df9SMatthew Ahrens  */
381219b94df9SMatthew Ahrens int
38133b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
381419b94df9SMatthew Ahrens {
381519b94df9SMatthew Ahrens 	int ret;
38164cde22c2SChris Williamson 	nvlist_t *errlist = NULL;
38171d452cf5Sahrens 
38184445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
38191d452cf5Sahrens 
38204cde22c2SChris Williamson 	if (ret == 0) {
38214cde22c2SChris Williamson 		nvlist_free(errlist);
38223b2aab18SMatthew Ahrens 		return (0);
38234cde22c2SChris Williamson 	}
38244445fffbSMatthew Ahrens 
3825a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
38263b2aab18SMatthew Ahrens 		char errbuf[1024];
38273b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
38283b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
38293b2aab18SMatthew Ahrens 
38303b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
38313b2aab18SMatthew Ahrens 	}
38323b2aab18SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
38333b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
38343b2aab18SMatthew Ahrens 		char errbuf[1024];
38353b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
38363b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
38373b2aab18SMatthew Ahrens 		    nvpair_name(pair));
38383b2aab18SMatthew Ahrens 
38393b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
38403b2aab18SMatthew Ahrens 		case EEXIST:
38413b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
38423b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
38433b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
38443b2aab18SMatthew Ahrens 			break;
38453b2aab18SMatthew Ahrens 		default:
38463b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
38473b2aab18SMatthew Ahrens 			break;
38481d452cf5Sahrens 		}
38491d452cf5Sahrens 	}
38501d452cf5Sahrens 
38514cde22c2SChris Williamson 	nvlist_free(errlist);
38524445fffbSMatthew Ahrens 	return (ret);
38531d452cf5Sahrens }
38541d452cf5Sahrens 
3855fa9e4066Sahrens /*
3856fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3857fa9e4066Sahrens  */
3858fa9e4066Sahrens int
3859e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3860fa9e4066Sahrens {
38619adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3862fa9e4066Sahrens 	int ret;
386399653d4eSeschrock 	char errbuf[1024];
386499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3865e9dbad6fSeschrock 	uint64_t zoned;
3866fa9e4066Sahrens 
3867fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3868fa9e4066Sahrens 
386999653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
387099653d4eSeschrock 	    "cannot create '%s'"), target);
387199653d4eSeschrock 
387219b94df9SMatthew Ahrens 	/* validate the target/clone name */
3873f18faf3fSek 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
387499653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3875fa9e4066Sahrens 
3876fa9e4066Sahrens 	/* validate parents exist */
38777f1f55eaSvb 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3878fa9e4066Sahrens 		return (-1);
3879fa9e4066Sahrens 
3880fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3881fa9e4066Sahrens 
3882fa9e4066Sahrens 	/* do the clone */
3883e9dbad6fSeschrock 
3884e9dbad6fSeschrock 	if (props) {
38854445fffbSMatthew Ahrens 		zfs_type_t type;
38861c10ae76SMike Gerdts 
38874445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
38884445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
38894445fffbSMatthew Ahrens 		} else {
38904445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
38914445fffbSMatthew Ahrens 		}
38920a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3893eb633035STom Caputi 		    zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
3894e9dbad6fSeschrock 			return (-1);
38951c10ae76SMike Gerdts 		if (zfs_fix_auto_resv(zhp, props) == -1) {
38961c10ae76SMike Gerdts 			nvlist_free(props);
38971c10ae76SMike Gerdts 			return (-1);
38981c10ae76SMike Gerdts 		}
3899e9dbad6fSeschrock 	}
3900fa9e4066Sahrens 
3901eb633035STom Caputi 	if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
3902eb633035STom Caputi 		nvlist_free(props);
3903eb633035STom Caputi 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3904eb633035STom Caputi 	}
3905eb633035STom Caputi 
39064445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
39074445fffbSMatthew Ahrens 	nvlist_free(props);
3908e9dbad6fSeschrock 
3909fa9e4066Sahrens 	if (ret != 0) {
3910fa9e4066Sahrens 		switch (errno) {
3911fa9e4066Sahrens 
3912fa9e4066Sahrens 		case ENOENT:
3913fa9e4066Sahrens 			/*
3914fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3915fa9e4066Sahrens 			 * above, but there may a race condition that has since
3916fa9e4066Sahrens 			 * destroyed the parent.
3917fa9e4066Sahrens 			 *
3918fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3919fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3920fa9e4066Sahrens 			 * dataset doesn't exist.
3921fa9e4066Sahrens 			 */
392299653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
392399653d4eSeschrock 			    "no such parent '%s'"), parent);
392499653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3925fa9e4066Sahrens 
392699653d4eSeschrock 		case EXDEV:
392799653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
392899653d4eSeschrock 			    "source and target pools differ"));
392999653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
393099653d4eSeschrock 			    errbuf));
3931fa9e4066Sahrens 
393299653d4eSeschrock 		default:
393399653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
393499653d4eSeschrock 			    errbuf));
393599653d4eSeschrock 		}
393699653d4eSeschrock 	}
3937fa9e4066Sahrens 
393899653d4eSeschrock 	return (ret);
393999653d4eSeschrock }
394099653d4eSeschrock 
394199653d4eSeschrock /*
394299653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
394399653d4eSeschrock  */
394499653d4eSeschrock int
394599653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
394699653d4eSeschrock {
394799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3948a4b8c9aaSAndrew Stormont 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
394999653d4eSeschrock 	int ret;
395099653d4eSeschrock 	char errbuf[1024];
395199653d4eSeschrock 
395299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
395399653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
395499653d4eSeschrock 
395599653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
395699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
395799653d4eSeschrock 		    "snapshots can not be promoted"));
395899653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
395999653d4eSeschrock 	}
396099653d4eSeschrock 
3961a4b8c9aaSAndrew Stormont 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
396299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396399653d4eSeschrock 		    "not a cloned filesystem"));
396499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
396599653d4eSeschrock 	}
396699653d4eSeschrock 
3967add927f8Sloli 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3968add927f8Sloli 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3969add927f8Sloli 
3970a4b8c9aaSAndrew Stormont 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
397199653d4eSeschrock 
397299653d4eSeschrock 	if (ret != 0) {
3973a4b8c9aaSAndrew Stormont 		switch (ret) {
397499653d4eSeschrock 		case EEXIST:
3975681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
397699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3977681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3978a4b8c9aaSAndrew Stormont 			    snapname, zhp->zfs_dmustats.dds_origin);
397999653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3980fa9e4066Sahrens 
3981fa9e4066Sahrens 		default:
3982a4b8c9aaSAndrew Stormont 			return (zfs_standard_error(hdl, ret, errbuf));
3983fa9e4066Sahrens 		}
3984fa9e4066Sahrens 	}
3985e9dbad6fSeschrock 	return (ret);
39861d452cf5Sahrens }
39871d452cf5Sahrens 
39884445fffbSMatthew Ahrens typedef struct snapdata {
39894445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
39904445fffbSMatthew Ahrens 	const char *sd_snapname;
39914445fffbSMatthew Ahrens } snapdata_t;
39924445fffbSMatthew Ahrens 
39934445fffbSMatthew Ahrens static int
39944445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
39954445fffbSMatthew Ahrens {
39964445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
39979adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
39984445fffbSMatthew Ahrens 	int rv = 0;
39994445fffbSMatthew Ahrens 
4000ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4001ca48f36fSKeith M Wesolowski 		(void) snprintf(name, sizeof (name),
4002ca48f36fSKeith M Wesolowski 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
40034445fffbSMatthew Ahrens 
4004ca48f36fSKeith M Wesolowski 		fnvlist_add_boolean(sd->sd_nvl, name);
40054445fffbSMatthew Ahrens 
4006ca48f36fSKeith M Wesolowski 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4007ca48f36fSKeith M Wesolowski 	}
40084445fffbSMatthew Ahrens 	zfs_close(zhp);
4009ca48f36fSKeith M Wesolowski 
40104445fffbSMatthew Ahrens 	return (rv);
40114445fffbSMatthew Ahrens }
40124445fffbSMatthew Ahrens 
40135cabbc6bSPrashanth Sreenivasa int
40145cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
40155cabbc6bSPrashanth Sreenivasa {
40165cabbc6bSPrashanth Sreenivasa 	int err;
40175cabbc6bSPrashanth Sreenivasa 	char errbuf[1024];
40185cabbc6bSPrashanth Sreenivasa 
40195cabbc6bSPrashanth Sreenivasa 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40200b2e8253Sloli 	    "cannot remap dataset '%s'"), fs);
40215cabbc6bSPrashanth Sreenivasa 
40225cabbc6bSPrashanth Sreenivasa 	err = lzc_remap(fs);
40235cabbc6bSPrashanth Sreenivasa 
40245cabbc6bSPrashanth Sreenivasa 	if (err != 0) {
40250b2e8253Sloli 		switch (err) {
40260b2e8253Sloli 		case ENOTSUP:
40270b2e8253Sloli 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40280b2e8253Sloli 			    "pool must be upgraded"));
40290b2e8253Sloli 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
40300b2e8253Sloli 			break;
40310b2e8253Sloli 		case EINVAL:
40320b2e8253Sloli 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
40330b2e8253Sloli 			break;
40340b2e8253Sloli 		default:
40350b2e8253Sloli 			(void) zfs_standard_error(hdl, err, errbuf);
40360b2e8253Sloli 			break;
40370b2e8253Sloli 		}
40385cabbc6bSPrashanth Sreenivasa 	}
40395cabbc6bSPrashanth Sreenivasa 
40405cabbc6bSPrashanth Sreenivasa 	return (err);
40415cabbc6bSPrashanth Sreenivasa }
40425cabbc6bSPrashanth Sreenivasa 
4043fa9e4066Sahrens /*
40444445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
40454445fffbSMatthew Ahrens  * created.
4046fa9e4066Sahrens  */
4047fa9e4066Sahrens int
40484445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4049fa9e4066Sahrens {
4050fa9e4066Sahrens 	int ret;
405199653d4eSeschrock 	char errbuf[1024];
40524445fffbSMatthew Ahrens 	nvpair_t *elem;
40534445fffbSMatthew Ahrens 	nvlist_t *errors;
4054fa9e4066Sahrens 
405599653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40564445fffbSMatthew Ahrens 	    "cannot create snapshots "));
405799653d4eSeschrock 
40584445fffbSMatthew Ahrens 	elem = NULL;
40594445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
40604445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
40614445fffbSMatthew Ahrens 
40624445fffbSMatthew Ahrens 		/* validate the target name */
40634445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
40644445fffbSMatthew Ahrens 		    B_TRUE)) {
40654445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
40664445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
40674445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
40684445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
40694445fffbSMatthew Ahrens 		}
40704445fffbSMatthew Ahrens 	}
4071fa9e4066Sahrens 
4072e9316f76SJoe Stein 	/*
4073e9316f76SJoe Stein 	 * get pool handle for prop validation. assumes all snaps are in the
4074e9316f76SJoe Stein 	 * same pool, as does lzc_snapshot (below).
4075e9316f76SJoe Stein 	 */
40769adfa60dSMatthew Ahrens 	char pool[ZFS_MAX_DATASET_NAME_LEN];
4077e9316f76SJoe Stein 	elem = nvlist_next_nvpair(snaps, NULL);
4078e9316f76SJoe Stein 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4079e9316f76SJoe Stein 	pool[strcspn(pool, "/@")] = '\0';
4080e9316f76SJoe Stein 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4081e9316f76SJoe Stein 
40824445fffbSMatthew Ahrens 	if (props != NULL &&
40834445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4084eb633035STom Caputi 	    props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4085e9316f76SJoe Stein 		zpool_close(zpool_hdl);
40864445fffbSMatthew Ahrens 		return (-1);
40874445fffbSMatthew Ahrens 	}
4088e9316f76SJoe Stein 	zpool_close(zpool_hdl);
4089bb0ade09Sahrens 
40904445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
40914445fffbSMatthew Ahrens 
40924445fffbSMatthew Ahrens 	if (ret != 0) {
40934445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
40944445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
40954445fffbSMatthew Ahrens 		    elem != NULL;
40964445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
40974445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
40984445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
40994445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
41004445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
41014445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
41024445fffbSMatthew Ahrens 			printed = B_TRUE;
4103bb0ade09Sahrens 		}
41044445fffbSMatthew Ahrens 		if (!printed) {
41054445fffbSMatthew Ahrens 			switch (ret) {
41064445fffbSMatthew Ahrens 			case EXDEV:
41074445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41084445fffbSMatthew Ahrens 				    "multiple snapshots of same "
41094445fffbSMatthew Ahrens 				    "fs not allowed"));
41104445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4111bb0ade09Sahrens 
41124445fffbSMatthew Ahrens 				break;
41134445fffbSMatthew Ahrens 			default:
41144445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
41154445fffbSMatthew Ahrens 			}
41164445fffbSMatthew Ahrens 		}
4117bb0ade09Sahrens 	}
4118bb0ade09Sahrens 
41194445fffbSMatthew Ahrens 	nvlist_free(props);
41204445fffbSMatthew Ahrens 	nvlist_free(errors);
41214445fffbSMatthew Ahrens 	return (ret);
41224445fffbSMatthew Ahrens }
41234445fffbSMatthew Ahrens 
41244445fffbSMatthew Ahrens int
41254445fffbSMatthew Ahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
41264445fffbSMatthew Ahrens     nvlist_t *props)
41274445fffbSMatthew Ahrens {
41284445fffbSMatthew Ahrens 	int ret;
41294445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
41309adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
41314445fffbSMatthew Ahrens 	char *cp;
41324445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
41334445fffbSMatthew Ahrens 	char errbuf[1024];
41344445fffbSMatthew Ahrens 
41354445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
41364445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
41374445fffbSMatthew Ahrens 
41384445fffbSMatthew Ahrens 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
41394445fffbSMatthew Ahrens 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41404445fffbSMatthew Ahrens 
41414445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
41424445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
41434445fffbSMatthew Ahrens 	*cp = '\0';
41444445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
4145fa9e4066Sahrens 
41464445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4147fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
4148fa9e4066Sahrens 		return (-1);
4149fa9e4066Sahrens 	}
4150fa9e4066Sahrens 
41514445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
41524445fffbSMatthew Ahrens 	if (recursive) {
41534445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
41544445fffbSMatthew Ahrens 	} else {
41554445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
4156681d9761SEric Taylor 	}
4157fa9e4066Sahrens 
41584445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
41594445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
4160fa9e4066Sahrens 	zfs_close(zhp);
4161fa9e4066Sahrens 	return (ret);
4162fa9e4066Sahrens }
4163fa9e4066Sahrens 
4164fa9e4066Sahrens /*
4165b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
4166b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
4167b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
4168b12a1c38Slling  * group.
4169fa9e4066Sahrens  */
4170b12a1c38Slling typedef struct rollback_data {
4171b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
4172b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
4173c391e322Sahrens 	boolean_t	cb_error;
4174c391e322Sahrens 	boolean_t	cb_force;
4175b12a1c38Slling } rollback_data_t;
4176b12a1c38Slling 
4177b12a1c38Slling static int
417878f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4179b12a1c38Slling {
4180b12a1c38Slling 	rollback_data_t *cbp = data;
418178f17100SMatthew Ahrens 	prop_changelist_t *clp;
418278f17100SMatthew Ahrens 
418378f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
418478f17100SMatthew Ahrens 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
418578f17100SMatthew Ahrens 	    cbp->cb_force ? MS_FORCE: 0);
418678f17100SMatthew Ahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
418778f17100SMatthew Ahrens 		cbp->cb_error = B_TRUE;
418878f17100SMatthew Ahrens 		zfs_close(zhp);
418978f17100SMatthew Ahrens 		return (0);
419078f17100SMatthew Ahrens 	}
419178f17100SMatthew Ahrens 	if (zfs_destroy(zhp, B_FALSE) != 0)
419278f17100SMatthew Ahrens 		cbp->cb_error = B_TRUE;
419378f17100SMatthew Ahrens 	else
419478f17100SMatthew Ahrens 		changelist_remove(clp, zhp->zfs_name);
419578f17100SMatthew Ahrens 	(void) changelist_postfix(clp);
419678f17100SMatthew Ahrens 	changelist_free(clp);
4197b12a1c38Slling 
419878f17100SMatthew Ahrens 	zfs_close(zhp);
419978f17100SMatthew Ahrens 	return (0);
420078f17100SMatthew Ahrens }
4201b12a1c38Slling 
420278f17100SMatthew Ahrens static int
420378f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
420478f17100SMatthew Ahrens {
420578f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
4206b12a1c38Slling 
420778f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
420878f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
420978f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
4210c391e322Sahrens 
421178f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4212b12a1c38Slling 	}
4213b12a1c38Slling 
4214b12a1c38Slling 	zfs_close(zhp);
4215b12a1c38Slling 	return (0);
4216b12a1c38Slling }
4217b12a1c38Slling 
4218b12a1c38Slling /*
4219b12a1c38Slling  * Given a dataset, rollback to a specific snapshot, discarding any
4220b12a1c38Slling  * data changes since then and making it the active dataset.
4221b12a1c38Slling  *
422278f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
422378f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
4224b12a1c38Slling  */
4225b12a1c38Slling int
4226c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4227b12a1c38Slling {
4228b12a1c38Slling 	rollback_data_t cb = { 0 };
42294ccbb6e7Sahrens 	int err;
42307b97dc1aSrm 	boolean_t restore_resv = 0;
4231f83b46baSPaul Dagnelie 	uint64_t old_volsize = 0, new_volsize;
42327b97dc1aSrm 	zfs_prop_t resv_prop;
4233b12a1c38Slling 
42344ccbb6e7Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
42354ccbb6e7Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
4236b12a1c38Slling 
4237b12a1c38Slling 	/*
42382e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
4239b12a1c38Slling 	 */
4240c391e322Sahrens 	cb.cb_force = force;
4241b12a1c38Slling 	cb.cb_target = snap->zfs_name;
4242b12a1c38Slling 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
42430d8fa8f8SMartin Matuska 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
424478f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4245b12a1c38Slling 
4246c391e322Sahrens 	if (cb.cb_error)
4247c391e322Sahrens 		return (-1);
4248b12a1c38Slling 
4249b12a1c38Slling 	/*
4250b12a1c38Slling 	 * Now that we have verified that the snapshot is the latest,
4251b12a1c38Slling 	 * rollback to the given snapshot.
4252b12a1c38Slling 	 */
4253b12a1c38Slling 
42547b97dc1aSrm 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
42557b97dc1aSrm 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
42567b97dc1aSrm 			return (-1);
42577b97dc1aSrm 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
42587b97dc1aSrm 		restore_resv =
42597b97dc1aSrm 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
42607b97dc1aSrm 	}
42614ccbb6e7Sahrens 
4262b12a1c38Slling 	/*
426377b17137SAndriy Gapon 	 * Pass both the filesystem and the wanted snapshot names,
426477b17137SAndriy Gapon 	 * we would get an error back if the snapshot is destroyed or
426577b17137SAndriy Gapon 	 * a new snapshot is created before this request is processed.
4266b12a1c38Slling 	 */
426777b17137SAndriy Gapon 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
426895643f75SAndriy Gapon 	if (err != 0) {
426995643f75SAndriy Gapon 		char errbuf[1024];
427095643f75SAndriy Gapon 
427195643f75SAndriy Gapon 		(void) snprintf(errbuf, sizeof (errbuf),
42724ccbb6e7Sahrens 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
42734ccbb6e7Sahrens 		    zhp->zfs_name);
427495643f75SAndriy Gapon 		switch (err) {
427595643f75SAndriy Gapon 		case EEXIST:
427695643f75SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
427795643f75SAndriy Gapon 			    "there is a snapshot or bookmark more recent "
427895643f75SAndriy Gapon 			    "than '%s'"), snap->zfs_name);
427995643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
428095643f75SAndriy Gapon 			break;
428195643f75SAndriy Gapon 		case ESRCH:
428295643f75SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
428395643f75SAndriy Gapon 			    "'%s' is not found among snapshots of '%s'"),
428495643f75SAndriy Gapon 			    snap->zfs_name, zhp->zfs_name);
428595643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
428695643f75SAndriy Gapon 			break;
428795643f75SAndriy Gapon 		case EINVAL:
428895643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
428995643f75SAndriy Gapon 			break;
429095643f75SAndriy Gapon 		default:
429195643f75SAndriy Gapon 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
429295643f75SAndriy Gapon 		}
4293b9415e83Srm 		return (err);
4294b9415e83Srm 	}
42957b97dc1aSrm 
42967b97dc1aSrm 	/*
42977b97dc1aSrm 	 * For volumes, if the pre-rollback volsize matched the pre-
42987b97dc1aSrm 	 * rollback reservation and the volsize has changed then set
42997b97dc1aSrm 	 * the reservation property to the post-rollback volsize.
43007b97dc1aSrm 	 * Make a new handle since the rollback closed the dataset.
43017b97dc1aSrm 	 */
4302b9415e83Srm 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4303b9415e83Srm 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
43047b97dc1aSrm 		if (restore_resv) {
43057b97dc1aSrm 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
43067b97dc1aSrm 			if (old_volsize != new_volsize)
4307b9415e83Srm 				err = zfs_prop_set_int(zhp, resv_prop,
4308b9415e83Srm 				    new_volsize);
43097b97dc1aSrm 		}
43107b97dc1aSrm 		zfs_close(zhp);
43114ccbb6e7Sahrens 	}
43124ccbb6e7Sahrens 	return (err);
4313b12a1c38Slling }
4314b12a1c38Slling 
4315fa9e4066Sahrens /*
4316fa9e4066Sahrens  * Renames the given dataset.
4317fa9e4066Sahrens  */
4318fa9e4066Sahrens int
43196a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
43206a9cb0eaSEric Schrock     boolean_t force_unmount)
4321fa9e4066Sahrens {
432288f61deeSIgor Kozhukhov 	int ret = 0;
4323fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
4324fa9e4066Sahrens 	char *delim;
4325cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
4326cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
4327cdf5b4caSmmusante 	char *parentname = NULL;
43289adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
432999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
433099653d4eSeschrock 	char errbuf[1024];
4331fa9e4066Sahrens 
4332fa9e4066Sahrens 	/* if we have the same exact name, just return success */
4333fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
4334fa9e4066Sahrens 		return (0);
4335fa9e4066Sahrens 
433699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
433799653d4eSeschrock 	    "cannot rename to '%s'"), target);
433899653d4eSeschrock 
4339add927f8Sloli 	/* make sure source name is valid */
4340add927f8Sloli 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4341add927f8Sloli 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4342add927f8Sloli 
4343fa9e4066Sahrens 	/*
4344fa9e4066Sahrens 	 * Make sure the target name is valid
4345fa9e4066Sahrens 	 */
4346fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
434798579b20Snd 		if ((strchr(target, '@') == NULL) ||
434898579b20Snd 		    *target == '@') {
434998579b20Snd 			/*
435098579b20Snd 			 * Snapshot target name is abbreviated,
435198579b20Snd 			 * reconstruct full dataset name
435298579b20Snd 			 */
435398579b20Snd 			(void) strlcpy(parent, zhp->zfs_name,
435498579b20Snd 			    sizeof (parent));
435598579b20Snd 			delim = strchr(parent, '@');
435698579b20Snd 			if (strchr(target, '@') == NULL)
435798579b20Snd 				*(++delim) = '\0';
435898579b20Snd 			else
435998579b20Snd 				*delim = '\0';
436098579b20Snd 			(void) strlcat(parent, target, sizeof (parent));
436198579b20Snd 			target = parent;
436298579b20Snd 		} else {
436398579b20Snd 			/*
436498579b20Snd 			 * Make sure we're renaming within the same dataset.
436598579b20Snd 			 */
436698579b20Snd 			delim = strchr(target, '@');
436798579b20Snd 			if (strncmp(zhp->zfs_name, target, delim - target)
436898579b20Snd 			    != 0 || zhp->zfs_name[delim - target] != '@') {
436998579b20Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437098579b20Snd 				    "snapshots must be part of same "
437198579b20Snd 				    "dataset"));
437298579b20Snd 				return (zfs_error(hdl, EZFS_CROSSTARGET,
4373b1b8ab34Slling 				    errbuf));
437498579b20Snd 			}
4375fa9e4066Sahrens 		}
43765ac95da7SSerapheim Dimitropoulos 
4377f18faf3fSek 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
437898579b20Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4379fa9e4066Sahrens 	} else {
4380cdf5b4caSmmusante 		if (recursive) {
4381cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4382cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
4383cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4384cdf5b4caSmmusante 		}
4385cdf5b4caSmmusante 
4386f18faf3fSek 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
438798579b20Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4388e9dbad6fSeschrock 
4389fa9e4066Sahrens 		/* validate parents */
4390d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4391fa9e4066Sahrens 			return (-1);
4392fa9e4066Sahrens 
4393fa9e4066Sahrens 		/* make sure we're in the same pool */
4394fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4395fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4396fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
439799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439899653d4eSeschrock 			    "datasets must be within same pool"));
439999653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4400fa9e4066Sahrens 		}
4401f2fdf992Snd 
4402f2fdf992Snd 		/* new name cannot be a child of the current dataset name */
4403d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
4404f2fdf992Snd 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4405d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
4406f2fdf992Snd 			    "current dataset name"));
4407f2fdf992Snd 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4408f2fdf992Snd 		}
4409fa9e4066Sahrens 	}
4410fa9e4066Sahrens 
441199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
441299653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
441399653d4eSeschrock 
4414fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4415fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
441699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
441799653d4eSeschrock 		    "dataset is used in a non-global zone"));
441899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4419fa9e4066Sahrens 	}
4420fa9e4066Sahrens 
4421cdf5b4caSmmusante 	if (recursive) {
4422f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4423f0c5ee21Smmusante 		if (parentname == NULL) {
4424f0c5ee21Smmusante 			ret = -1;
4425f0c5ee21Smmusante 			goto error;
4426f0c5ee21Smmusante 		}
4427cdf5b4caSmmusante 		delim = strchr(parentname, '@');
4428cdf5b4caSmmusante 		*delim = '\0';
4429990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4430cdf5b4caSmmusante 		if (zhrp == NULL) {
4431f0c5ee21Smmusante 			ret = -1;
4432f0c5ee21Smmusante 			goto error;
4433cdf5b4caSmmusante 		}
443433cde0d0SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
44356a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
44366a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
4437cdf5b4caSmmusante 			return (-1);
4438cdf5b4caSmmusante 
4439cdf5b4caSmmusante 		if (changelist_haszonedchild(cl)) {
4440cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4441cdf5b4caSmmusante 			    "child dataset with inherited mountpoint is used "
4442cdf5b4caSmmusante 			    "in a non-global zone"));
4443cdf5b4caSmmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4444f83b46baSPaul Dagnelie 			ret = -1;
4445cdf5b4caSmmusante 			goto error;
4446cdf5b4caSmmusante 		}
4447cdf5b4caSmmusante 
4448cdf5b4caSmmusante 		if ((ret = changelist_prefix(cl)) != 0)
4449cdf5b4caSmmusante 			goto error;
4450cdf5b4caSmmusante 	}
4451fa9e4066Sahrens 
4452e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
4453fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4454fa9e4066Sahrens 	else
4455fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4456fa9e4066Sahrens 
445798579b20Snd 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4458e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
445998579b20Snd 
4460cdf5b4caSmmusante 	zc.zc_cookie = recursive;
4461cdf5b4caSmmusante 
4462ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4463cdf5b4caSmmusante 		/*
4464cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
4465cdf5b4caSmmusante 		 * be in zc.zc_name
4466cdf5b4caSmmusante 		 */
4467cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
44683cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
4469cdf5b4caSmmusante 
4470cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
4471cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4472cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
4473cdf5b4caSmmusante 			    "with the new name"));
4474a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4475eb633035STom Caputi 		} else if (errno == EACCES) {
4476a60ca23dSTom Caputi 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4477a60ca23dSTom Caputi 			    "cannot move encrypted child outside of "
4478a60ca23dSTom Caputi 			    "its encryption root"));
4479eb633035STom Caputi 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4480cdf5b4caSmmusante 		} else {
4481cdf5b4caSmmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4482cdf5b4caSmmusante 		}
4483fa9e4066Sahrens 
4484fa9e4066Sahrens 		/*
4485fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
4486fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
4487fa9e4066Sahrens 		 */
448833cde0d0SMatthew Ahrens 		if (cl != NULL)
4489cdf5b4caSmmusante 			(void) changelist_postfix(cl);
4490fa9e4066Sahrens 	} else {
449133cde0d0SMatthew Ahrens 		if (cl != NULL) {
4492cdf5b4caSmmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
4493cdf5b4caSmmusante 			ret = changelist_postfix(cl);
4494cdf5b4caSmmusante 		}
4495fa9e4066Sahrens 	}
4496fa9e4066Sahrens 
4497fa9e4066Sahrens error:
449833cde0d0SMatthew Ahrens 	if (parentname != NULL) {
4499cdf5b4caSmmusante 		free(parentname);
4500cdf5b4caSmmusante 	}
450133cde0d0SMatthew Ahrens 	if (zhrp != NULL) {
4502cdf5b4caSmmusante 		zfs_close(zhrp);
4503cdf5b4caSmmusante 	}
450433cde0d0SMatthew Ahrens 	if (cl != NULL) {
4505cdf5b4caSmmusante 		changelist_free(cl);
4506cdf5b4caSmmusante 	}
4507fa9e4066Sahrens 	return (ret);
4508fa9e4066Sahrens }
4509fa9e4066Sahrens 
4510e9dbad6fSeschrock nvlist_t *
4511e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4512e9dbad6fSeschrock {
4513e9dbad6fSeschrock 	return (zhp->zfs_user_props);
4514e9dbad6fSeschrock }
4515e9dbad6fSeschrock 
451692241e0bSTom Erickson nvlist_t *
451792241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
451892241e0bSTom Erickson {
451992241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
452092241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
452192241e0bSTom Erickson 			return (NULL);
452292241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
452392241e0bSTom Erickson }
452492241e0bSTom Erickson 
4525b1b8ab34Slling /*
4526b1b8ab34Slling  * This function is used by 'zfs list' to determine the exact set of columns to
4527b1b8ab34Slling  * display, and their maximum widths.  This does two main things:
4528b1b8ab34Slling  *
4529b1b8ab34Slling  *      - If this is a list of all properties, then expand the list to include
4530b1b8ab34Slling  *        all native properties, and set a flag so that for each dataset we look
4531b1b8ab34Slling  *        for new unique user properties and add them to the list.
4532b1b8ab34Slling  *
4533b1b8ab34Slling  *      - For non fixed-width properties, keep track of the maximum width seen
453492241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
453592241e0bSTom Erickson  *        requested received property values, we also need to compute the width
453692241e0bSTom Erickson  *        of the RECEIVED column.
4537b1b8ab34Slling  */
4538b1b8ab34Slling int
453943d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
454043d68d68SYuri Pankov     boolean_t literal)
4541b1b8ab34Slling {
4542b1b8ab34Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4543990b4856Slling 	zprop_list_t *entry;
4544990b4856Slling 	zprop_list_t **last, **start;
4545b1b8ab34Slling 	nvlist_t *userprops, *propval;
4546b1b8ab34Slling 	nvpair_t *elem;
4547b1b8ab34Slling 	char *strval;
4548b1b8ab34Slling 	char buf[ZFS_MAXPROPLEN];
4549b1b8ab34Slling 
4550990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4551b1b8ab34Slling 		return (-1);
4552e9dbad6fSeschrock 
4553e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4554e9dbad6fSeschrock 
4555e9dbad6fSeschrock 	entry = *plp;
4556e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4557e9dbad6fSeschrock 		/*
4558e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4559e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4560e9dbad6fSeschrock 		 * non-native property.
4561e9dbad6fSeschrock 		 */
4562e9dbad6fSeschrock 		start = plp;
4563e9dbad6fSeschrock 		while (*start != NULL) {
4564990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4565e9dbad6fSeschrock 				break;
4566e9dbad6fSeschrock 			start = &(*start)->pl_next;
4567e9dbad6fSeschrock 		}
4568e9dbad6fSeschrock 
4569e9dbad6fSeschrock 		elem = NULL;
4570e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4571e9dbad6fSeschrock 			/*
4572e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4573e9dbad6fSeschrock 			 */
4574e9dbad6fSeschrock 			for (last = start; *last != NULL;
4575e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4576e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4577e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4578e9dbad6fSeschrock 					break;
4579e9dbad6fSeschrock 			}
4580e9dbad6fSeschrock 
4581e9dbad6fSeschrock 			if (*last == NULL) {
4582e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4583990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4584e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4585e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4586e9dbad6fSeschrock 					free(entry);
4587e9dbad6fSeschrock 					return (-1);
4588e9dbad6fSeschrock 				}
4589e9dbad6fSeschrock 
4590990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4591e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4592e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4593e9dbad6fSeschrock 				*last = entry;
4594e9dbad6fSeschrock 			}
4595e9dbad6fSeschrock 		}
4596e9dbad6fSeschrock 	}
4597e9dbad6fSeschrock 
4598e9dbad6fSeschrock 	/*
4599e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4600e9dbad6fSeschrock 	 */
4601e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
460243d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
4603e9dbad6fSeschrock 			continue;
4604e9dbad6fSeschrock 
4605990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4606e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
460743d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4608e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4609e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4610e9dbad6fSeschrock 			}
461192241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
461292241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
461343d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
461492241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
461592241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
461692241e0bSTom Erickson 		} else {
461792241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
461892241e0bSTom Erickson 			    &propval) == 0) {
461992241e0bSTom Erickson 				verify(nvlist_lookup_string(propval,
462092241e0bSTom Erickson 				    ZPROP_VALUE, &strval) == 0);
462192241e0bSTom Erickson 				if (strlen(strval) > entry->pl_width)
462292241e0bSTom Erickson 					entry->pl_width = strlen(strval);
462392241e0bSTom Erickson 			}
462492241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
462592241e0bSTom Erickson 			    entry->pl_user_prop,
462643d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
462792241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
462892241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
4629e9dbad6fSeschrock 		}
4630e9dbad6fSeschrock 	}
4631e9dbad6fSeschrock 
4632e9dbad6fSeschrock 	return (0);
4633e9dbad6fSeschrock }
4634ecd6cf80Smarks 
4635ecd6cf80Smarks int
4636ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4637743a77edSAlan Wright     char *resource, void *export, void *sharetab,
4638743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
4639ecd6cf80Smarks {
4640ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4641ecd6cf80Smarks 	int error;
4642ecd6cf80Smarks 
4643ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4644ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4645743a77edSAlan Wright 	if (resource)
4646743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4647ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4648ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4649da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4650ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4651ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4652ecd6cf80Smarks 	return (error);
4653ecd6cf80Smarks }
46542e5e9e19SSanjeev Bagewadi 
46552e5e9e19SSanjeev Bagewadi void
46562e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
46572e5e9e19SSanjeev Bagewadi {
46582e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
46592e5e9e19SSanjeev Bagewadi 
46602e5e9e19SSanjeev Bagewadi 	/*
46612e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
46622e5e9e19SSanjeev Bagewadi 	 * properties.
46632e5e9e19SSanjeev Bagewadi 	 */
46642e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
46652e5e9e19SSanjeev Bagewadi 
46662e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
46672e5e9e19SSanjeev Bagewadi 
46682e5e9e19SSanjeev Bagewadi 	while (curr) {
46692e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
46702e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
46712e5e9e19SSanjeev Bagewadi 
467214843421SMatthew Ahrens 		/*
4673faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4674faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4675faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4676faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4677faaa6415SEric Schrock 		 * software, for example).
467814843421SMatthew Ahrens 		 */
467914843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
46802e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
46812e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
46822e5e9e19SSanjeev Bagewadi 		curr = next;
46832e5e9e19SSanjeev Bagewadi 	}
46842e5e9e19SSanjeev Bagewadi }
4685743a77edSAlan Wright 
4686743a77edSAlan Wright static int
4687743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4688743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4689743a77edSAlan Wright {
4690743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4691743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4692743a77edSAlan Wright 	int error;
4693743a77edSAlan Wright 
4694743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4695743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4696743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4697743a77edSAlan Wright 
4698743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4699743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4700743a77edSAlan Wright 			(void) no_memory(hdl);
470130925561SChris Williamson 			return (0);
4702743a77edSAlan Wright 		}
4703743a77edSAlan Wright 	}
4704743a77edSAlan Wright 
4705743a77edSAlan Wright 	switch (cmd) {
4706743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4707743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4708743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4709743a77edSAlan Wright 		break;
4710743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4711743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4712743a77edSAlan Wright 		    resource1) != 0) {
4713743a77edSAlan Wright 				(void) no_memory(hdl);
4714743a77edSAlan Wright 				return (-1);
4715743a77edSAlan Wright 		}
4716743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4717743a77edSAlan Wright 		    resource2) != 0) {
4718743a77edSAlan Wright 				(void) no_memory(hdl);
4719743a77edSAlan Wright 				return (-1);
4720743a77edSAlan Wright 		}
4721743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4722743a77edSAlan Wright 			nvlist_free(nvlist);
4723743a77edSAlan Wright 			return (-1);
4724743a77edSAlan Wright 		}
4725743a77edSAlan Wright 		break;
4726743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4727743a77edSAlan Wright 		break;
4728743a77edSAlan Wright 	default:
4729743a77edSAlan Wright 		return (-1);
4730743a77edSAlan Wright 	}
4731743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4732aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlist);
4733743a77edSAlan Wright 	return (error);
4734743a77edSAlan Wright }
4735743a77edSAlan Wright 
4736743a77edSAlan Wright int
4737743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4738743a77edSAlan Wright     char *path, char *resource)
4739743a77edSAlan Wright {
4740743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4741743a77edSAlan Wright 	    resource, NULL));
4742743a77edSAlan Wright }
4743743a77edSAlan Wright 
4744743a77edSAlan Wright int
4745743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4746743a77edSAlan Wright     char *path, char *resource)
4747743a77edSAlan Wright {
4748743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4749743a77edSAlan Wright 	    resource, NULL));
4750743a77edSAlan Wright }
4751743a77edSAlan Wright 
4752743a77edSAlan Wright int
4753743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4754743a77edSAlan Wright {
4755743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4756743a77edSAlan Wright 	    NULL, NULL));
4757743a77edSAlan Wright }
4758743a77edSAlan Wright 
4759743a77edSAlan Wright int
4760743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4761743a77edSAlan Wright     char *oldname, char *newname)
4762743a77edSAlan Wright {
4763743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4764743a77edSAlan Wright 	    oldname, newname));
4765743a77edSAlan Wright }
476614843421SMatthew Ahrens 
476714843421SMatthew Ahrens int
476814843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
476914843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
477014843421SMatthew Ahrens {
477114843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
477214843421SMatthew Ahrens 	zfs_useracct_t buf[100];
477370f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
477470f56fa6SYuri Pankov 	int ret;
477514843421SMatthew Ahrens 
477619b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
477714843421SMatthew Ahrens 
477814843421SMatthew Ahrens 	zc.zc_objset_type = type;
477914843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
478014843421SMatthew Ahrens 
478170f56fa6SYuri Pankov 	for (;;) {
478214843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
478314843421SMatthew Ahrens 
478414843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
478570f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
47863b2aab18SMatthew Ahrens 			char errbuf[1024];
478770f56fa6SYuri Pankov 
4788f67950b2SNasf-Fan 			if ((errno == ENOTSUP &&
4789f67950b2SNasf-Fan 			    (type == ZFS_PROP_USEROBJUSED ||
4790f67950b2SNasf-Fan 			    type == ZFS_PROP_GROUPOBJUSED ||
4791f67950b2SNasf-Fan 			    type == ZFS_PROP_USEROBJQUOTA ||
4792f67950b2SNasf-Fan 			    type == ZFS_PROP_GROUPOBJQUOTA ||
4793f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTOBJUSED ||
4794f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTOBJQUOTA ||
4795f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTUSED ||
4796f67950b2SNasf-Fan 			    type == ZFS_PROP_PROJECTQUOTA)))
4797f67950b2SNasf-Fan 				break;
4798f67950b2SNasf-Fan 
479970f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
480070f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
480170f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
480270f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
480370f56fa6SYuri Pankov 		}
480470f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
480514843421SMatthew Ahrens 			break;
480614843421SMatthew Ahrens 
480714843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
480870f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
480970f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
481070f56fa6SYuri Pankov 				return (ret);
481114843421SMatthew Ahrens 			zua++;
481214843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
481314843421SMatthew Ahrens 		}
481414843421SMatthew Ahrens 	}
481514843421SMatthew Ahrens 
481670f56fa6SYuri Pankov 	return (0);
481714843421SMatthew Ahrens }
4818842727c2SChris Kirby 
48193b2aab18SMatthew Ahrens struct holdarg {
48203b2aab18SMatthew Ahrens 	nvlist_t *nvl;
48213b2aab18SMatthew Ahrens 	const char *snapname;
48223b2aab18SMatthew Ahrens 	const char *tag;
48233b2aab18SMatthew Ahrens 	boolean_t recursive;
4824bb6e7075SMatthew Ahrens 	int error;
48253b2aab18SMatthew Ahrens };
48263b2aab18SMatthew Ahrens 
48273b2aab18SMatthew Ahrens static int
48283b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
48293b2aab18SMatthew Ahrens {
48303b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
48319adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
48323b2aab18SMatthew Ahrens 	int rv = 0;
48333b2aab18SMatthew Ahrens 
48343b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
48353b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
48363b2aab18SMatthew Ahrens 
4837a7a845e4SSteven Hartland 	if (lzc_exists(name))
48383b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
48393b2aab18SMatthew Ahrens 
48403b2aab18SMatthew Ahrens 	if (ha->recursive)
48413b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
48423b2aab18SMatthew Ahrens 	zfs_close(zhp);
48433b2aab18SMatthew Ahrens 	return (rv);
48443b2aab18SMatthew Ahrens }
48453b2aab18SMatthew Ahrens 
4846842727c2SChris Kirby int
4847842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4848a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4849842727c2SChris Kirby {
48503b2aab18SMatthew Ahrens 	int ret;
48513b2aab18SMatthew Ahrens 	struct holdarg ha;
4852842727c2SChris Kirby 
48533b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
48543b2aab18SMatthew Ahrens 	ha.snapname = snapname;
48553b2aab18SMatthew Ahrens 	ha.tag = tag;
48563b2aab18SMatthew Ahrens 	ha.recursive = recursive;
48573b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4858013023d4SMartin Matuska 
4859a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4860a7a845e4SSteven Hartland 		char errbuf[1024];
4861a7a845e4SSteven Hartland 
4862013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4863013023d4SMartin Matuska 		ret = ENOENT;
4864a7a845e4SSteven Hartland 		(void) snprintf(errbuf, sizeof (errbuf),
4865a7a845e4SSteven Hartland 		    dgettext(TEXT_DOMAIN,
4866a7a845e4SSteven Hartland 		    "cannot hold snapshot '%s@%s'"),
4867a7a845e4SSteven Hartland 		    zhp->zfs_name, snapname);
4868a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4869013023d4SMartin Matuska 		return (ret);
4870013023d4SMartin Matuska 	}
4871013023d4SMartin Matuska 
4872a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
48733b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4874a7f53a56SChris Kirby 
4875a7a845e4SSteven Hartland 	return (ret);
4876a7a845e4SSteven Hartland }
4877a7a845e4SSteven Hartland 
4878a7a845e4SSteven Hartland int
4879a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4880a7a845e4SSteven Hartland {
4881a7a845e4SSteven Hartland 	int ret;
4882a7a845e4SSteven Hartland 	nvlist_t *errors;
4883a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4884a7a845e4SSteven Hartland 	char errbuf[1024];
4885a7a845e4SSteven Hartland 	nvpair_t *elem;
4886a7a845e4SSteven Hartland 
4887a7a845e4SSteven Hartland 	errors = NULL;
4888a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4889a7a845e4SSteven Hartland 
4890a7a845e4SSteven Hartland 	if (ret == 0) {
4891a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4892a7a845e4SSteven Hartland 		fnvlist_free(errors);
48933b2aab18SMatthew Ahrens 		return (0);
4894a7a845e4SSteven Hartland 	}
4895842727c2SChris Kirby 
4896a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
48973b2aab18SMatthew Ahrens 		/* no hold-specific errors */
48983b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
48993b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
49003b2aab18SMatthew Ahrens 		switch (ret) {
49013b2aab18SMatthew Ahrens 		case ENOTSUP:
49023b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49033b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
49043b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
49053b2aab18SMatthew Ahrens 			break;
49063b2aab18SMatthew Ahrens 		case EINVAL:
49073b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49083b2aab18SMatthew Ahrens 			break;
49093b2aab18SMatthew Ahrens 		default:
49103b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
49113b2aab18SMatthew Ahrens 		}
49123b2aab18SMatthew Ahrens 	}
4913842727c2SChris Kirby 
49143b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
49153b2aab18SMatthew Ahrens 	    elem != NULL;
49163b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
49173b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
49183b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
49193b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
49203b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
492115508ac0SChris Kirby 		case E2BIG:
492215508ac0SChris Kirby 			/*
492315508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
492415508ac0SChris Kirby 			 * prepended. So even if we passed the length check
492515508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
492615508ac0SChris Kirby 			 * up being slightly too long.
492715508ac0SChris Kirby 			 */
49283b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
49293b2aab18SMatthew Ahrens 			break;
4930842727c2SChris Kirby 		case EINVAL:
49313b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49323b2aab18SMatthew Ahrens 			break;
4933842727c2SChris Kirby 		case EEXIST:
49343b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
49353b2aab18SMatthew Ahrens 			break;
4936842727c2SChris Kirby 		default:
49373b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
49383b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4939842727c2SChris Kirby 		}
4940842727c2SChris Kirby 	}
4941842727c2SChris Kirby 
49423b2aab18SMatthew Ahrens 	fnvlist_free(errors);
49433b2aab18SMatthew Ahrens 	return (ret);
49443b2aab18SMatthew Ahrens }
49453b2aab18SMatthew Ahrens 
49463b2aab18SMatthew Ahrens static int
49473b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
49483b2aab18SMatthew Ahrens {
49493b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
49509adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
49513b2aab18SMatthew Ahrens 	int rv = 0;
4952bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
49533b2aab18SMatthew Ahrens 
49543b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
49553b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
49563b2aab18SMatthew Ahrens 
4957bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4958bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4959bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4960bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4961bb6e7075SMatthew Ahrens 	} else {
4962bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4963bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4964bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4965bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
49663b2aab18SMatthew Ahrens 	}
49673b2aab18SMatthew Ahrens 
49683b2aab18SMatthew Ahrens 	if (ha->recursive)
49693b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
49703b2aab18SMatthew Ahrens 	zfs_close(zhp);
49713b2aab18SMatthew Ahrens 	return (rv);
4972842727c2SChris Kirby }
4973842727c2SChris Kirby 
4974842727c2SChris Kirby int
4975842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4976842727c2SChris Kirby     boolean_t recursive)
4977842727c2SChris Kirby {
49783b2aab18SMatthew Ahrens 	int ret;
49793b2aab18SMatthew Ahrens 	struct holdarg ha;
4980a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
49813b2aab18SMatthew Ahrens 	nvpair_t *elem;
4982842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4983013023d4SMartin Matuska 	char errbuf[1024];
4984842727c2SChris Kirby 
49853b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
49863b2aab18SMatthew Ahrens 	ha.snapname = snapname;
49873b2aab18SMatthew Ahrens 	ha.tag = tag;
49883b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4989bb6e7075SMatthew Ahrens 	ha.error = 0;
49903b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4991013023d4SMartin Matuska 
4992a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4993013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4994bb6e7075SMatthew Ahrens 		ret = ha.error;
4995013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4996013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4997013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
4998013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4999bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
5000bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5001bb6e7075SMatthew Ahrens 		} else {
5002bb6e7075SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
5003bb6e7075SMatthew Ahrens 		}
5004013023d4SMartin Matuska 		return (ret);
5005013023d4SMartin Matuska 	}
5006013023d4SMartin Matuska 
50073b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
50083b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
5009842727c2SChris Kirby 
5010a7a845e4SSteven Hartland 	if (ret == 0) {
5011a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
5012a7a845e4SSteven Hartland 		fnvlist_free(errors);
50133b2aab18SMatthew Ahrens 		return (0);
5014a7a845e4SSteven Hartland 	}
50153b2aab18SMatthew Ahrens 
5016a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
50173b2aab18SMatthew Ahrens 		/* no hold-specific errors */
5018842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
50193b2aab18SMatthew Ahrens 		    "cannot release"));
5020842727c2SChris Kirby 		switch (errno) {
5021842727c2SChris Kirby 		case ENOTSUP:
5022842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5023842727c2SChris Kirby 			    "pool must be upgraded"));
50243b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
50253b2aab18SMatthew Ahrens 			break;
50263b2aab18SMatthew Ahrens 		default:
50273b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
50283b2aab18SMatthew Ahrens 		}
50293b2aab18SMatthew Ahrens 	}
50303b2aab18SMatthew Ahrens 
50313b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
50323b2aab18SMatthew Ahrens 	    elem != NULL;
50333b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
50343b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
50353b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
50363b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
50373b2aab18SMatthew Ahrens 		    nvpair_name(elem));
50383b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
50393b2aab18SMatthew Ahrens 		case ESRCH:
50403b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
50413b2aab18SMatthew Ahrens 			break;
5042842727c2SChris Kirby 		case EINVAL:
50433b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
50443b2aab18SMatthew Ahrens 			break;
5045842727c2SChris Kirby 		default:
50463b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
50473b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
5048842727c2SChris Kirby 		}
5049842727c2SChris Kirby 	}
5050842727c2SChris Kirby 
50513b2aab18SMatthew Ahrens 	fnvlist_free(errors);
50523b2aab18SMatthew Ahrens 	return (ret);
5053842727c2SChris Kirby }
5054ca45db41SChris Kirby 
50551af68beaSAlexander Stetsenko int
50561af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
50571af68beaSAlexander Stetsenko {
50581af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
50591af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
50601af68beaSAlexander Stetsenko 	int nvsz = 2048;
50611af68beaSAlexander Stetsenko 	void *nvbuf;
50621af68beaSAlexander Stetsenko 	int err = 0;
50633b2aab18SMatthew Ahrens 	char errbuf[1024];
50641af68beaSAlexander Stetsenko 
50651af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
50661af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
50671af68beaSAlexander Stetsenko 
50681af68beaSAlexander Stetsenko tryagain:
50691af68beaSAlexander Stetsenko 
50701af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
50711af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
50721af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
50731af68beaSAlexander Stetsenko 		goto out;
50741af68beaSAlexander Stetsenko 	}
50751af68beaSAlexander Stetsenko 
50761af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
50771af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
50781af68beaSAlexander Stetsenko 
50799adfa60dSMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
50801af68beaSAlexander Stetsenko 
5081d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
50821af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
50831af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
50841af68beaSAlexander Stetsenko 		    zc.zc_name);
50851af68beaSAlexander Stetsenko 		switch (errno) {
50861af68beaSAlexander Stetsenko 		case ENOMEM:
50871af68beaSAlexander Stetsenko 			free(nvbuf);
50881af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
50891af68beaSAlexander Stetsenko 			goto tryagain;
50901af68beaSAlexander Stetsenko 
50911af68beaSAlexander Stetsenko 		case ENOTSUP:
50921af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50931af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
50941af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
50951af68beaSAlexander Stetsenko 			break;
50961af68beaSAlexander Stetsenko 		case EINVAL:
50971af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
50981af68beaSAlexander Stetsenko 			break;
50991af68beaSAlexander Stetsenko 		case ENOENT:
51001af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
51011af68beaSAlexander Stetsenko 			break;
51021af68beaSAlexander Stetsenko 		default:
51031af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
51041af68beaSAlexander Stetsenko 			break;
51051af68beaSAlexander Stetsenko 		}
51061af68beaSAlexander Stetsenko 	} else {
51071af68beaSAlexander Stetsenko 		/* success */
51081af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
51091af68beaSAlexander Stetsenko 		if (rc) {
51101af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
51111af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
51121af68beaSAlexander Stetsenko 			    zc.zc_name);
51131af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
51141af68beaSAlexander Stetsenko 		}
51151af68beaSAlexander Stetsenko 	}
51161af68beaSAlexander Stetsenko 
51171af68beaSAlexander Stetsenko 	free(nvbuf);
51181af68beaSAlexander Stetsenko out:
51191af68beaSAlexander Stetsenko 	return (err);
51201af68beaSAlexander Stetsenko }
51211af68beaSAlexander Stetsenko 
51221af68beaSAlexander Stetsenko int
51231af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
51241af68beaSAlexander Stetsenko {
51251af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
51261af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
51271af68beaSAlexander Stetsenko 	char *nvbuf;
51283b2aab18SMatthew Ahrens 	char errbuf[1024];
51291af68beaSAlexander Stetsenko 	size_t nvsz;
51301af68beaSAlexander Stetsenko 	int err;
51311af68beaSAlexander Stetsenko 
51321af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
51331af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
51341af68beaSAlexander Stetsenko 
51351af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
51361af68beaSAlexander Stetsenko 	assert(err == 0);
51371af68beaSAlexander Stetsenko 
51381af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
51391af68beaSAlexander Stetsenko 
51401af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
51411af68beaSAlexander Stetsenko 	assert(err == 0);
51421af68beaSAlexander Stetsenko 
51431af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
51441af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
51451af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
51461af68beaSAlexander Stetsenko 
51471af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
51481af68beaSAlexander Stetsenko 
51491af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
51501af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
51511af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
51521af68beaSAlexander Stetsenko 		    zc.zc_name);
51531af68beaSAlexander Stetsenko 		switch (errno) {
51541af68beaSAlexander Stetsenko 		case ENOTSUP:
51551af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51561af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
51571af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51581af68beaSAlexander Stetsenko 			break;
51591af68beaSAlexander Stetsenko 		case EINVAL:
51601af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51611af68beaSAlexander Stetsenko 			break;
51621af68beaSAlexander Stetsenko 		case ENOENT:
51631af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
51641af68beaSAlexander Stetsenko 			break;
51651af68beaSAlexander Stetsenko 		default:
51661af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
51671af68beaSAlexander Stetsenko 			break;
51681af68beaSAlexander Stetsenko 		}
51691af68beaSAlexander Stetsenko 	}
51701af68beaSAlexander Stetsenko 
51711af68beaSAlexander Stetsenko 	free(nvbuf);
51721af68beaSAlexander Stetsenko 
51731af68beaSAlexander Stetsenko 	return (err);
51741af68beaSAlexander Stetsenko }
51751af68beaSAlexander Stetsenko 
51761af68beaSAlexander Stetsenko int
51771af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
51781af68beaSAlexander Stetsenko {
51793b2aab18SMatthew Ahrens 	int err;
51803b2aab18SMatthew Ahrens 	char errbuf[1024];
51811af68beaSAlexander Stetsenko 
51823b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
51831af68beaSAlexander Stetsenko 
51843b2aab18SMatthew Ahrens 	if (err != 0) {
51853b2aab18SMatthew Ahrens 		libzfs_handle_t *hdl = zhp->zfs_hdl;
51861af68beaSAlexander Stetsenko 
51871af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
51881af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
51893b2aab18SMatthew Ahrens 		    zhp->zfs_name);
51903b2aab18SMatthew Ahrens 		switch (err) {
51911af68beaSAlexander Stetsenko 		case ENOTSUP:
51921af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51931af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
51941af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51951af68beaSAlexander Stetsenko 			break;
51961af68beaSAlexander Stetsenko 		case EINVAL:
51971af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51981af68beaSAlexander Stetsenko 			break;
51991af68beaSAlexander Stetsenko 		case ENOENT:
52001af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
52011af68beaSAlexander Stetsenko 			break;
52021af68beaSAlexander Stetsenko 		default:
52031af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
52041af68beaSAlexander Stetsenko 			break;
52051af68beaSAlexander Stetsenko 		}
52061af68beaSAlexander Stetsenko 	}
52071af68beaSAlexander Stetsenko 
52081af68beaSAlexander Stetsenko 	return (err);
52091af68beaSAlexander Stetsenko }
52101af68beaSAlexander Stetsenko 
52113e30c24aSWill Andrews /*
5212b73ccab0SMike Gerdts  * The theory of raidz space accounting
5213b73ccab0SMike Gerdts  *
5214b73ccab0SMike Gerdts  * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5215b73ccab0SMike Gerdts  * will "reference" 128KB, even though it allocates more than that, to store the
5216b73ccab0SMike Gerdts  * parity information (and perhaps skip sectors). This concept of the
5217b73ccab0SMike Gerdts  * "referenced" (and other DMU space accounting) being lower than the allocated
5218b73ccab0SMike Gerdts  * space by a constant factor is called "raidz deflation."
5219b73ccab0SMike Gerdts  *
5220b73ccab0SMike Gerdts  * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5221b73ccab0SMike Gerdts  * block size. However, zvols typically have a much smaller block size (default
5222b73ccab0SMike Gerdts  * 8KB). These smaller blocks may require proportionally much more parity
5223b73ccab0SMike Gerdts  * information (and perhaps skip sectors). In this case, the change to the
5224b73ccab0SMike Gerdts  * "referenced" property may be much more than the logical block size.
5225b73ccab0SMike Gerdts  *
5226b73ccab0SMike Gerdts  * Suppose a raidz vdev has 5 disks with ashift=12.  A 128k block may be written
5227b73ccab0SMike Gerdts  * as follows.
5228b73ccab0SMike Gerdts  *
5229b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5230b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5231b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5232b73ccab0SMike Gerdts  * |  P0   |  D0   |  D8   |  D16  |  D24  |
5233b73ccab0SMike Gerdts  * |  P1   |  D1   |  D9   |  D17  |  D25  |
5234b73ccab0SMike Gerdts  * |  P2   |  D2   |  D10  |  D18  |  D26  |
5235b73ccab0SMike Gerdts  * |  P3   |  D3   |  D11  |  D19  |  D27  |
5236b73ccab0SMike Gerdts  * |  P4   |  D4   |  D12  |  D20  |  D28  |
5237b73ccab0SMike Gerdts  * |  P5   |  D5   |  D13  |  D21  |  D29  |
5238b73ccab0SMike Gerdts  * |  P6   |  D6   |  D14  |  D22  |  D30  |
5239b73ccab0SMike Gerdts  * |  P7   |  D7   |  D15  |  D23  |  D31  |
5240b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5241b73ccab0SMike Gerdts  *
5242b73ccab0SMike Gerdts  * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5243b73ccab0SMike Gerdts  * sectors.  The dataset's referenced will increase by 128k and the pool's
5244b73ccab0SMike Gerdts  * allocated and free properties will be adjusted by 160k.
5245b73ccab0SMike Gerdts  *
5246b73ccab0SMike Gerdts  * A 4k block written to the same raidz vdev will require two 4k sectors.  The
5247b73ccab0SMike Gerdts  * blank cells represent unallocated space.
5248b73ccab0SMike Gerdts  *
5249b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5250b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5251b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5252b73ccab0SMike Gerdts  * |  P0   |  D0   |       |       |       |
5253b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5254b73ccab0SMike Gerdts  *
5255b73ccab0SMike Gerdts  * Above, notice that the 4k block required one sector for parity and another
5256b73ccab0SMike Gerdts  * for data.  vdev_raidz_asize() will return 8k and as such the pool's allocated
5257b73ccab0SMike Gerdts  * and free properties will be adjusted by 8k.  The dataset will not be charged
5258b73ccab0SMike Gerdts  * 8k.  Rather, it will be charged a value that is scaled according to the
5259b73ccab0SMike Gerdts  * overhead of the 128k block on the same vdev.  This 8k allocation will be
5260b73ccab0SMike Gerdts  * charged 8k * 128k / 160k.  128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5261b73ccab0SMike Gerdts  * calculated in the 128k block example above.
5262b73ccab0SMike Gerdts  *
5263b73ccab0SMike Gerdts  * Every raidz allocation is sized to be a multiple of nparity+1 sectors.  That
5264b73ccab0SMike Gerdts  * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5265b73ccab0SMike Gerdts  * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5266b73ccab0SMike Gerdts  * multiple of of 4 sectors.  When a block does not fill the required number of
5267b73ccab0SMike Gerdts  * sectors, skip blocks (sectors) are used.
5268b73ccab0SMike Gerdts  *
5269b73ccab0SMike Gerdts  * An 8k block being written to a raidz vdev may be written as follows:
5270b73ccab0SMike Gerdts  *
5271b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5272b73ccab0SMike Gerdts  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5273b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5274b73ccab0SMike Gerdts  * |  P0   |  D0   |  D1   |  S0   |       |
5275b73ccab0SMike Gerdts  * +-------+-------+-------+-------+-------+
5276b73ccab0SMike Gerdts  *
5277b73ccab0SMike Gerdts  * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5278b73ccab0SMike Gerdts  * added.  For this 8k block, the pool's allocated and free properties are
5279b73ccab0SMike Gerdts  * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5280b73ccab0SMike Gerdts  * 160k.  Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5281b73ccab0SMike Gerdts  * the 128k block example above.
5282b73ccab0SMike Gerdts  *
5283b73ccab0SMike Gerdts  * Compression may lead to a variety of block sizes being written for the same
5284b73ccab0SMike Gerdts  * volume or file.  There is no clear way to reserve just the amount of space
5285b73ccab0SMike Gerdts  * that will be required, so the worst case (no compression) is assumed.
5286b73ccab0SMike Gerdts  * Note that metadata blocks will typically be compressed, so the reservation
5287b73ccab0SMike Gerdts  * size returned by zvol_volsize_to_reservation() will generally be slightly
5288b73ccab0SMike Gerdts  * larger than the maximum that the volume can reference.
5289b73ccab0SMike Gerdts  */
5290b73ccab0SMike Gerdts 
5291b73ccab0SMike Gerdts /*
5292b73ccab0SMike Gerdts  * Derived from function of same name in uts/common/fs/zfs/vdev_raidz.c.
5293b73ccab0SMike Gerdts  * Returns the amount of space (in bytes) that will be allocated for the
5294b73ccab0SMike Gerdts  * specified block size. Note that the "referenced" space accounted will be less
5295b73ccab0SMike Gerdts  * than this, but not necessarily equal to "blksize", due to RAIDZ deflation.
5296b73ccab0SMike Gerdts  */
5297b73ccab0SMike Gerdts static uint64_t
5298b73ccab0SMike Gerdts vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5299b73ccab0SMike Gerdts     uint64_t blksize)
5300b73ccab0SMike Gerdts {
5301b73ccab0SMike Gerdts 	uint64_t asize, ndata;
5302b73ccab0SMike Gerdts 
5303b73ccab0SMike Gerdts 	ASSERT3U(ndisks, >, nparity);
5304b73ccab0SMike Gerdts 	ndata = ndisks - nparity;
5305b73ccab0SMike Gerdts 	asize = ((blksize - 1) >> ashift) + 1;
5306b73ccab0SMike Gerdts 	asize += nparity * ((asize + ndata - 1) / ndata);
5307b73ccab0SMike Gerdts 	asize = roundup(asize, nparity + 1) << ashift;
5308b73ccab0SMike Gerdts 
5309b73ccab0SMike Gerdts 	return (asize);
5310b73ccab0SMike Gerdts }
5311b73ccab0SMike Gerdts 
5312b73ccab0SMike Gerdts /*
5313b73ccab0SMike Gerdts  * Determine how much space will be allocated if it lands on the most space-
5314b73ccab0SMike Gerdts  * inefficient top-level vdev.  Returns the size in bytes required to store one
5315b73ccab0SMike Gerdts  * copy of the volume data.  See theory comment above.
5316b73ccab0SMike Gerdts  */
5317b73ccab0SMike Gerdts static uint64_t
5318b73ccab0SMike Gerdts volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5319b73ccab0SMike Gerdts {
5320b73ccab0SMike Gerdts 	nvlist_t *config, *tree, **vdevs;
5321b73ccab0SMike Gerdts 	uint_t nvdevs, v;
5322b73ccab0SMike Gerdts 	uint64_t ret = 0;
5323b73ccab0SMike Gerdts 
5324b73ccab0SMike Gerdts 	config = zpool_get_config(zhp, NULL);
5325b73ccab0SMike Gerdts 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5326b73ccab0SMike Gerdts 	    nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5327b73ccab0SMike Gerdts 	    &vdevs, &nvdevs) != 0) {
5328b73ccab0SMike Gerdts 		return (nblocks * blksize);
5329b73ccab0SMike Gerdts 	}
5330b73ccab0SMike Gerdts 
5331b73ccab0SMike Gerdts 	for (v = 0; v < nvdevs; v++) {
5332b73ccab0SMike Gerdts 		char *type;
5333b73ccab0SMike Gerdts 		uint64_t nparity, ashift, asize, tsize;
5334b73ccab0SMike Gerdts 		nvlist_t **disks;
5335b73ccab0SMike Gerdts 		uint_t ndisks;
5336b73ccab0SMike Gerdts 		uint64_t volsize;
5337b73ccab0SMike Gerdts 
5338b73ccab0SMike Gerdts 		if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5339b73ccab0SMike Gerdts 		    &type) != 0 || strcmp(type, VDEV_TYPE_RAIDZ) != 0 ||
5340b73ccab0SMike Gerdts 		    nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_NPARITY,
5341b73ccab0SMike Gerdts 		    &nparity) != 0 ||
5342b73ccab0SMike Gerdts 		    nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_ASHIFT,
5343b73ccab0SMike Gerdts 		    &ashift) != 0 ||
5344b73ccab0SMike Gerdts 		    nvlist_lookup_nvlist_array(vdevs[v], ZPOOL_CONFIG_CHILDREN,
5345b73ccab0SMike Gerdts 		    &disks, &ndisks) != 0) {
5346b73ccab0SMike Gerdts 			continue;
5347b73ccab0SMike Gerdts 		}
5348b73ccab0SMike Gerdts 
5349b73ccab0SMike Gerdts 		/* allocation size for the "typical" 128k block */
5350b73ccab0SMike Gerdts 		tsize = vdev_raidz_asize(ndisks, nparity, ashift,
5351b73ccab0SMike Gerdts 		    SPA_OLD_MAXBLOCKSIZE);
5352b73ccab0SMike Gerdts 		/* allocation size for the blksize block */
5353b73ccab0SMike Gerdts 		asize = vdev_raidz_asize(ndisks, nparity, ashift, blksize);
5354b73ccab0SMike Gerdts 
5355b73ccab0SMike Gerdts 		/*
5356b73ccab0SMike Gerdts 		 * Scale this size down as a ratio of 128k / tsize.  See theory
5357b73ccab0SMike Gerdts 		 * statement above.
5358b73ccab0SMike Gerdts 		 */
5359b73ccab0SMike Gerdts 		volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize;
5360b73ccab0SMike Gerdts 		if (volsize > ret) {
5361b73ccab0SMike Gerdts 			ret = volsize;
5362b73ccab0SMike Gerdts 		}
5363b73ccab0SMike Gerdts 	}
5364b73ccab0SMike Gerdts 
5365b73ccab0SMike Gerdts 	if (ret == 0) {
5366b73ccab0SMike Gerdts 		ret = nblocks * blksize;
5367b73ccab0SMike Gerdts 	}
5368b73ccab0SMike Gerdts 
5369b73ccab0SMike Gerdts 	return (ret);
5370b73ccab0SMike Gerdts }
5371b73ccab0SMike Gerdts 
5372b73ccab0SMike Gerdts /*
5373b73ccab0SMike Gerdts  * Convert the zvol's volume size to an appropriate reservation.  See theory
5374b73ccab0SMike Gerdts  * comment above.
5375b73ccab0SMike Gerdts  *
53763e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
5377b73ccab0SMike Gerdts  * suite's shell version in reservation.shlib.
53783e30c24aSWill Andrews  */
5379c1449561SEric Taylor uint64_t
5380b73ccab0SMike Gerdts zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5381b73ccab0SMike Gerdts     nvlist_t *props)
5382c1449561SEric Taylor {
5383c1449561SEric Taylor 	uint64_t numdb;
5384c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
5385c1449561SEric Taylor 	int ncopies;
5386c1449561SEric Taylor 	char *strval;
5387c1449561SEric Taylor 
5388c1449561SEric Taylor 	if (nvlist_lookup_string(props,
5389c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5390c1449561SEric Taylor 		ncopies = atoi(strval);
5391c1449561SEric Taylor 	else
5392c1449561SEric Taylor 		ncopies = 1;
5393c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
5394c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5395c1449561SEric Taylor 	    &volblocksize) != 0)
5396c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5397b73ccab0SMike Gerdts 
5398b73ccab0SMike Gerdts 	nblocks = volsize / volblocksize;
5399b73ccab0SMike Gerdts 	/*
5400b73ccab0SMike Gerdts 	 * Metadata defaults to using 128k blocks, not volblocksize blocks.  For
5401b73ccab0SMike Gerdts 	 * this reason, only the data blocks are scaled based on vdev config.
5402b73ccab0SMike Gerdts 	 */
5403b73ccab0SMike Gerdts 	volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5404b73ccab0SMike Gerdts 
5405c1449561SEric Taylor 	/* start with metadnode L0-L6 */
5406c1449561SEric Taylor 	numdb = 7;
5407c1449561SEric Taylor 	/* calculate number of indirects */
5408c1449561SEric Taylor 	while (nblocks > 1) {
5409c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
5410c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
5411c1449561SEric Taylor 		numdb += nblocks;
5412c1449561SEric Taylor 	}
5413c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5414c1449561SEric Taylor 	volsize *= ncopies;
5415c1449561SEric Taylor 	/*
5416c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5417c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
5418c1449561SEric Taylor 	 * 1100 bytes
5419c1449561SEric Taylor 	 */
5420c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5421c1449561SEric Taylor 	volsize += numdb;
5422c1449561SEric Taylor 	return (volsize);
5423c1449561SEric Taylor }
5424