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