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