xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_util.c (revision b695575577bae0337af339d76949713bfe1c9013)
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 	case EAGAIN:
368 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
369 		    "pool I/O is currently suspended"));
370 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
371 		break;
372 	default:
373 		zfs_error_aux(hdl, strerror(errno));
374 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
375 		break;
376 	}
377 
378 	va_end(ap);
379 	return (-1);
380 }
381 
382 int
383 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
384 {
385 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
386 }
387 
388 /*PRINTFLIKE3*/
389 int
390 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
391 {
392 	va_list ap;
393 
394 	va_start(ap, fmt);
395 
396 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
397 		va_end(ap);
398 		return (-1);
399 	}
400 
401 	switch (error) {
402 	case ENODEV:
403 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
404 		break;
405 
406 	case ENOENT:
407 		zfs_error_aux(hdl,
408 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
409 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
410 		break;
411 
412 	case EEXIST:
413 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
414 		    "pool already exists"));
415 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
416 		break;
417 
418 	case EBUSY:
419 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
420 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
421 		break;
422 
423 	case ENXIO:
424 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
425 		    "one or more devices is currently unavailable"));
426 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
427 		break;
428 
429 	case ENAMETOOLONG:
430 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
431 		break;
432 
433 	case ENOTSUP:
434 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
435 		break;
436 
437 	case EINVAL:
438 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
439 		break;
440 
441 	case ENOSPC:
442 	case EDQUOT:
443 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
444 		return (-1);
445 	case EAGAIN:
446 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
447 		    "pool I/O is currently suspended"));
448 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
449 		break;
450 
451 	default:
452 		zfs_error_aux(hdl, strerror(error));
453 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
454 	}
455 
456 	va_end(ap);
457 	return (-1);
458 }
459 
460 /*
461  * Display an out of memory error message and abort the current program.
462  */
463 int
464 no_memory(libzfs_handle_t *hdl)
465 {
466 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
467 }
468 
469 /*
470  * A safe form of malloc() which will die if the allocation fails.
471  */
472 void *
473 zfs_alloc(libzfs_handle_t *hdl, size_t size)
474 {
475 	void *data;
476 
477 	if ((data = calloc(1, size)) == NULL)
478 		(void) no_memory(hdl);
479 
480 	return (data);
481 }
482 
483 /*
484  * A safe form of realloc(), which also zeroes newly allocated space.
485  */
486 void *
487 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
488 {
489 	void *ret;
490 
491 	if ((ret = realloc(ptr, newsize)) == NULL) {
492 		(void) no_memory(hdl);
493 		return (NULL);
494 	}
495 
496 	bzero((char *)ret + oldsize, (newsize - oldsize));
497 	return (ret);
498 }
499 
500 /*
501  * A safe form of strdup() which will die if the allocation fails.
502  */
503 char *
504 zfs_strdup(libzfs_handle_t *hdl, const char *str)
505 {
506 	char *ret;
507 
508 	if ((ret = strdup(str)) == NULL)
509 		(void) no_memory(hdl);
510 
511 	return (ret);
512 }
513 
514 /*
515  * Convert a number to an appropriately human-readable output.
516  */
517 void
518 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
519 {
520 	uint64_t n = num;
521 	int index = 0;
522 	char u;
523 
524 	while (n >= 1024) {
525 		n /= 1024;
526 		index++;
527 	}
528 
529 	u = " KMGTPE"[index];
530 
531 	if (index == 0) {
532 		(void) snprintf(buf, buflen, "%llu", n);
533 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
534 		/*
535 		 * If this is an even multiple of the base, always display
536 		 * without any decimal precision.
537 		 */
538 		(void) snprintf(buf, buflen, "%llu%c", n, u);
539 	} else {
540 		/*
541 		 * We want to choose a precision that reflects the best choice
542 		 * for fitting in 5 characters.  This can get rather tricky when
543 		 * we have numbers that are very close to an order of magnitude.
544 		 * For example, when displaying 10239 (which is really 9.999K),
545 		 * we want only a single place of precision for 10.0K.  We could
546 		 * develop some complex heuristics for this, but it's much
547 		 * easier just to try each combination in turn.
548 		 */
549 		int i;
550 		for (i = 2; i >= 0; i--) {
551 			if (snprintf(buf, buflen, "%.*f%c", i,
552 			    (double)num / (1ULL << 10 * index), u) <= 5)
553 				break;
554 		}
555 	}
556 }
557 
558 void
559 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
560 {
561 	hdl->libzfs_printerr = printerr;
562 }
563 
564 libzfs_handle_t *
565 libzfs_init(void)
566 {
567 	libzfs_handle_t *hdl;
568 
569 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
570 		return (NULL);
571 	}
572 
573 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
574 		free(hdl);
575 		return (NULL);
576 	}
577 
578 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
579 		(void) close(hdl->libzfs_fd);
580 		free(hdl);
581 		return (NULL);
582 	}
583 
584 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
585 
586 	zfs_prop_init();
587 	zpool_prop_init();
588 	libzfs_mnttab_init(hdl);
589 
590 	return (hdl);
591 }
592 
593 void
594 libzfs_fini(libzfs_handle_t *hdl)
595 {
596 	(void) close(hdl->libzfs_fd);
597 	if (hdl->libzfs_mnttab)
598 		(void) fclose(hdl->libzfs_mnttab);
599 	if (hdl->libzfs_sharetab)
600 		(void) fclose(hdl->libzfs_sharetab);
601 	zfs_uninit_libshare(hdl);
602 	if (hdl->libzfs_log_str)
603 		(void) free(hdl->libzfs_log_str);
604 	zpool_free_handles(hdl);
605 	namespace_clear(hdl);
606 	libzfs_mnttab_fini(hdl);
607 	free(hdl);
608 }
609 
610 libzfs_handle_t *
611 zpool_get_handle(zpool_handle_t *zhp)
612 {
613 	return (zhp->zpool_hdl);
614 }
615 
616 libzfs_handle_t *
617 zfs_get_handle(zfs_handle_t *zhp)
618 {
619 	return (zhp->zfs_hdl);
620 }
621 
622 zpool_handle_t *
623 zfs_get_pool_handle(const zfs_handle_t *zhp)
624 {
625 	return (zhp->zpool_hdl);
626 }
627 
628 /*
629  * Given a name, determine whether or not it's a valid path
630  * (starts with '/' or "./").  If so, walk the mnttab trying
631  * to match the device number.  If not, treat the path as an
632  * fs/vol/snap name.
633  */
634 zfs_handle_t *
635 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
636 {
637 	struct stat64 statbuf;
638 	struct extmnttab entry;
639 	int ret;
640 
641 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
642 		/*
643 		 * It's not a valid path, assume it's a name of type 'argtype'.
644 		 */
645 		return (zfs_open(hdl, path, argtype));
646 	}
647 
648 	if (stat64(path, &statbuf) != 0) {
649 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
650 		return (NULL);
651 	}
652 
653 	rewind(hdl->libzfs_mnttab);
654 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
655 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
656 		    statbuf.st_dev) {
657 			break;
658 		}
659 	}
660 	if (ret != 0) {
661 		return (NULL);
662 	}
663 
664 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
665 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
666 		    path);
667 		return (NULL);
668 	}
669 
670 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
671 }
672 
673 /*
674  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
675  * an ioctl().
676  */
677 int
678 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
679 {
680 	if (len == 0)
681 		len = 2048;
682 	zc->zc_nvlist_dst_size = len;
683 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
684 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
685 		return (-1);
686 
687 	return (0);
688 }
689 
690 /*
691  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
692  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
693  * filled in by the kernel to indicate the actual required size.
694  */
695 int
696 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
697 {
698 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
699 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
700 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
701 	    == NULL)
702 		return (-1);
703 
704 	return (0);
705 }
706 
707 /*
708  * Called to free the src and dst nvlists stored in the command structure.
709  */
710 void
711 zcmd_free_nvlists(zfs_cmd_t *zc)
712 {
713 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
714 	free((void *)(uintptr_t)zc->zc_nvlist_src);
715 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
716 }
717 
718 static int
719 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
720     nvlist_t *nvl)
721 {
722 	char *packed;
723 	size_t len;
724 
725 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
726 
727 	if ((packed = zfs_alloc(hdl, len)) == NULL)
728 		return (-1);
729 
730 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
731 
732 	*outnv = (uint64_t)(uintptr_t)packed;
733 	*outlen = len;
734 
735 	return (0);
736 }
737 
738 int
739 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
740 {
741 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
742 	    &zc->zc_nvlist_conf_size, nvl));
743 }
744 
745 int
746 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
747 {
748 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
749 	    &zc->zc_nvlist_src_size, nvl));
750 }
751 
752 /*
753  * Unpacks an nvlist from the ZFS ioctl command structure.
754  */
755 int
756 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
757 {
758 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
759 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
760 		return (no_memory(hdl));
761 
762 	return (0);
763 }
764 
765 int
766 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
767 {
768 	int error;
769 
770 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
771 	error = ioctl(hdl->libzfs_fd, request, zc);
772 	if (hdl->libzfs_log_str) {
773 		free(hdl->libzfs_log_str);
774 		hdl->libzfs_log_str = NULL;
775 	}
776 	zc->zc_history = 0;
777 
778 	return (error);
779 }
780 
781 /*
782  * ================================================================
783  * API shared by zfs and zpool property management
784  * ================================================================
785  */
786 
787 static void
788 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
789 {
790 	zprop_list_t *pl = cbp->cb_proplist;
791 	int i;
792 	char *title;
793 	size_t len;
794 
795 	cbp->cb_first = B_FALSE;
796 	if (cbp->cb_scripted)
797 		return;
798 
799 	/*
800 	 * Start with the length of the column headers.
801 	 */
802 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
803 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
804 	    "PROPERTY"));
805 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
806 	    "VALUE"));
807 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
808 	    "SOURCE"));
809 
810 	/* first property is always NAME */
811 	assert(cbp->cb_proplist->pl_prop ==
812 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
813 
814 	/*
815 	 * Go through and calculate the widths for each column.  For the
816 	 * 'source' column, we kludge it up by taking the worst-case scenario of
817 	 * inheriting from the longest name.  This is acceptable because in the
818 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
819 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
820 	 * if the name of the property is much longer the any values we find.
821 	 */
822 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
823 		/*
824 		 * 'PROPERTY' column
825 		 */
826 		if (pl->pl_prop != ZPROP_INVAL) {
827 			const char *propname = (type == ZFS_TYPE_POOL) ?
828 			    zpool_prop_to_name(pl->pl_prop) :
829 			    zfs_prop_to_name(pl->pl_prop);
830 
831 			len = strlen(propname);
832 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
833 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
834 		} else {
835 			len = strlen(pl->pl_user_prop);
836 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
837 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
838 		}
839 
840 		/*
841 		 * 'VALUE' column.  The first property is always the 'name'
842 		 * property that was tacked on either by /sbin/zfs's
843 		 * zfs_do_get() or when calling zprop_expand_list(), so we
844 		 * ignore its width.  If the user specified the name property
845 		 * to display, then it will be later in the list in any case.
846 		 */
847 		if (pl != cbp->cb_proplist &&
848 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
849 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
850 
851 		/*
852 		 * 'NAME' and 'SOURCE' columns
853 		 */
854 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
855 		    ZFS_PROP_NAME) &&
856 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
857 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
858 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
859 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
860 		}
861 	}
862 
863 	/*
864 	 * Now go through and print the headers.
865 	 */
866 	for (i = 0; i < 4; i++) {
867 		switch (cbp->cb_columns[i]) {
868 		case GET_COL_NAME:
869 			title = dgettext(TEXT_DOMAIN, "NAME");
870 			break;
871 		case GET_COL_PROPERTY:
872 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
873 			break;
874 		case GET_COL_VALUE:
875 			title = dgettext(TEXT_DOMAIN, "VALUE");
876 			break;
877 		case GET_COL_SOURCE:
878 			title = dgettext(TEXT_DOMAIN, "SOURCE");
879 			break;
880 		default:
881 			title = NULL;
882 		}
883 
884 		if (title != NULL) {
885 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
886 				(void) printf("%s", title);
887 			else
888 				(void) printf("%-*s  ",
889 				    cbp->cb_colwidths[cbp->cb_columns[i]],
890 				    title);
891 		}
892 	}
893 	(void) printf("\n");
894 }
895 
896 /*
897  * Display a single line of output, according to the settings in the callback
898  * structure.
899  */
900 void
901 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
902     const char *propname, const char *value, zprop_source_t sourcetype,
903     const char *source)
904 {
905 	int i;
906 	const char *str;
907 	char buf[128];
908 
909 	/*
910 	 * Ignore those source types that the user has chosen to ignore.
911 	 */
912 	if ((sourcetype & cbp->cb_sources) == 0)
913 		return;
914 
915 	if (cbp->cb_first)
916 		zprop_print_headers(cbp, cbp->cb_type);
917 
918 	for (i = 0; i < 4; i++) {
919 		switch (cbp->cb_columns[i]) {
920 		case GET_COL_NAME:
921 			str = name;
922 			break;
923 
924 		case GET_COL_PROPERTY:
925 			str = propname;
926 			break;
927 
928 		case GET_COL_VALUE:
929 			str = value;
930 			break;
931 
932 		case GET_COL_SOURCE:
933 			switch (sourcetype) {
934 			case ZPROP_SRC_NONE:
935 				str = "-";
936 				break;
937 
938 			case ZPROP_SRC_DEFAULT:
939 				str = "default";
940 				break;
941 
942 			case ZPROP_SRC_LOCAL:
943 				str = "local";
944 				break;
945 
946 			case ZPROP_SRC_TEMPORARY:
947 				str = "temporary";
948 				break;
949 
950 			case ZPROP_SRC_INHERITED:
951 				(void) snprintf(buf, sizeof (buf),
952 				    "inherited from %s", source);
953 				str = buf;
954 				break;
955 			}
956 			break;
957 
958 		default:
959 			continue;
960 		}
961 
962 		if (cbp->cb_columns[i + 1] == 0)
963 			(void) printf("%s", str);
964 		else if (cbp->cb_scripted)
965 			(void) printf("%s\t", str);
966 		else
967 			(void) printf("%-*s  ",
968 			    cbp->cb_colwidths[cbp->cb_columns[i]],
969 			    str);
970 
971 	}
972 
973 	(void) printf("\n");
974 }
975 
976 /*
977  * Given a numeric suffix, convert the value into a number of bits that the
978  * resulting value must be shifted.
979  */
980 static int
981 str2shift(libzfs_handle_t *hdl, const char *buf)
982 {
983 	const char *ends = "BKMGTPEZ";
984 	int i;
985 
986 	if (buf[0] == '\0')
987 		return (0);
988 	for (i = 0; i < strlen(ends); i++) {
989 		if (toupper(buf[0]) == ends[i])
990 			break;
991 	}
992 	if (i == strlen(ends)) {
993 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
994 		    "invalid numeric suffix '%s'"), buf);
995 		return (-1);
996 	}
997 
998 	/*
999 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
1000 	 * allow 'BB' - that's just weird.
1001 	 */
1002 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1003 	    toupper(buf[0]) != 'B'))
1004 		return (10*i);
1005 
1006 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1007 	    "invalid numeric suffix '%s'"), buf);
1008 	return (-1);
1009 }
1010 
1011 /*
1012  * Convert a string of the form '100G' into a real number.  Used when setting
1013  * properties or creating a volume.  'buf' is used to place an extended error
1014  * message for the caller to use.
1015  */
1016 int
1017 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1018 {
1019 	char *end;
1020 	int shift;
1021 
1022 	*num = 0;
1023 
1024 	/* Check to see if this looks like a number.  */
1025 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1026 		if (hdl)
1027 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1028 			    "bad numeric value '%s'"), value);
1029 		return (-1);
1030 	}
1031 
1032 	/* Rely on stroull() to process the numeric portion.  */
1033 	errno = 0;
1034 	*num = strtoull(value, &end, 10);
1035 
1036 	/*
1037 	 * Check for ERANGE, which indicates that the value is too large to fit
1038 	 * in a 64-bit value.
1039 	 */
1040 	if (errno == ERANGE) {
1041 		if (hdl)
1042 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1043 			    "numeric value is too large"));
1044 		return (-1);
1045 	}
1046 
1047 	/*
1048 	 * If we have a decimal value, then do the computation with floating
1049 	 * point arithmetic.  Otherwise, use standard arithmetic.
1050 	 */
1051 	if (*end == '.') {
1052 		double fval = strtod(value, &end);
1053 
1054 		if ((shift = str2shift(hdl, end)) == -1)
1055 			return (-1);
1056 
1057 		fval *= pow(2, shift);
1058 
1059 		if (fval > UINT64_MAX) {
1060 			if (hdl)
1061 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 				    "numeric value is too large"));
1063 			return (-1);
1064 		}
1065 
1066 		*num = (uint64_t)fval;
1067 	} else {
1068 		if ((shift = str2shift(hdl, end)) == -1)
1069 			return (-1);
1070 
1071 		/* Check for overflow */
1072 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1073 			if (hdl)
1074 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1075 				    "numeric value is too large"));
1076 			return (-1);
1077 		}
1078 
1079 		*num <<= shift;
1080 	}
1081 
1082 	return (0);
1083 }
1084 
1085 /*
1086  * Given a propname=value nvpair to set, parse any numeric properties
1087  * (index, boolean, etc) if they are specified as strings and add the
1088  * resulting nvpair to the returned nvlist.
1089  *
1090  * At the DSL layer, all properties are either 64-bit numbers or strings.
1091  * We want the user to be able to ignore this fact and specify properties
1092  * as native values (numbers, for example) or as strings (to simplify
1093  * command line utilities).  This also handles converting index types
1094  * (compression, checksum, etc) from strings to their on-disk index.
1095  */
1096 int
1097 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1098     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1099     const char *errbuf)
1100 {
1101 	data_type_t datatype = nvpair_type(elem);
1102 	zprop_type_t proptype;
1103 	const char *propname;
1104 	char *value;
1105 	boolean_t isnone = B_FALSE;
1106 
1107 	if (type == ZFS_TYPE_POOL) {
1108 		proptype = zpool_prop_get_type(prop);
1109 		propname = zpool_prop_to_name(prop);
1110 	} else {
1111 		proptype = zfs_prop_get_type(prop);
1112 		propname = zfs_prop_to_name(prop);
1113 	}
1114 
1115 	/*
1116 	 * Convert any properties to the internal DSL value types.
1117 	 */
1118 	*svalp = NULL;
1119 	*ivalp = 0;
1120 
1121 	switch (proptype) {
1122 	case PROP_TYPE_STRING:
1123 		if (datatype != DATA_TYPE_STRING) {
1124 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125 			    "'%s' must be a string"), nvpair_name(elem));
1126 			goto error;
1127 		}
1128 		(void) nvpair_value_string(elem, svalp);
1129 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1130 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131 			    "'%s' is too long"), nvpair_name(elem));
1132 			goto error;
1133 		}
1134 		break;
1135 
1136 	case PROP_TYPE_NUMBER:
1137 		if (datatype == DATA_TYPE_STRING) {
1138 			(void) nvpair_value_string(elem, &value);
1139 			if (strcmp(value, "none") == 0) {
1140 				isnone = B_TRUE;
1141 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1142 			    != 0) {
1143 				goto error;
1144 			}
1145 		} else if (datatype == DATA_TYPE_UINT64) {
1146 			(void) nvpair_value_uint64(elem, ivalp);
1147 		} else {
1148 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 			    "'%s' must be a number"), nvpair_name(elem));
1150 			goto error;
1151 		}
1152 
1153 		/*
1154 		 * Quota special: force 'none' and don't allow 0.
1155 		 */
1156 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1157 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1158 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1159 			    "use 'none' to disable quota/refquota"));
1160 			goto error;
1161 		}
1162 		break;
1163 
1164 	case PROP_TYPE_INDEX:
1165 		if (datatype != DATA_TYPE_STRING) {
1166 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1167 			    "'%s' must be a string"), nvpair_name(elem));
1168 			goto error;
1169 		}
1170 
1171 		(void) nvpair_value_string(elem, &value);
1172 
1173 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1174 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1175 			    "'%s' must be one of '%s'"), propname,
1176 			    zprop_values(prop, type));
1177 			goto error;
1178 		}
1179 		break;
1180 
1181 	default:
1182 		abort();
1183 	}
1184 
1185 	/*
1186 	 * Add the result to our return set of properties.
1187 	 */
1188 	if (*svalp != NULL) {
1189 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1190 			(void) no_memory(hdl);
1191 			return (-1);
1192 		}
1193 	} else {
1194 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1195 			(void) no_memory(hdl);
1196 			return (-1);
1197 		}
1198 	}
1199 
1200 	return (0);
1201 error:
1202 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1203 	return (-1);
1204 }
1205 
1206 static int
1207 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1208     zfs_type_t type)
1209 {
1210 	int prop;
1211 	zprop_list_t *entry;
1212 
1213 	prop = zprop_name_to_prop(propname, type);
1214 
1215 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1216 		prop = ZPROP_INVAL;
1217 
1218 	/*
1219 	 * When no property table entry can be found, return failure if
1220 	 * this is a pool property or if this isn't a user-defined
1221 	 * dataset property,
1222 	 */
1223 	if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1224 	    (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) {
1225 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1226 		    "invalid property '%s'"), propname);
1227 		return (zfs_error(hdl, EZFS_BADPROP,
1228 		    dgettext(TEXT_DOMAIN, "bad property list")));
1229 	}
1230 
1231 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1232 		return (-1);
1233 
1234 	entry->pl_prop = prop;
1235 	if (prop == ZPROP_INVAL) {
1236 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) {
1237 			free(entry);
1238 			return (-1);
1239 		}
1240 		entry->pl_width = strlen(propname);
1241 	} else {
1242 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1243 		    type);
1244 	}
1245 
1246 	*listp = entry;
1247 
1248 	return (0);
1249 }
1250 
1251 /*
1252  * Given a comma-separated list of properties, construct a property list
1253  * containing both user-defined and native properties.  This function will
1254  * return a NULL list if 'all' is specified, which can later be expanded
1255  * by zprop_expand_list().
1256  */
1257 int
1258 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1259     zfs_type_t type)
1260 {
1261 	*listp = NULL;
1262 
1263 	/*
1264 	 * If 'all' is specified, return a NULL list.
1265 	 */
1266 	if (strcmp(props, "all") == 0)
1267 		return (0);
1268 
1269 	/*
1270 	 * If no props were specified, return an error.
1271 	 */
1272 	if (props[0] == '\0') {
1273 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1274 		    "no properties specified"));
1275 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1276 		    "bad property list")));
1277 	}
1278 
1279 	/*
1280 	 * It would be nice to use getsubopt() here, but the inclusion of column
1281 	 * aliases makes this more effort than it's worth.
1282 	 */
1283 	while (*props != '\0') {
1284 		size_t len;
1285 		char *p;
1286 		char c;
1287 
1288 		if ((p = strchr(props, ',')) == NULL) {
1289 			len = strlen(props);
1290 			p = props + len;
1291 		} else {
1292 			len = p - props;
1293 		}
1294 
1295 		/*
1296 		 * Check for empty options.
1297 		 */
1298 		if (len == 0) {
1299 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1300 			    "empty property name"));
1301 			return (zfs_error(hdl, EZFS_BADPROP,
1302 			    dgettext(TEXT_DOMAIN, "bad property list")));
1303 		}
1304 
1305 		/*
1306 		 * Check all regular property names.
1307 		 */
1308 		c = props[len];
1309 		props[len] = '\0';
1310 
1311 		if (strcmp(props, "space") == 0) {
1312 			static char *spaceprops[] = {
1313 				"name", "avail", "used", "usedbysnapshots",
1314 				"usedbydataset", "usedbyrefreservation",
1315 				"usedbychildren", NULL
1316 			};
1317 			int i;
1318 
1319 			for (i = 0; spaceprops[i]; i++) {
1320 				if (addlist(hdl, spaceprops[i], listp, type))
1321 					return (-1);
1322 				listp = &(*listp)->pl_next;
1323 			}
1324 		} else {
1325 			if (addlist(hdl, props, listp, type))
1326 				return (-1);
1327 			listp = &(*listp)->pl_next;
1328 		}
1329 
1330 		props = p;
1331 		if (c == ',')
1332 			props++;
1333 	}
1334 
1335 	return (0);
1336 }
1337 
1338 void
1339 zprop_free_list(zprop_list_t *pl)
1340 {
1341 	zprop_list_t *next;
1342 
1343 	while (pl != NULL) {
1344 		next = pl->pl_next;
1345 		free(pl->pl_user_prop);
1346 		free(pl);
1347 		pl = next;
1348 	}
1349 }
1350 
1351 typedef struct expand_data {
1352 	zprop_list_t	**last;
1353 	libzfs_handle_t	*hdl;
1354 	zfs_type_t type;
1355 } expand_data_t;
1356 
1357 int
1358 zprop_expand_list_cb(int prop, void *cb)
1359 {
1360 	zprop_list_t *entry;
1361 	expand_data_t *edp = cb;
1362 
1363 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1364 		return (ZPROP_INVAL);
1365 
1366 	entry->pl_prop = prop;
1367 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1368 	entry->pl_all = B_TRUE;
1369 
1370 	*(edp->last) = entry;
1371 	edp->last = &entry->pl_next;
1372 
1373 	return (ZPROP_CONT);
1374 }
1375 
1376 int
1377 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1378 {
1379 	zprop_list_t *entry;
1380 	zprop_list_t **last;
1381 	expand_data_t exp;
1382 
1383 	if (*plp == NULL) {
1384 		/*
1385 		 * If this is the very first time we've been called for an 'all'
1386 		 * specification, expand the list to include all native
1387 		 * properties.
1388 		 */
1389 		last = plp;
1390 
1391 		exp.last = last;
1392 		exp.hdl = hdl;
1393 		exp.type = type;
1394 
1395 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1396 		    B_FALSE, type) == ZPROP_INVAL)
1397 			return (-1);
1398 
1399 		/*
1400 		 * Add 'name' to the beginning of the list, which is handled
1401 		 * specially.
1402 		 */
1403 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1404 			return (-1);
1405 
1406 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1407 		    ZFS_PROP_NAME;
1408 		entry->pl_width = zprop_width(entry->pl_prop,
1409 		    &entry->pl_fixed, type);
1410 		entry->pl_all = B_TRUE;
1411 		entry->pl_next = *plp;
1412 		*plp = entry;
1413 	}
1414 	return (0);
1415 }
1416 
1417 int
1418 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1419     zfs_type_t type)
1420 {
1421 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1422 }
1423