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