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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Internal utility routines for the ZFS library.
28  */
29 
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <libintl.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <math.h>
40 #include <sys/mnttab.h>
41 #include <sys/mntent.h>
42 #include <sys/types.h>
43 
44 #include <libzfs.h>
45 
46 #include "libzfs_impl.h"
47 #include "zfs_prop.h"
48 
49 int
50 libzfs_errno(libzfs_handle_t *hdl)
51 {
52 	return (hdl->libzfs_error);
53 }
54 
55 const char *
56 libzfs_error_action(libzfs_handle_t *hdl)
57 {
58 	return (hdl->libzfs_action);
59 }
60 
61 const char *
62 libzfs_error_description(libzfs_handle_t *hdl)
63 {
64 	if (hdl->libzfs_desc[0] != '\0')
65 		return (hdl->libzfs_desc);
66 
67 	switch (hdl->libzfs_error) {
68 	case EZFS_NOMEM:
69 		return (dgettext(TEXT_DOMAIN, "out of memory"));
70 	case EZFS_BADPROP:
71 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
72 	case EZFS_PROPREADONLY:
73 		return (dgettext(TEXT_DOMAIN, "read only property"));
74 	case EZFS_PROPTYPE:
75 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
76 		    "datasets of this type"));
77 	case EZFS_PROPNONINHERIT:
78 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
79 	case EZFS_PROPSPACE:
80 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
81 	case EZFS_BADTYPE:
82 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
83 		    "datasets of this type"));
84 	case EZFS_BUSY:
85 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
86 	case EZFS_EXISTS:
87 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
88 	case EZFS_NOENT:
89 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
90 	case EZFS_BADSTREAM:
91 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
92 	case EZFS_DSREADONLY:
93 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
94 	case EZFS_VOLTOOBIG:
95 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
96 		    "this system"));
97 	case EZFS_VOLHASDATA:
98 		return (dgettext(TEXT_DOMAIN, "volume has data"));
99 	case EZFS_INVALIDNAME:
100 		return (dgettext(TEXT_DOMAIN, "invalid name"));
101 	case EZFS_BADRESTORE:
102 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
103 		    "destination"));
104 	case EZFS_BADBACKUP:
105 		return (dgettext(TEXT_DOMAIN, "backup failed"));
106 	case EZFS_BADTARGET:
107 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
108 	case EZFS_NODEVICE:
109 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
110 	case EZFS_BADDEV:
111 		return (dgettext(TEXT_DOMAIN, "invalid device"));
112 	case EZFS_NOREPLICAS:
113 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
114 	case EZFS_RESILVERING:
115 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
116 	case EZFS_BADVERSION:
117 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
118 	case EZFS_POOLUNAVAIL:
119 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
120 	case EZFS_DEVOVERFLOW:
121 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
122 	case EZFS_BADPATH:
123 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
124 	case EZFS_CROSSTARGET:
125 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
126 		    "pools"));
127 	case EZFS_ZONED:
128 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
129 	case EZFS_MOUNTFAILED:
130 		return (dgettext(TEXT_DOMAIN, "mount failed"));
131 	case EZFS_UMOUNTFAILED:
132 		return (dgettext(TEXT_DOMAIN, "umount failed"));
133 	case EZFS_UNSHARENFSFAILED:
134 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
135 	case EZFS_SHARENFSFAILED:
136 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
137 	case EZFS_UNSHARESMBFAILED:
138 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
139 	case EZFS_SHARESMBFAILED:
140 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
141 	case EZFS_ISCSISVCUNAVAIL:
142 		return (dgettext(TEXT_DOMAIN,
143 		    "iscsitgt service need to be enabled by "
144 		    "a privileged user"));
145 	case EZFS_DEVLINKS:
146 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
147 	case EZFS_PERM:
148 		return (dgettext(TEXT_DOMAIN, "permission denied"));
149 	case EZFS_NOSPC:
150 		return (dgettext(TEXT_DOMAIN, "out of space"));
151 	case EZFS_IO:
152 		return (dgettext(TEXT_DOMAIN, "I/O error"));
153 	case EZFS_INTR:
154 		return (dgettext(TEXT_DOMAIN, "signal received"));
155 	case EZFS_ISSPARE:
156 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
157 		    "spare"));
158 	case EZFS_INVALCONFIG:
159 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
160 	case EZFS_RECURSIVE:
161 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
162 	case EZFS_NOHISTORY:
163 		return (dgettext(TEXT_DOMAIN, "no history available"));
164 	case EZFS_UNSHAREISCSIFAILED:
165 		return (dgettext(TEXT_DOMAIN,
166 		    "iscsitgtd failed request to unshare"));
167 	case EZFS_SHAREISCSIFAILED:
168 		return (dgettext(TEXT_DOMAIN,
169 		    "iscsitgtd failed request to share"));
170 	case EZFS_POOLPROPS:
171 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
172 		    "pool properties"));
173 	case EZFS_POOL_NOTSUP:
174 		return (dgettext(TEXT_DOMAIN, "operation not supported "
175 		    "on this type of pool"));
176 	case EZFS_POOL_INVALARG:
177 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
178 		    "this pool operation"));
179 	case EZFS_NAMETOOLONG:
180 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
181 	case EZFS_OPENFAILED:
182 		return (dgettext(TEXT_DOMAIN, "open failed"));
183 	case EZFS_NOCAP:
184 		return (dgettext(TEXT_DOMAIN,
185 		    "disk capacity information could not be retrieved"));
186 	case EZFS_LABELFAILED:
187 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
188 	case EZFS_BADWHO:
189 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
190 	case EZFS_BADPERM:
191 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
192 	case EZFS_BADPERMSET:
193 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
194 	case EZFS_NODELEGATION:
195 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
196 		    "disabled on pool"));
197 	case EZFS_PERMRDONLY:
198 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
199 		    " modified"));
200 	case EZFS_BADCACHE:
201 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
202 	case EZFS_ISL2CACHE:
203 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
204 	case EZFS_VDEVNOTSUP:
205 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
206 		    "supported"));
207 	case EZFS_NOTSUP:
208 		return (dgettext(TEXT_DOMAIN, "operation not supported "
209 		    "on this dataset"));
210 	case EZFS_ACTIVE_SPARE:
211 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
212 		    "device"));
213 	case EZFS_UNKNOWN:
214 		return (dgettext(TEXT_DOMAIN, "unknown error"));
215 	default:
216 		assert(hdl->libzfs_error == 0);
217 		return (dgettext(TEXT_DOMAIN, "no error"));
218 	}
219 }
220 
221 /*PRINTFLIKE2*/
222 void
223 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
224 {
225 	va_list ap;
226 
227 	va_start(ap, fmt);
228 
229 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
230 	    fmt, ap);
231 	hdl->libzfs_desc_active = 1;
232 
233 	va_end(ap);
234 }
235 
236 static void
237 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
238 {
239 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
240 	    fmt, ap);
241 	hdl->libzfs_error = error;
242 
243 	if (hdl->libzfs_desc_active)
244 		hdl->libzfs_desc_active = 0;
245 	else
246 		hdl->libzfs_desc[0] = '\0';
247 
248 	if (hdl->libzfs_printerr) {
249 		if (error == EZFS_UNKNOWN) {
250 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
251 			    "error: %s\n"), libzfs_error_description(hdl));
252 			abort();
253 		}
254 
255 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
256 		    libzfs_error_description(hdl));
257 		if (error == EZFS_NOMEM)
258 			exit(1);
259 	}
260 }
261 
262 int
263 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
264 {
265 	return (zfs_error_fmt(hdl, error, "%s", msg));
266 }
267 
268 /*PRINTFLIKE3*/
269 int
270 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
271 {
272 	va_list ap;
273 
274 	va_start(ap, fmt);
275 
276 	zfs_verror(hdl, error, fmt, ap);
277 
278 	va_end(ap);
279 
280 	return (-1);
281 }
282 
283 static int
284 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
285     va_list ap)
286 {
287 	switch (error) {
288 	case EPERM:
289 	case EACCES:
290 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
291 		return (-1);
292 
293 	case ECANCELED:
294 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
295 		return (-1);
296 
297 	case EIO:
298 		zfs_verror(hdl, EZFS_IO, fmt, ap);
299 		return (-1);
300 
301 	case EINTR:
302 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
303 		return (-1);
304 	}
305 
306 	return (0);
307 }
308 
309 int
310 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
311 {
312 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
313 }
314 
315 /*PRINTFLIKE3*/
316 int
317 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
318 {
319 	va_list ap;
320 
321 	va_start(ap, fmt);
322 
323 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
324 		va_end(ap);
325 		return (-1);
326 	}
327 
328 	switch (error) {
329 	case ENXIO:
330 	case ENODEV:
331 		zfs_verror(hdl, EZFS_IO, fmt, ap);
332 		break;
333 
334 	case ENOENT:
335 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
336 		    "dataset does not exist"));
337 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
338 		break;
339 
340 	case ENOSPC:
341 	case EDQUOT:
342 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
343 		return (-1);
344 
345 	case EEXIST:
346 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
347 		    "dataset already exists"));
348 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
349 		break;
350 
351 	case EBUSY:
352 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
353 		    "dataset is busy"));
354 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
355 		break;
356 	case EROFS:
357 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358 		    "snapshot permissions cannot be modified"));
359 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
360 		break;
361 	case ENAMETOOLONG:
362 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
363 		break;
364 	case ENOTSUP:
365 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
366 		break;
367 	default:
368 		zfs_error_aux(hdl, strerror(errno));
369 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
370 		break;
371 	}
372 
373 	va_end(ap);
374 	return (-1);
375 }
376 
377 int
378 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
379 {
380 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
381 }
382 
383 /*PRINTFLIKE3*/
384 int
385 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
386 {
387 	va_list ap;
388 
389 	va_start(ap, fmt);
390 
391 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
392 		va_end(ap);
393 		return (-1);
394 	}
395 
396 	switch (error) {
397 	case ENODEV:
398 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
399 		break;
400 
401 	case ENOENT:
402 		zfs_error_aux(hdl,
403 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
404 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
405 		break;
406 
407 	case EEXIST:
408 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
409 		    "pool already exists"));
410 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
411 		break;
412 
413 	case EBUSY:
414 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
415 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
416 		break;
417 
418 	case ENXIO:
419 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
420 		    "one or more devices is currently unavailable"));
421 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
422 		break;
423 
424 	case ENAMETOOLONG:
425 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
426 		break;
427 
428 	case ENOTSUP:
429 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
430 		break;
431 
432 	case EINVAL:
433 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
434 		break;
435 
436 	case ENOSPC:
437 	case EDQUOT:
438 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
439 		return (-1);
440 
441 	default:
442 		zfs_error_aux(hdl, strerror(error));
443 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
444 	}
445 
446 	va_end(ap);
447 	return (-1);
448 }
449 
450 /*
451  * Display an out of memory error message and abort the current program.
452  */
453 int
454 no_memory(libzfs_handle_t *hdl)
455 {
456 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
457 }
458 
459 /*
460  * A safe form of malloc() which will die if the allocation fails.
461  */
462 void *
463 zfs_alloc(libzfs_handle_t *hdl, size_t size)
464 {
465 	void *data;
466 
467 	if ((data = calloc(1, size)) == NULL)
468 		(void) no_memory(hdl);
469 
470 	return (data);
471 }
472 
473 /*
474  * A safe form of realloc(), which also zeroes newly allocated space.
475  */
476 void *
477 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
478 {
479 	void *ret;
480 
481 	if ((ret = realloc(ptr, newsize)) == NULL) {
482 		(void) no_memory(hdl);
483 		return (NULL);
484 	}
485 
486 	bzero((char *)ret + oldsize, (newsize - oldsize));
487 	return (ret);
488 }
489 
490 /*
491  * A safe form of strdup() which will die if the allocation fails.
492  */
493 char *
494 zfs_strdup(libzfs_handle_t *hdl, const char *str)
495 {
496 	char *ret;
497 
498 	if ((ret = strdup(str)) == NULL)
499 		(void) no_memory(hdl);
500 
501 	return (ret);
502 }
503 
504 /*
505  * Convert a number to an appropriately human-readable output.
506  */
507 void
508 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
509 {
510 	uint64_t n = num;
511 	int index = 0;
512 	char u;
513 
514 	while (n >= 1024) {
515 		n /= 1024;
516 		index++;
517 	}
518 
519 	u = " KMGTPE"[index];
520 
521 	if (index == 0) {
522 		(void) snprintf(buf, buflen, "%llu", n);
523 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
524 		/*
525 		 * If this is an even multiple of the base, always display
526 		 * without any decimal precision.
527 		 */
528 		(void) snprintf(buf, buflen, "%llu%c", n, u);
529 	} else {
530 		/*
531 		 * We want to choose a precision that reflects the best choice
532 		 * for fitting in 5 characters.  This can get rather tricky when
533 		 * we have numbers that are very close to an order of magnitude.
534 		 * For example, when displaying 10239 (which is really 9.999K),
535 		 * we want only a single place of precision for 10.0K.  We could
536 		 * develop some complex heuristics for this, but it's much
537 		 * easier just to try each combination in turn.
538 		 */
539 		int i;
540 		for (i = 2; i >= 0; i--) {
541 			if (snprintf(buf, buflen, "%.*f%c", i,
542 			    (double)num / (1ULL << 10 * index), u) <= 5)
543 				break;
544 		}
545 	}
546 }
547 
548 void
549 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
550 {
551 	hdl->libzfs_printerr = printerr;
552 }
553 
554 libzfs_handle_t *
555 libzfs_init(void)
556 {
557 	libzfs_handle_t *hdl;
558 
559 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
560 		return (NULL);
561 	}
562 
563 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
564 		free(hdl);
565 		return (NULL);
566 	}
567 
568 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
569 		(void) close(hdl->libzfs_fd);
570 		free(hdl);
571 		return (NULL);
572 	}
573 
574 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
575 
576 	zfs_prop_init();
577 	zpool_prop_init();
578 	libzfs_mnttab_init(hdl);
579 
580 	return (hdl);
581 }
582 
583 void
584 libzfs_fini(libzfs_handle_t *hdl)
585 {
586 	(void) close(hdl->libzfs_fd);
587 	if (hdl->libzfs_mnttab)
588 		(void) fclose(hdl->libzfs_mnttab);
589 	if (hdl->libzfs_sharetab)
590 		(void) fclose(hdl->libzfs_sharetab);
591 	zfs_uninit_libshare(hdl);
592 	if (hdl->libzfs_log_str)
593 		(void) free(hdl->libzfs_log_str);
594 	zpool_free_handles(hdl);
595 	namespace_clear(hdl);
596 	libzfs_mnttab_fini(hdl);
597 	free(hdl);
598 }
599 
600 libzfs_handle_t *
601 zpool_get_handle(zpool_handle_t *zhp)
602 {
603 	return (zhp->zpool_hdl);
604 }
605 
606 libzfs_handle_t *
607 zfs_get_handle(zfs_handle_t *zhp)
608 {
609 	return (zhp->zfs_hdl);
610 }
611 
612 zpool_handle_t *
613 zfs_get_pool_handle(const zfs_handle_t *zhp)
614 {
615 	return (zhp->zpool_hdl);
616 }
617 
618 /*
619  * Given a name, determine whether or not it's a valid path
620  * (starts with '/' or "./").  If so, walk the mnttab trying
621  * to match the device number.  If not, treat the path as an
622  * fs/vol/snap name.
623  */
624 zfs_handle_t *
625 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
626 {
627 	struct stat64 statbuf;
628 	struct extmnttab entry;
629 	int ret;
630 
631 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
632 		/*
633 		 * It's not a valid path, assume it's a name of type 'argtype'.
634 		 */
635 		return (zfs_open(hdl, path, argtype));
636 	}
637 
638 	if (stat64(path, &statbuf) != 0) {
639 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
640 		return (NULL);
641 	}
642 
643 	rewind(hdl->libzfs_mnttab);
644 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
645 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
646 		    statbuf.st_dev) {
647 			break;
648 		}
649 	}
650 	if (ret != 0) {
651 		return (NULL);
652 	}
653 
654 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
655 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
656 		    path);
657 		return (NULL);
658 	}
659 
660 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
661 }
662 
663 /*
664  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
665  * an ioctl().
666  */
667 int
668 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
669 {
670 	if (len == 0)
671 		len = 2048;
672 	zc->zc_nvlist_dst_size = len;
673 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
674 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
675 		return (-1);
676 
677 	return (0);
678 }
679 
680 /*
681  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
682  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
683  * filled in by the kernel to indicate the actual required size.
684  */
685 int
686 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
687 {
688 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
689 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
690 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
691 	    == NULL)
692 		return (-1);
693 
694 	return (0);
695 }
696 
697 /*
698  * Called to free the src and dst nvlists stored in the command structure.
699  */
700 void
701 zcmd_free_nvlists(zfs_cmd_t *zc)
702 {
703 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
704 	free((void *)(uintptr_t)zc->zc_nvlist_src);
705 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
706 }
707 
708 static int
709 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
710     nvlist_t *nvl)
711 {
712 	char *packed;
713 	size_t len;
714 
715 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
716 
717 	if ((packed = zfs_alloc(hdl, len)) == NULL)
718 		return (-1);
719 
720 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
721 
722 	*outnv = (uint64_t)(uintptr_t)packed;
723 	*outlen = len;
724 
725 	return (0);
726 }
727 
728 int
729 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
730 {
731 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
732 	    &zc->zc_nvlist_conf_size, nvl));
733 }
734 
735 int
736 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
737 {
738 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
739 	    &zc->zc_nvlist_src_size, nvl));
740 }
741 
742 /*
743  * Unpacks an nvlist from the ZFS ioctl command structure.
744  */
745 int
746 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
747 {
748 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
749 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
750 		return (no_memory(hdl));
751 
752 	return (0);
753 }
754 
755 int
756 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
757 {
758 	int error;
759 
760 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
761 	error = ioctl(hdl->libzfs_fd, request, zc);
762 	if (hdl->libzfs_log_str) {
763 		free(hdl->libzfs_log_str);
764 		hdl->libzfs_log_str = NULL;
765 	}
766 	zc->zc_history = 0;
767 
768 	return (error);
769 }
770 
771 /*
772  * ================================================================
773  * API shared by zfs and zpool property management
774  * ================================================================
775  */
776 
777 static void
778 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
779 {
780 	zprop_list_t *pl = cbp->cb_proplist;
781 	int i;
782 	char *title;
783 	size_t len;
784 
785 	cbp->cb_first = B_FALSE;
786 	if (cbp->cb_scripted)
787 		return;
788 
789 	/*
790 	 * Start with the length of the column headers.
791 	 */
792 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
793 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
794 	    "PROPERTY"));
795 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
796 	    "VALUE"));
797 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
798 	    "SOURCE"));
799 
800 	/* first property is always NAME */
801 	assert(cbp->cb_proplist->pl_prop ==
802 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
803 
804 	/*
805 	 * Go through and calculate the widths for each column.  For the
806 	 * 'source' column, we kludge it up by taking the worst-case scenario of
807 	 * inheriting from the longest name.  This is acceptable because in the
808 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
809 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
810 	 * if the name of the property is much longer the any values we find.
811 	 */
812 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
813 		/*
814 		 * 'PROPERTY' column
815 		 */
816 		if (pl->pl_prop != ZPROP_INVAL) {
817 			const char *propname = (type == ZFS_TYPE_POOL) ?
818 			    zpool_prop_to_name(pl->pl_prop) :
819 			    zfs_prop_to_name(pl->pl_prop);
820 
821 			len = strlen(propname);
822 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
823 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
824 		} else {
825 			len = strlen(pl->pl_user_prop);
826 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
827 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
828 		}
829 
830 		/*
831 		 * 'VALUE' column.  The first property is always the 'name'
832 		 * property that was tacked on either by /sbin/zfs's
833 		 * zfs_do_get() or when calling zprop_expand_list(), so we
834 		 * ignore its width.  If the user specified the name property
835 		 * to display, then it will be later in the list in any case.
836 		 */
837 		if (pl != cbp->cb_proplist &&
838 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
839 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
840 
841 		/*
842 		 * 'NAME' and 'SOURCE' columns
843 		 */
844 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
845 		    ZFS_PROP_NAME) &&
846 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
847 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
848 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
849 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
850 		}
851 	}
852 
853 	/*
854 	 * Now go through and print the headers.
855 	 */
856 	for (i = 0; i < 4; i++) {
857 		switch (cbp->cb_columns[i]) {
858 		case GET_COL_NAME:
859 			title = dgettext(TEXT_DOMAIN, "NAME");
860 			break;
861 		case GET_COL_PROPERTY:
862 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
863 			break;
864 		case GET_COL_VALUE:
865 			title = dgettext(TEXT_DOMAIN, "VALUE");
866 			break;
867 		case GET_COL_SOURCE:
868 			title = dgettext(TEXT_DOMAIN, "SOURCE");
869 			break;
870 		default:
871 			title = NULL;
872 		}
873 
874 		if (title != NULL) {
875 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
876 				(void) printf("%s", title);
877 			else
878 				(void) printf("%-*s  ",
879 				    cbp->cb_colwidths[cbp->cb_columns[i]],
880 				    title);
881 		}
882 	}
883 	(void) printf("\n");
884 }
885 
886 /*
887  * Display a single line of output, according to the settings in the callback
888  * structure.
889  */
890 void
891 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
892     const char *propname, const char *value, zprop_source_t sourcetype,
893     const char *source)
894 {
895 	int i;
896 	const char *str;
897 	char buf[128];
898 
899 	/*
900 	 * Ignore those source types that the user has chosen to ignore.
901 	 */
902 	if ((sourcetype & cbp->cb_sources) == 0)
903 		return;
904 
905 	if (cbp->cb_first)
906 		zprop_print_headers(cbp, cbp->cb_type);
907 
908 	for (i = 0; i < 4; i++) {
909 		switch (cbp->cb_columns[i]) {
910 		case GET_COL_NAME:
911 			str = name;
912 			break;
913 
914 		case GET_COL_PROPERTY:
915 			str = propname;
916 			break;
917 
918 		case GET_COL_VALUE:
919 			str = value;
920 			break;
921 
922 		case GET_COL_SOURCE:
923 			switch (sourcetype) {
924 			case ZPROP_SRC_NONE:
925 				str = "-";
926 				break;
927 
928 			case ZPROP_SRC_DEFAULT:
929 				str = "default";
930 				break;
931 
932 			case ZPROP_SRC_LOCAL:
933 				str = "local";
934 				break;
935 
936 			case ZPROP_SRC_TEMPORARY:
937 				str = "temporary";
938 				break;
939 
940 			case ZPROP_SRC_INHERITED:
941 				(void) snprintf(buf, sizeof (buf),
942 				    "inherited from %s", source);
943 				str = buf;
944 				break;
945 			}
946 			break;
947 
948 		default:
949 			continue;
950 		}
951 
952 		if (cbp->cb_columns[i + 1] == 0)
953 			(void) printf("%s", str);
954 		else if (cbp->cb_scripted)
955 			(void) printf("%s\t", str);
956 		else
957 			(void) printf("%-*s  ",
958 			    cbp->cb_colwidths[cbp->cb_columns[i]],
959 			    str);
960 
961 	}
962 
963 	(void) printf("\n");
964 }
965 
966 /*
967  * Given a numeric suffix, convert the value into a number of bits that the
968  * resulting value must be shifted.
969  */
970 static int
971 str2shift(libzfs_handle_t *hdl, const char *buf)
972 {
973 	const char *ends = "BKMGTPEZ";
974 	int i;
975 
976 	if (buf[0] == '\0')
977 		return (0);
978 	for (i = 0; i < strlen(ends); i++) {
979 		if (toupper(buf[0]) == ends[i])
980 			break;
981 	}
982 	if (i == strlen(ends)) {
983 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984 		    "invalid numeric suffix '%s'"), buf);
985 		return (-1);
986 	}
987 
988 	/*
989 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
990 	 * allow 'BB' - that's just weird.
991 	 */
992 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
993 	    toupper(buf[0]) != 'B'))
994 		return (10*i);
995 
996 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997 	    "invalid numeric suffix '%s'"), buf);
998 	return (-1);
999 }
1000 
1001 /*
1002  * Convert a string of the form '100G' into a real number.  Used when setting
1003  * properties or creating a volume.  'buf' is used to place an extended error
1004  * message for the caller to use.
1005  */
1006 int
1007 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1008 {
1009 	char *end;
1010 	int shift;
1011 
1012 	*num = 0;
1013 
1014 	/* Check to see if this looks like a number.  */
1015 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1016 		if (hdl)
1017 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 			    "bad numeric value '%s'"), value);
1019 		return (-1);
1020 	}
1021 
1022 	/* Rely on stroull() to process the numeric portion.  */
1023 	errno = 0;
1024 	*num = strtoull(value, &end, 10);
1025 
1026 	/*
1027 	 * Check for ERANGE, which indicates that the value is too large to fit
1028 	 * in a 64-bit value.
1029 	 */
1030 	if (errno == ERANGE) {
1031 		if (hdl)
1032 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1033 			    "numeric value is too large"));
1034 		return (-1);
1035 	}
1036 
1037 	/*
1038 	 * If we have a decimal value, then do the computation with floating
1039 	 * point arithmetic.  Otherwise, use standard arithmetic.
1040 	 */
1041 	if (*end == '.') {
1042 		double fval = strtod(value, &end);
1043 
1044 		if ((shift = str2shift(hdl, end)) == -1)
1045 			return (-1);
1046 
1047 		fval *= pow(2, shift);
1048 
1049 		if (fval > UINT64_MAX) {
1050 			if (hdl)
1051 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1052 				    "numeric value is too large"));
1053 			return (-1);
1054 		}
1055 
1056 		*num = (uint64_t)fval;
1057 	} else {
1058 		if ((shift = str2shift(hdl, end)) == -1)
1059 			return (-1);
1060 
1061 		/* Check for overflow */
1062 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1063 			if (hdl)
1064 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1065 				    "numeric value is too large"));
1066 			return (-1);
1067 		}
1068 
1069 		*num <<= shift;
1070 	}
1071 
1072 	return (0);
1073 }
1074 
1075 /*
1076  * Given a propname=value nvpair to set, parse any numeric properties
1077  * (index, boolean, etc) if they are specified as strings and add the
1078  * resulting nvpair to the returned nvlist.
1079  *
1080  * At the DSL layer, all properties are either 64-bit numbers or strings.
1081  * We want the user to be able to ignore this fact and specify properties
1082  * as native values (numbers, for example) or as strings (to simplify
1083  * command line utilities).  This also handles converting index types
1084  * (compression, checksum, etc) from strings to their on-disk index.
1085  */
1086 int
1087 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1088     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1089     const char *errbuf)
1090 {
1091 	data_type_t datatype = nvpair_type(elem);
1092 	zprop_type_t proptype;
1093 	const char *propname;
1094 	char *value;
1095 	boolean_t isnone = B_FALSE;
1096 
1097 	if (type == ZFS_TYPE_POOL) {
1098 		proptype = zpool_prop_get_type(prop);
1099 		propname = zpool_prop_to_name(prop);
1100 	} else {
1101 		proptype = zfs_prop_get_type(prop);
1102 		propname = zfs_prop_to_name(prop);
1103 	}
1104 
1105 	/*
1106 	 * Convert any properties to the internal DSL value types.
1107 	 */
1108 	*svalp = NULL;
1109 	*ivalp = 0;
1110 
1111 	switch (proptype) {
1112 	case PROP_TYPE_STRING:
1113 		if (datatype != DATA_TYPE_STRING) {
1114 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115 			    "'%s' must be a string"), nvpair_name(elem));
1116 			goto error;
1117 		}
1118 		(void) nvpair_value_string(elem, svalp);
1119 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1120 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1121 			    "'%s' is too long"), nvpair_name(elem));
1122 			goto error;
1123 		}
1124 		break;
1125 
1126 	case PROP_TYPE_NUMBER:
1127 		if (datatype == DATA_TYPE_STRING) {
1128 			(void) nvpair_value_string(elem, &value);
1129 			if (strcmp(value, "none") == 0) {
1130 				isnone = B_TRUE;
1131 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1132 			    != 0) {
1133 				goto error;
1134 			}
1135 		} else if (datatype == DATA_TYPE_UINT64) {
1136 			(void) nvpair_value_uint64(elem, ivalp);
1137 		} else {
1138 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1139 			    "'%s' must be a number"), nvpair_name(elem));
1140 			goto error;
1141 		}
1142 
1143 		/*
1144 		 * Quota special: force 'none' and don't allow 0.
1145 		 */
1146 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1147 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1148 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 			    "use 'none' to disable quota/refquota"));
1150 			goto error;
1151 		}
1152 		break;
1153 
1154 	case PROP_TYPE_INDEX:
1155 		if (datatype != DATA_TYPE_STRING) {
1156 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1157 			    "'%s' must be a string"), nvpair_name(elem));
1158 			goto error;
1159 		}
1160 
1161 		(void) nvpair_value_string(elem, &value);
1162 
1163 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1164 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1165 			    "'%s' must be one of '%s'"), propname,
1166 			    zprop_values(prop, type));
1167 			goto error;
1168 		}
1169 		break;
1170 
1171 	default:
1172 		abort();
1173 	}
1174 
1175 	/*
1176 	 * Add the result to our return set of properties.
1177 	 */
1178 	if (*svalp != NULL) {
1179 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1180 			(void) no_memory(hdl);
1181 			return (-1);
1182 		}
1183 	} else {
1184 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1185 			(void) no_memory(hdl);
1186 			return (-1);
1187 		}
1188 	}
1189 
1190 	return (0);
1191 error:
1192 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1193 	return (-1);
1194 }
1195 
1196 static int
1197 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1198     zfs_type_t type)
1199 {
1200 	int prop;
1201 	zprop_list_t *entry;
1202 
1203 	prop = zprop_name_to_prop(propname, type);
1204 
1205 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1206 		prop = ZPROP_INVAL;
1207 
1208 	/*
1209 	 * When no property table entry can be found, return failure if
1210 	 * this is a pool property or if this isn't a user-defined
1211 	 * dataset property,
1212 	 */
1213 	if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1214 	    !zfs_prop_user(propname))) {
1215 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1216 		    "invalid property '%s'"), propname);
1217 		return (zfs_error(hdl, EZFS_BADPROP,
1218 		    dgettext(TEXT_DOMAIN, "bad property list")));
1219 	}
1220 
1221 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1222 		return (-1);
1223 
1224 	entry->pl_prop = prop;
1225 	if (prop == ZPROP_INVAL) {
1226 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) {
1227 			free(entry);
1228 			return (-1);
1229 		}
1230 		entry->pl_width = strlen(propname);
1231 	} else {
1232 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1233 		    type);
1234 	}
1235 
1236 	*listp = entry;
1237 
1238 	return (0);
1239 }
1240 
1241 /*
1242  * Given a comma-separated list of properties, construct a property list
1243  * containing both user-defined and native properties.  This function will
1244  * return a NULL list if 'all' is specified, which can later be expanded
1245  * by zprop_expand_list().
1246  */
1247 int
1248 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1249     zfs_type_t type)
1250 {
1251 	*listp = NULL;
1252 
1253 	/*
1254 	 * If 'all' is specified, return a NULL list.
1255 	 */
1256 	if (strcmp(props, "all") == 0)
1257 		return (0);
1258 
1259 	/*
1260 	 * If no props were specified, return an error.
1261 	 */
1262 	if (props[0] == '\0') {
1263 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1264 		    "no properties specified"));
1265 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1266 		    "bad property list")));
1267 	}
1268 
1269 	/*
1270 	 * It would be nice to use getsubopt() here, but the inclusion of column
1271 	 * aliases makes this more effort than it's worth.
1272 	 */
1273 	while (*props != '\0') {
1274 		size_t len;
1275 		char *p;
1276 		char c;
1277 
1278 		if ((p = strchr(props, ',')) == NULL) {
1279 			len = strlen(props);
1280 			p = props + len;
1281 		} else {
1282 			len = p - props;
1283 		}
1284 
1285 		/*
1286 		 * Check for empty options.
1287 		 */
1288 		if (len == 0) {
1289 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1290 			    "empty property name"));
1291 			return (zfs_error(hdl, EZFS_BADPROP,
1292 			    dgettext(TEXT_DOMAIN, "bad property list")));
1293 		}
1294 
1295 		/*
1296 		 * Check all regular property names.
1297 		 */
1298 		c = props[len];
1299 		props[len] = '\0';
1300 
1301 		if (strcmp(props, "space") == 0) {
1302 			static char *spaceprops[] = {
1303 				"name", "avail", "used", "usedbysnapshots",
1304 				"usedbydataset", "usedbyrefreservation",
1305 				"usedbychildren", NULL
1306 			};
1307 			int i;
1308 
1309 			for (i = 0; spaceprops[i]; i++) {
1310 				if (addlist(hdl, spaceprops[i], listp, type))
1311 					return (-1);
1312 				listp = &(*listp)->pl_next;
1313 			}
1314 		} else {
1315 			if (addlist(hdl, props, listp, type))
1316 				return (-1);
1317 			listp = &(*listp)->pl_next;
1318 		}
1319 
1320 		props = p;
1321 		if (c == ',')
1322 			props++;
1323 	}
1324 
1325 	return (0);
1326 }
1327 
1328 void
1329 zprop_free_list(zprop_list_t *pl)
1330 {
1331 	zprop_list_t *next;
1332 
1333 	while (pl != NULL) {
1334 		next = pl->pl_next;
1335 		free(pl->pl_user_prop);
1336 		free(pl);
1337 		pl = next;
1338 	}
1339 }
1340 
1341 typedef struct expand_data {
1342 	zprop_list_t	**last;
1343 	libzfs_handle_t	*hdl;
1344 	zfs_type_t type;
1345 } expand_data_t;
1346 
1347 int
1348 zprop_expand_list_cb(int prop, void *cb)
1349 {
1350 	zprop_list_t *entry;
1351 	expand_data_t *edp = cb;
1352 
1353 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1354 		return (ZPROP_INVAL);
1355 
1356 	entry->pl_prop = prop;
1357 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1358 	entry->pl_all = B_TRUE;
1359 
1360 	*(edp->last) = entry;
1361 	edp->last = &entry->pl_next;
1362 
1363 	return (ZPROP_CONT);
1364 }
1365 
1366 int
1367 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1368 {
1369 	zprop_list_t *entry;
1370 	zprop_list_t **last;
1371 	expand_data_t exp;
1372 
1373 	if (*plp == NULL) {
1374 		/*
1375 		 * If this is the very first time we've been called for an 'all'
1376 		 * specification, expand the list to include all native
1377 		 * properties.
1378 		 */
1379 		last = plp;
1380 
1381 		exp.last = last;
1382 		exp.hdl = hdl;
1383 		exp.type = type;
1384 
1385 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1386 		    B_FALSE, type) == ZPROP_INVAL)
1387 			return (-1);
1388 
1389 		/*
1390 		 * Add 'name' to the beginning of the list, which is handled
1391 		 * specially.
1392 		 */
1393 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1394 			return (-1);
1395 
1396 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1397 		    ZFS_PROP_NAME;
1398 		entry->pl_width = zprop_width(entry->pl_prop,
1399 		    &entry->pl_fixed, type);
1400 		entry->pl_all = B_TRUE;
1401 		entry->pl_next = *plp;
1402 		*plp = entry;
1403 	}
1404 	return (0);
1405 }
1406 
1407 int
1408 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1409     zfs_type_t type)
1410 {
1411 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1412 }
1413