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