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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)libzfs_util.c 1.29 08/04/01 SMI"
29 * Internal utility routines for the ZFS library.
42 #include <sys/mnttab.h>
43 #include <sys/mntent.h>
44 #include <sys/types.h>
48 #include "libzfs_impl.h"
52 libzfs_errno(libzfs_handle_t *hdl)
54 return (hdl->libzfs_error);
58 libzfs_error_action(libzfs_handle_t *hdl)
60 return (hdl->libzfs_action);
64 libzfs_error_description(libzfs_handle_t *hdl)
66 if (hdl->libzfs_desc[0] != '\0')
67 return (hdl->libzfs_desc);
69 switch (hdl->libzfs_error) {
71 return (dgettext(TEXT_DOMAIN, "out of memory"));
73 return (dgettext(TEXT_DOMAIN, "invalid property value"));
74 case EZFS_PROPREADONLY:
75 return (dgettext(TEXT_DOMAIN, "read only property"));
77 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
78 "datasets of this type"));
79 case EZFS_PROPNONINHERIT:
80 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
82 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
84 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
85 "datasets of this type"));
87 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
89 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
91 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
93 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
95 return (dgettext(TEXT_DOMAIN, "dataset is read only"));
97 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
100 return (dgettext(TEXT_DOMAIN, "volume has data"));
101 case EZFS_INVALIDNAME:
102 return (dgettext(TEXT_DOMAIN, "invalid name"));
103 case EZFS_BADRESTORE:
104 return (dgettext(TEXT_DOMAIN, "unable to restore to "
107 return (dgettext(TEXT_DOMAIN, "backup failed"));
109 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
111 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
113 return (dgettext(TEXT_DOMAIN, "invalid device"));
114 case EZFS_NOREPLICAS:
115 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
116 case EZFS_RESILVERING:
117 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
118 case EZFS_BADVERSION:
119 return (dgettext(TEXT_DOMAIN, "unsupported version"));
120 case EZFS_POOLUNAVAIL:
121 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
122 case EZFS_DEVOVERFLOW:
123 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
125 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
126 case EZFS_CROSSTARGET:
127 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
130 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
131 case EZFS_MOUNTFAILED:
132 return (dgettext(TEXT_DOMAIN, "mount failed"));
133 case EZFS_UMOUNTFAILED:
134 return (dgettext(TEXT_DOMAIN, "umount failed"));
135 case EZFS_UNSHARENFSFAILED:
136 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
137 case EZFS_SHARENFSFAILED:
138 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
139 case EZFS_UNSHARESMBFAILED:
140 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
141 case EZFS_SHARESMBFAILED:
142 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
143 case EZFS_ISCSISVCUNAVAIL:
144 return (dgettext(TEXT_DOMAIN,
145 "iscsitgt service need to be enabled by "
146 "a privileged user"));
148 return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
150 return (dgettext(TEXT_DOMAIN, "permission denied"));
152 return (dgettext(TEXT_DOMAIN, "out of space"));
154 return (dgettext(TEXT_DOMAIN, "I/O error"));
156 return (dgettext(TEXT_DOMAIN, "signal received"));
158 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
160 case EZFS_INVALCONFIG:
161 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
163 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
165 return (dgettext(TEXT_DOMAIN, "no history available"));
166 case EZFS_UNSHAREISCSIFAILED:
167 return (dgettext(TEXT_DOMAIN,
168 "iscsitgtd failed request to unshare"));
169 case EZFS_SHAREISCSIFAILED:
170 return (dgettext(TEXT_DOMAIN,
171 "iscsitgtd failed request to share"));
173 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
175 case EZFS_POOL_NOTSUP:
176 return (dgettext(TEXT_DOMAIN, "operation not supported "
177 "on this type of pool"));
178 case EZFS_POOL_INVALARG:
179 return (dgettext(TEXT_DOMAIN, "invalid argument for "
180 "this pool operation"));
181 case EZFS_NAMETOOLONG:
182 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
183 case EZFS_OPENFAILED:
184 return (dgettext(TEXT_DOMAIN, "open failed"));
186 return (dgettext(TEXT_DOMAIN,
187 "disk capacity information could not be retrieved"));
188 case EZFS_LABELFAILED:
189 return (dgettext(TEXT_DOMAIN, "write of label failed"));
191 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
193 return (dgettext(TEXT_DOMAIN, "invalid permission"));
194 case EZFS_BADPERMSET:
195 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
196 case EZFS_NODELEGATION:
197 return (dgettext(TEXT_DOMAIN, "delegated administration is "
198 "disabled on pool"));
199 case EZFS_PERMRDONLY:
200 return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
203 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
205 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
206 case EZFS_VDEVNOTSUP:
207 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
210 return (dgettext(TEXT_DOMAIN, "unknown error"));
212 assert(hdl->libzfs_error == 0);
213 return (dgettext(TEXT_DOMAIN, "no error"));
219 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
225 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
227 hdl->libzfs_desc_active = 1;
233 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
235 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
237 hdl->libzfs_error = error;
239 if (hdl->libzfs_desc_active)
240 hdl->libzfs_desc_active = 0;
242 hdl->libzfs_desc[0] = '\0';
244 if (hdl->libzfs_printerr) {
245 if (error == EZFS_UNKNOWN) {
246 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
247 "error: %s\n"), libzfs_error_description(hdl));
251 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
252 libzfs_error_description(hdl));
253 if (error == EZFS_NOMEM)
259 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
261 return (zfs_error_fmt(hdl, error, "%s", msg));
266 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
272 zfs_verror(hdl, error, fmt, ap);
280 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
286 zfs_verror(hdl, EZFS_PERM, fmt, ap);
290 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
294 zfs_verror(hdl, EZFS_IO, fmt, ap);
298 zfs_verror(hdl, EZFS_INTR, fmt, ap);
306 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
308 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
313 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
319 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
327 zfs_verror(hdl, EZFS_IO, fmt, ap);
331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
332 "dataset does not exist"));
333 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
338 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
343 "dataset already exists"));
344 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
350 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
353 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
354 "snapshot permissions cannot be modified"));
355 zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
358 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
361 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
364 zfs_error_aux(hdl, strerror(errno));
365 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
374 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
376 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
381 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
387 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
394 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
399 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
400 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
405 "pool already exists"));
406 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
411 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
416 "one or more devices is currently unavailable"));
417 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
421 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
425 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
429 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
434 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
438 zfs_error_aux(hdl, strerror(error));
439 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
447 * Display an out of memory error message and abort the current program.
450 no_memory(libzfs_handle_t *hdl)
452 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
456 * A safe form of malloc() which will die if the allocation fails.
459 zfs_alloc(libzfs_handle_t *hdl, size_t size)
463 if ((data = calloc(1, size)) == NULL)
464 (void) no_memory(hdl);
470 * A safe form of realloc(), which also zeroes newly allocated space.
473 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
477 if ((ret = realloc(ptr, newsize)) == NULL) {
478 (void) no_memory(hdl);
483 bzero((char *)ret + oldsize, (newsize - oldsize));
488 * A safe form of strdup() which will die if the allocation fails.
491 zfs_strdup(libzfs_handle_t *hdl, const char *str)
495 if ((ret = strdup(str)) == NULL)
496 (void) no_memory(hdl);
502 * Convert a number to an appropriately human-readable output.
505 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
516 u = " KMGTPE"[index];
519 (void) snprintf(buf, buflen, "%llu", n);
520 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
522 * If this is an even multiple of the base, always display
523 * without any decimal precision.
525 (void) snprintf(buf, buflen, "%llu%c", n, u);
528 * We want to choose a precision that reflects the best choice
529 * for fitting in 5 characters. This can get rather tricky when
530 * we have numbers that are very close to an order of magnitude.
531 * For example, when displaying 10239 (which is really 9.999K),
532 * we want only a single place of precision for 10.0K. We could
533 * develop some complex heuristics for this, but it's much
534 * easier just to try each combination in turn.
537 for (i = 2; i >= 0; i--) {
538 if (snprintf(buf, buflen, "%.*f%c", i,
539 (double)num / (1ULL << 10 * index), u) <= 5)
546 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
548 hdl->libzfs_printerr = printerr;
554 libzfs_handle_t *hdl;
556 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
560 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
565 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
566 (void) close(hdl->libzfs_fd);
571 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
580 libzfs_fini(libzfs_handle_t *hdl)
582 (void) close(hdl->libzfs_fd);
583 if (hdl->libzfs_mnttab)
584 (void) fclose(hdl->libzfs_mnttab);
585 if (hdl->libzfs_sharetab)
586 (void) fclose(hdl->libzfs_sharetab);
587 zfs_uninit_libshare(hdl);
588 if (hdl->libzfs_log_str)
589 (void) free(hdl->libzfs_log_str);
590 namespace_clear(hdl);
595 zpool_get_handle(zpool_handle_t *zhp)
597 return (zhp->zpool_hdl);
601 zfs_get_handle(zfs_handle_t *zhp)
603 return (zhp->zfs_hdl);
607 * Given a name, determine whether or not it's a valid path
608 * (starts with '/' or "./"). If so, walk the mnttab trying
609 * to match the device number. If not, treat the path as an
613 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
615 struct stat64 statbuf;
616 struct extmnttab entry;
619 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
621 * It's not a valid path, assume it's a name of type 'argtype'.
623 return (zfs_open(hdl, path, argtype));
626 if (stat64(path, &statbuf) != 0) {
627 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
631 rewind(hdl->libzfs_mnttab);
632 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
633 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
642 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
643 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
648 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
652 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
656 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
660 zc->zc_nvlist_dst_size = len;
661 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
662 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
669 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
670 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
671 * filled in by the kernel to indicate the actual required size.
674 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
676 free((void *)(uintptr_t)zc->zc_nvlist_dst);
677 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
678 zfs_alloc(hdl, zc->zc_nvlist_dst_size))
686 * Called to free the src and dst nvlists stored in the command structure.
689 zcmd_free_nvlists(zfs_cmd_t *zc)
691 free((void *)(uintptr_t)zc->zc_nvlist_conf);
692 free((void *)(uintptr_t)zc->zc_nvlist_src);
693 free((void *)(uintptr_t)zc->zc_nvlist_dst);
697 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
703 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
705 if ((packed = zfs_alloc(hdl, len)) == NULL)
708 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
710 *outnv = (uint64_t)(uintptr_t)packed;
717 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
719 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
720 &zc->zc_nvlist_conf_size, nvl));
724 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
726 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
727 &zc->zc_nvlist_src_size, nvl));
731 * Unpacks an nvlist from the ZFS ioctl command structure.
734 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
736 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
737 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
738 return (no_memory(hdl));
744 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
748 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
749 error = ioctl(hdl->libzfs_fd, request, zc);
750 if (hdl->libzfs_log_str) {
751 free(hdl->libzfs_log_str);
752 hdl->libzfs_log_str = NULL;
760 * ================================================================
761 * API shared by zfs and zpool property management
762 * ================================================================
766 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
768 zprop_list_t *pl = cbp->cb_proplist;
773 cbp->cb_first = B_FALSE;
774 if (cbp->cb_scripted)
778 * Start with the length of the column headers.
780 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
781 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
783 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
785 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
789 * Go through and calculate the widths for each column. For the
790 * 'source' column, we kludge it up by taking the worst-case scenario of
791 * inheriting from the longest name. This is acceptable because in the
792 * majority of cases 'SOURCE' is the last column displayed, and we don't
793 * use the width anyway. Note that the 'VALUE' column can be oversized,
794 * if the name of the property is much longer the any values we find.
796 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
800 if (pl->pl_prop != ZPROP_INVAL) {
801 const char *propname = (type == ZFS_TYPE_POOL) ?
802 zpool_prop_to_name(pl->pl_prop) :
803 zfs_prop_to_name(pl->pl_prop);
805 len = strlen(propname);
806 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
807 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
809 len = strlen(pl->pl_user_prop);
810 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
811 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
817 if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
818 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
819 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
822 * 'NAME' and 'SOURCE' columns
824 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
826 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
827 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
828 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
829 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
834 * Now go through and print the headers.
836 for (i = 0; i < 4; i++) {
837 switch (cbp->cb_columns[i]) {
839 title = dgettext(TEXT_DOMAIN, "NAME");
841 case GET_COL_PROPERTY:
842 title = dgettext(TEXT_DOMAIN, "PROPERTY");
845 title = dgettext(TEXT_DOMAIN, "VALUE");
848 title = dgettext(TEXT_DOMAIN, "SOURCE");
855 if (i == 3 || cbp->cb_columns[i + 1] == 0)
856 (void) printf("%s", title);
858 (void) printf("%-*s ",
859 cbp->cb_colwidths[cbp->cb_columns[i]],
867 * Display a single line of output, according to the settings in the callback
871 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
872 const char *propname, const char *value, zprop_source_t sourcetype,
880 * Ignore those source types that the user has chosen to ignore.
882 if ((sourcetype & cbp->cb_sources) == 0)
886 zprop_print_headers(cbp, cbp->cb_type);
888 for (i = 0; i < 4; i++) {
889 switch (cbp->cb_columns[i]) {
894 case GET_COL_PROPERTY:
903 switch (sourcetype) {
908 case ZPROP_SRC_DEFAULT:
912 case ZPROP_SRC_LOCAL:
916 case ZPROP_SRC_TEMPORARY:
920 case ZPROP_SRC_INHERITED:
921 (void) snprintf(buf, sizeof (buf),
922 "inherited from %s", source);
932 if (cbp->cb_columns[i + 1] == 0)
933 (void) printf("%s", str);
934 else if (cbp->cb_scripted)
935 (void) printf("%s\t", str);
937 (void) printf("%-*s ",
938 cbp->cb_colwidths[cbp->cb_columns[i]],
947 * Given a numeric suffix, convert the value into a number of bits that the
948 * resulting value must be shifted.
951 str2shift(libzfs_handle_t *hdl, const char *buf)
953 const char *ends = "BKMGTPEZ";
958 for (i = 0; i < strlen(ends); i++) {
959 if (toupper(buf[0]) == ends[i])
962 if (i == strlen(ends)) {
963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 "invalid numeric suffix '%s'"), buf);
969 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
970 * allow 'BB' - that's just weird.
972 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
973 toupper(buf[0]) != 'B'))
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "invalid numeric suffix '%s'"), buf);
982 * Convert a string of the form '100G' into a real number. Used when setting
983 * properties or creating a volume. 'buf' is used to place an extended error
984 * message for the caller to use.
987 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
994 /* Check to see if this looks like a number. */
995 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
997 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998 "bad numeric value '%s'"), value);
1002 /* Rely on stroll() to process the numeric portion. */
1004 *num = strtoll(value, &end, 10);
1007 * Check for ERANGE, which indicates that the value is too large to fit
1008 * in a 64-bit value.
1010 if (errno == ERANGE) {
1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013 "numeric value is too large"));
1018 * If we have a decimal value, then do the computation with floating
1019 * point arithmetic. Otherwise, use standard arithmetic.
1022 double fval = strtod(value, &end);
1024 if ((shift = str2shift(hdl, end)) == -1)
1027 fval *= pow(2, shift);
1029 if (fval > UINT64_MAX) {
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "numeric value is too large"));
1036 *num = (uint64_t)fval;
1038 if ((shift = str2shift(hdl, end)) == -1)
1041 /* Check for overflow */
1042 if (shift >= 64 || (*num << shift) >> shift != *num) {
1044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1045 "numeric value is too large"));
1056 * Given a propname=value nvpair to set, parse any numeric properties
1057 * (index, boolean, etc) if they are specified as strings and add the
1058 * resulting nvpair to the returned nvlist.
1060 * At the DSL layer, all properties are either 64-bit numbers or strings.
1061 * We want the user to be able to ignore this fact and specify properties
1062 * as native values (numbers, for example) or as strings (to simplify
1063 * command line utilities). This also handles converting index types
1064 * (compression, checksum, etc) from strings to their on-disk index.
1067 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1068 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1071 data_type_t datatype = nvpair_type(elem);
1072 zprop_type_t proptype;
1073 const char *propname;
1075 boolean_t isnone = B_FALSE;
1077 if (type == ZFS_TYPE_POOL) {
1078 proptype = zpool_prop_get_type(prop);
1079 propname = zpool_prop_to_name(prop);
1081 proptype = zfs_prop_get_type(prop);
1082 propname = zfs_prop_to_name(prop);
1086 * Convert any properties to the internal DSL value types.
1092 case PROP_TYPE_STRING:
1093 if (datatype != DATA_TYPE_STRING) {
1094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1095 "'%s' must be a string"), nvpair_name(elem));
1098 (void) nvpair_value_string(elem, svalp);
1099 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1101 "'%s' is too long"), nvpair_name(elem));
1106 case PROP_TYPE_NUMBER:
1107 if (datatype == DATA_TYPE_STRING) {
1108 (void) nvpair_value_string(elem, &value);
1109 if (strcmp(value, "none") == 0) {
1111 } else if (zfs_nicestrtonum(hdl, value, ivalp)
1115 } else if (datatype == DATA_TYPE_UINT64) {
1116 (void) nvpair_value_uint64(elem, ivalp);
1118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1119 "'%s' must be a number"), nvpair_name(elem));
1124 * Quota special: force 'none' and don't allow 0.
1126 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1127 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129 "use 'none' to disable quota/refquota"));
1134 case PROP_TYPE_INDEX:
1135 if (datatype != DATA_TYPE_STRING) {
1136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1137 "'%s' must be a string"), nvpair_name(elem));
1141 (void) nvpair_value_string(elem, &value);
1143 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1145 "'%s' must be one of '%s'"), propname,
1146 zprop_values(prop, type));
1156 * Add the result to our return set of properties.
1158 if (*svalp != NULL) {
1159 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1160 (void) no_memory(hdl);
1164 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1165 (void) no_memory(hdl);
1172 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1177 * Given a comma-separated list of properties, construct a property list
1178 * containing both user-defined and native properties. This function will
1179 * return a NULL list if 'all' is specified, which can later be expanded
1180 * by zprop_expand_list().
1183 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1190 zprop_list_t *entry;
1191 zprop_list_t **last;
1197 * If 'all' is specified, return a NULL list.
1199 if (strcmp(props, "all") == 0)
1203 * If no props were specified, return an error.
1205 if (props[0] == '\0') {
1206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1207 "no properties specified"));
1208 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1209 "bad property list")));
1213 * It would be nice to use getsubopt() here, but the inclusion of column
1214 * aliases makes this more effort than it's worth.
1217 while (*s != '\0') {
1218 if ((p = strchr(s, ',')) == NULL) {
1226 * Check for empty options.
1229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1230 "empty property name"));
1231 return (zfs_error(hdl, EZFS_BADPROP,
1232 dgettext(TEXT_DOMAIN, "bad property list")));
1236 * Check all regular property names.
1240 prop = zprop_name_to_prop(s, type);
1242 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1246 * When no property table entry can be found, return failure if
1247 * this is a pool property or if this isn't a user-defined
1250 if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1251 !zfs_prop_user(s))) {
1252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253 "invalid property '%s'"), s);
1254 return (zfs_error(hdl, EZFS_BADPROP,
1255 dgettext(TEXT_DOMAIN, "bad property list")));
1258 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1261 entry->pl_prop = prop;
1262 if (prop == ZPROP_INVAL) {
1263 if ((entry->pl_user_prop = zfs_strdup(hdl, s))
1268 entry->pl_width = strlen(s);
1270 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1275 last = &entry->pl_next;
1286 zprop_free_list(zprop_list_t *pl)
1290 while (pl != NULL) {
1292 free(pl->pl_user_prop);
1298 typedef struct expand_data {
1299 zprop_list_t **last;
1300 libzfs_handle_t *hdl;
1305 zprop_expand_list_cb(int prop, void *cb)
1307 zprop_list_t *entry;
1308 expand_data_t *edp = cb;
1310 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1311 return (ZPROP_INVAL);
1313 entry->pl_prop = prop;
1314 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1315 entry->pl_all = B_TRUE;
1317 *(edp->last) = entry;
1318 edp->last = &entry->pl_next;
1320 return (ZPROP_CONT);
1324 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1326 zprop_list_t *entry;
1327 zprop_list_t **last;
1332 * If this is the very first time we've been called for an 'all'
1333 * specification, expand the list to include all native
1342 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1343 B_FALSE, type) == ZPROP_INVAL)
1347 * Add 'name' to the beginning of the list, which is handled
1350 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1353 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1355 entry->pl_width = zprop_width(entry->pl_prop,
1356 &entry->pl_fixed, type);
1357 entry->pl_all = B_TRUE;
1358 entry->pl_next = *plp;
1365 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1368 return (zprop_iter_common(func, cb, show_all, ordered, type));