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