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