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