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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
30 * Internal utility routines for the ZFS library.
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
48 #include <sys/mnttab.h>
49 #include <sys/mntent.h>
50 #include <sys/types.h>
51 #include <libcmdutils.h>
54 #include <libzfs_core.h>
56 #include "libzfs_impl.h"
58 #include "zfeature_common.h"
62 libzfs_errno(libzfs_handle_t *hdl)
64 return (hdl->libzfs_error);
68 libzfs_error_action(libzfs_handle_t *hdl)
70 return (hdl->libzfs_action);
74 libzfs_error_description(libzfs_handle_t *hdl)
76 if (hdl->libzfs_desc[0] != '\0')
77 return (hdl->libzfs_desc);
79 switch (hdl->libzfs_error) {
81 return (dgettext(TEXT_DOMAIN, "out of memory"));
83 return (dgettext(TEXT_DOMAIN, "invalid property value"));
84 case EZFS_PROPREADONLY:
85 return (dgettext(TEXT_DOMAIN, "read-only property"));
87 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
88 "datasets of this type"));
89 case EZFS_PROPNONINHERIT:
90 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
92 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
94 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
95 "datasets of this type"));
97 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
99 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
101 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
103 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
104 case EZFS_DSREADONLY:
105 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
107 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
109 case EZFS_INVALIDNAME:
110 return (dgettext(TEXT_DOMAIN, "invalid name"));
111 case EZFS_BADRESTORE:
112 return (dgettext(TEXT_DOMAIN, "unable to restore to "
115 return (dgettext(TEXT_DOMAIN, "backup failed"));
117 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
119 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
121 return (dgettext(TEXT_DOMAIN, "invalid device"));
122 case EZFS_NOREPLICAS:
123 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
124 case EZFS_RESILVERING:
125 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
126 case EZFS_BADVERSION:
127 return (dgettext(TEXT_DOMAIN, "unsupported version or "
129 case EZFS_POOLUNAVAIL:
130 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
131 case EZFS_DEVOVERFLOW:
132 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
134 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
135 case EZFS_CROSSTARGET:
136 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
139 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
140 case EZFS_MOUNTFAILED:
141 return (dgettext(TEXT_DOMAIN, "mount failed"));
142 case EZFS_UMOUNTFAILED:
143 return (dgettext(TEXT_DOMAIN, "umount failed"));
144 case EZFS_UNSHARENFSFAILED:
145 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
146 case EZFS_SHARENFSFAILED:
147 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
148 case EZFS_UNSHARESMBFAILED:
149 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
150 case EZFS_SHARESMBFAILED:
151 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
153 return (dgettext(TEXT_DOMAIN, "permission denied"));
155 return (dgettext(TEXT_DOMAIN, "out of space"));
157 return (dgettext(TEXT_DOMAIN, "bad address"));
159 return (dgettext(TEXT_DOMAIN, "I/O error"));
161 return (dgettext(TEXT_DOMAIN, "signal received"));
163 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
165 case EZFS_INVALCONFIG:
166 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
168 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
170 return (dgettext(TEXT_DOMAIN, "no history available"));
172 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
174 case EZFS_POOL_NOTSUP:
175 return (dgettext(TEXT_DOMAIN, "operation not supported "
176 "on this type of pool"));
177 case EZFS_POOL_INVALARG:
178 return (dgettext(TEXT_DOMAIN, "invalid argument for "
179 "this pool operation"));
180 case EZFS_NAMETOOLONG:
181 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
182 case EZFS_OPENFAILED:
183 return (dgettext(TEXT_DOMAIN, "open failed"));
185 return (dgettext(TEXT_DOMAIN,
186 "disk capacity information could not be retrieved"));
187 case EZFS_LABELFAILED:
188 return (dgettext(TEXT_DOMAIN, "write of label failed"));
190 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
192 return (dgettext(TEXT_DOMAIN, "invalid permission"));
193 case EZFS_BADPERMSET:
194 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
195 case EZFS_NODELEGATION:
196 return (dgettext(TEXT_DOMAIN, "delegated administration is "
197 "disabled on pool"));
199 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
201 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
202 case EZFS_VDEVNOTSUP:
203 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
206 return (dgettext(TEXT_DOMAIN, "operation not supported "
208 case EZFS_ACTIVE_SPARE:
209 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
211 case EZFS_UNPLAYED_LOGS:
212 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
214 case EZFS_REFTAG_RELE:
215 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
216 case EZFS_REFTAG_HOLD:
217 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
219 case EZFS_TAGTOOLONG:
220 return (dgettext(TEXT_DOMAIN, "tag too long"));
221 case EZFS_PIPEFAILED:
222 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
223 case EZFS_THREADCREATEFAILED:
224 return (dgettext(TEXT_DOMAIN, "thread create failed"));
225 case EZFS_POSTSPLIT_ONLINE:
226 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
229 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
230 "use 'zpool scrub -s' to cancel current scrub"));
232 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
234 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
236 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
237 case EZFS_POOLREADONLY:
238 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
240 return (dgettext(TEXT_DOMAIN, "unknown error"));
242 assert(hdl->libzfs_error == 0);
243 return (dgettext(TEXT_DOMAIN, "no error"));
249 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
255 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
257 hdl->libzfs_desc_active = 1;
263 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
265 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
267 hdl->libzfs_error = error;
269 if (hdl->libzfs_desc_active)
270 hdl->libzfs_desc_active = 0;
272 hdl->libzfs_desc[0] = '\0';
274 if (hdl->libzfs_printerr) {
275 if (error == EZFS_UNKNOWN) {
276 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
277 "error: %s\n"), libzfs_error_description(hdl));
281 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
282 libzfs_error_description(hdl));
283 if (error == EZFS_NOMEM)
289 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
291 return (zfs_error_fmt(hdl, error, "%s", msg));
296 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
302 zfs_verror(hdl, error, fmt, ap);
310 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
316 zfs_verror(hdl, EZFS_PERM, fmt, ap);
320 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
324 zfs_verror(hdl, EZFS_IO, fmt, ap);
328 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
332 zfs_verror(hdl, EZFS_INTR, fmt, ap);
340 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
342 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
347 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
353 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
362 zfs_verror(hdl, EZFS_IO, fmt, ap);
366 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
367 "dataset does not exist"));
368 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
373 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
379 "dataset already exists"));
380 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
386 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
389 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
392 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
395 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399 "pool I/O is currently suspended"));
400 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
403 zfs_error_aux(hdl, strerror(error));
404 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
413 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
415 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
420 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
426 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
433 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
438 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
439 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
444 "pool already exists"));
445 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
450 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
455 "one or more devices is currently unavailable"));
456 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
460 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
464 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
468 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
473 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
479 "pool I/O is currently suspended"));
480 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
484 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
488 zfs_error_aux(hdl, strerror(error));
489 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
497 * Display an out of memory error message and abort the current program.
500 no_memory(libzfs_handle_t *hdl)
502 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
506 * A safe form of malloc() which will die if the allocation fails.
509 zfs_alloc(libzfs_handle_t *hdl, size_t size)
513 if ((data = calloc(1, size)) == NULL)
514 (void) no_memory(hdl);
520 * A safe form of asprintf() which will die if the allocation fails.
524 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
532 err = vasprintf(&ret, fmt, ap);
537 (void) no_memory(hdl);
543 * A safe form of realloc(), which also zeroes newly allocated space.
546 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
550 if ((ret = realloc(ptr, newsize)) == NULL) {
551 (void) no_memory(hdl);
555 bzero((char *)ret + oldsize, (newsize - oldsize));
560 * A safe form of strdup() which will die if the allocation fails.
563 zfs_strdup(libzfs_handle_t *hdl, const char *str)
567 if ((ret = strdup(str)) == NULL)
568 (void) no_memory(hdl);
574 * Convert a number to an appropriately human-readable output.
577 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
579 nicenum(num, buf, buflen);
583 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
585 hdl->libzfs_printerr = printerr;
593 if (modfind("zfs") < 0) {
594 /* Not present in kernel, try loading it. */
595 if (kldload("zfs") < 0 || modfind("zfs") < 0) {
606 libzfs_handle_t *hdl;
608 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
612 if (libzfs_load() < 0) {
617 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
622 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
623 (void) close(hdl->libzfs_fd);
628 hdl->libzfs_sharetab = fopen(ZFS_EXPORTS_PATH, "r");
630 if (libzfs_core_init() != 0) {
631 (void) close(hdl->libzfs_fd);
632 (void) fclose(hdl->libzfs_mnttab);
633 (void) fclose(hdl->libzfs_sharetab);
640 zpool_feature_init();
641 libzfs_mnttab_init(hdl);
647 libzfs_fini(libzfs_handle_t *hdl)
649 (void) close(hdl->libzfs_fd);
650 if (hdl->libzfs_mnttab)
651 (void) fclose(hdl->libzfs_mnttab);
652 if (hdl->libzfs_sharetab)
653 (void) fclose(hdl->libzfs_sharetab);
654 zfs_uninit_libshare(hdl);
655 zpool_free_handles(hdl);
657 libzfs_fru_clear(hdl, B_TRUE);
659 namespace_clear(hdl);
660 libzfs_mnttab_fini(hdl);
666 zpool_get_handle(zpool_handle_t *zhp)
668 return (zhp->zpool_hdl);
672 zfs_get_handle(zfs_handle_t *zhp)
674 return (zhp->zfs_hdl);
678 zfs_get_pool_handle(const zfs_handle_t *zhp)
680 return (zhp->zpool_hdl);
684 * Given a name, determine whether or not it's a valid path
685 * (starts with '/' or "./"). If so, walk the mnttab trying
686 * to match the device number. If not, treat the path as an
690 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
692 struct stat64 statbuf;
693 struct extmnttab entry;
696 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
698 * It's not a valid path, assume it's a name of type 'argtype'.
700 return (zfs_open(hdl, path, argtype));
703 if (stat64(path, &statbuf) != 0) {
704 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
709 rewind(hdl->libzfs_mnttab);
710 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
711 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
720 ret = statfs(path, &sfs);
722 statfs2mnttab(&sfs, &entry);
724 (void) fprintf(stderr, "%s: %s\n", path,
733 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
734 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
739 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
743 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
747 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
751 zc->zc_nvlist_dst_size = len;
753 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
754 if (zc->zc_nvlist_dst == 0)
761 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
762 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
763 * filled in by the kernel to indicate the actual required size.
766 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
768 free((void *)(uintptr_t)zc->zc_nvlist_dst);
770 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
771 if (zc->zc_nvlist_dst == 0)
778 * Called to free the src and dst nvlists stored in the command structure.
781 zcmd_free_nvlists(zfs_cmd_t *zc)
783 free((void *)(uintptr_t)zc->zc_nvlist_conf);
784 free((void *)(uintptr_t)zc->zc_nvlist_src);
785 free((void *)(uintptr_t)zc->zc_nvlist_dst);
786 zc->zc_nvlist_conf = NULL;
787 zc->zc_nvlist_src = NULL;
788 zc->zc_nvlist_dst = NULL;
792 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
798 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
800 if ((packed = zfs_alloc(hdl, len)) == NULL)
803 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
805 *outnv = (uint64_t)(uintptr_t)packed;
812 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
814 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
815 &zc->zc_nvlist_conf_size, nvl));
819 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
821 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
822 &zc->zc_nvlist_src_size, nvl));
826 * Unpacks an nvlist from the ZFS ioctl command structure.
829 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
831 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
832 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
833 return (no_memory(hdl));
839 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
841 return (ioctl(hdl->libzfs_fd, request, zc));
845 * ================================================================
846 * API shared by zfs and zpool property management
847 * ================================================================
851 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
853 zprop_list_t *pl = cbp->cb_proplist;
858 cbp->cb_first = B_FALSE;
859 if (cbp->cb_scripted)
863 * Start with the length of the column headers.
865 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
866 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
868 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
870 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
872 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
875 /* first property is always NAME */
876 assert(cbp->cb_proplist->pl_prop ==
877 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
880 * Go through and calculate the widths for each column. For the
881 * 'source' column, we kludge it up by taking the worst-case scenario of
882 * inheriting from the longest name. This is acceptable because in the
883 * majority of cases 'SOURCE' is the last column displayed, and we don't
884 * use the width anyway. Note that the 'VALUE' column can be oversized,
885 * if the name of the property is much longer than any values we find.
887 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
891 if (pl->pl_prop != ZPROP_INVAL) {
892 const char *propname = (type == ZFS_TYPE_POOL) ?
893 zpool_prop_to_name(pl->pl_prop) :
894 zfs_prop_to_name(pl->pl_prop);
896 len = strlen(propname);
897 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
898 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
900 len = strlen(pl->pl_user_prop);
901 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
902 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
906 * 'VALUE' column. The first property is always the 'name'
907 * property that was tacked on either by /sbin/zfs's
908 * zfs_do_get() or when calling zprop_expand_list(), so we
909 * ignore its width. If the user specified the name property
910 * to display, then it will be later in the list in any case.
912 if (pl != cbp->cb_proplist &&
913 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
914 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
916 /* 'RECEIVED' column. */
917 if (pl != cbp->cb_proplist &&
918 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
919 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
922 * 'NAME' and 'SOURCE' columns
924 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
926 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
927 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
928 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
929 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
934 * Now go through and print the headers.
936 for (i = 0; i < ZFS_GET_NCOLS; i++) {
937 switch (cbp->cb_columns[i]) {
939 title = dgettext(TEXT_DOMAIN, "NAME");
941 case GET_COL_PROPERTY:
942 title = dgettext(TEXT_DOMAIN, "PROPERTY");
945 title = dgettext(TEXT_DOMAIN, "VALUE");
948 title = dgettext(TEXT_DOMAIN, "RECEIVED");
951 title = dgettext(TEXT_DOMAIN, "SOURCE");
958 if (i == (ZFS_GET_NCOLS - 1) ||
959 cbp->cb_columns[i + 1] == GET_COL_NONE)
960 (void) printf("%s", title);
962 (void) printf("%-*s ",
963 cbp->cb_colwidths[cbp->cb_columns[i]],
971 * Display a single line of output, according to the settings in the callback
975 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
976 const char *propname, const char *value, zprop_source_t sourcetype,
977 const char *source, const char *recvd_value)
980 const char *str = NULL;
984 * Ignore those source types that the user has chosen to ignore.
986 if ((sourcetype & cbp->cb_sources) == 0)
990 zprop_print_headers(cbp, cbp->cb_type);
992 for (i = 0; i < ZFS_GET_NCOLS; i++) {
993 switch (cbp->cb_columns[i]) {
998 case GET_COL_PROPERTY:
1006 case GET_COL_SOURCE:
1007 switch (sourcetype) {
1008 case ZPROP_SRC_NONE:
1012 case ZPROP_SRC_DEFAULT:
1016 case ZPROP_SRC_LOCAL:
1020 case ZPROP_SRC_TEMPORARY:
1024 case ZPROP_SRC_INHERITED:
1025 (void) snprintf(buf, sizeof (buf),
1026 "inherited from %s", source);
1029 case ZPROP_SRC_RECEIVED:
1035 assert(!"unhandled zprop_source_t");
1040 str = (recvd_value == NULL ? "-" : recvd_value);
1047 if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1048 (void) printf("%s", str);
1049 else if (cbp->cb_scripted)
1050 (void) printf("%s\t", str);
1052 (void) printf("%-*s ",
1053 cbp->cb_colwidths[cbp->cb_columns[i]],
1057 (void) printf("\n");
1061 * Given a numeric suffix, convert the value into a number of bits that the
1062 * resulting value must be shifted.
1065 str2shift(libzfs_handle_t *hdl, const char *buf)
1067 const char *ends = "BKMGTPEZ";
1072 for (i = 0; i < strlen(ends); i++) {
1073 if (toupper(buf[0]) == ends[i])
1076 if (i == strlen(ends)) {
1077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1078 "invalid numeric suffix '%s'"), buf);
1083 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
1084 * allow 'BB' - that's just weird.
1086 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1087 toupper(buf[0]) != 'B'))
1090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1091 "invalid numeric suffix '%s'"), buf);
1096 * Convert a string of the form '100G' into a real number. Used when setting
1097 * properties or creating a volume. 'buf' is used to place an extended error
1098 * message for the caller to use.
1101 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1108 /* Check to see if this looks like a number. */
1109 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1112 "bad numeric value '%s'"), value);
1116 /* Rely on strtoull() to process the numeric portion. */
1118 *num = strtoull(value, &end, 10);
1121 * Check for ERANGE, which indicates that the value is too large to fit
1122 * in a 64-bit value.
1124 if (errno == ERANGE) {
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127 "numeric value is too large"));
1132 * If we have a decimal value, then do the computation with floating
1133 * point arithmetic. Otherwise, use standard arithmetic.
1136 double fval = strtod(value, &end);
1138 if ((shift = str2shift(hdl, end)) == -1)
1141 fval *= pow(2, shift);
1143 if (fval > UINT64_MAX) {
1145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1146 "numeric value is too large"));
1150 *num = (uint64_t)fval;
1152 if ((shift = str2shift(hdl, end)) == -1)
1155 /* Check for overflow */
1156 if (shift >= 64 || (*num << shift) >> shift != *num) {
1158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1159 "numeric value is too large"));
1170 * Given a propname=value nvpair to set, parse any numeric properties
1171 * (index, boolean, etc) if they are specified as strings and add the
1172 * resulting nvpair to the returned nvlist.
1174 * At the DSL layer, all properties are either 64-bit numbers or strings.
1175 * We want the user to be able to ignore this fact and specify properties
1176 * as native values (numbers, for example) or as strings (to simplify
1177 * command line utilities). This also handles converting index types
1178 * (compression, checksum, etc) from strings to their on-disk index.
1181 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1182 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1185 data_type_t datatype = nvpair_type(elem);
1186 zprop_type_t proptype;
1187 const char *propname;
1189 boolean_t isnone = B_FALSE;
1191 if (type == ZFS_TYPE_POOL) {
1192 proptype = zpool_prop_get_type(prop);
1193 propname = zpool_prop_to_name(prop);
1195 proptype = zfs_prop_get_type(prop);
1196 propname = zfs_prop_to_name(prop);
1200 * Convert any properties to the internal DSL value types.
1206 case PROP_TYPE_STRING:
1207 if (datatype != DATA_TYPE_STRING) {
1208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1209 "'%s' must be a string"), nvpair_name(elem));
1212 (void) nvpair_value_string(elem, svalp);
1213 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1215 "'%s' is too long"), nvpair_name(elem));
1220 case PROP_TYPE_NUMBER:
1221 if (datatype == DATA_TYPE_STRING) {
1222 (void) nvpair_value_string(elem, &value);
1223 if (strcmp(value, "none") == 0) {
1225 } else if (zfs_nicestrtonum(hdl, value, ivalp)
1229 } else if (datatype == DATA_TYPE_UINT64) {
1230 (void) nvpair_value_uint64(elem, ivalp);
1232 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1233 "'%s' must be a number"), nvpair_name(elem));
1238 * Quota special: force 'none' and don't allow 0.
1240 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1241 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1242 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1243 "use 'none' to disable quota/refquota"));
1248 * Special handling for "*_limit=none". In this case it's not
1251 if ((type & ZFS_TYPE_DATASET) && isnone &&
1252 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1253 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1254 *ivalp = UINT64_MAX;
1258 case PROP_TYPE_INDEX:
1259 if (datatype != DATA_TYPE_STRING) {
1260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1261 "'%s' must be a string"), nvpair_name(elem));
1265 (void) nvpair_value_string(elem, &value);
1267 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "'%s' must be one of '%s'"), propname,
1270 zprop_values(prop, type));
1280 * Add the result to our return set of properties.
1282 if (*svalp != NULL) {
1283 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1284 (void) no_memory(hdl);
1288 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1289 (void) no_memory(hdl);
1296 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1301 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1305 zprop_list_t *entry;
1307 prop = zprop_name_to_prop(propname, type);
1309 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1313 * When no property table entry can be found, return failure if
1314 * this is a pool property or if this isn't a user-defined
1317 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1318 !zpool_prop_feature(propname) &&
1319 !zpool_prop_unsupported(propname)) ||
1320 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1321 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1323 "invalid property '%s'"), propname);
1324 return (zfs_error(hdl, EZFS_BADPROP,
1325 dgettext(TEXT_DOMAIN, "bad property list")));
1328 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1331 entry->pl_prop = prop;
1332 if (prop == ZPROP_INVAL) {
1333 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1338 entry->pl_width = strlen(propname);
1340 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1350 * Given a comma-separated list of properties, construct a property list
1351 * containing both user-defined and native properties. This function will
1352 * return a NULL list if 'all' is specified, which can later be expanded
1353 * by zprop_expand_list().
1356 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1362 * If 'all' is specified, return a NULL list.
1364 if (strcmp(props, "all") == 0)
1368 * If no props were specified, return an error.
1370 if (props[0] == '\0') {
1371 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1372 "no properties specified"));
1373 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1374 "bad property list")));
1378 * It would be nice to use getsubopt() here, but the inclusion of column
1379 * aliases makes this more effort than it's worth.
1381 while (*props != '\0') {
1386 if ((p = strchr(props, ',')) == NULL) {
1387 len = strlen(props);
1394 * Check for empty options.
1397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1398 "empty property name"));
1399 return (zfs_error(hdl, EZFS_BADPROP,
1400 dgettext(TEXT_DOMAIN, "bad property list")));
1404 * Check all regular property names.
1409 if (strcmp(props, "space") == 0) {
1410 static char *spaceprops[] = {
1411 "name", "avail", "used", "usedbysnapshots",
1412 "usedbydataset", "usedbyrefreservation",
1413 "usedbychildren", NULL
1417 for (i = 0; spaceprops[i]; i++) {
1418 if (addlist(hdl, spaceprops[i], listp, type))
1420 listp = &(*listp)->pl_next;
1423 if (addlist(hdl, props, listp, type))
1425 listp = &(*listp)->pl_next;
1437 zprop_free_list(zprop_list_t *pl)
1441 while (pl != NULL) {
1443 free(pl->pl_user_prop);
1449 typedef struct expand_data {
1450 zprop_list_t **last;
1451 libzfs_handle_t *hdl;
1456 zprop_expand_list_cb(int prop, void *cb)
1458 zprop_list_t *entry;
1459 expand_data_t *edp = cb;
1461 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1462 return (ZPROP_INVAL);
1464 entry->pl_prop = prop;
1465 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1466 entry->pl_all = B_TRUE;
1468 *(edp->last) = entry;
1469 edp->last = &entry->pl_next;
1471 return (ZPROP_CONT);
1475 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1477 zprop_list_t *entry;
1478 zprop_list_t **last;
1483 * If this is the very first time we've been called for an 'all'
1484 * specification, expand the list to include all native
1493 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1494 B_FALSE, type) == ZPROP_INVAL)
1498 * Add 'name' to the beginning of the list, which is handled
1501 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1504 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1506 entry->pl_width = zprop_width(entry->pl_prop,
1507 &entry->pl_fixed, type);
1508 entry->pl_all = B_TRUE;
1509 entry->pl_next = *plp;
1516 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1519 return (zprop_iter_common(func, cb, show_all, ordered, type));