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