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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Internal utility routines for the ZFS library.
39 #include <sys/mnttab.h>
40 #include <sys/mntent.h>
41 #include <sys/types.h>
45 #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 "
96 case EZFS_INVALIDNAME:
97 return (dgettext(TEXT_DOMAIN, "invalid name"));
99 return (dgettext(TEXT_DOMAIN, "unable to restore to "
102 return (dgettext(TEXT_DOMAIN, "backup failed"));
104 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
106 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
108 return (dgettext(TEXT_DOMAIN, "invalid device"));
109 case EZFS_NOREPLICAS:
110 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
111 case EZFS_RESILVERING:
112 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
113 case EZFS_BADVERSION:
114 return (dgettext(TEXT_DOMAIN, "unsupported version"));
115 case EZFS_POOLUNAVAIL:
116 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
117 case EZFS_DEVOVERFLOW:
118 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
120 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
121 case EZFS_CROSSTARGET:
122 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
125 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
126 case EZFS_MOUNTFAILED:
127 return (dgettext(TEXT_DOMAIN, "mount failed"));
128 case EZFS_UMOUNTFAILED:
129 return (dgettext(TEXT_DOMAIN, "umount failed"));
130 case EZFS_UNSHARENFSFAILED:
131 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
132 case EZFS_SHARENFSFAILED:
133 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
134 case EZFS_UNSHARESMBFAILED:
135 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
136 case EZFS_SHARESMBFAILED:
137 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
139 return (dgettext(TEXT_DOMAIN, "permission denied"));
141 return (dgettext(TEXT_DOMAIN, "out of space"));
143 return (dgettext(TEXT_DOMAIN, "bad address"));
145 return (dgettext(TEXT_DOMAIN, "I/O error"));
147 return (dgettext(TEXT_DOMAIN, "signal received"));
149 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
151 case EZFS_INVALCONFIG:
152 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
154 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
156 return (dgettext(TEXT_DOMAIN, "no history available"));
158 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
160 case EZFS_POOL_NOTSUP:
161 return (dgettext(TEXT_DOMAIN, "operation not supported "
162 "on this type of pool"));
163 case EZFS_POOL_INVALARG:
164 return (dgettext(TEXT_DOMAIN, "invalid argument for "
165 "this pool operation"));
166 case EZFS_NAMETOOLONG:
167 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
168 case EZFS_OPENFAILED:
169 return (dgettext(TEXT_DOMAIN, "open failed"));
171 return (dgettext(TEXT_DOMAIN,
172 "disk capacity information could not be retrieved"));
173 case EZFS_LABELFAILED:
174 return (dgettext(TEXT_DOMAIN, "write of label failed"));
176 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
178 return (dgettext(TEXT_DOMAIN, "invalid permission"));
179 case EZFS_BADPERMSET:
180 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
181 case EZFS_NODELEGATION:
182 return (dgettext(TEXT_DOMAIN, "delegated administration is "
183 "disabled on pool"));
185 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
187 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
188 case EZFS_VDEVNOTSUP:
189 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
192 return (dgettext(TEXT_DOMAIN, "operation not supported "
194 case EZFS_ACTIVE_SPARE:
195 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
197 case EZFS_UNPLAYED_LOGS:
198 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
200 case EZFS_REFTAG_RELE:
201 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
202 case EZFS_REFTAG_HOLD:
203 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
205 case EZFS_TAGTOOLONG:
206 return (dgettext(TEXT_DOMAIN, "tag too long"));
207 case EZFS_PIPEFAILED:
208 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
209 case EZFS_THREADCREATEFAILED:
210 return (dgettext(TEXT_DOMAIN, "thread create failed"));
211 case EZFS_POSTSPLIT_ONLINE:
212 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
215 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
216 "use 'zpool scrub -s' to cancel current scrub"));
218 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
220 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
222 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
223 case EZFS_POOLREADONLY:
224 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
226 return (dgettext(TEXT_DOMAIN, "unknown error"));
228 assert(hdl->libzfs_error == 0);
229 return (dgettext(TEXT_DOMAIN, "no error"));
235 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
241 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
243 hdl->libzfs_desc_active = 1;
249 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
251 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
253 hdl->libzfs_error = error;
255 if (hdl->libzfs_desc_active)
256 hdl->libzfs_desc_active = 0;
258 hdl->libzfs_desc[0] = '\0';
260 if (hdl->libzfs_printerr) {
261 if (error == EZFS_UNKNOWN) {
262 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
263 "error: %s\n"), libzfs_error_description(hdl));
267 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
268 libzfs_error_description(hdl));
269 if (error == EZFS_NOMEM)
275 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
277 return (zfs_error_fmt(hdl, error, "%s", msg));
282 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
288 zfs_verror(hdl, error, fmt, ap);
296 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
302 zfs_verror(hdl, EZFS_PERM, fmt, ap);
306 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
310 zfs_verror(hdl, EZFS_IO, fmt, ap);
314 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
318 zfs_verror(hdl, EZFS_INTR, fmt, ap);
326 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
328 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
333 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
339 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
347 zfs_verror(hdl, EZFS_IO, fmt, ap);
351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
352 "dataset does not exist"));
353 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
358 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
363 "dataset already exists"));
364 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
370 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
373 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
376 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
379 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
383 "pool I/O is currently suspended"));
384 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
387 zfs_error_aux(hdl, strerror(error));
388 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
397 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
399 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
404 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
410 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
417 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
422 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
423 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
428 "pool already exists"));
429 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
434 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439 "one or more devices is currently unavailable"));
440 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
444 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
448 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
452 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
457 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
462 "pool I/O is currently suspended"));
463 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
467 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
471 zfs_error_aux(hdl, strerror(error));
472 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
480 * Display an out of memory error message and abort the current program.
483 no_memory(libzfs_handle_t *hdl)
485 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
489 * A safe form of malloc() which will die if the allocation fails.
492 zfs_alloc(libzfs_handle_t *hdl, size_t size)
496 if ((data = calloc(1, size)) == NULL)
497 (void) no_memory(hdl);
503 * A safe form of asprintf() which will die if the allocation fails.
507 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
515 err = vasprintf(&ret, fmt, ap);
520 (void) no_memory(hdl);
526 * A safe form of realloc(), which also zeroes newly allocated space.
529 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
533 if ((ret = realloc(ptr, newsize)) == NULL) {
534 (void) no_memory(hdl);
538 bzero((char *)ret + oldsize, (newsize - oldsize));
543 * A safe form of strdup() which will die if the allocation fails.
546 zfs_strdup(libzfs_handle_t *hdl, const char *str)
550 if ((ret = strdup(str)) == NULL)
551 (void) no_memory(hdl);
557 * Convert a number to an appropriately human-readable output.
560 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
571 u = " KMGTPE"[index];
574 (void) snprintf(buf, buflen, "%llu", (u_longlong_t) n);
575 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
577 * If this is an even multiple of the base, always display
578 * without any decimal precision.
580 (void) snprintf(buf, buflen, "%llu%c", (u_longlong_t) n, u);
583 * We want to choose a precision that reflects the best choice
584 * for fitting in 5 characters. This can get rather tricky when
585 * we have numbers that are very close to an order of magnitude.
586 * For example, when displaying 10239 (which is really 9.999K),
587 * we want only a single place of precision for 10.0K. We could
588 * develop some complex heuristics for this, but it's much
589 * easier just to try each combination in turn.
592 for (i = 2; i >= 0; i--) {
593 if (snprintf(buf, buflen, "%.*f%c", i,
594 (double)num / (1ULL << 10 * index), u) <= 5)
601 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
603 hdl->libzfs_printerr = printerr;
609 libzfs_handle_t *hdl;
611 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
615 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
620 #ifdef HAVE_SETMNTENT
621 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
623 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
625 (void) close(hdl->libzfs_fd);
630 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
634 libzfs_mnttab_init(hdl);
640 libzfs_fini(libzfs_handle_t *hdl)
642 (void) close(hdl->libzfs_fd);
643 if (hdl->libzfs_mnttab)
644 #ifdef HAVE_SETMNTENT
645 (void) endmntent(hdl->libzfs_mnttab);
647 (void) fclose(hdl->libzfs_mnttab);
649 if (hdl->libzfs_sharetab)
650 (void) fclose(hdl->libzfs_sharetab);
651 zfs_uninit_libshare(hdl);
652 if (hdl->libzfs_log_str)
653 (void) free(hdl->libzfs_log_str);
654 zpool_free_handles(hdl);
655 libzfs_fru_clear(hdl, B_TRUE);
656 namespace_clear(hdl);
657 libzfs_mnttab_fini(hdl);
662 zpool_get_handle(zpool_handle_t *zhp)
664 return (zhp->zpool_hdl);
668 zfs_get_handle(zfs_handle_t *zhp)
670 return (zhp->zfs_hdl);
674 zfs_get_pool_handle(const zfs_handle_t *zhp)
676 return (zhp->zpool_hdl);
680 * Given a name, determine whether or not it's a valid path
681 * (starts with '/' or "./"). If so, walk the mnttab trying
682 * to match the device number. If not, treat the path as an
686 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
688 struct stat64 statbuf;
689 struct extmnttab entry;
692 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
694 * It's not a valid path, assume it's a name of type 'argtype'.
696 return (zfs_open(hdl, path, argtype));
699 if (stat64(path, &statbuf) != 0) {
700 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
704 rewind(hdl->libzfs_mnttab);
705 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
706 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
715 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
716 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
721 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
725 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
729 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
733 zc->zc_nvlist_dst_size = len;
734 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
735 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == 0)
742 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
743 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
744 * filled in by the kernel to indicate the actual required size.
747 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
749 free((void *)(uintptr_t)zc->zc_nvlist_dst);
750 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
751 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == 0)
758 * Called to free the src and dst nvlists stored in the command structure.
761 zcmd_free_nvlists(zfs_cmd_t *zc)
763 free((void *)(uintptr_t)zc->zc_nvlist_conf);
764 free((void *)(uintptr_t)zc->zc_nvlist_src);
765 free((void *)(uintptr_t)zc->zc_nvlist_dst);
769 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
775 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
777 if ((packed = zfs_alloc(hdl, len)) == NULL)
780 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
782 *outnv = (uint64_t)(uintptr_t)packed;
789 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
791 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
792 &zc->zc_nvlist_conf_size, nvl));
796 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
798 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
799 &zc->zc_nvlist_src_size, nvl));
803 * Unpacks an nvlist from the ZFS ioctl command structure.
806 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
808 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
809 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
810 return (no_memory(hdl));
816 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
820 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
821 error = ioctl(hdl->libzfs_fd, request, zc);
822 if (hdl->libzfs_log_str) {
823 free(hdl->libzfs_log_str);
824 hdl->libzfs_log_str = NULL;
832 * ================================================================
833 * API shared by zfs and zpool property management
834 * ================================================================
838 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
840 zprop_list_t *pl = cbp->cb_proplist;
845 cbp->cb_first = B_FALSE;
846 if (cbp->cb_scripted)
850 * Start with the length of the column headers.
852 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
853 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
855 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
857 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
859 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
862 /* first property is always NAME */
863 assert(cbp->cb_proplist->pl_prop ==
864 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
867 * Go through and calculate the widths for each column. For the
868 * 'source' column, we kludge it up by taking the worst-case scenario of
869 * inheriting from the longest name. This is acceptable because in the
870 * majority of cases 'SOURCE' is the last column displayed, and we don't
871 * use the width anyway. Note that the 'VALUE' column can be oversized,
872 * if the name of the property is much longer than any values we find.
874 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
878 if (pl->pl_prop != ZPROP_INVAL) {
879 const char *propname = (type == ZFS_TYPE_POOL) ?
880 zpool_prop_to_name(pl->pl_prop) :
881 zfs_prop_to_name(pl->pl_prop);
883 len = strlen(propname);
884 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
885 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
887 len = strlen(pl->pl_user_prop);
888 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
889 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
893 * 'VALUE' column. The first property is always the 'name'
894 * property that was tacked on either by /sbin/zfs's
895 * zfs_do_get() or when calling zprop_expand_list(), so we
896 * ignore its width. If the user specified the name property
897 * to display, then it will be later in the list in any case.
899 if (pl != cbp->cb_proplist &&
900 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
901 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
903 /* 'RECEIVED' column. */
904 if (pl != cbp->cb_proplist &&
905 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
906 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
909 * 'NAME' and 'SOURCE' columns
911 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
913 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
914 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
915 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
916 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
921 * Now go through and print the headers.
923 for (i = 0; i < ZFS_GET_NCOLS; i++) {
924 switch (cbp->cb_columns[i]) {
926 title = dgettext(TEXT_DOMAIN, "NAME");
928 case GET_COL_PROPERTY:
929 title = dgettext(TEXT_DOMAIN, "PROPERTY");
932 title = dgettext(TEXT_DOMAIN, "VALUE");
935 title = dgettext(TEXT_DOMAIN, "RECEIVED");
938 title = dgettext(TEXT_DOMAIN, "SOURCE");
945 if (i == (ZFS_GET_NCOLS - 1) ||
946 cbp->cb_columns[i + 1] == GET_COL_NONE)
947 (void) printf("%s", title);
949 (void) printf("%-*s ",
950 cbp->cb_colwidths[cbp->cb_columns[i]],
958 * Display a single line of output, according to the settings in the callback
962 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
963 const char *propname, const char *value, zprop_source_t sourcetype,
964 const char *source, const char *recvd_value)
967 const char *str = NULL;
971 * Ignore those source types that the user has chosen to ignore.
973 if ((sourcetype & cbp->cb_sources) == 0)
977 zprop_print_headers(cbp, cbp->cb_type);
979 for (i = 0; i < ZFS_GET_NCOLS; i++) {
980 switch (cbp->cb_columns[i]) {
985 case GET_COL_PROPERTY:
994 switch (sourcetype) {
999 case ZPROP_SRC_DEFAULT:
1003 case ZPROP_SRC_LOCAL:
1007 case ZPROP_SRC_TEMPORARY:
1011 case ZPROP_SRC_INHERITED:
1012 (void) snprintf(buf, sizeof (buf),
1013 "inherited from %s", source);
1016 case ZPROP_SRC_RECEIVED:
1023 str = (recvd_value == NULL ? "-" : recvd_value);
1030 if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1031 (void) printf("%s", str);
1032 else if (cbp->cb_scripted)
1033 (void) printf("%s\t", str);
1035 (void) printf("%-*s ",
1036 cbp->cb_colwidths[cbp->cb_columns[i]],
1040 (void) printf("\n");
1044 * Given a numeric suffix, convert the value into a number of bits that the
1045 * resulting value must be shifted.
1048 str2shift(libzfs_handle_t *hdl, const char *buf)
1050 const char *ends = "BKMGTPEZ";
1055 for (i = 0; i < strlen(ends); i++) {
1056 if (toupper(buf[0]) == ends[i])
1059 if (i == strlen(ends)) {
1060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1061 "invalid numeric suffix '%s'"), buf);
1066 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
1067 * allow 'BB' - that's just weird.
1069 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1070 toupper(buf[0]) != 'B'))
1073 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1074 "invalid numeric suffix '%s'"), buf);
1079 * Convert a string of the form '100G' into a real number. Used when setting
1080 * properties or creating a volume. 'buf' is used to place an extended error
1081 * message for the caller to use.
1084 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1091 /* Check to see if this looks like a number. */
1092 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1095 "bad numeric value '%s'"), value);
1099 /* Rely on strtoull() to process the numeric portion. */
1101 *num = strtoull(value, &end, 10);
1104 * Check for ERANGE, which indicates that the value is too large to fit
1105 * in a 64-bit value.
1107 if (errno == ERANGE) {
1109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1110 "numeric value is too large"));
1115 * If we have a decimal value, then do the computation with floating
1116 * point arithmetic. Otherwise, use standard arithmetic.
1119 double fval = strtod(value, &end);
1121 if ((shift = str2shift(hdl, end)) == -1)
1124 fval *= pow(2, shift);
1126 if (fval > UINT64_MAX) {
1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129 "numeric value is too large"));
1133 *num = (uint64_t)fval;
1135 if ((shift = str2shift(hdl, end)) == -1)
1138 /* Check for overflow */
1139 if (shift >= 64 || (*num << shift) >> shift != *num) {
1141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1142 "numeric value is too large"));
1153 * Given a propname=value nvpair to set, parse any numeric properties
1154 * (index, boolean, etc) if they are specified as strings and add the
1155 * resulting nvpair to the returned nvlist.
1157 * At the DSL layer, all properties are either 64-bit numbers or strings.
1158 * We want the user to be able to ignore this fact and specify properties
1159 * as native values (numbers, for example) or as strings (to simplify
1160 * command line utilities). This also handles converting index types
1161 * (compression, checksum, etc) from strings to their on-disk index.
1164 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1165 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1168 data_type_t datatype = nvpair_type(elem);
1169 zprop_type_t proptype;
1170 const char *propname;
1172 boolean_t isnone = B_FALSE;
1174 if (type == ZFS_TYPE_POOL) {
1175 proptype = zpool_prop_get_type(prop);
1176 propname = zpool_prop_to_name(prop);
1178 proptype = zfs_prop_get_type(prop);
1179 propname = zfs_prop_to_name(prop);
1183 * Convert any properties to the internal DSL value types.
1189 case PROP_TYPE_STRING:
1190 if (datatype != DATA_TYPE_STRING) {
1191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1192 "'%s' must be a string"), nvpair_name(elem));
1195 (void) nvpair_value_string(elem, svalp);
1196 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198 "'%s' is too long"), nvpair_name(elem));
1203 case PROP_TYPE_NUMBER:
1204 if (datatype == DATA_TYPE_STRING) {
1205 (void) nvpair_value_string(elem, &value);
1206 if (strcmp(value, "none") == 0) {
1208 } else if (zfs_nicestrtonum(hdl, value, ivalp)
1212 } else if (datatype == DATA_TYPE_UINT64) {
1213 (void) nvpair_value_uint64(elem, ivalp);
1215 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1216 "'%s' must be a number"), nvpair_name(elem));
1221 * Quota special: force 'none' and don't allow 0.
1223 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1224 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1225 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1226 "use 'none' to disable quota/refquota"));
1231 case PROP_TYPE_INDEX:
1232 if (datatype != DATA_TYPE_STRING) {
1233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1234 "'%s' must be a string"), nvpair_name(elem));
1238 (void) nvpair_value_string(elem, &value);
1240 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1242 "'%s' must be one of '%s'"), propname,
1243 zprop_values(prop, type));
1253 * Add the result to our return set of properties.
1255 if (*svalp != NULL) {
1256 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1257 (void) no_memory(hdl);
1261 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1262 (void) no_memory(hdl);
1269 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1274 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1278 zprop_list_t *entry;
1280 prop = zprop_name_to_prop(propname, type);
1282 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1286 * When no property table entry can be found, return failure if
1287 * this is a pool property or if this isn't a user-defined
1290 if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1291 (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) {
1292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1293 "invalid property '%s'"), propname);
1294 return (zfs_error(hdl, EZFS_BADPROP,
1295 dgettext(TEXT_DOMAIN, "bad property list")));
1298 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1301 entry->pl_prop = prop;
1302 if (prop == ZPROP_INVAL) {
1303 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) {
1307 entry->pl_width = strlen(propname);
1309 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1319 * Given a comma-separated list of properties, construct a property list
1320 * containing both user-defined and native properties. This function will
1321 * return a NULL list if 'all' is specified, which can later be expanded
1322 * by zprop_expand_list().
1325 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1331 * If 'all' is specified, return a NULL list.
1333 if (strcmp(props, "all") == 0)
1337 * If no props were specified, return an error.
1339 if (props[0] == '\0') {
1340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1341 "no properties specified"));
1342 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1343 "bad property list")));
1347 * It would be nice to use getsubopt() here, but the inclusion of column
1348 * aliases makes this more effort than it's worth.
1350 while (*props != '\0') {
1355 if ((p = strchr(props, ',')) == NULL) {
1356 len = strlen(props);
1363 * Check for empty options.
1366 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1367 "empty property name"));
1368 return (zfs_error(hdl, EZFS_BADPROP,
1369 dgettext(TEXT_DOMAIN, "bad property list")));
1373 * Check all regular property names.
1378 if (strcmp(props, "space") == 0) {
1379 static char *spaceprops[] = {
1380 "name", "avail", "used", "usedbysnapshots",
1381 "usedbydataset", "usedbyrefreservation",
1382 "usedbychildren", NULL
1386 for (i = 0; spaceprops[i]; i++) {
1387 if (addlist(hdl, spaceprops[i], listp, type))
1389 listp = &(*listp)->pl_next;
1392 if (addlist(hdl, props, listp, type))
1394 listp = &(*listp)->pl_next;
1406 zprop_free_list(zprop_list_t *pl)
1410 while (pl != NULL) {
1412 free(pl->pl_user_prop);
1418 typedef struct expand_data {
1419 zprop_list_t **last;
1420 libzfs_handle_t *hdl;
1425 zprop_expand_list_cb(int prop, void *cb)
1427 zprop_list_t *entry;
1428 expand_data_t *edp = cb;
1430 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1431 return (ZPROP_INVAL);
1433 entry->pl_prop = prop;
1434 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1435 entry->pl_all = B_TRUE;
1437 *(edp->last) = entry;
1438 edp->last = &entry->pl_next;
1440 return (ZPROP_CONT);
1444 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1446 zprop_list_t *entry;
1447 zprop_list_t **last;
1452 * If this is the very first time we've been called for an 'all'
1453 * specification, expand the list to include all native
1462 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1463 B_FALSE, type) == ZPROP_INVAL)
1467 * Add 'name' to the beginning of the list, which is handled
1470 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1473 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1475 entry->pl_width = zprop_width(entry->pl_prop,
1476 &entry->pl_fixed, type);
1477 entry->pl_all = B_TRUE;
1478 entry->pl_next = *plp;
1485 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1488 return (zprop_iter_common(func, cb, show_all, ordered, type));