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