xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision add927f8c8d101e16c23eb9cd270be4fd7edf7d5)
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.
24a2afb611SJerry Jelinek  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25ad2760acSMatthew Ahrens  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26f0f3ef5aSGarrett D'Amore  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
270d8fa8f8SMartin Matuska  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
29a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
30c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
319fa2266dSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
3288f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33a4b8c9aaSAndrew Stormont  * Copyright 2017 RackTop Systems.
34fa9e4066Sahrens  */
35fa9e4066Sahrens 
36fa9e4066Sahrens #include <ctype.h>
37fa9e4066Sahrens #include <errno.h>
38fa9e4066Sahrens #include <libintl.h>
39fa9e4066Sahrens #include <math.h>
40fa9e4066Sahrens #include <stdio.h>
41fa9e4066Sahrens #include <stdlib.h>
42fa9e4066Sahrens #include <strings.h>
43fa9e4066Sahrens #include <unistd.h>
443cb34c60Sahrens #include <stddef.h>
45fa9e4066Sahrens #include <zone.h>
4699653d4eSeschrock #include <fcntl.h>
47fa9e4066Sahrens #include <sys/mntent.h>
48b12a1c38Slling #include <sys/mount.h>
49ecd6cf80Smarks #include <priv.h>
50ecd6cf80Smarks #include <pwd.h>
51ecd6cf80Smarks #include <grp.h>
52ecd6cf80Smarks #include <stddef.h>
53ecd6cf80Smarks #include <ucred.h>
5414843421SMatthew Ahrens #include <idmap.h>
5514843421SMatthew Ahrens #include <aclutils.h>
563b12c289SMatthew Ahrens #include <directory.h>
57fa9e4066Sahrens 
58c1449561SEric Taylor #include <sys/dnode.h>
59fa9e4066Sahrens #include <sys/spa.h>
60e9dbad6fSeschrock #include <sys/zap.h>
61fa9e4066Sahrens #include <libzfs.h>
62fa9e4066Sahrens 
63fa9e4066Sahrens #include "zfs_namecheck.h"
64fa9e4066Sahrens #include "zfs_prop.h"
65fa9e4066Sahrens #include "libzfs_impl.h"
66ecd6cf80Smarks #include "zfs_deleg.h"
67fa9e4066Sahrens 
6814843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
6914843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
70cdf5b4caSmmusante 
71fa9e4066Sahrens /*
72fa9e4066Sahrens  * Given a single type (not a mask of types), return the type in a human
73fa9e4066Sahrens  * readable form.
74fa9e4066Sahrens  */
75fa9e4066Sahrens const char *
76fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
77fa9e4066Sahrens {
78fa9e4066Sahrens 	switch (type) {
79fa9e4066Sahrens 	case ZFS_TYPE_FILESYSTEM:
80fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
81fa9e4066Sahrens 	case ZFS_TYPE_SNAPSHOT:
82fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
83fa9e4066Sahrens 	case ZFS_TYPE_VOLUME:
84fa9e4066Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
8588f61deeSIgor Kozhukhov 	case ZFS_TYPE_POOL:
8688f61deeSIgor Kozhukhov 		return (dgettext(TEXT_DOMAIN, "pool"));
8788f61deeSIgor Kozhukhov 	case ZFS_TYPE_BOOKMARK:
8888f61deeSIgor Kozhukhov 		return (dgettext(TEXT_DOMAIN, "bookmark"));
8988f61deeSIgor Kozhukhov 	default:
9088f61deeSIgor Kozhukhov 		assert(!"unhandled zfs_type_t");
91fa9e4066Sahrens 	}
92fa9e4066Sahrens 
93fa9e4066Sahrens 	return (NULL);
94fa9e4066Sahrens }
95fa9e4066Sahrens 
96fa9e4066Sahrens /*
97fa9e4066Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
9814843421SMatthew Ahrens  * provide a more meaningful error message.  We call zfs_error_aux() to
9914843421SMatthew Ahrens  * explain exactly why the name was not valid.
100fa9e4066Sahrens  */
10199d5e173STim Haley int
102f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
103f18faf3fSek110237     boolean_t modifying)
104fa9e4066Sahrens {
105fa9e4066Sahrens 	namecheck_err_t why;
106fa9e4066Sahrens 	char what;
107fa9e4066Sahrens 
108edb901aaSMarcel Telka 	if (entity_namecheck(path, &why, &what) != 0) {
10999653d4eSeschrock 		if (hdl != NULL) {
110fa9e4066Sahrens 			switch (why) {
111b81d61a6Slling 			case NAME_ERR_TOOLONG:
11299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11399653d4eSeschrock 				    "name is too long"));
114b81d61a6Slling 				break;
115b81d61a6Slling 
116fa9e4066Sahrens 			case NAME_ERR_LEADING_SLASH:
11799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11899653d4eSeschrock 				    "leading slash in name"));
119fa9e4066Sahrens 				break;
120fa9e4066Sahrens 
121fa9e4066Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
12299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12399653d4eSeschrock 				    "empty component in name"));
124fa9e4066Sahrens 				break;
125fa9e4066Sahrens 
126fa9e4066Sahrens 			case NAME_ERR_TRAILING_SLASH:
12799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12899653d4eSeschrock 				    "trailing slash in name"));
129fa9e4066Sahrens 				break;
130fa9e4066Sahrens 
131fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
13299653d4eSeschrock 				zfs_error_aux(hdl,
133fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
13499653d4eSeschrock 				    "'%c' in name"), what);
135fa9e4066Sahrens 				break;
136fa9e4066Sahrens 
137edb901aaSMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
13899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139edb901aaSMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
140edb901aaSMarcel Telka 				    "name"));
141fa9e4066Sahrens 				break;
1425ad82045Snd150628 
1435ad82045Snd150628 			case NAME_ERR_NOLETTER:
1445ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1455ad82045Snd150628 				    "pool doesn't begin with a letter"));
1465ad82045Snd150628 				break;
1475ad82045Snd150628 
1485ad82045Snd150628 			case NAME_ERR_RESERVED:
1495ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505ad82045Snd150628 				    "name is reserved"));
1515ad82045Snd150628 				break;
1525ad82045Snd150628 
1535ad82045Snd150628 			case NAME_ERR_DISKLIKE:
1545ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1555ad82045Snd150628 				    "reserved disk name"));
1565ad82045Snd150628 				break;
15788f61deeSIgor Kozhukhov 
15888f61deeSIgor Kozhukhov 			default:
15988f61deeSIgor Kozhukhov 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16088f61deeSIgor Kozhukhov 				    "(%d) not defined"), why);
16188f61deeSIgor Kozhukhov 				break;
162fa9e4066Sahrens 			}
163fa9e4066Sahrens 		}
164fa9e4066Sahrens 
165fa9e4066Sahrens 		return (0);
166fa9e4066Sahrens 	}
167fa9e4066Sahrens 
168fa9e4066Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
16999653d4eSeschrock 		if (hdl != NULL)
17099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171edb901aaSMarcel Telka 			    "snapshot delimiter '@' is not expected here"));
172fa9e4066Sahrens 		return (0);
173fa9e4066Sahrens 	}
174fa9e4066Sahrens 
1751d452cf5Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1761d452cf5Sahrens 		if (hdl != NULL)
1771d452cf5Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178d7d4af51Smmusante 			    "missing '@' delimiter in snapshot name"));
1791d452cf5Sahrens 		return (0);
1801d452cf5Sahrens 	}
1811d452cf5Sahrens 
182edb901aaSMarcel Telka 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
183edb901aaSMarcel Telka 		if (hdl != NULL)
184edb901aaSMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185edb901aaSMarcel Telka 			    "bookmark delimiter '#' is not expected here"));
186edb901aaSMarcel Telka 		return (0);
187edb901aaSMarcel Telka 	}
188edb901aaSMarcel Telka 
189edb901aaSMarcel Telka 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
190edb901aaSMarcel Telka 		if (hdl != NULL)
191edb901aaSMarcel Telka 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
192edb901aaSMarcel Telka 			    "missing '#' delimiter in bookmark name"));
193edb901aaSMarcel Telka 		return (0);
194edb901aaSMarcel Telka 	}
195edb901aaSMarcel Telka 
196f18faf3fSek110237 	if (modifying && strchr(path, '%') != NULL) {
197f18faf3fSek110237 		if (hdl != NULL)
198f18faf3fSek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199f18faf3fSek110237 			    "invalid character %c in name"), '%');
200f18faf3fSek110237 		return (0);
201f18faf3fSek110237 	}
202f18faf3fSek110237 
20399653d4eSeschrock 	return (-1);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens int
207fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
208fa9e4066Sahrens {
209e7cbe64fSgw25295 	if (type == ZFS_TYPE_POOL)
210e7cbe64fSgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
211f18faf3fSek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
212fa9e4066Sahrens }
213fa9e4066Sahrens 
214fa9e4066Sahrens /*
215e9dbad6fSeschrock  * This function takes the raw DSL properties, and filters out the user-defined
216e9dbad6fSeschrock  * properties into a separate nvlist.
217e9dbad6fSeschrock  */
218fac3008cSeschrock static nvlist_t *
219fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
220e9dbad6fSeschrock {
221e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
222e9dbad6fSeschrock 	nvpair_t *elem;
223e9dbad6fSeschrock 	nvlist_t *propval;
224fac3008cSeschrock 	nvlist_t *nvl;
225e9dbad6fSeschrock 
226fac3008cSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
227fac3008cSeschrock 		(void) no_memory(hdl);
228fac3008cSeschrock 		return (NULL);
229fac3008cSeschrock 	}
230e9dbad6fSeschrock 
231e9dbad6fSeschrock 	elem = NULL;
232fac3008cSeschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
233e9dbad6fSeschrock 		if (!zfs_prop_user(nvpair_name(elem)))
234e9dbad6fSeschrock 			continue;
235e9dbad6fSeschrock 
236e9dbad6fSeschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
237fac3008cSeschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
238fac3008cSeschrock 			nvlist_free(nvl);
239fac3008cSeschrock 			(void) no_memory(hdl);
240fac3008cSeschrock 			return (NULL);
241fac3008cSeschrock 		}
242e9dbad6fSeschrock 	}
243e9dbad6fSeschrock 
244fac3008cSeschrock 	return (nvl);
245e9dbad6fSeschrock }
246e9dbad6fSeschrock 
24729ab75c9Srm160521 static zpool_handle_t *
24829ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
24929ab75c9Srm160521 {
25029ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
25129ab75c9Srm160521 	zpool_handle_t *zph;
25229ab75c9Srm160521 
25329ab75c9Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
25429ab75c9Srm160521 		if (hdl->libzfs_pool_handles != NULL)
25529ab75c9Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
25629ab75c9Srm160521 		hdl->libzfs_pool_handles = zph;
25729ab75c9Srm160521 	}
25829ab75c9Srm160521 	return (zph);
25929ab75c9Srm160521 }
26029ab75c9Srm160521 
26129ab75c9Srm160521 static zpool_handle_t *
26229ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
26329ab75c9Srm160521 {
26429ab75c9Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
26529ab75c9Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
26629ab75c9Srm160521 
26729ab75c9Srm160521 	while ((zph != NULL) &&
26829ab75c9Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
26929ab75c9Srm160521 		zph = zph->zpool_next;
27029ab75c9Srm160521 	return (zph);
27129ab75c9Srm160521 }
27229ab75c9Srm160521 
27329ab75c9Srm160521 /*
27429ab75c9Srm160521  * Returns a handle to the pool that contains the provided dataset.
27529ab75c9Srm160521  * If a handle to that pool already exists then that handle is returned.
27629ab75c9Srm160521  * Otherwise, a new handle is created and added to the list of handles.
27729ab75c9Srm160521  */
27829ab75c9Srm160521 static zpool_handle_t *
27929ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp)
28029ab75c9Srm160521 {
28129ab75c9Srm160521 	char *pool_name;
28229ab75c9Srm160521 	int len;
28329ab75c9Srm160521 	zpool_handle_t *zph;
28429ab75c9Srm160521 
28578f17100SMatthew Ahrens 	len = strcspn(zhp->zfs_name, "/@#") + 1;
28629ab75c9Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
28729ab75c9Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
28829ab75c9Srm160521 
28929ab75c9Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
29029ab75c9Srm160521 	if (zph == NULL)
29129ab75c9Srm160521 		zph = zpool_add_handle(zhp, pool_name);
29229ab75c9Srm160521 
29329ab75c9Srm160521 	free(pool_name);
29429ab75c9Srm160521 	return (zph);
29529ab75c9Srm160521 }
29629ab75c9Srm160521 
29729ab75c9Srm160521 void
29829ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
29929ab75c9Srm160521 {
30029ab75c9Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
30129ab75c9Srm160521 
30229ab75c9Srm160521 	while (zph != NULL) {
30329ab75c9Srm160521 		next = zph->zpool_next;
30429ab75c9Srm160521 		zpool_close(zph);
30529ab75c9Srm160521 		zph = next;
30629ab75c9Srm160521 	}
30729ab75c9Srm160521 	hdl->libzfs_pool_handles = NULL;
30829ab75c9Srm160521 }
30929ab75c9Srm160521 
310e9dbad6fSeschrock /*
311fa9e4066Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
312fa9e4066Sahrens  */
313fa9e4066Sahrens static int
314ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
315fa9e4066Sahrens {
316e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
317fa9e4066Sahrens 
318ebedde84SEric Taylor 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
319fa9e4066Sahrens 
320ebedde84SEric Taylor 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3217f7322feSeschrock 		if (errno == ENOMEM) {
322ebedde84SEric Taylor 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
32399653d4eSeschrock 				return (-1);
324e9dbad6fSeschrock 			}
3257f7322feSeschrock 		} else {
326fa9e4066Sahrens 			return (-1);
3277f7322feSeschrock 		}
3287f7322feSeschrock 	}
329ebedde84SEric Taylor 	return (0);
330fac3008cSeschrock }
331fac3008cSeschrock 
33292241e0bSTom Erickson /*
33392241e0bSTom Erickson  * Utility function to get the received properties of the given object.
33492241e0bSTom Erickson  */
33592241e0bSTom Erickson static int
33692241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
33792241e0bSTom Erickson {
33892241e0bSTom Erickson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33992241e0bSTom Erickson 	nvlist_t *recvdprops;
34092241e0bSTom Erickson 	zfs_cmd_t zc = { 0 };
34192241e0bSTom Erickson 	int err;
34292241e0bSTom Erickson 
34392241e0bSTom Erickson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
34492241e0bSTom Erickson 		return (-1);
34592241e0bSTom Erickson 
34692241e0bSTom Erickson 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34792241e0bSTom Erickson 
34892241e0bSTom Erickson 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
34992241e0bSTom Erickson 		if (errno == ENOMEM) {
35092241e0bSTom Erickson 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
35192241e0bSTom Erickson 				return (-1);
35292241e0bSTom Erickson 			}
35392241e0bSTom Erickson 		} else {
35492241e0bSTom Erickson 			zcmd_free_nvlists(&zc);
35592241e0bSTom Erickson 			return (-1);
35692241e0bSTom Erickson 		}
35792241e0bSTom Erickson 	}
35892241e0bSTom Erickson 
35992241e0bSTom Erickson 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
36092241e0bSTom Erickson 	zcmd_free_nvlists(&zc);
36192241e0bSTom Erickson 	if (err != 0)
36292241e0bSTom Erickson 		return (-1);
36392241e0bSTom Erickson 
36492241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
36592241e0bSTom Erickson 	zhp->zfs_recvd_props = recvdprops;
36692241e0bSTom Erickson 
36792241e0bSTom Erickson 	return (0);
36892241e0bSTom Erickson }
36992241e0bSTom Erickson 
370ebedde84SEric Taylor static int
371ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
372ebedde84SEric Taylor {
373ebedde84SEric Taylor 	nvlist_t *allprops, *userprops;
374ebedde84SEric Taylor 
375ebedde84SEric Taylor 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
376ebedde84SEric Taylor 
377ebedde84SEric Taylor 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
378ebedde84SEric Taylor 		return (-1);
379ebedde84SEric Taylor 	}
380fac3008cSeschrock 
38114843421SMatthew Ahrens 	/*
38214843421SMatthew Ahrens 	 * XXX Why do we store the user props separately, in addition to
38314843421SMatthew Ahrens 	 * storing them in zfs_props?
38414843421SMatthew Ahrens 	 */
385fac3008cSeschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
386fac3008cSeschrock 		nvlist_free(allprops);
387fac3008cSeschrock 		return (-1);
388fac3008cSeschrock 	}
389fac3008cSeschrock 
39099653d4eSeschrock 	nvlist_free(zhp->zfs_props);
391fac3008cSeschrock 	nvlist_free(zhp->zfs_user_props);
39299653d4eSeschrock 
393fac3008cSeschrock 	zhp->zfs_props = allprops;
394fac3008cSeschrock 	zhp->zfs_user_props = userprops;
39599653d4eSeschrock 
396fa9e4066Sahrens 	return (0);
397fa9e4066Sahrens }
398fa9e4066Sahrens 
399ebedde84SEric Taylor static int
400ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
401ebedde84SEric Taylor {
402ebedde84SEric Taylor 	int rc = 0;
403ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
404ebedde84SEric Taylor 
405ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
406ebedde84SEric Taylor 		return (-1);
407ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) != 0)
408ebedde84SEric Taylor 		rc = -1;
409ebedde84SEric Taylor 	else if (put_stats_zhdl(zhp, &zc) != 0)
410ebedde84SEric Taylor 		rc = -1;
411ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
412ebedde84SEric Taylor 	return (rc);
413ebedde84SEric Taylor }
414ebedde84SEric Taylor 
415fa9e4066Sahrens /*
416fa9e4066Sahrens  * Refresh the properties currently stored in the handle.
417fa9e4066Sahrens  */
418fa9e4066Sahrens void
419fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
420fa9e4066Sahrens {
421fa9e4066Sahrens 	(void) get_stats(zhp);
422fa9e4066Sahrens }
423fa9e4066Sahrens 
424fa9e4066Sahrens /*
425fa9e4066Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
426fa9e4066Sahrens  * zfs_iter_* to create child handles on the fly.
427fa9e4066Sahrens  */
428ebedde84SEric Taylor static int
429ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
430fa9e4066Sahrens {
431503ad85cSMatthew Ahrens 	if (put_stats_zhdl(zhp, zc) != 0)
432ebedde84SEric Taylor 		return (-1);
43331fd60d3Sahrens 
434fa9e4066Sahrens 	/*
435fa9e4066Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
436fa9e4066Sahrens 	 * the high-level type.
437fa9e4066Sahrens 	 */
438a2eea2e1Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
439a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
440a2eea2e1Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
441a2eea2e1Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
442a2eea2e1Sahrens 	else
443a2eea2e1Sahrens 		abort();
444a2eea2e1Sahrens 
445fa9e4066Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
446fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
447fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
448fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
449fa9e4066Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
450fa9e4066Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
451fa9e4066Sahrens 	else
45299653d4eSeschrock 		abort();	/* we should never see any other types */
453fa9e4066Sahrens 
4549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (-1);
4569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
457ebedde84SEric Taylor 	return (0);
458ebedde84SEric Taylor }
459ebedde84SEric Taylor 
460ebedde84SEric Taylor zfs_handle_t *
461ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
462ebedde84SEric Taylor {
463ebedde84SEric Taylor 	zfs_cmd_t zc = { 0 };
464ebedde84SEric Taylor 
465ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
466ebedde84SEric Taylor 
467ebedde84SEric Taylor 	if (zhp == NULL)
468ebedde84SEric Taylor 		return (NULL);
469ebedde84SEric Taylor 
470ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
471ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
472ebedde84SEric Taylor 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
473ebedde84SEric Taylor 		free(zhp);
474ebedde84SEric Taylor 		return (NULL);
475ebedde84SEric Taylor 	}
476ebedde84SEric Taylor 	if (get_stats_ioctl(zhp, &zc) == -1) {
477ebedde84SEric Taylor 		zcmd_free_nvlists(&zc);
478ebedde84SEric Taylor 		free(zhp);
479ebedde84SEric Taylor 		return (NULL);
480ebedde84SEric Taylor 	}
481ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, &zc) == -1) {
482ebedde84SEric Taylor 		free(zhp);
483ebedde84SEric Taylor 		zhp = NULL;
484ebedde84SEric Taylor 	}
485ebedde84SEric Taylor 	zcmd_free_nvlists(&zc);
486ebedde84SEric Taylor 	return (zhp);
487ebedde84SEric Taylor }
488ebedde84SEric Taylor 
48919b94df9SMatthew Ahrens zfs_handle_t *
490ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
491ebedde84SEric Taylor {
492ebedde84SEric Taylor 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
493ebedde84SEric Taylor 
494ebedde84SEric Taylor 	if (zhp == NULL)
495ebedde84SEric Taylor 		return (NULL);
496ebedde84SEric Taylor 
497ebedde84SEric Taylor 	zhp->zfs_hdl = hdl;
498ebedde84SEric Taylor 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
499ebedde84SEric Taylor 	if (make_dataset_handle_common(zhp, zc) == -1) {
500ebedde84SEric Taylor 		free(zhp);
501ebedde84SEric Taylor 		return (NULL);
502ebedde84SEric Taylor 	}
503fa9e4066Sahrens 	return (zhp);
504fa9e4066Sahrens }
505fa9e4066Sahrens 
50619b94df9SMatthew Ahrens zfs_handle_t *
5070d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
5080d8fa8f8SMartin Matuska {
5090d8fa8f8SMartin Matuska 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5100d8fa8f8SMartin Matuska 
5110d8fa8f8SMartin Matuska 	if (zhp == NULL)
5120d8fa8f8SMartin Matuska 		return (NULL);
5130d8fa8f8SMartin Matuska 
5140d8fa8f8SMartin Matuska 	zhp->zfs_hdl = pzhp->zfs_hdl;
5150d8fa8f8SMartin Matuska 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5160d8fa8f8SMartin Matuska 	zhp->zfs_head_type = pzhp->zfs_type;
5170d8fa8f8SMartin Matuska 	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
5180d8fa8f8SMartin Matuska 	zhp->zpool_hdl = zpool_handle(zhp);
5190d8fa8f8SMartin Matuska 	return (zhp);
5200d8fa8f8SMartin Matuska }
5210d8fa8f8SMartin Matuska 
5220d8fa8f8SMartin Matuska zfs_handle_t *
52319b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
52419b94df9SMatthew Ahrens {
52519b94df9SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
52619b94df9SMatthew Ahrens 
52719b94df9SMatthew Ahrens 	if (zhp == NULL)
52819b94df9SMatthew Ahrens 		return (NULL);
52919b94df9SMatthew Ahrens 
53019b94df9SMatthew Ahrens 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
53119b94df9SMatthew Ahrens 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
53219b94df9SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
53319b94df9SMatthew Ahrens 	    sizeof (zhp->zfs_name));
53419b94df9SMatthew Ahrens 	zhp->zfs_type = zhp_orig->zfs_type;
53519b94df9SMatthew Ahrens 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
53619b94df9SMatthew Ahrens 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
53719b94df9SMatthew Ahrens 	if (zhp_orig->zfs_props != NULL) {
53819b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
53919b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54019b94df9SMatthew Ahrens 			zfs_close(zhp);
54119b94df9SMatthew Ahrens 			return (NULL);
54219b94df9SMatthew Ahrens 		}
54319b94df9SMatthew Ahrens 	}
54419b94df9SMatthew Ahrens 	if (zhp_orig->zfs_user_props != NULL) {
54519b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_user_props,
54619b94df9SMatthew Ahrens 		    &zhp->zfs_user_props, 0) != 0) {
54719b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
54819b94df9SMatthew Ahrens 			zfs_close(zhp);
54919b94df9SMatthew Ahrens 			return (NULL);
55019b94df9SMatthew Ahrens 		}
55119b94df9SMatthew Ahrens 	}
55219b94df9SMatthew Ahrens 	if (zhp_orig->zfs_recvd_props != NULL) {
55319b94df9SMatthew Ahrens 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
55419b94df9SMatthew Ahrens 		    &zhp->zfs_recvd_props, 0)) {
55519b94df9SMatthew Ahrens 			(void) no_memory(zhp->zfs_hdl);
55619b94df9SMatthew Ahrens 			zfs_close(zhp);
55719b94df9SMatthew Ahrens 			return (NULL);
55819b94df9SMatthew Ahrens 		}
55919b94df9SMatthew Ahrens 	}
56019b94df9SMatthew Ahrens 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
56119b94df9SMatthew Ahrens 	if (zhp_orig->zfs_mntopts != NULL) {
56219b94df9SMatthew Ahrens 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
56319b94df9SMatthew Ahrens 		    zhp_orig->zfs_mntopts);
56419b94df9SMatthew Ahrens 	}
56519b94df9SMatthew Ahrens 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
56619b94df9SMatthew Ahrens 	return (zhp);
56719b94df9SMatthew Ahrens }
56819b94df9SMatthew Ahrens 
56978f17100SMatthew Ahrens boolean_t
57078f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
57178f17100SMatthew Ahrens {
57278f17100SMatthew Ahrens 	nvlist_t *bmarks;
57378f17100SMatthew Ahrens 	nvlist_t *props;
5749adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
57578f17100SMatthew Ahrens 	char *bmark_name;
57678f17100SMatthew Ahrens 	char *pound;
57778f17100SMatthew Ahrens 	int err;
57878f17100SMatthew Ahrens 	boolean_t rv;
57978f17100SMatthew Ahrens 
58078f17100SMatthew Ahrens 
58178f17100SMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
58278f17100SMatthew Ahrens 	pound = strchr(fsname, '#');
58378f17100SMatthew Ahrens 	if (pound == NULL)
58478f17100SMatthew Ahrens 		return (B_FALSE);
58578f17100SMatthew Ahrens 
58678f17100SMatthew Ahrens 	*pound = '\0';
58778f17100SMatthew Ahrens 	bmark_name = pound + 1;
58878f17100SMatthew Ahrens 	props = fnvlist_alloc();
58978f17100SMatthew Ahrens 	err = lzc_get_bookmarks(fsname, props, &bmarks);
59078f17100SMatthew Ahrens 	nvlist_free(props);
59178f17100SMatthew Ahrens 	if (err != 0) {
59278f17100SMatthew Ahrens 		nvlist_free(bmarks);
59378f17100SMatthew Ahrens 		return (B_FALSE);
59478f17100SMatthew Ahrens 	}
59578f17100SMatthew Ahrens 
59678f17100SMatthew Ahrens 	rv = nvlist_exists(bmarks, bmark_name);
59778f17100SMatthew Ahrens 	nvlist_free(bmarks);
59878f17100SMatthew Ahrens 	return (rv);
59978f17100SMatthew Ahrens }
60078f17100SMatthew Ahrens 
60178f17100SMatthew Ahrens zfs_handle_t *
60278f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
60378f17100SMatthew Ahrens     nvlist_t *bmark_props)
60478f17100SMatthew Ahrens {
60578f17100SMatthew Ahrens 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
60678f17100SMatthew Ahrens 
60778f17100SMatthew Ahrens 	if (zhp == NULL)
60878f17100SMatthew Ahrens 		return (NULL);
60978f17100SMatthew Ahrens 
61078f17100SMatthew Ahrens 	/* Fill in the name. */
61178f17100SMatthew Ahrens 	zhp->zfs_hdl = parent->zfs_hdl;
61278f17100SMatthew Ahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
61378f17100SMatthew Ahrens 
61478f17100SMatthew Ahrens 	/* Set the property lists. */
61578f17100SMatthew Ahrens 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
61678f17100SMatthew Ahrens 		free(zhp);
61778f17100SMatthew Ahrens 		return (NULL);
61878f17100SMatthew Ahrens 	}
61978f17100SMatthew Ahrens 
62078f17100SMatthew Ahrens 	/* Set the types. */
62178f17100SMatthew Ahrens 	zhp->zfs_head_type = parent->zfs_head_type;
62278f17100SMatthew Ahrens 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
62378f17100SMatthew Ahrens 
62478f17100SMatthew Ahrens 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
62578f17100SMatthew Ahrens 		nvlist_free(zhp->zfs_props);
62678f17100SMatthew Ahrens 		free(zhp);
62778f17100SMatthew Ahrens 		return (NULL);
62878f17100SMatthew Ahrens 	}
62978f17100SMatthew Ahrens 
63078f17100SMatthew Ahrens 	return (zhp);
63178f17100SMatthew Ahrens }
63278f17100SMatthew Ahrens 
633edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data {
634edb901aaSMarcel Telka 	const char *path;
635edb901aaSMarcel Telka 	zfs_handle_t *zhp;
636edb901aaSMarcel Telka };
637edb901aaSMarcel Telka 
638edb901aaSMarcel Telka static int
639edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
640edb901aaSMarcel Telka {
641edb901aaSMarcel Telka 	struct zfs_open_bookmarks_cb_data *dp = data;
642edb901aaSMarcel Telka 
643fa9e4066Sahrens 	/*
644edb901aaSMarcel Telka 	 * Is it the one we are looking for?
645edb901aaSMarcel Telka 	 */
646edb901aaSMarcel Telka 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
647edb901aaSMarcel Telka 		/*
648edb901aaSMarcel Telka 		 * We found it.  Save it and let the caller know we are done.
649edb901aaSMarcel Telka 		 */
650edb901aaSMarcel Telka 		dp->zhp = zhp;
651edb901aaSMarcel Telka 		return (EEXIST);
652edb901aaSMarcel Telka 	}
653edb901aaSMarcel Telka 
654edb901aaSMarcel Telka 	/*
655edb901aaSMarcel Telka 	 * Not found.  Close the handle and ask for another one.
656edb901aaSMarcel Telka 	 */
657edb901aaSMarcel Telka 	zfs_close(zhp);
658edb901aaSMarcel Telka 	return (0);
659edb901aaSMarcel Telka }
660edb901aaSMarcel Telka 
661edb901aaSMarcel Telka /*
662edb901aaSMarcel Telka  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
663fa9e4066Sahrens  * argument is a mask of acceptable types.  The function will print an
664fa9e4066Sahrens  * appropriate error message and return NULL if it can't be opened.
665fa9e4066Sahrens  */
666fa9e4066Sahrens zfs_handle_t *
66799653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
668fa9e4066Sahrens {
669fa9e4066Sahrens 	zfs_handle_t *zhp;
67099653d4eSeschrock 	char errbuf[1024];
671edb901aaSMarcel Telka 	char *bookp;
67299653d4eSeschrock 
67399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
67499653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
675fa9e4066Sahrens 
676fa9e4066Sahrens 	/*
67799653d4eSeschrock 	 * Validate the name before we even try to open it.
678fa9e4066Sahrens 	 */
679edb901aaSMarcel Telka 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
68099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
681fa9e4066Sahrens 		return (NULL);
682fa9e4066Sahrens 	}
683fa9e4066Sahrens 
684fa9e4066Sahrens 	/*
685edb901aaSMarcel Telka 	 * Bookmarks needs to be handled separately.
686edb901aaSMarcel Telka 	 */
687edb901aaSMarcel Telka 	bookp = strchr(path, '#');
688edb901aaSMarcel Telka 	if (bookp == NULL) {
689edb901aaSMarcel Telka 		/*
690edb901aaSMarcel Telka 		 * Try to get stats for the dataset, which will tell us if it
691edb901aaSMarcel Telka 		 * exists.
692fa9e4066Sahrens 		 */
693fa9e4066Sahrens 		errno = 0;
69499653d4eSeschrock 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
695ece3d9b3Slling 			(void) zfs_standard_error(hdl, errno, errbuf);
696fa9e4066Sahrens 			return (NULL);
697fa9e4066Sahrens 		}
698edb901aaSMarcel Telka 	} else {
699edb901aaSMarcel Telka 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
700edb901aaSMarcel Telka 		zfs_handle_t *pzhp;
701edb901aaSMarcel Telka 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
702edb901aaSMarcel Telka 
703edb901aaSMarcel Telka 		/*
704edb901aaSMarcel Telka 		 * We need to cut out '#' and everything after '#'
705edb901aaSMarcel Telka 		 * to get the parent dataset name only.
706edb901aaSMarcel Telka 		 */
707edb901aaSMarcel Telka 		assert(bookp - path < sizeof (dsname));
708edb901aaSMarcel Telka 		(void) strncpy(dsname, path, bookp - path);
709edb901aaSMarcel Telka 		dsname[bookp - path] = '\0';
710edb901aaSMarcel Telka 
711edb901aaSMarcel Telka 		/*
712edb901aaSMarcel Telka 		 * Create handle for the parent dataset.
713edb901aaSMarcel Telka 		 */
714edb901aaSMarcel Telka 		errno = 0;
715edb901aaSMarcel Telka 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
716edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
717edb901aaSMarcel Telka 			return (NULL);
718edb901aaSMarcel Telka 		}
719edb901aaSMarcel Telka 
720edb901aaSMarcel Telka 		/*
721edb901aaSMarcel Telka 		 * Iterate bookmarks to find the right one.
722edb901aaSMarcel Telka 		 */
723edb901aaSMarcel Telka 		errno = 0;
724edb901aaSMarcel Telka 		if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
725edb901aaSMarcel Telka 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
726edb901aaSMarcel Telka 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
727edb901aaSMarcel Telka 			zfs_close(pzhp);
728edb901aaSMarcel Telka 			return (NULL);
729edb901aaSMarcel Telka 		}
730edb901aaSMarcel Telka 		if (cb_data.zhp == NULL) {
731edb901aaSMarcel Telka 			(void) zfs_standard_error(hdl, errno, errbuf);
732edb901aaSMarcel Telka 			zfs_close(pzhp);
733edb901aaSMarcel Telka 			return (NULL);
734edb901aaSMarcel Telka 		}
735edb901aaSMarcel Telka 		zhp = cb_data.zhp;
736edb901aaSMarcel Telka 
737edb901aaSMarcel Telka 		/*
738edb901aaSMarcel Telka 		 * Cleanup.
739edb901aaSMarcel Telka 		 */
740edb901aaSMarcel Telka 		zfs_close(pzhp);
741edb901aaSMarcel Telka 	}
742fa9e4066Sahrens 
743fa9e4066Sahrens 	if (!(types & zhp->zfs_type)) {
74499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
74594de1d4cSeschrock 		zfs_close(zhp);
746fa9e4066Sahrens 		return (NULL);
747fa9e4066Sahrens 	}
748fa9e4066Sahrens 
749fa9e4066Sahrens 	return (zhp);
750fa9e4066Sahrens }
751fa9e4066Sahrens 
752fa9e4066Sahrens /*
753fa9e4066Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
754fa9e4066Sahrens  */
755fa9e4066Sahrens void
756fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
757fa9e4066Sahrens {
758fa9e4066Sahrens 	if (zhp->zfs_mntopts)
759fa9e4066Sahrens 		free(zhp->zfs_mntopts);
76099653d4eSeschrock 	nvlist_free(zhp->zfs_props);
761e9dbad6fSeschrock 	nvlist_free(zhp->zfs_user_props);
76292241e0bSTom Erickson 	nvlist_free(zhp->zfs_recvd_props);
763fa9e4066Sahrens 	free(zhp);
764fa9e4066Sahrens }
765fa9e4066Sahrens 
766ebedde84SEric Taylor typedef struct mnttab_node {
767ebedde84SEric Taylor 	struct mnttab mtn_mt;
768ebedde84SEric Taylor 	avl_node_t mtn_node;
769ebedde84SEric Taylor } mnttab_node_t;
770ebedde84SEric Taylor 
771ebedde84SEric Taylor static int
772ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
773ebedde84SEric Taylor {
774ebedde84SEric Taylor 	const mnttab_node_t *mtn1 = arg1;
775ebedde84SEric Taylor 	const mnttab_node_t *mtn2 = arg2;
776ebedde84SEric Taylor 	int rv;
777ebedde84SEric Taylor 
778ebedde84SEric Taylor 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
779ebedde84SEric Taylor 
780ebedde84SEric Taylor 	if (rv == 0)
781ebedde84SEric Taylor 		return (0);
782ebedde84SEric Taylor 	return (rv > 0 ? 1 : -1);
783ebedde84SEric Taylor }
784ebedde84SEric Taylor 
785ebedde84SEric Taylor void
786ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
787ebedde84SEric Taylor {
788ebedde84SEric Taylor 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
789ebedde84SEric Taylor 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
790ebedde84SEric Taylor 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
791b2634b9cSEric Taylor }
792b2634b9cSEric Taylor 
793b2634b9cSEric Taylor void
794b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
795b2634b9cSEric Taylor {
796b2634b9cSEric Taylor 	struct mnttab entry;
797ebedde84SEric Taylor 
798ebedde84SEric Taylor 	rewind(hdl->libzfs_mnttab);
799ebedde84SEric Taylor 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
800ebedde84SEric Taylor 		mnttab_node_t *mtn;
801ebedde84SEric Taylor 
802ebedde84SEric Taylor 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
803ebedde84SEric Taylor 			continue;
804ebedde84SEric Taylor 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
805ebedde84SEric Taylor 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
806ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
807ebedde84SEric Taylor 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
808ebedde84SEric Taylor 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
809ebedde84SEric Taylor 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
810ebedde84SEric Taylor 	}
811ebedde84SEric Taylor }
812ebedde84SEric Taylor 
813ebedde84SEric Taylor void
814ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
815ebedde84SEric Taylor {
816ebedde84SEric Taylor 	void *cookie = NULL;
817ebedde84SEric Taylor 	mnttab_node_t *mtn;
818ebedde84SEric Taylor 
81988f61deeSIgor Kozhukhov 	while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
82088f61deeSIgor Kozhukhov 	    != NULL) {
821ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_special);
822ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mountp);
823ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_fstype);
824ebedde84SEric Taylor 		free(mtn->mtn_mt.mnt_mntopts);
825ebedde84SEric Taylor 		free(mtn);
826ebedde84SEric Taylor 	}
827ebedde84SEric Taylor 	avl_destroy(&hdl->libzfs_mnttab_cache);
828ebedde84SEric Taylor }
829ebedde84SEric Taylor 
830b2634b9cSEric Taylor void
831b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
832b2634b9cSEric Taylor {
833b2634b9cSEric Taylor 	hdl->libzfs_mnttab_enable = enable;
834b2634b9cSEric Taylor }
835b2634b9cSEric Taylor 
836ebedde84SEric Taylor int
837ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
838ebedde84SEric Taylor     struct mnttab *entry)
839ebedde84SEric Taylor {
840ebedde84SEric Taylor 	mnttab_node_t find;
841ebedde84SEric Taylor 	mnttab_node_t *mtn;
842ebedde84SEric Taylor 
843b2634b9cSEric Taylor 	if (!hdl->libzfs_mnttab_enable) {
844b2634b9cSEric Taylor 		struct mnttab srch = { 0 };
845b2634b9cSEric Taylor 
846b2634b9cSEric Taylor 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
847b2634b9cSEric Taylor 			libzfs_mnttab_fini(hdl);
848b2634b9cSEric Taylor 		rewind(hdl->libzfs_mnttab);
849b2634b9cSEric Taylor 		srch.mnt_special = (char *)fsname;
850b2634b9cSEric Taylor 		srch.mnt_fstype = MNTTYPE_ZFS;
851b2634b9cSEric Taylor 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
852b2634b9cSEric Taylor 			return (0);
853b2634b9cSEric Taylor 		else
854b2634b9cSEric Taylor 			return (ENOENT);
855b2634b9cSEric Taylor 	}
856b2634b9cSEric Taylor 
857ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
858b2634b9cSEric Taylor 		libzfs_mnttab_update(hdl);
859ebedde84SEric Taylor 
860ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
861ebedde84SEric Taylor 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
862ebedde84SEric Taylor 	if (mtn) {
863ebedde84SEric Taylor 		*entry = mtn->mtn_mt;
864ebedde84SEric Taylor 		return (0);
865ebedde84SEric Taylor 	}
866ebedde84SEric Taylor 	return (ENOENT);
867ebedde84SEric Taylor }
868ebedde84SEric Taylor 
869ebedde84SEric Taylor void
870ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
871ebedde84SEric Taylor     const char *mountp, const char *mntopts)
872ebedde84SEric Taylor {
873ebedde84SEric Taylor 	mnttab_node_t *mtn;
874ebedde84SEric Taylor 
875ebedde84SEric Taylor 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
876ebedde84SEric Taylor 		return;
877ebedde84SEric Taylor 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
878ebedde84SEric Taylor 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
879ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
880ebedde84SEric Taylor 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
881ebedde84SEric Taylor 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
882ebedde84SEric Taylor 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
883ebedde84SEric Taylor }
884ebedde84SEric Taylor 
885ebedde84SEric Taylor void
886ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
887ebedde84SEric Taylor {
888ebedde84SEric Taylor 	mnttab_node_t find;
889ebedde84SEric Taylor 	mnttab_node_t *ret;
890ebedde84SEric Taylor 
891ebedde84SEric Taylor 	find.mtn_mt.mnt_special = (char *)fsname;
89288f61deeSIgor Kozhukhov 	if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
89388f61deeSIgor Kozhukhov 	    != NULL) {
894ebedde84SEric Taylor 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
895ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_special);
896ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mountp);
897ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_fstype);
898ebedde84SEric Taylor 		free(ret->mtn_mt.mnt_mntopts);
899ebedde84SEric Taylor 		free(ret);
900ebedde84SEric Taylor 	}
901ebedde84SEric Taylor }
902ebedde84SEric Taylor 
9037b97dc1aSrm160521 int
9047b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9057b97dc1aSrm160521 {
90629ab75c9Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9077b97dc1aSrm160521 
9087b97dc1aSrm160521 	if (zpool_handle == NULL)
9097b97dc1aSrm160521 		return (-1);
9107b97dc1aSrm160521 
9117b97dc1aSrm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
9127b97dc1aSrm160521 	    ZPOOL_PROP_VERSION, NULL);
9137b97dc1aSrm160521 	return (0);
9147b97dc1aSrm160521 }
9157b97dc1aSrm160521 
9167b97dc1aSrm160521 /*
9177b97dc1aSrm160521  * The choice of reservation property depends on the SPA version.
9187b97dc1aSrm160521  */
9197b97dc1aSrm160521 static int
9207b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9217b97dc1aSrm160521 {
9227b97dc1aSrm160521 	int spa_version;
9237b97dc1aSrm160521 
9247b97dc1aSrm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
9257b97dc1aSrm160521 		return (-1);
9267b97dc1aSrm160521 
9277b97dc1aSrm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
9287b97dc1aSrm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
9297b97dc1aSrm160521 	else
9307b97dc1aSrm160521 		*resv_prop = ZFS_PROP_RESERVATION;
9317b97dc1aSrm160521 
9327b97dc1aSrm160521 	return (0);
9337b97dc1aSrm160521 }
9347b97dc1aSrm160521 
935b1b8ab34Slling /*
936e9dbad6fSeschrock  * Given an nvlist of properties to set, validates that they are correct, and
937e9dbad6fSeschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
938e9dbad6fSeschrock  * strings.
939fa9e4066Sahrens  */
9400a48a24eStimh nvlist_t *
9410a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
942e9316f76SJoe Stein     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
943e9316f76SJoe Stein     const char *errbuf)
944fa9e4066Sahrens {
945e9dbad6fSeschrock 	nvpair_t *elem;
946e9dbad6fSeschrock 	uint64_t intval;
947e9dbad6fSeschrock 	char *strval;
948990b4856Slling 	zfs_prop_t prop;
949e9dbad6fSeschrock 	nvlist_t *ret;
950da6c28aaSamw 	int chosen_normal = -1;
951da6c28aaSamw 	int chosen_utf = -1;
952990b4856Slling 
953e9dbad6fSeschrock 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
954e9dbad6fSeschrock 		(void) no_memory(hdl);
955e9dbad6fSeschrock 		return (NULL);
956e9dbad6fSeschrock 	}
957fa9e4066Sahrens 
95814843421SMatthew Ahrens 	/*
95914843421SMatthew Ahrens 	 * Make sure this property is valid and applies to this type.
96014843421SMatthew Ahrens 	 */
96114843421SMatthew Ahrens 
962e9dbad6fSeschrock 	elem = NULL;
963e9dbad6fSeschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
964990b4856Slling 		const char *propname = nvpair_name(elem);
96599653d4eSeschrock 
96614843421SMatthew Ahrens 		prop = zfs_name_to_prop(propname);
96714843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
968fa9e4066Sahrens 			/*
96914843421SMatthew Ahrens 			 * This is a user property: make sure it's a
970990b4856Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
971990b4856Slling 			 */
972990b4856Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
973990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974990b4856Slling 				    "'%s' must be a string"), propname);
975990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
976990b4856Slling 				goto error;
977990b4856Slling 			}
978990b4856Slling 
979990b4856Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
980e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981e9dbad6fSeschrock 				    "property name '%s' is too long"),
982e9dbad6fSeschrock 				    propname);
983990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
984e9dbad6fSeschrock 				goto error;
985e9dbad6fSeschrock 			}
986e9dbad6fSeschrock 
987e9dbad6fSeschrock 			(void) nvpair_value_string(elem, &strval);
988e9dbad6fSeschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
989e9dbad6fSeschrock 				(void) no_memory(hdl);
990e9dbad6fSeschrock 				goto error;
991e9dbad6fSeschrock 			}
992e9dbad6fSeschrock 			continue;
993e9dbad6fSeschrock 		}
994fa9e4066Sahrens 
99514843421SMatthew Ahrens 		/*
99614843421SMatthew Ahrens 		 * Currently, only user properties can be modified on
99714843421SMatthew Ahrens 		 * snapshots.
99814843421SMatthew Ahrens 		 */
999bb0ade09Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
1000bb0ade09Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001bb0ade09Sahrens 			    "this property can not be modified for snapshots"));
1002bb0ade09Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1003bb0ade09Sahrens 			goto error;
1004bb0ade09Sahrens 		}
1005bb0ade09Sahrens 
100614843421SMatthew Ahrens 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
100714843421SMatthew Ahrens 			zfs_userquota_prop_t uqtype;
100814843421SMatthew Ahrens 			char newpropname[128];
100914843421SMatthew Ahrens 			char domain[128];
101014843421SMatthew Ahrens 			uint64_t rid;
101114843421SMatthew Ahrens 			uint64_t valary[3];
101214843421SMatthew Ahrens 
101314843421SMatthew Ahrens 			if (userquota_propname_decode(propname, zoned,
101414843421SMatthew Ahrens 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
101514843421SMatthew Ahrens 				zfs_error_aux(hdl,
101614843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN,
101714843421SMatthew Ahrens 				    "'%s' has an invalid user/group name"),
101814843421SMatthew Ahrens 				    propname);
101914843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
102014843421SMatthew Ahrens 				goto error;
102114843421SMatthew Ahrens 			}
102214843421SMatthew Ahrens 
102314843421SMatthew Ahrens 			if (uqtype != ZFS_PROP_USERQUOTA &&
102414843421SMatthew Ahrens 			    uqtype != ZFS_PROP_GROUPQUOTA) {
102514843421SMatthew Ahrens 				zfs_error_aux(hdl,
102614843421SMatthew Ahrens 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
102714843421SMatthew Ahrens 				    propname);
102814843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
102914843421SMatthew Ahrens 				    errbuf);
103014843421SMatthew Ahrens 				goto error;
103114843421SMatthew Ahrens 			}
103214843421SMatthew Ahrens 
103314843421SMatthew Ahrens 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
103414843421SMatthew Ahrens 				(void) nvpair_value_string(elem, &strval);
103514843421SMatthew Ahrens 				if (strcmp(strval, "none") == 0) {
103614843421SMatthew Ahrens 					intval = 0;
103714843421SMatthew Ahrens 				} else if (zfs_nicestrtonum(hdl,
103814843421SMatthew Ahrens 				    strval, &intval) != 0) {
103914843421SMatthew Ahrens 					(void) zfs_error(hdl,
104014843421SMatthew Ahrens 					    EZFS_BADPROP, errbuf);
104114843421SMatthew Ahrens 					goto error;
104214843421SMatthew Ahrens 				}
104314843421SMatthew Ahrens 			} else if (nvpair_type(elem) ==
104414843421SMatthew Ahrens 			    DATA_TYPE_UINT64) {
104514843421SMatthew Ahrens 				(void) nvpair_value_uint64(elem, &intval);
104614843421SMatthew Ahrens 				if (intval == 0) {
104714843421SMatthew Ahrens 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
104814843421SMatthew Ahrens 					    "use 'none' to disable "
104914843421SMatthew Ahrens 					    "userquota/groupquota"));
105014843421SMatthew Ahrens 					goto error;
105114843421SMatthew Ahrens 				}
105214843421SMatthew Ahrens 			} else {
105314843421SMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105414843421SMatthew Ahrens 				    "'%s' must be a number"), propname);
105514843421SMatthew Ahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
105614843421SMatthew Ahrens 				goto error;
105714843421SMatthew Ahrens 			}
105814843421SMatthew Ahrens 
10592d5843dbSMatthew Ahrens 			/*
10602d5843dbSMatthew Ahrens 			 * Encode the prop name as
10612d5843dbSMatthew Ahrens 			 * userquota@<hex-rid>-domain, to make it easy
10622d5843dbSMatthew Ahrens 			 * for the kernel to decode.
10632d5843dbSMatthew Ahrens 			 */
106414843421SMatthew Ahrens 			(void) snprintf(newpropname, sizeof (newpropname),
10652d5843dbSMatthew Ahrens 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10662d5843dbSMatthew Ahrens 			    (longlong_t)rid, domain);
106714843421SMatthew Ahrens 			valary[0] = uqtype;
106814843421SMatthew Ahrens 			valary[1] = rid;
106914843421SMatthew Ahrens 			valary[2] = intval;
107014843421SMatthew Ahrens 			if (nvlist_add_uint64_array(ret, newpropname,
107114843421SMatthew Ahrens 			    valary, 3) != 0) {
107214843421SMatthew Ahrens 				(void) no_memory(hdl);
107314843421SMatthew Ahrens 				goto error;
107414843421SMatthew Ahrens 			}
107514843421SMatthew Ahrens 			continue;
107619b94df9SMatthew Ahrens 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
107719b94df9SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107819b94df9SMatthew Ahrens 			    "'%s' is readonly"),
107919b94df9SMatthew Ahrens 			    propname);
108019b94df9SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
108119b94df9SMatthew Ahrens 			goto error;
108214843421SMatthew Ahrens 		}
108314843421SMatthew Ahrens 
108414843421SMatthew Ahrens 		if (prop == ZPROP_INVAL) {
108514843421SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
108614843421SMatthew Ahrens 			    "invalid property '%s'"), propname);
108714843421SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
108814843421SMatthew Ahrens 			goto error;
108914843421SMatthew Ahrens 		}
109014843421SMatthew Ahrens 
1091e9dbad6fSeschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
1092e9dbad6fSeschrock 			zfs_error_aux(hdl,
1093e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1094e9dbad6fSeschrock 			    "apply to datasets of this type"), propname);
1095e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1096e9dbad6fSeschrock 			goto error;
1097e9dbad6fSeschrock 		}
1098e9dbad6fSeschrock 
1099e9dbad6fSeschrock 		if (zfs_prop_readonly(prop) &&
1100da6c28aaSamw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
1101e9dbad6fSeschrock 			zfs_error_aux(hdl,
1102e9dbad6fSeschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1103e9dbad6fSeschrock 			    propname);
1104e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1105e9dbad6fSeschrock 			goto error;
1106e9dbad6fSeschrock 		}
1107e9dbad6fSeschrock 
1108990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1109990b4856Slling 		    &strval, &intval, errbuf) != 0)
1110e9dbad6fSeschrock 			goto error;
1111e9dbad6fSeschrock 
1112e9dbad6fSeschrock 		/*
1113e9dbad6fSeschrock 		 * Perform some additional checks for specific properties.
1114e9dbad6fSeschrock 		 */
1115e9dbad6fSeschrock 		switch (prop) {
1116e7437265Sahrens 		case ZFS_PROP_VERSION:
1117e7437265Sahrens 		{
1118e7437265Sahrens 			int version;
1119e7437265Sahrens 
1120e7437265Sahrens 			if (zhp == NULL)
1121e7437265Sahrens 				break;
1122e7437265Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1123e7437265Sahrens 			if (intval < version) {
1124e7437265Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125e7437265Sahrens 				    "Can not downgrade; already at version %u"),
1126e7437265Sahrens 				    version);
1127e7437265Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1128e7437265Sahrens 				goto error;
1129e7437265Sahrens 			}
1130e7437265Sahrens 			break;
1131e7437265Sahrens 		}
1132e7437265Sahrens 
1133e9dbad6fSeschrock 		case ZFS_PROP_VOLBLOCKSIZE:
1134b5152584SMatthew Ahrens 		case ZFS_PROP_RECORDSIZE:
1135b5152584SMatthew Ahrens 		{
1136b5152584SMatthew Ahrens 			int maxbs = SPA_MAXBLOCKSIZE;
1137e9316f76SJoe Stein 			if (zpool_hdl != NULL) {
1138e9316f76SJoe Stein 				maxbs = zpool_get_prop_int(zpool_hdl,
1139b5152584SMatthew Ahrens 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1140b5152584SMatthew Ahrens 			}
1141b5152584SMatthew Ahrens 			/*
1142b5152584SMatthew Ahrens 			 * Volumes are limited to a volblocksize of 128KB,
1143b5152584SMatthew Ahrens 			 * because they typically service workloads with
1144b5152584SMatthew Ahrens 			 * small random writes, which incur a large performance
1145b5152584SMatthew Ahrens 			 * penalty with large blocks.
1146b5152584SMatthew Ahrens 			 */
1147b5152584SMatthew Ahrens 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1148b5152584SMatthew Ahrens 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1149b5152584SMatthew Ahrens 			/*
1150b5152584SMatthew Ahrens 			 * The value must be a power of two between
1151b5152584SMatthew Ahrens 			 * SPA_MINBLOCKSIZE and maxbs.
1152b5152584SMatthew Ahrens 			 */
1153e9dbad6fSeschrock 			if (intval < SPA_MINBLOCKSIZE ||
1154b5152584SMatthew Ahrens 			    intval > maxbs || !ISP2(intval)) {
1155e9dbad6fSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1156b5152584SMatthew Ahrens 				    "'%s' must be power of 2 from 512B "
1157b5152584SMatthew Ahrens 				    "to %uKB"), propname, maxbs >> 10);
1158e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1159e9dbad6fSeschrock 				goto error;
1160e9dbad6fSeschrock 			}
1161e9dbad6fSeschrock 			break;
1162b5152584SMatthew Ahrens 		}
11634201a95eSRic Aleshire 		case ZFS_PROP_MLSLABEL:
11644201a95eSRic Aleshire 		{
11654201a95eSRic Aleshire 			/*
11664201a95eSRic Aleshire 			 * Verify the mlslabel string and convert to
11674201a95eSRic Aleshire 			 * internal hex label string.
11684201a95eSRic Aleshire 			 */
11694201a95eSRic Aleshire 
11704201a95eSRic Aleshire 			m_label_t *new_sl;
11714201a95eSRic Aleshire 			char *hex = NULL;	/* internal label string */
11724201a95eSRic Aleshire 
11734201a95eSRic Aleshire 			/* Default value is already OK. */
11744201a95eSRic Aleshire 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
11754201a95eSRic Aleshire 				break;
11764201a95eSRic Aleshire 
11774201a95eSRic Aleshire 			/* Verify the label can be converted to binary form */
11784201a95eSRic Aleshire 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
11794201a95eSRic Aleshire 			    (str_to_label(strval, &new_sl, MAC_LABEL,
11804201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1)) {
11814201a95eSRic Aleshire 				goto badlabel;
11824201a95eSRic Aleshire 			}
11834201a95eSRic Aleshire 
11844201a95eSRic Aleshire 			/* Now translate to hex internal label string */
11854201a95eSRic Aleshire 			if (label_to_str(new_sl, &hex, M_INTERNAL,
11864201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
11874201a95eSRic Aleshire 				if (hex)
11884201a95eSRic Aleshire 					free(hex);
11894201a95eSRic Aleshire 				goto badlabel;
11904201a95eSRic Aleshire 			}
11914201a95eSRic Aleshire 			m_label_free(new_sl);
11924201a95eSRic Aleshire 
11934201a95eSRic Aleshire 			/* If string is already in internal form, we're done. */
11944201a95eSRic Aleshire 			if (strcmp(strval, hex) == 0) {
11954201a95eSRic Aleshire 				free(hex);
11964201a95eSRic Aleshire 				break;
11974201a95eSRic Aleshire 			}
11984201a95eSRic Aleshire 
11994201a95eSRic Aleshire 			/* Replace the label string with the internal form. */
1200569038c9SRic Aleshire 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
12014201a95eSRic Aleshire 			    DATA_TYPE_STRING);
12024201a95eSRic Aleshire 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12034201a95eSRic Aleshire 			    hex) == 0);
12044201a95eSRic Aleshire 			free(hex);
12054201a95eSRic Aleshire 
12064201a95eSRic Aleshire 			break;
12074201a95eSRic Aleshire 
12084201a95eSRic Aleshire badlabel:
12094201a95eSRic Aleshire 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12104201a95eSRic Aleshire 			    "invalid mlslabel '%s'"), strval);
12114201a95eSRic Aleshire 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12124201a95eSRic Aleshire 			m_label_free(new_sl);	/* OK if null */
12134201a95eSRic Aleshire 			goto error;
12144201a95eSRic Aleshire 
12154201a95eSRic Aleshire 		}
12164201a95eSRic Aleshire 
1217e9dbad6fSeschrock 		case ZFS_PROP_MOUNTPOINT:
121889eef05eSrm160521 		{
121989eef05eSrm160521 			namecheck_err_t why;
122089eef05eSrm160521 
1221e9dbad6fSeschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1222e9dbad6fSeschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1223e9dbad6fSeschrock 				break;
1224e9dbad6fSeschrock 
122589eef05eSrm160521 			if (mountpoint_namecheck(strval, &why)) {
122689eef05eSrm160521 				switch (why) {
122789eef05eSrm160521 				case NAME_ERR_LEADING_SLASH:
122889eef05eSrm160521 					zfs_error_aux(hdl,
122989eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
1230e9dbad6fSeschrock 					    "'%s' must be an absolute path, "
1231e9dbad6fSeschrock 					    "'none', or 'legacy'"), propname);
123289eef05eSrm160521 					break;
123389eef05eSrm160521 				case NAME_ERR_TOOLONG:
123489eef05eSrm160521 					zfs_error_aux(hdl,
123589eef05eSrm160521 					    dgettext(TEXT_DOMAIN,
123689eef05eSrm160521 					    "component of '%s' is too long"),
123789eef05eSrm160521 					    propname);
123889eef05eSrm160521 					break;
123988f61deeSIgor Kozhukhov 
124088f61deeSIgor Kozhukhov 				default:
124188f61deeSIgor Kozhukhov 					zfs_error_aux(hdl,
124288f61deeSIgor Kozhukhov 					    dgettext(TEXT_DOMAIN,
124388f61deeSIgor Kozhukhov 					    "(%d) not defined"),
124488f61deeSIgor Kozhukhov 					    why);
124588f61deeSIgor Kozhukhov 					break;
124689eef05eSrm160521 				}
1247e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1248e9dbad6fSeschrock 				goto error;
1249e9dbad6fSeschrock 			}
125089eef05eSrm160521 		}
125189eef05eSrm160521 
1252f3861e1aSahl 			/*FALLTHRU*/
1253e9dbad6fSeschrock 
1254da6c28aaSamw 		case ZFS_PROP_SHARESMB:
1255f3861e1aSahl 		case ZFS_PROP_SHARENFS:
1256e9dbad6fSeschrock 			/*
1257da6c28aaSamw 			 * For the mountpoint and sharenfs or sharesmb
1258da6c28aaSamw 			 * properties, check if it can be set in a
1259da6c28aaSamw 			 * global/non-global zone based on
1260f3861e1aSahl 			 * the zoned property value:
1261fa9e4066Sahrens 			 *
1262fa9e4066Sahrens 			 *		global zone	    non-global zone
1263f3861e1aSahl 			 * --------------------------------------------------
1264fa9e4066Sahrens 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1265fa9e4066Sahrens 			 *		sharenfs (no)	    sharenfs (no)
1266da6c28aaSamw 			 *		sharesmb (no)	    sharesmb (no)
1267fa9e4066Sahrens 			 *
1268fa9e4066Sahrens 			 * zoned=off	mountpoint (yes)	N/A
1269fa9e4066Sahrens 			 *		sharenfs (yes)
1270da6c28aaSamw 			 *		sharesmb (yes)
1271fa9e4066Sahrens 			 */
1272e9dbad6fSeschrock 			if (zoned) {
1273fa9e4066Sahrens 				if (getzoneid() == GLOBAL_ZONEID) {
127499653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1275e9dbad6fSeschrock 					    "'%s' cannot be set on "
1276e9dbad6fSeschrock 					    "dataset in a non-global zone"),
1277e9dbad6fSeschrock 					    propname);
1278e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1279e9dbad6fSeschrock 					    errbuf);
1280e9dbad6fSeschrock 					goto error;
1281da6c28aaSamw 				} else if (prop == ZFS_PROP_SHARENFS ||
1282da6c28aaSamw 				    prop == ZFS_PROP_SHARESMB) {
128399653d4eSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1284e9dbad6fSeschrock 					    "'%s' cannot be set in "
1285e9dbad6fSeschrock 					    "a non-global zone"), propname);
1286e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_ZONED,
1287e9dbad6fSeschrock 					    errbuf);
1288e9dbad6fSeschrock 					goto error;
1289fa9e4066Sahrens 				}
1290fa9e4066Sahrens 			} else if (getzoneid() != GLOBAL_ZONEID) {
1291fa9e4066Sahrens 				/*
1292fa9e4066Sahrens 				 * If zoned property is 'off', this must be in
129314843421SMatthew Ahrens 				 * a global zone. If not, something is wrong.
1294fa9e4066Sahrens 				 */
129599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1296e9dbad6fSeschrock 				    "'%s' cannot be set while dataset "
1297e9dbad6fSeschrock 				    "'zoned' property is set"), propname);
1298e9dbad6fSeschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1299e9dbad6fSeschrock 				goto error;
1300fa9e4066Sahrens 			}
1301f3861e1aSahl 
130267331909Sdougm 			/*
130367331909Sdougm 			 * At this point, it is legitimate to set the
130467331909Sdougm 			 * property. Now we want to make sure that the
130567331909Sdougm 			 * property value is valid if it is sharenfs.
130667331909Sdougm 			 */
1307da6c28aaSamw 			if ((prop == ZFS_PROP_SHARENFS ||
1308da6c28aaSamw 			    prop == ZFS_PROP_SHARESMB) &&
130967331909Sdougm 			    strcmp(strval, "on") != 0 &&
131067331909Sdougm 			    strcmp(strval, "off") != 0) {
1311da6c28aaSamw 				zfs_share_proto_t proto;
1312da6c28aaSamw 
1313da6c28aaSamw 				if (prop == ZFS_PROP_SHARESMB)
1314da6c28aaSamw 					proto = PROTO_SMB;
1315da6c28aaSamw 				else
1316da6c28aaSamw 					proto = PROTO_NFS;
131767331909Sdougm 
131867331909Sdougm 				/*
1319da6c28aaSamw 				 * Must be an valid sharing protocol
1320da6c28aaSamw 				 * option string so init the libshare
1321da6c28aaSamw 				 * in order to enable the parser and
1322da6c28aaSamw 				 * then parse the options. We use the
1323da6c28aaSamw 				 * control API since we don't care about
1324da6c28aaSamw 				 * the current configuration and don't
132567331909Sdougm 				 * want the overhead of loading it
132667331909Sdougm 				 * until we actually do something.
132767331909Sdougm 				 */
132867331909Sdougm 
132967331909Sdougm 				if (zfs_init_libshare(hdl,
133067331909Sdougm 				    SA_INIT_CONTROL_API) != SA_OK) {
1331fac3008cSeschrock 					/*
1332fac3008cSeschrock 					 * An error occurred so we can't do
1333fac3008cSeschrock 					 * anything
1334fac3008cSeschrock 					 */
133567331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133667331909Sdougm 					    "'%s' cannot be set: problem "
133767331909Sdougm 					    "in share initialization"),
133867331909Sdougm 					    propname);
1339fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1340fac3008cSeschrock 					    errbuf);
134167331909Sdougm 					goto error;
134267331909Sdougm 				}
134367331909Sdougm 
1344da6c28aaSamw 				if (zfs_parse_options(strval, proto) != SA_OK) {
134567331909Sdougm 					/*
134667331909Sdougm 					 * There was an error in parsing so
134767331909Sdougm 					 * deal with it by issuing an error
134867331909Sdougm 					 * message and leaving after
134967331909Sdougm 					 * uninitializing the the libshare
135067331909Sdougm 					 * interface.
135167331909Sdougm 					 */
135267331909Sdougm 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135367331909Sdougm 					    "'%s' cannot be set to invalid "
135467331909Sdougm 					    "options"), propname);
1355fac3008cSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1356fac3008cSeschrock 					    errbuf);
135767331909Sdougm 					zfs_uninit_libshare(hdl);
135867331909Sdougm 					goto error;
135967331909Sdougm 				}
136067331909Sdougm 				zfs_uninit_libshare(hdl);
136167331909Sdougm 			}
136267331909Sdougm 
1363f3861e1aSahl 			break;
136488f61deeSIgor Kozhukhov 
1365da6c28aaSamw 		case ZFS_PROP_UTF8ONLY:
1366da6c28aaSamw 			chosen_utf = (int)intval;
1367da6c28aaSamw 			break;
136888f61deeSIgor Kozhukhov 
1369da6c28aaSamw 		case ZFS_PROP_NORMALIZE:
1370da6c28aaSamw 			chosen_normal = (int)intval;
1371da6c28aaSamw 			break;
137288f61deeSIgor Kozhukhov 
137388f61deeSIgor Kozhukhov 		default:
137488f61deeSIgor Kozhukhov 			break;
1375fa9e4066Sahrens 		}
1376fa9e4066Sahrens 
1377e9dbad6fSeschrock 		/*
1378e9dbad6fSeschrock 		 * For changes to existing volumes, we have some additional
1379e9dbad6fSeschrock 		 * checks to enforce.
1380e9dbad6fSeschrock 		 */
1381e9dbad6fSeschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1382e9dbad6fSeschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
1383e9dbad6fSeschrock 			    ZFS_PROP_VOLSIZE);
1384e9dbad6fSeschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
1385e9dbad6fSeschrock 			    ZFS_PROP_VOLBLOCKSIZE);
1386e9dbad6fSeschrock 			char buf[64];
1387e9dbad6fSeschrock 
1388e9dbad6fSeschrock 			switch (prop) {
1389e9dbad6fSeschrock 			case ZFS_PROP_RESERVATION:
1390a9799022Sck153898 			case ZFS_PROP_REFRESERVATION:
1391e9dbad6fSeschrock 				if (intval > volsize) {
1392e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1393e9dbad6fSeschrock 					    "'%s' is greater than current "
1394e9dbad6fSeschrock 					    "volume size"), propname);
1395e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1396e9dbad6fSeschrock 					    errbuf);
1397e9dbad6fSeschrock 					goto error;
1398e9dbad6fSeschrock 				}
1399e9dbad6fSeschrock 				break;
1400e9dbad6fSeschrock 
1401e9dbad6fSeschrock 			case ZFS_PROP_VOLSIZE:
1402e9dbad6fSeschrock 				if (intval % blocksize != 0) {
1403e9dbad6fSeschrock 					zfs_nicenum(blocksize, buf,
1404e9dbad6fSeschrock 					    sizeof (buf));
1405e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1406e9dbad6fSeschrock 					    "'%s' must be a multiple of "
1407e9dbad6fSeschrock 					    "volume block size (%s)"),
1408e9dbad6fSeschrock 					    propname, buf);
1409e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1410e9dbad6fSeschrock 					    errbuf);
1411e9dbad6fSeschrock 					goto error;
1412e9dbad6fSeschrock 				}
1413e9dbad6fSeschrock 
1414e9dbad6fSeschrock 				if (intval == 0) {
1415e9dbad6fSeschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1416e9dbad6fSeschrock 					    "'%s' cannot be zero"),
1417e9dbad6fSeschrock 					    propname);
1418e9dbad6fSeschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
1419e9dbad6fSeschrock 					    errbuf);
1420e9dbad6fSeschrock 					goto error;
1421e9dbad6fSeschrock 				}
1422f3861e1aSahl 				break;
142388f61deeSIgor Kozhukhov 
142488f61deeSIgor Kozhukhov 			default:
142588f61deeSIgor Kozhukhov 				break;
1426e9dbad6fSeschrock 			}
1427e9dbad6fSeschrock 		}
1428e9dbad6fSeschrock 	}
1429e9dbad6fSeschrock 
1430e9dbad6fSeschrock 	/*
1431da6c28aaSamw 	 * If normalization was chosen, but no UTF8 choice was made,
1432da6c28aaSamw 	 * enforce rejection of non-UTF8 names.
1433da6c28aaSamw 	 *
1434da6c28aaSamw 	 * If normalization was chosen, but rejecting non-UTF8 names
1435da6c28aaSamw 	 * was explicitly not chosen, it is an error.
1436da6c28aaSamw 	 */
1437de8267e0Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
1438da6c28aaSamw 		if (nvlist_add_uint64(ret,
1439da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1440da6c28aaSamw 			(void) no_memory(hdl);
1441da6c28aaSamw 			goto error;
1442da6c28aaSamw 		}
1443de8267e0Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1444da6c28aaSamw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1445da6c28aaSamw 		    "'%s' must be set 'on' if normalization chosen"),
1446da6c28aaSamw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1447da6c28aaSamw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1448da6c28aaSamw 		goto error;
1449da6c28aaSamw 	}
1450e9dbad6fSeschrock 	return (ret);
1451e9dbad6fSeschrock 
1452e9dbad6fSeschrock error:
1453e9dbad6fSeschrock 	nvlist_free(ret);
1454e9dbad6fSeschrock 	return (NULL);
1455e9dbad6fSeschrock }
1456e9dbad6fSeschrock 
145736db6475SEric Taylor int
145836db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
145936db6475SEric Taylor {
146036db6475SEric Taylor 	uint64_t old_volsize;
146136db6475SEric Taylor 	uint64_t new_volsize;
146236db6475SEric Taylor 	uint64_t old_reservation;
146336db6475SEric Taylor 	uint64_t new_reservation;
146436db6475SEric Taylor 	zfs_prop_t resv_prop;
1465c61ea566SGeorge Wilson 	nvlist_t *props;
146636db6475SEric Taylor 
146736db6475SEric Taylor 	/*
146836db6475SEric Taylor 	 * If this is an existing volume, and someone is setting the volsize,
146936db6475SEric Taylor 	 * make sure that it matches the reservation, or add it if necessary.
147036db6475SEric Taylor 	 */
147136db6475SEric Taylor 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
147236db6475SEric Taylor 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
147336db6475SEric Taylor 		return (-1);
147436db6475SEric Taylor 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1475c61ea566SGeorge Wilson 
1476c61ea566SGeorge Wilson 	props = fnvlist_alloc();
1477c61ea566SGeorge Wilson 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1478c61ea566SGeorge Wilson 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1479c61ea566SGeorge Wilson 
1480c61ea566SGeorge Wilson 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1481c61ea566SGeorge Wilson 	    old_reservation) || nvlist_exists(nvl,
1482c61ea566SGeorge Wilson 	    zfs_prop_to_name(resv_prop))) {
1483c61ea566SGeorge Wilson 		fnvlist_free(props);
148436db6475SEric Taylor 		return (0);
148536db6475SEric Taylor 	}
148636db6475SEric Taylor 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1487c61ea566SGeorge Wilson 	    &new_volsize) != 0) {
1488c61ea566SGeorge Wilson 		fnvlist_free(props);
148936db6475SEric Taylor 		return (-1);
1490c61ea566SGeorge Wilson 	}
1491c61ea566SGeorge Wilson 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1492c61ea566SGeorge Wilson 	fnvlist_free(props);
1493c61ea566SGeorge Wilson 
149436db6475SEric Taylor 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
149536db6475SEric Taylor 	    new_reservation) != 0) {
149636db6475SEric Taylor 		(void) no_memory(zhp->zfs_hdl);
149736db6475SEric Taylor 		return (-1);
149836db6475SEric Taylor 	}
149936db6475SEric Taylor 	return (1);
150036db6475SEric Taylor }
150136db6475SEric Taylor 
150292241e0bSTom Erickson void
150392241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
150492241e0bSTom Erickson     char *errbuf)
150592241e0bSTom Erickson {
150692241e0bSTom Erickson 	switch (err) {
150792241e0bSTom Erickson 
150892241e0bSTom Erickson 	case ENOSPC:
150992241e0bSTom Erickson 		/*
151092241e0bSTom Erickson 		 * For quotas and reservations, ENOSPC indicates
151192241e0bSTom Erickson 		 * something different; setting a quota or reservation
151292241e0bSTom Erickson 		 * doesn't use any disk space.
151392241e0bSTom Erickson 		 */
151492241e0bSTom Erickson 		switch (prop) {
151592241e0bSTom Erickson 		case ZFS_PROP_QUOTA:
151692241e0bSTom Erickson 		case ZFS_PROP_REFQUOTA:
151792241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151892241e0bSTom Erickson 			    "size is less than current used or "
151992241e0bSTom Erickson 			    "reserved space"));
152092241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
152192241e0bSTom Erickson 			break;
152292241e0bSTom Erickson 
152392241e0bSTom Erickson 		case ZFS_PROP_RESERVATION:
152492241e0bSTom Erickson 		case ZFS_PROP_REFRESERVATION:
152592241e0bSTom Erickson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152692241e0bSTom Erickson 			    "size is greater than available space"));
152792241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
152892241e0bSTom Erickson 			break;
152992241e0bSTom Erickson 
153092241e0bSTom Erickson 		default:
153192241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
153292241e0bSTom Erickson 			break;
153392241e0bSTom Erickson 		}
153492241e0bSTom Erickson 		break;
153592241e0bSTom Erickson 
153692241e0bSTom Erickson 	case EBUSY:
153792241e0bSTom Erickson 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
153892241e0bSTom Erickson 		break;
153992241e0bSTom Erickson 
154092241e0bSTom Erickson 	case EROFS:
154192241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
154292241e0bSTom Erickson 		break;
154392241e0bSTom Erickson 
15446fdcb3d1SWill Andrews 	case E2BIG:
15456fdcb3d1SWill Andrews 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15466fdcb3d1SWill Andrews 		    "property value too long"));
15476fdcb3d1SWill Andrews 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
15486fdcb3d1SWill Andrews 		break;
15496fdcb3d1SWill Andrews 
155092241e0bSTom Erickson 	case ENOTSUP:
155192241e0bSTom Erickson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155292241e0bSTom Erickson 		    "pool and or dataset must be upgraded to set this "
155392241e0bSTom Erickson 		    "property or value"));
155492241e0bSTom Erickson 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
155592241e0bSTom Erickson 		break;
155692241e0bSTom Erickson 
155792241e0bSTom Erickson 	case ERANGE:
1558b5152584SMatthew Ahrens 		if (prop == ZFS_PROP_COMPRESSION ||
1559b5152584SMatthew Ahrens 		    prop == ZFS_PROP_RECORDSIZE) {
156092241e0bSTom Erickson 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156192241e0bSTom Erickson 			    "property setting is not allowed on "
156292241e0bSTom Erickson 			    "bootable datasets"));
156392241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
156445818ee1SMatthew Ahrens 		} else if (prop == ZFS_PROP_CHECKSUM ||
156545818ee1SMatthew Ahrens 		    prop == ZFS_PROP_DEDUP) {
156645818ee1SMatthew Ahrens 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156745818ee1SMatthew Ahrens 			    "property setting is not allowed on "
156845818ee1SMatthew Ahrens 			    "root pools"));
156945818ee1SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
157092241e0bSTom Erickson 		} else {
157192241e0bSTom Erickson 			(void) zfs_standard_error(hdl, err, errbuf);
157292241e0bSTom Erickson 		}
157392241e0bSTom Erickson 		break;
157492241e0bSTom Erickson 
1575ab003da8SJim Dunham 	case EINVAL:
1576ab003da8SJim Dunham 		if (prop == ZPROP_INVAL) {
1577ab003da8SJim Dunham 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1578ab003da8SJim Dunham 		} else {
1579ab003da8SJim Dunham 			(void) zfs_standard_error(hdl, err, errbuf);
1580ab003da8SJim Dunham 		}
1581ab003da8SJim Dunham 		break;
1582ab003da8SJim Dunham 
158392241e0bSTom Erickson 	case EOVERFLOW:
158492241e0bSTom Erickson 		/*
158592241e0bSTom Erickson 		 * This platform can't address a volume this big.
158692241e0bSTom Erickson 		 */
158792241e0bSTom Erickson #ifdef _ILP32
158892241e0bSTom Erickson 		if (prop == ZFS_PROP_VOLSIZE) {
158992241e0bSTom Erickson 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
159092241e0bSTom Erickson 			break;
159192241e0bSTom Erickson 		}
159292241e0bSTom Erickson #endif
159392241e0bSTom Erickson 		/* FALLTHROUGH */
159492241e0bSTom Erickson 	default:
159592241e0bSTom Erickson 		(void) zfs_standard_error(hdl, err, errbuf);
159692241e0bSTom Erickson 	}
159792241e0bSTom Erickson }
159892241e0bSTom Erickson 
1599e9dbad6fSeschrock /*
1600e9dbad6fSeschrock  * Given a property name and value, set the property for the given dataset.
1601e9dbad6fSeschrock  */
1602e9dbad6fSeschrock int
1603e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1604e9dbad6fSeschrock {
1605e9dbad6fSeschrock 	int ret = -1;
1606e9dbad6fSeschrock 	char errbuf[1024];
1607e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
160830925561SChris Williamson 	nvlist_t *nvl = NULL;
1609e9dbad6fSeschrock 
1610e9dbad6fSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
1611e9dbad6fSeschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1612e9dbad6fSeschrock 	    zhp->zfs_name);
1613e9dbad6fSeschrock 
1614e9dbad6fSeschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1615e9dbad6fSeschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
1616e9dbad6fSeschrock 		(void) no_memory(hdl);
1617e9dbad6fSeschrock 		goto error;
1618e9dbad6fSeschrock 	}
1619e9dbad6fSeschrock 
162030925561SChris Williamson 	ret = zfs_prop_set_list(zhp, nvl);
162130925561SChris Williamson 
162230925561SChris Williamson error:
162330925561SChris Williamson 	nvlist_free(nvl);
162430925561SChris Williamson 	return (ret);
162530925561SChris Williamson }
162630925561SChris Williamson 
162730925561SChris Williamson 
162830925561SChris Williamson 
162930925561SChris Williamson /*
163030925561SChris Williamson  * Given an nvlist of property names and values, set the properties for the
163130925561SChris Williamson  * given dataset.
163230925561SChris Williamson  */
163330925561SChris Williamson int
163430925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
163530925561SChris Williamson {
163630925561SChris Williamson 	zfs_cmd_t zc = { 0 };
163730925561SChris Williamson 	int ret = -1;
163830925561SChris Williamson 	prop_changelist_t **cls = NULL;
163930925561SChris Williamson 	int cl_idx;
164030925561SChris Williamson 	char errbuf[1024];
164130925561SChris Williamson 	libzfs_handle_t *hdl = zhp->zfs_hdl;
164230925561SChris Williamson 	nvlist_t *nvl;
164330925561SChris Williamson 	int nvl_len;
1644f83b46baSPaul Dagnelie 	int added_resv = 0;
164530925561SChris Williamson 
164630925561SChris Williamson 	(void) snprintf(errbuf, sizeof (errbuf),
164730925561SChris Williamson 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
164830925561SChris Williamson 	    zhp->zfs_name);
164930925561SChris Williamson 
165030925561SChris Williamson 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1651e9316f76SJoe Stein 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1652e9316f76SJoe Stein 	    errbuf)) == NULL)
1653e9dbad6fSeschrock 		goto error;
1654990b4856Slling 
165530925561SChris Williamson 	/*
165630925561SChris Williamson 	 * We have to check for any extra properties which need to be added
165730925561SChris Williamson 	 * before computing the length of the nvlist.
165830925561SChris Williamson 	 */
165930925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
166030925561SChris Williamson 	    elem != NULL;
166130925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
166230925561SChris Williamson 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
166330925561SChris Williamson 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
166430925561SChris Williamson 			goto error;
166530925561SChris Williamson 		}
166630925561SChris Williamson 	}
166730925561SChris Williamson 	/*
166830925561SChris Williamson 	 * Check how many properties we're setting and allocate an array to
166930925561SChris Williamson 	 * store changelist pointers for postfix().
167030925561SChris Williamson 	 */
167130925561SChris Williamson 	nvl_len = 0;
167230925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
167330925561SChris Williamson 	    elem != NULL;
167430925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem))
167530925561SChris Williamson 		nvl_len++;
167630925561SChris Williamson 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
167730925561SChris Williamson 		goto error;
1678e9dbad6fSeschrock 
167930925561SChris Williamson 	cl_idx = 0;
168030925561SChris Williamson 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
168130925561SChris Williamson 	    elem != NULL;
168230925561SChris Williamson 	    elem = nvlist_next_nvpair(nvl, elem)) {
1683e9dbad6fSeschrock 
168430925561SChris Williamson 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
168530925561SChris Williamson 
168630925561SChris Williamson 		assert(cl_idx < nvl_len);
168730925561SChris Williamson 		/*
168830925561SChris Williamson 		 * We don't want to unmount & remount the dataset when changing
168930925561SChris Williamson 		 * its canmount property to 'on' or 'noauto'.  We only use
169030925561SChris Williamson 		 * the changelist logic to unmount when setting canmount=off.
169130925561SChris Williamson 		 */
1692c079fa4dSAndriy Gapon 		if (prop != ZFS_PROP_CANMOUNT ||
1693c079fa4dSAndriy Gapon 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1694c079fa4dSAndriy Gapon 		    zfs_is_mounted(zhp, NULL))) {
169530925561SChris Williamson 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
169630925561SChris Williamson 			if (cls[cl_idx] == NULL)
169736db6475SEric Taylor 				goto error;
169836db6475SEric Taylor 		}
169936db6475SEric Taylor 
170030925561SChris Williamson 		if (prop == ZFS_PROP_MOUNTPOINT &&
170130925561SChris Williamson 		    changelist_haszonedchild(cls[cl_idx])) {
170299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1703fa9e4066Sahrens 			    "child dataset with inherited mountpoint is used "
170499653d4eSeschrock 			    "in a non-global zone"));
170599653d4eSeschrock 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1706fa9e4066Sahrens 			goto error;
1707fa9e4066Sahrens 		}
1708fa9e4066Sahrens 
170930925561SChris Williamson 		if (cls[cl_idx] != NULL &&
171030925561SChris Williamson 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1711fa9e4066Sahrens 			goto error;
1712fa9e4066Sahrens 
171330925561SChris Williamson 		cl_idx++;
171430925561SChris Williamson 	}
171530925561SChris Williamson 	assert(cl_idx == nvl_len);
171630925561SChris Williamson 
1717fa9e4066Sahrens 	/*
171830925561SChris Williamson 	 * Execute the corresponding ioctl() to set this list of properties.
1719fa9e4066Sahrens 	 */
1720fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1721fa9e4066Sahrens 
172230925561SChris Williamson 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
172330925561SChris Williamson 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1724e9dbad6fSeschrock 		goto error;
1725e9dbad6fSeschrock 
1726ecd6cf80Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1727743a77edSAlan Wright 
1728fa9e4066Sahrens 	if (ret != 0) {
172930925561SChris Williamson 		/* Get the list of unset properties back and report them. */
173030925561SChris Williamson 		nvlist_t *errorprops = NULL;
173130925561SChris Williamson 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
173230925561SChris Williamson 			goto error;
173330925561SChris Williamson 		for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
173430925561SChris Williamson 		    elem != NULL;
173530925561SChris Williamson 		    elem = nvlist_next_nvpair(nvl, elem)) {
173630925561SChris Williamson 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
173792241e0bSTom Erickson 			zfs_setprop_error(hdl, prop, errno, errbuf);
173830925561SChris Williamson 		}
173930925561SChris Williamson 		nvlist_free(errorprops);
174030925561SChris Williamson 
174136db6475SEric Taylor 		if (added_resv && errno == ENOSPC) {
174236db6475SEric Taylor 			/* clean up the volsize property we tried to set */
174336db6475SEric Taylor 			uint64_t old_volsize = zfs_prop_get_int(zhp,
174436db6475SEric Taylor 			    ZFS_PROP_VOLSIZE);
174536db6475SEric Taylor 			nvlist_free(nvl);
174630925561SChris Williamson 			nvl = NULL;
174736db6475SEric Taylor 			zcmd_free_nvlists(&zc);
174830925561SChris Williamson 
174936db6475SEric Taylor 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
175036db6475SEric Taylor 				goto error;
175136db6475SEric Taylor 			if (nvlist_add_uint64(nvl,
175236db6475SEric Taylor 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
175336db6475SEric Taylor 			    old_volsize) != 0)
175436db6475SEric Taylor 				goto error;
175536db6475SEric Taylor 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
175636db6475SEric Taylor 				goto error;
175736db6475SEric Taylor 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
175836db6475SEric Taylor 		}
1759fa9e4066Sahrens 	} else {
176030925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
176130925561SChris Williamson 			if (cls[cl_idx] != NULL) {
176230925561SChris Williamson 				int clp_err = changelist_postfix(cls[cl_idx]);
176330925561SChris Williamson 				if (clp_err != 0)
176430925561SChris Williamson 					ret = clp_err;
176530925561SChris Williamson 			}
176630925561SChris Williamson 		}
1767a227b7f4Shs24103 
1768fa9e4066Sahrens 		/*
1769fa9e4066Sahrens 		 * Refresh the statistics so the new property value
1770fa9e4066Sahrens 		 * is reflected.
1771fa9e4066Sahrens 		 */
1772a227b7f4Shs24103 		if (ret == 0)
1773fa9e4066Sahrens 			(void) get_stats(zhp);
1774fa9e4066Sahrens 	}
1775fa9e4066Sahrens 
1776fa9e4066Sahrens error:
1777e9dbad6fSeschrock 	nvlist_free(nvl);
1778e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
177930925561SChris Williamson 	if (cls != NULL) {
178030925561SChris Williamson 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
178130925561SChris Williamson 			if (cls[cl_idx] != NULL)
178230925561SChris Williamson 				changelist_free(cls[cl_idx]);
178330925561SChris Williamson 		}
178430925561SChris Williamson 		free(cls);
178530925561SChris Williamson 	}
1786fa9e4066Sahrens 	return (ret);
1787fa9e4066Sahrens }
1788fa9e4066Sahrens 
1789fa9e4066Sahrens /*
179092241e0bSTom Erickson  * Given a property, inherit the value from the parent dataset, or if received
179192241e0bSTom Erickson  * is TRUE, revert to the received value, if any.
1792fa9e4066Sahrens  */
1793fa9e4066Sahrens int
179492241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1795fa9e4066Sahrens {
1796fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1797fa9e4066Sahrens 	int ret;
1798fa9e4066Sahrens 	prop_changelist_t *cl;
179999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
180099653d4eSeschrock 	char errbuf[1024];
1801e9dbad6fSeschrock 	zfs_prop_t prop;
180299653d4eSeschrock 
180399653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
180499653d4eSeschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1805fa9e4066Sahrens 
180692241e0bSTom Erickson 	zc.zc_cookie = received;
1807990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1808e9dbad6fSeschrock 		/*
1809e9dbad6fSeschrock 		 * For user properties, the amount of work we have to do is very
1810e9dbad6fSeschrock 		 * small, so just do it here.
1811e9dbad6fSeschrock 		 */
1812e9dbad6fSeschrock 		if (!zfs_prop_user(propname)) {
1813e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1814e9dbad6fSeschrock 			    "invalid property"));
1815e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1816e9dbad6fSeschrock 		}
1817e9dbad6fSeschrock 
1818e9dbad6fSeschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1819e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1820e9dbad6fSeschrock 
1821e45ce728Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1822e9dbad6fSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
1823e9dbad6fSeschrock 
1824e9dbad6fSeschrock 		return (0);
1825e9dbad6fSeschrock 	}
1826e9dbad6fSeschrock 
1827fa9e4066Sahrens 	/*
1828fa9e4066Sahrens 	 * Verify that this property is inheritable.
1829fa9e4066Sahrens 	 */
183099653d4eSeschrock 	if (zfs_prop_readonly(prop))
183199653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1832fa9e4066Sahrens 
183392241e0bSTom Erickson 	if (!zfs_prop_inheritable(prop) && !received)
183499653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1835fa9e4066Sahrens 
1836fa9e4066Sahrens 	/*
1837fa9e4066Sahrens 	 * Check to see if the value applies to this type
1838fa9e4066Sahrens 	 */
183999653d4eSeschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
184099653d4eSeschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1841fa9e4066Sahrens 
1842bf7c2d40Srm160521 	/*
184336db6475SEric Taylor 	 * Normalize the name, to get rid of shorthand abbreviations.
1844bf7c2d40Srm160521 	 */
1845bf7c2d40Srm160521 	propname = zfs_prop_to_name(prop);
1846fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1847e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1848fa9e4066Sahrens 
1849fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1850fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
185199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185299653d4eSeschrock 		    "dataset is used in a non-global zone"));
185399653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1854fa9e4066Sahrens 	}
1855fa9e4066Sahrens 
1856fa9e4066Sahrens 	/*
1857fa9e4066Sahrens 	 * Determine datasets which will be affected by this change, if any.
1858fa9e4066Sahrens 	 */
18590069fd67STim Haley 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1860fa9e4066Sahrens 		return (-1);
1861fa9e4066Sahrens 
1862fa9e4066Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
186399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
186499653d4eSeschrock 		    "child dataset with inherited mountpoint is used "
186599653d4eSeschrock 		    "in a non-global zone"));
186699653d4eSeschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1867fa9e4066Sahrens 		goto error;
1868fa9e4066Sahrens 	}
1869fa9e4066Sahrens 
1870fa9e4066Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1871fa9e4066Sahrens 		goto error;
1872fa9e4066Sahrens 
1873e45ce728Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
187499653d4eSeschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1875fa9e4066Sahrens 	} else {
1876fa9e4066Sahrens 
1877efc555ebSnd150628 		if ((ret = changelist_postfix(cl)) != 0)
1878fa9e4066Sahrens 			goto error;
1879fa9e4066Sahrens 
1880fa9e4066Sahrens 		/*
1881fa9e4066Sahrens 		 * Refresh the statistics so the new property is reflected.
1882fa9e4066Sahrens 		 */
1883fa9e4066Sahrens 		(void) get_stats(zhp);
1884fa9e4066Sahrens 	}
1885fa9e4066Sahrens 
1886fa9e4066Sahrens error:
1887fa9e4066Sahrens 	changelist_free(cl);
1888fa9e4066Sahrens 	return (ret);
1889fa9e4066Sahrens }
1890fa9e4066Sahrens 
1891fa9e4066Sahrens /*
18927f7322feSeschrock  * True DSL properties are stored in an nvlist.  The following two functions
18937f7322feSeschrock  * extract them appropriately.
18947f7322feSeschrock  */
18957f7322feSeschrock static uint64_t
18967f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
18977f7322feSeschrock {
18987f7322feSeschrock 	nvlist_t *nv;
18997f7322feSeschrock 	uint64_t value;
19007f7322feSeschrock 
1901a2eea2e1Sahrens 	*source = NULL;
19027f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19037f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
1904990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1905990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19067f7322feSeschrock 	} else {
19072e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
19082e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
19097f7322feSeschrock 		value = zfs_prop_default_numeric(prop);
19107f7322feSeschrock 		*source = "";
19117f7322feSeschrock 	}
19127f7322feSeschrock 
19137f7322feSeschrock 	return (value);
19147f7322feSeschrock }
19157f7322feSeschrock 
19169c3fd121SMatthew Ahrens static const char *
19177f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19187f7322feSeschrock {
19197f7322feSeschrock 	nvlist_t *nv;
19209c3fd121SMatthew Ahrens 	const char *value;
19217f7322feSeschrock 
1922a2eea2e1Sahrens 	*source = NULL;
19237f7322feSeschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19247f7322feSeschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
19259c3fd121SMatthew Ahrens 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1926990b4856Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19277f7322feSeschrock 	} else {
19282e5e9e19SSanjeev Bagewadi 		verify(!zhp->zfs_props_table ||
19292e5e9e19SSanjeev Bagewadi 		    zhp->zfs_props_table[prop] == B_TRUE);
19309c3fd121SMatthew Ahrens 		value = zfs_prop_default_string(prop);
19317f7322feSeschrock 		*source = "";
19327f7322feSeschrock 	}
19337f7322feSeschrock 
19347f7322feSeschrock 	return (value);
19357f7322feSeschrock }
19367f7322feSeschrock 
193792241e0bSTom Erickson static boolean_t
193892241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
193992241e0bSTom Erickson {
194092241e0bSTom Erickson 	return (zhp->zfs_props == zhp->zfs_recvd_props);
194192241e0bSTom Erickson }
194292241e0bSTom Erickson 
194392241e0bSTom Erickson static void
194492241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
194592241e0bSTom Erickson {
194692241e0bSTom Erickson 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
194792241e0bSTom Erickson 	zhp->zfs_props = zhp->zfs_recvd_props;
194892241e0bSTom Erickson }
194992241e0bSTom Erickson 
195092241e0bSTom Erickson static void
195192241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
195292241e0bSTom Erickson {
195392241e0bSTom Erickson 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
195492241e0bSTom Erickson 	*cookie = 0;
195592241e0bSTom Erickson }
195692241e0bSTom Erickson 
19577f7322feSeschrock /*
1958fa9e4066Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1959fa9e4066Sahrens  * zfs_prop_get_int() are built using this interface.
1960fa9e4066Sahrens  *
1961fa9e4066Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1962fa9e4066Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1963fa9e4066Sahrens  * If they differ from the on-disk values, report the current values and mark
1964fa9e4066Sahrens  * the source "temporary".
1965fa9e4066Sahrens  */
196699653d4eSeschrock static int
1967990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
196899653d4eSeschrock     char **source, uint64_t *val)
1969fa9e4066Sahrens {
1970bd00f61bSrm160521 	zfs_cmd_t zc = { 0 };
197196510749Stimh 	nvlist_t *zplprops = NULL;
1972fa9e4066Sahrens 	struct mnttab mnt;
19733ccfa83cSahrens 	char *mntopt_on = NULL;
19743ccfa83cSahrens 	char *mntopt_off = NULL;
197592241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1976fa9e4066Sahrens 
1977fa9e4066Sahrens 	*source = NULL;
1978fa9e4066Sahrens 
19793ccfa83cSahrens 	switch (prop) {
19803ccfa83cSahrens 	case ZFS_PROP_ATIME:
19813ccfa83cSahrens 		mntopt_on = MNTOPT_ATIME;
19823ccfa83cSahrens 		mntopt_off = MNTOPT_NOATIME;
19833ccfa83cSahrens 		break;
19843ccfa83cSahrens 
19853ccfa83cSahrens 	case ZFS_PROP_DEVICES:
19863ccfa83cSahrens 		mntopt_on = MNTOPT_DEVICES;
19873ccfa83cSahrens 		mntopt_off = MNTOPT_NODEVICES;
19883ccfa83cSahrens 		break;
19893ccfa83cSahrens 
19903ccfa83cSahrens 	case ZFS_PROP_EXEC:
19913ccfa83cSahrens 		mntopt_on = MNTOPT_EXEC;
19923ccfa83cSahrens 		mntopt_off = MNTOPT_NOEXEC;
19933ccfa83cSahrens 		break;
19943ccfa83cSahrens 
19953ccfa83cSahrens 	case ZFS_PROP_READONLY:
19963ccfa83cSahrens 		mntopt_on = MNTOPT_RO;
19973ccfa83cSahrens 		mntopt_off = MNTOPT_RW;
19983ccfa83cSahrens 		break;
19993ccfa83cSahrens 
20003ccfa83cSahrens 	case ZFS_PROP_SETUID:
20013ccfa83cSahrens 		mntopt_on = MNTOPT_SETUID;
20023ccfa83cSahrens 		mntopt_off = MNTOPT_NOSETUID;
20033ccfa83cSahrens 		break;
20043ccfa83cSahrens 
20053ccfa83cSahrens 	case ZFS_PROP_XATTR:
20063ccfa83cSahrens 		mntopt_on = MNTOPT_XATTR;
20073ccfa83cSahrens 		mntopt_off = MNTOPT_NOXATTR;
20083ccfa83cSahrens 		break;
2009da6c28aaSamw 
2010da6c28aaSamw 	case ZFS_PROP_NBMAND:
2011da6c28aaSamw 		mntopt_on = MNTOPT_NBMAND;
2012da6c28aaSamw 		mntopt_off = MNTOPT_NONBMAND;
2013da6c28aaSamw 		break;
201488f61deeSIgor Kozhukhov 
201588f61deeSIgor Kozhukhov 	default:
201688f61deeSIgor Kozhukhov 		break;
20173ccfa83cSahrens 	}
20183ccfa83cSahrens 
20193bb79becSeschrock 	/*
20203bb79becSeschrock 	 * Because looking up the mount options is potentially expensive
20213bb79becSeschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
20223bb79becSeschrock 	 * we're looking up a property which requires its presence.
20233bb79becSeschrock 	 */
20243bb79becSeschrock 	if (!zhp->zfs_mntcheck &&
20253ccfa83cSahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2026ebedde84SEric Taylor 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2027ebedde84SEric Taylor 		struct mnttab entry;
20283bb79becSeschrock 
2029ebedde84SEric Taylor 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2030ebedde84SEric Taylor 			zhp->zfs_mntopts = zfs_strdup(hdl,
20313ccfa83cSahrens 			    entry.mnt_mntopts);
20323ccfa83cSahrens 			if (zhp->zfs_mntopts == NULL)
20333bb79becSeschrock 				return (-1);
20343ccfa83cSahrens 		}
20353bb79becSeschrock 
20363bb79becSeschrock 		zhp->zfs_mntcheck = B_TRUE;
20373bb79becSeschrock 	}
20383bb79becSeschrock 
2039fa9e4066Sahrens 	if (zhp->zfs_mntopts == NULL)
2040fa9e4066Sahrens 		mnt.mnt_mntopts = "";
2041fa9e4066Sahrens 	else
2042fa9e4066Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2043fa9e4066Sahrens 
2044fa9e4066Sahrens 	switch (prop) {
2045fa9e4066Sahrens 	case ZFS_PROP_ATIME:
2046fa9e4066Sahrens 	case ZFS_PROP_DEVICES:
2047fa9e4066Sahrens 	case ZFS_PROP_EXEC:
20483ccfa83cSahrens 	case ZFS_PROP_READONLY:
20493ccfa83cSahrens 	case ZFS_PROP_SETUID:
20503ccfa83cSahrens 	case ZFS_PROP_XATTR:
2051da6c28aaSamw 	case ZFS_PROP_NBMAND:
205299653d4eSeschrock 		*val = getprop_uint64(zhp, prop, source);
2053fa9e4066Sahrens 
205492241e0bSTom Erickson 		if (received)
205592241e0bSTom Erickson 			break;
205692241e0bSTom Erickson 
20573ccfa83cSahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
205899653d4eSeschrock 			*val = B_TRUE;
2059fa9e4066Sahrens 			if (src)
2060990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
20613ccfa83cSahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
206299653d4eSeschrock 			*val = B_FALSE;
2063fa9e4066Sahrens 			if (src)
2064990b4856Slling 				*src = ZPROP_SRC_TEMPORARY;
2065fa9e4066Sahrens 		}
206699653d4eSeschrock 		break;
2067fa9e4066Sahrens 
20683ccfa83cSahrens 	case ZFS_PROP_CANMOUNT:
2069d41c4376SMark J Musante 	case ZFS_PROP_VOLSIZE:
2070fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2071a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2072fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2073a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2074a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2075a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2076a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2077a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2078a2eea2e1Sahrens 		*val = getprop_uint64(zhp, prop, source);
207992241e0bSTom Erickson 
208092241e0bSTom Erickson 		if (*source == NULL) {
208192241e0bSTom Erickson 			/* not default, must be local */
2082fa9e4066Sahrens 			*source = zhp->zfs_name;
208392241e0bSTom Erickson 		}
208499653d4eSeschrock 		break;
2085fa9e4066Sahrens 
2086fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
208799653d4eSeschrock 		*val = (zhp->zfs_mntopts != NULL);
208899653d4eSeschrock 		break;
2089fa9e4066Sahrens 
209039c23413Seschrock 	case ZFS_PROP_NUMCLONES:
209139c23413Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
209239c23413Seschrock 		break;
209339c23413Seschrock 
2094bd00f61bSrm160521 	case ZFS_PROP_VERSION:
2095de8267e0Stimh 	case ZFS_PROP_NORMALIZE:
2096de8267e0Stimh 	case ZFS_PROP_UTF8ONLY:
2097de8267e0Stimh 	case ZFS_PROP_CASE:
2098de8267e0Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2099de8267e0Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
21003d7934e1Srm160521 			return (-1);
2101bd00f61bSrm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2102de8267e0Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2103de8267e0Stimh 			zcmd_free_nvlists(&zc);
2104f4b94bdeSMatthew Ahrens 			return (-1);
2105bd00f61bSrm160521 		}
2106de8267e0Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2107de8267e0Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2108de8267e0Stimh 		    val) != 0) {
2109de8267e0Stimh 			zcmd_free_nvlists(&zc);
2110f4b94bdeSMatthew Ahrens 			return (-1);
2111de8267e0Stimh 		}
211296510749Stimh 		nvlist_free(zplprops);
2113de8267e0Stimh 		zcmd_free_nvlists(&zc);
2114bd00f61bSrm160521 		break;
2115bd00f61bSrm160521 
2116ca48f36fSKeith M Wesolowski 	case ZFS_PROP_INCONSISTENT:
2117ca48f36fSKeith M Wesolowski 		*val = zhp->zfs_dmustats.dds_inconsistent;
2118ca48f36fSKeith M Wesolowski 		break;
2119ca48f36fSKeith M Wesolowski 
2120fa9e4066Sahrens 	default:
2121e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
212291ebeef5Sahrens 		case PROP_TYPE_NUMBER:
212391ebeef5Sahrens 		case PROP_TYPE_INDEX:
2124e7437265Sahrens 			*val = getprop_uint64(zhp, prop, source);
212574e7dc98SMatthew Ahrens 			/*
212614843421SMatthew Ahrens 			 * If we tried to use a default value for a
212774e7dc98SMatthew Ahrens 			 * readonly property, it means that it was not
21284d86c0eaSMatthew Ahrens 			 * present.  Note this only applies to "truly"
21294d86c0eaSMatthew Ahrens 			 * readonly properties, not set-once properties
21304d86c0eaSMatthew Ahrens 			 * like volblocksize.
213174e7dc98SMatthew Ahrens 			 */
213274e7dc98SMatthew Ahrens 			if (zfs_prop_readonly(prop) &&
21334d86c0eaSMatthew Ahrens 			    !zfs_prop_setonce(prop) &&
213431f572c2STom Erickson 			    *source != NULL && (*source)[0] == '\0') {
213531f572c2STom Erickson 				*source = NULL;
2136ad2760acSMatthew Ahrens 				return (-1);
213774e7dc98SMatthew Ahrens 			}
2138e7437265Sahrens 			break;
2139e7437265Sahrens 
214091ebeef5Sahrens 		case PROP_TYPE_STRING:
2141e7437265Sahrens 		default:
214299653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
214399653d4eSeschrock 			    "cannot get non-numeric property"));
214499653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
214599653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "internal error")));
2146fa9e4066Sahrens 		}
2147e7437265Sahrens 	}
2148fa9e4066Sahrens 
2149fa9e4066Sahrens 	return (0);
2150fa9e4066Sahrens }
2151fa9e4066Sahrens 
2152fa9e4066Sahrens /*
2153fa9e4066Sahrens  * Calculate the source type, given the raw source string.
2154fa9e4066Sahrens  */
2155fa9e4066Sahrens static void
2156990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2157fa9e4066Sahrens     char *statbuf, size_t statlen)
2158fa9e4066Sahrens {
2159990b4856Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2160fa9e4066Sahrens 		return;
2161fa9e4066Sahrens 
2162fa9e4066Sahrens 	if (source == NULL) {
2163990b4856Slling 		*srctype = ZPROP_SRC_NONE;
2164fa9e4066Sahrens 	} else if (source[0] == '\0') {
2165990b4856Slling 		*srctype = ZPROP_SRC_DEFAULT;
216692241e0bSTom Erickson 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
216792241e0bSTom Erickson 		*srctype = ZPROP_SRC_RECEIVED;
2168fa9e4066Sahrens 	} else {
2169fa9e4066Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
2170990b4856Slling 			*srctype = ZPROP_SRC_LOCAL;
2171fa9e4066Sahrens 		} else {
2172fa9e4066Sahrens 			(void) strlcpy(statbuf, source, statlen);
2173990b4856Slling 			*srctype = ZPROP_SRC_INHERITED;
2174fa9e4066Sahrens 		}
2175fa9e4066Sahrens 	}
2176fa9e4066Sahrens 
2177fa9e4066Sahrens }
2178fa9e4066Sahrens 
217992241e0bSTom Erickson int
218092241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
218192241e0bSTom Erickson     size_t proplen, boolean_t literal)
218292241e0bSTom Erickson {
218392241e0bSTom Erickson 	zfs_prop_t prop;
218492241e0bSTom Erickson 	int err = 0;
218592241e0bSTom Erickson 
218692241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
218792241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
218892241e0bSTom Erickson 			return (-1);
218992241e0bSTom Erickson 
219092241e0bSTom Erickson 	prop = zfs_name_to_prop(propname);
219192241e0bSTom Erickson 
219292241e0bSTom Erickson 	if (prop != ZPROP_INVAL) {
219392241e0bSTom Erickson 		uint64_t cookie;
219492241e0bSTom Erickson 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
219592241e0bSTom Erickson 			return (-1);
219692241e0bSTom Erickson 		zfs_set_recvd_props_mode(zhp, &cookie);
219792241e0bSTom Erickson 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
219892241e0bSTom Erickson 		    NULL, NULL, 0, literal);
219992241e0bSTom Erickson 		zfs_unset_recvd_props_mode(zhp, &cookie);
220092241e0bSTom Erickson 	} else {
220192241e0bSTom Erickson 		nvlist_t *propval;
220292241e0bSTom Erickson 		char *recvdval;
220392241e0bSTom Erickson 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
220492241e0bSTom Erickson 		    propname, &propval) != 0)
220592241e0bSTom Erickson 			return (-1);
220692241e0bSTom Erickson 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
220792241e0bSTom Erickson 		    &recvdval) == 0);
220892241e0bSTom Erickson 		(void) strlcpy(propbuf, recvdval, proplen);
220992241e0bSTom Erickson 	}
221092241e0bSTom Erickson 
221192241e0bSTom Erickson 	return (err == 0 ? 0 : -1);
221292241e0bSTom Erickson }
221392241e0bSTom Erickson 
221419b94df9SMatthew Ahrens static int
221519b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
221619b94df9SMatthew Ahrens {
221719b94df9SMatthew Ahrens 	nvlist_t *value;
221819b94df9SMatthew Ahrens 	nvpair_t *pair;
221919b94df9SMatthew Ahrens 
222019b94df9SMatthew Ahrens 	value = zfs_get_clones_nvl(zhp);
222119b94df9SMatthew Ahrens 	if (value == NULL)
222219b94df9SMatthew Ahrens 		return (-1);
222319b94df9SMatthew Ahrens 
222419b94df9SMatthew Ahrens 	propbuf[0] = '\0';
222519b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
222619b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(value, pair)) {
222719b94df9SMatthew Ahrens 		if (propbuf[0] != '\0')
222819b94df9SMatthew Ahrens 			(void) strlcat(propbuf, ",", proplen);
222919b94df9SMatthew Ahrens 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
223019b94df9SMatthew Ahrens 	}
223119b94df9SMatthew Ahrens 
223219b94df9SMatthew Ahrens 	return (0);
223319b94df9SMatthew Ahrens }
223419b94df9SMatthew Ahrens 
223519b94df9SMatthew Ahrens struct get_clones_arg {
223619b94df9SMatthew Ahrens 	uint64_t numclones;
223719b94df9SMatthew Ahrens 	nvlist_t *value;
223819b94df9SMatthew Ahrens 	const char *origin;
22399adfa60dSMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
224019b94df9SMatthew Ahrens };
224119b94df9SMatthew Ahrens 
224219b94df9SMatthew Ahrens int
224319b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
224419b94df9SMatthew Ahrens {
224519b94df9SMatthew Ahrens 	struct get_clones_arg *gca = arg;
224619b94df9SMatthew Ahrens 
224719b94df9SMatthew Ahrens 	if (gca->numclones == 0) {
224819b94df9SMatthew Ahrens 		zfs_close(zhp);
224919b94df9SMatthew Ahrens 		return (0);
225019b94df9SMatthew Ahrens 	}
225119b94df9SMatthew Ahrens 
225219b94df9SMatthew Ahrens 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
225319b94df9SMatthew Ahrens 	    NULL, NULL, 0, B_TRUE) != 0)
225419b94df9SMatthew Ahrens 		goto out;
225519b94df9SMatthew Ahrens 	if (strcmp(gca->buf, gca->origin) == 0) {
22563b2aab18SMatthew Ahrens 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
225719b94df9SMatthew Ahrens 		gca->numclones--;
225819b94df9SMatthew Ahrens 	}
225919b94df9SMatthew Ahrens 
226019b94df9SMatthew Ahrens out:
226119b94df9SMatthew Ahrens 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
226219b94df9SMatthew Ahrens 	zfs_close(zhp);
226319b94df9SMatthew Ahrens 	return (0);
226419b94df9SMatthew Ahrens }
226519b94df9SMatthew Ahrens 
226619b94df9SMatthew Ahrens nvlist_t *
226719b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
226819b94df9SMatthew Ahrens {
226919b94df9SMatthew Ahrens 	nvlist_t *nv, *value;
227019b94df9SMatthew Ahrens 
227119b94df9SMatthew Ahrens 	if (nvlist_lookup_nvlist(zhp->zfs_props,
227219b94df9SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
227319b94df9SMatthew Ahrens 		struct get_clones_arg gca;
227419b94df9SMatthew Ahrens 
227519b94df9SMatthew Ahrens 		/*
227619b94df9SMatthew Ahrens 		 * if this is a snapshot, then the kernel wasn't able
227719b94df9SMatthew Ahrens 		 * to get the clones.  Do it by slowly iterating.
227819b94df9SMatthew Ahrens 		 */
227919b94df9SMatthew Ahrens 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
228019b94df9SMatthew Ahrens 			return (NULL);
228119b94df9SMatthew Ahrens 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
228219b94df9SMatthew Ahrens 			return (NULL);
228319b94df9SMatthew Ahrens 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
228419b94df9SMatthew Ahrens 			nvlist_free(nv);
228519b94df9SMatthew Ahrens 			return (NULL);
228619b94df9SMatthew Ahrens 		}
228719b94df9SMatthew Ahrens 
228819b94df9SMatthew Ahrens 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
228919b94df9SMatthew Ahrens 		gca.value = value;
229019b94df9SMatthew Ahrens 		gca.origin = zhp->zfs_name;
229119b94df9SMatthew Ahrens 
229219b94df9SMatthew Ahrens 		if (gca.numclones != 0) {
229319b94df9SMatthew Ahrens 			zfs_handle_t *root;
22949adfa60dSMatthew Ahrens 			char pool[ZFS_MAX_DATASET_NAME_LEN];
229519b94df9SMatthew Ahrens 			char *cp = pool;
229619b94df9SMatthew Ahrens 
229719b94df9SMatthew Ahrens 			/* get the pool name */
229819b94df9SMatthew Ahrens 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
229919b94df9SMatthew Ahrens 			(void) strsep(&cp, "/@");
230019b94df9SMatthew Ahrens 			root = zfs_open(zhp->zfs_hdl, pool,
230119b94df9SMatthew Ahrens 			    ZFS_TYPE_FILESYSTEM);
230219b94df9SMatthew Ahrens 
230319b94df9SMatthew Ahrens 			(void) get_clones_cb(root, &gca);
230419b94df9SMatthew Ahrens 		}
230519b94df9SMatthew Ahrens 
230619b94df9SMatthew Ahrens 		if (gca.numclones != 0 ||
230719b94df9SMatthew Ahrens 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
230819b94df9SMatthew Ahrens 		    nvlist_add_nvlist(zhp->zfs_props,
230919b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
231019b94df9SMatthew Ahrens 			nvlist_free(nv);
231119b94df9SMatthew Ahrens 			nvlist_free(value);
231219b94df9SMatthew Ahrens 			return (NULL);
231319b94df9SMatthew Ahrens 		}
231419b94df9SMatthew Ahrens 		nvlist_free(nv);
231519b94df9SMatthew Ahrens 		nvlist_free(value);
231619b94df9SMatthew Ahrens 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
231719b94df9SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
231819b94df9SMatthew Ahrens 	}
231919b94df9SMatthew Ahrens 
232019b94df9SMatthew Ahrens 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
232119b94df9SMatthew Ahrens 
232219b94df9SMatthew Ahrens 	return (value);
232319b94df9SMatthew Ahrens }
232419b94df9SMatthew Ahrens 
2325fa9e4066Sahrens /*
2326dfc11533SChris Williamson  * Accepts a property and value and checks that the value
2327dfc11533SChris Williamson  * matches the one found by the channel program. If they are
2328dfc11533SChris Williamson  * not equal, print both of them.
2329dfc11533SChris Williamson  */
2330dfc11533SChris Williamson void
2331dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2332dfc11533SChris Williamson     const char *strval)
2333dfc11533SChris Williamson {
2334dfc11533SChris Williamson 	if (!zhp->zfs_hdl->libzfs_prop_debug)
2335dfc11533SChris Williamson 		return;
2336dfc11533SChris Williamson 	int error;
2337dfc11533SChris Williamson 	char *poolname = zhp->zpool_hdl->zpool_name;
2338dfc11533SChris Williamson 	const char *program =
2339dfc11533SChris Williamson 	    "args = ...\n"
2340dfc11533SChris Williamson 	    "ds = args['dataset']\n"
2341dfc11533SChris Williamson 	    "prop = args['property']\n"
2342dfc11533SChris Williamson 	    "value, setpoint = zfs.get_prop(ds, prop)\n"
2343dfc11533SChris Williamson 	    "return {value=value, setpoint=setpoint}\n";
2344dfc11533SChris Williamson 	nvlist_t *outnvl;
2345dfc11533SChris Williamson 	nvlist_t *retnvl;
2346dfc11533SChris Williamson 	nvlist_t *argnvl = fnvlist_alloc();
2347dfc11533SChris Williamson 
2348dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2349dfc11533SChris Williamson 	fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2350dfc11533SChris Williamson 
2351a3b28680SSerapheim Dimitropoulos 	error = lzc_channel_program_nosync(poolname, program,
2352dfc11533SChris Williamson 	    10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2353dfc11533SChris Williamson 
2354dfc11533SChris Williamson 	if (error == 0) {
2355dfc11533SChris Williamson 		retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2356dfc11533SChris Williamson 		if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2357dfc11533SChris Williamson 			int64_t ans;
2358dfc11533SChris Williamson 			error = nvlist_lookup_int64(retnvl, "value", &ans);
2359dfc11533SChris Williamson 			if (error != 0) {
2360dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2361dfc11533SChris Williamson 				    error);
2362dfc11533SChris Williamson 				return;
2363dfc11533SChris Williamson 			}
2364dfc11533SChris Williamson 			if (ans != intval) {
2365dfc11533SChris Williamson 				(void) fprintf(stderr,
2366dfc11533SChris Williamson 				    "%s: zfs found %lld, but zcp found %lld\n",
2367dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2368dfc11533SChris Williamson 				    (longlong_t)intval, (longlong_t)ans);
2369dfc11533SChris Williamson 			}
2370dfc11533SChris Williamson 		} else {
2371dfc11533SChris Williamson 			char *str_ans;
2372dfc11533SChris Williamson 			error = nvlist_lookup_string(retnvl, "value", &str_ans);
2373dfc11533SChris Williamson 			if (error != 0) {
2374dfc11533SChris Williamson 				(void) fprintf(stderr, "zcp check error: %u\n",
2375dfc11533SChris Williamson 				    error);
2376dfc11533SChris Williamson 				return;
2377dfc11533SChris Williamson 			}
2378dfc11533SChris Williamson 			if (strcmp(strval, str_ans) != 0) {
2379dfc11533SChris Williamson 				(void) fprintf(stderr,
2380dfc11533SChris Williamson 				    "%s: zfs found %s, but zcp found %s\n",
2381dfc11533SChris Williamson 				    zfs_prop_to_name(prop),
2382dfc11533SChris Williamson 				    strval, str_ans);
2383dfc11533SChris Williamson 			}
2384dfc11533SChris Williamson 		}
2385dfc11533SChris Williamson 	} else {
2386dfc11533SChris Williamson 		(void) fprintf(stderr,
2387dfc11533SChris Williamson 		    "zcp check failed, channel program error: %u\n", error);
2388dfc11533SChris Williamson 	}
2389dfc11533SChris Williamson 	nvlist_free(argnvl);
2390dfc11533SChris Williamson 	nvlist_free(outnvl);
2391dfc11533SChris Williamson }
2392dfc11533SChris Williamson 
2393dfc11533SChris Williamson /*
2394fa9e4066Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2395fa9e4066Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2396fa9e4066Sahrens  * human-readable form.
2397fa9e4066Sahrens  *
2398fa9e4066Sahrens  * Returns 0 on success, or -1 on error.
2399fa9e4066Sahrens  */
2400fa9e4066Sahrens int
2401fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2402990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2403fa9e4066Sahrens {
2404fa9e4066Sahrens 	char *source = NULL;
2405fa9e4066Sahrens 	uint64_t val;
24069c3fd121SMatthew Ahrens 	const char *str;
2407e9dbad6fSeschrock 	const char *strval;
240892241e0bSTom Erickson 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2409fa9e4066Sahrens 
2410fa9e4066Sahrens 	/*
2411fa9e4066Sahrens 	 * Check to see if this property applies to our object
2412fa9e4066Sahrens 	 */
2413fa9e4066Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2414fa9e4066Sahrens 		return (-1);
2415fa9e4066Sahrens 
241692241e0bSTom Erickson 	if (received && zfs_prop_readonly(prop))
241792241e0bSTom Erickson 		return (-1);
241892241e0bSTom Erickson 
2419fa9e4066Sahrens 	if (src)
2420990b4856Slling 		*src = ZPROP_SRC_NONE;
2421fa9e4066Sahrens 
2422fa9e4066Sahrens 	switch (prop) {
2423fa9e4066Sahrens 	case ZFS_PROP_CREATION:
2424fa9e4066Sahrens 		/*
2425fa9e4066Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2426fa9e4066Sahrens 		 * this into a string unless 'literal' is specified.
2427fa9e4066Sahrens 		 */
2428fa9e4066Sahrens 		{
2429a2eea2e1Sahrens 			val = getprop_uint64(zhp, prop, &source);
2430a2eea2e1Sahrens 			time_t time = (time_t)val;
2431fa9e4066Sahrens 			struct tm t;
2432fa9e4066Sahrens 
2433fa9e4066Sahrens 			if (literal ||
2434fa9e4066Sahrens 			    localtime_r(&time, &t) == NULL ||
2435fa9e4066Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2436fa9e4066Sahrens 			    &t) == 0)
2437a2eea2e1Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2438fa9e4066Sahrens 		}
2439dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2440fa9e4066Sahrens 		break;
2441fa9e4066Sahrens 
2442fa9e4066Sahrens 	case ZFS_PROP_MOUNTPOINT:
2443fa9e4066Sahrens 		/*
2444fa9e4066Sahrens 		 * Getting the precise mountpoint can be tricky.
2445fa9e4066Sahrens 		 *
2446fa9e4066Sahrens 		 *  - for 'none' or 'legacy', return those values.
2447fa9e4066Sahrens 		 *  - for inherited mountpoints, we want to take everything
2448fa9e4066Sahrens 		 *    after our ancestor and append it to the inherited value.
2449fa9e4066Sahrens 		 *
2450fa9e4066Sahrens 		 * If the pool has an alternate root, we want to prepend that
2451fa9e4066Sahrens 		 * root to any values we return.
2452fa9e4066Sahrens 		 */
245329ab75c9Srm160521 
24547f7322feSeschrock 		str = getprop_string(zhp, prop, &source);
2455fa9e4066Sahrens 
2456b21718f0Sgw25295 		if (str[0] == '/') {
245729ab75c9Srm160521 			char buf[MAXPATHLEN];
245829ab75c9Srm160521 			char *root = buf;
2459a79992aaSTom Erickson 			const char *relpath;
2460fa9e4066Sahrens 
2461a79992aaSTom Erickson 			/*
2462a79992aaSTom Erickson 			 * If we inherit the mountpoint, even from a dataset
2463a79992aaSTom Erickson 			 * with a received value, the source will be the path of
2464a79992aaSTom Erickson 			 * the dataset we inherit from. If source is
2465a79992aaSTom Erickson 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2466a79992aaSTom Erickson 			 * inherited.
2467a79992aaSTom Erickson 			 */
2468a79992aaSTom Erickson 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2469a79992aaSTom Erickson 				relpath = "";
2470a79992aaSTom Erickson 			} else {
2471a79992aaSTom Erickson 				relpath = zhp->zfs_name + strlen(source);
2472fa9e4066Sahrens 				if (relpath[0] == '/')
2473fa9e4066Sahrens 					relpath++;
2474a79992aaSTom Erickson 			}
2475b21718f0Sgw25295 
247629ab75c9Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
2477c58b3526SAdam Stevko 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2478c58b3526SAdam Stevko 			    B_FALSE)) || (strcmp(root, "-") == 0))
247929ab75c9Srm160521 				root[0] = '\0';
2480b21718f0Sgw25295 			/*
2481b21718f0Sgw25295 			 * Special case an alternate root of '/'. This will
2482b21718f0Sgw25295 			 * avoid having multiple leading slashes in the
2483b21718f0Sgw25295 			 * mountpoint path.
2484b21718f0Sgw25295 			 */
2485b21718f0Sgw25295 			if (strcmp(root, "/") == 0)
2486b21718f0Sgw25295 				root++;
2487b21718f0Sgw25295 
2488b21718f0Sgw25295 			/*
2489b21718f0Sgw25295 			 * If the mountpoint is '/' then skip over this
2490b21718f0Sgw25295 			 * if we are obtaining either an alternate root or
2491b21718f0Sgw25295 			 * an inherited mountpoint.
2492b21718f0Sgw25295 			 */
2493b21718f0Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2494b21718f0Sgw25295 			    relpath[0] != '\0'))
24957f7322feSeschrock 				str++;
2496fa9e4066Sahrens 
2497fa9e4066Sahrens 			if (relpath[0] == '\0')
2498fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
24997f7322feSeschrock 				    root, str);
2500fa9e4066Sahrens 			else
2501fa9e4066Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
25027f7322feSeschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2503fa9e4066Sahrens 				    relpath);
2504fa9e4066Sahrens 		} else {
2505fa9e4066Sahrens 			/* 'legacy' or 'none' */
25067f7322feSeschrock 			(void) strlcpy(propbuf, str, proplen);
2507fa9e4066Sahrens 		}
2508dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, propbuf);
2509fa9e4066Sahrens 		break;
2510fa9e4066Sahrens 
2511fa9e4066Sahrens 	case ZFS_PROP_ORIGIN:
25129c3fd121SMatthew Ahrens 		str = getprop_string(zhp, prop, &source);
25139c3fd121SMatthew Ahrens 		if (str == NULL)
2514fa9e4066Sahrens 			return (-1);
25159c3fd121SMatthew Ahrens 		(void) strlcpy(propbuf, str, proplen);
2516dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, str);
2517fa9e4066Sahrens 		break;
2518fa9e4066Sahrens 
251919b94df9SMatthew Ahrens 	case ZFS_PROP_CLONES:
252019b94df9SMatthew Ahrens 		if (get_clones_string(zhp, propbuf, proplen) != 0)
252119b94df9SMatthew Ahrens 			return (-1);
252219b94df9SMatthew Ahrens 		break;
252319b94df9SMatthew Ahrens 
2524fa9e4066Sahrens 	case ZFS_PROP_QUOTA:
2525a9799022Sck153898 	case ZFS_PROP_REFQUOTA:
2526fa9e4066Sahrens 	case ZFS_PROP_RESERVATION:
2527a9799022Sck153898 	case ZFS_PROP_REFRESERVATION:
2528a9799022Sck153898 
252999653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
253099653d4eSeschrock 			return (-1);
2531fa9e4066Sahrens 		/*
2532fa9e4066Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2533fa9e4066Sahrens 		 * (unless literal is set), and indicate that it's the default
2534fa9e4066Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2535fa9e4066Sahrens 		 * that its set locally.
2536fa9e4066Sahrens 		 */
2537fa9e4066Sahrens 		if (val == 0) {
2538fa9e4066Sahrens 			if (literal)
2539fa9e4066Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2540fa9e4066Sahrens 			else
2541fa9e4066Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2542fa9e4066Sahrens 		} else {
2543fa9e4066Sahrens 			if (literal)
25445ad82045Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
25455ad82045Snd150628 				    (u_longlong_t)val);
2546fa9e4066Sahrens 			else
2547fa9e4066Sahrens 				zfs_nicenum(val, propbuf, proplen);
2548fa9e4066Sahrens 		}
2549dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2550fa9e4066Sahrens 		break;
2551fa9e4066Sahrens 
2552a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2553a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2554a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_COUNT:
2555a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_COUNT:
2556a2afb611SJerry Jelinek 
2557a2afb611SJerry Jelinek 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2558a2afb611SJerry Jelinek 			return (-1);
2559a2afb611SJerry Jelinek 
2560a2afb611SJerry Jelinek 		/*
2561a2afb611SJerry Jelinek 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2562a2afb611SJerry Jelinek 		 * literal is set), and indicate that it's the default value.
2563a2afb611SJerry Jelinek 		 * Otherwise, we print the number nicely and indicate that it's
2564a2afb611SJerry Jelinek 		 * set locally.
2565a2afb611SJerry Jelinek 		 */
2566a2afb611SJerry Jelinek 		if (literal) {
2567a2afb611SJerry Jelinek 			(void) snprintf(propbuf, proplen, "%llu",
2568a2afb611SJerry Jelinek 			    (u_longlong_t)val);
2569a2afb611SJerry Jelinek 		} else if (val == UINT64_MAX) {
2570a2afb611SJerry Jelinek 			(void) strlcpy(propbuf, "none", proplen);
2571a2afb611SJerry Jelinek 		} else {
2572a2afb611SJerry Jelinek 			zfs_nicenum(val, propbuf, proplen);
2573a2afb611SJerry Jelinek 		}
2574dfc11533SChris Williamson 
2575dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2576a2afb611SJerry Jelinek 		break;
2577a2afb611SJerry Jelinek 
2578187d6ac0SMatt Ahrens 	case ZFS_PROP_REFRATIO:
2579fa9e4066Sahrens 	case ZFS_PROP_COMPRESSRATIO:
258099653d4eSeschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
258199653d4eSeschrock 			return (-1);
2582b24ab676SJeff Bonwick 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2583b24ab676SJeff Bonwick 		    (u_longlong_t)(val / 100),
2584b24ab676SJeff Bonwick 		    (u_longlong_t)(val % 100));
2585dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2586fa9e4066Sahrens 		break;
2587fa9e4066Sahrens 
2588fa9e4066Sahrens 	case ZFS_PROP_TYPE:
2589fa9e4066Sahrens 		switch (zhp->zfs_type) {
2590fa9e4066Sahrens 		case ZFS_TYPE_FILESYSTEM:
2591fa9e4066Sahrens 			str = "filesystem";
2592fa9e4066Sahrens 			break;
2593fa9e4066Sahrens 		case ZFS_TYPE_VOLUME:
2594fa9e4066Sahrens 			str = "volume";
2595fa9e4066Sahrens 			break;
2596fa9e4066Sahrens 		case ZFS_TYPE_SNAPSHOT:
2597fa9e4066Sahrens 			str = "snapshot";
2598fa9e4066Sahrens 			break;
259978f17100SMatthew Ahrens 		case ZFS_TYPE_BOOKMARK:
260078f17100SMatthew Ahrens 			str = "bookmark";
260178f17100SMatthew Ahrens 			break;
2602fa9e4066Sahrens 		default:
260399653d4eSeschrock 			abort();
2604fa9e4066Sahrens 		}
2605fa9e4066Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2606dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, propbuf);
2607fa9e4066Sahrens 		break;
2608fa9e4066Sahrens 
2609fa9e4066Sahrens 	case ZFS_PROP_MOUNTED:
2610fa9e4066Sahrens 		/*
2611fa9e4066Sahrens 		 * The 'mounted' property is a pseudo-property that described
2612fa9e4066Sahrens 		 * whether the filesystem is currently mounted.  Even though
2613fa9e4066Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2614fa9e4066Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2615fa9e4066Sahrens 		 */
261699653d4eSeschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
261799653d4eSeschrock 		    src, &source, &val) != 0)
261899653d4eSeschrock 			return (-1);
261999653d4eSeschrock 		if (val)
2620fa9e4066Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2621fa9e4066Sahrens 		else
2622fa9e4066Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2623fa9e4066Sahrens 		break;
2624fa9e4066Sahrens 
2625fa9e4066Sahrens 	case ZFS_PROP_NAME:
2626fa9e4066Sahrens 		/*
2627fa9e4066Sahrens 		 * The 'name' property is a pseudo-property derived from the
2628fa9e4066Sahrens 		 * dataset name.  It is presented as a real property to simplify
2629fa9e4066Sahrens 		 * consumers.
2630fa9e4066Sahrens 		 */
2631fa9e4066Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2632dfc11533SChris Williamson 		zcp_check(zhp, prop, NULL, propbuf);
2633fa9e4066Sahrens 		break;
2634fa9e4066Sahrens 
26354201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
26364201a95eSRic Aleshire 		{
26374201a95eSRic Aleshire 			m_label_t *new_sl = NULL;
26384201a95eSRic Aleshire 			char *ascii = NULL;	/* human readable label */
26394201a95eSRic Aleshire 
26404201a95eSRic Aleshire 			(void) strlcpy(propbuf,
26414201a95eSRic Aleshire 			    getprop_string(zhp, prop, &source), proplen);
26424201a95eSRic Aleshire 
26434201a95eSRic Aleshire 			if (literal || (strcasecmp(propbuf,
26444201a95eSRic Aleshire 			    ZFS_MLSLABEL_DEFAULT) == 0))
26454201a95eSRic Aleshire 				break;
26464201a95eSRic Aleshire 
26474201a95eSRic Aleshire 			/*
26484201a95eSRic Aleshire 			 * Try to translate the internal hex string to
26494201a95eSRic Aleshire 			 * human-readable output.  If there are any
26504201a95eSRic Aleshire 			 * problems just use the hex string.
26514201a95eSRic Aleshire 			 */
26524201a95eSRic Aleshire 
26534201a95eSRic Aleshire 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
26544201a95eSRic Aleshire 			    L_NO_CORRECTION, NULL) == -1) {
26554201a95eSRic Aleshire 				m_label_free(new_sl);
26564201a95eSRic Aleshire 				break;
26574201a95eSRic Aleshire 			}
26584201a95eSRic Aleshire 
26594201a95eSRic Aleshire 			if (label_to_str(new_sl, &ascii, M_LABEL,
26604201a95eSRic Aleshire 			    DEF_NAMES) != 0) {
26614201a95eSRic Aleshire 				if (ascii)
26624201a95eSRic Aleshire 					free(ascii);
26634201a95eSRic Aleshire 				m_label_free(new_sl);
26644201a95eSRic Aleshire 				break;
26654201a95eSRic Aleshire 			}
26664201a95eSRic Aleshire 			m_label_free(new_sl);
26674201a95eSRic Aleshire 
26684201a95eSRic Aleshire 			(void) strlcpy(propbuf, ascii, proplen);
26694201a95eSRic Aleshire 			free(ascii);
26704201a95eSRic Aleshire 		}
26714201a95eSRic Aleshire 		break;
26724201a95eSRic Aleshire 
2673f0f3ef5aSGarrett D'Amore 	case ZFS_PROP_GUID:
2674f0f3ef5aSGarrett D'Amore 		/*
2675f0f3ef5aSGarrett D'Amore 		 * GUIDs are stored as numbers, but they are identifiers.
2676f0f3ef5aSGarrett D'Amore 		 * We don't want them to be pretty printed, because pretty
2677f0f3ef5aSGarrett D'Amore 		 * printing mangles the ID into a truncated and useless value.
2678f0f3ef5aSGarrett D'Amore 		 */
2679f0f3ef5aSGarrett D'Amore 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2680f0f3ef5aSGarrett D'Amore 			return (-1);
2681f0f3ef5aSGarrett D'Amore 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2682dfc11533SChris Williamson 		zcp_check(zhp, prop, val, NULL);
2683f0f3ef5aSGarrett D'Amore 		break;
2684f0f3ef5aSGarrett D'Amore 
2685fa9e4066Sahrens 	default:
2686e7437265Sahrens 		switch (zfs_prop_get_type(prop)) {
268791ebeef5Sahrens 		case PROP_TYPE_NUMBER:
2688e7437265Sahrens 			if (get_numeric_property(zhp, prop, src,
2689dfc11533SChris Williamson 			    &source, &val) != 0) {
2690e7437265Sahrens 				return (-1);
2691dfc11533SChris Williamson 			}
2692dfc11533SChris Williamson 
2693dfc11533SChris Williamson 			if (literal) {
2694e7437265Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
2695e7437265Sahrens 				    (u_longlong_t)val);
2696dfc11533SChris Williamson 			} else {
2697e7437265Sahrens 				zfs_nicenum(val, propbuf, proplen);
2698dfc11533SChris Williamson 			}
2699dfc11533SChris Williamson 			zcp_check(zhp, prop, val, NULL);
2700e7437265Sahrens 			break;
2701e7437265Sahrens 
270291ebeef5Sahrens 		case PROP_TYPE_STRING:
27039c3fd121SMatthew Ahrens 			str = getprop_string(zhp, prop, &source);
27049c3fd121SMatthew Ahrens 			if (str == NULL)
27059c3fd121SMatthew Ahrens 				return (-1);
2706dfc11533SChris Williamson 
27079c3fd121SMatthew Ahrens 			(void) strlcpy(propbuf, str, proplen);
2708dfc11533SChris Williamson 			zcp_check(zhp, prop, NULL, str);
2709e7437265Sahrens 			break;
2710e7437265Sahrens 
271191ebeef5Sahrens 		case PROP_TYPE_INDEX:
2712fe192f76Sahrens 			if (get_numeric_property(zhp, prop, src,
2713fe192f76Sahrens 			    &source, &val) != 0)
2714fe192f76Sahrens 				return (-1);
2715fe192f76Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2716e7437265Sahrens 				return (-1);
2717dfc11533SChris Williamson 
2718e7437265Sahrens 			(void) strlcpy(propbuf, strval, proplen);
2719dfc11533SChris Williamson 			zcp_check(zhp, prop, NULL, strval);
2720e7437265Sahrens 			break;
2721e7437265Sahrens 
2722e7437265Sahrens 		default:
272399653d4eSeschrock 			abort();
2724fa9e4066Sahrens 		}
2725e7437265Sahrens 	}
2726fa9e4066Sahrens 
2727fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2728fa9e4066Sahrens 
2729fa9e4066Sahrens 	return (0);
2730fa9e4066Sahrens }
2731fa9e4066Sahrens 
2732fa9e4066Sahrens /*
2733fa9e4066Sahrens  * Utility function to get the given numeric property.  Does no validation that
2734fa9e4066Sahrens  * the given property is the appropriate type; should only be used with
2735fa9e4066Sahrens  * hard-coded property types.
2736fa9e4066Sahrens  */
2737fa9e4066Sahrens uint64_t
2738fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2739fa9e4066Sahrens {
2740fa9e4066Sahrens 	char *source;
274199653d4eSeschrock 	uint64_t val;
2742fa9e4066Sahrens 
27433cb34c60Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
274499653d4eSeschrock 
274599653d4eSeschrock 	return (val);
2746fa9e4066Sahrens }
2747fa9e4066Sahrens 
27487b97dc1aSrm160521 int
27497b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
27507b97dc1aSrm160521 {
27517b97dc1aSrm160521 	char buf[64];
27527b97dc1aSrm160521 
275314843421SMatthew Ahrens 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
27547b97dc1aSrm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
27557b97dc1aSrm160521 }
27567b97dc1aSrm160521 
2757fa9e4066Sahrens /*
2758fa9e4066Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2759fa9e4066Sahrens  */
2760fa9e4066Sahrens int
2761fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2762990b4856Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2763fa9e4066Sahrens {
2764fa9e4066Sahrens 	char *source;
2765fa9e4066Sahrens 
2766fa9e4066Sahrens 	/*
2767fa9e4066Sahrens 	 * Check to see if this property applies to our object
2768fa9e4066Sahrens 	 */
2769e45ce728Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2770ece3d9b3Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
277199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
277299653d4eSeschrock 		    zfs_prop_to_name(prop)));
2773e45ce728Sahrens 	}
2774fa9e4066Sahrens 
2775fa9e4066Sahrens 	if (src)
2776990b4856Slling 		*src = ZPROP_SRC_NONE;
2777fa9e4066Sahrens 
277899653d4eSeschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
277999653d4eSeschrock 		return (-1);
2780fa9e4066Sahrens 
2781fa9e4066Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2782fa9e4066Sahrens 
2783fa9e4066Sahrens 	return (0);
2784fa9e4066Sahrens }
2785fa9e4066Sahrens 
278614843421SMatthew Ahrens static int
278714843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
278814843421SMatthew Ahrens     char **domainp, idmap_rid_t *ridp)
278914843421SMatthew Ahrens {
279014843421SMatthew Ahrens 	idmap_get_handle_t *get_hdl = NULL;
279114843421SMatthew Ahrens 	idmap_stat status;
279214843421SMatthew Ahrens 	int err = EINVAL;
279314843421SMatthew Ahrens 
27941fdeec65Sjoyce mcintosh 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
279514843421SMatthew Ahrens 		goto out;
279614843421SMatthew Ahrens 
279714843421SMatthew Ahrens 	if (isuser) {
279814843421SMatthew Ahrens 		err = idmap_get_sidbyuid(get_hdl, id,
279914843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
280014843421SMatthew Ahrens 	} else {
280114843421SMatthew Ahrens 		err = idmap_get_sidbygid(get_hdl, id,
280214843421SMatthew Ahrens 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
280314843421SMatthew Ahrens 	}
280414843421SMatthew Ahrens 	if (err == IDMAP_SUCCESS &&
280514843421SMatthew Ahrens 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
280614843421SMatthew Ahrens 	    status == IDMAP_SUCCESS)
280714843421SMatthew Ahrens 		err = 0;
280814843421SMatthew Ahrens 	else
280914843421SMatthew Ahrens 		err = EINVAL;
281014843421SMatthew Ahrens out:
281114843421SMatthew Ahrens 	if (get_hdl)
281214843421SMatthew Ahrens 		idmap_get_destroy(get_hdl);
281314843421SMatthew Ahrens 	return (err);
281414843421SMatthew Ahrens }
281514843421SMatthew Ahrens 
281614843421SMatthew Ahrens /*
281714843421SMatthew Ahrens  * convert the propname into parameters needed by kernel
281814843421SMatthew Ahrens  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
281914843421SMatthew Ahrens  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
282014843421SMatthew Ahrens  */
282114843421SMatthew Ahrens static int
282214843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
282314843421SMatthew Ahrens     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
282414843421SMatthew Ahrens {
282514843421SMatthew Ahrens 	zfs_userquota_prop_t type;
282614843421SMatthew Ahrens 	char *cp, *end;
28273b12c289SMatthew Ahrens 	char *numericsid = NULL;
282814843421SMatthew Ahrens 	boolean_t isuser;
282914843421SMatthew Ahrens 
283014843421SMatthew Ahrens 	domain[0] = '\0';
28311ed6b69aSGordon Ross 	*ridp = 0;
283214843421SMatthew Ahrens 	/* Figure out the property type ({user|group}{quota|space}) */
283314843421SMatthew Ahrens 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
283414843421SMatthew Ahrens 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
283514843421SMatthew Ahrens 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
283614843421SMatthew Ahrens 			break;
283714843421SMatthew Ahrens 	}
283814843421SMatthew Ahrens 	if (type == ZFS_NUM_USERQUOTA_PROPS)
283914843421SMatthew Ahrens 		return (EINVAL);
284014843421SMatthew Ahrens 	*typep = type;
284114843421SMatthew Ahrens 
284214843421SMatthew Ahrens 	isuser = (type == ZFS_PROP_USERQUOTA ||
284314843421SMatthew Ahrens 	    type == ZFS_PROP_USERUSED);
284414843421SMatthew Ahrens 
284514843421SMatthew Ahrens 	cp = strchr(propname, '@') + 1;
284614843421SMatthew Ahrens 
284714843421SMatthew Ahrens 	if (strchr(cp, '@')) {
284814843421SMatthew Ahrens 		/*
284914843421SMatthew Ahrens 		 * It's a SID name (eg "user@domain") that needs to be
28503b12c289SMatthew Ahrens 		 * turned into S-1-domainID-RID.
285114843421SMatthew Ahrens 		 */
28521ed6b69aSGordon Ross 		int flag = 0;
28531ed6b69aSGordon Ross 		idmap_stat stat, map_stat;
28541ed6b69aSGordon Ross 		uid_t pid;
28551ed6b69aSGordon Ross 		idmap_rid_t rid;
28561ed6b69aSGordon Ross 		idmap_get_handle_t *gh = NULL;
28571ed6b69aSGordon Ross 
28581ed6b69aSGordon Ross 		stat = idmap_get_create(&gh);
28591ed6b69aSGordon Ross 		if (stat != IDMAP_SUCCESS) {
28601ed6b69aSGordon Ross 			idmap_get_destroy(gh);
28611ed6b69aSGordon Ross 			return (ENOMEM);
28621ed6b69aSGordon Ross 		}
286314843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
286414843421SMatthew Ahrens 			return (ENOENT);
28653b12c289SMatthew Ahrens 		if (isuser) {
28661ed6b69aSGordon Ross 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
28671ed6b69aSGordon Ross 			if (stat < 0)
28681ed6b69aSGordon Ross 				return (ENOENT);
28691ed6b69aSGordon Ross 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
28701ed6b69aSGordon Ross 			    &rid, &map_stat);
28713b12c289SMatthew Ahrens 		} else {
28721ed6b69aSGordon Ross 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
28731ed6b69aSGordon Ross 			if (stat < 0)
28741ed6b69aSGordon Ross 				return (ENOENT);
28751ed6b69aSGordon Ross 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
28761ed6b69aSGordon Ross 			    &rid, &map_stat);
28773b12c289SMatthew Ahrens 		}
28781ed6b69aSGordon Ross 		if (stat < 0) {
28791ed6b69aSGordon Ross 			idmap_get_destroy(gh);
28801ed6b69aSGordon Ross 			return (ENOENT);
28811ed6b69aSGordon Ross 		}
28821ed6b69aSGordon Ross 		stat = idmap_get_mappings(gh);
28831ed6b69aSGordon Ross 		idmap_get_destroy(gh);
28841ed6b69aSGordon Ross 
28851ed6b69aSGordon Ross 		if (stat < 0) {
288614843421SMatthew Ahrens 			return (ENOENT);
28873b12c289SMatthew Ahrens 		}
28883b12c289SMatthew Ahrens 		if (numericsid == NULL)
288914843421SMatthew Ahrens 			return (ENOENT);
28903b12c289SMatthew Ahrens 		cp = numericsid;
28911ed6b69aSGordon Ross 		*ridp = rid;
28923b12c289SMatthew Ahrens 		/* will be further decoded below */
28933b12c289SMatthew Ahrens 	}
28943b12c289SMatthew Ahrens 
28953b12c289SMatthew Ahrens 	if (strncmp(cp, "S-1-", 4) == 0) {
289614843421SMatthew Ahrens 		/* It's a numeric SID (eg "S-1-234-567-89") */
28973b12c289SMatthew Ahrens 		(void) strlcpy(domain, cp, domainlen);
28981ed6b69aSGordon Ross 		errno = 0;
28991ed6b69aSGordon Ross 		if (*ridp == 0) {
290014843421SMatthew Ahrens 			cp = strrchr(domain, '-');
290114843421SMatthew Ahrens 			*cp = '\0';
290214843421SMatthew Ahrens 			cp++;
290314843421SMatthew Ahrens 			*ridp = strtoull(cp, &end, 10);
29041ed6b69aSGordon Ross 		} else {
29051ed6b69aSGordon Ross 			end = "";
29061ed6b69aSGordon Ross 		}
29073b12c289SMatthew Ahrens 		if (numericsid) {
29083b12c289SMatthew Ahrens 			free(numericsid);
29093b12c289SMatthew Ahrens 			numericsid = NULL;
29103b12c289SMatthew Ahrens 		}
2911777badbaSMatthew Ahrens 		if (errno != 0 || *end != '\0')
291214843421SMatthew Ahrens 			return (EINVAL);
291314843421SMatthew Ahrens 	} else if (!isdigit(*cp)) {
291414843421SMatthew Ahrens 		/*
291514843421SMatthew Ahrens 		 * It's a user/group name (eg "user") that needs to be
291614843421SMatthew Ahrens 		 * turned into a uid/gid
291714843421SMatthew Ahrens 		 */
291814843421SMatthew Ahrens 		if (zoned && getzoneid() == GLOBAL_ZONEID)
291914843421SMatthew Ahrens 			return (ENOENT);
292014843421SMatthew Ahrens 		if (isuser) {
292114843421SMatthew Ahrens 			struct passwd *pw;
292214843421SMatthew Ahrens 			pw = getpwnam(cp);
292314843421SMatthew Ahrens 			if (pw == NULL)
292414843421SMatthew Ahrens 				return (ENOENT);
292514843421SMatthew Ahrens 			*ridp = pw->pw_uid;
292614843421SMatthew Ahrens 		} else {
292714843421SMatthew Ahrens 			struct group *gr;
292814843421SMatthew Ahrens 			gr = getgrnam(cp);
292914843421SMatthew Ahrens 			if (gr == NULL)
293014843421SMatthew Ahrens 				return (ENOENT);
293114843421SMatthew Ahrens 			*ridp = gr->gr_gid;
293214843421SMatthew Ahrens 		}
293314843421SMatthew Ahrens 	} else {
293414843421SMatthew Ahrens 		/* It's a user/group ID (eg "12345"). */
293514843421SMatthew Ahrens 		uid_t id = strtoul(cp, &end, 10);
293614843421SMatthew Ahrens 		idmap_rid_t rid;
293714843421SMatthew Ahrens 		char *mapdomain;
293814843421SMatthew Ahrens 
293914843421SMatthew Ahrens 		if (*end != '\0')
294014843421SMatthew Ahrens 			return (EINVAL);
294114843421SMatthew Ahrens 		if (id > MAXUID) {
294214843421SMatthew Ahrens 			/* It's an ephemeral ID. */
294314843421SMatthew Ahrens 			if (idmap_id_to_numeric_domain_rid(id, isuser,
294414843421SMatthew Ahrens 			    &mapdomain, &rid) != 0)
294514843421SMatthew Ahrens 				return (ENOENT);
29463b12c289SMatthew Ahrens 			(void) strlcpy(domain, mapdomain, domainlen);
294714843421SMatthew Ahrens 			*ridp = rid;
294814843421SMatthew Ahrens 		} else {
294914843421SMatthew Ahrens 			*ridp = id;
295014843421SMatthew Ahrens 		}
295114843421SMatthew Ahrens 	}
295214843421SMatthew Ahrens 
29533b12c289SMatthew Ahrens 	ASSERT3P(numericsid, ==, NULL);
295414843421SMatthew Ahrens 	return (0);
295514843421SMatthew Ahrens }
295614843421SMatthew Ahrens 
2957edea4b55SLin Ling static int
2958edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2959edea4b55SLin Ling     uint64_t *propvalue, zfs_userquota_prop_t *typep)
296014843421SMatthew Ahrens {
296114843421SMatthew Ahrens 	int err;
296214843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
296314843421SMatthew Ahrens 
296419b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
296514843421SMatthew Ahrens 
296614843421SMatthew Ahrens 	err = userquota_propname_decode(propname,
296714843421SMatthew Ahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2968edea4b55SLin Ling 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2969edea4b55SLin Ling 	zc.zc_objset_type = *typep;
297014843421SMatthew Ahrens 	if (err)
297114843421SMatthew Ahrens 		return (err);
297214843421SMatthew Ahrens 
297314843421SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
297414843421SMatthew Ahrens 	if (err)
297514843421SMatthew Ahrens 		return (err);
297614843421SMatthew Ahrens 
2977edea4b55SLin Ling 	*propvalue = zc.zc_cookie;
2978edea4b55SLin Ling 	return (0);
2979edea4b55SLin Ling }
2980edea4b55SLin Ling 
2981edea4b55SLin Ling int
2982edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2983edea4b55SLin Ling     uint64_t *propvalue)
2984edea4b55SLin Ling {
2985edea4b55SLin Ling 	zfs_userquota_prop_t type;
2986edea4b55SLin Ling 
2987edea4b55SLin Ling 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2988edea4b55SLin Ling 	    &type));
2989edea4b55SLin Ling }
2990edea4b55SLin Ling 
2991edea4b55SLin Ling int
2992edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2993edea4b55SLin Ling     char *propbuf, int proplen, boolean_t literal)
2994edea4b55SLin Ling {
2995edea4b55SLin Ling 	int err;
2996edea4b55SLin Ling 	uint64_t propvalue;
2997edea4b55SLin Ling 	zfs_userquota_prop_t type;
2998edea4b55SLin Ling 
2999edea4b55SLin Ling 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3000edea4b55SLin Ling 	    &type);
3001edea4b55SLin Ling 
3002edea4b55SLin Ling 	if (err)
3003edea4b55SLin Ling 		return (err);
3004edea4b55SLin Ling 
300514843421SMatthew Ahrens 	if (literal) {
3006edea4b55SLin Ling 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3007edea4b55SLin Ling 	} else if (propvalue == 0 &&
300814843421SMatthew Ahrens 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
300914843421SMatthew Ahrens 		(void) strlcpy(propbuf, "none", proplen);
301014843421SMatthew Ahrens 	} else {
3011edea4b55SLin Ling 		zfs_nicenum(propvalue, propbuf, proplen);
301214843421SMatthew Ahrens 	}
301314843421SMatthew Ahrens 	return (0);
301414843421SMatthew Ahrens }
301514843421SMatthew Ahrens 
301619b94df9SMatthew Ahrens int
301719b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
301819b94df9SMatthew Ahrens     uint64_t *propvalue)
301919b94df9SMatthew Ahrens {
302019b94df9SMatthew Ahrens 	int err;
302119b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
302219b94df9SMatthew Ahrens 	const char *snapname;
302319b94df9SMatthew Ahrens 
302419b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
302519b94df9SMatthew Ahrens 
302619b94df9SMatthew Ahrens 	snapname = strchr(propname, '@') + 1;
302719b94df9SMatthew Ahrens 	if (strchr(snapname, '@')) {
302819b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
302919b94df9SMatthew Ahrens 	} else {
303019b94df9SMatthew Ahrens 		/* snapname is the short name, append it to zhp's fsname */
303119b94df9SMatthew Ahrens 		char *cp;
303219b94df9SMatthew Ahrens 
303319b94df9SMatthew Ahrens 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
303419b94df9SMatthew Ahrens 		    sizeof (zc.zc_value));
303519b94df9SMatthew Ahrens 		cp = strchr(zc.zc_value, '@');
303619b94df9SMatthew Ahrens 		if (cp != NULL)
303719b94df9SMatthew Ahrens 			*cp = '\0';
303819b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
303919b94df9SMatthew Ahrens 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
304019b94df9SMatthew Ahrens 	}
304119b94df9SMatthew Ahrens 
304219b94df9SMatthew Ahrens 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
304319b94df9SMatthew Ahrens 	if (err)
304419b94df9SMatthew Ahrens 		return (err);
304519b94df9SMatthew Ahrens 
304619b94df9SMatthew Ahrens 	*propvalue = zc.zc_cookie;
304719b94df9SMatthew Ahrens 	return (0);
304819b94df9SMatthew Ahrens }
304919b94df9SMatthew Ahrens 
305019b94df9SMatthew Ahrens int
305119b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
305219b94df9SMatthew Ahrens     char *propbuf, int proplen, boolean_t literal)
305319b94df9SMatthew Ahrens {
305419b94df9SMatthew Ahrens 	int err;
305519b94df9SMatthew Ahrens 	uint64_t propvalue;
305619b94df9SMatthew Ahrens 
305719b94df9SMatthew Ahrens 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
305819b94df9SMatthew Ahrens 
305919b94df9SMatthew Ahrens 	if (err)
306019b94df9SMatthew Ahrens 		return (err);
306119b94df9SMatthew Ahrens 
306219b94df9SMatthew Ahrens 	if (literal) {
306319b94df9SMatthew Ahrens 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
306419b94df9SMatthew Ahrens 	} else {
306519b94df9SMatthew Ahrens 		zfs_nicenum(propvalue, propbuf, proplen);
306619b94df9SMatthew Ahrens 	}
306719b94df9SMatthew Ahrens 	return (0);
306819b94df9SMatthew Ahrens }
306919b94df9SMatthew Ahrens 
3070fa9e4066Sahrens /*
3071fa9e4066Sahrens  * Returns the name of the given zfs handle.
3072fa9e4066Sahrens  */
3073fa9e4066Sahrens const char *
3074fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp)
3075fa9e4066Sahrens {
3076fa9e4066Sahrens 	return (zhp->zfs_name);
3077fa9e4066Sahrens }
3078fa9e4066Sahrens 
3079fa9e4066Sahrens /*
30808808ac5dSYuri Pankov  * Returns the name of the parent pool for the given zfs handle.
30818808ac5dSYuri Pankov  */
30828808ac5dSYuri Pankov const char *
30838808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp)
30848808ac5dSYuri Pankov {
30858808ac5dSYuri Pankov 	return (zhp->zpool_hdl->zpool_name);
30868808ac5dSYuri Pankov }
30878808ac5dSYuri Pankov 
30888808ac5dSYuri Pankov /*
3089fa9e4066Sahrens  * Returns the type of the given zfs handle.
3090fa9e4066Sahrens  */
3091fa9e4066Sahrens zfs_type_t
3092fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp)
3093fa9e4066Sahrens {
3094fa9e4066Sahrens 	return (zhp->zfs_type);
3095fa9e4066Sahrens }
3096fa9e4066Sahrens 
30977f7322feSeschrock /*
3098d41c4376SMark J Musante  * Is one dataset name a child dataset of another?
3099d41c4376SMark J Musante  *
3100d41c4376SMark J Musante  * Needs to handle these cases:
3101d41c4376SMark J Musante  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3102d41c4376SMark J Musante  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3103d41c4376SMark J Musante  * Descendant?	No.		No.		No.		Yes.
3104d41c4376SMark J Musante  */
3105d41c4376SMark J Musante static boolean_t
3106d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
3107d41c4376SMark J Musante {
3108d41c4376SMark J Musante 	size_t d1len = strlen(ds1);
3109d41c4376SMark J Musante 
3110d41c4376SMark J Musante 	/* ds2 can't be a descendant if it's smaller */
3111d41c4376SMark J Musante 	if (strlen(ds2) < d1len)
3112d41c4376SMark J Musante 		return (B_FALSE);
3113d41c4376SMark J Musante 
3114d41c4376SMark J Musante 	/* otherwise, compare strings and verify that there's a '/' char */
3115d41c4376SMark J Musante 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3116d41c4376SMark J Musante }
3117d41c4376SMark J Musante 
3118d41c4376SMark J Musante /*
3119fa9e4066Sahrens  * Given a complete name, return just the portion that refers to the parent.
312019b94df9SMatthew Ahrens  * Will return -1 if there is no parent (path is just the name of the
312119b94df9SMatthew Ahrens  * pool).
3122fa9e4066Sahrens  */
3123fa9e4066Sahrens static int
3124fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3125fa9e4066Sahrens {
312619b94df9SMatthew Ahrens 	char *slashp;
3127fa9e4066Sahrens 
312819b94df9SMatthew Ahrens 	(void) strlcpy(buf, path, buflen);
312919b94df9SMatthew Ahrens 
313019b94df9SMatthew Ahrens 	if ((slashp = strrchr(buf, '/')) == NULL)
3131fa9e4066Sahrens 		return (-1);
313219b94df9SMatthew Ahrens 	*slashp = '\0';
3133fa9e4066Sahrens 
3134fa9e4066Sahrens 	return (0);
3135fa9e4066Sahrens }
3136fa9e4066Sahrens 
3137fa9e4066Sahrens /*
31387f1f55eaSvb160487  * If accept_ancestor is false, then check to make sure that the given path has
31397f1f55eaSvb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
31407f1f55eaSvb160487  * closest existing ancestor for the given path.  In prefixlen return the
31417f1f55eaSvb160487  * length of already existing prefix of the given path.  We also fetch the
31427f1f55eaSvb160487  * 'zoned' property, which is used to validate property settings when creating
31437f1f55eaSvb160487  * new datasets.
3144fa9e4066Sahrens  */
3145fa9e4066Sahrens static int
31467f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
31477f1f55eaSvb160487     boolean_t accept_ancestor, int *prefixlen)
3148fa9e4066Sahrens {
3149fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
31509adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3151fa9e4066Sahrens 	char *slash;
31527f7322feSeschrock 	zfs_handle_t *zhp;
315399653d4eSeschrock 	char errbuf[1024];
3154d41c4376SMark J Musante 	uint64_t is_zoned;
315599653d4eSeschrock 
3156deb8317bSMark J Musante 	(void) snprintf(errbuf, sizeof (errbuf),
3157deb8317bSMark J Musante 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3158fa9e4066Sahrens 
3159fa9e4066Sahrens 	/* get parent, and check to see if this is just a pool */
3160fa9e4066Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
316199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
316299653d4eSeschrock 		    "missing dataset name"));
316399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3164fa9e4066Sahrens 	}
3165fa9e4066Sahrens 
3166fa9e4066Sahrens 	/* check to see if the pool exists */
3167fa9e4066Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
3168fa9e4066Sahrens 		slash = parent + strlen(parent);
31698ac09fceSRichard Lowe 	(void) strncpy(zc.zc_name, parent, slash - parent);
3170fa9e4066Sahrens 	zc.zc_name[slash - parent] = '\0';
317199653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3172fa9e4066Sahrens 	    errno == ENOENT) {
317399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
317499653d4eSeschrock 		    "no such pool '%s'"), zc.zc_name);
317599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3176fa9e4066Sahrens 	}
3177fa9e4066Sahrens 
3178fa9e4066Sahrens 	/* check to see if the parent dataset exists */
31797f1f55eaSvb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
31807f1f55eaSvb160487 		if (errno == ENOENT && accept_ancestor) {
31817f1f55eaSvb160487 			/*
31827f1f55eaSvb160487 			 * Go deeper to find an ancestor, give up on top level.
31837f1f55eaSvb160487 			 */
31847f1f55eaSvb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
31857f1f55eaSvb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31867f1f55eaSvb160487 				    "no such pool '%s'"), zc.zc_name);
31877f1f55eaSvb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
31887f1f55eaSvb160487 			}
31897f1f55eaSvb160487 		} else if (errno == ENOENT) {
319099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
319199653d4eSeschrock 			    "parent does not exist"));
319299653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
31937f1f55eaSvb160487 		} else
319499653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3195fa9e4066Sahrens 	}
3196fa9e4066Sahrens 
3197d41c4376SMark J Musante 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3198d41c4376SMark J Musante 	if (zoned != NULL)
3199d41c4376SMark J Musante 		*zoned = is_zoned;
3200d41c4376SMark J Musante 
3201fa9e4066Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
3202d41c4376SMark J Musante 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
320399653d4eSeschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
32047f7322feSeschrock 		zfs_close(zhp);
3205fa9e4066Sahrens 		return (-1);
3206fa9e4066Sahrens 	}
3207fa9e4066Sahrens 
3208fa9e4066Sahrens 	/* make sure parent is a filesystem */
32097f7322feSeschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
321099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
321199653d4eSeschrock 		    "parent is not a filesystem"));
321299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
32137f7322feSeschrock 		zfs_close(zhp);
3214fa9e4066Sahrens 		return (-1);
3215fa9e4066Sahrens 	}
3216fa9e4066Sahrens 
32177f7322feSeschrock 	zfs_close(zhp);
32187f1f55eaSvb160487 	if (prefixlen != NULL)
32197f1f55eaSvb160487 		*prefixlen = strlen(parent);
32207f1f55eaSvb160487 	return (0);
32217f1f55eaSvb160487 }
32227f1f55eaSvb160487 
32237f1f55eaSvb160487 /*
32247f1f55eaSvb160487  * Finds whether the dataset of the given type(s) exists.
32257f1f55eaSvb160487  */
32267f1f55eaSvb160487 boolean_t
32277f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
32287f1f55eaSvb160487 {
32297f1f55eaSvb160487 	zfs_handle_t *zhp;
32307f1f55eaSvb160487 
3231f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
32327f1f55eaSvb160487 		return (B_FALSE);
32337f1f55eaSvb160487 
32347f1f55eaSvb160487 	/*
32357f1f55eaSvb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
32367f1f55eaSvb160487 	 */
32377f1f55eaSvb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
32387f1f55eaSvb160487 		int ds_type = zhp->zfs_type;
32397f1f55eaSvb160487 
32407f1f55eaSvb160487 		zfs_close(zhp);
32417f1f55eaSvb160487 		if (types & ds_type)
32427f1f55eaSvb160487 			return (B_TRUE);
32437f1f55eaSvb160487 	}
32447f1f55eaSvb160487 	return (B_FALSE);
32457f1f55eaSvb160487 }
32467f1f55eaSvb160487 
32477f1f55eaSvb160487 /*
32483cb34c60Sahrens  * Given a path to 'target', create all the ancestors between
32493cb34c60Sahrens  * the prefixlen portion of the path, and the target itself.
32503cb34c60Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
32513cb34c60Sahrens  */
32523cb34c60Sahrens int
32533cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
32543cb34c60Sahrens {
32553cb34c60Sahrens 	zfs_handle_t *h;
32563cb34c60Sahrens 	char *cp;
32573cb34c60Sahrens 	const char *opname;
32583cb34c60Sahrens 
32593cb34c60Sahrens 	/* make sure prefix exists */
32603cb34c60Sahrens 	cp = target + prefixlen;
32613cb34c60Sahrens 	if (*cp != '/') {
32623cb34c60Sahrens 		assert(strchr(cp, '/') == NULL);
32633cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
32643cb34c60Sahrens 	} else {
32653cb34c60Sahrens 		*cp = '\0';
32663cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
32673cb34c60Sahrens 		*cp = '/';
32683cb34c60Sahrens 	}
32693cb34c60Sahrens 	if (h == NULL)
32703cb34c60Sahrens 		return (-1);
32713cb34c60Sahrens 	zfs_close(h);
32723cb34c60Sahrens 
32733cb34c60Sahrens 	/*
32743cb34c60Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
32753cb34c60Sahrens 	 * up to the prefixlen-long one.
32763cb34c60Sahrens 	 */
32773cb34c60Sahrens 	for (cp = target + prefixlen + 1;
327888f61deeSIgor Kozhukhov 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
32793cb34c60Sahrens 
32803cb34c60Sahrens 		*cp = '\0';
32813cb34c60Sahrens 
32823cb34c60Sahrens 		h = make_dataset_handle(hdl, target);
32833cb34c60Sahrens 		if (h) {
32843cb34c60Sahrens 			/* it already exists, nothing to do here */
32853cb34c60Sahrens 			zfs_close(h);
32863cb34c60Sahrens 			continue;
32873cb34c60Sahrens 		}
32883cb34c60Sahrens 
32893cb34c60Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
32903cb34c60Sahrens 		    NULL) != 0) {
32913cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
32923cb34c60Sahrens 			goto ancestorerr;
32933cb34c60Sahrens 		}
32943cb34c60Sahrens 
32953cb34c60Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
32963cb34c60Sahrens 		if (h == NULL) {
32973cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
32983cb34c60Sahrens 			goto ancestorerr;
32993cb34c60Sahrens 		}
33003cb34c60Sahrens 
33013cb34c60Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
33023cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
33033cb34c60Sahrens 			goto ancestorerr;
33043cb34c60Sahrens 		}
33053cb34c60Sahrens 
33063cb34c60Sahrens 		if (zfs_share(h) != 0) {
33073cb34c60Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
33083cb34c60Sahrens 			goto ancestorerr;
33093cb34c60Sahrens 		}
33103cb34c60Sahrens 
33113cb34c60Sahrens 		zfs_close(h);
33123cb34c60Sahrens 	}
33133cb34c60Sahrens 
33143cb34c60Sahrens 	return (0);
33153cb34c60Sahrens 
33163cb34c60Sahrens ancestorerr:
33173cb34c60Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33183cb34c60Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
33193cb34c60Sahrens 	return (-1);
33203cb34c60Sahrens }
33213cb34c60Sahrens 
33223cb34c60Sahrens /*
33237f1f55eaSvb160487  * Creates non-existing ancestors of the given path.
33247f1f55eaSvb160487  */
33257f1f55eaSvb160487 int
33267f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
33277f1f55eaSvb160487 {
33287f1f55eaSvb160487 	int prefix;
33297f1f55eaSvb160487 	char *path_copy;
3330f83b46baSPaul Dagnelie 	int rc = 0;
33317f1f55eaSvb160487 
3332d41c4376SMark J Musante 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
33337f1f55eaSvb160487 		return (-1);
33347f1f55eaSvb160487 
33357f1f55eaSvb160487 	if ((path_copy = strdup(path)) != NULL) {
33367f1f55eaSvb160487 		rc = create_parents(hdl, path_copy, prefix);
33377f1f55eaSvb160487 		free(path_copy);
33387f1f55eaSvb160487 	}
33397f1f55eaSvb160487 	if (path_copy == NULL || rc != 0)
33407f1f55eaSvb160487 		return (-1);
33417f1f55eaSvb160487 
3342fa9e4066Sahrens 	return (0);
3343fa9e4066Sahrens }
3344fa9e4066Sahrens 
3345fa9e4066Sahrens /*
3346e9dbad6fSeschrock  * Create a new filesystem or volume.
3347fa9e4066Sahrens  */
3348fa9e4066Sahrens int
334999653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3350e9dbad6fSeschrock     nvlist_t *props)
3351fa9e4066Sahrens {
3352fa9e4066Sahrens 	int ret;
3353fa9e4066Sahrens 	uint64_t size = 0;
3354fa9e4066Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
335599653d4eSeschrock 	char errbuf[1024];
3356e9dbad6fSeschrock 	uint64_t zoned;
335726455f9eSAndriy Gapon 	enum lzc_dataset_type ost;
3358690031d3Sloli10K 	zpool_handle_t *zpool_handle;
335999653d4eSeschrock 
336099653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
336199653d4eSeschrock 	    "cannot create '%s'"), path);
3362fa9e4066Sahrens 
3363fa9e4066Sahrens 	/* validate the path, taking care to note the extended error message */
3364f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
336599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3366fa9e4066Sahrens 
3367fa9e4066Sahrens 	/* validate parents exist */
33687f1f55eaSvb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3369fa9e4066Sahrens 		return (-1);
3370fa9e4066Sahrens 
3371fa9e4066Sahrens 	/*
3372fa9e4066Sahrens 	 * The failure modes when creating a dataset of a different type over
3373fa9e4066Sahrens 	 * one that already exists is a little strange.  In particular, if you
3374fa9e4066Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3375fa9e4066Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3376fa9e4066Sahrens 	 * first try to see if the dataset exists.
3377fa9e4066Sahrens 	 */
33784445fffbSMatthew Ahrens 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
337999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
338099653d4eSeschrock 		    "dataset already exists"));
338199653d4eSeschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3382fa9e4066Sahrens 	}
3383fa9e4066Sahrens 
3384fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME)
338526455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZVOL;
3386fa9e4066Sahrens 	else
338726455f9eSAndriy Gapon 		ost = LZC_DATSET_TYPE_ZFS;
3388fa9e4066Sahrens 
3389e9316f76SJoe Stein 	/* open zpool handle for prop validation */
33909adfa60dSMatthew Ahrens 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3391e9316f76SJoe Stein 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3392e9316f76SJoe Stein 
3393e9316f76SJoe Stein 	/* truncate pool_path at first slash */
3394e9316f76SJoe Stein 	char *p = strchr(pool_path, '/');
3395e9316f76SJoe Stein 	if (p != NULL)
3396e9316f76SJoe Stein 		*p = '\0';
3397e9316f76SJoe Stein 
3398690031d3Sloli10K 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3399690031d3Sloli10K 		return (-1);
3400e9316f76SJoe Stein 
34010a48a24eStimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3402e9316f76SJoe Stein 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3403e9316f76SJoe Stein 		zpool_close(zpool_handle);
3404e9dbad6fSeschrock 		return (-1);
3405e9316f76SJoe Stein 	}
3406e9316f76SJoe Stein 	zpool_close(zpool_handle);
3407e9dbad6fSeschrock 
3408fa9e4066Sahrens 	if (type == ZFS_TYPE_VOLUME) {
34095c5460e9Seschrock 		/*
34105c5460e9Seschrock 		 * If we are creating a volume, the size and block size must
34115c5460e9Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
34125c5460e9Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
34135c5460e9Seschrock 		 * volsize must be a multiple of the block size, and cannot be
34145c5460e9Seschrock 		 * zero.
34155c5460e9Seschrock 		 */
3416e9dbad6fSeschrock 		if (props == NULL || nvlist_lookup_uint64(props,
3417e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3418e9dbad6fSeschrock 			nvlist_free(props);
341999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3420e9dbad6fSeschrock 			    "missing volume size"));
3421e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3422fa9e4066Sahrens 		}
3423fa9e4066Sahrens 
3424e9dbad6fSeschrock 		if ((ret = nvlist_lookup_uint64(props,
3425e9dbad6fSeschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3426e9dbad6fSeschrock 		    &blocksize)) != 0) {
3427e9dbad6fSeschrock 			if (ret == ENOENT) {
3428e9dbad6fSeschrock 				blocksize = zfs_prop_default_numeric(
3429e9dbad6fSeschrock 				    ZFS_PROP_VOLBLOCKSIZE);
3430e9dbad6fSeschrock 			} else {
3431e9dbad6fSeschrock 				nvlist_free(props);
343299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3433e9dbad6fSeschrock 				    "missing volume block size"));
3434e9dbad6fSeschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3435e9dbad6fSeschrock 			}
3436e9dbad6fSeschrock 		}
3437e9dbad6fSeschrock 
3438e9dbad6fSeschrock 		if (size == 0) {
3439e9dbad6fSeschrock 			nvlist_free(props);
3440e9dbad6fSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3441e9dbad6fSeschrock 			    "volume size cannot be zero"));
3442e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
34435c5460e9Seschrock 		}
34445c5460e9Seschrock 
34455c5460e9Seschrock 		if (size % blocksize != 0) {
3446e9dbad6fSeschrock 			nvlist_free(props);
344799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3448e9dbad6fSeschrock 			    "volume size must be a multiple of volume block "
3449e9dbad6fSeschrock 			    "size"));
3450e9dbad6fSeschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3451e9dbad6fSeschrock 		}
34525c5460e9Seschrock 	}
34535c5460e9Seschrock 
3454fa9e4066Sahrens 	/* create the dataset */
34554445fffbSMatthew Ahrens 	ret = lzc_create(path, ost, props);
34564445fffbSMatthew Ahrens 	nvlist_free(props);
3457e9dbad6fSeschrock 
3458fa9e4066Sahrens 	/* check for failure */
3459fa9e4066Sahrens 	if (ret != 0) {
34609adfa60dSMatthew Ahrens 		char parent[ZFS_MAX_DATASET_NAME_LEN];
3461fa9e4066Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3462fa9e4066Sahrens 
3463fa9e4066Sahrens 		switch (errno) {
3464fa9e4066Sahrens 		case ENOENT:
346599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
346699653d4eSeschrock 			    "no such parent '%s'"), parent);
346799653d4eSeschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3468fa9e4066Sahrens 
3469fa9e4066Sahrens 		case EINVAL:
347099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3471d7d4af51Smmusante 			    "parent '%s' is not a filesystem"), parent);
347299653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3473fa9e4066Sahrens 
347440feaa91Sahrens 		case ENOTSUP:
347540feaa91Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
347640feaa91Sahrens 			    "pool must be upgraded to set this "
347740feaa91Sahrens 			    "property or value"));
347840feaa91Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
34799fa2266dSYuri Pankov 		case ERANGE:
34809fa2266dSYuri Pankov 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34819fa2266dSYuri Pankov 			    "invalid property value(s) specified"));
34829fa2266dSYuri Pankov 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3483fa9e4066Sahrens #ifdef _ILP32
3484fa9e4066Sahrens 		case EOVERFLOW:
3485fa9e4066Sahrens 			/*
3486fa9e4066Sahrens 			 * This platform can't address a volume this big.
3487fa9e4066Sahrens 			 */
348899653d4eSeschrock 			if (type == ZFS_TYPE_VOLUME)
348999653d4eSeschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
349099653d4eSeschrock 				    errbuf));
3491fa9e4066Sahrens #endif
349299653d4eSeschrock 			/* FALLTHROUGH */
3493fa9e4066Sahrens 		default:
349499653d4eSeschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3495fa9e4066Sahrens 		}
3496fa9e4066Sahrens 	}
3497fa9e4066Sahrens 
3498fa9e4066Sahrens 	return (0);
3499fa9e4066Sahrens }
3500fa9e4066Sahrens 
3501fa9e4066Sahrens /*
3502fa9e4066Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
350365fec9f6SChristopher Siden  * isn't mounted, and that there are no active dependents. If the file system
350465fec9f6SChristopher Siden  * does not exist this function does nothing.
3505fa9e4066Sahrens  */
3506fa9e4066Sahrens int
3507842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3508fa9e4066Sahrens {
3509fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
3510fa9e4066Sahrens 
351178f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
351278f17100SMatthew Ahrens 		nvlist_t *nv = fnvlist_alloc();
351378f17100SMatthew Ahrens 		fnvlist_add_boolean(nv, zhp->zfs_name);
351478f17100SMatthew Ahrens 		int error = lzc_destroy_bookmarks(nv, NULL);
351578f17100SMatthew Ahrens 		fnvlist_free(nv);
351678f17100SMatthew Ahrens 		if (error != 0) {
351778f17100SMatthew Ahrens 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
351878f17100SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
351978f17100SMatthew Ahrens 			    zhp->zfs_name));
352078f17100SMatthew Ahrens 		}
352178f17100SMatthew Ahrens 		return (0);
352278f17100SMatthew Ahrens 	}
352378f17100SMatthew Ahrens 
3524fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3525fa9e4066Sahrens 
3526e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp)) {
3527fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3528fa9e4066Sahrens 	} else {
3529fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3530fa9e4066Sahrens 	}
3531fa9e4066Sahrens 
3532842727c2SChris Kirby 	zc.zc_defer_destroy = defer;
353365fec9f6SChristopher Siden 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
353465fec9f6SChristopher Siden 	    errno != ENOENT) {
3535ece3d9b3Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
353699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
353799653d4eSeschrock 		    zhp->zfs_name));
35381d452cf5Sahrens 	}
3539fa9e4066Sahrens 
3540fa9e4066Sahrens 	remove_mountpoint(zhp);
3541fa9e4066Sahrens 
3542fa9e4066Sahrens 	return (0);
3543fa9e4066Sahrens }
3544fa9e4066Sahrens 
35451d452cf5Sahrens struct destroydata {
354619b94df9SMatthew Ahrens 	nvlist_t *nvl;
354719b94df9SMatthew Ahrens 	const char *snapname;
35481d452cf5Sahrens };
35491d452cf5Sahrens 
35501d452cf5Sahrens static int
3551681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
35521d452cf5Sahrens {
35531d452cf5Sahrens 	struct destroydata *dd = arg;
35549adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3555681d9761SEric Taylor 	int rv = 0;
35561d452cf5Sahrens 
355719b94df9SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
355819b94df9SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, dd->snapname);
35591d452cf5Sahrens 
3560a7a845e4SSteven Hartland 	if (lzc_exists(name))
356119b94df9SMatthew Ahrens 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
35621d452cf5Sahrens 
356319b94df9SMatthew Ahrens 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
35643ccfa83cSahrens 	zfs_close(zhp);
35653ccfa83cSahrens 	return (rv);
35661d452cf5Sahrens }
35671d452cf5Sahrens 
35681d452cf5Sahrens /*
35691d452cf5Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
35701d452cf5Sahrens  */
35711d452cf5Sahrens int
3572842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
35731d452cf5Sahrens {
35741d452cf5Sahrens 	int ret;
35751d452cf5Sahrens 	struct destroydata dd = { 0 };
35761d452cf5Sahrens 
35771d452cf5Sahrens 	dd.snapname = snapname;
357819b94df9SMatthew Ahrens 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
357919b94df9SMatthew Ahrens 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
35801d452cf5Sahrens 
3581a7a845e4SSteven Hartland 	if (nvlist_empty(dd.nvl)) {
358219b94df9SMatthew Ahrens 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
35831d452cf5Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
358419b94df9SMatthew Ahrens 		    zhp->zfs_name, snapname);
358519b94df9SMatthew Ahrens 	} else {
35863b2aab18SMatthew Ahrens 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
358719b94df9SMatthew Ahrens 	}
358819b94df9SMatthew Ahrens 	nvlist_free(dd.nvl);
358919b94df9SMatthew Ahrens 	return (ret);
3590e5351341SMatthew Ahrens }
3591e5351341SMatthew Ahrens 
359219b94df9SMatthew Ahrens /*
35933b2aab18SMatthew Ahrens  * Destroys all the snapshots named in the nvlist.
359419b94df9SMatthew Ahrens  */
359519b94df9SMatthew Ahrens int
35963b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
359719b94df9SMatthew Ahrens {
359819b94df9SMatthew Ahrens 	int ret;
35994cde22c2SChris Williamson 	nvlist_t *errlist = NULL;
360019b94df9SMatthew Ahrens 
36014445fffbSMatthew Ahrens 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
36021d452cf5Sahrens 
36034cde22c2SChris Williamson 	if (ret == 0) {
36044cde22c2SChris Williamson 		nvlist_free(errlist);
36053b2aab18SMatthew Ahrens 		return (0);
36064cde22c2SChris Williamson 	}
36073b2aab18SMatthew Ahrens 
3608a7a845e4SSteven Hartland 	if (nvlist_empty(errlist)) {
36093b2aab18SMatthew Ahrens 		char errbuf[1024];
36103b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
36113b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
36123b2aab18SMatthew Ahrens 
36133b2aab18SMatthew Ahrens 		ret = zfs_standard_error(hdl, ret, errbuf);
36143b2aab18SMatthew Ahrens 	}
36154445fffbSMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
36164445fffbSMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
36171d452cf5Sahrens 		char errbuf[1024];
36184445fffbSMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
36194445fffbSMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
36204445fffbSMatthew Ahrens 		    nvpair_name(pair));
36211d452cf5Sahrens 
36224445fffbSMatthew Ahrens 		switch (fnvpair_value_int32(pair)) {
36231d452cf5Sahrens 		case EEXIST:
36243b2aab18SMatthew Ahrens 			zfs_error_aux(hdl,
36253b2aab18SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
36263b2aab18SMatthew Ahrens 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
36274445fffbSMatthew Ahrens 			break;
36281d452cf5Sahrens 		default:
36293b2aab18SMatthew Ahrens 			ret = zfs_standard_error(hdl, errno, errbuf);
36304445fffbSMatthew Ahrens 			break;
36314445fffbSMatthew Ahrens 		}
36321d452cf5Sahrens 	}
36331d452cf5Sahrens 
36344cde22c2SChris Williamson 	nvlist_free(errlist);
36354445fffbSMatthew Ahrens 	return (ret);
36361d452cf5Sahrens }
36371d452cf5Sahrens 
3638fa9e4066Sahrens /*
3639fa9e4066Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3640fa9e4066Sahrens  */
3641fa9e4066Sahrens int
3642e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3643fa9e4066Sahrens {
36449adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3645fa9e4066Sahrens 	int ret;
364699653d4eSeschrock 	char errbuf[1024];
364799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3648e9dbad6fSeschrock 	uint64_t zoned;
3649fa9e4066Sahrens 
3650fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3651fa9e4066Sahrens 
365299653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
365399653d4eSeschrock 	    "cannot create '%s'"), target);
365499653d4eSeschrock 
365519b94df9SMatthew Ahrens 	/* validate the target/clone name */
3656f18faf3fSek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
365799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3658fa9e4066Sahrens 
3659fa9e4066Sahrens 	/* validate parents exist */
36607f1f55eaSvb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3661fa9e4066Sahrens 		return (-1);
3662fa9e4066Sahrens 
3663fa9e4066Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3664fa9e4066Sahrens 
3665fa9e4066Sahrens 	/* do the clone */
3666e9dbad6fSeschrock 
3667e9dbad6fSeschrock 	if (props) {
36684445fffbSMatthew Ahrens 		zfs_type_t type;
36694445fffbSMatthew Ahrens 		if (ZFS_IS_VOLUME(zhp)) {
36704445fffbSMatthew Ahrens 			type = ZFS_TYPE_VOLUME;
36714445fffbSMatthew Ahrens 		} else {
36724445fffbSMatthew Ahrens 			type = ZFS_TYPE_FILESYSTEM;
36734445fffbSMatthew Ahrens 		}
36740a48a24eStimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3675e9316f76SJoe Stein 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3676e9dbad6fSeschrock 			return (-1);
3677e9dbad6fSeschrock 	}
3678e9dbad6fSeschrock 
36794445fffbSMatthew Ahrens 	ret = lzc_clone(target, zhp->zfs_name, props);
3680e9dbad6fSeschrock 	nvlist_free(props);
3681e9dbad6fSeschrock 
3682fa9e4066Sahrens 	if (ret != 0) {
3683fa9e4066Sahrens 		switch (errno) {
3684fa9e4066Sahrens 
3685fa9e4066Sahrens 		case ENOENT:
3686fa9e4066Sahrens 			/*
3687fa9e4066Sahrens 			 * The parent doesn't exist.  We should have caught this
3688fa9e4066Sahrens 			 * above, but there may a race condition that has since
3689fa9e4066Sahrens 			 * destroyed the parent.
3690fa9e4066Sahrens 			 *
3691fa9e4066Sahrens 			 * At this point, we don't know whether it's the source
3692fa9e4066Sahrens 			 * that doesn't exist anymore, or whether the target
3693fa9e4066Sahrens 			 * dataset doesn't exist.
3694fa9e4066Sahrens 			 */
369599653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
369699653d4eSeschrock 			    "no such parent '%s'"), parent);
369799653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3698fa9e4066Sahrens 
369999653d4eSeschrock 		case EXDEV:
370099653d4eSeschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
370199653d4eSeschrock 			    "source and target pools differ"));
370299653d4eSeschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
370399653d4eSeschrock 			    errbuf));
370499653d4eSeschrock 
370599653d4eSeschrock 		default:
370699653d4eSeschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
370799653d4eSeschrock 			    errbuf));
370899653d4eSeschrock 		}
370999653d4eSeschrock 	}
371099653d4eSeschrock 
371199653d4eSeschrock 	return (ret);
371299653d4eSeschrock }
371399653d4eSeschrock 
371499653d4eSeschrock /*
371599653d4eSeschrock  * Promotes the given clone fs to be the clone parent.
371699653d4eSeschrock  */
371799653d4eSeschrock int
371899653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
371999653d4eSeschrock {
372099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3721a4b8c9aaSAndrew Stormont 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
372299653d4eSeschrock 	int ret;
372399653d4eSeschrock 	char errbuf[1024];
372499653d4eSeschrock 
372599653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
372699653d4eSeschrock 	    "cannot promote '%s'"), zhp->zfs_name);
372799653d4eSeschrock 
372899653d4eSeschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
372999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
373099653d4eSeschrock 		    "snapshots can not be promoted"));
373199653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
373299653d4eSeschrock 	}
373399653d4eSeschrock 
3734a4b8c9aaSAndrew Stormont 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
373599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
373699653d4eSeschrock 		    "not a cloned filesystem"));
373799653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
373899653d4eSeschrock 	}
373999653d4eSeschrock 
3740*add927f8Sloli10K 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3741*add927f8Sloli10K 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3742*add927f8Sloli10K 
3743a4b8c9aaSAndrew Stormont 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
374499653d4eSeschrock 
374599653d4eSeschrock 	if (ret != 0) {
3746a4b8c9aaSAndrew Stormont 		switch (ret) {
3747fa9e4066Sahrens 		case EEXIST:
3748681d9761SEric Taylor 			/* There is a conflicting snapshot name. */
374999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3750681d9761SEric Taylor 			    "conflicting snapshot '%s' from parent '%s'"),
3751a4b8c9aaSAndrew Stormont 			    snapname, zhp->zfs_dmustats.dds_origin);
375299653d4eSeschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3753fa9e4066Sahrens 
3754fa9e4066Sahrens 		default:
3755a4b8c9aaSAndrew Stormont 			return (zfs_standard_error(hdl, ret, errbuf));
3756fa9e4066Sahrens 		}
3757fa9e4066Sahrens 	}
3758e9dbad6fSeschrock 	return (ret);
37591d452cf5Sahrens }
37601d452cf5Sahrens 
37614445fffbSMatthew Ahrens typedef struct snapdata {
37624445fffbSMatthew Ahrens 	nvlist_t *sd_nvl;
37634445fffbSMatthew Ahrens 	const char *sd_snapname;
37644445fffbSMatthew Ahrens } snapdata_t;
37654445fffbSMatthew Ahrens 
37664445fffbSMatthew Ahrens static int
37674445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
37684445fffbSMatthew Ahrens {
37694445fffbSMatthew Ahrens 	snapdata_t *sd = arg;
37709adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
37714445fffbSMatthew Ahrens 	int rv = 0;
37724445fffbSMatthew Ahrens 
3773ca48f36fSKeith M Wesolowski 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
37744445fffbSMatthew Ahrens 		(void) snprintf(name, sizeof (name),
37754445fffbSMatthew Ahrens 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
37764445fffbSMatthew Ahrens 
37774445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd->sd_nvl, name);
37784445fffbSMatthew Ahrens 
37794445fffbSMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3780ca48f36fSKeith M Wesolowski 	}
37814445fffbSMatthew Ahrens 	zfs_close(zhp);
3782ca48f36fSKeith M Wesolowski 
37834445fffbSMatthew Ahrens 	return (rv);
37844445fffbSMatthew Ahrens }
37854445fffbSMatthew Ahrens 
37865cabbc6bSPrashanth Sreenivasa int
37875cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
37885cabbc6bSPrashanth Sreenivasa {
37895cabbc6bSPrashanth Sreenivasa 	int err;
37905cabbc6bSPrashanth Sreenivasa 	char errbuf[1024];
37915cabbc6bSPrashanth Sreenivasa 
37925cabbc6bSPrashanth Sreenivasa 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37935cabbc6bSPrashanth Sreenivasa 	    "cannot remap filesystem '%s' "), fs);
37945cabbc6bSPrashanth Sreenivasa 
37955cabbc6bSPrashanth Sreenivasa 	err = lzc_remap(fs);
37965cabbc6bSPrashanth Sreenivasa 
37975cabbc6bSPrashanth Sreenivasa 	if (err != 0) {
37985cabbc6bSPrashanth Sreenivasa 		(void) zfs_standard_error(hdl, err, errbuf);
37995cabbc6bSPrashanth Sreenivasa 	}
38005cabbc6bSPrashanth Sreenivasa 
38015cabbc6bSPrashanth Sreenivasa 	return (err);
38025cabbc6bSPrashanth Sreenivasa }
38035cabbc6bSPrashanth Sreenivasa 
3804fa9e4066Sahrens /*
38054445fffbSMatthew Ahrens  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
38064445fffbSMatthew Ahrens  * created.
3807fa9e4066Sahrens  */
3808fa9e4066Sahrens int
38094445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
38104445fffbSMatthew Ahrens {
38114445fffbSMatthew Ahrens 	int ret;
38124445fffbSMatthew Ahrens 	char errbuf[1024];
38134445fffbSMatthew Ahrens 	nvpair_t *elem;
38144445fffbSMatthew Ahrens 	nvlist_t *errors;
38154445fffbSMatthew Ahrens 
38164445fffbSMatthew Ahrens 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38174445fffbSMatthew Ahrens 	    "cannot create snapshots "));
38184445fffbSMatthew Ahrens 
38194445fffbSMatthew Ahrens 	elem = NULL;
38204445fffbSMatthew Ahrens 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
38214445fffbSMatthew Ahrens 		const char *snapname = nvpair_name(elem);
38224445fffbSMatthew Ahrens 
38234445fffbSMatthew Ahrens 		/* validate the target name */
38244445fffbSMatthew Ahrens 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
38254445fffbSMatthew Ahrens 		    B_TRUE)) {
38264445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
38274445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
38284445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), snapname);
38294445fffbSMatthew Ahrens 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
38304445fffbSMatthew Ahrens 		}
38314445fffbSMatthew Ahrens 	}
38324445fffbSMatthew Ahrens 
3833e9316f76SJoe Stein 	/*
3834e9316f76SJoe Stein 	 * get pool handle for prop validation. assumes all snaps are in the
3835e9316f76SJoe Stein 	 * same pool, as does lzc_snapshot (below).
3836e9316f76SJoe Stein 	 */
38379adfa60dSMatthew Ahrens 	char pool[ZFS_MAX_DATASET_NAME_LEN];
3838e9316f76SJoe Stein 	elem = nvlist_next_nvpair(snaps, NULL);
3839e9316f76SJoe Stein 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3840e9316f76SJoe Stein 	pool[strcspn(pool, "/@")] = '\0';
3841e9316f76SJoe Stein 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3842e9316f76SJoe Stein 
38434445fffbSMatthew Ahrens 	if (props != NULL &&
38444445fffbSMatthew Ahrens 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3845e9316f76SJoe Stein 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3846e9316f76SJoe Stein 		zpool_close(zpool_hdl);
38474445fffbSMatthew Ahrens 		return (-1);
38484445fffbSMatthew Ahrens 	}
3849e9316f76SJoe Stein 	zpool_close(zpool_hdl);
38504445fffbSMatthew Ahrens 
38514445fffbSMatthew Ahrens 	ret = lzc_snapshot(snaps, props, &errors);
38524445fffbSMatthew Ahrens 
38534445fffbSMatthew Ahrens 	if (ret != 0) {
38544445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
38554445fffbSMatthew Ahrens 		for (elem = nvlist_next_nvpair(errors, NULL);
38564445fffbSMatthew Ahrens 		    elem != NULL;
38574445fffbSMatthew Ahrens 		    elem = nvlist_next_nvpair(errors, elem)) {
38584445fffbSMatthew Ahrens 			(void) snprintf(errbuf, sizeof (errbuf),
38594445fffbSMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
38604445fffbSMatthew Ahrens 			    "cannot create snapshot '%s'"), nvpair_name(elem));
38614445fffbSMatthew Ahrens 			(void) zfs_standard_error(hdl,
38624445fffbSMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
38634445fffbSMatthew Ahrens 			printed = B_TRUE;
38644445fffbSMatthew Ahrens 		}
38654445fffbSMatthew Ahrens 		if (!printed) {
38664445fffbSMatthew Ahrens 			switch (ret) {
38674445fffbSMatthew Ahrens 			case EXDEV:
38684445fffbSMatthew Ahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38694445fffbSMatthew Ahrens 				    "multiple snapshots of same "
38704445fffbSMatthew Ahrens 				    "fs not allowed"));
38714445fffbSMatthew Ahrens 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38724445fffbSMatthew Ahrens 
38734445fffbSMatthew Ahrens 				break;
38744445fffbSMatthew Ahrens 			default:
38754445fffbSMatthew Ahrens 				(void) zfs_standard_error(hdl, ret, errbuf);
38764445fffbSMatthew Ahrens 			}
38774445fffbSMatthew Ahrens 		}
38784445fffbSMatthew Ahrens 	}
38794445fffbSMatthew Ahrens 
38804445fffbSMatthew Ahrens 	nvlist_free(props);
38814445fffbSMatthew Ahrens 	nvlist_free(errors);
38824445fffbSMatthew Ahrens 	return (ret);
38834445fffbSMatthew Ahrens }
38844445fffbSMatthew Ahrens 
38854445fffbSMatthew Ahrens int
3886bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3887bb0ade09Sahrens     nvlist_t *props)
3888fa9e4066Sahrens {
3889fa9e4066Sahrens 	int ret;
38904445fffbSMatthew Ahrens 	snapdata_t sd = { 0 };
38919adfa60dSMatthew Ahrens 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
38924445fffbSMatthew Ahrens 	char *cp;
38934445fffbSMatthew Ahrens 	zfs_handle_t *zhp;
389499653d4eSeschrock 	char errbuf[1024];
3895fa9e4066Sahrens 
389699653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38974445fffbSMatthew Ahrens 	    "cannot snapshot %s"), path);
389899653d4eSeschrock 
3899f18faf3fSek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
390099653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3901fa9e4066Sahrens 
39024445fffbSMatthew Ahrens 	(void) strlcpy(fsname, path, sizeof (fsname));
39034445fffbSMatthew Ahrens 	cp = strchr(fsname, '@');
39044445fffbSMatthew Ahrens 	*cp = '\0';
39054445fffbSMatthew Ahrens 	sd.sd_snapname = cp + 1;
3906bb0ade09Sahrens 
39074445fffbSMatthew Ahrens 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3908fa9e4066Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3909fa9e4066Sahrens 		return (-1);
3910fa9e4066Sahrens 	}
3911fa9e4066Sahrens 
39124445fffbSMatthew Ahrens 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
39134445fffbSMatthew Ahrens 	if (recursive) {
39144445fffbSMatthew Ahrens 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
39154445fffbSMatthew Ahrens 	} else {
39164445fffbSMatthew Ahrens 		fnvlist_add_boolean(sd.sd_nvl, path);
3917681d9761SEric Taylor 	}
3918fa9e4066Sahrens 
39194445fffbSMatthew Ahrens 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
39204445fffbSMatthew Ahrens 	nvlist_free(sd.sd_nvl);
3921fa9e4066Sahrens 	zfs_close(zhp);
3922fa9e4066Sahrens 	return (ret);
3923fa9e4066Sahrens }
3924fa9e4066Sahrens 
3925fa9e4066Sahrens /*
3926b12a1c38Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3927b12a1c38Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3928b12a1c38Slling  * is a dependent and we should just destroy it without checking the transaction
3929b12a1c38Slling  * group.
3930fa9e4066Sahrens  */
3931b12a1c38Slling typedef struct rollback_data {
3932b12a1c38Slling 	const char	*cb_target;		/* the snapshot */
3933b12a1c38Slling 	uint64_t	cb_create;		/* creation time reference */
3934c391e322Sahrens 	boolean_t	cb_error;
3935c391e322Sahrens 	boolean_t	cb_force;
3936b12a1c38Slling } rollback_data_t;
3937b12a1c38Slling 
3938b12a1c38Slling static int
393978f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3940b12a1c38Slling {
3941b12a1c38Slling 	rollback_data_t *cbp = data;
3942c391e322Sahrens 	prop_changelist_t *clp;
3943c391e322Sahrens 
394478f17100SMatthew Ahrens 	/* We must destroy this clone; first unmount it */
39450069fd67STim Haley 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3946c391e322Sahrens 	    cbp->cb_force ? MS_FORCE: 0);
3947c391e322Sahrens 	if (clp == NULL || changelist_prefix(clp) != 0) {
3948c391e322Sahrens 		cbp->cb_error = B_TRUE;
3949c391e322Sahrens 		zfs_close(zhp);
3950c391e322Sahrens 		return (0);
3951c391e322Sahrens 	}
3952842727c2SChris Kirby 	if (zfs_destroy(zhp, B_FALSE) != 0)
3953c391e322Sahrens 		cbp->cb_error = B_TRUE;
3954c391e322Sahrens 	else
3955c391e322Sahrens 		changelist_remove(clp, zhp->zfs_name);
3956ba7b046eSahrens 	(void) changelist_postfix(clp);
3957c391e322Sahrens 	changelist_free(clp);
395878f17100SMatthew Ahrens 
395978f17100SMatthew Ahrens 	zfs_close(zhp);
396078f17100SMatthew Ahrens 	return (0);
396178f17100SMatthew Ahrens }
396278f17100SMatthew Ahrens 
396378f17100SMatthew Ahrens static int
396478f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
396578f17100SMatthew Ahrens {
396678f17100SMatthew Ahrens 	rollback_data_t *cbp = data;
396778f17100SMatthew Ahrens 
396878f17100SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
396978f17100SMatthew Ahrens 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
397078f17100SMatthew Ahrens 		    rollback_destroy_dependent, cbp);
397178f17100SMatthew Ahrens 
397278f17100SMatthew Ahrens 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3973b12a1c38Slling 	}
3974b12a1c38Slling 
3975b12a1c38Slling 	zfs_close(zhp);
3976b12a1c38Slling 	return (0);
3977b12a1c38Slling }
3978b12a1c38Slling 
3979b12a1c38Slling /*
39804ccbb6e7Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
39814ccbb6e7Sahrens  * data changes since then and making it the active dataset.
39824ccbb6e7Sahrens  *
398378f17100SMatthew Ahrens  * Any snapshots and bookmarks more recent than the target are
398478f17100SMatthew Ahrens  * destroyed, along with their dependents (i.e. clones).
3985b12a1c38Slling  */
39864ccbb6e7Sahrens int
3987c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3988fa9e4066Sahrens {
39894ccbb6e7Sahrens 	rollback_data_t cb = { 0 };
39904ccbb6e7Sahrens 	int err;
39917b97dc1aSrm160521 	boolean_t restore_resv = 0;
3992f83b46baSPaul Dagnelie 	uint64_t old_volsize = 0, new_volsize;
39937b97dc1aSrm160521 	zfs_prop_t resv_prop;
3994fa9e4066Sahrens 
3995fa9e4066Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3996fa9e4066Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3997fa9e4066Sahrens 
39984ccbb6e7Sahrens 	/*
39992e2c1355SMatthew Ahrens 	 * Destroy all recent snapshots and their dependents.
40004ccbb6e7Sahrens 	 */
4001c391e322Sahrens 	cb.cb_force = force;
40024ccbb6e7Sahrens 	cb.cb_target = snap->zfs_name;
40034ccbb6e7Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
40040d8fa8f8SMartin Matuska 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
400578f17100SMatthew Ahrens 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
40064ccbb6e7Sahrens 
4007c391e322Sahrens 	if (cb.cb_error)
4008c391e322Sahrens 		return (-1);
40094ccbb6e7Sahrens 
40104ccbb6e7Sahrens 	/*
40114ccbb6e7Sahrens 	 * Now that we have verified that the snapshot is the latest,
40124ccbb6e7Sahrens 	 * rollback to the given snapshot.
40134ccbb6e7Sahrens 	 */
40144ccbb6e7Sahrens 
40157b97dc1aSrm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
40167b97dc1aSrm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
40177b97dc1aSrm160521 			return (-1);
40187b97dc1aSrm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
40197b97dc1aSrm160521 		restore_resv =
40207b97dc1aSrm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
40217b97dc1aSrm160521 	}
4022fa9e4066Sahrens 
4023fa9e4066Sahrens 	/*
402477b17137SAndriy Gapon 	 * Pass both the filesystem and the wanted snapshot names,
402577b17137SAndriy Gapon 	 * we would get an error back if the snapshot is destroyed or
402677b17137SAndriy Gapon 	 * a new snapshot is created before this request is processed.
4027fa9e4066Sahrens 	 */
402877b17137SAndriy Gapon 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
402995643f75SAndriy Gapon 	if (err != 0) {
403095643f75SAndriy Gapon 		char errbuf[1024];
403195643f75SAndriy Gapon 
403295643f75SAndriy Gapon 		(void) snprintf(errbuf, sizeof (errbuf),
403395643f75SAndriy Gapon 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
403495643f75SAndriy Gapon 		    zhp->zfs_name);
403595643f75SAndriy Gapon 		switch (err) {
403695643f75SAndriy Gapon 		case EEXIST:
403777b17137SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
403895643f75SAndriy Gapon 			    "there is a snapshot or bookmark more recent "
403995643f75SAndriy Gapon 			    "than '%s'"), snap->zfs_name);
404095643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
404195643f75SAndriy Gapon 			break;
404295643f75SAndriy Gapon 		case ESRCH:
404395643f75SAndriy Gapon 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
404495643f75SAndriy Gapon 			    "'%s' is not found among snapshots of '%s'"),
404595643f75SAndriy Gapon 			    snap->zfs_name, zhp->zfs_name);
404695643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
404795643f75SAndriy Gapon 			break;
404895643f75SAndriy Gapon 		case EINVAL:
404995643f75SAndriy Gapon 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
405095643f75SAndriy Gapon 			break;
405195643f75SAndriy Gapon 		default:
405295643f75SAndriy Gapon 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
405395643f75SAndriy Gapon 		}
4054b9415e83Srm160521 		return (err);
4055b9415e83Srm160521 	}
4056fa9e4066Sahrens 
40577b97dc1aSrm160521 	/*
40587b97dc1aSrm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
40597b97dc1aSrm160521 	 * rollback reservation and the volsize has changed then set
40607b97dc1aSrm160521 	 * the reservation property to the post-rollback volsize.
40617b97dc1aSrm160521 	 * Make a new handle since the rollback closed the dataset.
40627b97dc1aSrm160521 	 */
4063b9415e83Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4064b9415e83Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
40657b97dc1aSrm160521 		if (restore_resv) {
40667b97dc1aSrm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
40677b97dc1aSrm160521 			if (old_volsize != new_volsize)
4068b9415e83Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
4069b9415e83Srm160521 				    new_volsize);
40707b97dc1aSrm160521 		}
40717b97dc1aSrm160521 		zfs_close(zhp);
40727b97dc1aSrm160521 	}
40734ccbb6e7Sahrens 	return (err);
4074b12a1c38Slling }
4075b12a1c38Slling 
4076b12a1c38Slling /*
4077fa9e4066Sahrens  * Renames the given dataset.
4078fa9e4066Sahrens  */
4079fa9e4066Sahrens int
40806a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
40816a9cb0eaSEric Schrock     boolean_t force_unmount)
4082fa9e4066Sahrens {
408388f61deeSIgor Kozhukhov 	int ret = 0;
4084fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
4085fa9e4066Sahrens 	char *delim;
4086cdf5b4caSmmusante 	prop_changelist_t *cl = NULL;
4087cdf5b4caSmmusante 	zfs_handle_t *zhrp = NULL;
4088cdf5b4caSmmusante 	char *parentname = NULL;
40899adfa60dSMatthew Ahrens 	char parent[ZFS_MAX_DATASET_NAME_LEN];
409099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
409199653d4eSeschrock 	char errbuf[1024];
4092fa9e4066Sahrens 
4093fa9e4066Sahrens 	/* if we have the same exact name, just return success */
4094fa9e4066Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
4095fa9e4066Sahrens 		return (0);
4096fa9e4066Sahrens 
409799653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
409899653d4eSeschrock 	    "cannot rename to '%s'"), target);
409999653d4eSeschrock 
4100*add927f8Sloli10K 	/* make sure source name is valid */
4101*add927f8Sloli10K 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4102*add927f8Sloli10K 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4103*add927f8Sloli10K 
4104fa9e4066Sahrens 	/*
4105fa9e4066Sahrens 	 * Make sure the target name is valid
4106fa9e4066Sahrens 	 */
4107fa9e4066Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
410898579b20Snd150628 		if ((strchr(target, '@') == NULL) ||
410998579b20Snd150628 		    *target == '@') {
411098579b20Snd150628 			/*
411198579b20Snd150628 			 * Snapshot target name is abbreviated,
411298579b20Snd150628 			 * reconstruct full dataset name
411398579b20Snd150628 			 */
411498579b20Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
411598579b20Snd150628 			    sizeof (parent));
411698579b20Snd150628 			delim = strchr(parent, '@');
411798579b20Snd150628 			if (strchr(target, '@') == NULL)
411898579b20Snd150628 				*(++delim) = '\0';
411998579b20Snd150628 			else
412098579b20Snd150628 				*delim = '\0';
412198579b20Snd150628 			(void) strlcat(parent, target, sizeof (parent));
412298579b20Snd150628 			target = parent;
412398579b20Snd150628 		} else {
4124fa9e4066Sahrens 			/*
4125fa9e4066Sahrens 			 * Make sure we're renaming within the same dataset.
4126fa9e4066Sahrens 			 */
412798579b20Snd150628 			delim = strchr(target, '@');
412898579b20Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
412998579b20Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
413099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
413198579b20Snd150628 				    "snapshots must be part of same "
413298579b20Snd150628 				    "dataset"));
413398579b20Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
413498579b20Snd150628 				    errbuf));
4135fa9e4066Sahrens 			}
413698579b20Snd150628 		}
4137f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
413898579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4139fa9e4066Sahrens 	} else {
4140cdf5b4caSmmusante 		if (recursive) {
4141cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4142cdf5b4caSmmusante 			    "recursive rename must be a snapshot"));
4143cdf5b4caSmmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4144cdf5b4caSmmusante 		}
4145cdf5b4caSmmusante 
4146f18faf3fSek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
414798579b20Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4148e9dbad6fSeschrock 
4149fa9e4066Sahrens 		/* validate parents */
4150d41c4376SMark J Musante 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4151fa9e4066Sahrens 			return (-1);
4152fa9e4066Sahrens 
4153fa9e4066Sahrens 		/* make sure we're in the same pool */
4154fa9e4066Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4155fa9e4066Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4156fa9e4066Sahrens 		    zhp->zfs_name[delim - target] != '/') {
415799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
415899653d4eSeschrock 			    "datasets must be within same pool"));
415999653d4eSeschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4160fa9e4066Sahrens 		}
4161f2fdf992Snd150628 
4162f2fdf992Snd150628 		/* new name cannot be a child of the current dataset name */
4163d41c4376SMark J Musante 		if (is_descendant(zhp->zfs_name, target)) {
4164f2fdf992Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4165d41c4376SMark J Musante 			    "New dataset name cannot be a descendant of "
4166f2fdf992Snd150628 			    "current dataset name"));
4167f2fdf992Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4168f2fdf992Snd150628 		}
4169fa9e4066Sahrens 	}
4170fa9e4066Sahrens 
417199653d4eSeschrock 	(void) snprintf(errbuf, sizeof (errbuf),
417299653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
417399653d4eSeschrock 
4174fa9e4066Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4175fa9e4066Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
417699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
417799653d4eSeschrock 		    "dataset is used in a non-global zone"));
417899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4179fa9e4066Sahrens 	}
4180fa9e4066Sahrens 
4181cdf5b4caSmmusante 	if (recursive) {
4182f0c5ee21Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4183f0c5ee21Smmusante 		if (parentname == NULL) {
4184f0c5ee21Smmusante 			ret = -1;
4185f0c5ee21Smmusante 			goto error;
4186f0c5ee21Smmusante 		}
4187cdf5b4caSmmusante 		delim = strchr(parentname, '@');
4188cdf5b4caSmmusante 		*delim = '\0';
4189990b4856Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4190cdf5b4caSmmusante 		if (zhrp == NULL) {
4191f0c5ee21Smmusante 			ret = -1;
4192f0c5ee21Smmusante 			goto error;
4193cdf5b4caSmmusante 		}
419433cde0d0SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
41956a9cb0eaSEric Schrock 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
41966a9cb0eaSEric Schrock 		    force_unmount ? MS_FORCE : 0)) == NULL)
419799653d4eSeschrock 			return (-1);
4198fa9e4066Sahrens 
4199fa9e4066Sahrens 		if (changelist_haszonedchild(cl)) {
420099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
420199653d4eSeschrock 			    "child dataset with inherited mountpoint is used "
420299653d4eSeschrock 			    "in a non-global zone"));
4203e9dbad6fSeschrock 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4204f83b46baSPaul Dagnelie 			ret = -1;
4205fa9e4066Sahrens 			goto error;
4206fa9e4066Sahrens 		}
4207fa9e4066Sahrens 
4208fa9e4066Sahrens 		if ((ret = changelist_prefix(cl)) != 0)
4209fa9e4066Sahrens 			goto error;
4210cdf5b4caSmmusante 	}
4211fa9e4066Sahrens 
4212e9dbad6fSeschrock 	if (ZFS_IS_VOLUME(zhp))
4213fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4214fa9e4066Sahrens 	else
4215fa9e4066Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4216fa9e4066Sahrens 
421798579b20Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4218e9dbad6fSeschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
421998579b20Snd150628 
4220cdf5b4caSmmusante 	zc.zc_cookie = recursive;
4221cdf5b4caSmmusante 
4222ecd6cf80Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4223cdf5b4caSmmusante 		/*
4224cdf5b4caSmmusante 		 * if it was recursive, the one that actually failed will
4225cdf5b4caSmmusante 		 * be in zc.zc_name
4226cdf5b4caSmmusante 		 */
4227cdf5b4caSmmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
42283cb34c60Sahrens 		    "cannot rename '%s'"), zc.zc_name);
4229cdf5b4caSmmusante 
4230cdf5b4caSmmusante 		if (recursive && errno == EEXIST) {
4231cdf5b4caSmmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4232cdf5b4caSmmusante 			    "a child dataset already has a snapshot "
4233cdf5b4caSmmusante 			    "with the new name"));
4234a10acbd6Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4235cdf5b4caSmmusante 		} else {
423699653d4eSeschrock 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4237cdf5b4caSmmusante 		}
4238fa9e4066Sahrens 
4239fa9e4066Sahrens 		/*
4240fa9e4066Sahrens 		 * On failure, we still want to remount any filesystems that
4241fa9e4066Sahrens 		 * were previously mounted, so we don't alter the system state.
4242fa9e4066Sahrens 		 */
424333cde0d0SMatthew Ahrens 		if (cl != NULL)
4244fa9e4066Sahrens 			(void) changelist_postfix(cl);
4245cdf5b4caSmmusante 	} else {
424633cde0d0SMatthew Ahrens 		if (cl != NULL) {
4247fa9e4066Sahrens 			changelist_rename(cl, zfs_get_name(zhp), target);
4248fa9e4066Sahrens 			ret = changelist_postfix(cl);
4249fa9e4066Sahrens 		}
4250cdf5b4caSmmusante 	}
4251fa9e4066Sahrens 
4252fa9e4066Sahrens error:
425333cde0d0SMatthew Ahrens 	if (parentname != NULL) {
4254cdf5b4caSmmusante 		free(parentname);
4255cdf5b4caSmmusante 	}
425633cde0d0SMatthew Ahrens 	if (zhrp != NULL) {
4257cdf5b4caSmmusante 		zfs_close(zhrp);
4258cdf5b4caSmmusante 	}
425933cde0d0SMatthew Ahrens 	if (cl != NULL) {
4260fa9e4066Sahrens 		changelist_free(cl);
4261cdf5b4caSmmusante 	}
4262fa9e4066Sahrens 	return (ret);
4263fa9e4066Sahrens }
4264fa9e4066Sahrens 
4265e9dbad6fSeschrock nvlist_t *
4266e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4267e9dbad6fSeschrock {
4268e9dbad6fSeschrock 	return (zhp->zfs_user_props);
4269e9dbad6fSeschrock }
4270e9dbad6fSeschrock 
427192241e0bSTom Erickson nvlist_t *
427292241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
427392241e0bSTom Erickson {
427492241e0bSTom Erickson 	if (zhp->zfs_recvd_props == NULL)
427592241e0bSTom Erickson 		if (get_recvd_props_ioctl(zhp) != 0)
427692241e0bSTom Erickson 			return (NULL);
427792241e0bSTom Erickson 	return (zhp->zfs_recvd_props);
427892241e0bSTom Erickson }
427992241e0bSTom Erickson 
4280e9dbad6fSeschrock /*
4281e9dbad6fSeschrock  * This function is used by 'zfs list' to determine the exact set of columns to
4282e9dbad6fSeschrock  * display, and their maximum widths.  This does two main things:
4283e9dbad6fSeschrock  *
4284e9dbad6fSeschrock  *      - If this is a list of all properties, then expand the list to include
4285e9dbad6fSeschrock  *        all native properties, and set a flag so that for each dataset we look
4286e9dbad6fSeschrock  *        for new unique user properties and add them to the list.
4287e9dbad6fSeschrock  *
4288e9dbad6fSeschrock  *      - For non fixed-width properties, keep track of the maximum width seen
428992241e0bSTom Erickson  *        so that we can size the column appropriately. If the user has
429092241e0bSTom Erickson  *        requested received property values, we also need to compute the width
429192241e0bSTom Erickson  *        of the RECEIVED column.
4292e9dbad6fSeschrock  */
4293e9dbad6fSeschrock int
429443d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
429543d68d68SYuri Pankov     boolean_t literal)
4296e9dbad6fSeschrock {
4297e9dbad6fSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4298990b4856Slling 	zprop_list_t *entry;
4299990b4856Slling 	zprop_list_t **last, **start;
4300e9dbad6fSeschrock 	nvlist_t *userprops, *propval;
4301e9dbad6fSeschrock 	nvpair_t *elem;
4302e9dbad6fSeschrock 	char *strval;
4303e9dbad6fSeschrock 	char buf[ZFS_MAXPROPLEN];
4304e9dbad6fSeschrock 
4305990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4306e9dbad6fSeschrock 		return (-1);
4307e9dbad6fSeschrock 
4308e9dbad6fSeschrock 	userprops = zfs_get_user_props(zhp);
4309e9dbad6fSeschrock 
4310e9dbad6fSeschrock 	entry = *plp;
4311e9dbad6fSeschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4312e9dbad6fSeschrock 		/*
4313e9dbad6fSeschrock 		 * Go through and add any user properties as necessary.  We
4314e9dbad6fSeschrock 		 * start by incrementing our list pointer to the first
4315e9dbad6fSeschrock 		 * non-native property.
4316e9dbad6fSeschrock 		 */
4317e9dbad6fSeschrock 		start = plp;
4318e9dbad6fSeschrock 		while (*start != NULL) {
4319990b4856Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
4320e9dbad6fSeschrock 				break;
4321e9dbad6fSeschrock 			start = &(*start)->pl_next;
4322e9dbad6fSeschrock 		}
4323e9dbad6fSeschrock 
4324e9dbad6fSeschrock 		elem = NULL;
4325e9dbad6fSeschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4326e9dbad6fSeschrock 			/*
4327e9dbad6fSeschrock 			 * See if we've already found this property in our list.
4328e9dbad6fSeschrock 			 */
4329e9dbad6fSeschrock 			for (last = start; *last != NULL;
4330e9dbad6fSeschrock 			    last = &(*last)->pl_next) {
4331e9dbad6fSeschrock 				if (strcmp((*last)->pl_user_prop,
4332e9dbad6fSeschrock 				    nvpair_name(elem)) == 0)
4333e9dbad6fSeschrock 					break;
4334e9dbad6fSeschrock 			}
4335e9dbad6fSeschrock 
4336e9dbad6fSeschrock 			if (*last == NULL) {
4337e9dbad6fSeschrock 				if ((entry = zfs_alloc(hdl,
4338990b4856Slling 				    sizeof (zprop_list_t))) == NULL ||
4339e9dbad6fSeschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4340e9dbad6fSeschrock 				    nvpair_name(elem)))) == NULL) {
4341e9dbad6fSeschrock 					free(entry);
4342e9dbad6fSeschrock 					return (-1);
4343e9dbad6fSeschrock 				}
4344e9dbad6fSeschrock 
4345990b4856Slling 				entry->pl_prop = ZPROP_INVAL;
4346e9dbad6fSeschrock 				entry->pl_width = strlen(nvpair_name(elem));
4347e9dbad6fSeschrock 				entry->pl_all = B_TRUE;
4348e9dbad6fSeschrock 				*last = entry;
4349e9dbad6fSeschrock 			}
4350e9dbad6fSeschrock 		}
4351e9dbad6fSeschrock 	}
4352e9dbad6fSeschrock 
4353e9dbad6fSeschrock 	/*
4354e9dbad6fSeschrock 	 * Now go through and check the width of any non-fixed columns
4355e9dbad6fSeschrock 	 */
4356e9dbad6fSeschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
435743d68d68SYuri Pankov 		if (entry->pl_fixed && !literal)
4358e9dbad6fSeschrock 			continue;
4359e9dbad6fSeschrock 
4360990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL) {
4361e9dbad6fSeschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
436243d68d68SYuri Pankov 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4363e9dbad6fSeschrock 				if (strlen(buf) > entry->pl_width)
4364e9dbad6fSeschrock 					entry->pl_width = strlen(buf);
4365e9dbad6fSeschrock 			}
436692241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
436792241e0bSTom Erickson 			    zfs_prop_to_name(entry->pl_prop),
436843d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
436992241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
437092241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
437192241e0bSTom Erickson 		} else {
437292241e0bSTom Erickson 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
437392241e0bSTom Erickson 			    &propval) == 0) {
4374e9dbad6fSeschrock 				verify(nvlist_lookup_string(propval,
4375990b4856Slling 				    ZPROP_VALUE, &strval) == 0);
4376e9dbad6fSeschrock 				if (strlen(strval) > entry->pl_width)
4377e9dbad6fSeschrock 					entry->pl_width = strlen(strval);
4378e9dbad6fSeschrock 			}
437992241e0bSTom Erickson 			if (received && zfs_prop_get_recvd(zhp,
438092241e0bSTom Erickson 			    entry->pl_user_prop,
438143d68d68SYuri Pankov 			    buf, sizeof (buf), literal) == 0)
438292241e0bSTom Erickson 				if (strlen(buf) > entry->pl_recvd_width)
438392241e0bSTom Erickson 					entry->pl_recvd_width = strlen(buf);
438492241e0bSTom Erickson 		}
4385e9dbad6fSeschrock 	}
4386e9dbad6fSeschrock 
4387e9dbad6fSeschrock 	return (0);
4388e9dbad6fSeschrock }
4389ecd6cf80Smarks 
4390ecd6cf80Smarks int
4391ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4392743a77edSAlan Wright     char *resource, void *export, void *sharetab,
4393743a77edSAlan Wright     int sharemax, zfs_share_op_t operation)
4394ecd6cf80Smarks {
4395ecd6cf80Smarks 	zfs_cmd_t zc = { 0 };
4396ecd6cf80Smarks 	int error;
4397ecd6cf80Smarks 
4398ecd6cf80Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4399ecd6cf80Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4400743a77edSAlan Wright 	if (resource)
4401743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4402ecd6cf80Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4403ecd6cf80Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4404da6c28aaSamw 	zc.zc_share.z_sharetype = operation;
4405ecd6cf80Smarks 	zc.zc_share.z_sharemax = sharemax;
4406ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4407ecd6cf80Smarks 	return (error);
4408ecd6cf80Smarks }
44092e5e9e19SSanjeev Bagewadi 
44102e5e9e19SSanjeev Bagewadi void
44112e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
44122e5e9e19SSanjeev Bagewadi {
44132e5e9e19SSanjeev Bagewadi 	nvpair_t *curr;
44142e5e9e19SSanjeev Bagewadi 
44152e5e9e19SSanjeev Bagewadi 	/*
44162e5e9e19SSanjeev Bagewadi 	 * Keep a reference to the props-table against which we prune the
44172e5e9e19SSanjeev Bagewadi 	 * properties.
44182e5e9e19SSanjeev Bagewadi 	 */
44192e5e9e19SSanjeev Bagewadi 	zhp->zfs_props_table = props;
44202e5e9e19SSanjeev Bagewadi 
44212e5e9e19SSanjeev Bagewadi 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
44222e5e9e19SSanjeev Bagewadi 
44232e5e9e19SSanjeev Bagewadi 	while (curr) {
44242e5e9e19SSanjeev Bagewadi 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
44252e5e9e19SSanjeev Bagewadi 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
44262e5e9e19SSanjeev Bagewadi 
442714843421SMatthew Ahrens 		/*
4428faaa6415SEric Schrock 		 * User properties will result in ZPROP_INVAL, and since we
4429faaa6415SEric Schrock 		 * only know how to prune standard ZFS properties, we always
4430faaa6415SEric Schrock 		 * leave these in the list.  This can also happen if we
4431faaa6415SEric Schrock 		 * encounter an unknown DSL property (when running older
4432faaa6415SEric Schrock 		 * software, for example).
443314843421SMatthew Ahrens 		 */
443414843421SMatthew Ahrens 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
44352e5e9e19SSanjeev Bagewadi 			(void) nvlist_remove(zhp->zfs_props,
44362e5e9e19SSanjeev Bagewadi 			    nvpair_name(curr), nvpair_type(curr));
44372e5e9e19SSanjeev Bagewadi 		curr = next;
44382e5e9e19SSanjeev Bagewadi 	}
44392e5e9e19SSanjeev Bagewadi }
4440743a77edSAlan Wright 
4441743a77edSAlan Wright static int
4442743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4443743a77edSAlan Wright     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4444743a77edSAlan Wright {
4445743a77edSAlan Wright 	zfs_cmd_t zc = { 0 };
4446743a77edSAlan Wright 	nvlist_t *nvlist = NULL;
4447743a77edSAlan Wright 	int error;
4448743a77edSAlan Wright 
4449743a77edSAlan Wright 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4450743a77edSAlan Wright 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4451743a77edSAlan Wright 	zc.zc_cookie = (uint64_t)cmd;
4452743a77edSAlan Wright 
4453743a77edSAlan Wright 	if (cmd == ZFS_SMB_ACL_RENAME) {
4454743a77edSAlan Wright 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4455743a77edSAlan Wright 			(void) no_memory(hdl);
445630925561SChris Williamson 			return (0);
4457743a77edSAlan Wright 		}
4458743a77edSAlan Wright 	}
4459743a77edSAlan Wright 
4460743a77edSAlan Wright 	switch (cmd) {
4461743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
4462743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
4463743a77edSAlan Wright 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4464743a77edSAlan Wright 		break;
4465743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
4466743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4467743a77edSAlan Wright 		    resource1) != 0) {
4468743a77edSAlan Wright 				(void) no_memory(hdl);
4469743a77edSAlan Wright 				return (-1);
4470743a77edSAlan Wright 		}
4471743a77edSAlan Wright 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4472743a77edSAlan Wright 		    resource2) != 0) {
4473743a77edSAlan Wright 				(void) no_memory(hdl);
4474743a77edSAlan Wright 				return (-1);
4475743a77edSAlan Wright 		}
4476743a77edSAlan Wright 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4477743a77edSAlan Wright 			nvlist_free(nvlist);
4478743a77edSAlan Wright 			return (-1);
4479743a77edSAlan Wright 		}
4480743a77edSAlan Wright 		break;
4481743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
4482743a77edSAlan Wright 		break;
4483743a77edSAlan Wright 	default:
4484743a77edSAlan Wright 		return (-1);
4485743a77edSAlan Wright 	}
4486743a77edSAlan Wright 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4487743a77edSAlan Wright 	nvlist_free(nvlist);
4488743a77edSAlan Wright 	return (error);
4489743a77edSAlan Wright }
4490743a77edSAlan Wright 
4491743a77edSAlan Wright int
4492743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4493743a77edSAlan Wright     char *path, char *resource)
4494743a77edSAlan Wright {
4495743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4496743a77edSAlan Wright 	    resource, NULL));
4497743a77edSAlan Wright }
4498743a77edSAlan Wright 
4499743a77edSAlan Wright int
4500743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4501743a77edSAlan Wright     char *path, char *resource)
4502743a77edSAlan Wright {
4503743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4504743a77edSAlan Wright 	    resource, NULL));
4505743a77edSAlan Wright }
4506743a77edSAlan Wright 
4507743a77edSAlan Wright int
4508743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4509743a77edSAlan Wright {
4510743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4511743a77edSAlan Wright 	    NULL, NULL));
4512743a77edSAlan Wright }
4513743a77edSAlan Wright 
4514743a77edSAlan Wright int
4515743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4516743a77edSAlan Wright     char *oldname, char *newname)
4517743a77edSAlan Wright {
4518743a77edSAlan Wright 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4519743a77edSAlan Wright 	    oldname, newname));
4520743a77edSAlan Wright }
452114843421SMatthew Ahrens 
452214843421SMatthew Ahrens int
452314843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
452414843421SMatthew Ahrens     zfs_userspace_cb_t func, void *arg)
452514843421SMatthew Ahrens {
452614843421SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
452714843421SMatthew Ahrens 	zfs_useracct_t buf[100];
452870f56fa6SYuri Pankov 	libzfs_handle_t *hdl = zhp->zfs_hdl;
452970f56fa6SYuri Pankov 	int ret;
453014843421SMatthew Ahrens 
453119b94df9SMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
453214843421SMatthew Ahrens 
453314843421SMatthew Ahrens 	zc.zc_objset_type = type;
453414843421SMatthew Ahrens 	zc.zc_nvlist_dst = (uintptr_t)buf;
453514843421SMatthew Ahrens 
453670f56fa6SYuri Pankov 	for (;;) {
453714843421SMatthew Ahrens 		zfs_useracct_t *zua = buf;
453814843421SMatthew Ahrens 
453914843421SMatthew Ahrens 		zc.zc_nvlist_dst_size = sizeof (buf);
454070f56fa6SYuri Pankov 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
45413b2aab18SMatthew Ahrens 			char errbuf[1024];
454270f56fa6SYuri Pankov 
454370f56fa6SYuri Pankov 			(void) snprintf(errbuf, sizeof (errbuf),
454470f56fa6SYuri Pankov 			    dgettext(TEXT_DOMAIN,
454570f56fa6SYuri Pankov 			    "cannot get used/quota for %s"), zc.zc_name);
454670f56fa6SYuri Pankov 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
454770f56fa6SYuri Pankov 		}
454870f56fa6SYuri Pankov 		if (zc.zc_nvlist_dst_size == 0)
454914843421SMatthew Ahrens 			break;
455014843421SMatthew Ahrens 
455114843421SMatthew Ahrens 		while (zc.zc_nvlist_dst_size > 0) {
455270f56fa6SYuri Pankov 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
455370f56fa6SYuri Pankov 			    zua->zu_space)) != 0)
455470f56fa6SYuri Pankov 				return (ret);
455514843421SMatthew Ahrens 			zua++;
455614843421SMatthew Ahrens 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
455714843421SMatthew Ahrens 		}
455814843421SMatthew Ahrens 	}
455914843421SMatthew Ahrens 
456070f56fa6SYuri Pankov 	return (0);
456114843421SMatthew Ahrens }
4562842727c2SChris Kirby 
45633b2aab18SMatthew Ahrens struct holdarg {
45643b2aab18SMatthew Ahrens 	nvlist_t *nvl;
45653b2aab18SMatthew Ahrens 	const char *snapname;
45663b2aab18SMatthew Ahrens 	const char *tag;
45673b2aab18SMatthew Ahrens 	boolean_t recursive;
4568bb6e7075SMatthew Ahrens 	int error;
45693b2aab18SMatthew Ahrens };
45703b2aab18SMatthew Ahrens 
45713b2aab18SMatthew Ahrens static int
45723b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
45733b2aab18SMatthew Ahrens {
45743b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
45759adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
45763b2aab18SMatthew Ahrens 	int rv = 0;
45773b2aab18SMatthew Ahrens 
45783b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
45793b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
45803b2aab18SMatthew Ahrens 
4581a7a845e4SSteven Hartland 	if (lzc_exists(name))
45823b2aab18SMatthew Ahrens 		fnvlist_add_string(ha->nvl, name, ha->tag);
45833b2aab18SMatthew Ahrens 
45843b2aab18SMatthew Ahrens 	if (ha->recursive)
45853b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
45863b2aab18SMatthew Ahrens 	zfs_close(zhp);
45873b2aab18SMatthew Ahrens 	return (rv);
45883b2aab18SMatthew Ahrens }
45893b2aab18SMatthew Ahrens 
4590842727c2SChris Kirby int
4591842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4592a7a845e4SSteven Hartland     boolean_t recursive, int cleanup_fd)
4593842727c2SChris Kirby {
45943b2aab18SMatthew Ahrens 	int ret;
45953b2aab18SMatthew Ahrens 	struct holdarg ha;
4596842727c2SChris Kirby 
45973b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
45983b2aab18SMatthew Ahrens 	ha.snapname = snapname;
45993b2aab18SMatthew Ahrens 	ha.tag = tag;
46003b2aab18SMatthew Ahrens 	ha.recursive = recursive;
46013b2aab18SMatthew Ahrens 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4602013023d4SMartin Matuska 
4603a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4604a7a845e4SSteven Hartland 		char errbuf[1024];
4605a7a845e4SSteven Hartland 
4606013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4607013023d4SMartin Matuska 		ret = ENOENT;
4608013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4609013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4610013023d4SMartin Matuska 		    "cannot hold snapshot '%s@%s'"),
4611013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4612a7a845e4SSteven Hartland 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4613013023d4SMartin Matuska 		return (ret);
4614013023d4SMartin Matuska 	}
4615013023d4SMartin Matuska 
4616a7a845e4SSteven Hartland 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
46173b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4618a7f53a56SChris Kirby 
4619a7a845e4SSteven Hartland 	return (ret);
4620a7a845e4SSteven Hartland }
4621842727c2SChris Kirby 
4622a7a845e4SSteven Hartland int
4623a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4624a7a845e4SSteven Hartland {
4625a7a845e4SSteven Hartland 	int ret;
4626a7a845e4SSteven Hartland 	nvlist_t *errors;
4627a7a845e4SSteven Hartland 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4628a7a845e4SSteven Hartland 	char errbuf[1024];
4629a7a845e4SSteven Hartland 	nvpair_t *elem;
4630a7a845e4SSteven Hartland 
4631a7a845e4SSteven Hartland 	errors = NULL;
4632a7a845e4SSteven Hartland 	ret = lzc_hold(holds, cleanup_fd, &errors);
4633a7a845e4SSteven Hartland 
4634a7a845e4SSteven Hartland 	if (ret == 0) {
4635a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4636a7a845e4SSteven Hartland 		fnvlist_free(errors);
4637a7a845e4SSteven Hartland 		return (0);
4638a7a845e4SSteven Hartland 	}
4639a7a845e4SSteven Hartland 
4640a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
46413b2aab18SMatthew Ahrens 		/* no hold-specific errors */
46423b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
46433b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "cannot hold"));
46443b2aab18SMatthew Ahrens 		switch (ret) {
46453b2aab18SMatthew Ahrens 		case ENOTSUP:
46463b2aab18SMatthew Ahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46473b2aab18SMatthew Ahrens 			    "pool must be upgraded"));
46483b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
46493b2aab18SMatthew Ahrens 			break;
46503b2aab18SMatthew Ahrens 		case EINVAL:
46513b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
46523b2aab18SMatthew Ahrens 			break;
46533b2aab18SMatthew Ahrens 		default:
46543b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl, ret, errbuf);
46553b2aab18SMatthew Ahrens 		}
46563b2aab18SMatthew Ahrens 	}
4657842727c2SChris Kirby 
46583b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
46593b2aab18SMatthew Ahrens 	    elem != NULL;
46603b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
46613b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
46623b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
46633b2aab18SMatthew Ahrens 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
46643b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
466515508ac0SChris Kirby 		case E2BIG:
466615508ac0SChris Kirby 			/*
466715508ac0SChris Kirby 			 * Temporary tags wind up having the ds object id
466815508ac0SChris Kirby 			 * prepended. So even if we passed the length check
466915508ac0SChris Kirby 			 * above, it's still possible for the tag to wind
467015508ac0SChris Kirby 			 * up being slightly too long.
467115508ac0SChris Kirby 			 */
46723b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
46733b2aab18SMatthew Ahrens 			break;
4674842727c2SChris Kirby 		case EINVAL:
46753b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
46763b2aab18SMatthew Ahrens 			break;
4677842727c2SChris Kirby 		case EEXIST:
46783b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
46793b2aab18SMatthew Ahrens 			break;
4680842727c2SChris Kirby 		default:
46813b2aab18SMatthew Ahrens 			(void) zfs_standard_error(hdl,
46823b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
4683842727c2SChris Kirby 		}
4684842727c2SChris Kirby 	}
4685842727c2SChris Kirby 
46863b2aab18SMatthew Ahrens 	fnvlist_free(errors);
46873b2aab18SMatthew Ahrens 	return (ret);
46883b2aab18SMatthew Ahrens }
46893b2aab18SMatthew Ahrens 
46903b2aab18SMatthew Ahrens static int
46913b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
46923b2aab18SMatthew Ahrens {
46933b2aab18SMatthew Ahrens 	struct holdarg *ha = arg;
46949adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
46953b2aab18SMatthew Ahrens 	int rv = 0;
4696bb6e7075SMatthew Ahrens 	nvlist_t *existing_holds;
46973b2aab18SMatthew Ahrens 
46983b2aab18SMatthew Ahrens 	(void) snprintf(name, sizeof (name),
46993b2aab18SMatthew Ahrens 	    "%s@%s", zhp->zfs_name, ha->snapname);
47003b2aab18SMatthew Ahrens 
4701bb6e7075SMatthew Ahrens 	if (lzc_get_holds(name, &existing_holds) != 0) {
4702bb6e7075SMatthew Ahrens 		ha->error = ENOENT;
4703bb6e7075SMatthew Ahrens 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4704bb6e7075SMatthew Ahrens 		ha->error = ESRCH;
4705bb6e7075SMatthew Ahrens 	} else {
4706bb6e7075SMatthew Ahrens 		nvlist_t *torelease = fnvlist_alloc();
4707bb6e7075SMatthew Ahrens 		fnvlist_add_boolean(torelease, ha->tag);
4708bb6e7075SMatthew Ahrens 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4709bb6e7075SMatthew Ahrens 		fnvlist_free(torelease);
47103b2aab18SMatthew Ahrens 	}
47113b2aab18SMatthew Ahrens 
47123b2aab18SMatthew Ahrens 	if (ha->recursive)
47133b2aab18SMatthew Ahrens 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
47143b2aab18SMatthew Ahrens 	zfs_close(zhp);
47153b2aab18SMatthew Ahrens 	return (rv);
4716842727c2SChris Kirby }
4717842727c2SChris Kirby 
4718842727c2SChris Kirby int
4719842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4720842727c2SChris Kirby     boolean_t recursive)
4721842727c2SChris Kirby {
47223b2aab18SMatthew Ahrens 	int ret;
47233b2aab18SMatthew Ahrens 	struct holdarg ha;
4724a7a845e4SSteven Hartland 	nvlist_t *errors = NULL;
47253b2aab18SMatthew Ahrens 	nvpair_t *elem;
4726842727c2SChris Kirby 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4727013023d4SMartin Matuska 	char errbuf[1024];
4728842727c2SChris Kirby 
47293b2aab18SMatthew Ahrens 	ha.nvl = fnvlist_alloc();
47303b2aab18SMatthew Ahrens 	ha.snapname = snapname;
47313b2aab18SMatthew Ahrens 	ha.tag = tag;
47323b2aab18SMatthew Ahrens 	ha.recursive = recursive;
4733bb6e7075SMatthew Ahrens 	ha.error = 0;
47343b2aab18SMatthew Ahrens 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4735013023d4SMartin Matuska 
4736a7a845e4SSteven Hartland 	if (nvlist_empty(ha.nvl)) {
4737013023d4SMartin Matuska 		fnvlist_free(ha.nvl);
4738bb6e7075SMatthew Ahrens 		ret = ha.error;
4739013023d4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4740013023d4SMartin Matuska 		    dgettext(TEXT_DOMAIN,
4741013023d4SMartin Matuska 		    "cannot release hold from snapshot '%s@%s'"),
4742013023d4SMartin Matuska 		    zhp->zfs_name, snapname);
4743bb6e7075SMatthew Ahrens 		if (ret == ESRCH) {
4744bb6e7075SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4745bb6e7075SMatthew Ahrens 		} else {
4746013023d4SMartin Matuska 			(void) zfs_standard_error(hdl, ret, errbuf);
4747bb6e7075SMatthew Ahrens 		}
4748013023d4SMartin Matuska 		return (ret);
4749013023d4SMartin Matuska 	}
4750013023d4SMartin Matuska 
47513b2aab18SMatthew Ahrens 	ret = lzc_release(ha.nvl, &errors);
47523b2aab18SMatthew Ahrens 	fnvlist_free(ha.nvl);
4753842727c2SChris Kirby 
4754a7a845e4SSteven Hartland 	if (ret == 0) {
4755a7a845e4SSteven Hartland 		/* There may be errors even in the success case. */
4756a7a845e4SSteven Hartland 		fnvlist_free(errors);
47573b2aab18SMatthew Ahrens 		return (0);
4758a7a845e4SSteven Hartland 	}
4759842727c2SChris Kirby 
4760a7a845e4SSteven Hartland 	if (nvlist_empty(errors)) {
47613b2aab18SMatthew Ahrens 		/* no hold-specific errors */
4762842727c2SChris Kirby 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
47633b2aab18SMatthew Ahrens 		    "cannot release"));
4764842727c2SChris Kirby 		switch (errno) {
4765842727c2SChris Kirby 		case ENOTSUP:
4766842727c2SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4767842727c2SChris Kirby 			    "pool must be upgraded"));
47683b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
47693b2aab18SMatthew Ahrens 			break;
4770842727c2SChris Kirby 		default:
47713b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4772842727c2SChris Kirby 		}
4773842727c2SChris Kirby 	}
4774842727c2SChris Kirby 
47753b2aab18SMatthew Ahrens 	for (elem = nvlist_next_nvpair(errors, NULL);
47763b2aab18SMatthew Ahrens 	    elem != NULL;
47773b2aab18SMatthew Ahrens 	    elem = nvlist_next_nvpair(errors, elem)) {
47783b2aab18SMatthew Ahrens 		(void) snprintf(errbuf, sizeof (errbuf),
47793b2aab18SMatthew Ahrens 		    dgettext(TEXT_DOMAIN,
47803b2aab18SMatthew Ahrens 		    "cannot release hold from snapshot '%s'"),
47813b2aab18SMatthew Ahrens 		    nvpair_name(elem));
47823b2aab18SMatthew Ahrens 		switch (fnvpair_value_int32(elem)) {
47833b2aab18SMatthew Ahrens 		case ESRCH:
47843b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
47853b2aab18SMatthew Ahrens 			break;
47863b2aab18SMatthew Ahrens 		case EINVAL:
47873b2aab18SMatthew Ahrens 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
47883b2aab18SMatthew Ahrens 			break;
47893b2aab18SMatthew Ahrens 		default:
47903b2aab18SMatthew Ahrens 			(void) zfs_standard_error_fmt(hdl,
47913b2aab18SMatthew Ahrens 			    fnvpair_value_int32(elem), errbuf);
47923b2aab18SMatthew Ahrens 		}
47933b2aab18SMatthew Ahrens 	}
47943b2aab18SMatthew Ahrens 
47953b2aab18SMatthew Ahrens 	fnvlist_free(errors);
47963b2aab18SMatthew Ahrens 	return (ret);
4797842727c2SChris Kirby }
4798ca45db41SChris Kirby 
47991af68beaSAlexander Stetsenko int
48001af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
48011af68beaSAlexander Stetsenko {
48021af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
48031af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
48041af68beaSAlexander Stetsenko 	int nvsz = 2048;
48051af68beaSAlexander Stetsenko 	void *nvbuf;
48061af68beaSAlexander Stetsenko 	int err = 0;
48073b2aab18SMatthew Ahrens 	char errbuf[1024];
48081af68beaSAlexander Stetsenko 
48091af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
48101af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
48111af68beaSAlexander Stetsenko 
48121af68beaSAlexander Stetsenko tryagain:
48131af68beaSAlexander Stetsenko 
48141af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
48151af68beaSAlexander Stetsenko 	if (nvbuf == NULL) {
48161af68beaSAlexander Stetsenko 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
48171af68beaSAlexander Stetsenko 		goto out;
48181af68beaSAlexander Stetsenko 	}
48191af68beaSAlexander Stetsenko 
48201af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst_size = nvsz;
48211af68beaSAlexander Stetsenko 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
48221af68beaSAlexander Stetsenko 
48239adfa60dSMatthew Ahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
48241af68beaSAlexander Stetsenko 
4825d7f601efSGeorge Wilson 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
48261af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
48271af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
48281af68beaSAlexander Stetsenko 		    zc.zc_name);
48291af68beaSAlexander Stetsenko 		switch (errno) {
48301af68beaSAlexander Stetsenko 		case ENOMEM:
48311af68beaSAlexander Stetsenko 			free(nvbuf);
48321af68beaSAlexander Stetsenko 			nvsz = zc.zc_nvlist_dst_size;
48331af68beaSAlexander Stetsenko 			goto tryagain;
48341af68beaSAlexander Stetsenko 
48351af68beaSAlexander Stetsenko 		case ENOTSUP:
48361af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
48371af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
48381af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
48391af68beaSAlexander Stetsenko 			break;
48401af68beaSAlexander Stetsenko 		case EINVAL:
48411af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
48421af68beaSAlexander Stetsenko 			break;
48431af68beaSAlexander Stetsenko 		case ENOENT:
48441af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
48451af68beaSAlexander Stetsenko 			break;
48461af68beaSAlexander Stetsenko 		default:
48471af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
48481af68beaSAlexander Stetsenko 			break;
48491af68beaSAlexander Stetsenko 		}
48501af68beaSAlexander Stetsenko 	} else {
48511af68beaSAlexander Stetsenko 		/* success */
48521af68beaSAlexander Stetsenko 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
48531af68beaSAlexander Stetsenko 		if (rc) {
48541af68beaSAlexander Stetsenko 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
48551af68beaSAlexander Stetsenko 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
48561af68beaSAlexander Stetsenko 			    zc.zc_name);
48571af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
48581af68beaSAlexander Stetsenko 		}
48591af68beaSAlexander Stetsenko 	}
48601af68beaSAlexander Stetsenko 
48611af68beaSAlexander Stetsenko 	free(nvbuf);
48621af68beaSAlexander Stetsenko out:
48631af68beaSAlexander Stetsenko 	return (err);
48641af68beaSAlexander Stetsenko }
48651af68beaSAlexander Stetsenko 
48661af68beaSAlexander Stetsenko int
48671af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
48681af68beaSAlexander Stetsenko {
48691af68beaSAlexander Stetsenko 	zfs_cmd_t zc = { 0 };
48701af68beaSAlexander Stetsenko 	libzfs_handle_t *hdl = zhp->zfs_hdl;
48711af68beaSAlexander Stetsenko 	char *nvbuf;
48723b2aab18SMatthew Ahrens 	char errbuf[1024];
48731af68beaSAlexander Stetsenko 	size_t nvsz;
48741af68beaSAlexander Stetsenko 	int err;
48751af68beaSAlexander Stetsenko 
48761af68beaSAlexander Stetsenko 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
48771af68beaSAlexander Stetsenko 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
48781af68beaSAlexander Stetsenko 
48791af68beaSAlexander Stetsenko 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
48801af68beaSAlexander Stetsenko 	assert(err == 0);
48811af68beaSAlexander Stetsenko 
48821af68beaSAlexander Stetsenko 	nvbuf = malloc(nvsz);
48831af68beaSAlexander Stetsenko 
48841af68beaSAlexander Stetsenko 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
48851af68beaSAlexander Stetsenko 	assert(err == 0);
48861af68beaSAlexander Stetsenko 
48871af68beaSAlexander Stetsenko 	zc.zc_nvlist_src_size = nvsz;
48881af68beaSAlexander Stetsenko 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
48891af68beaSAlexander Stetsenko 	zc.zc_perm_action = un;
48901af68beaSAlexander Stetsenko 
48911af68beaSAlexander Stetsenko 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
48921af68beaSAlexander Stetsenko 
48931af68beaSAlexander Stetsenko 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
48941af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
48951af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
48961af68beaSAlexander Stetsenko 		    zc.zc_name);
48971af68beaSAlexander Stetsenko 		switch (errno) {
48981af68beaSAlexander Stetsenko 		case ENOTSUP:
48991af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49001af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
49011af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
49021af68beaSAlexander Stetsenko 			break;
49031af68beaSAlexander Stetsenko 		case EINVAL:
49041af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
49051af68beaSAlexander Stetsenko 			break;
49061af68beaSAlexander Stetsenko 		case ENOENT:
49071af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
49081af68beaSAlexander Stetsenko 			break;
49091af68beaSAlexander Stetsenko 		default:
49101af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
49111af68beaSAlexander Stetsenko 			break;
49121af68beaSAlexander Stetsenko 		}
49131af68beaSAlexander Stetsenko 	}
49141af68beaSAlexander Stetsenko 
49151af68beaSAlexander Stetsenko 	free(nvbuf);
49161af68beaSAlexander Stetsenko 
49171af68beaSAlexander Stetsenko 	return (err);
49181af68beaSAlexander Stetsenko }
49191af68beaSAlexander Stetsenko 
49201af68beaSAlexander Stetsenko int
49211af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
49221af68beaSAlexander Stetsenko {
49233b2aab18SMatthew Ahrens 	int err;
49243b2aab18SMatthew Ahrens 	char errbuf[1024];
49253b2aab18SMatthew Ahrens 
49263b2aab18SMatthew Ahrens 	err = lzc_get_holds(zhp->zfs_name, nvl);
49273b2aab18SMatthew Ahrens 
49283b2aab18SMatthew Ahrens 	if (err != 0) {
49291af68beaSAlexander Stetsenko 		libzfs_handle_t *hdl = zhp->zfs_hdl;
49301af68beaSAlexander Stetsenko 
49311af68beaSAlexander Stetsenko 		(void) snprintf(errbuf, sizeof (errbuf),
49321af68beaSAlexander Stetsenko 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
49333b2aab18SMatthew Ahrens 		    zhp->zfs_name);
49343b2aab18SMatthew Ahrens 		switch (err) {
49351af68beaSAlexander Stetsenko 		case ENOTSUP:
49361af68beaSAlexander Stetsenko 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49371af68beaSAlexander Stetsenko 			    "pool must be upgraded"));
49381af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
49391af68beaSAlexander Stetsenko 			break;
49401af68beaSAlexander Stetsenko 		case EINVAL:
49411af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
49421af68beaSAlexander Stetsenko 			break;
49431af68beaSAlexander Stetsenko 		case ENOENT:
49441af68beaSAlexander Stetsenko 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
49451af68beaSAlexander Stetsenko 			break;
49461af68beaSAlexander Stetsenko 		default:
49471af68beaSAlexander Stetsenko 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
49481af68beaSAlexander Stetsenko 			break;
49491af68beaSAlexander Stetsenko 		}
49501af68beaSAlexander Stetsenko 	}
49511af68beaSAlexander Stetsenko 
49521af68beaSAlexander Stetsenko 	return (err);
49531af68beaSAlexander Stetsenko }
49541af68beaSAlexander Stetsenko 
49553e30c24aSWill Andrews /*
49563e30c24aSWill Andrews  * Convert the zvol's volume size to an appropriate reservation.
49573e30c24aSWill Andrews  * Note: If this routine is updated, it is necessary to update the ZFS test
49583e30c24aSWill Andrews  * suite's shell version in reservation.kshlib.
49593e30c24aSWill Andrews  */
4960c1449561SEric Taylor uint64_t
4961c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4962c1449561SEric Taylor {
4963c1449561SEric Taylor 	uint64_t numdb;
4964c1449561SEric Taylor 	uint64_t nblocks, volblocksize;
4965c1449561SEric Taylor 	int ncopies;
4966c1449561SEric Taylor 	char *strval;
4967c1449561SEric Taylor 
4968c1449561SEric Taylor 	if (nvlist_lookup_string(props,
4969c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4970c1449561SEric Taylor 		ncopies = atoi(strval);
4971c1449561SEric Taylor 	else
4972c1449561SEric Taylor 		ncopies = 1;
4973c1449561SEric Taylor 	if (nvlist_lookup_uint64(props,
4974c1449561SEric Taylor 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4975c1449561SEric Taylor 	    &volblocksize) != 0)
4976c1449561SEric Taylor 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4977c1449561SEric Taylor 	nblocks = volsize/volblocksize;
4978c1449561SEric Taylor 	/* start with metadnode L0-L6 */
4979c1449561SEric Taylor 	numdb = 7;
4980c1449561SEric Taylor 	/* calculate number of indirects */
4981c1449561SEric Taylor 	while (nblocks > 1) {
4982c1449561SEric Taylor 		nblocks += DNODES_PER_LEVEL - 1;
4983c1449561SEric Taylor 		nblocks /= DNODES_PER_LEVEL;
4984c1449561SEric Taylor 		numdb += nblocks;
4985c1449561SEric Taylor 	}
4986c1449561SEric Taylor 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4987c1449561SEric Taylor 	volsize *= ncopies;
4988c1449561SEric Taylor 	/*
4989c1449561SEric Taylor 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4990c1449561SEric Taylor 	 * compressed, but in practice they compress down to about
4991c1449561SEric Taylor 	 * 1100 bytes
4992c1449561SEric Taylor 	 */
4993c1449561SEric Taylor 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4994c1449561SEric Taylor 	volsize += numdb;
4995c1449561SEric Taylor 	return (volsize);
4996c1449561SEric Taylor }
4997