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