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.
51 #include <sys/mnttab.h>
52 #include <sys/mntent.h>
53 #include <sys/types.h>
57 #include <libzfs_core.h>
59 #include "libzfs_impl.h"
61 #include "zfeature_common.h"
62 #include <zfs_fletcher.h>
66 * We only care about the scheme in order to match the scheme
67 * with the handler. Each handler should validate the full URI
70 #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):"
73 libzfs_errno(libzfs_handle_t *hdl)
75 return (hdl->libzfs_error);
79 libzfs_error_action(libzfs_handle_t *hdl)
81 return (hdl->libzfs_action);
85 libzfs_error_description(libzfs_handle_t *hdl)
87 if (hdl->libzfs_desc[0] != '\0')
88 return (hdl->libzfs_desc);
90 switch (hdl->libzfs_error) {
92 return (dgettext(TEXT_DOMAIN, "out of memory"));
94 return (dgettext(TEXT_DOMAIN, "invalid property value"));
95 case EZFS_PROPREADONLY:
96 return (dgettext(TEXT_DOMAIN, "read-only property"));
98 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
99 "datasets of this type"));
100 case EZFS_PROPNONINHERIT:
101 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
103 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
105 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
106 "datasets of this type"));
108 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
110 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
112 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
114 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
115 case EZFS_DSREADONLY:
116 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
118 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
120 case EZFS_INVALIDNAME:
121 return (dgettext(TEXT_DOMAIN, "invalid name"));
122 case EZFS_BADRESTORE:
123 return (dgettext(TEXT_DOMAIN, "unable to restore to "
126 return (dgettext(TEXT_DOMAIN, "backup failed"));
128 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
130 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
132 return (dgettext(TEXT_DOMAIN, "invalid device"));
133 case EZFS_NOREPLICAS:
134 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
135 case EZFS_RESILVERING:
136 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
137 case EZFS_BADVERSION:
138 return (dgettext(TEXT_DOMAIN, "unsupported version or "
140 case EZFS_POOLUNAVAIL:
141 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
142 case EZFS_DEVOVERFLOW:
143 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
145 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
146 case EZFS_CROSSTARGET:
147 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
150 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
151 case EZFS_MOUNTFAILED:
152 return (dgettext(TEXT_DOMAIN, "mount failed"));
153 case EZFS_UMOUNTFAILED:
154 return (dgettext(TEXT_DOMAIN, "unmount failed"));
155 case EZFS_UNSHARENFSFAILED:
156 return (dgettext(TEXT_DOMAIN, "NFS share removal failed"));
157 case EZFS_SHARENFSFAILED:
158 return (dgettext(TEXT_DOMAIN, "NFS share creation failed"));
159 case EZFS_UNSHARESMBFAILED:
160 return (dgettext(TEXT_DOMAIN, "SMB share removal failed"));
161 case EZFS_SHARESMBFAILED:
162 return (dgettext(TEXT_DOMAIN, "SMB share creation failed"));
164 return (dgettext(TEXT_DOMAIN, "permission denied"));
166 return (dgettext(TEXT_DOMAIN, "out of space"));
168 return (dgettext(TEXT_DOMAIN, "bad address"));
170 return (dgettext(TEXT_DOMAIN, "I/O error"));
172 return (dgettext(TEXT_DOMAIN, "signal received"));
174 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
176 case EZFS_INVALCONFIG:
177 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
179 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
181 return (dgettext(TEXT_DOMAIN, "no history available"));
183 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
185 case EZFS_POOL_NOTSUP:
186 return (dgettext(TEXT_DOMAIN, "operation not supported "
187 "on this type of pool"));
188 case EZFS_POOL_INVALARG:
189 return (dgettext(TEXT_DOMAIN, "invalid argument for "
190 "this pool operation"));
191 case EZFS_NAMETOOLONG:
192 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
193 case EZFS_OPENFAILED:
194 return (dgettext(TEXT_DOMAIN, "open failed"));
196 return (dgettext(TEXT_DOMAIN,
197 "disk capacity information could not be retrieved"));
198 case EZFS_LABELFAILED:
199 return (dgettext(TEXT_DOMAIN, "write of label failed"));
201 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
203 return (dgettext(TEXT_DOMAIN, "invalid permission"));
204 case EZFS_BADPERMSET:
205 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
206 case EZFS_NODELEGATION:
207 return (dgettext(TEXT_DOMAIN, "delegated administration is "
208 "disabled on pool"));
210 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
212 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
213 case EZFS_VDEVNOTSUP:
214 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
217 return (dgettext(TEXT_DOMAIN, "operation not supported "
219 case EZFS_IOC_NOTSUPPORTED:
220 return (dgettext(TEXT_DOMAIN, "operation not supported by "
221 "zfs kernel module"));
222 case EZFS_ACTIVE_SPARE:
223 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
225 case EZFS_UNPLAYED_LOGS:
226 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
228 case EZFS_REFTAG_RELE:
229 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
230 case EZFS_REFTAG_HOLD:
231 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
233 case EZFS_TAGTOOLONG:
234 return (dgettext(TEXT_DOMAIN, "tag too long"));
235 case EZFS_PIPEFAILED:
236 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
237 case EZFS_THREADCREATEFAILED:
238 return (dgettext(TEXT_DOMAIN, "thread create failed"));
239 case EZFS_POSTSPLIT_ONLINE:
240 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
242 case EZFS_SCRUB_PAUSED:
243 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
244 "use 'zpool scrub' to resume"));
246 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
247 "use 'zpool scrub -s' to cancel current scrub"));
249 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
251 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
253 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
254 case EZFS_POOLREADONLY:
255 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
256 case EZFS_NO_PENDING:
257 return (dgettext(TEXT_DOMAIN, "operation is not "
259 case EZFS_CHECKPOINT_EXISTS:
260 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
261 case EZFS_DISCARDING_CHECKPOINT:
262 return (dgettext(TEXT_DOMAIN, "currently discarding "
264 case EZFS_NO_CHECKPOINT:
265 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
266 case EZFS_DEVRM_IN_PROGRESS:
267 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
268 case EZFS_VDEV_TOO_BIG:
269 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
270 case EZFS_ACTIVE_POOL:
271 return (dgettext(TEXT_DOMAIN, "pool is imported on a "
273 case EZFS_CRYPTOFAILED:
274 return (dgettext(TEXT_DOMAIN, "encryption failure"));
276 return (dgettext(TEXT_DOMAIN, "argument list too long"));
277 case EZFS_INITIALIZING:
278 return (dgettext(TEXT_DOMAIN, "currently initializing"));
279 case EZFS_NO_INITIALIZE:
280 return (dgettext(TEXT_DOMAIN, "there is no active "
282 case EZFS_WRONG_PARENT:
283 return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
285 return (dgettext(TEXT_DOMAIN, "currently trimming"));
287 return (dgettext(TEXT_DOMAIN, "there is no active trim"));
288 case EZFS_TRIM_NOTSUP:
289 return (dgettext(TEXT_DOMAIN, "trim operations are not "
290 "supported by this device"));
291 case EZFS_NO_RESILVER_DEFER:
292 return (dgettext(TEXT_DOMAIN, "this action requires the "
293 "resilver_defer feature"));
294 case EZFS_EXPORT_IN_PROGRESS:
295 return (dgettext(TEXT_DOMAIN, "pool export in progress"));
296 case EZFS_REBUILDING:
297 return (dgettext(TEXT_DOMAIN, "currently sequentially "
299 case EZFS_VDEV_NOTSUP:
300 return (dgettext(TEXT_DOMAIN, "operation not supported "
301 "on this type of vdev"));
303 return (dgettext(TEXT_DOMAIN, "unknown error"));
305 assert(hdl->libzfs_error == 0);
306 return (dgettext(TEXT_DOMAIN, "no error"));
311 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
317 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
319 hdl->libzfs_desc_active = 1;
325 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
327 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
329 hdl->libzfs_error = error;
331 if (hdl->libzfs_desc_active)
332 hdl->libzfs_desc_active = 0;
334 hdl->libzfs_desc[0] = '\0';
336 if (hdl->libzfs_printerr) {
337 if (error == EZFS_UNKNOWN) {
338 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
339 "error: %s: %s\n"), hdl->libzfs_action,
340 libzfs_error_description(hdl));
344 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
345 libzfs_error_description(hdl));
346 if (error == EZFS_NOMEM)
352 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
354 return (zfs_error_fmt(hdl, error, "%s", msg));
358 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
364 zfs_verror(hdl, error, fmt, ap);
372 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
378 zfs_verror(hdl, EZFS_PERM, fmt, ap);
382 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
386 zfs_verror(hdl, EZFS_IO, fmt, ap);
390 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
394 zfs_verror(hdl, EZFS_INTR, fmt, ap);
402 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
404 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
408 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
414 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
423 zfs_verror(hdl, EZFS_IO, fmt, ap);
427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
428 "dataset does not exist"));
429 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
434 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439 "dataset already exists"));
440 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
446 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
449 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
452 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
455 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
459 "pool I/O is currently suspended"));
460 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
463 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
465 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE:
466 case ZFS_ERR_IOC_CMD_UNAVAIL:
467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
468 "module does not support this operation. A reboot may "
469 "be required to enable this operation."));
470 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
472 case ZFS_ERR_IOC_ARG_UNAVAIL:
473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
474 "module does not support an option for this operation. "
475 "A reboot may be required to enable this option."));
476 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
478 case ZFS_ERR_IOC_ARG_REQUIRED:
479 case ZFS_ERR_IOC_ARG_BADTYPE:
480 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
482 case ZFS_ERR_WRONG_PARENT:
483 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
485 case ZFS_ERR_BADPROP:
486 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
489 zfs_error_aux(hdl, "%s", strerror(error));
490 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
499 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
506 * For quotas and reservations, ENOSPC indicates
507 * something different; setting a quota or reservation
508 * doesn't use any disk space.
512 case ZFS_PROP_REFQUOTA:
513 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
514 "size is less than current used or "
516 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
519 case ZFS_PROP_RESERVATION:
520 case ZFS_PROP_REFRESERVATION:
521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
522 "size is greater than available space"));
523 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
527 (void) zfs_standard_error(hdl, err, errbuf);
533 (void) zfs_standard_error(hdl, EBUSY, errbuf);
537 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
542 "property value too long"));
543 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548 "pool and or dataset must be upgraded to set this "
549 "property or value"));
550 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
554 if (prop == ZFS_PROP_COMPRESSION ||
555 prop == ZFS_PROP_DNODESIZE ||
556 prop == ZFS_PROP_RECORDSIZE) {
557 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
558 "property setting is not allowed on "
559 "bootable datasets"));
560 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
561 } else if (prop == ZFS_PROP_CHECKSUM ||
562 prop == ZFS_PROP_DEDUP) {
563 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
564 "property setting is not allowed on "
566 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
568 (void) zfs_standard_error(hdl, err, errbuf);
573 if (prop == ZPROP_INVAL) {
574 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
576 (void) zfs_standard_error(hdl, err, errbuf);
580 case ZFS_ERR_BADPROP:
581 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
585 if (prop == ZFS_PROP_KEYLOCATION) {
586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
587 "keylocation may only be set on encryption roots"));
588 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
590 (void) zfs_standard_error(hdl, err, errbuf);
596 * This platform can't address a volume this big.
599 if (prop == ZFS_PROP_VOLSIZE) {
600 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
606 (void) zfs_standard_error(hdl, err, errbuf);
611 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
613 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
617 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
623 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
630 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
635 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
636 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
641 "pool already exists"));
642 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
646 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
647 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
650 /* There is no pending operation to cancel */
652 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
656 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
657 "one or more devices is currently unavailable"));
658 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
662 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
666 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
670 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
675 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
680 "pool I/O is currently suspended"));
681 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
685 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
689 "block size out of range or does not match"));
690 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
693 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
695 case ZFS_ERR_CHECKPOINT_EXISTS:
696 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
698 case ZFS_ERR_DISCARDING_CHECKPOINT:
699 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
701 case ZFS_ERR_NO_CHECKPOINT:
702 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
704 case ZFS_ERR_DEVRM_IN_PROGRESS:
705 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
707 case ZFS_ERR_VDEV_TOO_BIG:
708 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
710 case ZFS_ERR_EXPORT_IN_PROGRESS:
711 zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap);
713 case ZFS_ERR_RESILVER_IN_PROGRESS:
714 zfs_verror(hdl, EZFS_RESILVERING, fmt, ap);
716 case ZFS_ERR_REBUILD_IN_PROGRESS:
717 zfs_verror(hdl, EZFS_REBUILDING, fmt, ap);
719 case ZFS_ERR_BADPROP:
720 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
722 case ZFS_ERR_VDEV_NOTSUP:
723 zfs_verror(hdl, EZFS_VDEV_NOTSUP, fmt, ap);
725 case ZFS_ERR_IOC_CMD_UNAVAIL:
726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
727 "module does not support this operation. A reboot may "
728 "be required to enable this operation."));
729 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
731 case ZFS_ERR_IOC_ARG_UNAVAIL:
732 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
733 "module does not support an option for this operation. "
734 "A reboot may be required to enable this option."));
735 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
737 case ZFS_ERR_IOC_ARG_REQUIRED:
738 case ZFS_ERR_IOC_ARG_BADTYPE:
739 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
742 zfs_error_aux(hdl, "%s", strerror(error));
743 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
751 * Display an out of memory error message and abort the current program.
754 no_memory(libzfs_handle_t *hdl)
756 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
760 * A safe form of malloc() which will die if the allocation fails.
763 zfs_alloc(libzfs_handle_t *hdl, size_t size)
767 if ((data = calloc(1, size)) == NULL)
768 (void) no_memory(hdl);
774 * A safe form of asprintf() which will die if the allocation fails.
777 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
785 err = vasprintf(&ret, fmt, ap);
790 (void) no_memory(hdl);
798 * A safe form of realloc(), which also zeroes newly allocated space.
801 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
805 if ((ret = realloc(ptr, newsize)) == NULL) {
806 (void) no_memory(hdl);
810 memset((char *)ret + oldsize, 0, newsize - oldsize);
815 * A safe form of strdup() which will die if the allocation fails.
818 zfs_strdup(libzfs_handle_t *hdl, const char *str)
822 if ((ret = strdup(str)) == NULL)
823 (void) no_memory(hdl);
829 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
831 hdl->libzfs_printerr = printerr;
835 * Read lines from an open file descriptor and store them in an array of
836 * strings until EOF. lines[] will be allocated and populated with all the
837 * lines read. All newlines are replaced with NULL terminators for
838 * convenience. lines[] must be freed after use with libzfs_free_str_array().
840 * Returns the number of lines read.
843 libzfs_read_stdout_from_fd(int fd, char **lines[])
850 char **tmp_lines = NULL, **tmp;
852 fp = fdopen(fd, "r");
857 while (getline(&line, &len, fp) != -1) {
858 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
860 /* Return the lines we were able to process */
865 /* Remove newline if not EOF */
866 if (line[strlen(line) - 1] == '\n')
867 line[strlen(line) - 1] = '\0';
869 tmp_lines[lines_cnt] = strdup(line);
870 if (tmp_lines[lines_cnt] == NULL)
881 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
882 char **lines[], int *lines_cnt)
885 int error, devnull_fd;
889 * Setup a pipe between our child and parent process if we're
892 if (lines != NULL && pipe2(link, O_NONBLOCK | O_CLOEXEC) == -1)
898 devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
903 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
904 (void) dup2(devnull_fd, STDOUT_FILENO);
905 else if (lines != NULL) {
906 /* Save the output to lines[] */
907 dup2(link[1], STDOUT_FILENO);
910 if (!(flags & STDERR_VERBOSE))
911 (void) dup2(devnull_fd, STDERR_FILENO);
913 if (flags & NO_DEFAULT_PATH) {
917 execve(path, argv, env);
922 execvpe(path, argv, env);
926 } else if (pid > 0) {
930 while ((error = waitpid(pid, &status, 0)) == -1 &&
933 if (error < 0 || !WIFEXITED(status))
938 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
940 return (WEXITSTATUS(status));
947 libzfs_run_process(const char *path, char *argv[], int flags)
949 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
953 * Run a command and store its stdout lines in an array of strings (lines[]).
954 * lines[] is allocated and populated for you, and the number of lines is set in
955 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
956 * All newlines (\n) in lines[] are terminated for convenience.
959 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
960 char **lines[], int *lines_cnt)
962 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
966 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
967 * means that *path needs to be the full path to the executable.
970 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
971 char *env[], char **lines[], int *lines_cnt)
973 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
978 * Free an array of strings. Free both the strings contained in the array and
982 libzfs_free_str_array(char **strs, int count)
991 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
994 * Returns 0 otherwise.
997 libzfs_envvar_is_set(char *envvar)
999 char *env = getenv(envvar);
1000 if (env && (strtoul(env, NULL, 0) > 0 ||
1001 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
1002 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
1011 libzfs_handle_t *hdl;
1015 if ((error = libzfs_load_module()) != 0) {
1020 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
1024 if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) {
1029 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) {
1034 if (libzfs_core_init() != 0) {
1035 (void) close(hdl->libzfs_fd);
1042 zpool_feature_init();
1044 libzfs_mnttab_init(hdl);
1047 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1048 hdl->libzfs_prop_debug = B_TRUE;
1050 if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) {
1051 if ((error = zfs_nicestrtonum(hdl, env,
1052 &hdl->libzfs_max_nvlist))) {
1054 (void) close(hdl->libzfs_fd);
1059 hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4);
1063 * For testing, remove some settable properties and features
1065 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1066 zprop_desc_t *proptbl;
1068 proptbl = zpool_prop_get_table();
1069 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1071 proptbl = zfs_prop_get_table();
1072 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1074 zfeature_info_t *ftbl = spa_feature_table;
1075 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1082 libzfs_fini(libzfs_handle_t *hdl)
1084 (void) close(hdl->libzfs_fd);
1085 zpool_free_handles(hdl);
1086 namespace_clear(hdl);
1087 libzfs_mnttab_fini(hdl);
1089 regfree(&hdl->libzfs_urire);
1091 #if LIBFETCH_DYNAMIC
1092 if (hdl->libfetch != (void *)-1 && hdl->libfetch != NULL)
1093 (void) dlclose(hdl->libfetch);
1094 free(hdl->libfetch_load_error);
1100 zpool_get_handle(zpool_handle_t *zhp)
1102 return (zhp->zpool_hdl);
1106 zfs_get_handle(zfs_handle_t *zhp)
1108 return (zhp->zfs_hdl);
1112 zfs_get_pool_handle(const zfs_handle_t *zhp)
1114 return (zhp->zpool_hdl);
1118 * Given a name, determine whether or not it's a valid path
1119 * (starts with '/' or "./"). If so, walk the mnttab trying
1120 * to match the device number. If not, treat the path as an
1121 * fs/vol/snap/bkmark name.
1124 zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype)
1126 struct stat64 statbuf;
1127 struct extmnttab entry;
1129 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1131 * It's not a valid path, assume it's a name of type 'argtype'.
1133 return (zfs_open(hdl, path, argtype));
1136 if (getextmntent(path, &entry, &statbuf) != 0)
1139 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1140 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1145 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1149 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1153 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1157 zc->zc_nvlist_dst_size = len;
1159 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1163 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1164 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1165 * filled in by the kernel to indicate the actual required size.
1168 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1170 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1172 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1176 * Called to free the src and dst nvlists stored in the command structure.
1179 zcmd_free_nvlists(zfs_cmd_t *zc)
1181 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1182 free((void *)(uintptr_t)zc->zc_nvlist_src);
1183 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1184 zc->zc_nvlist_conf = 0;
1185 zc->zc_nvlist_src = 0;
1186 zc->zc_nvlist_dst = 0;
1190 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1195 size_t len = fnvlist_size(nvl);
1196 packed = zfs_alloc(hdl, len);
1198 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1200 *outnv = (uint64_t)(uintptr_t)packed;
1205 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1207 zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1208 &zc->zc_nvlist_conf_size, nvl);
1212 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1214 zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1215 &zc->zc_nvlist_src_size, nvl);
1219 * Unpacks an nvlist from the ZFS ioctl command structure.
1222 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1224 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1225 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1226 return (no_memory(hdl));
1232 * ================================================================
1233 * API shared by zfs and zpool property management
1234 * ================================================================
1238 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1240 zprop_list_t *pl = cbp->cb_proplist;
1245 cbp->cb_first = B_FALSE;
1246 if (cbp->cb_scripted)
1250 * Start with the length of the column headers.
1252 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1253 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1255 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1257 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1259 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1262 /* first property is always NAME */
1263 assert(cbp->cb_proplist->pl_prop ==
1264 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1265 ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME)));
1268 * Go through and calculate the widths for each column. For the
1269 * 'source' column, we kludge it up by taking the worst-case scenario of
1270 * inheriting from the longest name. This is acceptable because in the
1271 * majority of cases 'SOURCE' is the last column displayed, and we don't
1272 * use the width anyway. Note that the 'VALUE' column can be oversized,
1273 * if the name of the property is much longer than any values we find.
1275 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1279 if (pl->pl_prop != ZPROP_INVAL) {
1280 const char *propname = (type == ZFS_TYPE_POOL) ?
1281 zpool_prop_to_name(pl->pl_prop) :
1282 ((type == ZFS_TYPE_VDEV) ?
1283 vdev_prop_to_name(pl->pl_prop) :
1284 zfs_prop_to_name(pl->pl_prop));
1286 assert(propname != NULL);
1287 len = strlen(propname);
1288 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1289 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1291 assert(pl->pl_user_prop != NULL);
1292 len = strlen(pl->pl_user_prop);
1293 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1294 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1298 * 'VALUE' column. The first property is always the 'name'
1299 * property that was tacked on either by /sbin/zfs's
1300 * zfs_do_get() or when calling zprop_expand_list(), so we
1301 * ignore its width. If the user specified the name property
1302 * to display, then it will be later in the list in any case.
1304 if (pl != cbp->cb_proplist &&
1305 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1306 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1308 /* 'RECEIVED' column. */
1309 if (pl != cbp->cb_proplist &&
1310 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1311 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1314 * 'NAME' and 'SOURCE' columns
1316 if (pl->pl_prop == ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1317 ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME :
1318 ZFS_PROP_NAME)) && pl->pl_width >
1319 cbp->cb_colwidths[GET_COL_NAME]) {
1320 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1321 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1322 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1327 * Now go through and print the headers.
1329 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1330 switch (cbp->cb_columns[i]) {
1332 title = dgettext(TEXT_DOMAIN, "NAME");
1334 case GET_COL_PROPERTY:
1335 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1338 title = dgettext(TEXT_DOMAIN, "VALUE");
1341 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1343 case GET_COL_SOURCE:
1344 title = dgettext(TEXT_DOMAIN, "SOURCE");
1350 if (title != NULL) {
1351 if (i == (ZFS_GET_NCOLS - 1) ||
1352 cbp->cb_columns[i + 1] == GET_COL_NONE)
1353 (void) printf("%s", title);
1355 (void) printf("%-*s ",
1356 cbp->cb_colwidths[cbp->cb_columns[i]],
1360 (void) printf("\n");
1364 * Display a single line of output, according to the settings in the callback
1368 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1369 const char *propname, const char *value, zprop_source_t sourcetype,
1370 const char *source, const char *recvd_value)
1373 const char *str = NULL;
1377 * Ignore those source types that the user has chosen to ignore.
1379 if ((sourcetype & cbp->cb_sources) == 0)
1383 zprop_print_headers(cbp, cbp->cb_type);
1385 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1386 switch (cbp->cb_columns[i]) {
1391 case GET_COL_PROPERTY:
1399 case GET_COL_SOURCE:
1400 switch (sourcetype) {
1401 case ZPROP_SRC_NONE:
1405 case ZPROP_SRC_DEFAULT:
1409 case ZPROP_SRC_LOCAL:
1413 case ZPROP_SRC_TEMPORARY:
1417 case ZPROP_SRC_INHERITED:
1418 (void) snprintf(buf, sizeof (buf),
1419 "inherited from %s", source);
1422 case ZPROP_SRC_RECEIVED:
1428 assert(!"unhandled zprop_source_t");
1433 str = (recvd_value == NULL ? "-" : recvd_value);
1440 if (i == (ZFS_GET_NCOLS - 1) ||
1441 cbp->cb_columns[i + 1] == GET_COL_NONE)
1442 (void) printf("%s", str);
1443 else if (cbp->cb_scripted)
1444 (void) printf("%s\t", str);
1446 (void) printf("%-*s ",
1447 cbp->cb_colwidths[cbp->cb_columns[i]],
1451 (void) printf("\n");
1455 * Given a numeric suffix, convert the value into a number of bits that the
1456 * resulting value must be shifted.
1459 str2shift(libzfs_handle_t *hdl, const char *buf)
1461 const char *ends = "BKMGTPEZ";
1466 for (i = 0; i < strlen(ends); i++) {
1467 if (toupper(buf[0]) == ends[i])
1470 if (i == strlen(ends)) {
1472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1473 "invalid numeric suffix '%s'"), buf);
1478 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1479 * However, 'BB' and 'BiB' are disallowed.
1481 if (buf[1] == '\0' ||
1482 (toupper(buf[0]) != 'B' &&
1483 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1484 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1490 "invalid numeric suffix '%s'"), buf);
1495 * Convert a string of the form '100G' into a real number. Used when setting
1496 * properties or creating a volume. 'buf' is used to place an extended error
1497 * message for the caller to use.
1500 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1507 /* Check to see if this looks like a number. */
1508 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511 "bad numeric value '%s'"), value);
1515 /* Rely on strtoull() to process the numeric portion. */
1517 *num = strtoull(value, &end, 10);
1520 * Check for ERANGE, which indicates that the value is too large to fit
1521 * in a 64-bit value.
1523 if (errno == ERANGE) {
1525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1526 "numeric value is too large"));
1531 * If we have a decimal value, then do the computation with floating
1532 * point arithmetic. Otherwise, use standard arithmetic.
1535 double fval = strtod(value, &end);
1537 if ((shift = str2shift(hdl, end)) == -1)
1540 fval *= pow(2, shift);
1543 * UINT64_MAX is not exactly representable as a double.
1544 * The closest representation is UINT64_MAX + 1, so we
1545 * use a >= comparison instead of > for the bounds check.
1547 if (fval >= (double)UINT64_MAX) {
1549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1550 "numeric value is too large"));
1554 *num = (uint64_t)fval;
1556 if ((shift = str2shift(hdl, end)) == -1)
1559 /* Check for overflow */
1560 if (shift >= 64 || (*num << shift) >> shift != *num) {
1562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1563 "numeric value is too large"));
1574 * Given a propname=value nvpair to set, parse any numeric properties
1575 * (index, boolean, etc) if they are specified as strings and add the
1576 * resulting nvpair to the returned nvlist.
1578 * At the DSL layer, all properties are either 64-bit numbers or strings.
1579 * We want the user to be able to ignore this fact and specify properties
1580 * as native values (numbers, for example) or as strings (to simplify
1581 * command line utilities). This also handles converting index types
1582 * (compression, checksum, etc) from strings to their on-disk index.
1585 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1586 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1589 data_type_t datatype = nvpair_type(elem);
1590 zprop_type_t proptype;
1591 const char *propname;
1593 boolean_t isnone = B_FALSE;
1594 boolean_t isauto = B_FALSE;
1597 if (type == ZFS_TYPE_POOL) {
1598 proptype = zpool_prop_get_type(prop);
1599 propname = zpool_prop_to_name(prop);
1600 } else if (type == ZFS_TYPE_VDEV) {
1601 proptype = vdev_prop_get_type(prop);
1602 propname = vdev_prop_to_name(prop);
1604 proptype = zfs_prop_get_type(prop);
1605 propname = zfs_prop_to_name(prop);
1609 * Convert any properties to the internal DSL value types.
1615 case PROP_TYPE_STRING:
1616 if (datatype != DATA_TYPE_STRING) {
1617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1618 "'%s' must be a string"), nvpair_name(elem));
1621 err = nvpair_value_string(elem, svalp);
1623 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1624 "'%s' is invalid"), nvpair_name(elem));
1627 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1629 "'%s' is too long"), nvpair_name(elem));
1634 case PROP_TYPE_NUMBER:
1635 if (datatype == DATA_TYPE_STRING) {
1636 (void) nvpair_value_string(elem, &value);
1637 if (strcmp(value, "none") == 0) {
1639 } else if (strcmp(value, "auto") == 0) {
1641 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1644 } else if (datatype == DATA_TYPE_UINT64) {
1645 (void) nvpair_value_uint64(elem, ivalp);
1647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1648 "'%s' must be a number"), nvpair_name(elem));
1653 * Quota special: force 'none' and don't allow 0.
1655 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1656 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1658 "use 'none' to disable quota/refquota"));
1663 * Special handling for "*_limit=none". In this case it's not
1666 if ((type & ZFS_TYPE_DATASET) && isnone &&
1667 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1668 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1669 *ivalp = UINT64_MAX;
1673 * Special handling for setting 'refreservation' to 'auto'. Use
1674 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1675 * 'auto' is only allowed on volumes.
1679 case ZFS_PROP_REFRESERVATION:
1680 if ((type & ZFS_TYPE_VOLUME) == 0) {
1681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1682 "'%s=auto' only allowed on "
1683 "volumes"), nvpair_name(elem));
1686 *ivalp = UINT64_MAX;
1689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1690 "'auto' is invalid value for '%s'"),
1698 case PROP_TYPE_INDEX:
1699 if (datatype != DATA_TYPE_STRING) {
1700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1701 "'%s' must be a string"), nvpair_name(elem));
1705 (void) nvpair_value_string(elem, &value);
1707 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1709 "'%s' must be one of '%s'"), propname,
1710 zprop_values(prop, type));
1720 * Add the result to our return set of properties.
1722 if (*svalp != NULL) {
1723 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1724 (void) no_memory(hdl);
1728 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1729 (void) no_memory(hdl);
1736 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1741 addlist(libzfs_handle_t *hdl, const char *propname, zprop_list_t **listp,
1744 int prop = zprop_name_to_prop(propname, type);
1745 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1749 * Return failure if no property table entry was found and this isn't
1750 * a user-defined property.
1752 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1753 !zpool_prop_feature(propname) &&
1754 !zpool_prop_unsupported(propname)) ||
1755 ((type == ZFS_TYPE_DATASET) && !zfs_prop_user(propname) &&
1756 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)) ||
1757 ((type == ZFS_TYPE_VDEV) && !vdev_prop_user(propname)))) {
1758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1759 "invalid property '%s'"), propname);
1760 return (zfs_error(hdl, EZFS_BADPROP,
1761 dgettext(TEXT_DOMAIN, "bad property list")));
1764 zprop_list_t *entry = zfs_alloc(hdl, sizeof (*entry));
1766 entry->pl_prop = prop;
1767 if (prop == ZPROP_INVAL) {
1768 entry->pl_user_prop = zfs_strdup(hdl, propname);
1769 entry->pl_width = strlen(propname);
1771 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1781 * Given a comma-separated list of properties, construct a property list
1782 * containing both user-defined and native properties. This function will
1783 * return a NULL list if 'all' is specified, which can later be expanded
1784 * by zprop_expand_list().
1787 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1793 * If 'all' is specified, return a NULL list.
1795 if (strcmp(props, "all") == 0)
1799 * If no props were specified, return an error.
1801 if (props[0] == '\0') {
1802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1803 "no properties specified"));
1804 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1805 "bad property list")));
1808 for (char *p; (p = strsep(&props, ",")); )
1809 if (strcmp(p, "space") == 0) {
1810 static const char *const spaceprops[] = {
1811 "name", "avail", "used", "usedbysnapshots",
1812 "usedbydataset", "usedbyrefreservation",
1816 for (int i = 0; i < ARRAY_SIZE(spaceprops); i++) {
1817 if (addlist(hdl, spaceprops[i], listp, type))
1819 listp = &(*listp)->pl_next;
1822 if (addlist(hdl, p, listp, type))
1824 listp = &(*listp)->pl_next;
1831 zprop_free_list(zprop_list_t *pl)
1835 while (pl != NULL) {
1837 free(pl->pl_user_prop);
1843 typedef struct expand_data {
1844 zprop_list_t **last;
1845 libzfs_handle_t *hdl;
1850 zprop_expand_list_cb(int prop, void *cb)
1852 zprop_list_t *entry;
1853 expand_data_t *edp = cb;
1855 entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t));
1857 entry->pl_prop = prop;
1858 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1859 entry->pl_all = B_TRUE;
1861 *(edp->last) = entry;
1862 edp->last = &entry->pl_next;
1864 return (ZPROP_CONT);
1868 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1870 zprop_list_t *entry;
1871 zprop_list_t **last;
1876 * If this is the very first time we've been called for an 'all'
1877 * specification, expand the list to include all native
1886 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1887 B_FALSE, type) == ZPROP_INVAL)
1891 * Add 'name' to the beginning of the list, which is handled
1894 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
1895 entry->pl_prop = ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1896 ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME));
1897 entry->pl_width = zprop_width(entry->pl_prop,
1898 &entry->pl_fixed, type);
1899 entry->pl_all = B_TRUE;
1900 entry->pl_next = *plp;
1907 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1910 return (zprop_iter_common(func, cb, show_all, ordered, type));
1914 * Fill given version buffer with zfs userland version
1917 zfs_version_userland(char *version, int len)
1919 (void) strlcpy(version, ZFS_META_ALIAS, len);
1923 * Prints both zfs userland and kernel versions
1924 * Returns 0 on success, and -1 on error (with errno set)
1927 zfs_version_print(void)
1929 char zver_userland[128];
1930 char zver_kernel[128];
1932 zfs_version_userland(zver_userland, sizeof (zver_userland));
1934 (void) printf("%s\n", zver_userland);
1936 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
1937 fprintf(stderr, "zfs_version_kernel() failed: %s\n",
1942 (void) printf("zfs-kmod-%s\n", zver_kernel);
1948 * Return 1 if the user requested ANSI color output, and our terminal supports
1949 * it. Return 0 for no color.
1954 static int use_color = -1;
1960 * For each zpool invocation, we do a single check to see if we should
1961 * be using color or not, and cache that value for the lifetime of the
1962 * the zpool command. That makes it cheap to call use_color() when
1963 * we're printing with color. We assume that the settings are not going
1964 * to change during the invocation of a zpool command (the user isn't
1965 * going to change the ZFS_COLOR value while zpool is running, for
1968 if (use_color != -1) {
1970 * We've already figured out if we should be using color or
1971 * not. Return the cached value.
1976 term = getenv("TERM");
1978 * The user sets the ZFS_COLOR env var set to enable zpool ANSI color
1979 * output. However if NO_COLOR is set (https://no-color.org/) then
1980 * don't use it. Also, don't use color if terminal doesn't support
1983 if (libzfs_envvar_is_set("ZFS_COLOR") &&
1984 !libzfs_envvar_is_set("NO_COLOR") &&
1985 isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 &&
1986 strcmp("unknown", term) != 0) {
1987 /* Color supported */
1997 * color_start() and color_end() are used for when you want to colorize a block
1998 * of text. For example:
2000 * color_start(ANSI_RED_FG)
2006 color_start(char *color)
2009 printf("%s", color);
2019 /* printf() with a color. If color is NULL, then do a normal printf. */
2021 printf_color(char *color, char *format, ...)
2029 va_start(aptr, format);
2030 rc = vprintf(format, aptr);