libzfs_dataset.c revision 298266
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
33 */
34
35#include <ctype.h>
36#include <errno.h>
37#include <libintl.h>
38#include <math.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <strings.h>
42#include <unistd.h>
43#include <stddef.h>
44#include <zone.h>
45#include <fcntl.h>
46#include <sys/mntent.h>
47#include <sys/mount.h>
48#include <priv.h>
49#include <pwd.h>
50#include <grp.h>
51#include <stddef.h>
52#include <idmap.h>
53
54#include <sys/dnode.h>
55#include <sys/spa.h>
56#include <sys/zap.h>
57#include <sys/misc.h>
58#include <libzfs.h>
59
60#include "zfs_namecheck.h"
61#include "zfs_prop.h"
62#include "libzfs_impl.h"
63#include "zfs_deleg.h"
64
65static int userquota_propname_decode(const char *propname, boolean_t zoned,
66    zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
67
68/*
69 * Given a single type (not a mask of types), return the type in a human
70 * readable form.
71 */
72const char *
73zfs_type_to_name(zfs_type_t type)
74{
75	switch (type) {
76	case ZFS_TYPE_FILESYSTEM:
77		return (dgettext(TEXT_DOMAIN, "filesystem"));
78	case ZFS_TYPE_SNAPSHOT:
79		return (dgettext(TEXT_DOMAIN, "snapshot"));
80	case ZFS_TYPE_VOLUME:
81		return (dgettext(TEXT_DOMAIN, "volume"));
82	}
83
84	return (NULL);
85}
86
87/*
88 * Given a path and mask of ZFS types, return a string describing this dataset.
89 * This is used when we fail to open a dataset and we cannot get an exact type.
90 * We guess what the type would have been based on the path and the mask of
91 * acceptable types.
92 */
93static const char *
94path_to_str(const char *path, int types)
95{
96	/*
97	 * When given a single type, always report the exact type.
98	 */
99	if (types == ZFS_TYPE_SNAPSHOT)
100		return (dgettext(TEXT_DOMAIN, "snapshot"));
101	if (types == ZFS_TYPE_FILESYSTEM)
102		return (dgettext(TEXT_DOMAIN, "filesystem"));
103	if (types == ZFS_TYPE_VOLUME)
104		return (dgettext(TEXT_DOMAIN, "volume"));
105
106	/*
107	 * The user is requesting more than one type of dataset.  If this is the
108	 * case, consult the path itself.  If we're looking for a snapshot, and
109	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
110	 * snapshot attribute and try again.
111	 */
112	if (types & ZFS_TYPE_SNAPSHOT) {
113		if (strchr(path, '@') != NULL)
114			return (dgettext(TEXT_DOMAIN, "snapshot"));
115		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
116	}
117
118	/*
119	 * The user has requested either filesystems or volumes.
120	 * We have no way of knowing a priori what type this would be, so always
121	 * report it as "filesystem" or "volume", our two primitive types.
122	 */
123	if (types & ZFS_TYPE_FILESYSTEM)
124		return (dgettext(TEXT_DOMAIN, "filesystem"));
125
126	assert(types & ZFS_TYPE_VOLUME);
127	return (dgettext(TEXT_DOMAIN, "volume"));
128}
129
130/*
131 * Validate a ZFS path.  This is used even before trying to open the dataset, to
132 * provide a more meaningful error message.  We call zfs_error_aux() to
133 * explain exactly why the name was not valid.
134 */
135int
136zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
137    boolean_t modifying)
138{
139	namecheck_err_t why;
140	char what;
141
142	(void) zfs_prop_get_table();
143	if (dataset_namecheck(path, &why, &what) != 0) {
144		if (hdl != NULL) {
145			switch (why) {
146			case NAME_ERR_TOOLONG:
147				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148				    "name is too long"));
149				break;
150
151			case NAME_ERR_LEADING_SLASH:
152				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153				    "leading slash in name"));
154				break;
155
156			case NAME_ERR_EMPTY_COMPONENT:
157				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158				    "empty component in name"));
159				break;
160
161			case NAME_ERR_TRAILING_SLASH:
162				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163				    "trailing slash in name"));
164				break;
165
166			case NAME_ERR_INVALCHAR:
167				zfs_error_aux(hdl,
168				    dgettext(TEXT_DOMAIN, "invalid character "
169				    "'%c' in name"), what);
170				break;
171
172			case NAME_ERR_MULTIPLE_AT:
173				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174				    "multiple '@' delimiters in name"));
175				break;
176
177			case NAME_ERR_NOLETTER:
178				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179				    "pool doesn't begin with a letter"));
180				break;
181
182			case NAME_ERR_RESERVED:
183				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184				    "name is reserved"));
185				break;
186
187			case NAME_ERR_DISKLIKE:
188				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
189				    "reserved disk name"));
190				break;
191			}
192		}
193
194		return (0);
195	}
196
197	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
198		if (hdl != NULL)
199			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200			    "snapshot delimiter '@' in filesystem name"));
201		return (0);
202	}
203
204	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
205		if (hdl != NULL)
206			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
207			    "missing '@' delimiter in snapshot name"));
208		return (0);
209	}
210
211	if (modifying && strchr(path, '%') != NULL) {
212		if (hdl != NULL)
213			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
214			    "invalid character %c in name"), '%');
215		return (0);
216	}
217
218	return (-1);
219}
220
221int
222zfs_name_valid(const char *name, zfs_type_t type)
223{
224	if (type == ZFS_TYPE_POOL)
225		return (zpool_name_valid(NULL, B_FALSE, name));
226	return (zfs_validate_name(NULL, name, type, B_FALSE));
227}
228
229/*
230 * This function takes the raw DSL properties, and filters out the user-defined
231 * properties into a separate nvlist.
232 */
233static nvlist_t *
234process_user_props(zfs_handle_t *zhp, nvlist_t *props)
235{
236	libzfs_handle_t *hdl = zhp->zfs_hdl;
237	nvpair_t *elem;
238	nvlist_t *propval;
239	nvlist_t *nvl;
240
241	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
242		(void) no_memory(hdl);
243		return (NULL);
244	}
245
246	elem = NULL;
247	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
248		if (!zfs_prop_user(nvpair_name(elem)))
249			continue;
250
251		verify(nvpair_value_nvlist(elem, &propval) == 0);
252		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
253			nvlist_free(nvl);
254			(void) no_memory(hdl);
255			return (NULL);
256		}
257	}
258
259	return (nvl);
260}
261
262static zpool_handle_t *
263zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
264{
265	libzfs_handle_t *hdl = zhp->zfs_hdl;
266	zpool_handle_t *zph;
267
268	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
269		if (hdl->libzfs_pool_handles != NULL)
270			zph->zpool_next = hdl->libzfs_pool_handles;
271		hdl->libzfs_pool_handles = zph;
272	}
273	return (zph);
274}
275
276static zpool_handle_t *
277zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
278{
279	libzfs_handle_t *hdl = zhp->zfs_hdl;
280	zpool_handle_t *zph = hdl->libzfs_pool_handles;
281
282	while ((zph != NULL) &&
283	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
284		zph = zph->zpool_next;
285	return (zph);
286}
287
288/*
289 * Returns a handle to the pool that contains the provided dataset.
290 * If a handle to that pool already exists then that handle is returned.
291 * Otherwise, a new handle is created and added to the list of handles.
292 */
293static zpool_handle_t *
294zpool_handle(zfs_handle_t *zhp)
295{
296	char *pool_name;
297	int len;
298	zpool_handle_t *zph;
299
300	len = strcspn(zhp->zfs_name, "/@#") + 1;
301	pool_name = zfs_alloc(zhp->zfs_hdl, len);
302	(void) strlcpy(pool_name, zhp->zfs_name, len);
303
304	zph = zpool_find_handle(zhp, pool_name, len);
305	if (zph == NULL)
306		zph = zpool_add_handle(zhp, pool_name);
307
308	free(pool_name);
309	return (zph);
310}
311
312void
313zpool_free_handles(libzfs_handle_t *hdl)
314{
315	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
316
317	while (zph != NULL) {
318		next = zph->zpool_next;
319		zpool_close(zph);
320		zph = next;
321	}
322	hdl->libzfs_pool_handles = NULL;
323}
324
325/*
326 * Utility function to gather stats (objset and zpl) for the given object.
327 */
328static int
329get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
330{
331	libzfs_handle_t *hdl = zhp->zfs_hdl;
332
333	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
334
335	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
336		if (errno == ENOMEM) {
337			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
338				return (-1);
339			}
340		} else {
341			return (-1);
342		}
343	}
344	return (0);
345}
346
347/*
348 * Utility function to get the received properties of the given object.
349 */
350static int
351get_recvd_props_ioctl(zfs_handle_t *zhp)
352{
353	libzfs_handle_t *hdl = zhp->zfs_hdl;
354	nvlist_t *recvdprops;
355	zfs_cmd_t zc = { 0 };
356	int err;
357
358	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
359		return (-1);
360
361	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
362
363	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
364		if (errno == ENOMEM) {
365			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
366				return (-1);
367			}
368		} else {
369			zcmd_free_nvlists(&zc);
370			return (-1);
371		}
372	}
373
374	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
375	zcmd_free_nvlists(&zc);
376	if (err != 0)
377		return (-1);
378
379	nvlist_free(zhp->zfs_recvd_props);
380	zhp->zfs_recvd_props = recvdprops;
381
382	return (0);
383}
384
385static int
386put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
387{
388	nvlist_t *allprops, *userprops;
389
390	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
391
392	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
393		return (-1);
394	}
395
396	/*
397	 * XXX Why do we store the user props separately, in addition to
398	 * storing them in zfs_props?
399	 */
400	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
401		nvlist_free(allprops);
402		return (-1);
403	}
404
405	nvlist_free(zhp->zfs_props);
406	nvlist_free(zhp->zfs_user_props);
407
408	zhp->zfs_props = allprops;
409	zhp->zfs_user_props = userprops;
410
411	return (0);
412}
413
414static int
415get_stats(zfs_handle_t *zhp)
416{
417	int rc = 0;
418	zfs_cmd_t zc = { 0 };
419
420	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
421		return (-1);
422	if (get_stats_ioctl(zhp, &zc) != 0)
423		rc = -1;
424	else if (put_stats_zhdl(zhp, &zc) != 0)
425		rc = -1;
426	zcmd_free_nvlists(&zc);
427	return (rc);
428}
429
430/*
431 * Refresh the properties currently stored in the handle.
432 */
433void
434zfs_refresh_properties(zfs_handle_t *zhp)
435{
436	(void) get_stats(zhp);
437}
438
439/*
440 * Makes a handle from the given dataset name.  Used by zfs_open() and
441 * zfs_iter_* to create child handles on the fly.
442 */
443static int
444make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
445{
446	if (put_stats_zhdl(zhp, zc) != 0)
447		return (-1);
448
449	/*
450	 * We've managed to open the dataset and gather statistics.  Determine
451	 * the high-level type.
452	 */
453	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
454		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
455	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
456		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
457	else
458		abort();
459
460	if (zhp->zfs_dmustats.dds_is_snapshot)
461		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
462	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
463		zhp->zfs_type = ZFS_TYPE_VOLUME;
464	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
465		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
466	else
467		abort();	/* we should never see any other types */
468
469	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
470		return (-1);
471
472	return (0);
473}
474
475zfs_handle_t *
476make_dataset_handle(libzfs_handle_t *hdl, const char *path)
477{
478	zfs_cmd_t zc = { 0 };
479
480	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
481
482	if (zhp == NULL)
483		return (NULL);
484
485	zhp->zfs_hdl = hdl;
486	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
487	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
488		free(zhp);
489		return (NULL);
490	}
491	if (get_stats_ioctl(zhp, &zc) == -1) {
492		zcmd_free_nvlists(&zc);
493		free(zhp);
494		return (NULL);
495	}
496	if (make_dataset_handle_common(zhp, &zc) == -1) {
497		free(zhp);
498		zhp = NULL;
499	}
500	zcmd_free_nvlists(&zc);
501	return (zhp);
502}
503
504zfs_handle_t *
505make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
506{
507	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
508
509	if (zhp == NULL)
510		return (NULL);
511
512	zhp->zfs_hdl = hdl;
513	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
514	if (make_dataset_handle_common(zhp, zc) == -1) {
515		free(zhp);
516		return (NULL);
517	}
518	return (zhp);
519}
520
521zfs_handle_t *
522make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
523{
524	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
525
526	if (zhp == NULL)
527		return (NULL);
528
529	zhp->zfs_hdl = pzhp->zfs_hdl;
530	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
531	zhp->zfs_head_type = pzhp->zfs_type;
532	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
533	zhp->zpool_hdl = zpool_handle(zhp);
534	return (zhp);
535}
536
537zfs_handle_t *
538zfs_handle_dup(zfs_handle_t *zhp_orig)
539{
540	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
541
542	if (zhp == NULL)
543		return (NULL);
544
545	zhp->zfs_hdl = zhp_orig->zfs_hdl;
546	zhp->zpool_hdl = zhp_orig->zpool_hdl;
547	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
548	    sizeof (zhp->zfs_name));
549	zhp->zfs_type = zhp_orig->zfs_type;
550	zhp->zfs_head_type = zhp_orig->zfs_head_type;
551	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
552	if (zhp_orig->zfs_props != NULL) {
553		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
554			(void) no_memory(zhp->zfs_hdl);
555			zfs_close(zhp);
556			return (NULL);
557		}
558	}
559	if (zhp_orig->zfs_user_props != NULL) {
560		if (nvlist_dup(zhp_orig->zfs_user_props,
561		    &zhp->zfs_user_props, 0) != 0) {
562			(void) no_memory(zhp->zfs_hdl);
563			zfs_close(zhp);
564			return (NULL);
565		}
566	}
567	if (zhp_orig->zfs_recvd_props != NULL) {
568		if (nvlist_dup(zhp_orig->zfs_recvd_props,
569		    &zhp->zfs_recvd_props, 0)) {
570			(void) no_memory(zhp->zfs_hdl);
571			zfs_close(zhp);
572			return (NULL);
573		}
574	}
575	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
576	if (zhp_orig->zfs_mntopts != NULL) {
577		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
578		    zhp_orig->zfs_mntopts);
579	}
580	zhp->zfs_props_table = zhp_orig->zfs_props_table;
581	return (zhp);
582}
583
584boolean_t
585zfs_bookmark_exists(const char *path)
586{
587	nvlist_t *bmarks;
588	nvlist_t *props;
589	char fsname[ZFS_MAXNAMELEN];
590	char *bmark_name;
591	char *pound;
592	int err;
593	boolean_t rv;
594
595
596	(void) strlcpy(fsname, path, sizeof (fsname));
597	pound = strchr(fsname, '#');
598	if (pound == NULL)
599		return (B_FALSE);
600
601	*pound = '\0';
602	bmark_name = pound + 1;
603	props = fnvlist_alloc();
604	err = lzc_get_bookmarks(fsname, props, &bmarks);
605	nvlist_free(props);
606	if (err != 0) {
607		nvlist_free(bmarks);
608		return (B_FALSE);
609	}
610
611	rv = nvlist_exists(bmarks, bmark_name);
612	nvlist_free(bmarks);
613	return (rv);
614}
615
616zfs_handle_t *
617make_bookmark_handle(zfs_handle_t *parent, const char *path,
618    nvlist_t *bmark_props)
619{
620	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
621
622	if (zhp == NULL)
623		return (NULL);
624
625	/* Fill in the name. */
626	zhp->zfs_hdl = parent->zfs_hdl;
627	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
628
629	/* Set the property lists. */
630	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
631		free(zhp);
632		return (NULL);
633	}
634
635	/* Set the types. */
636	zhp->zfs_head_type = parent->zfs_head_type;
637	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
638
639	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
640		nvlist_free(zhp->zfs_props);
641		free(zhp);
642		return (NULL);
643	}
644
645	return (zhp);
646}
647
648/*
649 * Opens the given snapshot, filesystem, or volume.   The 'types'
650 * argument is a mask of acceptable types.  The function will print an
651 * appropriate error message and return NULL if it can't be opened.
652 */
653zfs_handle_t *
654zfs_open(libzfs_handle_t *hdl, const char *path, int types)
655{
656	zfs_handle_t *zhp;
657	char errbuf[1024];
658
659	(void) snprintf(errbuf, sizeof (errbuf),
660	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
661
662	/*
663	 * Validate the name before we even try to open it.
664	 */
665	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
666		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
667		    "invalid dataset name"));
668		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
669		return (NULL);
670	}
671
672	/*
673	 * Try to get stats for the dataset, which will tell us if it exists.
674	 */
675	errno = 0;
676	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
677		(void) zfs_standard_error(hdl, errno, errbuf);
678		return (NULL);
679	}
680
681	if (zhp == NULL) {
682		char *at = strchr(path, '@');
683
684		if (at != NULL)
685			*at = '\0';
686		errno = 0;
687		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
688			(void) zfs_standard_error(hdl, errno, errbuf);
689			return (NULL);
690		}
691		if (at != NULL)
692			*at = '@';
693		(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
694		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
695	}
696
697	if (!(types & zhp->zfs_type)) {
698		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
699		zfs_close(zhp);
700		return (NULL);
701	}
702
703	return (zhp);
704}
705
706/*
707 * Release a ZFS handle.  Nothing to do but free the associated memory.
708 */
709void
710zfs_close(zfs_handle_t *zhp)
711{
712	if (zhp->zfs_mntopts)
713		free(zhp->zfs_mntopts);
714	nvlist_free(zhp->zfs_props);
715	nvlist_free(zhp->zfs_user_props);
716	nvlist_free(zhp->zfs_recvd_props);
717	free(zhp);
718}
719
720typedef struct mnttab_node {
721	struct mnttab mtn_mt;
722	avl_node_t mtn_node;
723} mnttab_node_t;
724
725static int
726libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
727{
728	const mnttab_node_t *mtn1 = arg1;
729	const mnttab_node_t *mtn2 = arg2;
730	int rv;
731
732	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
733
734	if (rv == 0)
735		return (0);
736	return (rv > 0 ? 1 : -1);
737}
738
739void
740libzfs_mnttab_init(libzfs_handle_t *hdl)
741{
742	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
743	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
744	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
745}
746
747void
748libzfs_mnttab_update(libzfs_handle_t *hdl)
749{
750	struct mnttab entry;
751
752	rewind(hdl->libzfs_mnttab);
753	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
754		mnttab_node_t *mtn;
755
756		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
757			continue;
758		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
759		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
760		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
761		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
762		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
763		avl_add(&hdl->libzfs_mnttab_cache, mtn);
764	}
765}
766
767void
768libzfs_mnttab_fini(libzfs_handle_t *hdl)
769{
770	void *cookie = NULL;
771	mnttab_node_t *mtn;
772
773	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
774		free(mtn->mtn_mt.mnt_special);
775		free(mtn->mtn_mt.mnt_mountp);
776		free(mtn->mtn_mt.mnt_fstype);
777		free(mtn->mtn_mt.mnt_mntopts);
778		free(mtn);
779	}
780	avl_destroy(&hdl->libzfs_mnttab_cache);
781}
782
783void
784libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
785{
786	hdl->libzfs_mnttab_enable = enable;
787}
788
789int
790libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
791    struct mnttab *entry)
792{
793	mnttab_node_t find;
794	mnttab_node_t *mtn;
795
796	if (!hdl->libzfs_mnttab_enable) {
797		struct mnttab srch = { 0 };
798
799		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
800			libzfs_mnttab_fini(hdl);
801		rewind(hdl->libzfs_mnttab);
802		srch.mnt_special = (char *)fsname;
803		srch.mnt_fstype = MNTTYPE_ZFS;
804		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
805			return (0);
806		else
807			return (ENOENT);
808	}
809
810	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
811		libzfs_mnttab_update(hdl);
812
813	find.mtn_mt.mnt_special = (char *)fsname;
814	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
815	if (mtn) {
816		*entry = mtn->mtn_mt;
817		return (0);
818	}
819	return (ENOENT);
820}
821
822void
823libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
824    const char *mountp, const char *mntopts)
825{
826	mnttab_node_t *mtn;
827
828	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
829		return;
830	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
831	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
832	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
833	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
834	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
835	avl_add(&hdl->libzfs_mnttab_cache, mtn);
836}
837
838void
839libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
840{
841	mnttab_node_t find;
842	mnttab_node_t *ret;
843
844	find.mtn_mt.mnt_special = (char *)fsname;
845	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
846		avl_remove(&hdl->libzfs_mnttab_cache, ret);
847		free(ret->mtn_mt.mnt_special);
848		free(ret->mtn_mt.mnt_mountp);
849		free(ret->mtn_mt.mnt_fstype);
850		free(ret->mtn_mt.mnt_mntopts);
851		free(ret);
852	}
853}
854
855int
856zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
857{
858	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
859
860	if (zpool_handle == NULL)
861		return (-1);
862
863	*spa_version = zpool_get_prop_int(zpool_handle,
864	    ZPOOL_PROP_VERSION, NULL);
865	return (0);
866}
867
868/*
869 * The choice of reservation property depends on the SPA version.
870 */
871static int
872zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
873{
874	int spa_version;
875
876	if (zfs_spa_version(zhp, &spa_version) < 0)
877		return (-1);
878
879	if (spa_version >= SPA_VERSION_REFRESERVATION)
880		*resv_prop = ZFS_PROP_REFRESERVATION;
881	else
882		*resv_prop = ZFS_PROP_RESERVATION;
883
884	return (0);
885}
886
887/*
888 * Given an nvlist of properties to set, validates that they are correct, and
889 * parses any numeric properties (index, boolean, etc) if they are specified as
890 * strings.
891 */
892nvlist_t *
893zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
894    uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
895    const char *errbuf)
896{
897	nvpair_t *elem;
898	uint64_t intval;
899	char *strval;
900	zfs_prop_t prop;
901	nvlist_t *ret;
902	int chosen_normal = -1;
903	int chosen_utf = -1;
904
905	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
906		(void) no_memory(hdl);
907		return (NULL);
908	}
909
910	/*
911	 * Make sure this property is valid and applies to this type.
912	 */
913
914	elem = NULL;
915	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
916		const char *propname = nvpair_name(elem);
917
918		prop = zfs_name_to_prop(propname);
919		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
920			/*
921			 * This is a user property: make sure it's a
922			 * string, and that it's less than ZAP_MAXNAMELEN.
923			 */
924			if (nvpair_type(elem) != DATA_TYPE_STRING) {
925				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
926				    "'%s' must be a string"), propname);
927				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
928				goto error;
929			}
930
931			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
932				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933				    "property name '%s' is too long"),
934				    propname);
935				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
936				goto error;
937			}
938
939			(void) nvpair_value_string(elem, &strval);
940			if (nvlist_add_string(ret, propname, strval) != 0) {
941				(void) no_memory(hdl);
942				goto error;
943			}
944			continue;
945		}
946
947		/*
948		 * Currently, only user properties can be modified on
949		 * snapshots.
950		 */
951		if (type == ZFS_TYPE_SNAPSHOT) {
952			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953			    "this property can not be modified for snapshots"));
954			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
955			goto error;
956		}
957
958		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
959			zfs_userquota_prop_t uqtype;
960			char newpropname[128];
961			char domain[128];
962			uint64_t rid;
963			uint64_t valary[3];
964
965			if (userquota_propname_decode(propname, zoned,
966			    &uqtype, domain, sizeof (domain), &rid) != 0) {
967				zfs_error_aux(hdl,
968				    dgettext(TEXT_DOMAIN,
969				    "'%s' has an invalid user/group name"),
970				    propname);
971				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
972				goto error;
973			}
974
975			if (uqtype != ZFS_PROP_USERQUOTA &&
976			    uqtype != ZFS_PROP_GROUPQUOTA) {
977				zfs_error_aux(hdl,
978				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
979				    propname);
980				(void) zfs_error(hdl, EZFS_PROPREADONLY,
981				    errbuf);
982				goto error;
983			}
984
985			if (nvpair_type(elem) == DATA_TYPE_STRING) {
986				(void) nvpair_value_string(elem, &strval);
987				if (strcmp(strval, "none") == 0) {
988					intval = 0;
989				} else if (zfs_nicestrtonum(hdl,
990				    strval, &intval) != 0) {
991					(void) zfs_error(hdl,
992					    EZFS_BADPROP, errbuf);
993					goto error;
994				}
995			} else if (nvpair_type(elem) ==
996			    DATA_TYPE_UINT64) {
997				(void) nvpair_value_uint64(elem, &intval);
998				if (intval == 0) {
999					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000					    "use 'none' to disable "
1001					    "userquota/groupquota"));
1002					goto error;
1003				}
1004			} else {
1005				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1006				    "'%s' must be a number"), propname);
1007				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1008				goto error;
1009			}
1010
1011			/*
1012			 * Encode the prop name as
1013			 * userquota@<hex-rid>-domain, to make it easy
1014			 * for the kernel to decode.
1015			 */
1016			(void) snprintf(newpropname, sizeof (newpropname),
1017			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1018			    (longlong_t)rid, domain);
1019			valary[0] = uqtype;
1020			valary[1] = rid;
1021			valary[2] = intval;
1022			if (nvlist_add_uint64_array(ret, newpropname,
1023			    valary, 3) != 0) {
1024				(void) no_memory(hdl);
1025				goto error;
1026			}
1027			continue;
1028		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1029			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1030			    "'%s' is readonly"),
1031			    propname);
1032			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1033			goto error;
1034		}
1035
1036		if (prop == ZPROP_INVAL) {
1037			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1038			    "invalid property '%s'"), propname);
1039			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1040			goto error;
1041		}
1042
1043		if (!zfs_prop_valid_for_type(prop, type)) {
1044			zfs_error_aux(hdl,
1045			    dgettext(TEXT_DOMAIN, "'%s' does not "
1046			    "apply to datasets of this type"), propname);
1047			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1048			goto error;
1049		}
1050
1051		if (zfs_prop_readonly(prop) &&
1052		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
1053			zfs_error_aux(hdl,
1054			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1055			    propname);
1056			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1057			goto error;
1058		}
1059
1060		if (zprop_parse_value(hdl, elem, prop, type, ret,
1061		    &strval, &intval, errbuf) != 0)
1062			goto error;
1063
1064		/*
1065		 * Perform some additional checks for specific properties.
1066		 */
1067		switch (prop) {
1068		case ZFS_PROP_VERSION:
1069		{
1070			int version;
1071
1072			if (zhp == NULL)
1073				break;
1074			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1075			if (intval < version) {
1076				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1077				    "Can not downgrade; already at version %u"),
1078				    version);
1079				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1080				goto error;
1081			}
1082			break;
1083		}
1084
1085		case ZFS_PROP_VOLBLOCKSIZE:
1086		case ZFS_PROP_RECORDSIZE:
1087		{
1088			int maxbs = SPA_MAXBLOCKSIZE;
1089			if (zpool_hdl != NULL) {
1090				maxbs = zpool_get_prop_int(zpool_hdl,
1091				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1092			}
1093			/*
1094			 * Volumes are limited to a volblocksize of 128KB,
1095			 * because they typically service workloads with
1096			 * small random writes, which incur a large performance
1097			 * penalty with large blocks.
1098			 */
1099			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1100				maxbs = SPA_OLD_MAXBLOCKSIZE;
1101			/*
1102			 * The value must be a power of two between
1103			 * SPA_MINBLOCKSIZE and maxbs.
1104			 */
1105			if (intval < SPA_MINBLOCKSIZE ||
1106			    intval > maxbs || !ISP2(intval)) {
1107				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1108				    "'%s' must be power of 2 from 512B "
1109				    "to %uKB"), propname, maxbs >> 10);
1110				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1111				goto error;
1112			}
1113			break;
1114		}
1115		case ZFS_PROP_MLSLABEL:
1116		{
1117#ifdef illumos
1118			/*
1119			 * Verify the mlslabel string and convert to
1120			 * internal hex label string.
1121			 */
1122
1123			m_label_t *new_sl;
1124			char *hex = NULL;	/* internal label string */
1125
1126			/* Default value is already OK. */
1127			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1128				break;
1129
1130			/* Verify the label can be converted to binary form */
1131			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1132			    (str_to_label(strval, &new_sl, MAC_LABEL,
1133			    L_NO_CORRECTION, NULL) == -1)) {
1134				goto badlabel;
1135			}
1136
1137			/* Now translate to hex internal label string */
1138			if (label_to_str(new_sl, &hex, M_INTERNAL,
1139			    DEF_NAMES) != 0) {
1140				if (hex)
1141					free(hex);
1142				goto badlabel;
1143			}
1144			m_label_free(new_sl);
1145
1146			/* If string is already in internal form, we're done. */
1147			if (strcmp(strval, hex) == 0) {
1148				free(hex);
1149				break;
1150			}
1151
1152			/* Replace the label string with the internal form. */
1153			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
1154			    DATA_TYPE_STRING);
1155			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1156			    hex) == 0);
1157			free(hex);
1158
1159			break;
1160
1161badlabel:
1162			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1163			    "invalid mlslabel '%s'"), strval);
1164			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1165			m_label_free(new_sl);	/* OK if null */
1166#else	/* !illumos */
1167			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1168			    "mlslabel is not supported on FreeBSD"));
1169			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1170#endif	/* illumos */
1171			goto error;
1172
1173		}
1174
1175		case ZFS_PROP_MOUNTPOINT:
1176		{
1177			namecheck_err_t why;
1178
1179			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1180			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1181				break;
1182
1183			if (mountpoint_namecheck(strval, &why)) {
1184				switch (why) {
1185				case NAME_ERR_LEADING_SLASH:
1186					zfs_error_aux(hdl,
1187					    dgettext(TEXT_DOMAIN,
1188					    "'%s' must be an absolute path, "
1189					    "'none', or 'legacy'"), propname);
1190					break;
1191				case NAME_ERR_TOOLONG:
1192					zfs_error_aux(hdl,
1193					    dgettext(TEXT_DOMAIN,
1194					    "component of '%s' is too long"),
1195					    propname);
1196					break;
1197				}
1198				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1199				goto error;
1200			}
1201		}
1202
1203			/*FALLTHRU*/
1204
1205		case ZFS_PROP_SHARESMB:
1206		case ZFS_PROP_SHARENFS:
1207			/*
1208			 * For the mountpoint and sharenfs or sharesmb
1209			 * properties, check if it can be set in a
1210			 * global/non-global zone based on
1211			 * the zoned property value:
1212			 *
1213			 *		global zone	    non-global zone
1214			 * --------------------------------------------------
1215			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1216			 *		sharenfs (no)	    sharenfs (no)
1217			 *		sharesmb (no)	    sharesmb (no)
1218			 *
1219			 * zoned=off	mountpoint (yes)	N/A
1220			 *		sharenfs (yes)
1221			 *		sharesmb (yes)
1222			 */
1223			if (zoned) {
1224				if (getzoneid() == GLOBAL_ZONEID) {
1225					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1226					    "'%s' cannot be set on "
1227					    "dataset in a non-global zone"),
1228					    propname);
1229					(void) zfs_error(hdl, EZFS_ZONED,
1230					    errbuf);
1231					goto error;
1232				} else if (prop == ZFS_PROP_SHARENFS ||
1233				    prop == ZFS_PROP_SHARESMB) {
1234					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235					    "'%s' cannot be set in "
1236					    "a non-global zone"), propname);
1237					(void) zfs_error(hdl, EZFS_ZONED,
1238					    errbuf);
1239					goto error;
1240				}
1241			} else if (getzoneid() != GLOBAL_ZONEID) {
1242				/*
1243				 * If zoned property is 'off', this must be in
1244				 * a global zone. If not, something is wrong.
1245				 */
1246				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1247				    "'%s' cannot be set while dataset "
1248				    "'zoned' property is set"), propname);
1249				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1250				goto error;
1251			}
1252
1253			/*
1254			 * At this point, it is legitimate to set the
1255			 * property. Now we want to make sure that the
1256			 * property value is valid if it is sharenfs.
1257			 */
1258			if ((prop == ZFS_PROP_SHARENFS ||
1259			    prop == ZFS_PROP_SHARESMB) &&
1260			    strcmp(strval, "on") != 0 &&
1261			    strcmp(strval, "off") != 0) {
1262				zfs_share_proto_t proto;
1263
1264				if (prop == ZFS_PROP_SHARESMB)
1265					proto = PROTO_SMB;
1266				else
1267					proto = PROTO_NFS;
1268
1269				/*
1270				 * Must be an valid sharing protocol
1271				 * option string so init the libshare
1272				 * in order to enable the parser and
1273				 * then parse the options. We use the
1274				 * control API since we don't care about
1275				 * the current configuration and don't
1276				 * want the overhead of loading it
1277				 * until we actually do something.
1278				 */
1279
1280				if (zfs_init_libshare(hdl,
1281				    SA_INIT_CONTROL_API) != SA_OK) {
1282					/*
1283					 * An error occurred so we can't do
1284					 * anything
1285					 */
1286					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1287					    "'%s' cannot be set: problem "
1288					    "in share initialization"),
1289					    propname);
1290					(void) zfs_error(hdl, EZFS_BADPROP,
1291					    errbuf);
1292					goto error;
1293				}
1294
1295				if (zfs_parse_options(strval, proto) != SA_OK) {
1296					/*
1297					 * There was an error in parsing so
1298					 * deal with it by issuing an error
1299					 * message and leaving after
1300					 * uninitializing the the libshare
1301					 * interface.
1302					 */
1303					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1304					    "'%s' cannot be set to invalid "
1305					    "options"), propname);
1306					(void) zfs_error(hdl, EZFS_BADPROP,
1307					    errbuf);
1308					zfs_uninit_libshare(hdl);
1309					goto error;
1310				}
1311				zfs_uninit_libshare(hdl);
1312			}
1313
1314			break;
1315		case ZFS_PROP_UTF8ONLY:
1316			chosen_utf = (int)intval;
1317			break;
1318		case ZFS_PROP_NORMALIZE:
1319			chosen_normal = (int)intval;
1320			break;
1321		}
1322
1323		/*
1324		 * For changes to existing volumes, we have some additional
1325		 * checks to enforce.
1326		 */
1327		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1328			uint64_t volsize = zfs_prop_get_int(zhp,
1329			    ZFS_PROP_VOLSIZE);
1330			uint64_t blocksize = zfs_prop_get_int(zhp,
1331			    ZFS_PROP_VOLBLOCKSIZE);
1332			char buf[64];
1333
1334			switch (prop) {
1335			case ZFS_PROP_RESERVATION:
1336			case ZFS_PROP_REFRESERVATION:
1337				if (intval > volsize) {
1338					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339					    "'%s' is greater than current "
1340					    "volume size"), propname);
1341					(void) zfs_error(hdl, EZFS_BADPROP,
1342					    errbuf);
1343					goto error;
1344				}
1345				break;
1346
1347			case ZFS_PROP_VOLSIZE:
1348				if (intval % blocksize != 0) {
1349					zfs_nicenum(blocksize, buf,
1350					    sizeof (buf));
1351					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352					    "'%s' must be a multiple of "
1353					    "volume block size (%s)"),
1354					    propname, buf);
1355					(void) zfs_error(hdl, EZFS_BADPROP,
1356					    errbuf);
1357					goto error;
1358				}
1359
1360				if (intval == 0) {
1361					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1362					    "'%s' cannot be zero"),
1363					    propname);
1364					(void) zfs_error(hdl, EZFS_BADPROP,
1365					    errbuf);
1366					goto error;
1367				}
1368				break;
1369			}
1370		}
1371	}
1372
1373	/*
1374	 * If normalization was chosen, but no UTF8 choice was made,
1375	 * enforce rejection of non-UTF8 names.
1376	 *
1377	 * If normalization was chosen, but rejecting non-UTF8 names
1378	 * was explicitly not chosen, it is an error.
1379	 */
1380	if (chosen_normal > 0 && chosen_utf < 0) {
1381		if (nvlist_add_uint64(ret,
1382		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1383			(void) no_memory(hdl);
1384			goto error;
1385		}
1386	} else if (chosen_normal > 0 && chosen_utf == 0) {
1387		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1388		    "'%s' must be set 'on' if normalization chosen"),
1389		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1390		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1391		goto error;
1392	}
1393	return (ret);
1394
1395error:
1396	nvlist_free(ret);
1397	return (NULL);
1398}
1399
1400int
1401zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1402{
1403	uint64_t old_volsize;
1404	uint64_t new_volsize;
1405	uint64_t old_reservation;
1406	uint64_t new_reservation;
1407	zfs_prop_t resv_prop;
1408	nvlist_t *props;
1409
1410	/*
1411	 * If this is an existing volume, and someone is setting the volsize,
1412	 * make sure that it matches the reservation, or add it if necessary.
1413	 */
1414	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1415	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1416		return (-1);
1417	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1418
1419	props = fnvlist_alloc();
1420	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1421	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1422
1423	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1424	    old_reservation) || nvlist_exists(nvl,
1425	    zfs_prop_to_name(resv_prop))) {
1426		fnvlist_free(props);
1427		return (0);
1428	}
1429	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1430	    &new_volsize) != 0) {
1431		fnvlist_free(props);
1432		return (-1);
1433	}
1434	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1435	fnvlist_free(props);
1436
1437	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1438	    new_reservation) != 0) {
1439		(void) no_memory(zhp->zfs_hdl);
1440		return (-1);
1441	}
1442	return (1);
1443}
1444
1445void
1446zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1447    char *errbuf)
1448{
1449	switch (err) {
1450
1451	case ENOSPC:
1452		/*
1453		 * For quotas and reservations, ENOSPC indicates
1454		 * something different; setting a quota or reservation
1455		 * doesn't use any disk space.
1456		 */
1457		switch (prop) {
1458		case ZFS_PROP_QUOTA:
1459		case ZFS_PROP_REFQUOTA:
1460			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1461			    "size is less than current used or "
1462			    "reserved space"));
1463			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1464			break;
1465
1466		case ZFS_PROP_RESERVATION:
1467		case ZFS_PROP_REFRESERVATION:
1468			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1469			    "size is greater than available space"));
1470			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1471			break;
1472
1473		default:
1474			(void) zfs_standard_error(hdl, err, errbuf);
1475			break;
1476		}
1477		break;
1478
1479	case EBUSY:
1480		(void) zfs_standard_error(hdl, EBUSY, errbuf);
1481		break;
1482
1483	case EROFS:
1484		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1485		break;
1486
1487	case ENOTSUP:
1488		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1489		    "pool and or dataset must be upgraded to set this "
1490		    "property or value"));
1491		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1492		break;
1493
1494	case ERANGE:
1495	case EDOM:
1496		if (prop == ZFS_PROP_COMPRESSION ||
1497		    prop == ZFS_PROP_RECORDSIZE) {
1498			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1499			    "property setting is not allowed on "
1500			    "bootable datasets"));
1501			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1502		} else if (prop == ZFS_PROP_CHECKSUM ||
1503		    prop == ZFS_PROP_DEDUP) {
1504			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505			    "property setting is not allowed on "
1506			    "root pools"));
1507			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1508		} else {
1509			(void) zfs_standard_error(hdl, err, errbuf);
1510		}
1511		break;
1512
1513	case EINVAL:
1514		if (prop == ZPROP_INVAL) {
1515			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1516		} else {
1517			(void) zfs_standard_error(hdl, err, errbuf);
1518		}
1519		break;
1520
1521	case EOVERFLOW:
1522		/*
1523		 * This platform can't address a volume this big.
1524		 */
1525#ifdef _ILP32
1526		if (prop == ZFS_PROP_VOLSIZE) {
1527			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1528			break;
1529		}
1530#endif
1531		/* FALLTHROUGH */
1532	default:
1533		(void) zfs_standard_error(hdl, err, errbuf);
1534	}
1535}
1536
1537/*
1538 * Given a property name and value, set the property for the given dataset.
1539 */
1540int
1541zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1542{
1543	int ret = -1;
1544	char errbuf[1024];
1545	libzfs_handle_t *hdl = zhp->zfs_hdl;
1546	nvlist_t *nvl = NULL;
1547
1548	(void) snprintf(errbuf, sizeof (errbuf),
1549	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1550	    zhp->zfs_name);
1551
1552	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1553	    nvlist_add_string(nvl, propname, propval) != 0) {
1554		(void) no_memory(hdl);
1555		goto error;
1556	}
1557
1558	ret = zfs_prop_set_list(zhp, nvl);
1559
1560error:
1561	nvlist_free(nvl);
1562	return (ret);
1563}
1564
1565
1566
1567/*
1568 * Given an nvlist of property names and values, set the properties for the
1569 * given dataset.
1570 */
1571int
1572zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1573{
1574	zfs_cmd_t zc = { 0 };
1575	int ret = -1;
1576	prop_changelist_t **cls = NULL;
1577	int cl_idx;
1578	char errbuf[1024];
1579	libzfs_handle_t *hdl = zhp->zfs_hdl;
1580	nvlist_t *nvl;
1581	int nvl_len;
1582	int added_resv;
1583
1584	(void) snprintf(errbuf, sizeof (errbuf),
1585	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1586	    zhp->zfs_name);
1587
1588	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1589	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1590	    errbuf)) == NULL)
1591		goto error;
1592
1593	/*
1594	 * We have to check for any extra properties which need to be added
1595	 * before computing the length of the nvlist.
1596	 */
1597	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1598	    elem != NULL;
1599	    elem = nvlist_next_nvpair(nvl, elem)) {
1600		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1601		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1602			goto error;
1603		}
1604	}
1605	/*
1606	 * Check how many properties we're setting and allocate an array to
1607	 * store changelist pointers for postfix().
1608	 */
1609	nvl_len = 0;
1610	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1611	    elem != NULL;
1612	    elem = nvlist_next_nvpair(nvl, elem))
1613		nvl_len++;
1614	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1615		goto error;
1616
1617	cl_idx = 0;
1618	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1619	    elem != NULL;
1620	    elem = nvlist_next_nvpair(nvl, elem)) {
1621
1622		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1623
1624		assert(cl_idx < nvl_len);
1625		/*
1626		 * We don't want to unmount & remount the dataset when changing
1627		 * its canmount property to 'on' or 'noauto'.  We only use
1628		 * the changelist logic to unmount when setting canmount=off.
1629		 */
1630		if (prop != ZFS_PROP_CANMOUNT ||
1631		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1632		     zfs_is_mounted(zhp, NULL))) {
1633			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1634			if (cls[cl_idx] == NULL)
1635				goto error;
1636		}
1637
1638		if (prop == ZFS_PROP_MOUNTPOINT &&
1639		    changelist_haszonedchild(cls[cl_idx])) {
1640			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1641			    "child dataset with inherited mountpoint is used "
1642			    "in a non-global zone"));
1643			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1644			goto error;
1645		}
1646
1647		/* We don't support those properties on FreeBSD. */
1648		switch (prop) {
1649		case ZFS_PROP_DEVICES:
1650		case ZFS_PROP_ISCSIOPTIONS:
1651		case ZFS_PROP_XATTR:
1652		case ZFS_PROP_VSCAN:
1653		case ZFS_PROP_NBMAND:
1654		case ZFS_PROP_MLSLABEL:
1655			(void) snprintf(errbuf, sizeof (errbuf),
1656			    "property '%s' not supported on FreeBSD",
1657			    nvpair_name(elem));
1658			ret = zfs_error(hdl, EZFS_PERM, errbuf);
1659			goto error;
1660		}
1661
1662		if (cls[cl_idx] != NULL &&
1663		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1664			goto error;
1665
1666		cl_idx++;
1667	}
1668	assert(cl_idx == nvl_len);
1669
1670	/*
1671	 * Execute the corresponding ioctl() to set this list of properties.
1672	 */
1673	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1674
1675	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1676	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1677		goto error;
1678
1679	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1680
1681	if (ret != 0) {
1682		/* Get the list of unset properties back and report them. */
1683		nvlist_t *errorprops = NULL;
1684		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1685			goto error;
1686		for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1687		    elem != NULL;
1688		    elem = nvlist_next_nvpair(nvl, elem)) {
1689			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1690			zfs_setprop_error(hdl, prop, errno, errbuf);
1691		}
1692		nvlist_free(errorprops);
1693
1694		if (added_resv && errno == ENOSPC) {
1695			/* clean up the volsize property we tried to set */
1696			uint64_t old_volsize = zfs_prop_get_int(zhp,
1697			    ZFS_PROP_VOLSIZE);
1698			nvlist_free(nvl);
1699			nvl = NULL;
1700			zcmd_free_nvlists(&zc);
1701
1702			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1703				goto error;
1704			if (nvlist_add_uint64(nvl,
1705			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1706			    old_volsize) != 0)
1707				goto error;
1708			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1709				goto error;
1710			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1711		}
1712	} else {
1713		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1714			if (cls[cl_idx] != NULL) {
1715				int clp_err = changelist_postfix(cls[cl_idx]);
1716				if (clp_err != 0)
1717					ret = clp_err;
1718			}
1719		}
1720
1721		/*
1722		 * Refresh the statistics so the new property value
1723		 * is reflected.
1724		 */
1725		if (ret == 0)
1726			(void) get_stats(zhp);
1727	}
1728
1729error:
1730	nvlist_free(nvl);
1731	zcmd_free_nvlists(&zc);
1732	if (cls != NULL) {
1733		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1734			if (cls[cl_idx] != NULL)
1735				changelist_free(cls[cl_idx]);
1736		}
1737		free(cls);
1738	}
1739	return (ret);
1740}
1741
1742/*
1743 * Given a property, inherit the value from the parent dataset, or if received
1744 * is TRUE, revert to the received value, if any.
1745 */
1746int
1747zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1748{
1749	zfs_cmd_t zc = { 0 };
1750	int ret;
1751	prop_changelist_t *cl;
1752	libzfs_handle_t *hdl = zhp->zfs_hdl;
1753	char errbuf[1024];
1754	zfs_prop_t prop;
1755
1756	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1757	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1758
1759	zc.zc_cookie = received;
1760	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1761		/*
1762		 * For user properties, the amount of work we have to do is very
1763		 * small, so just do it here.
1764		 */
1765		if (!zfs_prop_user(propname)) {
1766			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1767			    "invalid property"));
1768			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1769		}
1770
1771		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1772		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1773
1774		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1775			return (zfs_standard_error(hdl, errno, errbuf));
1776
1777		return (0);
1778	}
1779
1780	/*
1781	 * Verify that this property is inheritable.
1782	 */
1783	if (zfs_prop_readonly(prop))
1784		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1785
1786	if (!zfs_prop_inheritable(prop) && !received)
1787		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1788
1789	/*
1790	 * Check to see if the value applies to this type
1791	 */
1792	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1793		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1794
1795	/*
1796	 * Normalize the name, to get rid of shorthand abbreviations.
1797	 */
1798	propname = zfs_prop_to_name(prop);
1799	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1800	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1801
1802	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1803	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1804		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1805		    "dataset is used in a non-global zone"));
1806		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1807	}
1808
1809	/*
1810	 * Determine datasets which will be affected by this change, if any.
1811	 */
1812	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1813		return (-1);
1814
1815	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1816		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1817		    "child dataset with inherited mountpoint is used "
1818		    "in a non-global zone"));
1819		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1820		goto error;
1821	}
1822
1823	if ((ret = changelist_prefix(cl)) != 0)
1824		goto error;
1825
1826	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1827		return (zfs_standard_error(hdl, errno, errbuf));
1828	} else {
1829
1830		if ((ret = changelist_postfix(cl)) != 0)
1831			goto error;
1832
1833		/*
1834		 * Refresh the statistics so the new property is reflected.
1835		 */
1836		(void) get_stats(zhp);
1837	}
1838
1839error:
1840	changelist_free(cl);
1841	return (ret);
1842}
1843
1844/*
1845 * True DSL properties are stored in an nvlist.  The following two functions
1846 * extract them appropriately.
1847 */
1848static uint64_t
1849getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1850{
1851	nvlist_t *nv;
1852	uint64_t value;
1853
1854	*source = NULL;
1855	if (nvlist_lookup_nvlist(zhp->zfs_props,
1856	    zfs_prop_to_name(prop), &nv) == 0) {
1857		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1858		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1859	} else {
1860		verify(!zhp->zfs_props_table ||
1861		    zhp->zfs_props_table[prop] == B_TRUE);
1862		value = zfs_prop_default_numeric(prop);
1863		*source = "";
1864	}
1865
1866	return (value);
1867}
1868
1869static const char *
1870getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1871{
1872	nvlist_t *nv;
1873	const char *value;
1874
1875	*source = NULL;
1876	if (nvlist_lookup_nvlist(zhp->zfs_props,
1877	    zfs_prop_to_name(prop), &nv) == 0) {
1878		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1879		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1880	} else {
1881		verify(!zhp->zfs_props_table ||
1882		    zhp->zfs_props_table[prop] == B_TRUE);
1883		value = zfs_prop_default_string(prop);
1884		*source = "";
1885	}
1886
1887	return (value);
1888}
1889
1890static boolean_t
1891zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1892{
1893	return (zhp->zfs_props == zhp->zfs_recvd_props);
1894}
1895
1896static void
1897zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1898{
1899	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1900	zhp->zfs_props = zhp->zfs_recvd_props;
1901}
1902
1903static void
1904zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1905{
1906	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1907	*cookie = 0;
1908}
1909
1910/*
1911 * Internal function for getting a numeric property.  Both zfs_prop_get() and
1912 * zfs_prop_get_int() are built using this interface.
1913 *
1914 * Certain properties can be overridden using 'mount -o'.  In this case, scan
1915 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1916 * If they differ from the on-disk values, report the current values and mark
1917 * the source "temporary".
1918 */
1919static int
1920get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1921    char **source, uint64_t *val)
1922{
1923	zfs_cmd_t zc = { 0 };
1924	nvlist_t *zplprops = NULL;
1925	struct mnttab mnt;
1926	char *mntopt_on = NULL;
1927	char *mntopt_off = NULL;
1928	boolean_t received = zfs_is_recvd_props_mode(zhp);
1929
1930	*source = NULL;
1931
1932	switch (prop) {
1933	case ZFS_PROP_ATIME:
1934		mntopt_on = MNTOPT_ATIME;
1935		mntopt_off = MNTOPT_NOATIME;
1936		break;
1937
1938	case ZFS_PROP_DEVICES:
1939		mntopt_on = MNTOPT_DEVICES;
1940		mntopt_off = MNTOPT_NODEVICES;
1941		break;
1942
1943	case ZFS_PROP_EXEC:
1944		mntopt_on = MNTOPT_EXEC;
1945		mntopt_off = MNTOPT_NOEXEC;
1946		break;
1947
1948	case ZFS_PROP_READONLY:
1949		mntopt_on = MNTOPT_RO;
1950		mntopt_off = MNTOPT_RW;
1951		break;
1952
1953	case ZFS_PROP_SETUID:
1954		mntopt_on = MNTOPT_SETUID;
1955		mntopt_off = MNTOPT_NOSETUID;
1956		break;
1957
1958	case ZFS_PROP_XATTR:
1959		mntopt_on = MNTOPT_XATTR;
1960		mntopt_off = MNTOPT_NOXATTR;
1961		break;
1962
1963	case ZFS_PROP_NBMAND:
1964		mntopt_on = MNTOPT_NBMAND;
1965		mntopt_off = MNTOPT_NONBMAND;
1966		break;
1967	}
1968
1969	/*
1970	 * Because looking up the mount options is potentially expensive
1971	 * (iterating over all of /etc/mnttab), we defer its calculation until
1972	 * we're looking up a property which requires its presence.
1973	 */
1974	if (!zhp->zfs_mntcheck &&
1975	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1976		libzfs_handle_t *hdl = zhp->zfs_hdl;
1977		struct mnttab entry;
1978
1979		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1980			zhp->zfs_mntopts = zfs_strdup(hdl,
1981			    entry.mnt_mntopts);
1982			if (zhp->zfs_mntopts == NULL)
1983				return (-1);
1984		}
1985
1986		zhp->zfs_mntcheck = B_TRUE;
1987	}
1988
1989	if (zhp->zfs_mntopts == NULL)
1990		mnt.mnt_mntopts = "";
1991	else
1992		mnt.mnt_mntopts = zhp->zfs_mntopts;
1993
1994	switch (prop) {
1995	case ZFS_PROP_ATIME:
1996	case ZFS_PROP_DEVICES:
1997	case ZFS_PROP_EXEC:
1998	case ZFS_PROP_READONLY:
1999	case ZFS_PROP_SETUID:
2000	case ZFS_PROP_XATTR:
2001	case ZFS_PROP_NBMAND:
2002		*val = getprop_uint64(zhp, prop, source);
2003
2004		if (received)
2005			break;
2006
2007		if (hasmntopt(&mnt, mntopt_on) && !*val) {
2008			*val = B_TRUE;
2009			if (src)
2010				*src = ZPROP_SRC_TEMPORARY;
2011		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
2012			*val = B_FALSE;
2013			if (src)
2014				*src = ZPROP_SRC_TEMPORARY;
2015		}
2016		break;
2017
2018	case ZFS_PROP_CANMOUNT:
2019	case ZFS_PROP_VOLSIZE:
2020	case ZFS_PROP_QUOTA:
2021	case ZFS_PROP_REFQUOTA:
2022	case ZFS_PROP_RESERVATION:
2023	case ZFS_PROP_REFRESERVATION:
2024	case ZFS_PROP_FILESYSTEM_LIMIT:
2025	case ZFS_PROP_SNAPSHOT_LIMIT:
2026	case ZFS_PROP_FILESYSTEM_COUNT:
2027	case ZFS_PROP_SNAPSHOT_COUNT:
2028		*val = getprop_uint64(zhp, prop, source);
2029
2030		if (*source == NULL) {
2031			/* not default, must be local */
2032			*source = zhp->zfs_name;
2033		}
2034		break;
2035
2036	case ZFS_PROP_MOUNTED:
2037		*val = (zhp->zfs_mntopts != NULL);
2038		break;
2039
2040	case ZFS_PROP_NUMCLONES:
2041		*val = zhp->zfs_dmustats.dds_num_clones;
2042		break;
2043
2044	case ZFS_PROP_VERSION:
2045	case ZFS_PROP_NORMALIZE:
2046	case ZFS_PROP_UTF8ONLY:
2047	case ZFS_PROP_CASE:
2048		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2049		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2050			return (-1);
2051		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2052		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2053			zcmd_free_nvlists(&zc);
2054			return (-1);
2055		}
2056		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2057		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2058		    val) != 0) {
2059			zcmd_free_nvlists(&zc);
2060			return (-1);
2061		}
2062		nvlist_free(zplprops);
2063		zcmd_free_nvlists(&zc);
2064		break;
2065
2066	case ZFS_PROP_INCONSISTENT:
2067		*val = zhp->zfs_dmustats.dds_inconsistent;
2068		break;
2069
2070	default:
2071		switch (zfs_prop_get_type(prop)) {
2072		case PROP_TYPE_NUMBER:
2073		case PROP_TYPE_INDEX:
2074			*val = getprop_uint64(zhp, prop, source);
2075			/*
2076			 * If we tried to use a default value for a
2077			 * readonly property, it means that it was not
2078			 * present.
2079			 */
2080			if (zfs_prop_readonly(prop) &&
2081			    *source != NULL && (*source)[0] == '\0') {
2082				*source = NULL;
2083			}
2084			break;
2085
2086		case PROP_TYPE_STRING:
2087		default:
2088			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2089			    "cannot get non-numeric property"));
2090			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2091			    dgettext(TEXT_DOMAIN, "internal error")));
2092		}
2093	}
2094
2095	return (0);
2096}
2097
2098/*
2099 * Calculate the source type, given the raw source string.
2100 */
2101static void
2102get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2103    char *statbuf, size_t statlen)
2104{
2105	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2106		return;
2107
2108	if (source == NULL) {
2109		*srctype = ZPROP_SRC_NONE;
2110	} else if (source[0] == '\0') {
2111		*srctype = ZPROP_SRC_DEFAULT;
2112	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2113		*srctype = ZPROP_SRC_RECEIVED;
2114	} else {
2115		if (strcmp(source, zhp->zfs_name) == 0) {
2116			*srctype = ZPROP_SRC_LOCAL;
2117		} else {
2118			(void) strlcpy(statbuf, source, statlen);
2119			*srctype = ZPROP_SRC_INHERITED;
2120		}
2121	}
2122
2123}
2124
2125int
2126zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2127    size_t proplen, boolean_t literal)
2128{
2129	zfs_prop_t prop;
2130	int err = 0;
2131
2132	if (zhp->zfs_recvd_props == NULL)
2133		if (get_recvd_props_ioctl(zhp) != 0)
2134			return (-1);
2135
2136	prop = zfs_name_to_prop(propname);
2137
2138	if (prop != ZPROP_INVAL) {
2139		uint64_t cookie;
2140		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2141			return (-1);
2142		zfs_set_recvd_props_mode(zhp, &cookie);
2143		err = zfs_prop_get(zhp, prop, propbuf, proplen,
2144		    NULL, NULL, 0, literal);
2145		zfs_unset_recvd_props_mode(zhp, &cookie);
2146	} else {
2147		nvlist_t *propval;
2148		char *recvdval;
2149		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2150		    propname, &propval) != 0)
2151			return (-1);
2152		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2153		    &recvdval) == 0);
2154		(void) strlcpy(propbuf, recvdval, proplen);
2155	}
2156
2157	return (err == 0 ? 0 : -1);
2158}
2159
2160static int
2161get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2162{
2163	nvlist_t *value;
2164	nvpair_t *pair;
2165
2166	value = zfs_get_clones_nvl(zhp);
2167	if (value == NULL)
2168		return (-1);
2169
2170	propbuf[0] = '\0';
2171	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2172	    pair = nvlist_next_nvpair(value, pair)) {
2173		if (propbuf[0] != '\0')
2174			(void) strlcat(propbuf, ",", proplen);
2175		(void) strlcat(propbuf, nvpair_name(pair), proplen);
2176	}
2177
2178	return (0);
2179}
2180
2181struct get_clones_arg {
2182	uint64_t numclones;
2183	nvlist_t *value;
2184	const char *origin;
2185	char buf[ZFS_MAXNAMELEN];
2186};
2187
2188int
2189get_clones_cb(zfs_handle_t *zhp, void *arg)
2190{
2191	struct get_clones_arg *gca = arg;
2192
2193	if (gca->numclones == 0) {
2194		zfs_close(zhp);
2195		return (0);
2196	}
2197
2198	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2199	    NULL, NULL, 0, B_TRUE) != 0)
2200		goto out;
2201	if (strcmp(gca->buf, gca->origin) == 0) {
2202		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2203		gca->numclones--;
2204	}
2205
2206out:
2207	(void) zfs_iter_children(zhp, get_clones_cb, gca);
2208	zfs_close(zhp);
2209	return (0);
2210}
2211
2212nvlist_t *
2213zfs_get_clones_nvl(zfs_handle_t *zhp)
2214{
2215	nvlist_t *nv, *value;
2216
2217	if (nvlist_lookup_nvlist(zhp->zfs_props,
2218	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2219		struct get_clones_arg gca;
2220
2221		/*
2222		 * if this is a snapshot, then the kernel wasn't able
2223		 * to get the clones.  Do it by slowly iterating.
2224		 */
2225		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2226			return (NULL);
2227		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2228			return (NULL);
2229		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2230			nvlist_free(nv);
2231			return (NULL);
2232		}
2233
2234		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2235		gca.value = value;
2236		gca.origin = zhp->zfs_name;
2237
2238		if (gca.numclones != 0) {
2239			zfs_handle_t *root;
2240			char pool[ZFS_MAXNAMELEN];
2241			char *cp = pool;
2242
2243			/* get the pool name */
2244			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2245			(void) strsep(&cp, "/@");
2246			root = zfs_open(zhp->zfs_hdl, pool,
2247			    ZFS_TYPE_FILESYSTEM);
2248
2249			(void) get_clones_cb(root, &gca);
2250		}
2251
2252		if (gca.numclones != 0 ||
2253		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2254		    nvlist_add_nvlist(zhp->zfs_props,
2255		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2256			nvlist_free(nv);
2257			nvlist_free(value);
2258			return (NULL);
2259		}
2260		nvlist_free(nv);
2261		nvlist_free(value);
2262		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2263		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2264	}
2265
2266	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2267
2268	return (value);
2269}
2270
2271/*
2272 * Retrieve a property from the given object.  If 'literal' is specified, then
2273 * numbers are left as exact values.  Otherwise, numbers are converted to a
2274 * human-readable form.
2275 *
2276 * Returns 0 on success, or -1 on error.
2277 */
2278int
2279zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2280    zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2281{
2282	char *source = NULL;
2283	uint64_t val;
2284	const char *str;
2285	const char *strval;
2286	boolean_t received = zfs_is_recvd_props_mode(zhp);
2287
2288	/*
2289	 * Check to see if this property applies to our object
2290	 */
2291	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2292		return (-1);
2293
2294	if (received && zfs_prop_readonly(prop))
2295		return (-1);
2296
2297	if (src)
2298		*src = ZPROP_SRC_NONE;
2299
2300	switch (prop) {
2301	case ZFS_PROP_CREATION:
2302		/*
2303		 * 'creation' is a time_t stored in the statistics.  We convert
2304		 * this into a string unless 'literal' is specified.
2305		 */
2306		{
2307			val = getprop_uint64(zhp, prop, &source);
2308			time_t time = (time_t)val;
2309			struct tm t;
2310
2311			if (literal ||
2312			    localtime_r(&time, &t) == NULL ||
2313			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2314			    &t) == 0)
2315				(void) snprintf(propbuf, proplen, "%llu", val);
2316		}
2317		break;
2318
2319	case ZFS_PROP_MOUNTPOINT:
2320		/*
2321		 * Getting the precise mountpoint can be tricky.
2322		 *
2323		 *  - for 'none' or 'legacy', return those values.
2324		 *  - for inherited mountpoints, we want to take everything
2325		 *    after our ancestor and append it to the inherited value.
2326		 *
2327		 * If the pool has an alternate root, we want to prepend that
2328		 * root to any values we return.
2329		 */
2330
2331		str = getprop_string(zhp, prop, &source);
2332
2333		if (str[0] == '/') {
2334			char buf[MAXPATHLEN];
2335			char *root = buf;
2336			const char *relpath;
2337
2338			/*
2339			 * If we inherit the mountpoint, even from a dataset
2340			 * with a received value, the source will be the path of
2341			 * the dataset we inherit from. If source is
2342			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2343			 * inherited.
2344			 */
2345			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2346				relpath = "";
2347			} else {
2348				relpath = zhp->zfs_name + strlen(source);
2349				if (relpath[0] == '/')
2350					relpath++;
2351			}
2352
2353			if ((zpool_get_prop(zhp->zpool_hdl,
2354			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2355			    B_FALSE)) || (strcmp(root, "-") == 0))
2356				root[0] = '\0';
2357			/*
2358			 * Special case an alternate root of '/'. This will
2359			 * avoid having multiple leading slashes in the
2360			 * mountpoint path.
2361			 */
2362			if (strcmp(root, "/") == 0)
2363				root++;
2364
2365			/*
2366			 * If the mountpoint is '/' then skip over this
2367			 * if we are obtaining either an alternate root or
2368			 * an inherited mountpoint.
2369			 */
2370			if (str[1] == '\0' && (root[0] != '\0' ||
2371			    relpath[0] != '\0'))
2372				str++;
2373
2374			if (relpath[0] == '\0')
2375				(void) snprintf(propbuf, proplen, "%s%s",
2376				    root, str);
2377			else
2378				(void) snprintf(propbuf, proplen, "%s%s%s%s",
2379				    root, str, relpath[0] == '@' ? "" : "/",
2380				    relpath);
2381		} else {
2382			/* 'legacy' or 'none' */
2383			(void) strlcpy(propbuf, str, proplen);
2384		}
2385
2386		break;
2387
2388	case ZFS_PROP_ORIGIN:
2389		str = getprop_string(zhp, prop, &source);
2390		if (str == NULL)
2391			return (-1);
2392		(void) strlcpy(propbuf, str, proplen);
2393		break;
2394
2395	case ZFS_PROP_CLONES:
2396		if (get_clones_string(zhp, propbuf, proplen) != 0)
2397			return (-1);
2398		break;
2399
2400	case ZFS_PROP_QUOTA:
2401	case ZFS_PROP_REFQUOTA:
2402	case ZFS_PROP_RESERVATION:
2403	case ZFS_PROP_REFRESERVATION:
2404
2405		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2406			return (-1);
2407
2408		/*
2409		 * If quota or reservation is 0, we translate this into 'none'
2410		 * (unless literal is set), and indicate that it's the default
2411		 * value.  Otherwise, we print the number nicely and indicate
2412		 * that its set locally.
2413		 */
2414		if (val == 0) {
2415			if (literal)
2416				(void) strlcpy(propbuf, "0", proplen);
2417			else
2418				(void) strlcpy(propbuf, "none", proplen);
2419		} else {
2420			if (literal)
2421				(void) snprintf(propbuf, proplen, "%llu",
2422				    (u_longlong_t)val);
2423			else
2424				zfs_nicenum(val, propbuf, proplen);
2425		}
2426		break;
2427
2428	case ZFS_PROP_FILESYSTEM_LIMIT:
2429	case ZFS_PROP_SNAPSHOT_LIMIT:
2430	case ZFS_PROP_FILESYSTEM_COUNT:
2431	case ZFS_PROP_SNAPSHOT_COUNT:
2432
2433		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2434			return (-1);
2435
2436		/*
2437		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2438		 * literal is set), and indicate that it's the default value.
2439		 * Otherwise, we print the number nicely and indicate that it's
2440		 * set locally.
2441		 */
2442		if (literal) {
2443			(void) snprintf(propbuf, proplen, "%llu",
2444			    (u_longlong_t)val);
2445		} else if (val == UINT64_MAX) {
2446			(void) strlcpy(propbuf, "none", proplen);
2447		} else {
2448			zfs_nicenum(val, propbuf, proplen);
2449		}
2450		break;
2451
2452	case ZFS_PROP_REFRATIO:
2453	case ZFS_PROP_COMPRESSRATIO:
2454		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2455			return (-1);
2456		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2457		    (u_longlong_t)(val / 100),
2458		    (u_longlong_t)(val % 100));
2459		break;
2460
2461	case ZFS_PROP_TYPE:
2462		switch (zhp->zfs_type) {
2463		case ZFS_TYPE_FILESYSTEM:
2464			str = "filesystem";
2465			break;
2466		case ZFS_TYPE_VOLUME:
2467			str = "volume";
2468			break;
2469		case ZFS_TYPE_SNAPSHOT:
2470			str = "snapshot";
2471			break;
2472		case ZFS_TYPE_BOOKMARK:
2473			str = "bookmark";
2474			break;
2475		default:
2476			abort();
2477		}
2478		(void) snprintf(propbuf, proplen, "%s", str);
2479		break;
2480
2481	case ZFS_PROP_MOUNTED:
2482		/*
2483		 * The 'mounted' property is a pseudo-property that described
2484		 * whether the filesystem is currently mounted.  Even though
2485		 * it's a boolean value, the typical values of "on" and "off"
2486		 * don't make sense, so we translate to "yes" and "no".
2487		 */
2488		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2489		    src, &source, &val) != 0)
2490			return (-1);
2491		if (val)
2492			(void) strlcpy(propbuf, "yes", proplen);
2493		else
2494			(void) strlcpy(propbuf, "no", proplen);
2495		break;
2496
2497	case ZFS_PROP_NAME:
2498		/*
2499		 * The 'name' property is a pseudo-property derived from the
2500		 * dataset name.  It is presented as a real property to simplify
2501		 * consumers.
2502		 */
2503		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2504		break;
2505
2506	case ZFS_PROP_MLSLABEL:
2507		{
2508#ifdef illumos
2509			m_label_t *new_sl = NULL;
2510			char *ascii = NULL;	/* human readable label */
2511
2512			(void) strlcpy(propbuf,
2513			    getprop_string(zhp, prop, &source), proplen);
2514
2515			if (literal || (strcasecmp(propbuf,
2516			    ZFS_MLSLABEL_DEFAULT) == 0))
2517				break;
2518
2519			/*
2520			 * Try to translate the internal hex string to
2521			 * human-readable output.  If there are any
2522			 * problems just use the hex string.
2523			 */
2524
2525			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2526			    L_NO_CORRECTION, NULL) == -1) {
2527				m_label_free(new_sl);
2528				break;
2529			}
2530
2531			if (label_to_str(new_sl, &ascii, M_LABEL,
2532			    DEF_NAMES) != 0) {
2533				if (ascii)
2534					free(ascii);
2535				m_label_free(new_sl);
2536				break;
2537			}
2538			m_label_free(new_sl);
2539
2540			(void) strlcpy(propbuf, ascii, proplen);
2541			free(ascii);
2542#else	/* !illumos */
2543			propbuf[0] = '\0';
2544#endif	/* illumos */
2545		}
2546		break;
2547
2548	case ZFS_PROP_GUID:
2549		/*
2550		 * GUIDs are stored as numbers, but they are identifiers.
2551		 * We don't want them to be pretty printed, because pretty
2552		 * printing mangles the ID into a truncated and useless value.
2553		 */
2554		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2555			return (-1);
2556		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2557		break;
2558
2559	default:
2560		switch (zfs_prop_get_type(prop)) {
2561		case PROP_TYPE_NUMBER:
2562			if (get_numeric_property(zhp, prop, src,
2563			    &source, &val) != 0)
2564				return (-1);
2565			if (literal)
2566				(void) snprintf(propbuf, proplen, "%llu",
2567				    (u_longlong_t)val);
2568			else
2569				zfs_nicenum(val, propbuf, proplen);
2570			break;
2571
2572		case PROP_TYPE_STRING:
2573			str = getprop_string(zhp, prop, &source);
2574			if (str == NULL)
2575				return (-1);
2576			(void) strlcpy(propbuf, str, proplen);
2577			break;
2578
2579		case PROP_TYPE_INDEX:
2580			if (get_numeric_property(zhp, prop, src,
2581			    &source, &val) != 0)
2582				return (-1);
2583			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2584				return (-1);
2585			(void) strlcpy(propbuf, strval, proplen);
2586			break;
2587
2588		default:
2589			abort();
2590		}
2591	}
2592
2593	get_source(zhp, src, source, statbuf, statlen);
2594
2595	return (0);
2596}
2597
2598/*
2599 * Utility function to get the given numeric property.  Does no validation that
2600 * the given property is the appropriate type; should only be used with
2601 * hard-coded property types.
2602 */
2603uint64_t
2604zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2605{
2606	char *source;
2607	uint64_t val;
2608
2609	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2610
2611	return (val);
2612}
2613
2614int
2615zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2616{
2617	char buf[64];
2618
2619	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2620	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2621}
2622
2623/*
2624 * Similar to zfs_prop_get(), but returns the value as an integer.
2625 */
2626int
2627zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2628    zprop_source_t *src, char *statbuf, size_t statlen)
2629{
2630	char *source;
2631
2632	/*
2633	 * Check to see if this property applies to our object
2634	 */
2635	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2636		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2637		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2638		    zfs_prop_to_name(prop)));
2639	}
2640
2641	if (src)
2642		*src = ZPROP_SRC_NONE;
2643
2644	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2645		return (-1);
2646
2647	get_source(zhp, src, source, statbuf, statlen);
2648
2649	return (0);
2650}
2651
2652static int
2653idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2654    char **domainp, idmap_rid_t *ridp)
2655{
2656#ifdef illumos
2657	idmap_get_handle_t *get_hdl = NULL;
2658	idmap_stat status;
2659	int err = EINVAL;
2660
2661	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2662		goto out;
2663
2664	if (isuser) {
2665		err = idmap_get_sidbyuid(get_hdl, id,
2666		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2667	} else {
2668		err = idmap_get_sidbygid(get_hdl, id,
2669		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2670	}
2671	if (err == IDMAP_SUCCESS &&
2672	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2673	    status == IDMAP_SUCCESS)
2674		err = 0;
2675	else
2676		err = EINVAL;
2677out:
2678	if (get_hdl)
2679		idmap_get_destroy(get_hdl);
2680	return (err);
2681#else	/* !illumos */
2682	assert(!"invalid code path");
2683	return (EINVAL); // silence compiler warning
2684#endif	/* illumos */
2685}
2686
2687/*
2688 * convert the propname into parameters needed by kernel
2689 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2690 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2691 */
2692static int
2693userquota_propname_decode(const char *propname, boolean_t zoned,
2694    zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2695{
2696	zfs_userquota_prop_t type;
2697	char *cp, *end;
2698	char *numericsid = NULL;
2699	boolean_t isuser;
2700
2701	domain[0] = '\0';
2702	*ridp = 0;
2703	/* Figure out the property type ({user|group}{quota|space}) */
2704	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2705		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2706		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
2707			break;
2708	}
2709	if (type == ZFS_NUM_USERQUOTA_PROPS)
2710		return (EINVAL);
2711	*typep = type;
2712
2713	isuser = (type == ZFS_PROP_USERQUOTA ||
2714	    type == ZFS_PROP_USERUSED);
2715
2716	cp = strchr(propname, '@') + 1;
2717
2718	if (strchr(cp, '@')) {
2719#ifdef illumos
2720		/*
2721		 * It's a SID name (eg "user@domain") that needs to be
2722		 * turned into S-1-domainID-RID.
2723		 */
2724		int flag = 0;
2725		idmap_stat stat, map_stat;
2726		uid_t pid;
2727		idmap_rid_t rid;
2728		idmap_get_handle_t *gh = NULL;
2729
2730		stat = idmap_get_create(&gh);
2731		if (stat != IDMAP_SUCCESS) {
2732			idmap_get_destroy(gh);
2733			return (ENOMEM);
2734		}
2735		if (zoned && getzoneid() == GLOBAL_ZONEID)
2736			return (ENOENT);
2737		if (isuser) {
2738			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2739			if (stat < 0)
2740				return (ENOENT);
2741			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2742			    &rid, &map_stat);
2743		} else {
2744			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2745			if (stat < 0)
2746				return (ENOENT);
2747			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2748			    &rid, &map_stat);
2749		}
2750		if (stat < 0) {
2751			idmap_get_destroy(gh);
2752			return (ENOENT);
2753		}
2754		stat = idmap_get_mappings(gh);
2755		idmap_get_destroy(gh);
2756
2757		if (stat < 0) {
2758			return (ENOENT);
2759		}
2760		if (numericsid == NULL)
2761			return (ENOENT);
2762		cp = numericsid;
2763		*ridp = rid;
2764		/* will be further decoded below */
2765#else	/* !illumos */
2766		return (ENOENT);
2767#endif	/* illumos */
2768	}
2769
2770	if (strncmp(cp, "S-1-", 4) == 0) {
2771		/* It's a numeric SID (eg "S-1-234-567-89") */
2772		(void) strlcpy(domain, cp, domainlen);
2773		errno = 0;
2774		if (*ridp == 0) {
2775			cp = strrchr(domain, '-');
2776			*cp = '\0';
2777			cp++;
2778			*ridp = strtoull(cp, &end, 10);
2779		} else {
2780			end = "";
2781		}
2782		if (numericsid) {
2783			free(numericsid);
2784			numericsid = NULL;
2785		}
2786		if (errno != 0 || *end != '\0')
2787			return (EINVAL);
2788	} else if (!isdigit(*cp)) {
2789		/*
2790		 * It's a user/group name (eg "user") that needs to be
2791		 * turned into a uid/gid
2792		 */
2793		if (zoned && getzoneid() == GLOBAL_ZONEID)
2794			return (ENOENT);
2795		if (isuser) {
2796			struct passwd *pw;
2797			pw = getpwnam(cp);
2798			if (pw == NULL)
2799				return (ENOENT);
2800			*ridp = pw->pw_uid;
2801		} else {
2802			struct group *gr;
2803			gr = getgrnam(cp);
2804			if (gr == NULL)
2805				return (ENOENT);
2806			*ridp = gr->gr_gid;
2807		}
2808	} else {
2809		/* It's a user/group ID (eg "12345"). */
2810		uid_t id = strtoul(cp, &end, 10);
2811		idmap_rid_t rid;
2812		char *mapdomain;
2813
2814		if (*end != '\0')
2815			return (EINVAL);
2816		if (id > MAXUID) {
2817			/* It's an ephemeral ID. */
2818			if (idmap_id_to_numeric_domain_rid(id, isuser,
2819			    &mapdomain, &rid) != 0)
2820				return (ENOENT);
2821			(void) strlcpy(domain, mapdomain, domainlen);
2822			*ridp = rid;
2823		} else {
2824			*ridp = id;
2825		}
2826	}
2827
2828	ASSERT3P(numericsid, ==, NULL);
2829	return (0);
2830}
2831
2832static int
2833zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2834    uint64_t *propvalue, zfs_userquota_prop_t *typep)
2835{
2836	int err;
2837	zfs_cmd_t zc = { 0 };
2838
2839	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2840
2841	err = userquota_propname_decode(propname,
2842	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2843	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2844	zc.zc_objset_type = *typep;
2845	if (err)
2846		return (err);
2847
2848	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2849	if (err)
2850		return (err);
2851
2852	*propvalue = zc.zc_cookie;
2853	return (0);
2854}
2855
2856int
2857zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2858    uint64_t *propvalue)
2859{
2860	zfs_userquota_prop_t type;
2861
2862	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2863	    &type));
2864}
2865
2866int
2867zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2868    char *propbuf, int proplen, boolean_t literal)
2869{
2870	int err;
2871	uint64_t propvalue;
2872	zfs_userquota_prop_t type;
2873
2874	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2875	    &type);
2876
2877	if (err)
2878		return (err);
2879
2880	if (literal) {
2881		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2882	} else if (propvalue == 0 &&
2883	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2884		(void) strlcpy(propbuf, "none", proplen);
2885	} else {
2886		zfs_nicenum(propvalue, propbuf, proplen);
2887	}
2888	return (0);
2889}
2890
2891int
2892zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2893    uint64_t *propvalue)
2894{
2895	int err;
2896	zfs_cmd_t zc = { 0 };
2897	const char *snapname;
2898
2899	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2900
2901	snapname = strchr(propname, '@') + 1;
2902	if (strchr(snapname, '@')) {
2903		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2904	} else {
2905		/* snapname is the short name, append it to zhp's fsname */
2906		char *cp;
2907
2908		(void) strlcpy(zc.zc_value, zhp->zfs_name,
2909		    sizeof (zc.zc_value));
2910		cp = strchr(zc.zc_value, '@');
2911		if (cp != NULL)
2912			*cp = '\0';
2913		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2914		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2915	}
2916
2917	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2918	if (err)
2919		return (err);
2920
2921	*propvalue = zc.zc_cookie;
2922	return (0);
2923}
2924
2925int
2926zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2927    char *propbuf, int proplen, boolean_t literal)
2928{
2929	int err;
2930	uint64_t propvalue;
2931
2932	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2933
2934	if (err)
2935		return (err);
2936
2937	if (literal) {
2938		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2939	} else {
2940		zfs_nicenum(propvalue, propbuf, proplen);
2941	}
2942	return (0);
2943}
2944
2945/*
2946 * Returns the name of the given zfs handle.
2947 */
2948const char *
2949zfs_get_name(const zfs_handle_t *zhp)
2950{
2951	return (zhp->zfs_name);
2952}
2953
2954/*
2955 * Returns the type of the given zfs handle.
2956 */
2957zfs_type_t
2958zfs_get_type(const zfs_handle_t *zhp)
2959{
2960	return (zhp->zfs_type);
2961}
2962
2963/*
2964 * Is one dataset name a child dataset of another?
2965 *
2966 * Needs to handle these cases:
2967 * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2968 * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2969 * Descendant?	No.		No.		No.		Yes.
2970 */
2971static boolean_t
2972is_descendant(const char *ds1, const char *ds2)
2973{
2974	size_t d1len = strlen(ds1);
2975
2976	/* ds2 can't be a descendant if it's smaller */
2977	if (strlen(ds2) < d1len)
2978		return (B_FALSE);
2979
2980	/* otherwise, compare strings and verify that there's a '/' char */
2981	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2982}
2983
2984/*
2985 * Given a complete name, return just the portion that refers to the parent.
2986 * Will return -1 if there is no parent (path is just the name of the
2987 * pool).
2988 */
2989static int
2990parent_name(const char *path, char *buf, size_t buflen)
2991{
2992	char *slashp;
2993
2994	(void) strlcpy(buf, path, buflen);
2995
2996	if ((slashp = strrchr(buf, '/')) == NULL)
2997		return (-1);
2998	*slashp = '\0';
2999
3000	return (0);
3001}
3002
3003/*
3004 * If accept_ancestor is false, then check to make sure that the given path has
3005 * a parent, and that it exists.  If accept_ancestor is true, then find the
3006 * closest existing ancestor for the given path.  In prefixlen return the
3007 * length of already existing prefix of the given path.  We also fetch the
3008 * 'zoned' property, which is used to validate property settings when creating
3009 * new datasets.
3010 */
3011static int
3012check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3013    boolean_t accept_ancestor, int *prefixlen)
3014{
3015	zfs_cmd_t zc = { 0 };
3016	char parent[ZFS_MAXNAMELEN];
3017	char *slash;
3018	zfs_handle_t *zhp;
3019	char errbuf[1024];
3020	uint64_t is_zoned;
3021
3022	(void) snprintf(errbuf, sizeof (errbuf),
3023	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3024
3025	/* get parent, and check to see if this is just a pool */
3026	if (parent_name(path, parent, sizeof (parent)) != 0) {
3027		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3028		    "missing dataset name"));
3029		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3030	}
3031
3032	/* check to see if the pool exists */
3033	if ((slash = strchr(parent, '/')) == NULL)
3034		slash = parent + strlen(parent);
3035	(void) strncpy(zc.zc_name, parent, slash - parent);
3036	zc.zc_name[slash - parent] = '\0';
3037	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3038	    errno == ENOENT) {
3039		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3040		    "no such pool '%s'"), zc.zc_name);
3041		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3042	}
3043
3044	/* check to see if the parent dataset exists */
3045	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3046		if (errno == ENOENT && accept_ancestor) {
3047			/*
3048			 * Go deeper to find an ancestor, give up on top level.
3049			 */
3050			if (parent_name(parent, parent, sizeof (parent)) != 0) {
3051				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3052				    "no such pool '%s'"), zc.zc_name);
3053				return (zfs_error(hdl, EZFS_NOENT, errbuf));
3054			}
3055		} else if (errno == ENOENT) {
3056			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3057			    "parent does not exist"));
3058			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3059		} else
3060			return (zfs_standard_error(hdl, errno, errbuf));
3061	}
3062
3063	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3064	if (zoned != NULL)
3065		*zoned = is_zoned;
3066
3067	/* we are in a non-global zone, but parent is in the global zone */
3068	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3069		(void) zfs_standard_error(hdl, EPERM, errbuf);
3070		zfs_close(zhp);
3071		return (-1);
3072	}
3073
3074	/* make sure parent is a filesystem */
3075	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3076		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3077		    "parent is not a filesystem"));
3078		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3079		zfs_close(zhp);
3080		return (-1);
3081	}
3082
3083	zfs_close(zhp);
3084	if (prefixlen != NULL)
3085		*prefixlen = strlen(parent);
3086	return (0);
3087}
3088
3089/*
3090 * Finds whether the dataset of the given type(s) exists.
3091 */
3092boolean_t
3093zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3094{
3095	zfs_handle_t *zhp;
3096
3097	if (!zfs_validate_name(hdl, path, types, B_FALSE))
3098		return (B_FALSE);
3099
3100	/*
3101	 * Try to get stats for the dataset, which will tell us if it exists.
3102	 */
3103	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3104		int ds_type = zhp->zfs_type;
3105
3106		zfs_close(zhp);
3107		if (types & ds_type)
3108			return (B_TRUE);
3109	}
3110	return (B_FALSE);
3111}
3112
3113/*
3114 * Given a path to 'target', create all the ancestors between
3115 * the prefixlen portion of the path, and the target itself.
3116 * Fail if the initial prefixlen-ancestor does not already exist.
3117 */
3118int
3119create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3120{
3121	zfs_handle_t *h;
3122	char *cp;
3123	const char *opname;
3124
3125	/* make sure prefix exists */
3126	cp = target + prefixlen;
3127	if (*cp != '/') {
3128		assert(strchr(cp, '/') == NULL);
3129		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3130	} else {
3131		*cp = '\0';
3132		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3133		*cp = '/';
3134	}
3135	if (h == NULL)
3136		return (-1);
3137	zfs_close(h);
3138
3139	/*
3140	 * Attempt to create, mount, and share any ancestor filesystems,
3141	 * up to the prefixlen-long one.
3142	 */
3143	for (cp = target + prefixlen + 1;
3144	    cp = strchr(cp, '/'); *cp = '/', cp++) {
3145
3146		*cp = '\0';
3147
3148		h = make_dataset_handle(hdl, target);
3149		if (h) {
3150			/* it already exists, nothing to do here */
3151			zfs_close(h);
3152			continue;
3153		}
3154
3155		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3156		    NULL) != 0) {
3157			opname = dgettext(TEXT_DOMAIN, "create");
3158			goto ancestorerr;
3159		}
3160
3161		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3162		if (h == NULL) {
3163			opname = dgettext(TEXT_DOMAIN, "open");
3164			goto ancestorerr;
3165		}
3166
3167		if (zfs_mount(h, NULL, 0) != 0) {
3168			opname = dgettext(TEXT_DOMAIN, "mount");
3169			goto ancestorerr;
3170		}
3171
3172		if (zfs_share(h) != 0) {
3173			opname = dgettext(TEXT_DOMAIN, "share");
3174			goto ancestorerr;
3175		}
3176
3177		zfs_close(h);
3178	}
3179
3180	return (0);
3181
3182ancestorerr:
3183	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3184	    "failed to %s ancestor '%s'"), opname, target);
3185	return (-1);
3186}
3187
3188/*
3189 * Creates non-existing ancestors of the given path.
3190 */
3191int
3192zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3193{
3194	int prefix;
3195	char *path_copy;
3196	int rc;
3197
3198	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3199		return (-1);
3200
3201	if ((path_copy = strdup(path)) != NULL) {
3202		rc = create_parents(hdl, path_copy, prefix);
3203		free(path_copy);
3204	}
3205	if (path_copy == NULL || rc != 0)
3206		return (-1);
3207
3208	return (0);
3209}
3210
3211/*
3212 * Create a new filesystem or volume.
3213 */
3214int
3215zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3216    nvlist_t *props)
3217{
3218	int ret;
3219	uint64_t size = 0;
3220	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3221	char errbuf[1024];
3222	uint64_t zoned;
3223	dmu_objset_type_t ost;
3224
3225	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3226	    "cannot create '%s'"), path);
3227
3228	/* validate the path, taking care to note the extended error message */
3229	if (!zfs_validate_name(hdl, path, type, B_TRUE))
3230		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3231
3232	/* validate parents exist */
3233	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3234		return (-1);
3235
3236	/*
3237	 * The failure modes when creating a dataset of a different type over
3238	 * one that already exists is a little strange.  In particular, if you
3239	 * try to create a dataset on top of an existing dataset, the ioctl()
3240	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3241	 * first try to see if the dataset exists.
3242	 */
3243	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3244		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3245		    "dataset already exists"));
3246		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3247	}
3248
3249	if (type == ZFS_TYPE_VOLUME)
3250		ost = DMU_OST_ZVOL;
3251	else
3252		ost = DMU_OST_ZFS;
3253
3254	/* open zpool handle for prop validation */
3255	char pool_path[MAXNAMELEN];
3256	(void) strlcpy(pool_path, path, sizeof (pool_path));
3257
3258	/* truncate pool_path at first slash */
3259	char *p = strchr(pool_path, '/');
3260	if (p != NULL)
3261		*p = '\0';
3262
3263	zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3264
3265	if (props && (props = zfs_valid_proplist(hdl, type, props,
3266	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3267		zpool_close(zpool_handle);
3268		return (-1);
3269	}
3270	zpool_close(zpool_handle);
3271
3272	if (type == ZFS_TYPE_VOLUME) {
3273		/*
3274		 * If we are creating a volume, the size and block size must
3275		 * satisfy a few restraints.  First, the blocksize must be a
3276		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3277		 * volsize must be a multiple of the block size, and cannot be
3278		 * zero.
3279		 */
3280		if (props == NULL || nvlist_lookup_uint64(props,
3281		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3282			nvlist_free(props);
3283			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3284			    "missing volume size"));
3285			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3286		}
3287
3288		if ((ret = nvlist_lookup_uint64(props,
3289		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3290		    &blocksize)) != 0) {
3291			if (ret == ENOENT) {
3292				blocksize = zfs_prop_default_numeric(
3293				    ZFS_PROP_VOLBLOCKSIZE);
3294			} else {
3295				nvlist_free(props);
3296				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3297				    "missing volume block size"));
3298				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3299			}
3300		}
3301
3302		if (size == 0) {
3303			nvlist_free(props);
3304			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3305			    "volume size cannot be zero"));
3306			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3307		}
3308
3309		if (size % blocksize != 0) {
3310			nvlist_free(props);
3311			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3312			    "volume size must be a multiple of volume block "
3313			    "size"));
3314			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3315		}
3316	}
3317
3318	/* create the dataset */
3319	ret = lzc_create(path, ost, props);
3320	nvlist_free(props);
3321
3322	/* check for failure */
3323	if (ret != 0) {
3324		char parent[ZFS_MAXNAMELEN];
3325		(void) parent_name(path, parent, sizeof (parent));
3326
3327		switch (errno) {
3328		case ENOENT:
3329			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3330			    "no such parent '%s'"), parent);
3331			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3332
3333		case EINVAL:
3334			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3335			    "parent '%s' is not a filesystem"), parent);
3336			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3337
3338		case ENOTSUP:
3339			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3340			    "pool must be upgraded to set this "
3341			    "property or value"));
3342			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3343#ifdef _ILP32
3344		case EOVERFLOW:
3345			/*
3346			 * This platform can't address a volume this big.
3347			 */
3348			if (type == ZFS_TYPE_VOLUME)
3349				return (zfs_error(hdl, EZFS_VOLTOOBIG,
3350				    errbuf));
3351#endif
3352			/* FALLTHROUGH */
3353		default:
3354			return (zfs_standard_error(hdl, errno, errbuf));
3355		}
3356	}
3357
3358	return (0);
3359}
3360
3361/*
3362 * Destroys the given dataset.  The caller must make sure that the filesystem
3363 * isn't mounted, and that there are no active dependents. If the file system
3364 * does not exist this function does nothing.
3365 */
3366int
3367zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3368{
3369	zfs_cmd_t zc = { 0 };
3370
3371	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3372		nvlist_t *nv = fnvlist_alloc();
3373		fnvlist_add_boolean(nv, zhp->zfs_name);
3374		int error = lzc_destroy_bookmarks(nv, NULL);
3375		fnvlist_free(nv);
3376		if (error != 0) {
3377			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3378			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3379			    zhp->zfs_name));
3380		}
3381		return (0);
3382	}
3383
3384	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3385
3386	if (ZFS_IS_VOLUME(zhp)) {
3387		zc.zc_objset_type = DMU_OST_ZVOL;
3388	} else {
3389		zc.zc_objset_type = DMU_OST_ZFS;
3390	}
3391
3392	zc.zc_defer_destroy = defer;
3393	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3394	    errno != ENOENT) {
3395		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3396		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3397		    zhp->zfs_name));
3398	}
3399
3400	remove_mountpoint(zhp);
3401
3402	return (0);
3403}
3404
3405struct destroydata {
3406	nvlist_t *nvl;
3407	const char *snapname;
3408};
3409
3410static int
3411zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3412{
3413	struct destroydata *dd = arg;
3414	char name[ZFS_MAXNAMELEN];
3415	int rv = 0;
3416
3417	(void) snprintf(name, sizeof (name),
3418	    "%s@%s", zhp->zfs_name, dd->snapname);
3419
3420	if (lzc_exists(name))
3421		verify(nvlist_add_boolean(dd->nvl, name) == 0);
3422
3423	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3424	zfs_close(zhp);
3425	return (rv);
3426}
3427
3428/*
3429 * Destroys all snapshots with the given name in zhp & descendants.
3430 */
3431int
3432zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3433{
3434	int ret;
3435	struct destroydata dd = { 0 };
3436
3437	dd.snapname = snapname;
3438	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3439	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3440
3441	if (nvlist_empty(dd.nvl)) {
3442		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3443		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3444		    zhp->zfs_name, snapname);
3445	} else {
3446		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3447	}
3448	nvlist_free(dd.nvl);
3449	return (ret);
3450}
3451
3452/*
3453 * Destroys all the snapshots named in the nvlist.
3454 */
3455int
3456zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3457{
3458	int ret;
3459	nvlist_t *errlist;
3460
3461	ret = lzc_destroy_snaps(snaps, defer, &errlist);
3462
3463	if (ret == 0)
3464		return (0);
3465
3466	if (nvlist_empty(errlist)) {
3467		char errbuf[1024];
3468		(void) snprintf(errbuf, sizeof (errbuf),
3469		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3470
3471		ret = zfs_standard_error(hdl, ret, errbuf);
3472	}
3473	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3474	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3475		char errbuf[1024];
3476		(void) snprintf(errbuf, sizeof (errbuf),
3477		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3478		    nvpair_name(pair));
3479
3480		switch (fnvpair_value_int32(pair)) {
3481		case EEXIST:
3482			zfs_error_aux(hdl,
3483			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3484			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3485			break;
3486		default:
3487			ret = zfs_standard_error(hdl, errno, errbuf);
3488			break;
3489		}
3490	}
3491
3492	return (ret);
3493}
3494
3495/*
3496 * Clones the given dataset.  The target must be of the same type as the source.
3497 */
3498int
3499zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3500{
3501	char parent[ZFS_MAXNAMELEN];
3502	int ret;
3503	char errbuf[1024];
3504	libzfs_handle_t *hdl = zhp->zfs_hdl;
3505	uint64_t zoned;
3506
3507	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3508
3509	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3510	    "cannot create '%s'"), target);
3511
3512	/* validate the target/clone name */
3513	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3514		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3515
3516	/* validate parents exist */
3517	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3518		return (-1);
3519
3520	(void) parent_name(target, parent, sizeof (parent));
3521
3522	/* do the clone */
3523
3524	if (props) {
3525		zfs_type_t type;
3526		if (ZFS_IS_VOLUME(zhp)) {
3527			type = ZFS_TYPE_VOLUME;
3528		} else {
3529			type = ZFS_TYPE_FILESYSTEM;
3530		}
3531		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3532		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3533			return (-1);
3534	}
3535
3536	ret = lzc_clone(target, zhp->zfs_name, props);
3537	nvlist_free(props);
3538
3539	if (ret != 0) {
3540		switch (errno) {
3541
3542		case ENOENT:
3543			/*
3544			 * The parent doesn't exist.  We should have caught this
3545			 * above, but there may a race condition that has since
3546			 * destroyed the parent.
3547			 *
3548			 * At this point, we don't know whether it's the source
3549			 * that doesn't exist anymore, or whether the target
3550			 * dataset doesn't exist.
3551			 */
3552			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3553			    "no such parent '%s'"), parent);
3554			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3555
3556		case EXDEV:
3557			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3558			    "source and target pools differ"));
3559			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3560			    errbuf));
3561
3562		default:
3563			return (zfs_standard_error(zhp->zfs_hdl, errno,
3564			    errbuf));
3565		}
3566	}
3567
3568	return (ret);
3569}
3570
3571/*
3572 * Promotes the given clone fs to be the clone parent.
3573 */
3574int
3575zfs_promote(zfs_handle_t *zhp)
3576{
3577	libzfs_handle_t *hdl = zhp->zfs_hdl;
3578	zfs_cmd_t zc = { 0 };
3579	char parent[MAXPATHLEN];
3580	int ret;
3581	char errbuf[1024];
3582
3583	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3584	    "cannot promote '%s'"), zhp->zfs_name);
3585
3586	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3587		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3588		    "snapshots can not be promoted"));
3589		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3590	}
3591
3592	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3593	if (parent[0] == '\0') {
3594		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3595		    "not a cloned filesystem"));
3596		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3597	}
3598
3599	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3600	    sizeof (zc.zc_value));
3601	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3602	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3603
3604	if (ret != 0) {
3605		int save_errno = errno;
3606
3607		switch (save_errno) {
3608		case EEXIST:
3609			/* There is a conflicting snapshot name. */
3610			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3611			    "conflicting snapshot '%s' from parent '%s'"),
3612			    zc.zc_string, parent);
3613			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3614
3615		default:
3616			return (zfs_standard_error(hdl, save_errno, errbuf));
3617		}
3618	}
3619	return (ret);
3620}
3621
3622typedef struct snapdata {
3623	nvlist_t *sd_nvl;
3624	const char *sd_snapname;
3625} snapdata_t;
3626
3627static int
3628zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3629{
3630	snapdata_t *sd = arg;
3631	char name[ZFS_MAXNAMELEN];
3632	int rv = 0;
3633
3634	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3635		(void) snprintf(name, sizeof (name),
3636		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3637
3638		fnvlist_add_boolean(sd->sd_nvl, name);
3639
3640		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3641	}
3642	zfs_close(zhp);
3643
3644	return (rv);
3645}
3646
3647/*
3648 * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3649 * created.
3650 */
3651int
3652zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3653{
3654	int ret;
3655	char errbuf[1024];
3656	nvpair_t *elem;
3657	nvlist_t *errors;
3658
3659	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3660	    "cannot create snapshots "));
3661
3662	elem = NULL;
3663	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3664		const char *snapname = nvpair_name(elem);
3665
3666		/* validate the target name */
3667		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3668		    B_TRUE)) {
3669			(void) snprintf(errbuf, sizeof (errbuf),
3670			    dgettext(TEXT_DOMAIN,
3671			    "cannot create snapshot '%s'"), snapname);
3672			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3673		}
3674	}
3675
3676	/*
3677	 * get pool handle for prop validation. assumes all snaps are in the
3678	 * same pool, as does lzc_snapshot (below).
3679	 */
3680	char pool[MAXNAMELEN];
3681	elem = nvlist_next_nvpair(snaps, NULL);
3682	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3683	pool[strcspn(pool, "/@")] = '\0';
3684	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3685
3686	if (props != NULL &&
3687	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3688	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3689		zpool_close(zpool_hdl);
3690		return (-1);
3691	}
3692	zpool_close(zpool_hdl);
3693
3694	ret = lzc_snapshot(snaps, props, &errors);
3695
3696	if (ret != 0) {
3697		boolean_t printed = B_FALSE;
3698		for (elem = nvlist_next_nvpair(errors, NULL);
3699		    elem != NULL;
3700		    elem = nvlist_next_nvpair(errors, elem)) {
3701			(void) snprintf(errbuf, sizeof (errbuf),
3702			    dgettext(TEXT_DOMAIN,
3703			    "cannot create snapshot '%s'"), nvpair_name(elem));
3704			(void) zfs_standard_error(hdl,
3705			    fnvpair_value_int32(elem), errbuf);
3706			printed = B_TRUE;
3707		}
3708		if (!printed) {
3709			switch (ret) {
3710			case EXDEV:
3711				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3712				    "multiple snapshots of same "
3713				    "fs not allowed"));
3714				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3715
3716				break;
3717			default:
3718				(void) zfs_standard_error(hdl, ret, errbuf);
3719			}
3720		}
3721	}
3722
3723	nvlist_free(props);
3724	nvlist_free(errors);
3725	return (ret);
3726}
3727
3728int
3729zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3730    nvlist_t *props)
3731{
3732	int ret;
3733	snapdata_t sd = { 0 };
3734	char fsname[ZFS_MAXNAMELEN];
3735	char *cp;
3736	zfs_handle_t *zhp;
3737	char errbuf[1024];
3738
3739	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3740	    "cannot snapshot %s"), path);
3741
3742	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3743		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3744
3745	(void) strlcpy(fsname, path, sizeof (fsname));
3746	cp = strchr(fsname, '@');
3747	*cp = '\0';
3748	sd.sd_snapname = cp + 1;
3749
3750	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3751	    ZFS_TYPE_VOLUME)) == NULL) {
3752		return (-1);
3753	}
3754
3755	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3756	if (recursive) {
3757		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3758	} else {
3759		fnvlist_add_boolean(sd.sd_nvl, path);
3760	}
3761
3762	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3763	nvlist_free(sd.sd_nvl);
3764	zfs_close(zhp);
3765	return (ret);
3766}
3767
3768/*
3769 * Destroy any more recent snapshots.  We invoke this callback on any dependents
3770 * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3771 * is a dependent and we should just destroy it without checking the transaction
3772 * group.
3773 */
3774typedef struct rollback_data {
3775	const char	*cb_target;		/* the snapshot */
3776	uint64_t	cb_create;		/* creation time reference */
3777	boolean_t	cb_error;
3778	boolean_t	cb_force;
3779} rollback_data_t;
3780
3781static int
3782rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3783{
3784	rollback_data_t *cbp = data;
3785	prop_changelist_t *clp;
3786
3787	/* We must destroy this clone; first unmount it */
3788	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3789	    cbp->cb_force ? MS_FORCE: 0);
3790	if (clp == NULL || changelist_prefix(clp) != 0) {
3791		cbp->cb_error = B_TRUE;
3792		zfs_close(zhp);
3793		return (0);
3794	}
3795	if (zfs_destroy(zhp, B_FALSE) != 0)
3796		cbp->cb_error = B_TRUE;
3797	else
3798		changelist_remove(clp, zhp->zfs_name);
3799	(void) changelist_postfix(clp);
3800	changelist_free(clp);
3801
3802	zfs_close(zhp);
3803	return (0);
3804}
3805
3806static int
3807rollback_destroy(zfs_handle_t *zhp, void *data)
3808{
3809	rollback_data_t *cbp = data;
3810
3811	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3812		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3813		    rollback_destroy_dependent, cbp);
3814
3815		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3816	}
3817
3818	zfs_close(zhp);
3819	return (0);
3820}
3821
3822/*
3823 * Given a dataset, rollback to a specific snapshot, discarding any
3824 * data changes since then and making it the active dataset.
3825 *
3826 * Any snapshots and bookmarks more recent than the target are
3827 * destroyed, along with their dependents (i.e. clones).
3828 */
3829int
3830zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3831{
3832	rollback_data_t cb = { 0 };
3833	int err;
3834	boolean_t restore_resv = 0;
3835	uint64_t old_volsize, new_volsize;
3836	zfs_prop_t resv_prop;
3837
3838	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3839	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3840
3841	/*
3842	 * Destroy all recent snapshots and their dependents.
3843	 */
3844	cb.cb_force = force;
3845	cb.cb_target = snap->zfs_name;
3846	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3847	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3848	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3849
3850	if (cb.cb_error)
3851		return (-1);
3852
3853	/*
3854	 * Now that we have verified that the snapshot is the latest,
3855	 * rollback to the given snapshot.
3856	 */
3857
3858	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3859		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3860			return (-1);
3861		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3862		restore_resv =
3863		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3864	}
3865
3866	/*
3867	 * We rely on zfs_iter_children() to verify that there are no
3868	 * newer snapshots for the given dataset.  Therefore, we can
3869	 * simply pass the name on to the ioctl() call.  There is still
3870	 * an unlikely race condition where the user has taken a
3871	 * snapshot since we verified that this was the most recent.
3872	 */
3873	err = lzc_rollback(zhp->zfs_name, NULL, 0);
3874	if (err != 0) {
3875		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3876		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3877		    zhp->zfs_name);
3878		return (err);
3879	}
3880
3881	/*
3882	 * For volumes, if the pre-rollback volsize matched the pre-
3883	 * rollback reservation and the volsize has changed then set
3884	 * the reservation property to the post-rollback volsize.
3885	 * Make a new handle since the rollback closed the dataset.
3886	 */
3887	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3888	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3889		if (restore_resv) {
3890			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3891			if (old_volsize != new_volsize)
3892				err = zfs_prop_set_int(zhp, resv_prop,
3893				    new_volsize);
3894		}
3895		zfs_close(zhp);
3896	}
3897	return (err);
3898}
3899
3900/*
3901 * Renames the given dataset.
3902 */
3903int
3904zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
3905    renameflags_t flags)
3906{
3907	int ret;
3908	zfs_cmd_t zc = { 0 };
3909	char *delim;
3910	prop_changelist_t *cl = NULL;
3911	zfs_handle_t *zhrp = NULL;
3912	char *parentname = NULL;
3913	char parent[ZFS_MAXNAMELEN];
3914	char property[ZFS_MAXPROPLEN];
3915	libzfs_handle_t *hdl = zhp->zfs_hdl;
3916	char errbuf[1024];
3917
3918	/* if we have the same exact name, just return success */
3919	if (strcmp(zhp->zfs_name, target) == 0)
3920		return (0);
3921
3922	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3923	    "cannot rename to '%s'"), target);
3924
3925	if (source != NULL) {
3926		/*
3927		 * This is recursive snapshots rename, put snapshot name
3928		 * (that might not exist) into zfs_name.
3929		 */
3930		assert(flags.recurse);
3931
3932		(void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
3933		(void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
3934		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
3935	}
3936
3937	/*
3938	 * Make sure the target name is valid
3939	 */
3940	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3941		if ((strchr(target, '@') == NULL) ||
3942		    *target == '@') {
3943			/*
3944			 * Snapshot target name is abbreviated,
3945			 * reconstruct full dataset name
3946			 */
3947			(void) strlcpy(parent, zhp->zfs_name,
3948			    sizeof (parent));
3949			delim = strchr(parent, '@');
3950			if (strchr(target, '@') == NULL)
3951				*(++delim) = '\0';
3952			else
3953				*delim = '\0';
3954			(void) strlcat(parent, target, sizeof (parent));
3955			target = parent;
3956		} else {
3957			/*
3958			 * Make sure we're renaming within the same dataset.
3959			 */
3960			delim = strchr(target, '@');
3961			if (strncmp(zhp->zfs_name, target, delim - target)
3962			    != 0 || zhp->zfs_name[delim - target] != '@') {
3963				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3964				    "snapshots must be part of same "
3965				    "dataset"));
3966				return (zfs_error(hdl, EZFS_CROSSTARGET,
3967				    errbuf));
3968			}
3969		}
3970		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3971			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3972	} else {
3973		if (flags.recurse) {
3974			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3975			    "recursive rename must be a snapshot"));
3976			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3977		}
3978
3979		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3980			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3981
3982		/* validate parents */
3983		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3984			return (-1);
3985
3986		/* make sure we're in the same pool */
3987		verify((delim = strchr(target, '/')) != NULL);
3988		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3989		    zhp->zfs_name[delim - target] != '/') {
3990			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3991			    "datasets must be within same pool"));
3992			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3993		}
3994
3995		/* new name cannot be a child of the current dataset name */
3996		if (is_descendant(zhp->zfs_name, target)) {
3997			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3998			    "New dataset name cannot be a descendant of "
3999			    "current dataset name"));
4000			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4001		}
4002	}
4003
4004	(void) snprintf(errbuf, sizeof (errbuf),
4005	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4006
4007	if (getzoneid() == GLOBAL_ZONEID &&
4008	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4009		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4010		    "dataset is used in a non-global zone"));
4011		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4012	}
4013
4014	/*
4015	 * Avoid unmounting file systems with mountpoint property set to
4016	 * 'legacy' or 'none' even if -u option is not given.
4017	 */
4018	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4019	    !flags.recurse && !flags.nounmount &&
4020	    zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4021	    sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4022	    (strcmp(property, "legacy") == 0 ||
4023	     strcmp(property, "none") == 0)) {
4024		flags.nounmount = B_TRUE;
4025	}
4026	if (flags.recurse) {
4027
4028		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4029		if (parentname == NULL) {
4030			ret = -1;
4031			goto error;
4032		}
4033		delim = strchr(parentname, '@');
4034		*delim = '\0';
4035		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4036		if (zhrp == NULL) {
4037			ret = -1;
4038			goto error;
4039		}
4040	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4041		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4042		    flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4043		    flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4044			return (-1);
4045		}
4046
4047		if (changelist_haszonedchild(cl)) {
4048			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4049			    "child dataset with inherited mountpoint is used "
4050			    "in a non-global zone"));
4051			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4052			goto error;
4053		}
4054
4055		if ((ret = changelist_prefix(cl)) != 0)
4056			goto error;
4057	}
4058
4059	if (ZFS_IS_VOLUME(zhp))
4060		zc.zc_objset_type = DMU_OST_ZVOL;
4061	else
4062		zc.zc_objset_type = DMU_OST_ZFS;
4063
4064	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4065	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4066
4067	zc.zc_cookie = flags.recurse ? 1 : 0;
4068	if (flags.nounmount)
4069		zc.zc_cookie |= 2;
4070
4071	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4072		/*
4073		 * if it was recursive, the one that actually failed will
4074		 * be in zc.zc_name
4075		 */
4076		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4077		    "cannot rename '%s'"), zc.zc_name);
4078
4079		if (flags.recurse && errno == EEXIST) {
4080			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4081			    "a child dataset already has a snapshot "
4082			    "with the new name"));
4083			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4084		} else {
4085			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4086		}
4087
4088		/*
4089		 * On failure, we still want to remount any filesystems that
4090		 * were previously mounted, so we don't alter the system state.
4091		 */
4092		if (cl != NULL)
4093			(void) changelist_postfix(cl);
4094	} else {
4095		if (cl != NULL) {
4096			changelist_rename(cl, zfs_get_name(zhp), target);
4097			ret = changelist_postfix(cl);
4098		}
4099	}
4100
4101error:
4102	if (parentname != NULL) {
4103		free(parentname);
4104	}
4105	if (zhrp != NULL) {
4106		zfs_close(zhrp);
4107	}
4108	if (cl != NULL) {
4109		changelist_free(cl);
4110	}
4111	return (ret);
4112}
4113
4114nvlist_t *
4115zfs_get_user_props(zfs_handle_t *zhp)
4116{
4117	return (zhp->zfs_user_props);
4118}
4119
4120nvlist_t *
4121zfs_get_recvd_props(zfs_handle_t *zhp)
4122{
4123	if (zhp->zfs_recvd_props == NULL)
4124		if (get_recvd_props_ioctl(zhp) != 0)
4125			return (NULL);
4126	return (zhp->zfs_recvd_props);
4127}
4128
4129/*
4130 * This function is used by 'zfs list' to determine the exact set of columns to
4131 * display, and their maximum widths.  This does two main things:
4132 *
4133 *      - If this is a list of all properties, then expand the list to include
4134 *        all native properties, and set a flag so that for each dataset we look
4135 *        for new unique user properties and add them to the list.
4136 *
4137 *      - For non fixed-width properties, keep track of the maximum width seen
4138 *        so that we can size the column appropriately. If the user has
4139 *        requested received property values, we also need to compute the width
4140 *        of the RECEIVED column.
4141 */
4142int
4143zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4144    boolean_t literal)
4145{
4146	libzfs_handle_t *hdl = zhp->zfs_hdl;
4147	zprop_list_t *entry;
4148	zprop_list_t **last, **start;
4149	nvlist_t *userprops, *propval;
4150	nvpair_t *elem;
4151	char *strval;
4152	char buf[ZFS_MAXPROPLEN];
4153
4154	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4155		return (-1);
4156
4157	userprops = zfs_get_user_props(zhp);
4158
4159	entry = *plp;
4160	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4161		/*
4162		 * Go through and add any user properties as necessary.  We
4163		 * start by incrementing our list pointer to the first
4164		 * non-native property.
4165		 */
4166		start = plp;
4167		while (*start != NULL) {
4168			if ((*start)->pl_prop == ZPROP_INVAL)
4169				break;
4170			start = &(*start)->pl_next;
4171		}
4172
4173		elem = NULL;
4174		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4175			/*
4176			 * See if we've already found this property in our list.
4177			 */
4178			for (last = start; *last != NULL;
4179			    last = &(*last)->pl_next) {
4180				if (strcmp((*last)->pl_user_prop,
4181				    nvpair_name(elem)) == 0)
4182					break;
4183			}
4184
4185			if (*last == NULL) {
4186				if ((entry = zfs_alloc(hdl,
4187				    sizeof (zprop_list_t))) == NULL ||
4188				    ((entry->pl_user_prop = zfs_strdup(hdl,
4189				    nvpair_name(elem)))) == NULL) {
4190					free(entry);
4191					return (-1);
4192				}
4193
4194				entry->pl_prop = ZPROP_INVAL;
4195				entry->pl_width = strlen(nvpair_name(elem));
4196				entry->pl_all = B_TRUE;
4197				*last = entry;
4198			}
4199		}
4200	}
4201
4202	/*
4203	 * Now go through and check the width of any non-fixed columns
4204	 */
4205	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4206		if (entry->pl_fixed && !literal)
4207			continue;
4208
4209		if (entry->pl_prop != ZPROP_INVAL) {
4210			if (zfs_prop_get(zhp, entry->pl_prop,
4211			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4212				if (strlen(buf) > entry->pl_width)
4213					entry->pl_width = strlen(buf);
4214			}
4215			if (received && zfs_prop_get_recvd(zhp,
4216			    zfs_prop_to_name(entry->pl_prop),
4217			    buf, sizeof (buf), literal) == 0)
4218				if (strlen(buf) > entry->pl_recvd_width)
4219					entry->pl_recvd_width = strlen(buf);
4220		} else {
4221			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4222			    &propval) == 0) {
4223				verify(nvlist_lookup_string(propval,
4224				    ZPROP_VALUE, &strval) == 0);
4225				if (strlen(strval) > entry->pl_width)
4226					entry->pl_width = strlen(strval);
4227			}
4228			if (received && zfs_prop_get_recvd(zhp,
4229			    entry->pl_user_prop,
4230			    buf, sizeof (buf), literal) == 0)
4231				if (strlen(buf) > entry->pl_recvd_width)
4232					entry->pl_recvd_width = strlen(buf);
4233		}
4234	}
4235
4236	return (0);
4237}
4238
4239int
4240zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4241    char *resource, void *export, void *sharetab,
4242    int sharemax, zfs_share_op_t operation)
4243{
4244	zfs_cmd_t zc = { 0 };
4245	int error;
4246
4247	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4248	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4249	if (resource)
4250		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4251	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4252	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4253	zc.zc_share.z_sharetype = operation;
4254	zc.zc_share.z_sharemax = sharemax;
4255	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4256	return (error);
4257}
4258
4259void
4260zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4261{
4262	nvpair_t *curr;
4263
4264	/*
4265	 * Keep a reference to the props-table against which we prune the
4266	 * properties.
4267	 */
4268	zhp->zfs_props_table = props;
4269
4270	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4271
4272	while (curr) {
4273		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4274		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4275
4276		/*
4277		 * User properties will result in ZPROP_INVAL, and since we
4278		 * only know how to prune standard ZFS properties, we always
4279		 * leave these in the list.  This can also happen if we
4280		 * encounter an unknown DSL property (when running older
4281		 * software, for example).
4282		 */
4283		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4284			(void) nvlist_remove(zhp->zfs_props,
4285			    nvpair_name(curr), nvpair_type(curr));
4286		curr = next;
4287	}
4288}
4289
4290#ifdef illumos
4291static int
4292zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4293    zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4294{
4295	zfs_cmd_t zc = { 0 };
4296	nvlist_t *nvlist = NULL;
4297	int error;
4298
4299	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4300	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4301	zc.zc_cookie = (uint64_t)cmd;
4302
4303	if (cmd == ZFS_SMB_ACL_RENAME) {
4304		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4305			(void) no_memory(hdl);
4306			return (0);
4307		}
4308	}
4309
4310	switch (cmd) {
4311	case ZFS_SMB_ACL_ADD:
4312	case ZFS_SMB_ACL_REMOVE:
4313		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4314		break;
4315	case ZFS_SMB_ACL_RENAME:
4316		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4317		    resource1) != 0) {
4318				(void) no_memory(hdl);
4319				return (-1);
4320		}
4321		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4322		    resource2) != 0) {
4323				(void) no_memory(hdl);
4324				return (-1);
4325		}
4326		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4327			nvlist_free(nvlist);
4328			return (-1);
4329		}
4330		break;
4331	case ZFS_SMB_ACL_PURGE:
4332		break;
4333	default:
4334		return (-1);
4335	}
4336	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4337	nvlist_free(nvlist);
4338	return (error);
4339}
4340
4341int
4342zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4343    char *path, char *resource)
4344{
4345	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4346	    resource, NULL));
4347}
4348
4349int
4350zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4351    char *path, char *resource)
4352{
4353	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4354	    resource, NULL));
4355}
4356
4357int
4358zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4359{
4360	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4361	    NULL, NULL));
4362}
4363
4364int
4365zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4366    char *oldname, char *newname)
4367{
4368	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4369	    oldname, newname));
4370}
4371#endif	/* illumos */
4372
4373int
4374zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4375    zfs_userspace_cb_t func, void *arg)
4376{
4377	zfs_cmd_t zc = { 0 };
4378	zfs_useracct_t buf[100];
4379	libzfs_handle_t *hdl = zhp->zfs_hdl;
4380	int ret;
4381
4382	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4383
4384	zc.zc_objset_type = type;
4385	zc.zc_nvlist_dst = (uintptr_t)buf;
4386
4387	for (;;) {
4388		zfs_useracct_t *zua = buf;
4389
4390		zc.zc_nvlist_dst_size = sizeof (buf);
4391		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4392			char errbuf[1024];
4393
4394			(void) snprintf(errbuf, sizeof (errbuf),
4395			    dgettext(TEXT_DOMAIN,
4396			    "cannot get used/quota for %s"), zc.zc_name);
4397			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4398		}
4399		if (zc.zc_nvlist_dst_size == 0)
4400			break;
4401
4402		while (zc.zc_nvlist_dst_size > 0) {
4403			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4404			    zua->zu_space)) != 0)
4405				return (ret);
4406			zua++;
4407			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4408		}
4409	}
4410
4411	return (0);
4412}
4413
4414struct holdarg {
4415	nvlist_t *nvl;
4416	const char *snapname;
4417	const char *tag;
4418	boolean_t recursive;
4419	int error;
4420};
4421
4422static int
4423zfs_hold_one(zfs_handle_t *zhp, void *arg)
4424{
4425	struct holdarg *ha = arg;
4426	char name[ZFS_MAXNAMELEN];
4427	int rv = 0;
4428
4429	(void) snprintf(name, sizeof (name),
4430	    "%s@%s", zhp->zfs_name, ha->snapname);
4431
4432	if (lzc_exists(name))
4433		fnvlist_add_string(ha->nvl, name, ha->tag);
4434
4435	if (ha->recursive)
4436		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4437	zfs_close(zhp);
4438	return (rv);
4439}
4440
4441int
4442zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4443    boolean_t recursive, int cleanup_fd)
4444{
4445	int ret;
4446	struct holdarg ha;
4447
4448	ha.nvl = fnvlist_alloc();
4449	ha.snapname = snapname;
4450	ha.tag = tag;
4451	ha.recursive = recursive;
4452	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4453
4454	if (nvlist_empty(ha.nvl)) {
4455		char errbuf[1024];
4456
4457		fnvlist_free(ha.nvl);
4458		ret = ENOENT;
4459		(void) snprintf(errbuf, sizeof (errbuf),
4460		    dgettext(TEXT_DOMAIN,
4461		    "cannot hold snapshot '%s@%s'"),
4462		    zhp->zfs_name, snapname);
4463		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4464		return (ret);
4465	}
4466
4467	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4468	fnvlist_free(ha.nvl);
4469
4470	return (ret);
4471}
4472
4473int
4474zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4475{
4476	int ret;
4477	nvlist_t *errors;
4478	libzfs_handle_t *hdl = zhp->zfs_hdl;
4479	char errbuf[1024];
4480	nvpair_t *elem;
4481
4482	errors = NULL;
4483	ret = lzc_hold(holds, cleanup_fd, &errors);
4484
4485	if (ret == 0) {
4486		/* There may be errors even in the success case. */
4487		fnvlist_free(errors);
4488		return (0);
4489	}
4490
4491	if (nvlist_empty(errors)) {
4492		/* no hold-specific errors */
4493		(void) snprintf(errbuf, sizeof (errbuf),
4494		    dgettext(TEXT_DOMAIN, "cannot hold"));
4495		switch (ret) {
4496		case ENOTSUP:
4497			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4498			    "pool must be upgraded"));
4499			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4500			break;
4501		case EINVAL:
4502			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4503			break;
4504		default:
4505			(void) zfs_standard_error(hdl, ret, errbuf);
4506		}
4507	}
4508
4509	for (elem = nvlist_next_nvpair(errors, NULL);
4510	    elem != NULL;
4511	    elem = nvlist_next_nvpair(errors, elem)) {
4512		(void) snprintf(errbuf, sizeof (errbuf),
4513		    dgettext(TEXT_DOMAIN,
4514		    "cannot hold snapshot '%s'"), nvpair_name(elem));
4515		switch (fnvpair_value_int32(elem)) {
4516		case E2BIG:
4517			/*
4518			 * Temporary tags wind up having the ds object id
4519			 * prepended. So even if we passed the length check
4520			 * above, it's still possible for the tag to wind
4521			 * up being slightly too long.
4522			 */
4523			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4524			break;
4525		case EINVAL:
4526			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4527			break;
4528		case EEXIST:
4529			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4530			break;
4531		default:
4532			(void) zfs_standard_error(hdl,
4533			    fnvpair_value_int32(elem), errbuf);
4534		}
4535	}
4536
4537	fnvlist_free(errors);
4538	return (ret);
4539}
4540
4541static int
4542zfs_release_one(zfs_handle_t *zhp, void *arg)
4543{
4544	struct holdarg *ha = arg;
4545	char name[ZFS_MAXNAMELEN];
4546	int rv = 0;
4547	nvlist_t *existing_holds;
4548
4549	(void) snprintf(name, sizeof (name),
4550	    "%s@%s", zhp->zfs_name, ha->snapname);
4551
4552	if (lzc_get_holds(name, &existing_holds) != 0) {
4553		ha->error = ENOENT;
4554	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4555		ha->error = ESRCH;
4556	} else {
4557		nvlist_t *torelease = fnvlist_alloc();
4558		fnvlist_add_boolean(torelease, ha->tag);
4559		fnvlist_add_nvlist(ha->nvl, name, torelease);
4560		fnvlist_free(torelease);
4561	}
4562
4563	if (ha->recursive)
4564		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4565	zfs_close(zhp);
4566	return (rv);
4567}
4568
4569int
4570zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4571    boolean_t recursive)
4572{
4573	int ret;
4574	struct holdarg ha;
4575	nvlist_t *errors = NULL;
4576	nvpair_t *elem;
4577	libzfs_handle_t *hdl = zhp->zfs_hdl;
4578	char errbuf[1024];
4579
4580	ha.nvl = fnvlist_alloc();
4581	ha.snapname = snapname;
4582	ha.tag = tag;
4583	ha.recursive = recursive;
4584	ha.error = 0;
4585	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4586
4587	if (nvlist_empty(ha.nvl)) {
4588		fnvlist_free(ha.nvl);
4589		ret = ha.error;
4590		(void) snprintf(errbuf, sizeof (errbuf),
4591		    dgettext(TEXT_DOMAIN,
4592		    "cannot release hold from snapshot '%s@%s'"),
4593		    zhp->zfs_name, snapname);
4594		if (ret == ESRCH) {
4595			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4596		} else {
4597			(void) zfs_standard_error(hdl, ret, errbuf);
4598		}
4599		return (ret);
4600	}
4601
4602	ret = lzc_release(ha.nvl, &errors);
4603	fnvlist_free(ha.nvl);
4604
4605	if (ret == 0) {
4606		/* There may be errors even in the success case. */
4607		fnvlist_free(errors);
4608		return (0);
4609	}
4610
4611	if (nvlist_empty(errors)) {
4612		/* no hold-specific errors */
4613		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4614		    "cannot release"));
4615		switch (errno) {
4616		case ENOTSUP:
4617			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4618			    "pool must be upgraded"));
4619			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4620			break;
4621		default:
4622			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4623		}
4624	}
4625
4626	for (elem = nvlist_next_nvpair(errors, NULL);
4627	    elem != NULL;
4628	    elem = nvlist_next_nvpair(errors, elem)) {
4629		(void) snprintf(errbuf, sizeof (errbuf),
4630		    dgettext(TEXT_DOMAIN,
4631		    "cannot release hold from snapshot '%s'"),
4632		    nvpair_name(elem));
4633		switch (fnvpair_value_int32(elem)) {
4634		case ESRCH:
4635			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4636			break;
4637		case EINVAL:
4638			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4639			break;
4640		default:
4641			(void) zfs_standard_error_fmt(hdl,
4642			    fnvpair_value_int32(elem), errbuf);
4643		}
4644	}
4645
4646	fnvlist_free(errors);
4647	return (ret);
4648}
4649
4650int
4651zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4652{
4653	zfs_cmd_t zc = { 0 };
4654	libzfs_handle_t *hdl = zhp->zfs_hdl;
4655	int nvsz = 2048;
4656	void *nvbuf;
4657	int err = 0;
4658	char errbuf[1024];
4659
4660	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4661	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4662
4663tryagain:
4664
4665	nvbuf = malloc(nvsz);
4666	if (nvbuf == NULL) {
4667		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4668		goto out;
4669	}
4670
4671	zc.zc_nvlist_dst_size = nvsz;
4672	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4673
4674	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4675
4676	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4677		(void) snprintf(errbuf, sizeof (errbuf),
4678		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4679		    zc.zc_name);
4680		switch (errno) {
4681		case ENOMEM:
4682			free(nvbuf);
4683			nvsz = zc.zc_nvlist_dst_size;
4684			goto tryagain;
4685
4686		case ENOTSUP:
4687			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4688			    "pool must be upgraded"));
4689			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4690			break;
4691		case EINVAL:
4692			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4693			break;
4694		case ENOENT:
4695			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4696			break;
4697		default:
4698			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4699			break;
4700		}
4701	} else {
4702		/* success */
4703		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4704		if (rc) {
4705			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
4706			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
4707			    zc.zc_name);
4708			err = zfs_standard_error_fmt(hdl, rc, errbuf);
4709		}
4710	}
4711
4712	free(nvbuf);
4713out:
4714	return (err);
4715}
4716
4717int
4718zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4719{
4720	zfs_cmd_t zc = { 0 };
4721	libzfs_handle_t *hdl = zhp->zfs_hdl;
4722	char *nvbuf;
4723	char errbuf[1024];
4724	size_t nvsz;
4725	int err;
4726
4727	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4728	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4729
4730	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4731	assert(err == 0);
4732
4733	nvbuf = malloc(nvsz);
4734
4735	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4736	assert(err == 0);
4737
4738	zc.zc_nvlist_src_size = nvsz;
4739	zc.zc_nvlist_src = (uintptr_t)nvbuf;
4740	zc.zc_perm_action = un;
4741
4742	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4743
4744	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4745		(void) snprintf(errbuf, sizeof (errbuf),
4746		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4747		    zc.zc_name);
4748		switch (errno) {
4749		case ENOTSUP:
4750			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4751			    "pool must be upgraded"));
4752			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4753			break;
4754		case EINVAL:
4755			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4756			break;
4757		case ENOENT:
4758			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4759			break;
4760		default:
4761			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4762			break;
4763		}
4764	}
4765
4766	free(nvbuf);
4767
4768	return (err);
4769}
4770
4771int
4772zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4773{
4774	int err;
4775	char errbuf[1024];
4776
4777	err = lzc_get_holds(zhp->zfs_name, nvl);
4778
4779	if (err != 0) {
4780		libzfs_handle_t *hdl = zhp->zfs_hdl;
4781
4782		(void) snprintf(errbuf, sizeof (errbuf),
4783		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4784		    zhp->zfs_name);
4785		switch (err) {
4786		case ENOTSUP:
4787			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4788			    "pool must be upgraded"));
4789			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4790			break;
4791		case EINVAL:
4792			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4793			break;
4794		case ENOENT:
4795			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4796			break;
4797		default:
4798			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4799			break;
4800		}
4801	}
4802
4803	return (err);
4804}
4805
4806/*
4807 * Convert the zvol's volume size to an appropriate reservation.
4808 * Note: If this routine is updated, it is necessary to update the ZFS test
4809 * suite's shell version in reservation.kshlib.
4810 */
4811uint64_t
4812zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4813{
4814	uint64_t numdb;
4815	uint64_t nblocks, volblocksize;
4816	int ncopies;
4817	char *strval;
4818
4819	if (nvlist_lookup_string(props,
4820	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4821		ncopies = atoi(strval);
4822	else
4823		ncopies = 1;
4824	if (nvlist_lookup_uint64(props,
4825	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4826	    &volblocksize) != 0)
4827		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4828	nblocks = volsize/volblocksize;
4829	/* start with metadnode L0-L6 */
4830	numdb = 7;
4831	/* calculate number of indirects */
4832	while (nblocks > 1) {
4833		nblocks += DNODES_PER_LEVEL - 1;
4834		nblocks /= DNODES_PER_LEVEL;
4835		numdb += nblocks;
4836	}
4837	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4838	volsize *= ncopies;
4839	/*
4840	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4841	 * compressed, but in practice they compress down to about
4842	 * 1100 bytes
4843	 */
4844	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4845	volsize += numdb;
4846	return (volsize);
4847}
4848
4849/*
4850 * Attach/detach the given filesystem to/from the given jail.
4851 */
4852int
4853zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4854{
4855	libzfs_handle_t *hdl = zhp->zfs_hdl;
4856	zfs_cmd_t zc = { 0 };
4857	char errbuf[1024];
4858	unsigned long cmd;
4859	int ret;
4860
4861	if (attach) {
4862		(void) snprintf(errbuf, sizeof (errbuf),
4863		    dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4864	} else {
4865		(void) snprintf(errbuf, sizeof (errbuf),
4866		    dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
4867	}
4868
4869	switch (zhp->zfs_type) {
4870	case ZFS_TYPE_VOLUME:
4871		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4872		    "volumes can not be jailed"));
4873		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4874	case ZFS_TYPE_SNAPSHOT:
4875		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4876		    "snapshots can not be jailed"));
4877		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4878	}
4879	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4880
4881	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4882	zc.zc_objset_type = DMU_OST_ZFS;
4883	zc.zc_jailid = jailid;
4884
4885	cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4886	if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4887		zfs_standard_error(hdl, errno, errbuf);
4888
4889	return (ret);
4890}
4891