4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2018, 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 libzfs_errno(libzfs_handle_t *hdl)
61 return (hdl->libzfs_error);
65 libzfs_error_init(int error)
69 return (dgettext(TEXT_DOMAIN, "The ZFS modules are not "
70 "loaded.\nTry running '/sbin/modprobe zfs' as root "
73 return (dgettext(TEXT_DOMAIN, "/dev/zfs and /proc/self/mounts "
74 "are required.\nTry running 'udevadm trigger' and 'mount "
75 "-t proc proc /proc' as root.\n"));
77 return (dgettext(TEXT_DOMAIN, "The ZFS modules cannot be "
78 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
79 "root to manually load them.\n"));
81 return (dgettext(TEXT_DOMAIN, "Permission denied the "
82 "ZFS utilities must be run as root.\n"));
84 return (dgettext(TEXT_DOMAIN, "Failed to initialize the "
85 "libzfs library.\n"));
90 libzfs_error_action(libzfs_handle_t *hdl)
92 return (hdl->libzfs_action);
96 libzfs_error_description(libzfs_handle_t *hdl)
98 if (hdl->libzfs_desc[0] != '\0')
99 return (hdl->libzfs_desc);
101 switch (hdl->libzfs_error) {
103 return (dgettext(TEXT_DOMAIN, "out of memory"));
105 return (dgettext(TEXT_DOMAIN, "invalid property value"));
106 case EZFS_PROPREADONLY:
107 return (dgettext(TEXT_DOMAIN, "read-only property"));
109 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
110 "datasets of this type"));
111 case EZFS_PROPNONINHERIT:
112 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
114 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
116 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
117 "datasets of this type"));
119 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
121 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
123 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
125 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
126 case EZFS_DSREADONLY:
127 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
129 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
131 case EZFS_INVALIDNAME:
132 return (dgettext(TEXT_DOMAIN, "invalid name"));
133 case EZFS_BADRESTORE:
134 return (dgettext(TEXT_DOMAIN, "unable to restore to "
137 return (dgettext(TEXT_DOMAIN, "backup failed"));
139 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
141 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
143 return (dgettext(TEXT_DOMAIN, "invalid device"));
144 case EZFS_NOREPLICAS:
145 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
146 case EZFS_RESILVERING:
147 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
148 case EZFS_BADVERSION:
149 return (dgettext(TEXT_DOMAIN, "unsupported version or "
151 case EZFS_POOLUNAVAIL:
152 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
153 case EZFS_DEVOVERFLOW:
154 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
156 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
157 case EZFS_CROSSTARGET:
158 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
161 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
162 case EZFS_MOUNTFAILED:
163 return (dgettext(TEXT_DOMAIN, "mount failed"));
164 case EZFS_UMOUNTFAILED:
165 return (dgettext(TEXT_DOMAIN, "umount failed"));
166 case EZFS_UNSHARENFSFAILED:
167 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
168 case EZFS_SHARENFSFAILED:
169 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
170 case EZFS_UNSHARESMBFAILED:
171 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
172 case EZFS_SHARESMBFAILED:
173 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
175 return (dgettext(TEXT_DOMAIN, "permission denied"));
177 return (dgettext(TEXT_DOMAIN, "out of space"));
179 return (dgettext(TEXT_DOMAIN, "bad address"));
181 return (dgettext(TEXT_DOMAIN, "I/O error"));
183 return (dgettext(TEXT_DOMAIN, "signal received"));
185 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
187 case EZFS_INVALCONFIG:
188 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
190 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
192 return (dgettext(TEXT_DOMAIN, "no history available"));
194 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
196 case EZFS_POOL_NOTSUP:
197 return (dgettext(TEXT_DOMAIN, "operation not supported "
198 "on this type of pool"));
199 case EZFS_POOL_INVALARG:
200 return (dgettext(TEXT_DOMAIN, "invalid argument for "
201 "this pool operation"));
202 case EZFS_NAMETOOLONG:
203 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
204 case EZFS_OPENFAILED:
205 return (dgettext(TEXT_DOMAIN, "open failed"));
207 return (dgettext(TEXT_DOMAIN,
208 "disk capacity information could not be retrieved"));
209 case EZFS_LABELFAILED:
210 return (dgettext(TEXT_DOMAIN, "write of label failed"));
212 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
214 return (dgettext(TEXT_DOMAIN, "invalid permission"));
215 case EZFS_BADPERMSET:
216 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
217 case EZFS_NODELEGATION:
218 return (dgettext(TEXT_DOMAIN, "delegated administration is "
219 "disabled on pool"));
221 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
223 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
224 case EZFS_VDEVNOTSUP:
225 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
228 return (dgettext(TEXT_DOMAIN, "operation not supported "
230 case EZFS_IOC_NOTSUPPORTED:
231 return (dgettext(TEXT_DOMAIN, "operation not supported by "
232 "zfs kernel module"));
233 case EZFS_ACTIVE_SPARE:
234 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
236 case EZFS_UNPLAYED_LOGS:
237 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
239 case EZFS_REFTAG_RELE:
240 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
241 case EZFS_REFTAG_HOLD:
242 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
244 case EZFS_TAGTOOLONG:
245 return (dgettext(TEXT_DOMAIN, "tag too long"));
246 case EZFS_PIPEFAILED:
247 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
248 case EZFS_THREADCREATEFAILED:
249 return (dgettext(TEXT_DOMAIN, "thread create failed"));
250 case EZFS_POSTSPLIT_ONLINE:
251 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
253 case EZFS_SCRUB_PAUSED:
254 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
255 "use 'zpool scrub' to resume"));
257 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
258 "use 'zpool scrub -s' to cancel current scrub"));
260 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
262 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
264 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
265 case EZFS_POOLREADONLY:
266 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
267 case EZFS_NO_PENDING:
268 return (dgettext(TEXT_DOMAIN, "operation is not "
270 case EZFS_CHECKPOINT_EXISTS:
271 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
272 case EZFS_DISCARDING_CHECKPOINT:
273 return (dgettext(TEXT_DOMAIN, "currently discarding "
275 case EZFS_NO_CHECKPOINT:
276 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
277 case EZFS_DEVRM_IN_PROGRESS:
278 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
279 case EZFS_VDEV_TOO_BIG:
280 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
281 case EZFS_ACTIVE_POOL:
282 return (dgettext(TEXT_DOMAIN, "pool is imported on a "
284 case EZFS_CRYPTOFAILED:
285 return (dgettext(TEXT_DOMAIN, "encryption failure"));
287 return (dgettext(TEXT_DOMAIN, "unknown error"));
289 assert(hdl->libzfs_error == 0);
290 return (dgettext(TEXT_DOMAIN, "no error"));
296 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
302 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
304 hdl->libzfs_desc_active = 1;
310 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
312 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
314 hdl->libzfs_error = error;
316 if (hdl->libzfs_desc_active)
317 hdl->libzfs_desc_active = 0;
319 hdl->libzfs_desc[0] = '\0';
321 if (hdl->libzfs_printerr) {
322 if (error == EZFS_UNKNOWN) {
323 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
324 "error: %s\n"), libzfs_error_description(hdl));
328 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
329 libzfs_error_description(hdl));
330 if (error == EZFS_NOMEM)
336 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
338 return (zfs_error_fmt(hdl, error, "%s", msg));
343 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
349 zfs_verror(hdl, error, fmt, ap);
357 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
363 zfs_verror(hdl, EZFS_PERM, fmt, ap);
367 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
371 zfs_verror(hdl, EZFS_IO, fmt, ap);
375 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
379 zfs_verror(hdl, EZFS_INTR, fmt, ap);
387 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
389 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
394 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
400 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
409 zfs_verror(hdl, EZFS_IO, fmt, ap);
413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
414 "dataset does not exist"));
415 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
420 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
425 "dataset already exists"));
426 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
432 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
435 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
438 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
441 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
445 "pool I/O is currently suspended"));
446 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
449 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
451 case ZFS_ERR_IOC_CMD_UNAVAIL:
452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
453 "module does not support this operation. A reboot may "
454 "be required to enable this operation."));
455 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
457 case ZFS_ERR_IOC_ARG_UNAVAIL:
458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
459 "module does not support an option for this operation. "
460 "A reboot may be required to enable this option."));
461 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
463 case ZFS_ERR_IOC_ARG_REQUIRED:
464 case ZFS_ERR_IOC_ARG_BADTYPE:
465 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
468 zfs_error_aux(hdl, strerror(error));
469 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
478 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
480 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
485 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
491 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
498 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
503 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
504 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 "pool already exists"));
510 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
515 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
518 /* There is no pending operation to cancel */
520 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
525 "one or more devices is currently unavailable"));
526 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
530 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
534 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
538 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
543 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548 "pool I/O is currently suspended"));
549 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
553 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
557 "block size out of range or does not match"));
558 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
561 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
563 case ZFS_ERR_CHECKPOINT_EXISTS:
564 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
566 case ZFS_ERR_DISCARDING_CHECKPOINT:
567 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
569 case ZFS_ERR_NO_CHECKPOINT:
570 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
572 case ZFS_ERR_DEVRM_IN_PROGRESS:
573 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
575 case ZFS_ERR_VDEV_TOO_BIG:
576 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
578 case ZFS_ERR_IOC_CMD_UNAVAIL:
579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
580 "module does not support this operation. A reboot may "
581 "be required to enable this operation."));
582 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
584 case ZFS_ERR_IOC_ARG_UNAVAIL:
585 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
586 "module does not support an option for this operation. "
587 "A reboot may be required to enable this option."));
588 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
590 case ZFS_ERR_IOC_ARG_REQUIRED:
591 case ZFS_ERR_IOC_ARG_BADTYPE:
592 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
595 zfs_error_aux(hdl, strerror(error));
596 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
604 * Display an out of memory error message and abort the current program.
607 no_memory(libzfs_handle_t *hdl)
609 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
613 * A safe form of malloc() which will die if the allocation fails.
616 zfs_alloc(libzfs_handle_t *hdl, size_t size)
620 if ((data = calloc(1, size)) == NULL)
621 (void) no_memory(hdl);
627 * A safe form of asprintf() which will die if the allocation fails.
631 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
639 err = vasprintf(&ret, fmt, ap);
644 (void) no_memory(hdl);
650 * A safe form of realloc(), which also zeroes newly allocated space.
653 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
657 if ((ret = realloc(ptr, newsize)) == NULL) {
658 (void) no_memory(hdl);
662 bzero((char *)ret + oldsize, (newsize - oldsize));
667 * A safe form of strdup() which will die if the allocation fails.
670 zfs_strdup(libzfs_handle_t *hdl, const char *str)
674 if ((ret = strdup(str)) == NULL)
675 (void) no_memory(hdl);
681 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
683 hdl->libzfs_printerr = printerr;
687 libzfs_module_loaded(const char *module)
689 const char path_prefix[] = "/sys/module/";
692 memcpy(path, path_prefix, sizeof (path_prefix) - 1);
693 strcpy(path + sizeof (path_prefix) - 1, module);
695 return (access(path, F_OK) == 0);
700 * Read lines from an open file descriptor and store them in an array of
701 * strings until EOF. lines[] will be allocated and populated with all the
702 * lines read. All newlines are replaced with NULL terminators for
703 * convenience. lines[] must be freed after use with libzfs_free_str_array().
705 * Returns the number of lines read.
708 libzfs_read_stdout_from_fd(int fd, char **lines[])
715 char **tmp_lines = NULL, **tmp;
719 fp = fdopen(fd, "r");
723 rc = getline(&line, &len, fp);
727 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
729 /* Return the lines we were able to process */
734 /* Terminate newlines */
735 if ((nl = strchr(line, '\n')) != NULL)
737 tmp_lines[lines_cnt] = line;
747 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
748 char **lines[], int *lines_cnt)
751 int error, devnull_fd;
755 * Setup a pipe between our child and parent process if we're
758 if ((lines != NULL) && pipe(link) == -1)
764 devnull_fd = open("/dev/null", O_WRONLY);
769 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
770 (void) dup2(devnull_fd, STDOUT_FILENO);
771 else if (lines != NULL) {
772 /* Save the output to lines[] */
773 dup2(link[1], STDOUT_FILENO);
778 if (!(flags & STDERR_VERBOSE))
779 (void) dup2(devnull_fd, STDERR_FILENO);
783 if (flags & NO_DEFAULT_PATH) {
787 execve(path, argv, env);
792 execvpe(path, argv, env);
796 } else if (pid > 0) {
800 while ((error = waitpid(pid, &status, 0)) == -1 &&
802 if (error < 0 || !WIFEXITED(status))
807 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
809 return (WEXITSTATUS(status));
816 libzfs_run_process(const char *path, char *argv[], int flags)
818 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
822 * Run a command and store its stdout lines in an array of strings (lines[]).
823 * lines[] is allocated and populated for you, and the number of lines is set in
824 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
825 * All newlines (\n) in lines[] are terminated for convenience.
828 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
829 char **lines[], int *lines_cnt)
831 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
835 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
836 * means that *path needs to be the full path to the executable.
839 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
840 char *env[], char **lines[], int *lines_cnt)
842 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
847 * Free an array of strings. Free both the strings contained in the array and
851 libzfs_free_str_array(char **strs, int count)
860 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
863 * Returns 0 otherwise.
866 libzfs_envvar_is_set(char *envvar)
868 char *env = getenv(envvar);
869 if (env && (strtoul(env, NULL, 0) > 0 ||
870 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
871 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
878 * Verify the required ZFS_DEV device is available and optionally attempt
879 * to load the ZFS modules. Under normal circumstances the modules
880 * should already have been loaded by some external mechanism.
882 * Environment variables:
883 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
884 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
887 libzfs_load_module(const char *module)
889 char *argv[4] = {"/sbin/modprobe", "-q", (char *)module, (char *)0};
890 char *load_str, *timeout_str;
891 long timeout = 10; /* seconds */
892 long busy_timeout = 10; /* milliseconds */
896 /* Optionally request module loading */
897 if (!libzfs_module_loaded(module)) {
898 load_str = getenv("ZFS_MODULE_LOADING");
900 if (!strncasecmp(load_str, "YES", strlen("YES")) ||
901 !strncasecmp(load_str, "ON", strlen("ON")))
908 if (libzfs_run_process("/sbin/modprobe", argv, 0))
912 if (!libzfs_module_loaded(module))
917 * Device creation by udev is asynchronous and waiting may be
918 * required. Busy wait for 10ms and then fall back to polling every
919 * 10ms for the allowed timeout (default 10s, max 10m). This is
920 * done to optimize for the common case where the device is
921 * immediately available and to avoid penalizing the possible
922 * case where udev is slow or unable to create the device.
924 timeout_str = getenv("ZFS_MODULE_TIMEOUT");
926 timeout = strtol(timeout_str, NULL, 0);
927 timeout = MAX(MIN(timeout, (10 * 60)), 0); /* 0 <= N <= 600 */
932 fd = open(ZFS_DEV, O_RDWR);
936 } else if (errno != ENOENT) {
938 } else if (NSEC2MSEC(gethrtime() - start) < busy_timeout) {
941 usleep(10 * MILLISEC);
943 } while (NSEC2MSEC(gethrtime() - start) < (timeout * MILLISEC));
951 libzfs_handle_t *hdl;
954 error = libzfs_load_module(ZFS_DRIVER);
960 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
964 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
969 #ifdef HAVE_SETMNTENT
970 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
972 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
974 (void) close(hdl->libzfs_fd);
979 hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
981 if (libzfs_core_init() != 0) {
982 (void) close(hdl->libzfs_fd);
983 (void) fclose(hdl->libzfs_mnttab);
984 if (hdl->libzfs_sharetab)
985 (void) fclose(hdl->libzfs_sharetab);
992 zpool_feature_init();
993 libzfs_mnttab_init(hdl);
996 if (getenv("ZFS_PROP_DEBUG") != NULL) {
997 hdl->libzfs_prop_debug = B_TRUE;
1001 * For testing, remove some settable properties and features
1003 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1004 zprop_desc_t *proptbl;
1006 proptbl = zpool_prop_get_table();
1007 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1009 proptbl = zfs_prop_get_table();
1010 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1012 zfeature_info_t *ftbl = spa_feature_table;
1013 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1020 libzfs_fini(libzfs_handle_t *hdl)
1022 (void) close(hdl->libzfs_fd);
1023 if (hdl->libzfs_mnttab)
1024 #ifdef HAVE_SETMNTENT
1025 (void) endmntent(hdl->libzfs_mnttab);
1027 (void) fclose(hdl->libzfs_mnttab);
1029 if (hdl->libzfs_sharetab)
1030 (void) fclose(hdl->libzfs_sharetab);
1031 zfs_uninit_libshare(hdl);
1032 zpool_free_handles(hdl);
1033 namespace_clear(hdl);
1034 libzfs_mnttab_fini(hdl);
1041 zpool_get_handle(zpool_handle_t *zhp)
1043 return (zhp->zpool_hdl);
1047 zfs_get_handle(zfs_handle_t *zhp)
1049 return (zhp->zfs_hdl);
1053 zfs_get_pool_handle(const zfs_handle_t *zhp)
1055 return (zhp->zpool_hdl);
1059 * Given a name, determine whether or not it's a valid path
1060 * (starts with '/' or "./"). If so, walk the mnttab trying
1061 * to match the device number. If not, treat the path as an
1062 * fs/vol/snap/bkmark name.
1065 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
1067 struct stat64 statbuf;
1068 struct extmnttab entry;
1071 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1073 * It's not a valid path, assume it's a name of type 'argtype'.
1075 return (zfs_open(hdl, path, argtype));
1078 if (stat64(path, &statbuf) != 0) {
1079 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
1083 /* Reopen MNTTAB to prevent reading stale data from open file */
1084 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1087 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
1088 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
1097 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1098 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1103 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1107 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1111 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1115 zc->zc_nvlist_dst_size = len;
1117 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1118 if (zc->zc_nvlist_dst == 0)
1125 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1126 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1127 * filled in by the kernel to indicate the actual required size.
1130 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1132 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1134 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1135 if (zc->zc_nvlist_dst == 0)
1142 * Called to free the src and dst nvlists stored in the command structure.
1145 zcmd_free_nvlists(zfs_cmd_t *zc)
1147 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1148 free((void *)(uintptr_t)zc->zc_nvlist_src);
1149 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1150 zc->zc_nvlist_conf = 0;
1151 zc->zc_nvlist_src = 0;
1152 zc->zc_nvlist_dst = 0;
1156 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1162 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1164 if ((packed = zfs_alloc(hdl, len)) == NULL)
1167 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1169 *outnv = (uint64_t)(uintptr_t)packed;
1176 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1178 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1179 &zc->zc_nvlist_conf_size, nvl));
1183 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1185 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1186 &zc->zc_nvlist_src_size, nvl));
1190 * Unpacks an nvlist from the ZFS ioctl command structure.
1193 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1195 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1196 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1197 return (no_memory(hdl));
1203 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
1205 return (ioctl(hdl->libzfs_fd, request, zc));
1209 * ================================================================
1210 * API shared by zfs and zpool property management
1211 * ================================================================
1215 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1217 zprop_list_t *pl = cbp->cb_proplist;
1222 cbp->cb_first = B_FALSE;
1223 if (cbp->cb_scripted)
1227 * Start with the length of the column headers.
1229 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1230 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1232 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1234 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1236 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1239 /* first property is always NAME */
1240 assert(cbp->cb_proplist->pl_prop ==
1241 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1244 * Go through and calculate the widths for each column. For the
1245 * 'source' column, we kludge it up by taking the worst-case scenario of
1246 * inheriting from the longest name. This is acceptable because in the
1247 * majority of cases 'SOURCE' is the last column displayed, and we don't
1248 * use the width anyway. Note that the 'VALUE' column can be oversized,
1249 * if the name of the property is much longer than any values we find.
1251 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1255 if (pl->pl_prop != ZPROP_INVAL) {
1256 const char *propname = (type == ZFS_TYPE_POOL) ?
1257 zpool_prop_to_name(pl->pl_prop) :
1258 zfs_prop_to_name(pl->pl_prop);
1260 len = strlen(propname);
1261 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1262 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1264 len = strlen(pl->pl_user_prop);
1265 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1266 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1270 * 'VALUE' column. The first property is always the 'name'
1271 * property that was tacked on either by /sbin/zfs's
1272 * zfs_do_get() or when calling zprop_expand_list(), so we
1273 * ignore its width. If the user specified the name property
1274 * to display, then it will be later in the list in any case.
1276 if (pl != cbp->cb_proplist &&
1277 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1278 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1280 /* 'RECEIVED' column. */
1281 if (pl != cbp->cb_proplist &&
1282 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1283 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1286 * 'NAME' and 'SOURCE' columns
1288 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1290 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1291 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1292 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1293 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1298 * Now go through and print the headers.
1300 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1301 switch (cbp->cb_columns[i]) {
1303 title = dgettext(TEXT_DOMAIN, "NAME");
1305 case GET_COL_PROPERTY:
1306 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1309 title = dgettext(TEXT_DOMAIN, "VALUE");
1312 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1314 case GET_COL_SOURCE:
1315 title = dgettext(TEXT_DOMAIN, "SOURCE");
1321 if (title != NULL) {
1322 if (i == (ZFS_GET_NCOLS - 1) ||
1323 cbp->cb_columns[i + 1] == GET_COL_NONE)
1324 (void) printf("%s", title);
1326 (void) printf("%-*s ",
1327 cbp->cb_colwidths[cbp->cb_columns[i]],
1331 (void) printf("\n");
1335 * Display a single line of output, according to the settings in the callback
1339 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1340 const char *propname, const char *value, zprop_source_t sourcetype,
1341 const char *source, const char *recvd_value)
1344 const char *str = NULL;
1348 * Ignore those source types that the user has chosen to ignore.
1350 if ((sourcetype & cbp->cb_sources) == 0)
1354 zprop_print_headers(cbp, cbp->cb_type);
1356 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1357 switch (cbp->cb_columns[i]) {
1362 case GET_COL_PROPERTY:
1370 case GET_COL_SOURCE:
1371 switch (sourcetype) {
1372 case ZPROP_SRC_NONE:
1376 case ZPROP_SRC_DEFAULT:
1380 case ZPROP_SRC_LOCAL:
1384 case ZPROP_SRC_TEMPORARY:
1388 case ZPROP_SRC_INHERITED:
1389 (void) snprintf(buf, sizeof (buf),
1390 "inherited from %s", source);
1393 case ZPROP_SRC_RECEIVED:
1399 assert(!"unhandled zprop_source_t");
1404 str = (recvd_value == NULL ? "-" : recvd_value);
1411 if (i == (ZFS_GET_NCOLS - 1) ||
1412 cbp->cb_columns[i + 1] == GET_COL_NONE)
1413 (void) printf("%s", str);
1414 else if (cbp->cb_scripted)
1415 (void) printf("%s\t", str);
1417 (void) printf("%-*s ",
1418 cbp->cb_colwidths[cbp->cb_columns[i]],
1422 (void) printf("\n");
1426 * Given a numeric suffix, convert the value into a number of bits that the
1427 * resulting value must be shifted.
1430 str2shift(libzfs_handle_t *hdl, const char *buf)
1432 const char *ends = "BKMGTPEZ";
1437 for (i = 0; i < strlen(ends); i++) {
1438 if (toupper(buf[0]) == ends[i])
1441 if (i == strlen(ends)) {
1443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1444 "invalid numeric suffix '%s'"), buf);
1449 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1450 * However, 'BB' and 'BiB' are disallowed.
1452 if (buf[1] == '\0' ||
1453 (toupper(buf[0]) != 'B' &&
1454 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1455 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1461 "invalid numeric suffix '%s'"), buf);
1466 * Convert a string of the form '100G' into a real number. Used when setting
1467 * properties or creating a volume. 'buf' is used to place an extended error
1468 * message for the caller to use.
1471 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1478 /* Check to see if this looks like a number. */
1479 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482 "bad numeric value '%s'"), value);
1486 /* Rely on strtoull() to process the numeric portion. */
1488 *num = strtoull(value, &end, 10);
1491 * Check for ERANGE, which indicates that the value is too large to fit
1492 * in a 64-bit value.
1494 if (errno == ERANGE) {
1496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1497 "numeric value is too large"));
1502 * If we have a decimal value, then do the computation with floating
1503 * point arithmetic. Otherwise, use standard arithmetic.
1506 double fval = strtod(value, &end);
1508 if ((shift = str2shift(hdl, end)) == -1)
1511 fval *= pow(2, shift);
1513 if (fval > UINT64_MAX) {
1515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1516 "numeric value is too large"));
1520 *num = (uint64_t)fval;
1522 if ((shift = str2shift(hdl, end)) == -1)
1525 /* Check for overflow */
1526 if (shift >= 64 || (*num << shift) >> shift != *num) {
1528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1529 "numeric value is too large"));
1540 * Given a propname=value nvpair to set, parse any numeric properties
1541 * (index, boolean, etc) if they are specified as strings and add the
1542 * resulting nvpair to the returned nvlist.
1544 * At the DSL layer, all properties are either 64-bit numbers or strings.
1545 * We want the user to be able to ignore this fact and specify properties
1546 * as native values (numbers, for example) or as strings (to simplify
1547 * command line utilities). This also handles converting index types
1548 * (compression, checksum, etc) from strings to their on-disk index.
1551 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1552 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1555 data_type_t datatype = nvpair_type(elem);
1556 zprop_type_t proptype;
1557 const char *propname;
1559 boolean_t isnone = B_FALSE;
1560 boolean_t isauto = B_FALSE;
1563 if (type == ZFS_TYPE_POOL) {
1564 proptype = zpool_prop_get_type(prop);
1565 propname = zpool_prop_to_name(prop);
1567 proptype = zfs_prop_get_type(prop);
1568 propname = zfs_prop_to_name(prop);
1572 * Convert any properties to the internal DSL value types.
1578 case PROP_TYPE_STRING:
1579 if (datatype != DATA_TYPE_STRING) {
1580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1581 "'%s' must be a string"), nvpair_name(elem));
1584 err = nvpair_value_string(elem, svalp);
1586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1587 "'%s' is invalid"), nvpair_name(elem));
1590 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1592 "'%s' is too long"), nvpair_name(elem));
1597 case PROP_TYPE_NUMBER:
1598 if (datatype == DATA_TYPE_STRING) {
1599 (void) nvpair_value_string(elem, &value);
1600 if (strcmp(value, "none") == 0) {
1602 } else if (strcmp(value, "auto") == 0) {
1604 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1607 } else if (datatype == DATA_TYPE_UINT64) {
1608 (void) nvpair_value_uint64(elem, ivalp);
1610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1611 "'%s' must be a number"), nvpair_name(elem));
1616 * Quota special: force 'none' and don't allow 0.
1618 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1619 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1621 "use 'none' to disable quota/refquota"));
1626 * Special handling for "*_limit=none". In this case it's not
1629 if ((type & ZFS_TYPE_DATASET) && isnone &&
1630 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1631 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1632 *ivalp = UINT64_MAX;
1636 * Special handling for setting 'refreservation' to 'auto'. Use
1637 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1638 * 'auto' is only allowed on volumes.
1642 case ZFS_PROP_REFRESERVATION:
1643 if ((type & ZFS_TYPE_VOLUME) == 0) {
1644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1645 "'%s=auto' only allowed on "
1646 "volumes"), nvpair_name(elem));
1649 *ivalp = UINT64_MAX;
1652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1653 "'auto' is invalid value for '%s'"),
1661 case PROP_TYPE_INDEX:
1662 if (datatype != DATA_TYPE_STRING) {
1663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1664 "'%s' must be a string"), nvpair_name(elem));
1668 (void) nvpair_value_string(elem, &value);
1670 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1672 "'%s' must be one of '%s'"), propname,
1673 zprop_values(prop, type));
1683 * Add the result to our return set of properties.
1685 if (*svalp != NULL) {
1686 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1687 (void) no_memory(hdl);
1691 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1692 (void) no_memory(hdl);
1699 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1704 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1708 zprop_list_t *entry;
1710 prop = zprop_name_to_prop(propname, type);
1712 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1716 * When no property table entry can be found, return failure if
1717 * this is a pool property or if this isn't a user-defined
1720 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1721 !zpool_prop_feature(propname) &&
1722 !zpool_prop_unsupported(propname)) ||
1723 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1724 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1726 "invalid property '%s'"), propname);
1727 return (zfs_error(hdl, EZFS_BADPROP,
1728 dgettext(TEXT_DOMAIN, "bad property list")));
1731 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1734 entry->pl_prop = prop;
1735 if (prop == ZPROP_INVAL) {
1736 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1741 entry->pl_width = strlen(propname);
1743 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1753 * Given a comma-separated list of properties, construct a property list
1754 * containing both user-defined and native properties. This function will
1755 * return a NULL list if 'all' is specified, which can later be expanded
1756 * by zprop_expand_list().
1759 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1765 * If 'all' is specified, return a NULL list.
1767 if (strcmp(props, "all") == 0)
1771 * If no props were specified, return an error.
1773 if (props[0] == '\0') {
1774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1775 "no properties specified"));
1776 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1777 "bad property list")));
1781 * It would be nice to use getsubopt() here, but the inclusion of column
1782 * aliases makes this more effort than it's worth.
1784 while (*props != '\0') {
1789 if ((p = strchr(props, ',')) == NULL) {
1790 len = strlen(props);
1797 * Check for empty options.
1800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1801 "empty property name"));
1802 return (zfs_error(hdl, EZFS_BADPROP,
1803 dgettext(TEXT_DOMAIN, "bad property list")));
1807 * Check all regular property names.
1812 if (strcmp(props, "space") == 0) {
1813 static char *spaceprops[] = {
1814 "name", "avail", "used", "usedbysnapshots",
1815 "usedbydataset", "usedbyrefreservation",
1816 "usedbychildren", NULL
1820 for (i = 0; spaceprops[i]; i++) {
1821 if (addlist(hdl, spaceprops[i], listp, type))
1823 listp = &(*listp)->pl_next;
1826 if (addlist(hdl, props, listp, type))
1828 listp = &(*listp)->pl_next;
1840 zprop_free_list(zprop_list_t *pl)
1844 while (pl != NULL) {
1846 free(pl->pl_user_prop);
1852 typedef struct expand_data {
1853 zprop_list_t **last;
1854 libzfs_handle_t *hdl;
1859 zprop_expand_list_cb(int prop, void *cb)
1861 zprop_list_t *entry;
1862 expand_data_t *edp = cb;
1864 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1865 return (ZPROP_INVAL);
1867 entry->pl_prop = prop;
1868 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1869 entry->pl_all = B_TRUE;
1871 *(edp->last) = entry;
1872 edp->last = &entry->pl_next;
1874 return (ZPROP_CONT);
1878 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1880 zprop_list_t *entry;
1881 zprop_list_t **last;
1886 * If this is the very first time we've been called for an 'all'
1887 * specification, expand the list to include all native
1896 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1897 B_FALSE, type) == ZPROP_INVAL)
1901 * Add 'name' to the beginning of the list, which is handled
1904 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1907 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1909 entry->pl_width = zprop_width(entry->pl_prop,
1910 &entry->pl_fixed, type);
1911 entry->pl_all = B_TRUE;
1912 entry->pl_next = *plp;
1919 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1922 return (zprop_iter_common(func, cb, show_all, ordered, type));