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