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 2019 Joyent, Inc.
25 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
31 * Internal utility routines for the ZFS library.
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
49 #include <sys/mnttab.h>
50 #include <sys/mntent.h>
51 #include <sys/types.h>
52 #include <libcmdutils.h>
55 #include <libzfs_core.h>
57 #include "libzfs_impl.h"
59 #include "zfs_comutil.h"
60 #include "zfeature_common.h"
64 libzfs_errno(libzfs_handle_t *hdl)
66 return (hdl->libzfs_error);
70 libzfs_error_action(libzfs_handle_t *hdl)
72 return (hdl->libzfs_action);
76 libzfs_error_description(libzfs_handle_t *hdl)
78 if (hdl->libzfs_desc[0] != '\0')
79 return (hdl->libzfs_desc);
81 switch (hdl->libzfs_error) {
83 return (dgettext(TEXT_DOMAIN, "out of memory"));
85 return (dgettext(TEXT_DOMAIN, "invalid property value"));
86 case EZFS_PROPREADONLY:
87 return (dgettext(TEXT_DOMAIN, "read-only property"));
89 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
90 "datasets of this type"));
91 case EZFS_PROPNONINHERIT:
92 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
94 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
96 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
97 "datasets of this type"));
99 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
101 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
103 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
105 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
106 case EZFS_DSREADONLY:
107 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
109 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
111 case EZFS_INVALIDNAME:
112 return (dgettext(TEXT_DOMAIN, "invalid name"));
113 case EZFS_BADRESTORE:
114 return (dgettext(TEXT_DOMAIN, "unable to restore to "
117 return (dgettext(TEXT_DOMAIN, "backup failed"));
119 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
121 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
123 return (dgettext(TEXT_DOMAIN, "invalid device"));
124 case EZFS_NOREPLICAS:
125 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
126 case EZFS_RESILVERING:
127 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
128 case EZFS_BADVERSION:
129 return (dgettext(TEXT_DOMAIN, "unsupported version or "
131 case EZFS_POOLUNAVAIL:
132 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
133 case EZFS_DEVOVERFLOW:
134 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
136 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
137 case EZFS_CROSSTARGET:
138 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
141 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
142 case EZFS_MOUNTFAILED:
143 return (dgettext(TEXT_DOMAIN, "mount failed"));
144 case EZFS_UMOUNTFAILED:
145 return (dgettext(TEXT_DOMAIN, "umount failed"));
146 case EZFS_UNSHARENFSFAILED:
147 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
148 case EZFS_SHARENFSFAILED:
149 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
150 case EZFS_UNSHARESMBFAILED:
151 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
152 case EZFS_SHARESMBFAILED:
153 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
155 return (dgettext(TEXT_DOMAIN, "permission denied"));
157 return (dgettext(TEXT_DOMAIN, "out of space"));
159 return (dgettext(TEXT_DOMAIN, "bad address"));
161 return (dgettext(TEXT_DOMAIN, "I/O error"));
163 return (dgettext(TEXT_DOMAIN, "signal received"));
165 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
167 case EZFS_INVALCONFIG:
168 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
170 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
172 return (dgettext(TEXT_DOMAIN, "no history available"));
174 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
176 case EZFS_POOL_NOTSUP:
177 return (dgettext(TEXT_DOMAIN, "operation not supported "
178 "on this type of pool"));
179 case EZFS_POOL_INVALARG:
180 return (dgettext(TEXT_DOMAIN, "invalid argument for "
181 "this pool operation"));
182 case EZFS_NAMETOOLONG:
183 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
184 case EZFS_OPENFAILED:
185 return (dgettext(TEXT_DOMAIN, "open failed"));
187 return (dgettext(TEXT_DOMAIN,
188 "disk capacity information could not be retrieved"));
189 case EZFS_LABELFAILED:
190 return (dgettext(TEXT_DOMAIN, "write of label failed"));
192 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
194 return (dgettext(TEXT_DOMAIN, "invalid permission"));
195 case EZFS_BADPERMSET:
196 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
197 case EZFS_NODELEGATION:
198 return (dgettext(TEXT_DOMAIN, "delegated administration is "
199 "disabled on pool"));
201 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
203 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
204 case EZFS_VDEVNOTSUP:
205 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
208 return (dgettext(TEXT_DOMAIN, "operation not supported "
210 case EZFS_IOC_NOTSUPPORTED:
211 return (dgettext(TEXT_DOMAIN, "operation not supported by "
212 "zfs kernel module"));
213 case EZFS_ACTIVE_SPARE:
214 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
216 case EZFS_UNPLAYED_LOGS:
217 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
219 case EZFS_REFTAG_RELE:
220 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
221 case EZFS_REFTAG_HOLD:
222 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
224 case EZFS_TAGTOOLONG:
225 return (dgettext(TEXT_DOMAIN, "tag too long"));
226 case EZFS_PIPEFAILED:
227 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
228 case EZFS_THREADCREATEFAILED:
229 return (dgettext(TEXT_DOMAIN, "thread create failed"));
230 case EZFS_POSTSPLIT_ONLINE:
231 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
233 case EZFS_SCRUB_PAUSED:
234 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
235 "use 'zpool scrub' to resume"));
237 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
238 "use 'zpool scrub -s' to cancel current scrub"));
240 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
242 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
244 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
245 case EZFS_POOLREADONLY:
246 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
247 case EZFS_NO_PENDING:
248 return (dgettext(TEXT_DOMAIN, "operation is not "
250 case EZFS_CHECKPOINT_EXISTS:
251 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
252 case EZFS_DISCARDING_CHECKPOINT:
253 return (dgettext(TEXT_DOMAIN, "currently discarding "
255 case EZFS_NO_CHECKPOINT:
256 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
257 case EZFS_DEVRM_IN_PROGRESS:
258 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
259 case EZFS_VDEV_TOO_BIG:
260 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
261 case EZFS_ACTIVE_POOL:
262 return (dgettext(TEXT_DOMAIN, "pool is imported on a "
265 return (dgettext(TEXT_DOMAIN, "argument list too long"));
266 case EZFS_INITIALIZING:
267 return (dgettext(TEXT_DOMAIN, "currently initializing"));
268 case EZFS_NO_INITIALIZE:
269 return (dgettext(TEXT_DOMAIN, "there is no active "
271 case EZFS_WRONG_PARENT:
272 return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
274 return (dgettext(TEXT_DOMAIN, "unknown error"));
276 assert(hdl->libzfs_error == 0);
277 return (dgettext(TEXT_DOMAIN, "no error"));
283 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
289 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
291 hdl->libzfs_desc_active = 1;
297 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
299 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
301 hdl->libzfs_error = error;
303 if (hdl->libzfs_desc_active)
304 hdl->libzfs_desc_active = 0;
306 hdl->libzfs_desc[0] = '\0';
308 if (hdl->libzfs_printerr) {
309 if (error == EZFS_UNKNOWN) {
310 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
311 "error: %s\n"), libzfs_error_description(hdl));
315 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
316 libzfs_error_description(hdl));
317 if (error == EZFS_NOMEM)
323 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
325 return (zfs_error_fmt(hdl, error, "%s", msg));
330 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
336 zfs_verror(hdl, error, fmt, ap);
344 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
350 zfs_verror(hdl, EZFS_PERM, fmt, ap);
354 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
358 zfs_verror(hdl, EZFS_IO, fmt, ap);
362 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
366 zfs_verror(hdl, EZFS_INTR, fmt, ap);
374 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
376 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
381 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
387 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
396 zfs_verror(hdl, EZFS_IO, fmt, ap);
400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
401 "dataset does not exist"));
402 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
407 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
413 "dataset already exists"));
414 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
420 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
423 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
426 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
429 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
433 "pool I/O is currently suspended"));
434 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
437 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
439 case ZFS_ERR_IOC_CMD_UNAVAIL:
440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
441 "module does not support this operation. A reboot may "
442 "be required to enable this operation."));
443 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
445 case ZFS_ERR_IOC_ARG_UNAVAIL:
446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
447 "module does not support an option for this operation. "
448 "A reboot may be required to enable this option."));
449 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
451 case ZFS_ERR_IOC_ARG_REQUIRED:
452 case ZFS_ERR_IOC_ARG_BADTYPE:
453 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
456 zfs_error_aux(hdl, strerror(error));
457 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
466 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
468 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
473 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
479 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
486 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
491 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
492 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
497 "pool already exists"));
498 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
503 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
507 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
508 "one or more devices is currently unavailable"));
509 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
513 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
517 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
521 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
526 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
532 "pool I/O is currently suspended"));
533 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
537 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
539 /* There is no pending operation to cancel */
541 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
544 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
546 case ZFS_ERR_CHECKPOINT_EXISTS:
547 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
549 case ZFS_ERR_DISCARDING_CHECKPOINT:
550 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
552 case ZFS_ERR_NO_CHECKPOINT:
553 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
555 case ZFS_ERR_DEVRM_IN_PROGRESS:
556 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
558 case ZFS_ERR_VDEV_TOO_BIG:
559 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
561 case ZFS_ERR_WRONG_PARENT:
562 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
564 case ZFS_ERR_IOC_CMD_UNAVAIL:
565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
566 "module does not support this operation. A reboot may "
567 "be required to enable this operation."));
568 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
570 case ZFS_ERR_IOC_ARG_UNAVAIL:
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
572 "module does not support an option for this operation. "
573 "A reboot may be required to enable this option."));
574 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
576 case ZFS_ERR_IOC_ARG_REQUIRED:
577 case ZFS_ERR_IOC_ARG_BADTYPE:
578 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
581 zfs_error_aux(hdl, strerror(error));
582 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
590 * Display an out of memory error message and abort the current program.
593 no_memory(libzfs_handle_t *hdl)
595 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
599 * A safe form of malloc() which will die if the allocation fails.
602 zfs_alloc(libzfs_handle_t *hdl, size_t size)
606 if ((data = calloc(1, size)) == NULL)
607 (void) no_memory(hdl);
613 * A safe form of asprintf() which will die if the allocation fails.
617 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
625 err = vasprintf(&ret, fmt, ap);
630 (void) no_memory(hdl);
636 * A safe form of realloc(), which also zeroes newly allocated space.
639 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
643 if ((ret = realloc(ptr, newsize)) == NULL) {
644 (void) no_memory(hdl);
648 bzero((char *)ret + oldsize, (newsize - oldsize));
653 * A safe form of strdup() which will die if the allocation fails.
656 zfs_strdup(libzfs_handle_t *hdl, const char *str)
660 if ((ret = strdup(str)) == NULL)
661 (void) no_memory(hdl);
667 * Convert a number to an appropriately human-readable output.
670 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
672 nicenum(num, buf, buflen);
676 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
678 hdl->libzfs_printerr = printerr;
686 if (modfind("zfs") < 0) {
687 /* Not present in kernel, try loading it. */
688 if (kldload("zfs") < 0 || modfind("zfs") < 0) {
699 libzfs_handle_t *hdl;
701 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
705 if (libzfs_load() < 0) {
710 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
715 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
716 (void) close(hdl->libzfs_fd);
721 hdl->libzfs_sharetab = fopen(ZFS_EXPORTS_PATH, "r");
723 if (libzfs_core_init() != 0) {
724 (void) close(hdl->libzfs_fd);
725 (void) fclose(hdl->libzfs_mnttab);
726 (void) fclose(hdl->libzfs_sharetab);
733 zpool_feature_init();
734 libzfs_mnttab_init(hdl);
736 if (getenv("ZFS_PROP_DEBUG") != NULL) {
737 hdl->libzfs_prop_debug = B_TRUE;
744 libzfs_fini(libzfs_handle_t *hdl)
746 (void) close(hdl->libzfs_fd);
747 if (hdl->libzfs_mnttab)
748 (void) fclose(hdl->libzfs_mnttab);
749 if (hdl->libzfs_sharetab)
750 (void) fclose(hdl->libzfs_sharetab);
751 zfs_uninit_libshare(hdl);
752 zpool_free_handles(hdl);
754 libzfs_fru_clear(hdl, B_TRUE);
756 namespace_clear(hdl);
757 libzfs_mnttab_fini(hdl);
763 zpool_get_handle(zpool_handle_t *zhp)
765 return (zhp->zpool_hdl);
769 zfs_get_handle(zfs_handle_t *zhp)
771 return (zhp->zfs_hdl);
775 zfs_get_pool_handle(const zfs_handle_t *zhp)
777 return (zhp->zpool_hdl);
781 * Given a name, determine whether or not it's a valid path
782 * (starts with '/' or "./"). If so, walk the mnttab trying
783 * to match the device number. If not, treat the path as an
784 * fs/vol/snap/bkmark name.
787 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
789 struct stat64 statbuf;
790 struct extmnttab entry;
793 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
795 * It's not a valid path, assume it's a name of type 'argtype'.
797 return (zfs_open(hdl, path, argtype));
800 if (stat64(path, &statbuf) != 0) {
801 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
806 rewind(hdl->libzfs_mnttab);
807 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
808 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
817 ret = statfs(path, &sfs);
819 statfs2mnttab(&sfs, &entry);
821 (void) fprintf(stderr, "%s: %s\n", path,
830 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
831 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
836 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
840 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
844 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
848 zc->zc_nvlist_dst_size = len;
850 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
851 if (zc->zc_nvlist_dst == 0)
858 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
859 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
860 * filled in by the kernel to indicate the actual required size.
863 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
865 free((void *)(uintptr_t)zc->zc_nvlist_dst);
867 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
868 if (zc->zc_nvlist_dst == 0)
875 * Called to free the src and dst nvlists stored in the command structure.
878 zcmd_free_nvlists(zfs_cmd_t *zc)
880 free((void *)(uintptr_t)zc->zc_nvlist_conf);
881 free((void *)(uintptr_t)zc->zc_nvlist_src);
882 free((void *)(uintptr_t)zc->zc_nvlist_dst);
883 zc->zc_nvlist_conf = NULL;
884 zc->zc_nvlist_src = NULL;
885 zc->zc_nvlist_dst = NULL;
889 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
895 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
897 if ((packed = zfs_alloc(hdl, len)) == NULL)
900 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
902 *outnv = (uint64_t)(uintptr_t)packed;
909 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
911 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
912 &zc->zc_nvlist_conf_size, nvl));
916 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
918 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
919 &zc->zc_nvlist_src_size, nvl));
923 * Unpacks an nvlist from the ZFS ioctl command structure.
926 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
928 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
929 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
930 return (no_memory(hdl));
936 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
938 return (ioctl(hdl->libzfs_fd, request, zc));
942 * ================================================================
943 * API shared by zfs and zpool property management
944 * ================================================================
948 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
950 zprop_list_t *pl = cbp->cb_proplist;
955 cbp->cb_first = B_FALSE;
956 if (cbp->cb_scripted)
960 * Start with the length of the column headers.
962 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
963 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
965 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
967 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
969 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
972 /* first property is always NAME */
973 assert(cbp->cb_proplist->pl_prop ==
974 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
977 * Go through and calculate the widths for each column. For the
978 * 'source' column, we kludge it up by taking the worst-case scenario of
979 * inheriting from the longest name. This is acceptable because in the
980 * majority of cases 'SOURCE' is the last column displayed, and we don't
981 * use the width anyway. Note that the 'VALUE' column can be oversized,
982 * if the name of the property is much longer than any values we find.
984 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
988 if (pl->pl_prop != ZPROP_INVAL) {
989 const char *propname = (type == ZFS_TYPE_POOL) ?
990 zpool_prop_to_name(pl->pl_prop) :
991 zfs_prop_to_name(pl->pl_prop);
993 len = strlen(propname);
994 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
995 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
997 len = strlen(pl->pl_user_prop);
998 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
999 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1003 * 'VALUE' column. The first property is always the 'name'
1004 * property that was tacked on either by /sbin/zfs's
1005 * zfs_do_get() or when calling zprop_expand_list(), so we
1006 * ignore its width. If the user specified the name property
1007 * to display, then it will be later in the list in any case.
1009 if (pl != cbp->cb_proplist &&
1010 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1011 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1013 /* 'RECEIVED' column. */
1014 if (pl != cbp->cb_proplist &&
1015 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1016 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1019 * 'NAME' and 'SOURCE' columns
1021 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1023 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1024 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1025 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1026 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1031 * Now go through and print the headers.
1033 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1034 switch (cbp->cb_columns[i]) {
1036 title = dgettext(TEXT_DOMAIN, "NAME");
1038 case GET_COL_PROPERTY:
1039 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1042 title = dgettext(TEXT_DOMAIN, "VALUE");
1045 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1047 case GET_COL_SOURCE:
1048 title = dgettext(TEXT_DOMAIN, "SOURCE");
1054 if (title != NULL) {
1055 if (i == (ZFS_GET_NCOLS - 1) ||
1056 cbp->cb_columns[i + 1] == GET_COL_NONE)
1057 (void) printf("%s", title);
1059 (void) printf("%-*s ",
1060 cbp->cb_colwidths[cbp->cb_columns[i]],
1064 (void) printf("\n");
1068 * Display a single line of output, according to the settings in the callback
1072 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1073 const char *propname, const char *value, zprop_source_t sourcetype,
1074 const char *source, const char *recvd_value)
1077 const char *str = NULL;
1081 * Ignore those source types that the user has chosen to ignore.
1083 if ((sourcetype & cbp->cb_sources) == 0)
1087 zprop_print_headers(cbp, cbp->cb_type);
1089 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1090 switch (cbp->cb_columns[i]) {
1095 case GET_COL_PROPERTY:
1103 case GET_COL_SOURCE:
1104 switch (sourcetype) {
1105 case ZPROP_SRC_NONE:
1109 case ZPROP_SRC_DEFAULT:
1113 case ZPROP_SRC_LOCAL:
1117 case ZPROP_SRC_TEMPORARY:
1121 case ZPROP_SRC_INHERITED:
1122 (void) snprintf(buf, sizeof (buf),
1123 "inherited from %s", source);
1126 case ZPROP_SRC_RECEIVED:
1132 assert(!"unhandled zprop_source_t");
1137 str = (recvd_value == NULL ? "-" : recvd_value);
1144 if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1145 (void) printf("%s", str);
1146 else if (cbp->cb_scripted)
1147 (void) printf("%s\t", str);
1149 (void) printf("%-*s ",
1150 cbp->cb_colwidths[cbp->cb_columns[i]],
1154 (void) printf("\n");
1158 * Given a numeric suffix, convert the value into a number of bits that the
1159 * resulting value must be shifted.
1162 str2shift(libzfs_handle_t *hdl, const char *buf)
1164 const char *ends = "BKMGTPEZ";
1169 for (i = 0; i < strlen(ends); i++) {
1170 if (toupper(buf[0]) == ends[i])
1173 if (i == strlen(ends)) {
1174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1175 "invalid numeric suffix '%s'"), buf);
1180 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
1181 * allow 'BB' - that's just weird.
1183 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1184 toupper(buf[0]) != 'B'))
1187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1188 "invalid numeric suffix '%s'"), buf);
1193 * Convert a string of the form '100G' into a real number. Used when setting
1194 * properties or creating a volume. 'buf' is used to place an extended error
1195 * message for the caller to use.
1198 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1205 /* Check to see if this looks like a number. */
1206 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1209 "bad numeric value '%s'"), value);
1213 /* Rely on strtoull() to process the numeric portion. */
1215 *num = strtoull(value, &end, 10);
1218 * Check for ERANGE, which indicates that the value is too large to fit
1219 * in a 64-bit value.
1221 if (errno == ERANGE) {
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "numeric value is too large"));
1229 * If we have a decimal value, then do the computation with floating
1230 * point arithmetic. Otherwise, use standard arithmetic.
1233 double fval = strtod(value, &end);
1235 if ((shift = str2shift(hdl, end)) == -1)
1238 fval *= pow(2, shift);
1240 if (fval > UINT64_MAX) {
1242 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1243 "numeric value is too large"));
1247 *num = (uint64_t)fval;
1249 if ((shift = str2shift(hdl, end)) == -1)
1252 /* Check for overflow */
1253 if (shift >= 64 || (*num << shift) >> shift != *num) {
1255 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1256 "numeric value is too large"));
1267 * Given a propname=value nvpair to set, parse any numeric properties
1268 * (index, boolean, etc) if they are specified as strings and add the
1269 * resulting nvpair to the returned nvlist.
1271 * At the DSL layer, all properties are either 64-bit numbers or strings.
1272 * We want the user to be able to ignore this fact and specify properties
1273 * as native values (numbers, for example) or as strings (to simplify
1274 * command line utilities). This also handles converting index types
1275 * (compression, checksum, etc) from strings to their on-disk index.
1278 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1279 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1282 data_type_t datatype = nvpair_type(elem);
1283 zprop_type_t proptype;
1284 const char *propname;
1286 boolean_t isnone = B_FALSE;
1287 boolean_t isauto = B_FALSE;
1289 if (type == ZFS_TYPE_POOL) {
1290 proptype = zpool_prop_get_type(prop);
1291 propname = zpool_prop_to_name(prop);
1293 proptype = zfs_prop_get_type(prop);
1294 propname = zfs_prop_to_name(prop);
1298 * Convert any properties to the internal DSL value types.
1304 case PROP_TYPE_STRING:
1305 if (datatype != DATA_TYPE_STRING) {
1306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1307 "'%s' must be a string"), nvpair_name(elem));
1310 (void) nvpair_value_string(elem, svalp);
1311 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1313 "'%s' is too long"), nvpair_name(elem));
1318 case PROP_TYPE_NUMBER:
1319 if (datatype == DATA_TYPE_STRING) {
1320 (void) nvpair_value_string(elem, &value);
1321 if (strcmp(value, "none") == 0) {
1323 } else if (strcmp(value, "auto") == 0) {
1325 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1328 } else if (datatype == DATA_TYPE_UINT64) {
1329 (void) nvpair_value_uint64(elem, ivalp);
1331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1332 "'%s' must be a number"), nvpair_name(elem));
1337 * Quota special: force 'none' and don't allow 0.
1339 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1340 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1342 "use 'none' to disable quota/refquota"));
1347 * Special handling for "*_limit=none". In this case it's not
1350 if ((type & ZFS_TYPE_DATASET) && isnone &&
1351 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1352 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1353 *ivalp = UINT64_MAX;
1357 * Special handling for setting 'refreservation' to 'auto'. Use
1358 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1359 * 'auto' is only allowed on volumes.
1363 case ZFS_PROP_REFRESERVATION:
1364 if ((type & ZFS_TYPE_VOLUME) == 0) {
1365 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1366 "'%s=auto' only allowed on "
1367 "volumes"), nvpair_name(elem));
1370 *ivalp = UINT64_MAX;
1373 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1374 "'auto' is invalid value for '%s'"),
1382 case PROP_TYPE_INDEX:
1383 if (datatype != DATA_TYPE_STRING) {
1384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1385 "'%s' must be a string"), nvpair_name(elem));
1389 (void) nvpair_value_string(elem, &value);
1391 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1393 "'%s' must be one of '%s'"), propname,
1394 zprop_values(prop, type));
1404 * Add the result to our return set of properties.
1406 if (*svalp != NULL) {
1407 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1408 (void) no_memory(hdl);
1412 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1413 (void) no_memory(hdl);
1420 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1425 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1429 zprop_list_t *entry;
1431 prop = zprop_name_to_prop(propname, type);
1433 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1437 * When no property table entry can be found, return failure if
1438 * this is a pool property or if this isn't a user-defined
1441 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1442 !zpool_prop_feature(propname) &&
1443 !zpool_prop_unsupported(propname)) ||
1444 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1445 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1447 "invalid property '%s'"), propname);
1448 return (zfs_error(hdl, EZFS_BADPROP,
1449 dgettext(TEXT_DOMAIN, "bad property list")));
1452 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1455 entry->pl_prop = prop;
1456 if (prop == ZPROP_INVAL) {
1457 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1462 entry->pl_width = strlen(propname);
1464 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1474 * Given a comma-separated list of properties, construct a property list
1475 * containing both user-defined and native properties. This function will
1476 * return a NULL list if 'all' is specified, which can later be expanded
1477 * by zprop_expand_list().
1480 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1486 * If 'all' is specified, return a NULL list.
1488 if (strcmp(props, "all") == 0)
1492 * If no props were specified, return an error.
1494 if (props[0] == '\0') {
1495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1496 "no properties specified"));
1497 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1498 "bad property list")));
1502 * It would be nice to use getsubopt() here, but the inclusion of column
1503 * aliases makes this more effort than it's worth.
1505 while (*props != '\0') {
1510 if ((p = strchr(props, ',')) == NULL) {
1511 len = strlen(props);
1518 * Check for empty options.
1521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1522 "empty property name"));
1523 return (zfs_error(hdl, EZFS_BADPROP,
1524 dgettext(TEXT_DOMAIN, "bad property list")));
1528 * Check all regular property names.
1533 if (strcmp(props, "space") == 0) {
1534 static char *spaceprops[] = {
1535 "name", "avail", "used", "usedbysnapshots",
1536 "usedbydataset", "usedbyrefreservation",
1537 "usedbychildren", NULL
1541 for (i = 0; spaceprops[i]; i++) {
1542 if (addlist(hdl, spaceprops[i], listp, type))
1544 listp = &(*listp)->pl_next;
1547 if (addlist(hdl, props, listp, type))
1549 listp = &(*listp)->pl_next;
1561 zprop_free_list(zprop_list_t *pl)
1565 while (pl != NULL) {
1567 free(pl->pl_user_prop);
1573 typedef struct expand_data {
1574 zprop_list_t **last;
1575 libzfs_handle_t *hdl;
1580 zprop_expand_list_cb(int prop, void *cb)
1582 zprop_list_t *entry;
1583 expand_data_t *edp = cb;
1585 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1586 return (ZPROP_INVAL);
1588 entry->pl_prop = prop;
1589 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1590 entry->pl_all = B_TRUE;
1592 *(edp->last) = entry;
1593 edp->last = &entry->pl_next;
1595 return (ZPROP_CONT);
1599 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1601 zprop_list_t *entry;
1602 zprop_list_t **last;
1607 * If this is the very first time we've been called for an 'all'
1608 * specification, expand the list to include all native
1617 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1618 B_FALSE, type) == ZPROP_INVAL)
1622 * Add 'name' to the beginning of the list, which is handled
1625 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1628 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1630 entry->pl_width = zprop_width(entry->pl_prop,
1631 &entry->pl_fixed, type);
1632 entry->pl_all = B_TRUE;
1633 entry->pl_next = *plp;
1640 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1643 return (zprop_iter_common(func, cb, show_all, ordered, type));
1647 get_system_hostid(void)
1652 * Allow the hostid to be subverted for testing.
1654 env = getenv("ZFS_HOSTID");
1656 ulong_t hostid = strtoull(env, NULL, 16);
1657 return (hostid & 0xFFFFFFFF);
1660 return (gethostid());