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, 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.
44 #include <sys/mnttab.h>
45 #include <sys/mntent.h>
46 #include <sys/types.h>
50 #include <libzfs_core.h>
52 #include "libzfs_impl.h"
54 #include "zfeature_common.h"
55 #include <zfs_fletcher.h>
59 * We only care about the scheme in order to match the scheme
60 * with the handler. Each handler should validate the full URI
63 #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):"
66 libzfs_errno(libzfs_handle_t *hdl)
68 return (hdl->libzfs_error);
72 libzfs_error_action(libzfs_handle_t *hdl)
74 return (hdl->libzfs_action);
78 libzfs_error_description(libzfs_handle_t *hdl)
80 if (hdl->libzfs_desc[0] != '\0')
81 return (hdl->libzfs_desc);
83 switch (hdl->libzfs_error) {
85 return (dgettext(TEXT_DOMAIN, "out of memory"));
87 return (dgettext(TEXT_DOMAIN, "invalid property value"));
88 case EZFS_PROPREADONLY:
89 return (dgettext(TEXT_DOMAIN, "read-only property"));
91 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
92 "datasets of this type"));
93 case EZFS_PROPNONINHERIT:
94 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
96 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
98 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
99 "datasets of this type"));
101 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
103 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
105 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
107 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
108 case EZFS_DSREADONLY:
109 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
111 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
113 case EZFS_INVALIDNAME:
114 return (dgettext(TEXT_DOMAIN, "invalid name"));
115 case EZFS_BADRESTORE:
116 return (dgettext(TEXT_DOMAIN, "unable to restore to "
119 return (dgettext(TEXT_DOMAIN, "backup failed"));
121 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
123 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
125 return (dgettext(TEXT_DOMAIN, "invalid device"));
126 case EZFS_NOREPLICAS:
127 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
128 case EZFS_RESILVERING:
129 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
130 case EZFS_BADVERSION:
131 return (dgettext(TEXT_DOMAIN, "unsupported version or "
133 case EZFS_POOLUNAVAIL:
134 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
135 case EZFS_DEVOVERFLOW:
136 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
138 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
139 case EZFS_CROSSTARGET:
140 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
143 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
144 case EZFS_MOUNTFAILED:
145 return (dgettext(TEXT_DOMAIN, "mount failed"));
146 case EZFS_UMOUNTFAILED:
147 return (dgettext(TEXT_DOMAIN, "umount failed"));
148 case EZFS_UNSHARENFSFAILED:
149 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
150 case EZFS_SHARENFSFAILED:
151 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
152 case EZFS_UNSHARESMBFAILED:
153 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
154 case EZFS_SHARESMBFAILED:
155 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
157 return (dgettext(TEXT_DOMAIN, "permission denied"));
159 return (dgettext(TEXT_DOMAIN, "out of space"));
161 return (dgettext(TEXT_DOMAIN, "bad address"));
163 return (dgettext(TEXT_DOMAIN, "I/O error"));
165 return (dgettext(TEXT_DOMAIN, "signal received"));
167 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
169 case EZFS_INVALCONFIG:
170 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
172 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
174 return (dgettext(TEXT_DOMAIN, "no history available"));
176 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
178 case EZFS_POOL_NOTSUP:
179 return (dgettext(TEXT_DOMAIN, "operation not supported "
180 "on this type of pool"));
181 case EZFS_POOL_INVALARG:
182 return (dgettext(TEXT_DOMAIN, "invalid argument for "
183 "this pool operation"));
184 case EZFS_NAMETOOLONG:
185 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
186 case EZFS_OPENFAILED:
187 return (dgettext(TEXT_DOMAIN, "open failed"));
189 return (dgettext(TEXT_DOMAIN,
190 "disk capacity information could not be retrieved"));
191 case EZFS_LABELFAILED:
192 return (dgettext(TEXT_DOMAIN, "write of label failed"));
194 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
196 return (dgettext(TEXT_DOMAIN, "invalid permission"));
197 case EZFS_BADPERMSET:
198 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
199 case EZFS_NODELEGATION:
200 return (dgettext(TEXT_DOMAIN, "delegated administration is "
201 "disabled on pool"));
203 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
205 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
206 case EZFS_VDEVNOTSUP:
207 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
210 return (dgettext(TEXT_DOMAIN, "operation not supported "
212 case EZFS_IOC_NOTSUPPORTED:
213 return (dgettext(TEXT_DOMAIN, "operation not supported by "
214 "zfs kernel module"));
215 case EZFS_ACTIVE_SPARE:
216 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
218 case EZFS_UNPLAYED_LOGS:
219 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
221 case EZFS_REFTAG_RELE:
222 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
223 case EZFS_REFTAG_HOLD:
224 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
226 case EZFS_TAGTOOLONG:
227 return (dgettext(TEXT_DOMAIN, "tag too long"));
228 case EZFS_PIPEFAILED:
229 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
230 case EZFS_THREADCREATEFAILED:
231 return (dgettext(TEXT_DOMAIN, "thread create failed"));
232 case EZFS_POSTSPLIT_ONLINE:
233 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
235 case EZFS_SCRUB_PAUSED:
236 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
237 "use 'zpool scrub' to resume"));
239 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
240 "use 'zpool scrub -s' to cancel current scrub"));
242 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
244 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
246 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
247 case EZFS_POOLREADONLY:
248 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
249 case EZFS_NO_PENDING:
250 return (dgettext(TEXT_DOMAIN, "operation is not "
252 case EZFS_CHECKPOINT_EXISTS:
253 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
254 case EZFS_DISCARDING_CHECKPOINT:
255 return (dgettext(TEXT_DOMAIN, "currently discarding "
257 case EZFS_NO_CHECKPOINT:
258 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
259 case EZFS_DEVRM_IN_PROGRESS:
260 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
261 case EZFS_VDEV_TOO_BIG:
262 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
263 case EZFS_ACTIVE_POOL:
264 return (dgettext(TEXT_DOMAIN, "pool is imported on a "
266 case EZFS_CRYPTOFAILED:
267 return (dgettext(TEXT_DOMAIN, "encryption failure"));
269 return (dgettext(TEXT_DOMAIN, "argument list too long"));
270 case EZFS_INITIALIZING:
271 return (dgettext(TEXT_DOMAIN, "currently initializing"));
272 case EZFS_NO_INITIALIZE:
273 return (dgettext(TEXT_DOMAIN, "there is no active "
275 case EZFS_WRONG_PARENT:
276 return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
278 return (dgettext(TEXT_DOMAIN, "currently trimming"));
280 return (dgettext(TEXT_DOMAIN, "there is no active trim"));
281 case EZFS_TRIM_NOTSUP:
282 return (dgettext(TEXT_DOMAIN, "trim operations are not "
283 "supported by this device"));
284 case EZFS_NO_RESILVER_DEFER:
285 return (dgettext(TEXT_DOMAIN, "this action requires the "
286 "resilver_defer feature"));
287 case EZFS_EXPORT_IN_PROGRESS:
288 return (dgettext(TEXT_DOMAIN, "pool export in progress"));
290 return (dgettext(TEXT_DOMAIN, "unknown error"));
292 assert(hdl->libzfs_error == 0);
293 return (dgettext(TEXT_DOMAIN, "no error"));
299 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
305 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
307 hdl->libzfs_desc_active = 1;
313 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
315 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
317 hdl->libzfs_error = error;
319 if (hdl->libzfs_desc_active)
320 hdl->libzfs_desc_active = 0;
322 hdl->libzfs_desc[0] = '\0';
324 if (hdl->libzfs_printerr) {
325 if (error == EZFS_UNKNOWN) {
326 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
327 "error: %s\n"), libzfs_error_description(hdl));
331 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
332 libzfs_error_description(hdl));
333 if (error == EZFS_NOMEM)
339 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
341 return (zfs_error_fmt(hdl, error, "%s", msg));
346 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
352 zfs_verror(hdl, error, fmt, ap);
360 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
366 zfs_verror(hdl, EZFS_PERM, fmt, ap);
370 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
374 zfs_verror(hdl, EZFS_IO, fmt, ap);
378 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
382 zfs_verror(hdl, EZFS_INTR, fmt, ap);
390 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
392 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
397 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
403 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
412 zfs_verror(hdl, EZFS_IO, fmt, ap);
416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
417 "dataset does not exist"));
418 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
423 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
428 "dataset already exists"));
429 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
435 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
438 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
441 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
444 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
448 "pool I/O is currently suspended"));
449 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
452 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
454 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE:
455 case ZFS_ERR_IOC_CMD_UNAVAIL:
456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
457 "module does not support this operation. A reboot may "
458 "be required to enable this operation."));
459 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
461 case ZFS_ERR_IOC_ARG_UNAVAIL:
462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
463 "module does not support an option for this operation. "
464 "A reboot may be required to enable this option."));
465 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
467 case ZFS_ERR_IOC_ARG_REQUIRED:
468 case ZFS_ERR_IOC_ARG_BADTYPE:
469 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
471 case ZFS_ERR_WRONG_PARENT:
472 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
475 zfs_error_aux(hdl, strerror(error));
476 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
485 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
492 * For quotas and reservations, ENOSPC indicates
493 * something different; setting a quota or reservation
494 * doesn't use any disk space.
498 case ZFS_PROP_REFQUOTA:
499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
500 "size is less than current used or "
502 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
505 case ZFS_PROP_RESERVATION:
506 case ZFS_PROP_REFRESERVATION:
507 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
508 "size is greater than available space"));
509 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
513 (void) zfs_standard_error(hdl, err, errbuf);
519 (void) zfs_standard_error(hdl, EBUSY, errbuf);
523 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
528 "property value too long"));
529 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
534 "pool and or dataset must be upgraded to set this "
535 "property or value"));
536 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
540 if (prop == ZFS_PROP_COMPRESSION ||
541 prop == ZFS_PROP_DNODESIZE ||
542 prop == ZFS_PROP_RECORDSIZE) {
543 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
544 "property setting is not allowed on "
545 "bootable datasets"));
546 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
547 } else if (prop == ZFS_PROP_CHECKSUM ||
548 prop == ZFS_PROP_DEDUP) {
549 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
550 "property setting is not allowed on "
552 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
554 (void) zfs_standard_error(hdl, err, errbuf);
559 if (prop == ZPROP_INVAL) {
560 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
562 (void) zfs_standard_error(hdl, err, errbuf);
567 if (prop == ZFS_PROP_KEYLOCATION) {
568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
569 "keylocation may only be set on encryption roots"));
570 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
572 (void) zfs_standard_error(hdl, err, errbuf);
578 * This platform can't address a volume this big.
581 if (prop == ZFS_PROP_VOLSIZE) {
582 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
588 (void) zfs_standard_error(hdl, err, errbuf);
593 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
595 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
600 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
606 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
613 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
618 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
619 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
623 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
624 "pool already exists"));
625 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
630 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
633 /* There is no pending operation to cancel */
635 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
640 "one or more devices is currently unavailable"));
641 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
645 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
649 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
653 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
658 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
663 "pool I/O is currently suspended"));
664 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
668 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
672 "block size out of range or does not match"));
673 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
676 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
678 case ZFS_ERR_CHECKPOINT_EXISTS:
679 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
681 case ZFS_ERR_DISCARDING_CHECKPOINT:
682 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
684 case ZFS_ERR_NO_CHECKPOINT:
685 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
687 case ZFS_ERR_DEVRM_IN_PROGRESS:
688 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
690 case ZFS_ERR_VDEV_TOO_BIG:
691 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
693 case ZFS_ERR_EXPORT_IN_PROGRESS:
694 zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap);
696 case ZFS_ERR_IOC_CMD_UNAVAIL:
697 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
698 "module does not support this operation. A reboot may "
699 "be required to enable this operation."));
700 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
702 case ZFS_ERR_IOC_ARG_UNAVAIL:
703 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
704 "module does not support an option for this operation. "
705 "A reboot may be required to enable this option."));
706 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
708 case ZFS_ERR_IOC_ARG_REQUIRED:
709 case ZFS_ERR_IOC_ARG_BADTYPE:
710 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
713 zfs_error_aux(hdl, strerror(error));
714 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
722 * Display an out of memory error message and abort the current program.
725 no_memory(libzfs_handle_t *hdl)
727 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
731 * A safe form of malloc() which will die if the allocation fails.
734 zfs_alloc(libzfs_handle_t *hdl, size_t size)
738 if ((data = calloc(1, size)) == NULL)
739 (void) no_memory(hdl);
745 * A safe form of asprintf() which will die if the allocation fails.
749 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
757 err = vasprintf(&ret, fmt, ap);
762 (void) no_memory(hdl);
768 * A safe form of realloc(), which also zeroes newly allocated space.
771 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
775 if ((ret = realloc(ptr, newsize)) == NULL) {
776 (void) no_memory(hdl);
780 bzero((char *)ret + oldsize, (newsize - oldsize));
785 * A safe form of strdup() which will die if the allocation fails.
788 zfs_strdup(libzfs_handle_t *hdl, const char *str)
792 if ((ret = strdup(str)) == NULL)
793 (void) no_memory(hdl);
799 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
801 hdl->libzfs_printerr = printerr;
805 * Read lines from an open file descriptor and store them in an array of
806 * strings until EOF. lines[] will be allocated and populated with all the
807 * lines read. All newlines are replaced with NULL terminators for
808 * convenience. lines[] must be freed after use with libzfs_free_str_array().
810 * Returns the number of lines read.
813 libzfs_read_stdout_from_fd(int fd, char **lines[])
820 char **tmp_lines = NULL, **tmp;
824 fp = fdopen(fd, "r");
828 rc = getline(&line, &len, fp);
832 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
834 /* Return the lines we were able to process */
839 /* Terminate newlines */
840 if ((nl = strchr(line, '\n')) != NULL)
842 tmp_lines[lines_cnt] = line;
852 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
853 char **lines[], int *lines_cnt)
856 int error, devnull_fd;
860 * Setup a pipe between our child and parent process if we're
863 if ((lines != NULL) && pipe(link) == -1)
869 devnull_fd = open("/dev/null", O_WRONLY);
874 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
875 (void) dup2(devnull_fd, STDOUT_FILENO);
876 else if (lines != NULL) {
877 /* Save the output to lines[] */
878 dup2(link[1], STDOUT_FILENO);
883 if (!(flags & STDERR_VERBOSE))
884 (void) dup2(devnull_fd, STDERR_FILENO);
888 if (flags & NO_DEFAULT_PATH) {
892 execve(path, argv, env);
897 execvpe(path, argv, env);
901 } else if (pid > 0) {
905 while ((error = waitpid(pid, &status, 0)) == -1 &&
907 if (error < 0 || !WIFEXITED(status))
912 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
914 return (WEXITSTATUS(status));
921 libzfs_run_process(const char *path, char *argv[], int flags)
923 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
927 * Run a command and store its stdout lines in an array of strings (lines[]).
928 * lines[] is allocated and populated for you, and the number of lines is set in
929 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
930 * All newlines (\n) in lines[] are terminated for convenience.
933 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
934 char **lines[], int *lines_cnt)
936 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
940 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
941 * means that *path needs to be the full path to the executable.
944 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
945 char *env[], char **lines[], int *lines_cnt)
947 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
952 * Free an array of strings. Free both the strings contained in the array and
956 libzfs_free_str_array(char **strs, int count)
965 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
968 * Returns 0 otherwise.
971 libzfs_envvar_is_set(char *envvar)
973 char *env = getenv(envvar);
974 if (env && (strtoul(env, NULL, 0) > 0 ||
975 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
976 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
985 libzfs_handle_t *hdl;
988 error = libzfs_load_module();
994 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
998 if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) {
1003 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL)) < 0) {
1008 #ifdef HAVE_SETMNTENT
1009 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
1011 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
1013 (void) close(hdl->libzfs_fd);
1018 hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
1020 if (libzfs_core_init() != 0) {
1021 (void) close(hdl->libzfs_fd);
1022 (void) fclose(hdl->libzfs_mnttab);
1023 if (hdl->libzfs_sharetab)
1024 (void) fclose(hdl->libzfs_sharetab);
1031 zpool_feature_init();
1032 libzfs_mnttab_init(hdl);
1035 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1036 hdl->libzfs_prop_debug = B_TRUE;
1040 * For testing, remove some settable properties and features
1042 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1043 zprop_desc_t *proptbl;
1045 proptbl = zpool_prop_get_table();
1046 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1048 proptbl = zfs_prop_get_table();
1049 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1051 zfeature_info_t *ftbl = spa_feature_table;
1052 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1059 libzfs_fini(libzfs_handle_t *hdl)
1061 (void) close(hdl->libzfs_fd);
1062 if (hdl->libzfs_mnttab)
1063 #ifdef HAVE_SETMNTENT
1064 (void) endmntent(hdl->libzfs_mnttab);
1066 (void) fclose(hdl->libzfs_mnttab);
1068 if (hdl->libzfs_sharetab)
1069 (void) fclose(hdl->libzfs_sharetab);
1070 zfs_uninit_libshare(hdl);
1071 zpool_free_handles(hdl);
1072 namespace_clear(hdl);
1073 libzfs_mnttab_fini(hdl);
1075 regfree(&hdl->libzfs_urire);
1081 zpool_get_handle(zpool_handle_t *zhp)
1083 return (zhp->zpool_hdl);
1087 zfs_get_handle(zfs_handle_t *zhp)
1089 return (zhp->zfs_hdl);
1093 zfs_get_pool_handle(const zfs_handle_t *zhp)
1095 return (zhp->zpool_hdl);
1099 * Given a name, determine whether or not it's a valid path
1100 * (starts with '/' or "./"). If so, walk the mnttab trying
1101 * to match the device number. If not, treat the path as an
1102 * fs/vol/snap/bkmark name.
1105 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
1107 struct stat64 statbuf;
1108 struct extmnttab entry;
1110 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1112 * It's not a valid path, assume it's a name of type 'argtype'.
1114 return (zfs_open(hdl, path, argtype));
1117 /* Reopen MNTTAB to prevent reading stale data from open file */
1118 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1121 if (getextmntent(path, &entry, &statbuf) != 0)
1124 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1125 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1130 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1134 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1138 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1142 zc->zc_nvlist_dst_size = len;
1144 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1145 if (zc->zc_nvlist_dst == 0)
1152 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1153 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1154 * filled in by the kernel to indicate the actual required size.
1157 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1159 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1161 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1162 if (zc->zc_nvlist_dst == 0)
1169 * Called to free the src and dst nvlists stored in the command structure.
1172 zcmd_free_nvlists(zfs_cmd_t *zc)
1174 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1175 free((void *)(uintptr_t)zc->zc_nvlist_src);
1176 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1177 zc->zc_nvlist_conf = 0;
1178 zc->zc_nvlist_src = 0;
1179 zc->zc_nvlist_dst = 0;
1183 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1189 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1191 if ((packed = zfs_alloc(hdl, len)) == NULL)
1194 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1196 *outnv = (uint64_t)(uintptr_t)packed;
1203 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1205 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1206 &zc->zc_nvlist_conf_size, nvl));
1210 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1212 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1213 &zc->zc_nvlist_src_size, nvl));
1217 * Unpacks an nvlist from the ZFS ioctl command structure.
1220 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1222 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1223 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1224 return (no_memory(hdl));
1230 * ================================================================
1231 * API shared by zfs and zpool property management
1232 * ================================================================
1236 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1238 zprop_list_t *pl = cbp->cb_proplist;
1243 cbp->cb_first = B_FALSE;
1244 if (cbp->cb_scripted)
1248 * Start with the length of the column headers.
1250 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1251 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1253 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1255 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1257 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1260 /* first property is always NAME */
1261 assert(cbp->cb_proplist->pl_prop ==
1262 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1265 * Go through and calculate the widths for each column. For the
1266 * 'source' column, we kludge it up by taking the worst-case scenario of
1267 * inheriting from the longest name. This is acceptable because in the
1268 * majority of cases 'SOURCE' is the last column displayed, and we don't
1269 * use the width anyway. Note that the 'VALUE' column can be oversized,
1270 * if the name of the property is much longer than any values we find.
1272 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1276 if (pl->pl_prop != ZPROP_INVAL) {
1277 const char *propname = (type == ZFS_TYPE_POOL) ?
1278 zpool_prop_to_name(pl->pl_prop) :
1279 zfs_prop_to_name(pl->pl_prop);
1281 len = strlen(propname);
1282 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1283 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1285 len = strlen(pl->pl_user_prop);
1286 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1287 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1291 * 'VALUE' column. The first property is always the 'name'
1292 * property that was tacked on either by /sbin/zfs's
1293 * zfs_do_get() or when calling zprop_expand_list(), so we
1294 * ignore its width. If the user specified the name property
1295 * to display, then it will be later in the list in any case.
1297 if (pl != cbp->cb_proplist &&
1298 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1299 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1301 /* 'RECEIVED' column. */
1302 if (pl != cbp->cb_proplist &&
1303 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1304 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1307 * 'NAME' and 'SOURCE' columns
1309 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1311 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1312 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1313 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1314 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1319 * Now go through and print the headers.
1321 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1322 switch (cbp->cb_columns[i]) {
1324 title = dgettext(TEXT_DOMAIN, "NAME");
1326 case GET_COL_PROPERTY:
1327 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1330 title = dgettext(TEXT_DOMAIN, "VALUE");
1333 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1335 case GET_COL_SOURCE:
1336 title = dgettext(TEXT_DOMAIN, "SOURCE");
1342 if (title != NULL) {
1343 if (i == (ZFS_GET_NCOLS - 1) ||
1344 cbp->cb_columns[i + 1] == GET_COL_NONE)
1345 (void) printf("%s", title);
1347 (void) printf("%-*s ",
1348 cbp->cb_colwidths[cbp->cb_columns[i]],
1352 (void) printf("\n");
1356 * Display a single line of output, according to the settings in the callback
1360 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1361 const char *propname, const char *value, zprop_source_t sourcetype,
1362 const char *source, const char *recvd_value)
1365 const char *str = NULL;
1369 * Ignore those source types that the user has chosen to ignore.
1371 if ((sourcetype & cbp->cb_sources) == 0)
1375 zprop_print_headers(cbp, cbp->cb_type);
1377 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1378 switch (cbp->cb_columns[i]) {
1383 case GET_COL_PROPERTY:
1391 case GET_COL_SOURCE:
1392 switch (sourcetype) {
1393 case ZPROP_SRC_NONE:
1397 case ZPROP_SRC_DEFAULT:
1401 case ZPROP_SRC_LOCAL:
1405 case ZPROP_SRC_TEMPORARY:
1409 case ZPROP_SRC_INHERITED:
1410 (void) snprintf(buf, sizeof (buf),
1411 "inherited from %s", source);
1414 case ZPROP_SRC_RECEIVED:
1420 assert(!"unhandled zprop_source_t");
1425 str = (recvd_value == NULL ? "-" : recvd_value);
1432 if (i == (ZFS_GET_NCOLS - 1) ||
1433 cbp->cb_columns[i + 1] == GET_COL_NONE)
1434 (void) printf("%s", str);
1435 else if (cbp->cb_scripted)
1436 (void) printf("%s\t", str);
1438 (void) printf("%-*s ",
1439 cbp->cb_colwidths[cbp->cb_columns[i]],
1443 (void) printf("\n");
1447 * Given a numeric suffix, convert the value into a number of bits that the
1448 * resulting value must be shifted.
1451 str2shift(libzfs_handle_t *hdl, const char *buf)
1453 const char *ends = "BKMGTPEZ";
1458 for (i = 0; i < strlen(ends); i++) {
1459 if (toupper(buf[0]) == ends[i])
1462 if (i == strlen(ends)) {
1464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465 "invalid numeric suffix '%s'"), buf);
1470 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1471 * However, 'BB' and 'BiB' are disallowed.
1473 if (buf[1] == '\0' ||
1474 (toupper(buf[0]) != 'B' &&
1475 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1476 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482 "invalid numeric suffix '%s'"), buf);
1487 * Convert a string of the form '100G' into a real number. Used when setting
1488 * properties or creating a volume. 'buf' is used to place an extended error
1489 * message for the caller to use.
1492 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1499 /* Check to see if this looks like a number. */
1500 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1503 "bad numeric value '%s'"), value);
1507 /* Rely on strtoull() to process the numeric portion. */
1509 *num = strtoull(value, &end, 10);
1512 * Check for ERANGE, which indicates that the value is too large to fit
1513 * in a 64-bit value.
1515 if (errno == ERANGE) {
1517 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1518 "numeric value is too large"));
1523 * If we have a decimal value, then do the computation with floating
1524 * point arithmetic. Otherwise, use standard arithmetic.
1527 double fval = strtod(value, &end);
1529 if ((shift = str2shift(hdl, end)) == -1)
1532 fval *= pow(2, shift);
1535 * UINT64_MAX is not exactly representable as a double.
1536 * The closest representation is UINT64_MAX + 1, so we
1537 * use a >= comparison instead of > for the bounds check.
1539 if (fval >= (double)UINT64_MAX) {
1541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542 "numeric value is too large"));
1546 *num = (uint64_t)fval;
1548 if ((shift = str2shift(hdl, end)) == -1)
1551 /* Check for overflow */
1552 if (shift >= 64 || (*num << shift) >> shift != *num) {
1554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1555 "numeric value is too large"));
1566 * Given a propname=value nvpair to set, parse any numeric properties
1567 * (index, boolean, etc) if they are specified as strings and add the
1568 * resulting nvpair to the returned nvlist.
1570 * At the DSL layer, all properties are either 64-bit numbers or strings.
1571 * We want the user to be able to ignore this fact and specify properties
1572 * as native values (numbers, for example) or as strings (to simplify
1573 * command line utilities). This also handles converting index types
1574 * (compression, checksum, etc) from strings to their on-disk index.
1577 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1578 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1581 data_type_t datatype = nvpair_type(elem);
1582 zprop_type_t proptype;
1583 const char *propname;
1585 boolean_t isnone = B_FALSE;
1586 boolean_t isauto = B_FALSE;
1589 if (type == ZFS_TYPE_POOL) {
1590 proptype = zpool_prop_get_type(prop);
1591 propname = zpool_prop_to_name(prop);
1593 proptype = zfs_prop_get_type(prop);
1594 propname = zfs_prop_to_name(prop);
1598 * Convert any properties to the internal DSL value types.
1604 case PROP_TYPE_STRING:
1605 if (datatype != DATA_TYPE_STRING) {
1606 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1607 "'%s' must be a string"), nvpair_name(elem));
1610 err = nvpair_value_string(elem, svalp);
1612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1613 "'%s' is invalid"), nvpair_name(elem));
1616 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1618 "'%s' is too long"), nvpair_name(elem));
1623 case PROP_TYPE_NUMBER:
1624 if (datatype == DATA_TYPE_STRING) {
1625 (void) nvpair_value_string(elem, &value);
1626 if (strcmp(value, "none") == 0) {
1628 } else if (strcmp(value, "auto") == 0) {
1630 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1633 } else if (datatype == DATA_TYPE_UINT64) {
1634 (void) nvpair_value_uint64(elem, ivalp);
1636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1637 "'%s' must be a number"), nvpair_name(elem));
1642 * Quota special: force 'none' and don't allow 0.
1644 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1645 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1646 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1647 "use 'none' to disable quota/refquota"));
1652 * Special handling for "*_limit=none". In this case it's not
1655 if ((type & ZFS_TYPE_DATASET) && isnone &&
1656 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1657 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1658 *ivalp = UINT64_MAX;
1662 * Special handling for setting 'refreservation' to 'auto'. Use
1663 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1664 * 'auto' is only allowed on volumes.
1668 case ZFS_PROP_REFRESERVATION:
1669 if ((type & ZFS_TYPE_VOLUME) == 0) {
1670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1671 "'%s=auto' only allowed on "
1672 "volumes"), nvpair_name(elem));
1675 *ivalp = UINT64_MAX;
1678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1679 "'auto' is invalid value for '%s'"),
1687 case PROP_TYPE_INDEX:
1688 if (datatype != DATA_TYPE_STRING) {
1689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1690 "'%s' must be a string"), nvpair_name(elem));
1694 (void) nvpair_value_string(elem, &value);
1696 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1697 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1698 "'%s' must be one of '%s'"), propname,
1699 zprop_values(prop, type));
1709 * Add the result to our return set of properties.
1711 if (*svalp != NULL) {
1712 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1713 (void) no_memory(hdl);
1717 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1718 (void) no_memory(hdl);
1725 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1730 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1734 zprop_list_t *entry;
1736 prop = zprop_name_to_prop(propname, type);
1738 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1742 * When no property table entry can be found, return failure if
1743 * this is a pool property or if this isn't a user-defined
1746 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1747 !zpool_prop_feature(propname) &&
1748 !zpool_prop_unsupported(propname)) ||
1749 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1750 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1752 "invalid property '%s'"), propname);
1753 return (zfs_error(hdl, EZFS_BADPROP,
1754 dgettext(TEXT_DOMAIN, "bad property list")));
1757 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1760 entry->pl_prop = prop;
1761 if (prop == ZPROP_INVAL) {
1762 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1767 entry->pl_width = strlen(propname);
1769 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1779 * Given a comma-separated list of properties, construct a property list
1780 * containing both user-defined and native properties. This function will
1781 * return a NULL list if 'all' is specified, which can later be expanded
1782 * by zprop_expand_list().
1785 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1791 * If 'all' is specified, return a NULL list.
1793 if (strcmp(props, "all") == 0)
1797 * If no props were specified, return an error.
1799 if (props[0] == '\0') {
1800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1801 "no properties specified"));
1802 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1803 "bad property list")));
1807 * It would be nice to use getsubopt() here, but the inclusion of column
1808 * aliases makes this more effort than it's worth.
1810 while (*props != '\0') {
1815 if ((p = strchr(props, ',')) == NULL) {
1816 len = strlen(props);
1823 * Check for empty options.
1826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1827 "empty property name"));
1828 return (zfs_error(hdl, EZFS_BADPROP,
1829 dgettext(TEXT_DOMAIN, "bad property list")));
1833 * Check all regular property names.
1838 if (strcmp(props, "space") == 0) {
1839 static char *spaceprops[] = {
1840 "name", "avail", "used", "usedbysnapshots",
1841 "usedbydataset", "usedbyrefreservation",
1842 "usedbychildren", NULL
1846 for (i = 0; spaceprops[i]; i++) {
1847 if (addlist(hdl, spaceprops[i], listp, type))
1849 listp = &(*listp)->pl_next;
1852 if (addlist(hdl, props, listp, type))
1854 listp = &(*listp)->pl_next;
1866 zprop_free_list(zprop_list_t *pl)
1870 while (pl != NULL) {
1872 free(pl->pl_user_prop);
1878 typedef struct expand_data {
1879 zprop_list_t **last;
1880 libzfs_handle_t *hdl;
1885 zprop_expand_list_cb(int prop, void *cb)
1887 zprop_list_t *entry;
1888 expand_data_t *edp = cb;
1890 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1891 return (ZPROP_INVAL);
1893 entry->pl_prop = prop;
1894 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1895 entry->pl_all = B_TRUE;
1897 *(edp->last) = entry;
1898 edp->last = &entry->pl_next;
1900 return (ZPROP_CONT);
1904 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1906 zprop_list_t *entry;
1907 zprop_list_t **last;
1912 * If this is the very first time we've been called for an 'all'
1913 * specification, expand the list to include all native
1922 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1923 B_FALSE, type) == ZPROP_INVAL)
1927 * Add 'name' to the beginning of the list, which is handled
1930 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1933 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1935 entry->pl_width = zprop_width(entry->pl_prop,
1936 &entry->pl_fixed, type);
1937 entry->pl_all = B_TRUE;
1938 entry->pl_next = *plp;
1945 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1948 return (zprop_iter_common(func, cb, show_all, ordered, type));
1952 * Fill given version buffer with zfs userland version
1955 zfs_version_userland(char *version, int len)
1957 (void) strlcpy(version, ZFS_META_ALIAS, len);
1961 * Prints both zfs userland and kernel versions
1962 * Returns 0 on success, and -1 on error (with errno set)
1965 zfs_version_print(void)
1967 char zver_userland[128];
1968 char zver_kernel[128];
1970 zfs_version_userland(zver_userland, sizeof (zver_userland));
1972 (void) printf("%s\n", zver_userland);
1974 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
1975 fprintf(stderr, "zfs_version_kernel() failed: %s\n",
1980 (void) printf("zfs-kmod-%s\n", zver_kernel);
1986 * Return 1 if the user requested ANSI color output, and our terminal supports
1987 * it. Return 0 for no color.
1992 static int use_color = -1;
1998 * For each zpool invocation, we do a single check to see if we should
1999 * be using color or not, and cache that value for the lifetime of the
2000 * the zpool command. That makes it cheap to call use_color() when
2001 * we're printing with color. We assume that the settings are not going
2002 * to change during the invocation of a zpool command (the user isn't
2003 * going to change the ZFS_COLOR value while zpool is running, for
2006 if (use_color != -1) {
2008 * We've already figured out if we should be using color or
2009 * not. Return the cached value.
2014 term = getenv("TERM");
2016 * The user sets the ZFS_COLOR env var set to enable zpool ANSI color
2017 * output. However if NO_COLOR is set (https://no-color.org/) then
2018 * don't use it. Also, don't use color if terminal doesn't support
2021 if (libzfs_envvar_is_set("ZFS_COLOR") &&
2022 !libzfs_envvar_is_set("NO_COLOR") &&
2023 isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 &&
2024 strcmp("unknown", term) != 0) {
2025 /* Color supported */
2035 * color_start() and color_end() are used for when you want to colorize a block
2036 * of text. For example:
2038 * color_start(ANSI_RED_FG)
2044 color_start(char *color)
2047 printf("%s", color);
2057 /* printf() with a color. If color is NULL, then do a normal printf. */
2059 printf_color(char *color, char *format, ...)
2067 va_start(aptr, format);
2068 rc = vprintf(format, aptr);