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