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, "argument list too long"));
288 case EZFS_INITIALIZING:
289 return (dgettext(TEXT_DOMAIN, "currently initializing"));
290 case EZFS_NO_INITIALIZE:
291 return (dgettext(TEXT_DOMAIN, "there is no active "
294 return (dgettext(TEXT_DOMAIN, "unknown error"));
296 assert(hdl->libzfs_error == 0);
297 return (dgettext(TEXT_DOMAIN, "no error"));
303 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
309 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
311 hdl->libzfs_desc_active = 1;
317 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
319 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
321 hdl->libzfs_error = error;
323 if (hdl->libzfs_desc_active)
324 hdl->libzfs_desc_active = 0;
326 hdl->libzfs_desc[0] = '\0';
328 if (hdl->libzfs_printerr) {
329 if (error == EZFS_UNKNOWN) {
330 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
331 "error: %s\n"), libzfs_error_description(hdl));
335 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
336 libzfs_error_description(hdl));
337 if (error == EZFS_NOMEM)
343 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
345 return (zfs_error_fmt(hdl, error, "%s", msg));
350 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
356 zfs_verror(hdl, error, fmt, ap);
364 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
370 zfs_verror(hdl, EZFS_PERM, fmt, ap);
374 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
378 zfs_verror(hdl, EZFS_IO, fmt, ap);
382 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
386 zfs_verror(hdl, EZFS_INTR, fmt, ap);
394 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
396 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
401 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
407 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
416 zfs_verror(hdl, EZFS_IO, fmt, ap);
420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
421 "dataset does not exist"));
422 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
427 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
432 "dataset already exists"));
433 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
442 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
445 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
448 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
452 "pool I/O is currently suspended"));
453 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
456 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
458 case ZFS_ERR_IOC_CMD_UNAVAIL:
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
460 "module does not support this operation. A reboot may "
461 "be required to enable this operation."));
462 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
464 case ZFS_ERR_IOC_ARG_UNAVAIL:
465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
466 "module does not support an option for this operation. "
467 "A reboot may be required to enable this option."));
468 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
470 case ZFS_ERR_IOC_ARG_REQUIRED:
471 case ZFS_ERR_IOC_ARG_BADTYPE:
472 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
475 zfs_error_aux(hdl, strerror(error));
476 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
485 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
487 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
492 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
498 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
505 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
510 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
511 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
516 "pool already exists"));
517 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
522 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
525 /* There is no pending operation to cancel */
527 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
532 "one or more devices is currently unavailable"));
533 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
537 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
541 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
545 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
550 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "pool I/O is currently suspended"));
556 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
560 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
564 "block size out of range or does not match"));
565 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
568 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
570 case ZFS_ERR_CHECKPOINT_EXISTS:
571 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
573 case ZFS_ERR_DISCARDING_CHECKPOINT:
574 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
576 case ZFS_ERR_NO_CHECKPOINT:
577 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
579 case ZFS_ERR_DEVRM_IN_PROGRESS:
580 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
582 case ZFS_ERR_VDEV_TOO_BIG:
583 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
585 case ZFS_ERR_IOC_CMD_UNAVAIL:
586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
587 "module does not support this operation. A reboot may "
588 "be required to enable this operation."));
589 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
591 case ZFS_ERR_IOC_ARG_UNAVAIL:
592 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
593 "module does not support an option for this operation. "
594 "A reboot may be required to enable this option."));
595 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
597 case ZFS_ERR_IOC_ARG_REQUIRED:
598 case ZFS_ERR_IOC_ARG_BADTYPE:
599 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
602 zfs_error_aux(hdl, strerror(error));
603 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
611 * Display an out of memory error message and abort the current program.
614 no_memory(libzfs_handle_t *hdl)
616 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
620 * A safe form of malloc() which will die if the allocation fails.
623 zfs_alloc(libzfs_handle_t *hdl, size_t size)
627 if ((data = calloc(1, size)) == NULL)
628 (void) no_memory(hdl);
634 * A safe form of asprintf() which will die if the allocation fails.
638 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
646 err = vasprintf(&ret, fmt, ap);
651 (void) no_memory(hdl);
657 * A safe form of realloc(), which also zeroes newly allocated space.
660 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
664 if ((ret = realloc(ptr, newsize)) == NULL) {
665 (void) no_memory(hdl);
669 bzero((char *)ret + oldsize, (newsize - oldsize));
674 * A safe form of strdup() which will die if the allocation fails.
677 zfs_strdup(libzfs_handle_t *hdl, const char *str)
681 if ((ret = strdup(str)) == NULL)
682 (void) no_memory(hdl);
688 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
690 hdl->libzfs_printerr = printerr;
694 libzfs_module_loaded(const char *module)
696 const char path_prefix[] = "/sys/module/";
699 memcpy(path, path_prefix, sizeof (path_prefix) - 1);
700 strcpy(path + sizeof (path_prefix) - 1, module);
702 return (access(path, F_OK) == 0);
707 * Read lines from an open file descriptor and store them in an array of
708 * strings until EOF. lines[] will be allocated and populated with all the
709 * lines read. All newlines are replaced with NULL terminators for
710 * convenience. lines[] must be freed after use with libzfs_free_str_array().
712 * Returns the number of lines read.
715 libzfs_read_stdout_from_fd(int fd, char **lines[])
722 char **tmp_lines = NULL, **tmp;
726 fp = fdopen(fd, "r");
730 rc = getline(&line, &len, fp);
734 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
736 /* Return the lines we were able to process */
741 /* Terminate newlines */
742 if ((nl = strchr(line, '\n')) != NULL)
744 tmp_lines[lines_cnt] = line;
754 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
755 char **lines[], int *lines_cnt)
758 int error, devnull_fd;
762 * Setup a pipe between our child and parent process if we're
765 if ((lines != NULL) && pipe(link) == -1)
771 devnull_fd = open("/dev/null", O_WRONLY);
776 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
777 (void) dup2(devnull_fd, STDOUT_FILENO);
778 else if (lines != NULL) {
779 /* Save the output to lines[] */
780 dup2(link[1], STDOUT_FILENO);
785 if (!(flags & STDERR_VERBOSE))
786 (void) dup2(devnull_fd, STDERR_FILENO);
790 if (flags & NO_DEFAULT_PATH) {
794 execve(path, argv, env);
799 execvpe(path, argv, env);
803 } else if (pid > 0) {
807 while ((error = waitpid(pid, &status, 0)) == -1 &&
809 if (error < 0 || !WIFEXITED(status))
814 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
816 return (WEXITSTATUS(status));
823 libzfs_run_process(const char *path, char *argv[], int flags)
825 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
829 * Run a command and store its stdout lines in an array of strings (lines[]).
830 * lines[] is allocated and populated for you, and the number of lines is set in
831 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
832 * All newlines (\n) in lines[] are terminated for convenience.
835 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
836 char **lines[], int *lines_cnt)
838 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
842 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
843 * means that *path needs to be the full path to the executable.
846 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
847 char *env[], char **lines[], int *lines_cnt)
849 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
854 * Free an array of strings. Free both the strings contained in the array and
858 libzfs_free_str_array(char **strs, int count)
867 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
870 * Returns 0 otherwise.
873 libzfs_envvar_is_set(char *envvar)
875 char *env = getenv(envvar);
876 if (env && (strtoul(env, NULL, 0) > 0 ||
877 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
878 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
885 * Verify the required ZFS_DEV device is available and optionally attempt
886 * to load the ZFS modules. Under normal circumstances the modules
887 * should already have been loaded by some external mechanism.
889 * Environment variables:
890 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
891 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
894 libzfs_load_module(const char *module)
896 char *argv[4] = {"/sbin/modprobe", "-q", (char *)module, (char *)0};
897 char *load_str, *timeout_str;
898 long timeout = 10; /* seconds */
899 long busy_timeout = 10; /* milliseconds */
903 /* Optionally request module loading */
904 if (!libzfs_module_loaded(module)) {
905 load_str = getenv("ZFS_MODULE_LOADING");
907 if (!strncasecmp(load_str, "YES", strlen("YES")) ||
908 !strncasecmp(load_str, "ON", strlen("ON")))
915 if (libzfs_run_process("/sbin/modprobe", argv, 0))
919 if (!libzfs_module_loaded(module))
924 * Device creation by udev is asynchronous and waiting may be
925 * required. Busy wait for 10ms and then fall back to polling every
926 * 10ms for the allowed timeout (default 10s, max 10m). This is
927 * done to optimize for the common case where the device is
928 * immediately available and to avoid penalizing the possible
929 * case where udev is slow or unable to create the device.
931 timeout_str = getenv("ZFS_MODULE_TIMEOUT");
933 timeout = strtol(timeout_str, NULL, 0);
934 timeout = MAX(MIN(timeout, (10 * 60)), 0); /* 0 <= N <= 600 */
939 fd = open(ZFS_DEV, O_RDWR);
943 } else if (errno != ENOENT) {
945 } else if (NSEC2MSEC(gethrtime() - start) < busy_timeout) {
948 usleep(10 * MILLISEC);
950 } while (NSEC2MSEC(gethrtime() - start) < (timeout * MILLISEC));
958 libzfs_handle_t *hdl;
961 error = libzfs_load_module(ZFS_DRIVER);
967 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
971 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
976 #ifdef HAVE_SETMNTENT
977 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
979 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
981 (void) close(hdl->libzfs_fd);
986 hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
988 if (libzfs_core_init() != 0) {
989 (void) close(hdl->libzfs_fd);
990 (void) fclose(hdl->libzfs_mnttab);
991 if (hdl->libzfs_sharetab)
992 (void) fclose(hdl->libzfs_sharetab);
999 zpool_feature_init();
1000 libzfs_mnttab_init(hdl);
1003 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1004 hdl->libzfs_prop_debug = B_TRUE;
1008 * For testing, remove some settable properties and features
1010 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1011 zprop_desc_t *proptbl;
1013 proptbl = zpool_prop_get_table();
1014 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1016 proptbl = zfs_prop_get_table();
1017 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1019 zfeature_info_t *ftbl = spa_feature_table;
1020 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1027 libzfs_fini(libzfs_handle_t *hdl)
1029 (void) close(hdl->libzfs_fd);
1030 if (hdl->libzfs_mnttab)
1031 #ifdef HAVE_SETMNTENT
1032 (void) endmntent(hdl->libzfs_mnttab);
1034 (void) fclose(hdl->libzfs_mnttab);
1036 if (hdl->libzfs_sharetab)
1037 (void) fclose(hdl->libzfs_sharetab);
1038 zfs_uninit_libshare(hdl);
1039 zpool_free_handles(hdl);
1040 namespace_clear(hdl);
1041 libzfs_mnttab_fini(hdl);
1048 zpool_get_handle(zpool_handle_t *zhp)
1050 return (zhp->zpool_hdl);
1054 zfs_get_handle(zfs_handle_t *zhp)
1056 return (zhp->zfs_hdl);
1060 zfs_get_pool_handle(const zfs_handle_t *zhp)
1062 return (zhp->zpool_hdl);
1066 * Given a name, determine whether or not it's a valid path
1067 * (starts with '/' or "./"). If so, walk the mnttab trying
1068 * to match the device number. If not, treat the path as an
1069 * fs/vol/snap/bkmark name.
1072 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
1074 struct stat64 statbuf;
1075 struct extmnttab entry;
1078 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1080 * It's not a valid path, assume it's a name of type 'argtype'.
1082 return (zfs_open(hdl, path, argtype));
1085 if (stat64(path, &statbuf) != 0) {
1086 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
1090 /* Reopen MNTTAB to prevent reading stale data from open file */
1091 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1094 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
1095 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
1104 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1105 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1110 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1114 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1118 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1122 zc->zc_nvlist_dst_size = len;
1124 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1125 if (zc->zc_nvlist_dst == 0)
1132 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1133 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1134 * filled in by the kernel to indicate the actual required size.
1137 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1139 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1141 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1142 if (zc->zc_nvlist_dst == 0)
1149 * Called to free the src and dst nvlists stored in the command structure.
1152 zcmd_free_nvlists(zfs_cmd_t *zc)
1154 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1155 free((void *)(uintptr_t)zc->zc_nvlist_src);
1156 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1157 zc->zc_nvlist_conf = 0;
1158 zc->zc_nvlist_src = 0;
1159 zc->zc_nvlist_dst = 0;
1163 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1169 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1171 if ((packed = zfs_alloc(hdl, len)) == NULL)
1174 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1176 *outnv = (uint64_t)(uintptr_t)packed;
1183 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1185 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1186 &zc->zc_nvlist_conf_size, nvl));
1190 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1192 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1193 &zc->zc_nvlist_src_size, nvl));
1197 * Unpacks an nvlist from the ZFS ioctl command structure.
1200 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1202 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1203 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1204 return (no_memory(hdl));
1210 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
1212 return (ioctl(hdl->libzfs_fd, request, zc));
1216 * ================================================================
1217 * API shared by zfs and zpool property management
1218 * ================================================================
1222 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1224 zprop_list_t *pl = cbp->cb_proplist;
1229 cbp->cb_first = B_FALSE;
1230 if (cbp->cb_scripted)
1234 * Start with the length of the column headers.
1236 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1237 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1239 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1241 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1243 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1246 /* first property is always NAME */
1247 assert(cbp->cb_proplist->pl_prop ==
1248 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1251 * Go through and calculate the widths for each column. For the
1252 * 'source' column, we kludge it up by taking the worst-case scenario of
1253 * inheriting from the longest name. This is acceptable because in the
1254 * majority of cases 'SOURCE' is the last column displayed, and we don't
1255 * use the width anyway. Note that the 'VALUE' column can be oversized,
1256 * if the name of the property is much longer than any values we find.
1258 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1262 if (pl->pl_prop != ZPROP_INVAL) {
1263 const char *propname = (type == ZFS_TYPE_POOL) ?
1264 zpool_prop_to_name(pl->pl_prop) :
1265 zfs_prop_to_name(pl->pl_prop);
1267 len = strlen(propname);
1268 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1269 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1271 len = strlen(pl->pl_user_prop);
1272 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1273 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1277 * 'VALUE' column. The first property is always the 'name'
1278 * property that was tacked on either by /sbin/zfs's
1279 * zfs_do_get() or when calling zprop_expand_list(), so we
1280 * ignore its width. If the user specified the name property
1281 * to display, then it will be later in the list in any case.
1283 if (pl != cbp->cb_proplist &&
1284 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1285 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1287 /* 'RECEIVED' column. */
1288 if (pl != cbp->cb_proplist &&
1289 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1290 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1293 * 'NAME' and 'SOURCE' columns
1295 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1297 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1298 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1299 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1300 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1305 * Now go through and print the headers.
1307 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1308 switch (cbp->cb_columns[i]) {
1310 title = dgettext(TEXT_DOMAIN, "NAME");
1312 case GET_COL_PROPERTY:
1313 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1316 title = dgettext(TEXT_DOMAIN, "VALUE");
1319 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1321 case GET_COL_SOURCE:
1322 title = dgettext(TEXT_DOMAIN, "SOURCE");
1328 if (title != NULL) {
1329 if (i == (ZFS_GET_NCOLS - 1) ||
1330 cbp->cb_columns[i + 1] == GET_COL_NONE)
1331 (void) printf("%s", title);
1333 (void) printf("%-*s ",
1334 cbp->cb_colwidths[cbp->cb_columns[i]],
1338 (void) printf("\n");
1342 * Display a single line of output, according to the settings in the callback
1346 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1347 const char *propname, const char *value, zprop_source_t sourcetype,
1348 const char *source, const char *recvd_value)
1351 const char *str = NULL;
1355 * Ignore those source types that the user has chosen to ignore.
1357 if ((sourcetype & cbp->cb_sources) == 0)
1361 zprop_print_headers(cbp, cbp->cb_type);
1363 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1364 switch (cbp->cb_columns[i]) {
1369 case GET_COL_PROPERTY:
1377 case GET_COL_SOURCE:
1378 switch (sourcetype) {
1379 case ZPROP_SRC_NONE:
1383 case ZPROP_SRC_DEFAULT:
1387 case ZPROP_SRC_LOCAL:
1391 case ZPROP_SRC_TEMPORARY:
1395 case ZPROP_SRC_INHERITED:
1396 (void) snprintf(buf, sizeof (buf),
1397 "inherited from %s", source);
1400 case ZPROP_SRC_RECEIVED:
1406 assert(!"unhandled zprop_source_t");
1411 str = (recvd_value == NULL ? "-" : recvd_value);
1418 if (i == (ZFS_GET_NCOLS - 1) ||
1419 cbp->cb_columns[i + 1] == GET_COL_NONE)
1420 (void) printf("%s", str);
1421 else if (cbp->cb_scripted)
1422 (void) printf("%s\t", str);
1424 (void) printf("%-*s ",
1425 cbp->cb_colwidths[cbp->cb_columns[i]],
1429 (void) printf("\n");
1433 * Given a numeric suffix, convert the value into a number of bits that the
1434 * resulting value must be shifted.
1437 str2shift(libzfs_handle_t *hdl, const char *buf)
1439 const char *ends = "BKMGTPEZ";
1444 for (i = 0; i < strlen(ends); i++) {
1445 if (toupper(buf[0]) == ends[i])
1448 if (i == strlen(ends)) {
1450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1451 "invalid numeric suffix '%s'"), buf);
1456 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1457 * However, 'BB' and 'BiB' are disallowed.
1459 if (buf[1] == '\0' ||
1460 (toupper(buf[0]) != 'B' &&
1461 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1462 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1468 "invalid numeric suffix '%s'"), buf);
1473 * Convert a string of the form '100G' into a real number. Used when setting
1474 * properties or creating a volume. 'buf' is used to place an extended error
1475 * message for the caller to use.
1478 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1485 /* Check to see if this looks like a number. */
1486 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1489 "bad numeric value '%s'"), value);
1493 /* Rely on strtoull() to process the numeric portion. */
1495 *num = strtoull(value, &end, 10);
1498 * Check for ERANGE, which indicates that the value is too large to fit
1499 * in a 64-bit value.
1501 if (errno == ERANGE) {
1503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1504 "numeric value is too large"));
1509 * If we have a decimal value, then do the computation with floating
1510 * point arithmetic. Otherwise, use standard arithmetic.
1513 double fval = strtod(value, &end);
1515 if ((shift = str2shift(hdl, end)) == -1)
1518 fval *= pow(2, shift);
1520 if (fval > UINT64_MAX) {
1522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1523 "numeric value is too large"));
1527 *num = (uint64_t)fval;
1529 if ((shift = str2shift(hdl, end)) == -1)
1532 /* Check for overflow */
1533 if (shift >= 64 || (*num << shift) >> shift != *num) {
1535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1536 "numeric value is too large"));
1547 * Given a propname=value nvpair to set, parse any numeric properties
1548 * (index, boolean, etc) if they are specified as strings and add the
1549 * resulting nvpair to the returned nvlist.
1551 * At the DSL layer, all properties are either 64-bit numbers or strings.
1552 * We want the user to be able to ignore this fact and specify properties
1553 * as native values (numbers, for example) or as strings (to simplify
1554 * command line utilities). This also handles converting index types
1555 * (compression, checksum, etc) from strings to their on-disk index.
1558 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1559 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1562 data_type_t datatype = nvpair_type(elem);
1563 zprop_type_t proptype;
1564 const char *propname;
1566 boolean_t isnone = B_FALSE;
1567 boolean_t isauto = B_FALSE;
1570 if (type == ZFS_TYPE_POOL) {
1571 proptype = zpool_prop_get_type(prop);
1572 propname = zpool_prop_to_name(prop);
1574 proptype = zfs_prop_get_type(prop);
1575 propname = zfs_prop_to_name(prop);
1579 * Convert any properties to the internal DSL value types.
1585 case PROP_TYPE_STRING:
1586 if (datatype != DATA_TYPE_STRING) {
1587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1588 "'%s' must be a string"), nvpair_name(elem));
1591 err = nvpair_value_string(elem, svalp);
1593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1594 "'%s' is invalid"), nvpair_name(elem));
1597 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1598 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1599 "'%s' is too long"), nvpair_name(elem));
1604 case PROP_TYPE_NUMBER:
1605 if (datatype == DATA_TYPE_STRING) {
1606 (void) nvpair_value_string(elem, &value);
1607 if (strcmp(value, "none") == 0) {
1609 } else if (strcmp(value, "auto") == 0) {
1611 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1614 } else if (datatype == DATA_TYPE_UINT64) {
1615 (void) nvpair_value_uint64(elem, ivalp);
1617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1618 "'%s' must be a number"), nvpair_name(elem));
1623 * Quota special: force 'none' and don't allow 0.
1625 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1626 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1628 "use 'none' to disable quota/refquota"));
1633 * Special handling for "*_limit=none". In this case it's not
1636 if ((type & ZFS_TYPE_DATASET) && isnone &&
1637 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1638 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1639 *ivalp = UINT64_MAX;
1643 * Special handling for setting 'refreservation' to 'auto'. Use
1644 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1645 * 'auto' is only allowed on volumes.
1649 case ZFS_PROP_REFRESERVATION:
1650 if ((type & ZFS_TYPE_VOLUME) == 0) {
1651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1652 "'%s=auto' only allowed on "
1653 "volumes"), nvpair_name(elem));
1656 *ivalp = UINT64_MAX;
1659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1660 "'auto' is invalid value for '%s'"),
1668 case PROP_TYPE_INDEX:
1669 if (datatype != DATA_TYPE_STRING) {
1670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1671 "'%s' must be a string"), nvpair_name(elem));
1675 (void) nvpair_value_string(elem, &value);
1677 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1679 "'%s' must be one of '%s'"), propname,
1680 zprop_values(prop, type));
1690 * Add the result to our return set of properties.
1692 if (*svalp != NULL) {
1693 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1694 (void) no_memory(hdl);
1698 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1699 (void) no_memory(hdl);
1706 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1711 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1715 zprop_list_t *entry;
1717 prop = zprop_name_to_prop(propname, type);
1719 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1723 * When no property table entry can be found, return failure if
1724 * this is a pool property or if this isn't a user-defined
1727 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1728 !zpool_prop_feature(propname) &&
1729 !zpool_prop_unsupported(propname)) ||
1730 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1731 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1732 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1733 "invalid property '%s'"), propname);
1734 return (zfs_error(hdl, EZFS_BADPROP,
1735 dgettext(TEXT_DOMAIN, "bad property list")));
1738 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1741 entry->pl_prop = prop;
1742 if (prop == ZPROP_INVAL) {
1743 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1748 entry->pl_width = strlen(propname);
1750 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1760 * Given a comma-separated list of properties, construct a property list
1761 * containing both user-defined and native properties. This function will
1762 * return a NULL list if 'all' is specified, which can later be expanded
1763 * by zprop_expand_list().
1766 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1772 * If 'all' is specified, return a NULL list.
1774 if (strcmp(props, "all") == 0)
1778 * If no props were specified, return an error.
1780 if (props[0] == '\0') {
1781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1782 "no properties specified"));
1783 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1784 "bad property list")));
1788 * It would be nice to use getsubopt() here, but the inclusion of column
1789 * aliases makes this more effort than it's worth.
1791 while (*props != '\0') {
1796 if ((p = strchr(props, ',')) == NULL) {
1797 len = strlen(props);
1804 * Check for empty options.
1807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1808 "empty property name"));
1809 return (zfs_error(hdl, EZFS_BADPROP,
1810 dgettext(TEXT_DOMAIN, "bad property list")));
1814 * Check all regular property names.
1819 if (strcmp(props, "space") == 0) {
1820 static char *spaceprops[] = {
1821 "name", "avail", "used", "usedbysnapshots",
1822 "usedbydataset", "usedbyrefreservation",
1823 "usedbychildren", NULL
1827 for (i = 0; spaceprops[i]; i++) {
1828 if (addlist(hdl, spaceprops[i], listp, type))
1830 listp = &(*listp)->pl_next;
1833 if (addlist(hdl, props, listp, type))
1835 listp = &(*listp)->pl_next;
1847 zprop_free_list(zprop_list_t *pl)
1851 while (pl != NULL) {
1853 free(pl->pl_user_prop);
1859 typedef struct expand_data {
1860 zprop_list_t **last;
1861 libzfs_handle_t *hdl;
1866 zprop_expand_list_cb(int prop, void *cb)
1868 zprop_list_t *entry;
1869 expand_data_t *edp = cb;
1871 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1872 return (ZPROP_INVAL);
1874 entry->pl_prop = prop;
1875 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1876 entry->pl_all = B_TRUE;
1878 *(edp->last) = entry;
1879 edp->last = &entry->pl_next;
1881 return (ZPROP_CONT);
1885 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1887 zprop_list_t *entry;
1888 zprop_list_t **last;
1893 * If this is the very first time we've been called for an 'all'
1894 * specification, expand the list to include all native
1903 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1904 B_FALSE, type) == ZPROP_INVAL)
1908 * Add 'name' to the beginning of the list, which is handled
1911 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1914 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1916 entry->pl_width = zprop_width(entry->pl_prop,
1917 &entry->pl_fixed, type);
1918 entry->pl_all = B_TRUE;
1919 entry->pl_next = *plp;
1926 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1929 return (zprop_iter_common(func, cb, show_all, ordered, type));