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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Internal utility routines for the ZFS library.
40 #include <sys/mnttab.h>
41 #include <sys/mntent.h>
42 #include <sys/types.h>
46 #include "libzfs_impl.h"
49 libzfs_errno(libzfs_handle_t *hdl)
51 return (hdl->libzfs_error);
55 libzfs_error_action(libzfs_handle_t *hdl)
57 return (hdl->libzfs_action);
61 libzfs_error_description(libzfs_handle_t *hdl)
63 if (hdl->libzfs_desc[0] != '\0')
64 return (hdl->libzfs_desc);
66 switch (hdl->libzfs_error) {
68 return (dgettext(TEXT_DOMAIN, "out of memory"));
70 return (dgettext(TEXT_DOMAIN, "invalid property value"));
71 case EZFS_PROPREADONLY:
72 return (dgettext(TEXT_DOMAIN, "read only property"));
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"));
79 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
81 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
82 "datasets of this type"));
84 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
86 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
88 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
90 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
92 return (dgettext(TEXT_DOMAIN, "dataset is read only"));
94 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
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 "
104 return (dgettext(TEXT_DOMAIN, "backup failed"));
106 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
108 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
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"));
122 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
123 case EZFS_CROSSTARGET:
124 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
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"));
137 return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
139 return (dgettext(TEXT_DOMAIN, "permission denied"));
141 return (dgettext(TEXT_DOMAIN, "out of space"));
143 return (dgettext(TEXT_DOMAIN, "I/O error"));
145 return (dgettext(TEXT_DOMAIN, "signal received"));
147 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
149 case EZFS_INVALCONFIG:
150 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
152 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
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"));
162 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
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"));
173 return (dgettext(TEXT_DOMAIN, "unknown error"));
175 assert(hdl->libzfs_error == 0);
176 return (dgettext(TEXT_DOMAIN, "no error"));
182 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
188 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
190 hdl->libzfs_desc_active = 1;
196 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
198 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
200 hdl->libzfs_error = error;
202 if (hdl->libzfs_desc_active)
203 hdl->libzfs_desc_active = 0;
205 hdl->libzfs_desc[0] = '\0';
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));
214 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
215 libzfs_error_description(hdl));
216 if (error == EZFS_NOMEM)
222 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
224 return (zfs_error_fmt(hdl, error, "%s", msg));
229 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
235 zfs_verror(hdl, error, fmt, ap);
243 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
249 zfs_verror(hdl, EZFS_PERM, fmt, ap);
253 zfs_verror(hdl, EZFS_IO, fmt, ap);
257 zfs_verror(hdl, EZFS_INTR, fmt, ap);
265 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
267 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
272 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
278 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
286 zfs_verror(hdl, EZFS_IO, fmt, ap);
290 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
291 "dataset does not exist"));
292 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
297 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
301 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
302 "dataset already exists"));
303 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
313 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
317 zfs_error_aux(hdl, strerror(errno));
318 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
327 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
329 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
334 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
340 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
347 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
352 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
353 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358 "pool already exists"));
359 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
364 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
369 "one or more devices is currently unavailable"));
370 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
374 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
378 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
382 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
386 zfs_error_aux(hdl, strerror(error));
387 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
395 * Display an out of memory error message and abort the current program.
398 no_memory(libzfs_handle_t *hdl)
400 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
404 * A safe form of malloc() which will die if the allocation fails.
407 zfs_alloc(libzfs_handle_t *hdl, size_t size)
411 if ((data = calloc(1, size)) == NULL)
412 (void) no_memory(hdl);
418 * A safe form of realloc(), which also zeroes newly allocated space.
421 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
425 if ((ret = realloc(ptr, newsize)) == NULL) {
426 (void) no_memory(hdl);
431 bzero((char *)ret + oldsize, (newsize - oldsize));
436 * A safe form of strdup() which will die if the allocation fails.
439 zfs_strdup(libzfs_handle_t *hdl, const char *str)
443 if ((ret = strdup(str)) == NULL)
444 (void) no_memory(hdl);
450 * Convert a number to an appropriately human-readable output.
453 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
464 u = " KMGTPE"[index];
467 (void) snprintf(buf, buflen, "%llu", n);
468 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
470 * If this is an even multiple of the base, always display
471 * without any decimal precision.
473 (void) snprintf(buf, buflen, "%llu%c", n, u);
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.
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)
495 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
497 hdl->libzfs_printerr = printerr;
505 if (modfind("zfs") < 0) {
506 /* Not present in kernel, try loading it. */
507 if (kldload("zfs") < 0 || modfind("zfs") < 0) {
518 libzfs_handle_t *hdl;
520 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
524 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
525 if (libzfs_load() == 0)
526 hdl->libzfs_fd = open(ZFS_DEV, O_RDWR);
527 if (hdl->libzfs_fd < 0) {
533 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
534 (void) close(hdl->libzfs_fd);
539 hdl->libzfs_sharetab = fopen(ZFS_EXPORTS_PATH, "r");
545 libzfs_fini(libzfs_handle_t *hdl)
547 (void) close(hdl->libzfs_fd);
548 if (hdl->libzfs_mnttab)
549 (void) fclose(hdl->libzfs_mnttab);
550 if (hdl->libzfs_sharetab)
551 (void) fclose(hdl->libzfs_sharetab);
552 namespace_clear(hdl);
557 zpool_get_handle(zpool_handle_t *zhp)
559 return (zhp->zpool_hdl);
563 zfs_get_handle(zfs_handle_t *zhp)
565 return (zhp->zfs_hdl);
569 * Given a name, determine whether or not it's a valid path
570 * (starts with '/' or "./"). If so, walk the mnttab trying
571 * to match the device number. If not, treat the path as an
575 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
577 struct statfs statbuf;
579 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
581 * It's not a valid path, assume it's a name of type 'argtype'.
583 return (zfs_open(hdl, path, argtype));
586 if (statfs(path, &statbuf) != 0) {
587 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
591 if (strcmp(statbuf.f_fstypename, MNTTYPE_ZFS) != 0) {
592 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
597 return (zfs_open(hdl, statbuf.f_mntfromname, ZFS_TYPE_FILESYSTEM));
601 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
605 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
609 zc->zc_nvlist_dst_size = len;
610 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
611 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == 0)
618 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
619 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
620 * filled in by the kernel to indicate the actual required size.
623 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
625 free((void *)(uintptr_t)zc->zc_nvlist_dst);
626 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
627 zfs_alloc(hdl, zc->zc_nvlist_dst_size))
635 * Called to free the src and dst nvlists stored in the command structure.
638 zcmd_free_nvlists(zfs_cmd_t *zc)
640 free((void *)(uintptr_t)zc->zc_nvlist_src);
641 free((void *)(uintptr_t)zc->zc_nvlist_dst);
645 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl,
651 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
653 if ((packed = zfs_alloc(hdl, len)) == NULL)
656 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
658 zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
659 zc->zc_nvlist_src_size = len;
667 * Unpacks an nvlist from the ZFS ioctl command structure.
670 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
672 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
673 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
674 return (no_memory(hdl));
680 zfs_print_prop_headers(libzfs_get_cbdata_t *cbp)
682 zfs_proplist_t *pl = cbp->cb_proplist;
687 cbp->cb_first = B_FALSE;
688 if (cbp->cb_scripted)
692 * Start with the length of the column headers.
694 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
695 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
697 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
699 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
703 * Go through and calculate the widths for each column. For the
704 * 'source' column, we kludge it up by taking the worst-case scenario of
705 * inheriting from the longest name. This is acceptable because in the
706 * majority of cases 'SOURCE' is the last column displayed, and we don't
707 * use the width anyway. Note that the 'VALUE' column can be oversized,
708 * if the name of the property is much longer the any values we find.
710 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
714 if (pl->pl_prop != ZFS_PROP_INVAL) {
715 len = strlen(zfs_prop_to_name(pl->pl_prop));
716 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
717 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
719 len = strlen(pl->pl_user_prop);
720 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
721 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
727 if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
728 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
729 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
732 * 'NAME' and 'SOURCE' columns
734 if (pl->pl_prop == ZFS_PROP_NAME &&
735 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
736 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
737 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
738 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
743 * Now go through and print the headers.
745 for (i = 0; i < 4; i++) {
746 switch (cbp->cb_columns[i]) {
748 title = dgettext(TEXT_DOMAIN, "NAME");
750 case GET_COL_PROPERTY:
751 title = dgettext(TEXT_DOMAIN, "PROPERTY");
754 title = dgettext(TEXT_DOMAIN, "VALUE");
757 title = dgettext(TEXT_DOMAIN, "SOURCE");
764 if (i == 3 || cbp->cb_columns[i + 1] == 0)
765 (void) printf("%s", title);
767 (void) printf("%-*s ",
768 cbp->cb_colwidths[cbp->cb_columns[i]],
776 * Display a single line of output, according to the settings in the callback
780 libzfs_print_one_property(const char *name, libzfs_get_cbdata_t *cbp,
781 const char *propname, const char *value, zfs_source_t sourcetype,
789 * Ignore those source types that the user has chosen to ignore.
791 if ((sourcetype & cbp->cb_sources) == 0)
795 zfs_print_prop_headers(cbp);
797 for (i = 0; i < 4; i++) {
798 switch (cbp->cb_columns[i]) {
803 case GET_COL_PROPERTY:
812 switch (sourcetype) {
817 case ZFS_SRC_DEFAULT:
825 case ZFS_SRC_TEMPORARY:
829 case ZFS_SRC_INHERITED:
830 (void) snprintf(buf, sizeof (buf),
831 "inherited from %s", source);
841 if (cbp->cb_columns[i + 1] == 0)
842 (void) printf("%s", str);
843 else if (cbp->cb_scripted)
844 (void) printf("%s\t", str);
846 (void) printf("%-*s ",
847 cbp->cb_colwidths[cbp->cb_columns[i]],