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