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