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