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 2020 Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2020 The FreeBSD Foundation
30 * Portions of this software were developed by Allan Jude
31 * under sponsorship from the FreeBSD Foundation.
35 * Internal utility routines for the ZFS library.
48 #include <sys/mnttab.h>
49 #include <sys/mntent.h>
50 #include <sys/types.h>
54 #include <libzfs_core.h>
56 #include "libzfs_impl.h"
58 #include "zfeature_common.h"
59 #include <zfs_fletcher.h>
63 * We only care about the scheme in order to match the scheme
64 * with the handler. Each handler should validate the full URI
67 #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):"
70 libzfs_errno(libzfs_handle_t *hdl)
72 return (hdl->libzfs_error);
76 libzfs_error_action(libzfs_handle_t *hdl)
78 return (hdl->libzfs_action);
82 libzfs_error_description(libzfs_handle_t *hdl)
84 if (hdl->libzfs_desc[0] != '\0')
85 return (hdl->libzfs_desc);
87 switch (hdl->libzfs_error) {
89 return (dgettext(TEXT_DOMAIN, "out of memory"));
91 return (dgettext(TEXT_DOMAIN, "invalid property value"));
92 case EZFS_PROPREADONLY:
93 return (dgettext(TEXT_DOMAIN, "read-only property"));
95 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
96 "datasets of this type"));
97 case EZFS_PROPNONINHERIT:
98 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
100 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
102 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
103 "datasets of this type"));
105 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
107 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
109 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
111 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
112 case EZFS_DSREADONLY:
113 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
115 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
117 case EZFS_INVALIDNAME:
118 return (dgettext(TEXT_DOMAIN, "invalid name"));
119 case EZFS_BADRESTORE:
120 return (dgettext(TEXT_DOMAIN, "unable to restore to "
123 return (dgettext(TEXT_DOMAIN, "backup failed"));
125 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
127 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
129 return (dgettext(TEXT_DOMAIN, "invalid device"));
130 case EZFS_NOREPLICAS:
131 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
132 case EZFS_RESILVERING:
133 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
134 case EZFS_BADVERSION:
135 return (dgettext(TEXT_DOMAIN, "unsupported version or "
137 case EZFS_POOLUNAVAIL:
138 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
139 case EZFS_DEVOVERFLOW:
140 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
142 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
143 case EZFS_CROSSTARGET:
144 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
147 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
148 case EZFS_MOUNTFAILED:
149 return (dgettext(TEXT_DOMAIN, "mount failed"));
150 case EZFS_UMOUNTFAILED:
151 return (dgettext(TEXT_DOMAIN, "umount failed"));
152 case EZFS_UNSHARENFSFAILED:
153 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
154 case EZFS_SHARENFSFAILED:
155 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
156 case EZFS_UNSHARESMBFAILED:
157 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
158 case EZFS_SHARESMBFAILED:
159 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
161 return (dgettext(TEXT_DOMAIN, "permission denied"));
163 return (dgettext(TEXT_DOMAIN, "out of space"));
165 return (dgettext(TEXT_DOMAIN, "bad address"));
167 return (dgettext(TEXT_DOMAIN, "I/O error"));
169 return (dgettext(TEXT_DOMAIN, "signal received"));
171 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
173 case EZFS_INVALCONFIG:
174 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
176 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
178 return (dgettext(TEXT_DOMAIN, "no history available"));
180 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
182 case EZFS_POOL_NOTSUP:
183 return (dgettext(TEXT_DOMAIN, "operation not supported "
184 "on this type of pool"));
185 case EZFS_POOL_INVALARG:
186 return (dgettext(TEXT_DOMAIN, "invalid argument for "
187 "this pool operation"));
188 case EZFS_NAMETOOLONG:
189 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
190 case EZFS_OPENFAILED:
191 return (dgettext(TEXT_DOMAIN, "open failed"));
193 return (dgettext(TEXT_DOMAIN,
194 "disk capacity information could not be retrieved"));
195 case EZFS_LABELFAILED:
196 return (dgettext(TEXT_DOMAIN, "write of label failed"));
198 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
200 return (dgettext(TEXT_DOMAIN, "invalid permission"));
201 case EZFS_BADPERMSET:
202 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
203 case EZFS_NODELEGATION:
204 return (dgettext(TEXT_DOMAIN, "delegated administration is "
205 "disabled on pool"));
207 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
209 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
210 case EZFS_VDEVNOTSUP:
211 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
214 return (dgettext(TEXT_DOMAIN, "operation not supported "
216 case EZFS_IOC_NOTSUPPORTED:
217 return (dgettext(TEXT_DOMAIN, "operation not supported by "
218 "zfs kernel module"));
219 case EZFS_ACTIVE_SPARE:
220 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
222 case EZFS_UNPLAYED_LOGS:
223 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
225 case EZFS_REFTAG_RELE:
226 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
227 case EZFS_REFTAG_HOLD:
228 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
230 case EZFS_TAGTOOLONG:
231 return (dgettext(TEXT_DOMAIN, "tag too long"));
232 case EZFS_PIPEFAILED:
233 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
234 case EZFS_THREADCREATEFAILED:
235 return (dgettext(TEXT_DOMAIN, "thread create failed"));
236 case EZFS_POSTSPLIT_ONLINE:
237 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
239 case EZFS_SCRUB_PAUSED:
240 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
241 "use 'zpool scrub' to resume"));
243 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
244 "use 'zpool scrub -s' to cancel current scrub"));
246 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
248 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
250 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
251 case EZFS_POOLREADONLY:
252 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
253 case EZFS_NO_PENDING:
254 return (dgettext(TEXT_DOMAIN, "operation is not "
256 case EZFS_CHECKPOINT_EXISTS:
257 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
258 case EZFS_DISCARDING_CHECKPOINT:
259 return (dgettext(TEXT_DOMAIN, "currently discarding "
261 case EZFS_NO_CHECKPOINT:
262 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
263 case EZFS_DEVRM_IN_PROGRESS:
264 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
265 case EZFS_VDEV_TOO_BIG:
266 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
267 case EZFS_ACTIVE_POOL:
268 return (dgettext(TEXT_DOMAIN, "pool is imported on a "
270 case EZFS_CRYPTOFAILED:
271 return (dgettext(TEXT_DOMAIN, "encryption failure"));
273 return (dgettext(TEXT_DOMAIN, "argument list too long"));
274 case EZFS_INITIALIZING:
275 return (dgettext(TEXT_DOMAIN, "currently initializing"));
276 case EZFS_NO_INITIALIZE:
277 return (dgettext(TEXT_DOMAIN, "there is no active "
279 case EZFS_WRONG_PARENT:
280 return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
282 return (dgettext(TEXT_DOMAIN, "currently trimming"));
284 return (dgettext(TEXT_DOMAIN, "there is no active trim"));
285 case EZFS_TRIM_NOTSUP:
286 return (dgettext(TEXT_DOMAIN, "trim operations are not "
287 "supported by this device"));
288 case EZFS_NO_RESILVER_DEFER:
289 return (dgettext(TEXT_DOMAIN, "this action requires the "
290 "resilver_defer feature"));
291 case EZFS_EXPORT_IN_PROGRESS:
292 return (dgettext(TEXT_DOMAIN, "pool export in progress"));
293 case EZFS_REBUILDING:
294 return (dgettext(TEXT_DOMAIN, "currently sequentially "
297 return (dgettext(TEXT_DOMAIN, "unknown error"));
299 assert(hdl->libzfs_error == 0);
300 return (dgettext(TEXT_DOMAIN, "no error"));
306 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
312 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
314 hdl->libzfs_desc_active = 1;
320 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
322 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
324 hdl->libzfs_error = error;
326 if (hdl->libzfs_desc_active)
327 hdl->libzfs_desc_active = 0;
329 hdl->libzfs_desc[0] = '\0';
331 if (hdl->libzfs_printerr) {
332 if (error == EZFS_UNKNOWN) {
333 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
334 "error: %s: %s\n"), hdl->libzfs_action,
335 libzfs_error_description(hdl));
339 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
340 libzfs_error_description(hdl));
341 if (error == EZFS_NOMEM)
347 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
349 return (zfs_error_fmt(hdl, error, "%s", msg));
354 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
360 zfs_verror(hdl, error, fmt, ap);
368 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
374 zfs_verror(hdl, EZFS_PERM, fmt, ap);
378 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
382 zfs_verror(hdl, EZFS_IO, fmt, ap);
386 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
390 zfs_verror(hdl, EZFS_INTR, fmt, ap);
398 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
400 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
405 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
411 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
420 zfs_verror(hdl, EZFS_IO, fmt, ap);
424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
425 "dataset does not exist"));
426 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
431 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
436 "dataset already exists"));
437 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
443 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
446 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
449 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
452 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
456 "pool I/O is currently suspended"));
457 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
460 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
462 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE:
463 case ZFS_ERR_IOC_CMD_UNAVAIL:
464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
465 "module does not support this operation. A reboot may "
466 "be required to enable this operation."));
467 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
469 case ZFS_ERR_IOC_ARG_UNAVAIL:
470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
471 "module does not support an option for this operation. "
472 "A reboot may be required to enable this option."));
473 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
475 case ZFS_ERR_IOC_ARG_REQUIRED:
476 case ZFS_ERR_IOC_ARG_BADTYPE:
477 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
479 case ZFS_ERR_WRONG_PARENT:
480 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
482 case ZFS_ERR_BADPROP:
483 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
486 zfs_error_aux(hdl, strerror(error));
487 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
496 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
503 * For quotas and reservations, ENOSPC indicates
504 * something different; setting a quota or reservation
505 * doesn't use any disk space.
509 case ZFS_PROP_REFQUOTA:
510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
511 "size is less than current used or "
513 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
516 case ZFS_PROP_RESERVATION:
517 case ZFS_PROP_REFRESERVATION:
518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
519 "size is greater than available space"));
520 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
524 (void) zfs_standard_error(hdl, err, errbuf);
530 (void) zfs_standard_error(hdl, EBUSY, errbuf);
534 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
539 "property value too long"));
540 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
545 "pool and or dataset must be upgraded to set this "
546 "property or value"));
547 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
551 if (prop == ZFS_PROP_COMPRESSION ||
552 prop == ZFS_PROP_DNODESIZE ||
553 prop == ZFS_PROP_RECORDSIZE) {
554 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "property setting is not allowed on "
556 "bootable datasets"));
557 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
558 } else if (prop == ZFS_PROP_CHECKSUM ||
559 prop == ZFS_PROP_DEDUP) {
560 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
561 "property setting is not allowed on "
563 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
565 (void) zfs_standard_error(hdl, err, errbuf);
570 if (prop == ZPROP_INVAL) {
571 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
573 (void) zfs_standard_error(hdl, err, errbuf);
577 case ZFS_ERR_BADPROP:
578 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
582 if (prop == ZFS_PROP_KEYLOCATION) {
583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
584 "keylocation may only be set on encryption roots"));
585 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
587 (void) zfs_standard_error(hdl, err, errbuf);
593 * This platform can't address a volume this big.
596 if (prop == ZFS_PROP_VOLSIZE) {
597 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
603 (void) zfs_standard_error(hdl, err, errbuf);
608 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
610 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
615 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
621 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
628 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
633 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
634 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
639 "pool already exists"));
640 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
645 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
648 /* There is no pending operation to cancel */
650 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
655 "one or more devices is currently unavailable"));
656 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
660 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
664 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
668 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
673 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
678 "pool I/O is currently suspended"));
679 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
683 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
687 "block size out of range or does not match"));
688 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
691 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
693 case ZFS_ERR_CHECKPOINT_EXISTS:
694 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
696 case ZFS_ERR_DISCARDING_CHECKPOINT:
697 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
699 case ZFS_ERR_NO_CHECKPOINT:
700 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
702 case ZFS_ERR_DEVRM_IN_PROGRESS:
703 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
705 case ZFS_ERR_VDEV_TOO_BIG:
706 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
708 case ZFS_ERR_EXPORT_IN_PROGRESS:
709 zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap);
711 case ZFS_ERR_RESILVER_IN_PROGRESS:
712 zfs_verror(hdl, EZFS_RESILVERING, fmt, ap);
714 case ZFS_ERR_REBUILD_IN_PROGRESS:
715 zfs_verror(hdl, EZFS_REBUILDING, fmt, ap);
717 case ZFS_ERR_BADPROP:
718 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
720 case ZFS_ERR_IOC_CMD_UNAVAIL:
721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
722 "module does not support this operation. A reboot may "
723 "be required to enable this operation."));
724 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
726 case ZFS_ERR_IOC_ARG_UNAVAIL:
727 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
728 "module does not support an option for this operation. "
729 "A reboot may be required to enable this option."));
730 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
732 case ZFS_ERR_IOC_ARG_REQUIRED:
733 case ZFS_ERR_IOC_ARG_BADTYPE:
734 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
737 zfs_error_aux(hdl, strerror(error));
738 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
746 * Display an out of memory error message and abort the current program.
749 no_memory(libzfs_handle_t *hdl)
751 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
755 * A safe form of malloc() which will die if the allocation fails.
758 zfs_alloc(libzfs_handle_t *hdl, size_t size)
762 if ((data = calloc(1, size)) == NULL)
763 (void) no_memory(hdl);
769 * A safe form of asprintf() which will die if the allocation fails.
773 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
781 err = vasprintf(&ret, fmt, ap);
786 (void) no_memory(hdl);
792 * A safe form of realloc(), which also zeroes newly allocated space.
795 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
799 if ((ret = realloc(ptr, newsize)) == NULL) {
800 (void) no_memory(hdl);
804 bzero((char *)ret + oldsize, (newsize - oldsize));
809 * A safe form of strdup() which will die if the allocation fails.
812 zfs_strdup(libzfs_handle_t *hdl, const char *str)
816 if ((ret = strdup(str)) == NULL)
817 (void) no_memory(hdl);
823 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
825 hdl->libzfs_printerr = printerr;
829 * Read lines from an open file descriptor and store them in an array of
830 * strings until EOF. lines[] will be allocated and populated with all the
831 * lines read. All newlines are replaced with NULL terminators for
832 * convenience. lines[] must be freed after use with libzfs_free_str_array().
834 * Returns the number of lines read.
837 libzfs_read_stdout_from_fd(int fd, char **lines[])
844 char **tmp_lines = NULL, **tmp;
848 fp = fdopen(fd, "r");
852 rc = getline(&line, &len, fp);
856 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
858 /* Return the lines we were able to process */
863 /* Terminate newlines */
864 if ((nl = strchr(line, '\n')) != NULL)
866 tmp_lines[lines_cnt] = line;
876 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
877 char **lines[], int *lines_cnt)
880 int error, devnull_fd;
884 * Setup a pipe between our child and parent process if we're
887 if ((lines != NULL) && pipe(link) == -1)
893 devnull_fd = open("/dev/null", O_WRONLY);
898 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
899 (void) dup2(devnull_fd, STDOUT_FILENO);
900 else if (lines != NULL) {
901 /* Save the output to lines[] */
902 dup2(link[1], STDOUT_FILENO);
907 if (!(flags & STDERR_VERBOSE))
908 (void) dup2(devnull_fd, STDERR_FILENO);
912 if (flags & NO_DEFAULT_PATH) {
916 execve(path, argv, env);
921 execvpe(path, argv, env);
925 } else if (pid > 0) {
929 while ((error = waitpid(pid, &status, 0)) == -1 &&
931 if (error < 0 || !WIFEXITED(status))
936 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
938 return (WEXITSTATUS(status));
945 libzfs_run_process(const char *path, char *argv[], int flags)
947 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
951 * Run a command and store its stdout lines in an array of strings (lines[]).
952 * lines[] is allocated and populated for you, and the number of lines is set in
953 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
954 * All newlines (\n) in lines[] are terminated for convenience.
957 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
958 char **lines[], int *lines_cnt)
960 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
964 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
965 * means that *path needs to be the full path to the executable.
968 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
969 char *env[], char **lines[], int *lines_cnt)
971 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
976 * Free an array of strings. Free both the strings contained in the array and
980 libzfs_free_str_array(char **strs, int count)
989 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
992 * Returns 0 otherwise.
995 libzfs_envvar_is_set(char *envvar)
997 char *env = getenv(envvar);
998 if (env && (strtoul(env, NULL, 0) > 0 ||
999 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
1000 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
1009 libzfs_handle_t *hdl;
1013 if ((error = libzfs_load_module()) != 0) {
1018 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
1022 if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) {
1027 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL)) < 0) {
1032 #ifdef HAVE_SETMNTENT
1033 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
1035 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
1037 (void) close(hdl->libzfs_fd);
1042 if (libzfs_core_init() != 0) {
1043 (void) close(hdl->libzfs_fd);
1044 (void) fclose(hdl->libzfs_mnttab);
1051 zpool_feature_init();
1052 libzfs_mnttab_init(hdl);
1055 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1056 hdl->libzfs_prop_debug = B_TRUE;
1058 if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) {
1059 if ((error = zfs_nicestrtonum(hdl, env,
1060 &hdl->libzfs_max_nvlist))) {
1062 (void) close(hdl->libzfs_fd);
1063 (void) fclose(hdl->libzfs_mnttab);
1068 hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4);
1072 * For testing, remove some settable properties and features
1074 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1075 zprop_desc_t *proptbl;
1077 proptbl = zpool_prop_get_table();
1078 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1080 proptbl = zfs_prop_get_table();
1081 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1083 zfeature_info_t *ftbl = spa_feature_table;
1084 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1091 libzfs_fini(libzfs_handle_t *hdl)
1093 (void) close(hdl->libzfs_fd);
1094 if (hdl->libzfs_mnttab)
1095 #ifdef HAVE_SETMNTENT
1096 (void) endmntent(hdl->libzfs_mnttab);
1098 (void) fclose(hdl->libzfs_mnttab);
1100 zpool_free_handles(hdl);
1101 namespace_clear(hdl);
1102 libzfs_mnttab_fini(hdl);
1104 regfree(&hdl->libzfs_urire);
1110 zpool_get_handle(zpool_handle_t *zhp)
1112 return (zhp->zpool_hdl);
1116 zfs_get_handle(zfs_handle_t *zhp)
1118 return (zhp->zfs_hdl);
1122 zfs_get_pool_handle(const zfs_handle_t *zhp)
1124 return (zhp->zpool_hdl);
1128 * Given a name, determine whether or not it's a valid path
1129 * (starts with '/' or "./"). If so, walk the mnttab trying
1130 * to match the device number. If not, treat the path as an
1131 * fs/vol/snap/bkmark name.
1134 zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype)
1136 struct stat64 statbuf;
1137 struct extmnttab entry;
1139 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1141 * It's not a valid path, assume it's a name of type 'argtype'.
1143 return (zfs_open(hdl, path, argtype));
1146 /* Reopen MNTTAB to prevent reading stale data from open file */
1147 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1150 if (getextmntent(path, &entry, &statbuf) != 0)
1153 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1154 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1159 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1163 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1167 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1171 zc->zc_nvlist_dst_size = len;
1173 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1174 if (zc->zc_nvlist_dst == 0)
1181 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1182 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1183 * filled in by the kernel to indicate the actual required size.
1186 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1188 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1190 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1191 if (zc->zc_nvlist_dst == 0)
1198 * Called to free the src and dst nvlists stored in the command structure.
1201 zcmd_free_nvlists(zfs_cmd_t *zc)
1203 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1204 free((void *)(uintptr_t)zc->zc_nvlist_src);
1205 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1206 zc->zc_nvlist_conf = 0;
1207 zc->zc_nvlist_src = 0;
1208 zc->zc_nvlist_dst = 0;
1212 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1218 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1220 if ((packed = zfs_alloc(hdl, len)) == NULL)
1223 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1225 *outnv = (uint64_t)(uintptr_t)packed;
1232 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1234 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1235 &zc->zc_nvlist_conf_size, nvl));
1239 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1241 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1242 &zc->zc_nvlist_src_size, nvl));
1246 * Unpacks an nvlist from the ZFS ioctl command structure.
1249 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1251 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1252 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1253 return (no_memory(hdl));
1259 * ================================================================
1260 * API shared by zfs and zpool property management
1261 * ================================================================
1265 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1267 zprop_list_t *pl = cbp->cb_proplist;
1272 cbp->cb_first = B_FALSE;
1273 if (cbp->cb_scripted)
1277 * Start with the length of the column headers.
1279 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1280 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1282 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1284 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1286 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1289 /* first property is always NAME */
1290 assert(cbp->cb_proplist->pl_prop ==
1291 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1294 * Go through and calculate the widths for each column. For the
1295 * 'source' column, we kludge it up by taking the worst-case scenario of
1296 * inheriting from the longest name. This is acceptable because in the
1297 * majority of cases 'SOURCE' is the last column displayed, and we don't
1298 * use the width anyway. Note that the 'VALUE' column can be oversized,
1299 * if the name of the property is much longer than any values we find.
1301 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1305 if (pl->pl_prop != ZPROP_INVAL) {
1306 const char *propname = (type == ZFS_TYPE_POOL) ?
1307 zpool_prop_to_name(pl->pl_prop) :
1308 zfs_prop_to_name(pl->pl_prop);
1310 len = strlen(propname);
1311 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1312 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1314 len = strlen(pl->pl_user_prop);
1315 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1316 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1320 * 'VALUE' column. The first property is always the 'name'
1321 * property that was tacked on either by /sbin/zfs's
1322 * zfs_do_get() or when calling zprop_expand_list(), so we
1323 * ignore its width. If the user specified the name property
1324 * to display, then it will be later in the list in any case.
1326 if (pl != cbp->cb_proplist &&
1327 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1328 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1330 /* 'RECEIVED' column. */
1331 if (pl != cbp->cb_proplist &&
1332 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1333 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1336 * 'NAME' and 'SOURCE' columns
1338 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1340 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1341 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1342 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1343 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1348 * Now go through and print the headers.
1350 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1351 switch (cbp->cb_columns[i]) {
1353 title = dgettext(TEXT_DOMAIN, "NAME");
1355 case GET_COL_PROPERTY:
1356 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1359 title = dgettext(TEXT_DOMAIN, "VALUE");
1362 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1364 case GET_COL_SOURCE:
1365 title = dgettext(TEXT_DOMAIN, "SOURCE");
1371 if (title != NULL) {
1372 if (i == (ZFS_GET_NCOLS - 1) ||
1373 cbp->cb_columns[i + 1] == GET_COL_NONE)
1374 (void) printf("%s", title);
1376 (void) printf("%-*s ",
1377 cbp->cb_colwidths[cbp->cb_columns[i]],
1381 (void) printf("\n");
1385 * Display a single line of output, according to the settings in the callback
1389 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1390 const char *propname, const char *value, zprop_source_t sourcetype,
1391 const char *source, const char *recvd_value)
1394 const char *str = NULL;
1398 * Ignore those source types that the user has chosen to ignore.
1400 if ((sourcetype & cbp->cb_sources) == 0)
1404 zprop_print_headers(cbp, cbp->cb_type);
1406 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1407 switch (cbp->cb_columns[i]) {
1412 case GET_COL_PROPERTY:
1420 case GET_COL_SOURCE:
1421 switch (sourcetype) {
1422 case ZPROP_SRC_NONE:
1426 case ZPROP_SRC_DEFAULT:
1430 case ZPROP_SRC_LOCAL:
1434 case ZPROP_SRC_TEMPORARY:
1438 case ZPROP_SRC_INHERITED:
1439 (void) snprintf(buf, sizeof (buf),
1440 "inherited from %s", source);
1443 case ZPROP_SRC_RECEIVED:
1449 assert(!"unhandled zprop_source_t");
1454 str = (recvd_value == NULL ? "-" : recvd_value);
1461 if (i == (ZFS_GET_NCOLS - 1) ||
1462 cbp->cb_columns[i + 1] == GET_COL_NONE)
1463 (void) printf("%s", str);
1464 else if (cbp->cb_scripted)
1465 (void) printf("%s\t", str);
1467 (void) printf("%-*s ",
1468 cbp->cb_colwidths[cbp->cb_columns[i]],
1472 (void) printf("\n");
1476 * Given a numeric suffix, convert the value into a number of bits that the
1477 * resulting value must be shifted.
1480 str2shift(libzfs_handle_t *hdl, const char *buf)
1482 const char *ends = "BKMGTPEZ";
1487 for (i = 0; i < strlen(ends); i++) {
1488 if (toupper(buf[0]) == ends[i])
1491 if (i == strlen(ends)) {
1493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1494 "invalid numeric suffix '%s'"), buf);
1499 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1500 * However, 'BB' and 'BiB' are disallowed.
1502 if (buf[1] == '\0' ||
1503 (toupper(buf[0]) != 'B' &&
1504 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1505 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511 "invalid numeric suffix '%s'"), buf);
1516 * Convert a string of the form '100G' into a real number. Used when setting
1517 * properties or creating a volume. 'buf' is used to place an extended error
1518 * message for the caller to use.
1521 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1528 /* Check to see if this looks like a number. */
1529 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532 "bad numeric value '%s'"), value);
1536 /* Rely on strtoull() to process the numeric portion. */
1538 *num = strtoull(value, &end, 10);
1541 * Check for ERANGE, which indicates that the value is too large to fit
1542 * in a 64-bit value.
1544 if (errno == ERANGE) {
1546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1547 "numeric value is too large"));
1552 * If we have a decimal value, then do the computation with floating
1553 * point arithmetic. Otherwise, use standard arithmetic.
1556 double fval = strtod(value, &end);
1558 if ((shift = str2shift(hdl, end)) == -1)
1561 fval *= pow(2, shift);
1564 * UINT64_MAX is not exactly representable as a double.
1565 * The closest representation is UINT64_MAX + 1, so we
1566 * use a >= comparison instead of > for the bounds check.
1568 if (fval >= (double)UINT64_MAX) {
1570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1571 "numeric value is too large"));
1575 *num = (uint64_t)fval;
1577 if ((shift = str2shift(hdl, end)) == -1)
1580 /* Check for overflow */
1581 if (shift >= 64 || (*num << shift) >> shift != *num) {
1583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1584 "numeric value is too large"));
1595 * Given a propname=value nvpair to set, parse any numeric properties
1596 * (index, boolean, etc) if they are specified as strings and add the
1597 * resulting nvpair to the returned nvlist.
1599 * At the DSL layer, all properties are either 64-bit numbers or strings.
1600 * We want the user to be able to ignore this fact and specify properties
1601 * as native values (numbers, for example) or as strings (to simplify
1602 * command line utilities). This also handles converting index types
1603 * (compression, checksum, etc) from strings to their on-disk index.
1606 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1607 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1610 data_type_t datatype = nvpair_type(elem);
1611 zprop_type_t proptype;
1612 const char *propname;
1614 boolean_t isnone = B_FALSE;
1615 boolean_t isauto = B_FALSE;
1618 if (type == ZFS_TYPE_POOL) {
1619 proptype = zpool_prop_get_type(prop);
1620 propname = zpool_prop_to_name(prop);
1622 proptype = zfs_prop_get_type(prop);
1623 propname = zfs_prop_to_name(prop);
1627 * Convert any properties to the internal DSL value types.
1633 case PROP_TYPE_STRING:
1634 if (datatype != DATA_TYPE_STRING) {
1635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1636 "'%s' must be a string"), nvpair_name(elem));
1639 err = nvpair_value_string(elem, svalp);
1641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1642 "'%s' is invalid"), nvpair_name(elem));
1645 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1646 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1647 "'%s' is too long"), nvpair_name(elem));
1652 case PROP_TYPE_NUMBER:
1653 if (datatype == DATA_TYPE_STRING) {
1654 (void) nvpair_value_string(elem, &value);
1655 if (strcmp(value, "none") == 0) {
1657 } else if (strcmp(value, "auto") == 0) {
1659 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1662 } else if (datatype == DATA_TYPE_UINT64) {
1663 (void) nvpair_value_uint64(elem, ivalp);
1665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1666 "'%s' must be a number"), nvpair_name(elem));
1671 * Quota special: force 'none' and don't allow 0.
1673 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1674 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1676 "use 'none' to disable quota/refquota"));
1681 * Special handling for "*_limit=none". In this case it's not
1684 if ((type & ZFS_TYPE_DATASET) && isnone &&
1685 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1686 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1687 *ivalp = UINT64_MAX;
1691 * Special handling for setting 'refreservation' to 'auto'. Use
1692 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1693 * 'auto' is only allowed on volumes.
1697 case ZFS_PROP_REFRESERVATION:
1698 if ((type & ZFS_TYPE_VOLUME) == 0) {
1699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1700 "'%s=auto' only allowed on "
1701 "volumes"), nvpair_name(elem));
1704 *ivalp = UINT64_MAX;
1707 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1708 "'auto' is invalid value for '%s'"),
1716 case PROP_TYPE_INDEX:
1717 if (datatype != DATA_TYPE_STRING) {
1718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1719 "'%s' must be a string"), nvpair_name(elem));
1723 (void) nvpair_value_string(elem, &value);
1725 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1727 "'%s' must be one of '%s'"), propname,
1728 zprop_values(prop, type));
1738 * Add the result to our return set of properties.
1740 if (*svalp != NULL) {
1741 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1742 (void) no_memory(hdl);
1746 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1747 (void) no_memory(hdl);
1754 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1759 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1763 zprop_list_t *entry;
1765 prop = zprop_name_to_prop(propname, type);
1767 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1771 * When no property table entry can be found, return failure if
1772 * this is a pool property or if this isn't a user-defined
1775 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1776 !zpool_prop_feature(propname) &&
1777 !zpool_prop_unsupported(propname)) ||
1778 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1779 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1781 "invalid property '%s'"), propname);
1782 return (zfs_error(hdl, EZFS_BADPROP,
1783 dgettext(TEXT_DOMAIN, "bad property list")));
1786 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1789 entry->pl_prop = prop;
1790 if (prop == ZPROP_INVAL) {
1791 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1796 entry->pl_width = strlen(propname);
1798 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1808 * Given a comma-separated list of properties, construct a property list
1809 * containing both user-defined and native properties. This function will
1810 * return a NULL list if 'all' is specified, which can later be expanded
1811 * by zprop_expand_list().
1814 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1820 * If 'all' is specified, return a NULL list.
1822 if (strcmp(props, "all") == 0)
1826 * If no props were specified, return an error.
1828 if (props[0] == '\0') {
1829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1830 "no properties specified"));
1831 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1832 "bad property list")));
1836 * It would be nice to use getsubopt() here, but the inclusion of column
1837 * aliases makes this more effort than it's worth.
1839 while (*props != '\0') {
1844 if ((p = strchr(props, ',')) == NULL) {
1845 len = strlen(props);
1852 * Check for empty options.
1855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1856 "empty property name"));
1857 return (zfs_error(hdl, EZFS_BADPROP,
1858 dgettext(TEXT_DOMAIN, "bad property list")));
1862 * Check all regular property names.
1867 if (strcmp(props, "space") == 0) {
1868 static char *spaceprops[] = {
1869 "name", "avail", "used", "usedbysnapshots",
1870 "usedbydataset", "usedbyrefreservation",
1871 "usedbychildren", NULL
1875 for (i = 0; spaceprops[i]; i++) {
1876 if (addlist(hdl, spaceprops[i], listp, type))
1878 listp = &(*listp)->pl_next;
1881 if (addlist(hdl, props, listp, type))
1883 listp = &(*listp)->pl_next;
1895 zprop_free_list(zprop_list_t *pl)
1899 while (pl != NULL) {
1901 free(pl->pl_user_prop);
1907 typedef struct expand_data {
1908 zprop_list_t **last;
1909 libzfs_handle_t *hdl;
1914 zprop_expand_list_cb(int prop, void *cb)
1916 zprop_list_t *entry;
1917 expand_data_t *edp = cb;
1919 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1920 return (ZPROP_INVAL);
1922 entry->pl_prop = prop;
1923 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1924 entry->pl_all = B_TRUE;
1926 *(edp->last) = entry;
1927 edp->last = &entry->pl_next;
1929 return (ZPROP_CONT);
1933 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1935 zprop_list_t *entry;
1936 zprop_list_t **last;
1941 * If this is the very first time we've been called for an 'all'
1942 * specification, expand the list to include all native
1951 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1952 B_FALSE, type) == ZPROP_INVAL)
1956 * Add 'name' to the beginning of the list, which is handled
1959 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1962 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1964 entry->pl_width = zprop_width(entry->pl_prop,
1965 &entry->pl_fixed, type);
1966 entry->pl_all = B_TRUE;
1967 entry->pl_next = *plp;
1974 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1977 return (zprop_iter_common(func, cb, show_all, ordered, type));
1981 * Fill given version buffer with zfs userland version
1984 zfs_version_userland(char *version, int len)
1986 (void) strlcpy(version, ZFS_META_ALIAS, len);
1990 * Prints both zfs userland and kernel versions
1991 * Returns 0 on success, and -1 on error (with errno set)
1994 zfs_version_print(void)
1996 char zver_userland[128];
1997 char zver_kernel[128];
1999 zfs_version_userland(zver_userland, sizeof (zver_userland));
2001 (void) printf("%s\n", zver_userland);
2003 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
2004 fprintf(stderr, "zfs_version_kernel() failed: %s\n",
2009 (void) printf("zfs-kmod-%s\n", zver_kernel);
2015 * Return 1 if the user requested ANSI color output, and our terminal supports
2016 * it. Return 0 for no color.
2021 static int use_color = -1;
2027 * For each zpool invocation, we do a single check to see if we should
2028 * be using color or not, and cache that value for the lifetime of the
2029 * the zpool command. That makes it cheap to call use_color() when
2030 * we're printing with color. We assume that the settings are not going
2031 * to change during the invocation of a zpool command (the user isn't
2032 * going to change the ZFS_COLOR value while zpool is running, for
2035 if (use_color != -1) {
2037 * We've already figured out if we should be using color or
2038 * not. Return the cached value.
2043 term = getenv("TERM");
2045 * The user sets the ZFS_COLOR env var set to enable zpool ANSI color
2046 * output. However if NO_COLOR is set (https://no-color.org/) then
2047 * don't use it. Also, don't use color if terminal doesn't support
2050 if (libzfs_envvar_is_set("ZFS_COLOR") &&
2051 !libzfs_envvar_is_set("NO_COLOR") &&
2052 isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 &&
2053 strcmp("unknown", term) != 0) {
2054 /* Color supported */
2064 * color_start() and color_end() are used for when you want to colorize a block
2065 * of text. For example:
2067 * color_start(ANSI_RED_FG)
2073 color_start(char *color)
2076 printf("%s", color);
2086 /* printf() with a color. If color is NULL, then do a normal printf. */
2088 printf_color(char *color, char *format, ...)
2096 va_start(aptr, format);
2097 rc = vprintf(format, aptr);