xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision f48205be61a214698b763ff550ab9e657525104c)
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 			(void) snprintf(buf, buflen, "%.*f%c", i,
494 			    (double)num / (1ULL << 10 * index), u);
495 			if (strlen(buf) <= 5)
496 				break;
497 		}
498 	}
499 }
500 
501 void
502 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
503 {
504 	hdl->libzfs_printerr = printerr;
505 }
506 
507 libzfs_handle_t *
508 libzfs_init(void)
509 {
510 	libzfs_handle_t *hdl;
511 
512 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
513 		return (NULL);
514 	}
515 
516 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
517 		free(hdl);
518 		return (NULL);
519 	}
520 
521 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
522 		(void) close(hdl->libzfs_fd);
523 		free(hdl);
524 		return (NULL);
525 	}
526 
527 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
528 
529 	return (hdl);
530 }
531 
532 void
533 libzfs_fini(libzfs_handle_t *hdl)
534 {
535 	(void) close(hdl->libzfs_fd);
536 	if (hdl->libzfs_mnttab)
537 		(void) fclose(hdl->libzfs_mnttab);
538 	if (hdl->libzfs_sharetab)
539 		(void) fclose(hdl->libzfs_sharetab);
540 	zfs_uninit_libshare(hdl);
541 	namespace_clear(hdl);
542 	free(hdl);
543 }
544 
545 libzfs_handle_t *
546 zpool_get_handle(zpool_handle_t *zhp)
547 {
548 	return (zhp->zpool_hdl);
549 }
550 
551 libzfs_handle_t *
552 zfs_get_handle(zfs_handle_t *zhp)
553 {
554 	return (zhp->zfs_hdl);
555 }
556 
557 /*
558  * Given a name, determine whether or not it's a valid path
559  * (starts with '/' or "./").  If so, walk the mnttab trying
560  * to match the device number.  If not, treat the path as an
561  * fs/vol/snap name.
562  */
563 zfs_handle_t *
564 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
565 {
566 	struct stat64 statbuf;
567 	struct extmnttab entry;
568 	int ret;
569 
570 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
571 		/*
572 		 * It's not a valid path, assume it's a name of type 'argtype'.
573 		 */
574 		return (zfs_open(hdl, path, argtype));
575 	}
576 
577 	if (stat64(path, &statbuf) != 0) {
578 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
579 		return (NULL);
580 	}
581 
582 	rewind(hdl->libzfs_mnttab);
583 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
584 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
585 		    statbuf.st_dev) {
586 			break;
587 		}
588 	}
589 	if (ret != 0) {
590 		return (NULL);
591 	}
592 
593 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
594 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
595 		    path);
596 		return (NULL);
597 	}
598 
599 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
600 }
601 
602 /*
603  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
604  * an ioctl().
605  */
606 int
607 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
608 {
609 	if (len == 0)
610 		len = 2048;
611 	zc->zc_nvlist_dst_size = len;
612 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
613 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
614 		return (-1);
615 
616 	return (0);
617 }
618 
619 /*
620  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
621  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
622  * filled in by the kernel to indicate the actual required size.
623  */
624 int
625 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
626 {
627 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
628 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
629 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
630 	    == NULL)
631 		return (-1);
632 
633 	return (0);
634 }
635 
636 /*
637  * Called to free the src and dst nvlists stored in the command structure.
638  */
639 void
640 zcmd_free_nvlists(zfs_cmd_t *zc)
641 {
642 	free((void *)(uintptr_t)zc->zc_nvlist_src);
643 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
644 }
645 
646 int
647 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl,
648     size_t *size)
649 {
650 	char *packed;
651 	size_t len;
652 
653 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
654 
655 	if ((packed = zfs_alloc(hdl, len)) == NULL)
656 		return (-1);
657 
658 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
659 
660 	zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
661 	zc->zc_nvlist_src_size = len;
662 
663 	if (size)
664 		*size = len;
665 	return (0);
666 }
667 
668 /*
669  * Unpacks an nvlist from the ZFS ioctl command structure.
670  */
671 int
672 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
673 {
674 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
675 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
676 		return (no_memory(hdl));
677 
678 	return (0);
679 }
680 
681 static void
682 zfs_print_prop_headers(libzfs_get_cbdata_t *cbp)
683 {
684 	zfs_proplist_t *pl = cbp->cb_proplist;
685 	int i;
686 	char *title;
687 	size_t len;
688 
689 	cbp->cb_first = B_FALSE;
690 	if (cbp->cb_scripted)
691 		return;
692 
693 	/*
694 	 * Start with the length of the column headers.
695 	 */
696 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
697 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
698 	    "PROPERTY"));
699 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
700 	    "VALUE"));
701 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
702 	    "SOURCE"));
703 
704 	/*
705 	 * Go through and calculate the widths for each column.  For the
706 	 * 'source' column, we kludge it up by taking the worst-case scenario of
707 	 * inheriting from the longest name.  This is acceptable because in the
708 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
709 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
710 	 * if the name of the property is much longer the any values we find.
711 	 */
712 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
713 		/*
714 		 * 'PROPERTY' column
715 		 */
716 		if (pl->pl_prop != ZFS_PROP_INVAL) {
717 			len = strlen(zfs_prop_to_name(pl->pl_prop));
718 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
719 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
720 		} else {
721 			len = strlen(pl->pl_user_prop);
722 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
723 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
724 		}
725 
726 		/*
727 		 * 'VALUE' column
728 		 */
729 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
730 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
731 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
732 
733 		/*
734 		 * 'NAME' and 'SOURCE' columns
735 		 */
736 		if (pl->pl_prop == ZFS_PROP_NAME &&
737 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
738 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
739 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
740 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
741 		}
742 	}
743 
744 	/*
745 	 * Now go through and print the headers.
746 	 */
747 	for (i = 0; i < 4; i++) {
748 		switch (cbp->cb_columns[i]) {
749 		case GET_COL_NAME:
750 			title = dgettext(TEXT_DOMAIN, "NAME");
751 			break;
752 		case GET_COL_PROPERTY:
753 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
754 			break;
755 		case GET_COL_VALUE:
756 			title = dgettext(TEXT_DOMAIN, "VALUE");
757 			break;
758 		case GET_COL_SOURCE:
759 			title = dgettext(TEXT_DOMAIN, "SOURCE");
760 			break;
761 		default:
762 			title = NULL;
763 		}
764 
765 		if (title != NULL) {
766 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
767 				(void) printf("%s", title);
768 			else
769 				(void) printf("%-*s  ",
770 				    cbp->cb_colwidths[cbp->cb_columns[i]],
771 				    title);
772 		}
773 	}
774 	(void) printf("\n");
775 }
776 
777 /*
778  * Display a single line of output, according to the settings in the callback
779  * structure.
780  */
781 void
782 libzfs_print_one_property(const char *name, libzfs_get_cbdata_t *cbp,
783     const char *propname, const char *value, zfs_source_t sourcetype,
784     const char *source)
785 {
786 	int i;
787 	const char *str;
788 	char buf[128];
789 
790 	/*
791 	 * Ignore those source types that the user has chosen to ignore.
792 	 */
793 	if ((sourcetype & cbp->cb_sources) == 0)
794 		return;
795 
796 	if (cbp->cb_first)
797 		zfs_print_prop_headers(cbp);
798 
799 	for (i = 0; i < 4; i++) {
800 		switch (cbp->cb_columns[i]) {
801 		case GET_COL_NAME:
802 			str = name;
803 			break;
804 
805 		case GET_COL_PROPERTY:
806 			str = propname;
807 			break;
808 
809 		case GET_COL_VALUE:
810 			str = value;
811 			break;
812 
813 		case GET_COL_SOURCE:
814 			switch (sourcetype) {
815 			case ZFS_SRC_NONE:
816 				str = "-";
817 				break;
818 
819 			case ZFS_SRC_DEFAULT:
820 				str = "default";
821 				break;
822 
823 			case ZFS_SRC_LOCAL:
824 				str = "local";
825 				break;
826 
827 			case ZFS_SRC_TEMPORARY:
828 				str = "temporary";
829 				break;
830 
831 			case ZFS_SRC_INHERITED:
832 				(void) snprintf(buf, sizeof (buf),
833 				    "inherited from %s", source);
834 				str = buf;
835 				break;
836 			}
837 			break;
838 
839 		default:
840 			continue;
841 		}
842 
843 		if (cbp->cb_columns[i + 1] == 0)
844 			(void) printf("%s", str);
845 		else if (cbp->cb_scripted)
846 			(void) printf("%s\t", str);
847 		else
848 			(void) printf("%-*s  ",
849 			    cbp->cb_colwidths[cbp->cb_columns[i]],
850 			    str);
851 
852 	}
853 
854 	(void) printf("\n");
855 }
856