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