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