xref: /titanic_51/usr/src/lib/libzfs/common/libzfs_util.c (revision b2e8ece49d4c2e04af8e8e83caa60e23caa58061)
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 <sys/mnttab.h>
41 #include <sys/mntent.h>
42 #include <sys/types.h>
43 
44 #include <libzfs.h>
45 
46 #include "libzfs_impl.h"
47 
48 int
49 libzfs_errno(libzfs_handle_t *hdl)
50 {
51 	return (hdl->libzfs_error);
52 }
53 
54 const char *
55 libzfs_error_action(libzfs_handle_t *hdl)
56 {
57 	return (hdl->libzfs_action);
58 }
59 
60 const char *
61 libzfs_error_description(libzfs_handle_t *hdl)
62 {
63 	if (hdl->libzfs_desc[0] != '\0')
64 		return (hdl->libzfs_desc);
65 
66 	switch (hdl->libzfs_error) {
67 	case EZFS_NOMEM:
68 		return (dgettext(TEXT_DOMAIN, "out of memory"));
69 	case EZFS_BADPROP:
70 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
71 	case EZFS_PROPREADONLY:
72 		return (dgettext(TEXT_DOMAIN, "read only property"));
73 	case EZFS_PROPTYPE:
74 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
75 		    "datasets of this type"));
76 	case EZFS_PROPNONINHERIT:
77 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
78 	case EZFS_PROPSPACE:
79 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
80 	case EZFS_BADTYPE:
81 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
82 		    "datasets of this type"));
83 	case EZFS_BUSY:
84 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
85 	case EZFS_EXISTS:
86 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
87 	case EZFS_NOENT:
88 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
89 	case EZFS_BADSTREAM:
90 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
91 	case EZFS_DSREADONLY:
92 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
93 	case EZFS_VOLTOOBIG:
94 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
95 		    "this system"));
96 	case EZFS_VOLHASDATA:
97 		return (dgettext(TEXT_DOMAIN, "volume has data"));
98 	case EZFS_INVALIDNAME:
99 		return (dgettext(TEXT_DOMAIN, "invalid name"));
100 	case EZFS_BADRESTORE:
101 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
102 		    "destination"));
103 	case EZFS_BADBACKUP:
104 		return (dgettext(TEXT_DOMAIN, "backup failed"));
105 	case EZFS_BADTARGET:
106 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
107 	case EZFS_NODEVICE:
108 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
109 	case EZFS_BADDEV:
110 		return (dgettext(TEXT_DOMAIN, "invalid device"));
111 	case EZFS_NOREPLICAS:
112 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
113 	case EZFS_RESILVERING:
114 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
115 	case EZFS_BADVERSION:
116 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
117 	case EZFS_POOLUNAVAIL:
118 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
119 	case EZFS_DEVOVERFLOW:
120 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
121 	case EZFS_BADPATH:
122 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
123 	case EZFS_CROSSTARGET:
124 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
125 		    "pools"));
126 	case EZFS_ZONED:
127 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
128 	case EZFS_MOUNTFAILED:
129 		return (dgettext(TEXT_DOMAIN, "mount failed"));
130 	case EZFS_UMOUNTFAILED:
131 		return (dgettext(TEXT_DOMAIN, "umount failed"));
132 	case EZFS_UNSHARENFSFAILED:
133 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
134 	case EZFS_SHARENFSFAILED:
135 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
136 	case EZFS_DEVLINKS:
137 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
138 	case EZFS_PERM:
139 		return (dgettext(TEXT_DOMAIN, "permission denied"));
140 	case EZFS_NOSPC:
141 		return (dgettext(TEXT_DOMAIN, "out of space"));
142 	case EZFS_IO:
143 		return (dgettext(TEXT_DOMAIN, "I/O error"));
144 	case EZFS_INTR:
145 		return (dgettext(TEXT_DOMAIN, "signal received"));
146 	case EZFS_ISSPARE:
147 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
148 		    "spare"));
149 	case EZFS_INVALCONFIG:
150 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
151 	case EZFS_RECURSIVE:
152 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
153 	case EZFS_NOHISTORY:
154 		return (dgettext(TEXT_DOMAIN, "no history available"));
155 	case EZFS_UNSHAREISCSIFAILED:
156 		return (dgettext(TEXT_DOMAIN,
157 		    "iscsitgtd failed request to unshare"));
158 	case EZFS_SHAREISCSIFAILED:
159 		return (dgettext(TEXT_DOMAIN,
160 		    "iscsitgtd failed request to share"));
161 	case EZFS_POOLPROPS:
162 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
163 		    "pool properties"));
164 	case EZFS_POOL_NOTSUP:
165 		return (dgettext(TEXT_DOMAIN, "operation not supported "
166 		    "on this type of pool"));
167 	case EZFS_POOL_INVALARG:
168 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
169 		    "this pool operation"));
170 	case EZFS_NAMETOOLONG:
171 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
172 	case EZFS_OPENFAILED:
173 		return (dgettext(TEXT_DOMAIN, "open failed"));
174 	case EZFS_NOCAP:
175 		return (dgettext(TEXT_DOMAIN,
176 		    "disk capacity information could not be retrieved"));
177 	case EZFS_LABELFAILED:
178 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
179 	case EZFS_UNKNOWN:
180 		return (dgettext(TEXT_DOMAIN, "unknown error"));
181 	default:
182 		assert(hdl->libzfs_error == 0);
183 		return (dgettext(TEXT_DOMAIN, "no error"));
184 	}
185 }
186 
187 /*PRINTFLIKE2*/
188 void
189 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
190 {
191 	va_list ap;
192 
193 	va_start(ap, fmt);
194 
195 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
196 	    fmt, ap);
197 	hdl->libzfs_desc_active = 1;
198 
199 	va_end(ap);
200 }
201 
202 static void
203 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
204 {
205 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
206 	    fmt, ap);
207 	hdl->libzfs_error = error;
208 
209 	if (hdl->libzfs_desc_active)
210 		hdl->libzfs_desc_active = 0;
211 	else
212 		hdl->libzfs_desc[0] = '\0';
213 
214 	if (hdl->libzfs_printerr) {
215 		if (error == EZFS_UNKNOWN) {
216 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
217 			    "error: %s\n"), libzfs_error_description(hdl));
218 			abort();
219 		}
220 
221 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
222 		    libzfs_error_description(hdl));
223 		if (error == EZFS_NOMEM)
224 			exit(1);
225 	}
226 }
227 
228 int
229 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
230 {
231 	return (zfs_error_fmt(hdl, error, "%s", msg));
232 }
233 
234 /*PRINTFLIKE3*/
235 int
236 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
237 {
238 	va_list ap;
239 
240 	va_start(ap, fmt);
241 
242 	zfs_verror(hdl, error, fmt, ap);
243 
244 	va_end(ap);
245 
246 	return (-1);
247 }
248 
249 static int
250 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
251     va_list ap)
252 {
253 	switch (error) {
254 	case EPERM:
255 	case EACCES:
256 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
257 		return (-1);
258 
259 	case EIO:
260 		zfs_verror(hdl, EZFS_IO, fmt, ap);
261 		return (-1);
262 
263 	case EINTR:
264 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
265 		return (-1);
266 	}
267 
268 	return (0);
269 }
270 
271 int
272 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
273 {
274 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
275 }
276 
277 /*PRINTFLIKE3*/
278 int
279 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
280 {
281 	va_list ap;
282 
283 	va_start(ap, fmt);
284 
285 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
286 		va_end(ap);
287 		return (-1);
288 	}
289 
290 
291 	switch (error) {
292 	case ENXIO:
293 		zfs_verror(hdl, EZFS_IO, fmt, ap);
294 		break;
295 
296 	case ENOENT:
297 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
298 		    "dataset does not exist"));
299 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
300 		break;
301 
302 	case ENOSPC:
303 	case EDQUOT:
304 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
305 		return (-1);
306 
307 	case EEXIST:
308 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309 		    "dataset already exists"));
310 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
311 		break;
312 
313 	case EBUSY:
314 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
315 		    "dataset is busy"));
316 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
317 		break;
318 
319 	case ENAMETOOLONG:
320 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
321 		break;
322 
323 	default:
324 		zfs_error_aux(hdl, strerror(errno));
325 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
326 		break;
327 	}
328 
329 	va_end(ap);
330 	return (-1);
331 }
332 
333 int
334 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
335 {
336 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
337 }
338 
339 /*PRINTFLIKE3*/
340 int
341 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
342 {
343 	va_list ap;
344 
345 	va_start(ap, fmt);
346 
347 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
348 		va_end(ap);
349 		return (-1);
350 	}
351 
352 	switch (error) {
353 	case ENODEV:
354 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
355 		break;
356 
357 	case ENOENT:
358 		zfs_error_aux(hdl,
359 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
360 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
361 		break;
362 
363 	case EEXIST:
364 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
365 		    "pool already exists"));
366 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
367 		break;
368 
369 	case EBUSY:
370 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
371 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
372 		break;
373 
374 	case ENXIO:
375 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
376 		    "one or more devices is currently unavailable"));
377 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
378 		break;
379 
380 	case ENAMETOOLONG:
381 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
382 		break;
383 
384 	case ENOTSUP:
385 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
386 		break;
387 
388 	case EINVAL:
389 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
390 		break;
391 
392 	default:
393 		zfs_error_aux(hdl, strerror(error));
394 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
395 	}
396 
397 	va_end(ap);
398 	return (-1);
399 }
400 
401 /*
402  * Display an out of memory error message and abort the current program.
403  */
404 int
405 no_memory(libzfs_handle_t *hdl)
406 {
407 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
408 }
409 
410 /*
411  * A safe form of malloc() which will die if the allocation fails.
412  */
413 void *
414 zfs_alloc(libzfs_handle_t *hdl, size_t size)
415 {
416 	void *data;
417 
418 	if ((data = calloc(1, size)) == NULL)
419 		(void) no_memory(hdl);
420 
421 	return (data);
422 }
423 
424 /*
425  * A safe form of realloc(), which also zeroes newly allocated space.
426  */
427 void *
428 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
429 {
430 	void *ret;
431 
432 	if ((ret = realloc(ptr, newsize)) == NULL) {
433 		(void) no_memory(hdl);
434 		free(ptr);
435 		return (NULL);
436 	}
437 
438 	bzero((char *)ret + oldsize, (newsize - oldsize));
439 	return (ret);
440 }
441 
442 /*
443  * A safe form of strdup() which will die if the allocation fails.
444  */
445 char *
446 zfs_strdup(libzfs_handle_t *hdl, const char *str)
447 {
448 	char *ret;
449 
450 	if ((ret = strdup(str)) == NULL)
451 		(void) no_memory(hdl);
452 
453 	return (ret);
454 }
455 
456 /*
457  * Convert a number to an appropriately human-readable output.
458  */
459 void
460 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
461 {
462 	uint64_t n = num;
463 	int index = 0;
464 	char u;
465 
466 	while (n >= 1024) {
467 		n /= 1024;
468 		index++;
469 	}
470 
471 	u = " KMGTPE"[index];
472 
473 	if (index == 0) {
474 		(void) snprintf(buf, buflen, "%llu", n);
475 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
476 		/*
477 		 * If this is an even multiple of the base, always display
478 		 * without any decimal precision.
479 		 */
480 		(void) snprintf(buf, buflen, "%llu%c", n, u);
481 	} else {
482 		/*
483 		 * We want to choose a precision that reflects the best choice
484 		 * for fitting in 5 characters.  This can get rather tricky when
485 		 * we have numbers that are very close to an order of magnitude.
486 		 * For example, when displaying 10239 (which is really 9.999K),
487 		 * we want only a single place of precision for 10.0K.  We could
488 		 * develop some complex heuristics for this, but it's much
489 		 * easier just to try each combination in turn.
490 		 */
491 		int i;
492 		for (i = 2; i >= 0; i--) {
493 			if (snprintf(buf, buflen, "%.*f%c", i,
494 			    (double)num / (1ULL << 10 * index), u) <= 5)
495 				break;
496 		}
497 	}
498 }
499 
500 void
501 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
502 {
503 	hdl->libzfs_printerr = printerr;
504 }
505 
506 libzfs_handle_t *
507 libzfs_init(void)
508 {
509 	libzfs_handle_t *hdl;
510 
511 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
512 		return (NULL);
513 	}
514 
515 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
516 		free(hdl);
517 		return (NULL);
518 	}
519 
520 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
521 		(void) close(hdl->libzfs_fd);
522 		free(hdl);
523 		return (NULL);
524 	}
525 
526 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
527 
528 	return (hdl);
529 }
530 
531 void
532 libzfs_fini(libzfs_handle_t *hdl)
533 {
534 	(void) close(hdl->libzfs_fd);
535 	if (hdl->libzfs_mnttab)
536 		(void) fclose(hdl->libzfs_mnttab);
537 	if (hdl->libzfs_sharetab)
538 		(void) fclose(hdl->libzfs_sharetab);
539 	zfs_uninit_libshare(hdl);
540 	namespace_clear(hdl);
541 	free(hdl);
542 }
543 
544 libzfs_handle_t *
545 zpool_get_handle(zpool_handle_t *zhp)
546 {
547 	return (zhp->zpool_hdl);
548 }
549 
550 libzfs_handle_t *
551 zfs_get_handle(zfs_handle_t *zhp)
552 {
553 	return (zhp->zfs_hdl);
554 }
555 
556 /*
557  * Given a name, determine whether or not it's a valid path
558  * (starts with '/' or "./").  If so, walk the mnttab trying
559  * to match the device number.  If not, treat the path as an
560  * fs/vol/snap name.
561  */
562 zfs_handle_t *
563 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
564 {
565 	struct stat64 statbuf;
566 	struct extmnttab entry;
567 	int ret;
568 
569 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
570 		/*
571 		 * It's not a valid path, assume it's a name of type 'argtype'.
572 		 */
573 		return (zfs_open(hdl, path, argtype));
574 	}
575 
576 	if (stat64(path, &statbuf) != 0) {
577 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
578 		return (NULL);
579 	}
580 
581 	rewind(hdl->libzfs_mnttab);
582 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
583 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
584 		    statbuf.st_dev) {
585 			break;
586 		}
587 	}
588 	if (ret != 0) {
589 		return (NULL);
590 	}
591 
592 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
593 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
594 		    path);
595 		return (NULL);
596 	}
597 
598 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
599 }
600 
601 /*
602  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
603  * an ioctl().
604  */
605 int
606 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
607 {
608 	if (len == 0)
609 		len = 2048;
610 	zc->zc_nvlist_dst_size = len;
611 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
612 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
613 		return (-1);
614 
615 	return (0);
616 }
617 
618 /*
619  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
620  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
621  * filled in by the kernel to indicate the actual required size.
622  */
623 int
624 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
625 {
626 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
627 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
628 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
629 	    == NULL)
630 		return (-1);
631 
632 	return (0);
633 }
634 
635 /*
636  * Called to free the src and dst nvlists stored in the command structure.
637  */
638 void
639 zcmd_free_nvlists(zfs_cmd_t *zc)
640 {
641 	free((void *)(uintptr_t)zc->zc_nvlist_src);
642 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
643 }
644 
645 int
646 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl,
647     size_t *size)
648 {
649 	char *packed;
650 	size_t len;
651 
652 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
653 
654 	if ((packed = zfs_alloc(hdl, len)) == NULL)
655 		return (-1);
656 
657 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
658 
659 	zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
660 	zc->zc_nvlist_src_size = len;
661 
662 	if (size)
663 		*size = len;
664 	return (0);
665 }
666 
667 /*
668  * Unpacks an nvlist from the ZFS ioctl command structure.
669  */
670 int
671 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
672 {
673 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
674 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
675 		return (no_memory(hdl));
676 
677 	return (0);
678 }
679 
680 static void
681 zfs_print_prop_headers(libzfs_get_cbdata_t *cbp)
682 {
683 	zfs_proplist_t *pl = cbp->cb_proplist;
684 	int i;
685 	char *title;
686 	size_t len;
687 
688 	cbp->cb_first = B_FALSE;
689 	if (cbp->cb_scripted)
690 		return;
691 
692 	/*
693 	 * Start with the length of the column headers.
694 	 */
695 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
696 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
697 	    "PROPERTY"));
698 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
699 	    "VALUE"));
700 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
701 	    "SOURCE"));
702 
703 	/*
704 	 * Go through and calculate the widths for each column.  For the
705 	 * 'source' column, we kludge it up by taking the worst-case scenario of
706 	 * inheriting from the longest name.  This is acceptable because in the
707 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
708 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
709 	 * if the name of the property is much longer the any values we find.
710 	 */
711 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
712 		/*
713 		 * 'PROPERTY' column
714 		 */
715 		if (pl->pl_prop != ZFS_PROP_INVAL) {
716 			len = strlen(zfs_prop_to_name(pl->pl_prop));
717 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
718 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
719 		} else {
720 			len = strlen(pl->pl_user_prop);
721 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
722 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
723 		}
724 
725 		/*
726 		 * 'VALUE' column
727 		 */
728 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
729 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
730 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
731 
732 		/*
733 		 * 'NAME' and 'SOURCE' columns
734 		 */
735 		if (pl->pl_prop == ZFS_PROP_NAME &&
736 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
737 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
738 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
739 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
740 		}
741 	}
742 
743 	/*
744 	 * Now go through and print the headers.
745 	 */
746 	for (i = 0; i < 4; i++) {
747 		switch (cbp->cb_columns[i]) {
748 		case GET_COL_NAME:
749 			title = dgettext(TEXT_DOMAIN, "NAME");
750 			break;
751 		case GET_COL_PROPERTY:
752 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
753 			break;
754 		case GET_COL_VALUE:
755 			title = dgettext(TEXT_DOMAIN, "VALUE");
756 			break;
757 		case GET_COL_SOURCE:
758 			title = dgettext(TEXT_DOMAIN, "SOURCE");
759 			break;
760 		default:
761 			title = NULL;
762 		}
763 
764 		if (title != NULL) {
765 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
766 				(void) printf("%s", title);
767 			else
768 				(void) printf("%-*s  ",
769 				    cbp->cb_colwidths[cbp->cb_columns[i]],
770 				    title);
771 		}
772 	}
773 	(void) printf("\n");
774 }
775 
776 /*
777  * Display a single line of output, according to the settings in the callback
778  * structure.
779  */
780 void
781 libzfs_print_one_property(const char *name, libzfs_get_cbdata_t *cbp,
782     const char *propname, const char *value, zfs_source_t sourcetype,
783     const char *source)
784 {
785 	int i;
786 	const char *str;
787 	char buf[128];
788 
789 	/*
790 	 * Ignore those source types that the user has chosen to ignore.
791 	 */
792 	if ((sourcetype & cbp->cb_sources) == 0)
793 		return;
794 
795 	if (cbp->cb_first)
796 		zfs_print_prop_headers(cbp);
797 
798 	for (i = 0; i < 4; i++) {
799 		switch (cbp->cb_columns[i]) {
800 		case GET_COL_NAME:
801 			str = name;
802 			break;
803 
804 		case GET_COL_PROPERTY:
805 			str = propname;
806 			break;
807 
808 		case GET_COL_VALUE:
809 			str = value;
810 			break;
811 
812 		case GET_COL_SOURCE:
813 			switch (sourcetype) {
814 			case ZFS_SRC_NONE:
815 				str = "-";
816 				break;
817 
818 			case ZFS_SRC_DEFAULT:
819 				str = "default";
820 				break;
821 
822 			case ZFS_SRC_LOCAL:
823 				str = "local";
824 				break;
825 
826 			case ZFS_SRC_TEMPORARY:
827 				str = "temporary";
828 				break;
829 
830 			case ZFS_SRC_INHERITED:
831 				(void) snprintf(buf, sizeof (buf),
832 				    "inherited from %s", source);
833 				str = buf;
834 				break;
835 			}
836 			break;
837 
838 		default:
839 			continue;
840 		}
841 
842 		if (cbp->cb_columns[i + 1] == 0)
843 			(void) printf("%s", str);
844 		else if (cbp->cb_scripted)
845 			(void) printf("%s\t", str);
846 		else
847 			(void) printf("%-*s  ",
848 			    cbp->cb_colwidths[cbp->cb_columns[i]],
849 			    str);
850 
851 	}
852 
853 	(void) printf("\n");
854 }
855