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>
57 #include <sys/zfs_sysfs.h>
60 libzfs_errno(libzfs_handle_t *hdl)
62 return (hdl->libzfs_error);
66 libzfs_error_init(int error)
70 return (dgettext(TEXT_DOMAIN, "The ZFS modules are not "
71 "loaded.\nTry running '/sbin/modprobe zfs' as root "
74 return (dgettext(TEXT_DOMAIN, "/dev/zfs and /proc/self/mounts "
75 "are required.\nTry running 'udevadm trigger' and 'mount "
76 "-t proc proc /proc' as root.\n"));
78 return (dgettext(TEXT_DOMAIN, "The ZFS modules cannot be "
79 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
80 "root to manually load them.\n"));
82 return (dgettext(TEXT_DOMAIN, "Permission denied the "
83 "ZFS utilities must be run as root.\n"));
85 return (dgettext(TEXT_DOMAIN, "Failed to initialize the "
86 "libzfs library.\n"));
91 libzfs_error_action(libzfs_handle_t *hdl)
93 return (hdl->libzfs_action);
97 libzfs_error_description(libzfs_handle_t *hdl)
99 if (hdl->libzfs_desc[0] != '\0')
100 return (hdl->libzfs_desc);
102 switch (hdl->libzfs_error) {
104 return (dgettext(TEXT_DOMAIN, "out of memory"));
106 return (dgettext(TEXT_DOMAIN, "invalid property value"));
107 case EZFS_PROPREADONLY:
108 return (dgettext(TEXT_DOMAIN, "read-only property"));
110 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
111 "datasets of this type"));
112 case EZFS_PROPNONINHERIT:
113 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
115 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
117 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
118 "datasets of this type"));
120 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
122 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
124 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
126 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
127 case EZFS_DSREADONLY:
128 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
130 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
132 case EZFS_INVALIDNAME:
133 return (dgettext(TEXT_DOMAIN, "invalid name"));
134 case EZFS_BADRESTORE:
135 return (dgettext(TEXT_DOMAIN, "unable to restore to "
138 return (dgettext(TEXT_DOMAIN, "backup failed"));
140 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
142 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
144 return (dgettext(TEXT_DOMAIN, "invalid device"));
145 case EZFS_NOREPLICAS:
146 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
147 case EZFS_RESILVERING:
148 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
149 case EZFS_BADVERSION:
150 return (dgettext(TEXT_DOMAIN, "unsupported version or "
152 case EZFS_POOLUNAVAIL:
153 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
154 case EZFS_DEVOVERFLOW:
155 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
157 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
158 case EZFS_CROSSTARGET:
159 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
162 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
163 case EZFS_MOUNTFAILED:
164 return (dgettext(TEXT_DOMAIN, "mount failed"));
165 case EZFS_UMOUNTFAILED:
166 return (dgettext(TEXT_DOMAIN, "umount failed"));
167 case EZFS_UNSHARENFSFAILED:
168 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
169 case EZFS_SHARENFSFAILED:
170 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
171 case EZFS_UNSHARESMBFAILED:
172 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
173 case EZFS_SHARESMBFAILED:
174 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
176 return (dgettext(TEXT_DOMAIN, "permission denied"));
178 return (dgettext(TEXT_DOMAIN, "out of space"));
180 return (dgettext(TEXT_DOMAIN, "bad address"));
182 return (dgettext(TEXT_DOMAIN, "I/O error"));
184 return (dgettext(TEXT_DOMAIN, "signal received"));
186 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
188 case EZFS_INVALCONFIG:
189 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
191 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
193 return (dgettext(TEXT_DOMAIN, "no history available"));
195 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
197 case EZFS_POOL_NOTSUP:
198 return (dgettext(TEXT_DOMAIN, "operation not supported "
199 "on this type of pool"));
200 case EZFS_POOL_INVALARG:
201 return (dgettext(TEXT_DOMAIN, "invalid argument for "
202 "this pool operation"));
203 case EZFS_NAMETOOLONG:
204 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
205 case EZFS_OPENFAILED:
206 return (dgettext(TEXT_DOMAIN, "open failed"));
208 return (dgettext(TEXT_DOMAIN,
209 "disk capacity information could not be retrieved"));
210 case EZFS_LABELFAILED:
211 return (dgettext(TEXT_DOMAIN, "write of label failed"));
213 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
215 return (dgettext(TEXT_DOMAIN, "invalid permission"));
216 case EZFS_BADPERMSET:
217 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
218 case EZFS_NODELEGATION:
219 return (dgettext(TEXT_DOMAIN, "delegated administration is "
220 "disabled on pool"));
222 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
224 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
225 case EZFS_VDEVNOTSUP:
226 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
229 return (dgettext(TEXT_DOMAIN, "operation not supported "
231 case EZFS_IOC_NOTSUPPORTED:
232 return (dgettext(TEXT_DOMAIN, "operation not supported by "
233 "zfs kernel module"));
234 case EZFS_ACTIVE_SPARE:
235 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
237 case EZFS_UNPLAYED_LOGS:
238 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
240 case EZFS_REFTAG_RELE:
241 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
242 case EZFS_REFTAG_HOLD:
243 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
245 case EZFS_TAGTOOLONG:
246 return (dgettext(TEXT_DOMAIN, "tag too long"));
247 case EZFS_PIPEFAILED:
248 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
249 case EZFS_THREADCREATEFAILED:
250 return (dgettext(TEXT_DOMAIN, "thread create failed"));
251 case EZFS_POSTSPLIT_ONLINE:
252 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
254 case EZFS_SCRUB_PAUSED:
255 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
256 "use 'zpool scrub' to resume"));
258 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
259 "use 'zpool scrub -s' to cancel current scrub"));
261 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
263 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
265 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
266 case EZFS_POOLREADONLY:
267 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
268 case EZFS_NO_PENDING:
269 return (dgettext(TEXT_DOMAIN, "operation is not "
271 case EZFS_CHECKPOINT_EXISTS:
272 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
273 case EZFS_DISCARDING_CHECKPOINT:
274 return (dgettext(TEXT_DOMAIN, "currently discarding "
276 case EZFS_NO_CHECKPOINT:
277 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
278 case EZFS_DEVRM_IN_PROGRESS:
279 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
280 case EZFS_VDEV_TOO_BIG:
281 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
282 case EZFS_ACTIVE_POOL:
283 return (dgettext(TEXT_DOMAIN, "pool is imported on a "
285 case EZFS_CRYPTOFAILED:
286 return (dgettext(TEXT_DOMAIN, "encryption failure"));
288 return (dgettext(TEXT_DOMAIN, "argument list too long"));
289 case EZFS_INITIALIZING:
290 return (dgettext(TEXT_DOMAIN, "currently initializing"));
291 case EZFS_NO_INITIALIZE:
292 return (dgettext(TEXT_DOMAIN, "there is no active "
294 case EZFS_WRONG_PARENT:
295 return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
297 return (dgettext(TEXT_DOMAIN, "currently trimming"));
299 return (dgettext(TEXT_DOMAIN, "there is no active trim"));
300 case EZFS_TRIM_NOTSUP:
301 return (dgettext(TEXT_DOMAIN, "trim operations are not "
302 "supported by this device"));
303 case EZFS_NO_RESILVER_DEFER:
304 return (dgettext(TEXT_DOMAIN, "this action requires the "
305 "resilver_defer feature"));
307 return (dgettext(TEXT_DOMAIN, "unknown error"));
309 assert(hdl->libzfs_error == 0);
310 return (dgettext(TEXT_DOMAIN, "no error"));
316 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
322 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
324 hdl->libzfs_desc_active = 1;
330 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
332 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
334 hdl->libzfs_error = error;
336 if (hdl->libzfs_desc_active)
337 hdl->libzfs_desc_active = 0;
339 hdl->libzfs_desc[0] = '\0';
341 if (hdl->libzfs_printerr) {
342 if (error == EZFS_UNKNOWN) {
343 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
344 "error: %s\n"), libzfs_error_description(hdl));
348 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
349 libzfs_error_description(hdl));
350 if (error == EZFS_NOMEM)
356 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
358 return (zfs_error_fmt(hdl, error, "%s", msg));
363 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
369 zfs_verror(hdl, error, fmt, ap);
377 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
383 zfs_verror(hdl, EZFS_PERM, fmt, ap);
387 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
391 zfs_verror(hdl, EZFS_IO, fmt, ap);
395 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
399 zfs_verror(hdl, EZFS_INTR, fmt, ap);
407 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
409 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
414 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
420 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
429 zfs_verror(hdl, EZFS_IO, fmt, ap);
433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
434 "dataset does not exist"));
435 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
440 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
445 "dataset already exists"));
446 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
452 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
455 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
458 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
461 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
465 "pool I/O is currently suspended"));
466 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
469 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
471 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE:
472 case ZFS_ERR_IOC_CMD_UNAVAIL:
473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
474 "module does not support this operation. A reboot may "
475 "be required to enable this operation."));
476 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
478 case ZFS_ERR_IOC_ARG_UNAVAIL:
479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
480 "module does not support an option for this operation. "
481 "A reboot may be required to enable this option."));
482 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
484 case ZFS_ERR_IOC_ARG_REQUIRED:
485 case ZFS_ERR_IOC_ARG_BADTYPE:
486 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
488 case ZFS_ERR_WRONG_PARENT:
489 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
492 zfs_error_aux(hdl, strerror(error));
493 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
502 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
504 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
509 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
515 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
522 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
527 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
528 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 "pool already exists"));
534 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
539 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
542 /* There is no pending operation to cancel */
544 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
549 "one or more devices is currently unavailable"));
550 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
554 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
558 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
562 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
567 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
572 "pool I/O is currently suspended"));
573 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
577 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
581 "block size out of range or does not match"));
582 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
585 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
587 case ZFS_ERR_CHECKPOINT_EXISTS:
588 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
590 case ZFS_ERR_DISCARDING_CHECKPOINT:
591 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
593 case ZFS_ERR_NO_CHECKPOINT:
594 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
596 case ZFS_ERR_DEVRM_IN_PROGRESS:
597 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
599 case ZFS_ERR_VDEV_TOO_BIG:
600 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
602 case ZFS_ERR_IOC_CMD_UNAVAIL:
603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
604 "module does not support this operation. A reboot may "
605 "be required to enable this operation."));
606 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
608 case ZFS_ERR_IOC_ARG_UNAVAIL:
609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
610 "module does not support an option for this operation. "
611 "A reboot may be required to enable this option."));
612 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
614 case ZFS_ERR_IOC_ARG_REQUIRED:
615 case ZFS_ERR_IOC_ARG_BADTYPE:
616 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
619 zfs_error_aux(hdl, strerror(error));
620 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
628 * Display an out of memory error message and abort the current program.
631 no_memory(libzfs_handle_t *hdl)
633 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
637 * A safe form of malloc() which will die if the allocation fails.
640 zfs_alloc(libzfs_handle_t *hdl, size_t size)
644 if ((data = calloc(1, size)) == NULL)
645 (void) no_memory(hdl);
651 * A safe form of asprintf() which will die if the allocation fails.
655 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
663 err = vasprintf(&ret, fmt, ap);
668 (void) no_memory(hdl);
674 * A safe form of realloc(), which also zeroes newly allocated space.
677 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
681 if ((ret = realloc(ptr, newsize)) == NULL) {
682 (void) no_memory(hdl);
686 bzero((char *)ret + oldsize, (newsize - oldsize));
691 * A safe form of strdup() which will die if the allocation fails.
694 zfs_strdup(libzfs_handle_t *hdl, const char *str)
698 if ((ret = strdup(str)) == NULL)
699 (void) no_memory(hdl);
705 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
707 hdl->libzfs_printerr = printerr;
711 libzfs_module_loaded(const char *module)
713 const char path_prefix[] = "/sys/module/";
716 memcpy(path, path_prefix, sizeof (path_prefix) - 1);
717 strcpy(path + sizeof (path_prefix) - 1, module);
719 return (access(path, F_OK) == 0);
724 * Read lines from an open file descriptor and store them in an array of
725 * strings until EOF. lines[] will be allocated and populated with all the
726 * lines read. All newlines are replaced with NULL terminators for
727 * convenience. lines[] must be freed after use with libzfs_free_str_array().
729 * Returns the number of lines read.
732 libzfs_read_stdout_from_fd(int fd, char **lines[])
739 char **tmp_lines = NULL, **tmp;
743 fp = fdopen(fd, "r");
747 rc = getline(&line, &len, fp);
751 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
753 /* Return the lines we were able to process */
758 /* Terminate newlines */
759 if ((nl = strchr(line, '\n')) != NULL)
761 tmp_lines[lines_cnt] = line;
771 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
772 char **lines[], int *lines_cnt)
775 int error, devnull_fd;
779 * Setup a pipe between our child and parent process if we're
782 if ((lines != NULL) && pipe(link) == -1)
788 devnull_fd = open("/dev/null", O_WRONLY);
793 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
794 (void) dup2(devnull_fd, STDOUT_FILENO);
795 else if (lines != NULL) {
796 /* Save the output to lines[] */
797 dup2(link[1], STDOUT_FILENO);
802 if (!(flags & STDERR_VERBOSE))
803 (void) dup2(devnull_fd, STDERR_FILENO);
807 if (flags & NO_DEFAULT_PATH) {
811 execve(path, argv, env);
816 execvpe(path, argv, env);
820 } else if (pid > 0) {
824 while ((error = waitpid(pid, &status, 0)) == -1 &&
826 if (error < 0 || !WIFEXITED(status))
831 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
833 return (WEXITSTATUS(status));
840 libzfs_run_process(const char *path, char *argv[], int flags)
842 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
846 * Run a command and store its stdout lines in an array of strings (lines[]).
847 * lines[] is allocated and populated for you, and the number of lines is set in
848 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
849 * All newlines (\n) in lines[] are terminated for convenience.
852 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
853 char **lines[], int *lines_cnt)
855 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
859 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
860 * means that *path needs to be the full path to the executable.
863 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
864 char *env[], char **lines[], int *lines_cnt)
866 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
871 * Free an array of strings. Free both the strings contained in the array and
875 libzfs_free_str_array(char **strs, int count)
884 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
887 * Returns 0 otherwise.
890 libzfs_envvar_is_set(char *envvar)
892 char *env = getenv(envvar);
893 if (env && (strtoul(env, NULL, 0) > 0 ||
894 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
895 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
902 * Verify the required ZFS_DEV device is available and optionally attempt
903 * to load the ZFS modules. Under normal circumstances the modules
904 * should already have been loaded by some external mechanism.
906 * Environment variables:
907 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
908 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
911 libzfs_load_module(const char *module)
913 char *argv[4] = {"/sbin/modprobe", "-q", (char *)module, (char *)0};
914 char *load_str, *timeout_str;
915 long timeout = 10; /* seconds */
916 long busy_timeout = 10; /* milliseconds */
920 /* Optionally request module loading */
921 if (!libzfs_module_loaded(module)) {
922 load_str = getenv("ZFS_MODULE_LOADING");
924 if (!strncasecmp(load_str, "YES", strlen("YES")) ||
925 !strncasecmp(load_str, "ON", strlen("ON")))
932 if (libzfs_run_process("/sbin/modprobe", argv, 0))
936 if (!libzfs_module_loaded(module))
941 * Device creation by udev is asynchronous and waiting may be
942 * required. Busy wait for 10ms and then fall back to polling every
943 * 10ms for the allowed timeout (default 10s, max 10m). This is
944 * done to optimize for the common case where the device is
945 * immediately available and to avoid penalizing the possible
946 * case where udev is slow or unable to create the device.
948 timeout_str = getenv("ZFS_MODULE_TIMEOUT");
950 timeout = strtol(timeout_str, NULL, 0);
951 timeout = MAX(MIN(timeout, (10 * 60)), 0); /* 0 <= N <= 600 */
956 fd = open(ZFS_DEV, O_RDWR);
960 } else if (errno != ENOENT) {
962 } else if (NSEC2MSEC(gethrtime() - start) < busy_timeout) {
965 usleep(10 * MILLISEC);
967 } while (NSEC2MSEC(gethrtime() - start) < (timeout * MILLISEC));
975 libzfs_handle_t *hdl;
978 error = libzfs_load_module(ZFS_DRIVER);
984 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
988 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
993 #ifdef HAVE_SETMNTENT
994 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
996 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
998 (void) close(hdl->libzfs_fd);
1003 hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
1005 if (libzfs_core_init() != 0) {
1006 (void) close(hdl->libzfs_fd);
1007 (void) fclose(hdl->libzfs_mnttab);
1008 if (hdl->libzfs_sharetab)
1009 (void) fclose(hdl->libzfs_sharetab);
1016 zpool_feature_init();
1017 libzfs_mnttab_init(hdl);
1020 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1021 hdl->libzfs_prop_debug = B_TRUE;
1025 * For testing, remove some settable properties and features
1027 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1028 zprop_desc_t *proptbl;
1030 proptbl = zpool_prop_get_table();
1031 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1033 proptbl = zfs_prop_get_table();
1034 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1036 zfeature_info_t *ftbl = spa_feature_table;
1037 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1044 libzfs_fini(libzfs_handle_t *hdl)
1046 (void) close(hdl->libzfs_fd);
1047 if (hdl->libzfs_mnttab)
1048 #ifdef HAVE_SETMNTENT
1049 (void) endmntent(hdl->libzfs_mnttab);
1051 (void) fclose(hdl->libzfs_mnttab);
1053 if (hdl->libzfs_sharetab)
1054 (void) fclose(hdl->libzfs_sharetab);
1055 zfs_uninit_libshare(hdl);
1056 zpool_free_handles(hdl);
1057 namespace_clear(hdl);
1058 libzfs_mnttab_fini(hdl);
1065 zpool_get_handle(zpool_handle_t *zhp)
1067 return (zhp->zpool_hdl);
1071 zfs_get_handle(zfs_handle_t *zhp)
1073 return (zhp->zfs_hdl);
1077 zfs_get_pool_handle(const zfs_handle_t *zhp)
1079 return (zhp->zpool_hdl);
1083 * Given a name, determine whether or not it's a valid path
1084 * (starts with '/' or "./"). If so, walk the mnttab trying
1085 * to match the device number. If not, treat the path as an
1086 * fs/vol/snap/bkmark name.
1089 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
1091 struct stat64 statbuf;
1092 struct extmnttab entry;
1095 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1097 * It's not a valid path, assume it's a name of type 'argtype'.
1099 return (zfs_open(hdl, path, argtype));
1102 if (stat64(path, &statbuf) != 0) {
1103 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
1107 /* Reopen MNTTAB to prevent reading stale data from open file */
1108 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1111 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
1112 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
1121 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1122 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1127 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1131 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1135 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1139 zc->zc_nvlist_dst_size = len;
1141 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1142 if (zc->zc_nvlist_dst == 0)
1149 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1150 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1151 * filled in by the kernel to indicate the actual required size.
1154 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1156 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1158 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1159 if (zc->zc_nvlist_dst == 0)
1166 * Called to free the src and dst nvlists stored in the command structure.
1169 zcmd_free_nvlists(zfs_cmd_t *zc)
1171 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1172 free((void *)(uintptr_t)zc->zc_nvlist_src);
1173 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1174 zc->zc_nvlist_conf = 0;
1175 zc->zc_nvlist_src = 0;
1176 zc->zc_nvlist_dst = 0;
1180 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1186 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1188 if ((packed = zfs_alloc(hdl, len)) == NULL)
1191 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1193 *outnv = (uint64_t)(uintptr_t)packed;
1200 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1202 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1203 &zc->zc_nvlist_conf_size, nvl));
1207 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1209 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1210 &zc->zc_nvlist_src_size, nvl));
1214 * Unpacks an nvlist from the ZFS ioctl command structure.
1217 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1219 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1220 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1221 return (no_memory(hdl));
1227 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
1229 return (ioctl(hdl->libzfs_fd, request, zc));
1233 * ================================================================
1234 * API shared by zfs and zpool property management
1235 * ================================================================
1239 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1241 zprop_list_t *pl = cbp->cb_proplist;
1246 cbp->cb_first = B_FALSE;
1247 if (cbp->cb_scripted)
1251 * Start with the length of the column headers.
1253 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1254 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1256 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1258 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1260 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1263 /* first property is always NAME */
1264 assert(cbp->cb_proplist->pl_prop ==
1265 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1268 * Go through and calculate the widths for each column. For the
1269 * 'source' column, we kludge it up by taking the worst-case scenario of
1270 * inheriting from the longest name. This is acceptable because in the
1271 * majority of cases 'SOURCE' is the last column displayed, and we don't
1272 * use the width anyway. Note that the 'VALUE' column can be oversized,
1273 * if the name of the property is much longer than any values we find.
1275 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1279 if (pl->pl_prop != ZPROP_INVAL) {
1280 const char *propname = (type == ZFS_TYPE_POOL) ?
1281 zpool_prop_to_name(pl->pl_prop) :
1282 zfs_prop_to_name(pl->pl_prop);
1284 len = strlen(propname);
1285 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1286 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1288 len = strlen(pl->pl_user_prop);
1289 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1290 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1294 * 'VALUE' column. The first property is always the 'name'
1295 * property that was tacked on either by /sbin/zfs's
1296 * zfs_do_get() or when calling zprop_expand_list(), so we
1297 * ignore its width. If the user specified the name property
1298 * to display, then it will be later in the list in any case.
1300 if (pl != cbp->cb_proplist &&
1301 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1302 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1304 /* 'RECEIVED' column. */
1305 if (pl != cbp->cb_proplist &&
1306 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1307 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1310 * 'NAME' and 'SOURCE' columns
1312 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1314 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1315 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1316 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1317 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1322 * Now go through and print the headers.
1324 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1325 switch (cbp->cb_columns[i]) {
1327 title = dgettext(TEXT_DOMAIN, "NAME");
1329 case GET_COL_PROPERTY:
1330 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1333 title = dgettext(TEXT_DOMAIN, "VALUE");
1336 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1338 case GET_COL_SOURCE:
1339 title = dgettext(TEXT_DOMAIN, "SOURCE");
1345 if (title != NULL) {
1346 if (i == (ZFS_GET_NCOLS - 1) ||
1347 cbp->cb_columns[i + 1] == GET_COL_NONE)
1348 (void) printf("%s", title);
1350 (void) printf("%-*s ",
1351 cbp->cb_colwidths[cbp->cb_columns[i]],
1355 (void) printf("\n");
1359 * Display a single line of output, according to the settings in the callback
1363 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1364 const char *propname, const char *value, zprop_source_t sourcetype,
1365 const char *source, const char *recvd_value)
1368 const char *str = NULL;
1372 * Ignore those source types that the user has chosen to ignore.
1374 if ((sourcetype & cbp->cb_sources) == 0)
1378 zprop_print_headers(cbp, cbp->cb_type);
1380 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1381 switch (cbp->cb_columns[i]) {
1386 case GET_COL_PROPERTY:
1394 case GET_COL_SOURCE:
1395 switch (sourcetype) {
1396 case ZPROP_SRC_NONE:
1400 case ZPROP_SRC_DEFAULT:
1404 case ZPROP_SRC_LOCAL:
1408 case ZPROP_SRC_TEMPORARY:
1412 case ZPROP_SRC_INHERITED:
1413 (void) snprintf(buf, sizeof (buf),
1414 "inherited from %s", source);
1417 case ZPROP_SRC_RECEIVED:
1423 assert(!"unhandled zprop_source_t");
1428 str = (recvd_value == NULL ? "-" : recvd_value);
1435 if (i == (ZFS_GET_NCOLS - 1) ||
1436 cbp->cb_columns[i + 1] == GET_COL_NONE)
1437 (void) printf("%s", str);
1438 else if (cbp->cb_scripted)
1439 (void) printf("%s\t", str);
1441 (void) printf("%-*s ",
1442 cbp->cb_colwidths[cbp->cb_columns[i]],
1446 (void) printf("\n");
1450 * Given a numeric suffix, convert the value into a number of bits that the
1451 * resulting value must be shifted.
1454 str2shift(libzfs_handle_t *hdl, const char *buf)
1456 const char *ends = "BKMGTPEZ";
1461 for (i = 0; i < strlen(ends); i++) {
1462 if (toupper(buf[0]) == ends[i])
1465 if (i == strlen(ends)) {
1467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1468 "invalid numeric suffix '%s'"), buf);
1473 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1474 * However, 'BB' and 'BiB' are disallowed.
1476 if (buf[1] == '\0' ||
1477 (toupper(buf[0]) != 'B' &&
1478 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1479 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1485 "invalid numeric suffix '%s'"), buf);
1490 * Convert a string of the form '100G' into a real number. Used when setting
1491 * properties or creating a volume. 'buf' is used to place an extended error
1492 * message for the caller to use.
1495 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1502 /* Check to see if this looks like a number. */
1503 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506 "bad numeric value '%s'"), value);
1510 /* Rely on strtoull() to process the numeric portion. */
1512 *num = strtoull(value, &end, 10);
1515 * Check for ERANGE, which indicates that the value is too large to fit
1516 * in a 64-bit value.
1518 if (errno == ERANGE) {
1520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1521 "numeric value is too large"));
1526 * If we have a decimal value, then do the computation with floating
1527 * point arithmetic. Otherwise, use standard arithmetic.
1530 double fval = strtod(value, &end);
1532 if ((shift = str2shift(hdl, end)) == -1)
1535 fval *= pow(2, shift);
1537 if (fval > UINT64_MAX) {
1539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1540 "numeric value is too large"));
1544 *num = (uint64_t)fval;
1546 if ((shift = str2shift(hdl, end)) == -1)
1549 /* Check for overflow */
1550 if (shift >= 64 || (*num << shift) >> shift != *num) {
1552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1553 "numeric value is too large"));
1564 * Given a propname=value nvpair to set, parse any numeric properties
1565 * (index, boolean, etc) if they are specified as strings and add the
1566 * resulting nvpair to the returned nvlist.
1568 * At the DSL layer, all properties are either 64-bit numbers or strings.
1569 * We want the user to be able to ignore this fact and specify properties
1570 * as native values (numbers, for example) or as strings (to simplify
1571 * command line utilities). This also handles converting index types
1572 * (compression, checksum, etc) from strings to their on-disk index.
1575 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1576 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1579 data_type_t datatype = nvpair_type(elem);
1580 zprop_type_t proptype;
1581 const char *propname;
1583 boolean_t isnone = B_FALSE;
1584 boolean_t isauto = B_FALSE;
1587 if (type == ZFS_TYPE_POOL) {
1588 proptype = zpool_prop_get_type(prop);
1589 propname = zpool_prop_to_name(prop);
1591 proptype = zfs_prop_get_type(prop);
1592 propname = zfs_prop_to_name(prop);
1596 * Convert any properties to the internal DSL value types.
1602 case PROP_TYPE_STRING:
1603 if (datatype != DATA_TYPE_STRING) {
1604 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1605 "'%s' must be a string"), nvpair_name(elem));
1608 err = nvpair_value_string(elem, svalp);
1610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1611 "'%s' is invalid"), nvpair_name(elem));
1614 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1616 "'%s' is too long"), nvpair_name(elem));
1621 case PROP_TYPE_NUMBER:
1622 if (datatype == DATA_TYPE_STRING) {
1623 (void) nvpair_value_string(elem, &value);
1624 if (strcmp(value, "none") == 0) {
1626 } else if (strcmp(value, "auto") == 0) {
1628 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1631 } else if (datatype == DATA_TYPE_UINT64) {
1632 (void) nvpair_value_uint64(elem, ivalp);
1634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1635 "'%s' must be a number"), nvpair_name(elem));
1640 * Quota special: force 'none' and don't allow 0.
1642 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1643 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1645 "use 'none' to disable quota/refquota"));
1650 * Special handling for "*_limit=none". In this case it's not
1653 if ((type & ZFS_TYPE_DATASET) && isnone &&
1654 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1655 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1656 *ivalp = UINT64_MAX;
1660 * Special handling for setting 'refreservation' to 'auto'. Use
1661 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1662 * 'auto' is only allowed on volumes.
1666 case ZFS_PROP_REFRESERVATION:
1667 if ((type & ZFS_TYPE_VOLUME) == 0) {
1668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1669 "'%s=auto' only allowed on "
1670 "volumes"), nvpair_name(elem));
1673 *ivalp = UINT64_MAX;
1676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1677 "'auto' is invalid value for '%s'"),
1685 case PROP_TYPE_INDEX:
1686 if (datatype != DATA_TYPE_STRING) {
1687 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1688 "'%s' must be a string"), nvpair_name(elem));
1692 (void) nvpair_value_string(elem, &value);
1694 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1696 "'%s' must be one of '%s'"), propname,
1697 zprop_values(prop, type));
1707 * Add the result to our return set of properties.
1709 if (*svalp != NULL) {
1710 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1711 (void) no_memory(hdl);
1715 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1716 (void) no_memory(hdl);
1723 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1728 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1732 zprop_list_t *entry;
1734 prop = zprop_name_to_prop(propname, type);
1736 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1740 * When no property table entry can be found, return failure if
1741 * this is a pool property or if this isn't a user-defined
1744 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1745 !zpool_prop_feature(propname) &&
1746 !zpool_prop_unsupported(propname)) ||
1747 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1748 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1750 "invalid property '%s'"), propname);
1751 return (zfs_error(hdl, EZFS_BADPROP,
1752 dgettext(TEXT_DOMAIN, "bad property list")));
1755 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1758 entry->pl_prop = prop;
1759 if (prop == ZPROP_INVAL) {
1760 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1765 entry->pl_width = strlen(propname);
1767 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1777 * Given a comma-separated list of properties, construct a property list
1778 * containing both user-defined and native properties. This function will
1779 * return a NULL list if 'all' is specified, which can later be expanded
1780 * by zprop_expand_list().
1783 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1789 * If 'all' is specified, return a NULL list.
1791 if (strcmp(props, "all") == 0)
1795 * If no props were specified, return an error.
1797 if (props[0] == '\0') {
1798 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1799 "no properties specified"));
1800 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1801 "bad property list")));
1805 * It would be nice to use getsubopt() here, but the inclusion of column
1806 * aliases makes this more effort than it's worth.
1808 while (*props != '\0') {
1813 if ((p = strchr(props, ',')) == NULL) {
1814 len = strlen(props);
1821 * Check for empty options.
1824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825 "empty property name"));
1826 return (zfs_error(hdl, EZFS_BADPROP,
1827 dgettext(TEXT_DOMAIN, "bad property list")));
1831 * Check all regular property names.
1836 if (strcmp(props, "space") == 0) {
1837 static char *spaceprops[] = {
1838 "name", "avail", "used", "usedbysnapshots",
1839 "usedbydataset", "usedbyrefreservation",
1840 "usedbychildren", NULL
1844 for (i = 0; spaceprops[i]; i++) {
1845 if (addlist(hdl, spaceprops[i], listp, type))
1847 listp = &(*listp)->pl_next;
1850 if (addlist(hdl, props, listp, type))
1852 listp = &(*listp)->pl_next;
1864 zprop_free_list(zprop_list_t *pl)
1868 while (pl != NULL) {
1870 free(pl->pl_user_prop);
1876 typedef struct expand_data {
1877 zprop_list_t **last;
1878 libzfs_handle_t *hdl;
1883 zprop_expand_list_cb(int prop, void *cb)
1885 zprop_list_t *entry;
1886 expand_data_t *edp = cb;
1888 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1889 return (ZPROP_INVAL);
1891 entry->pl_prop = prop;
1892 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1893 entry->pl_all = B_TRUE;
1895 *(edp->last) = entry;
1896 edp->last = &entry->pl_next;
1898 return (ZPROP_CONT);
1902 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1904 zprop_list_t *entry;
1905 zprop_list_t **last;
1910 * If this is the very first time we've been called for an 'all'
1911 * specification, expand the list to include all native
1920 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1921 B_FALSE, type) == ZPROP_INVAL)
1925 * Add 'name' to the beginning of the list, which is handled
1928 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1931 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1933 entry->pl_width = zprop_width(entry->pl_prop,
1934 &entry->pl_fixed, type);
1935 entry->pl_all = B_TRUE;
1936 entry->pl_next = *plp;
1943 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1946 return (zprop_iter_common(func, cb, show_all, ordered, type));
1950 * Fill given version buffer with zfs userland version
1953 zfs_version_userland(char *version, int len)
1955 (void) strlcpy(version, ZFS_META_ALIAS, len);
1959 * Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
1960 * Returns 0 on success, and -1 on error (with errno set)
1963 zfs_version_kernel(char *version, int len)
1969 if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY)) == -1)
1972 if ((rlen = read(fd, version, len)) == -1) {
1980 version[rlen-1] = '\0'; /* discard '\n' */
1982 if (close(fd) == -1)
1989 * Prints both zfs userland and kernel versions
1990 * Returns 0 on success, and -1 on error (with errno set)
1993 zfs_version_print(void)
1995 char zver_userland[128];
1996 char zver_kernel[128];
1998 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
1999 fprintf(stderr, "zfs_version_kernel() failed: %s\n",
2004 zfs_version_userland(zver_userland, sizeof (zver_userland));
2006 (void) printf("%s\n", zver_userland);
2007 (void) printf("zfs-kmod-%s\n", zver_kernel);