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