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