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_IOC_CMD_UNAVAIL:
472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
473 "module does not support this operation. A reboot may "
474 "be required to enable this operation."));
475 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
477 case ZFS_ERR_IOC_ARG_UNAVAIL:
478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
479 "module does not support an option for this operation. "
480 "A reboot may be required to enable this option."));
481 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
483 case ZFS_ERR_IOC_ARG_REQUIRED:
484 case ZFS_ERR_IOC_ARG_BADTYPE:
485 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
487 case ZFS_ERR_WRONG_PARENT:
488 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
491 zfs_error_aux(hdl, strerror(error));
492 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
501 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
503 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
508 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
514 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
521 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
526 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
527 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
532 "pool already exists"));
533 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
538 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
541 /* There is no pending operation to cancel */
543 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548 "one or more devices is currently unavailable"));
549 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
553 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
557 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
561 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
566 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
571 "pool I/O is currently suspended"));
572 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
576 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
580 "block size out of range or does not match"));
581 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
584 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
586 case ZFS_ERR_CHECKPOINT_EXISTS:
587 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
589 case ZFS_ERR_DISCARDING_CHECKPOINT:
590 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
592 case ZFS_ERR_NO_CHECKPOINT:
593 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
595 case ZFS_ERR_DEVRM_IN_PROGRESS:
596 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
598 case ZFS_ERR_VDEV_TOO_BIG:
599 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
601 case ZFS_ERR_IOC_CMD_UNAVAIL:
602 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
603 "module does not support this operation. A reboot may "
604 "be required to enable this operation."));
605 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
607 case ZFS_ERR_IOC_ARG_UNAVAIL:
608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
609 "module does not support an option for this operation. "
610 "A reboot may be required to enable this option."));
611 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
613 case ZFS_ERR_IOC_ARG_REQUIRED:
614 case ZFS_ERR_IOC_ARG_BADTYPE:
615 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
618 zfs_error_aux(hdl, strerror(error));
619 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
627 * Display an out of memory error message and abort the current program.
630 no_memory(libzfs_handle_t *hdl)
632 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
636 * A safe form of malloc() which will die if the allocation fails.
639 zfs_alloc(libzfs_handle_t *hdl, size_t size)
643 if ((data = calloc(1, size)) == NULL)
644 (void) no_memory(hdl);
650 * A safe form of asprintf() which will die if the allocation fails.
654 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
662 err = vasprintf(&ret, fmt, ap);
667 (void) no_memory(hdl);
673 * A safe form of realloc(), which also zeroes newly allocated space.
676 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
680 if ((ret = realloc(ptr, newsize)) == NULL) {
681 (void) no_memory(hdl);
685 bzero((char *)ret + oldsize, (newsize - oldsize));
690 * A safe form of strdup() which will die if the allocation fails.
693 zfs_strdup(libzfs_handle_t *hdl, const char *str)
697 if ((ret = strdup(str)) == NULL)
698 (void) no_memory(hdl);
704 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
706 hdl->libzfs_printerr = printerr;
710 libzfs_module_loaded(const char *module)
712 const char path_prefix[] = "/sys/module/";
715 memcpy(path, path_prefix, sizeof (path_prefix) - 1);
716 strcpy(path + sizeof (path_prefix) - 1, module);
718 return (access(path, F_OK) == 0);
723 * Read lines from an open file descriptor and store them in an array of
724 * strings until EOF. lines[] will be allocated and populated with all the
725 * lines read. All newlines are replaced with NULL terminators for
726 * convenience. lines[] must be freed after use with libzfs_free_str_array().
728 * Returns the number of lines read.
731 libzfs_read_stdout_from_fd(int fd, char **lines[])
738 char **tmp_lines = NULL, **tmp;
742 fp = fdopen(fd, "r");
746 rc = getline(&line, &len, fp);
750 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
752 /* Return the lines we were able to process */
757 /* Terminate newlines */
758 if ((nl = strchr(line, '\n')) != NULL)
760 tmp_lines[lines_cnt] = line;
770 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
771 char **lines[], int *lines_cnt)
774 int error, devnull_fd;
778 * Setup a pipe between our child and parent process if we're
781 if ((lines != NULL) && pipe(link) == -1)
787 devnull_fd = open("/dev/null", O_WRONLY);
792 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
793 (void) dup2(devnull_fd, STDOUT_FILENO);
794 else if (lines != NULL) {
795 /* Save the output to lines[] */
796 dup2(link[1], STDOUT_FILENO);
801 if (!(flags & STDERR_VERBOSE))
802 (void) dup2(devnull_fd, STDERR_FILENO);
806 if (flags & NO_DEFAULT_PATH) {
810 execve(path, argv, env);
815 execvpe(path, argv, env);
819 } else if (pid > 0) {
823 while ((error = waitpid(pid, &status, 0)) == -1 &&
825 if (error < 0 || !WIFEXITED(status))
830 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
832 return (WEXITSTATUS(status));
839 libzfs_run_process(const char *path, char *argv[], int flags)
841 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
845 * Run a command and store its stdout lines in an array of strings (lines[]).
846 * lines[] is allocated and populated for you, and the number of lines is set in
847 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
848 * All newlines (\n) in lines[] are terminated for convenience.
851 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
852 char **lines[], int *lines_cnt)
854 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
858 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
859 * means that *path needs to be the full path to the executable.
862 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
863 char *env[], char **lines[], int *lines_cnt)
865 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
870 * Free an array of strings. Free both the strings contained in the array and
874 libzfs_free_str_array(char **strs, int count)
883 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
886 * Returns 0 otherwise.
889 libzfs_envvar_is_set(char *envvar)
891 char *env = getenv(envvar);
892 if (env && (strtoul(env, NULL, 0) > 0 ||
893 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
894 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
901 * Verify the required ZFS_DEV device is available and optionally attempt
902 * to load the ZFS modules. Under normal circumstances the modules
903 * should already have been loaded by some external mechanism.
905 * Environment variables:
906 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
907 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
910 libzfs_load_module(const char *module)
912 char *argv[4] = {"/sbin/modprobe", "-q", (char *)module, (char *)0};
913 char *load_str, *timeout_str;
914 long timeout = 10; /* seconds */
915 long busy_timeout = 10; /* milliseconds */
919 /* Optionally request module loading */
920 if (!libzfs_module_loaded(module)) {
921 load_str = getenv("ZFS_MODULE_LOADING");
923 if (!strncasecmp(load_str, "YES", strlen("YES")) ||
924 !strncasecmp(load_str, "ON", strlen("ON")))
931 if (libzfs_run_process("/sbin/modprobe", argv, 0))
935 if (!libzfs_module_loaded(module))
940 * Device creation by udev is asynchronous and waiting may be
941 * required. Busy wait for 10ms and then fall back to polling every
942 * 10ms for the allowed timeout (default 10s, max 10m). This is
943 * done to optimize for the common case where the device is
944 * immediately available and to avoid penalizing the possible
945 * case where udev is slow or unable to create the device.
947 timeout_str = getenv("ZFS_MODULE_TIMEOUT");
949 timeout = strtol(timeout_str, NULL, 0);
950 timeout = MAX(MIN(timeout, (10 * 60)), 0); /* 0 <= N <= 600 */
955 fd = open(ZFS_DEV, O_RDWR);
959 } else if (errno != ENOENT) {
961 } else if (NSEC2MSEC(gethrtime() - start) < busy_timeout) {
964 usleep(10 * MILLISEC);
966 } while (NSEC2MSEC(gethrtime() - start) < (timeout * MILLISEC));
974 libzfs_handle_t *hdl;
977 error = libzfs_load_module(ZFS_DRIVER);
983 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
987 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
992 #ifdef HAVE_SETMNTENT
993 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
995 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
997 (void) close(hdl->libzfs_fd);
1002 hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
1004 if (libzfs_core_init() != 0) {
1005 (void) close(hdl->libzfs_fd);
1006 (void) fclose(hdl->libzfs_mnttab);
1007 if (hdl->libzfs_sharetab)
1008 (void) fclose(hdl->libzfs_sharetab);
1015 zpool_feature_init();
1016 libzfs_mnttab_init(hdl);
1019 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1020 hdl->libzfs_prop_debug = B_TRUE;
1024 * For testing, remove some settable properties and features
1026 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1027 zprop_desc_t *proptbl;
1029 proptbl = zpool_prop_get_table();
1030 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1032 proptbl = zfs_prop_get_table();
1033 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1035 zfeature_info_t *ftbl = spa_feature_table;
1036 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1043 libzfs_fini(libzfs_handle_t *hdl)
1045 (void) close(hdl->libzfs_fd);
1046 if (hdl->libzfs_mnttab)
1047 #ifdef HAVE_SETMNTENT
1048 (void) endmntent(hdl->libzfs_mnttab);
1050 (void) fclose(hdl->libzfs_mnttab);
1052 if (hdl->libzfs_sharetab)
1053 (void) fclose(hdl->libzfs_sharetab);
1054 zfs_uninit_libshare(hdl);
1055 zpool_free_handles(hdl);
1056 namespace_clear(hdl);
1057 libzfs_mnttab_fini(hdl);
1064 zpool_get_handle(zpool_handle_t *zhp)
1066 return (zhp->zpool_hdl);
1070 zfs_get_handle(zfs_handle_t *zhp)
1072 return (zhp->zfs_hdl);
1076 zfs_get_pool_handle(const zfs_handle_t *zhp)
1078 return (zhp->zpool_hdl);
1082 * Given a name, determine whether or not it's a valid path
1083 * (starts with '/' or "./"). If so, walk the mnttab trying
1084 * to match the device number. If not, treat the path as an
1085 * fs/vol/snap/bkmark name.
1088 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
1090 struct stat64 statbuf;
1091 struct extmnttab entry;
1094 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1096 * It's not a valid path, assume it's a name of type 'argtype'.
1098 return (zfs_open(hdl, path, argtype));
1101 if (stat64(path, &statbuf) != 0) {
1102 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
1106 /* Reopen MNTTAB to prevent reading stale data from open file */
1107 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1110 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
1111 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
1120 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1121 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1126 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1130 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1134 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1138 zc->zc_nvlist_dst_size = len;
1140 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1141 if (zc->zc_nvlist_dst == 0)
1148 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1149 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1150 * filled in by the kernel to indicate the actual required size.
1153 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1155 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1157 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1158 if (zc->zc_nvlist_dst == 0)
1165 * Called to free the src and dst nvlists stored in the command structure.
1168 zcmd_free_nvlists(zfs_cmd_t *zc)
1170 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1171 free((void *)(uintptr_t)zc->zc_nvlist_src);
1172 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1173 zc->zc_nvlist_conf = 0;
1174 zc->zc_nvlist_src = 0;
1175 zc->zc_nvlist_dst = 0;
1179 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1185 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1187 if ((packed = zfs_alloc(hdl, len)) == NULL)
1190 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1192 *outnv = (uint64_t)(uintptr_t)packed;
1199 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1201 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1202 &zc->zc_nvlist_conf_size, nvl));
1206 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1208 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1209 &zc->zc_nvlist_src_size, nvl));
1213 * Unpacks an nvlist from the ZFS ioctl command structure.
1216 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1218 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1219 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1220 return (no_memory(hdl));
1226 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
1228 return (ioctl(hdl->libzfs_fd, request, zc));
1232 * ================================================================
1233 * API shared by zfs and zpool property management
1234 * ================================================================
1238 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1240 zprop_list_t *pl = cbp->cb_proplist;
1245 cbp->cb_first = B_FALSE;
1246 if (cbp->cb_scripted)
1250 * Start with the length of the column headers.
1252 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1253 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1255 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1257 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1259 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1262 /* first property is always NAME */
1263 assert(cbp->cb_proplist->pl_prop ==
1264 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1267 * Go through and calculate the widths for each column. For the
1268 * 'source' column, we kludge it up by taking the worst-case scenario of
1269 * inheriting from the longest name. This is acceptable because in the
1270 * majority of cases 'SOURCE' is the last column displayed, and we don't
1271 * use the width anyway. Note that the 'VALUE' column can be oversized,
1272 * if the name of the property is much longer than any values we find.
1274 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1278 if (pl->pl_prop != ZPROP_INVAL) {
1279 const char *propname = (type == ZFS_TYPE_POOL) ?
1280 zpool_prop_to_name(pl->pl_prop) :
1281 zfs_prop_to_name(pl->pl_prop);
1283 len = strlen(propname);
1284 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1285 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1287 len = strlen(pl->pl_user_prop);
1288 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1289 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1293 * 'VALUE' column. The first property is always the 'name'
1294 * property that was tacked on either by /sbin/zfs's
1295 * zfs_do_get() or when calling zprop_expand_list(), so we
1296 * ignore its width. If the user specified the name property
1297 * to display, then it will be later in the list in any case.
1299 if (pl != cbp->cb_proplist &&
1300 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1301 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1303 /* 'RECEIVED' column. */
1304 if (pl != cbp->cb_proplist &&
1305 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1306 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1309 * 'NAME' and 'SOURCE' columns
1311 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1313 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1314 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1315 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1316 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1321 * Now go through and print the headers.
1323 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1324 switch (cbp->cb_columns[i]) {
1326 title = dgettext(TEXT_DOMAIN, "NAME");
1328 case GET_COL_PROPERTY:
1329 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1332 title = dgettext(TEXT_DOMAIN, "VALUE");
1335 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1337 case GET_COL_SOURCE:
1338 title = dgettext(TEXT_DOMAIN, "SOURCE");
1344 if (title != NULL) {
1345 if (i == (ZFS_GET_NCOLS - 1) ||
1346 cbp->cb_columns[i + 1] == GET_COL_NONE)
1347 (void) printf("%s", title);
1349 (void) printf("%-*s ",
1350 cbp->cb_colwidths[cbp->cb_columns[i]],
1354 (void) printf("\n");
1358 * Display a single line of output, according to the settings in the callback
1362 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1363 const char *propname, const char *value, zprop_source_t sourcetype,
1364 const char *source, const char *recvd_value)
1367 const char *str = NULL;
1371 * Ignore those source types that the user has chosen to ignore.
1373 if ((sourcetype & cbp->cb_sources) == 0)
1377 zprop_print_headers(cbp, cbp->cb_type);
1379 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1380 switch (cbp->cb_columns[i]) {
1385 case GET_COL_PROPERTY:
1393 case GET_COL_SOURCE:
1394 switch (sourcetype) {
1395 case ZPROP_SRC_NONE:
1399 case ZPROP_SRC_DEFAULT:
1403 case ZPROP_SRC_LOCAL:
1407 case ZPROP_SRC_TEMPORARY:
1411 case ZPROP_SRC_INHERITED:
1412 (void) snprintf(buf, sizeof (buf),
1413 "inherited from %s", source);
1416 case ZPROP_SRC_RECEIVED:
1422 assert(!"unhandled zprop_source_t");
1427 str = (recvd_value == NULL ? "-" : recvd_value);
1434 if (i == (ZFS_GET_NCOLS - 1) ||
1435 cbp->cb_columns[i + 1] == GET_COL_NONE)
1436 (void) printf("%s", str);
1437 else if (cbp->cb_scripted)
1438 (void) printf("%s\t", str);
1440 (void) printf("%-*s ",
1441 cbp->cb_colwidths[cbp->cb_columns[i]],
1445 (void) printf("\n");
1449 * Given a numeric suffix, convert the value into a number of bits that the
1450 * resulting value must be shifted.
1453 str2shift(libzfs_handle_t *hdl, const char *buf)
1455 const char *ends = "BKMGTPEZ";
1460 for (i = 0; i < strlen(ends); i++) {
1461 if (toupper(buf[0]) == ends[i])
1464 if (i == strlen(ends)) {
1466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1467 "invalid numeric suffix '%s'"), buf);
1472 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1473 * However, 'BB' and 'BiB' are disallowed.
1475 if (buf[1] == '\0' ||
1476 (toupper(buf[0]) != 'B' &&
1477 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1478 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1484 "invalid numeric suffix '%s'"), buf);
1489 * Convert a string of the form '100G' into a real number. Used when setting
1490 * properties or creating a volume. 'buf' is used to place an extended error
1491 * message for the caller to use.
1494 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1501 /* Check to see if this looks like a number. */
1502 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1504 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505 "bad numeric value '%s'"), value);
1509 /* Rely on strtoull() to process the numeric portion. */
1511 *num = strtoull(value, &end, 10);
1514 * Check for ERANGE, which indicates that the value is too large to fit
1515 * in a 64-bit value.
1517 if (errno == ERANGE) {
1519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1520 "numeric value is too large"));
1525 * If we have a decimal value, then do the computation with floating
1526 * point arithmetic. Otherwise, use standard arithmetic.
1529 double fval = strtod(value, &end);
1531 if ((shift = str2shift(hdl, end)) == -1)
1534 fval *= pow(2, shift);
1536 if (fval > UINT64_MAX) {
1538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1539 "numeric value is too large"));
1543 *num = (uint64_t)fval;
1545 if ((shift = str2shift(hdl, end)) == -1)
1548 /* Check for overflow */
1549 if (shift >= 64 || (*num << shift) >> shift != *num) {
1551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1552 "numeric value is too large"));
1563 * Given a propname=value nvpair to set, parse any numeric properties
1564 * (index, boolean, etc) if they are specified as strings and add the
1565 * resulting nvpair to the returned nvlist.
1567 * At the DSL layer, all properties are either 64-bit numbers or strings.
1568 * We want the user to be able to ignore this fact and specify properties
1569 * as native values (numbers, for example) or as strings (to simplify
1570 * command line utilities). This also handles converting index types
1571 * (compression, checksum, etc) from strings to their on-disk index.
1574 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1575 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1578 data_type_t datatype = nvpair_type(elem);
1579 zprop_type_t proptype;
1580 const char *propname;
1582 boolean_t isnone = B_FALSE;
1583 boolean_t isauto = B_FALSE;
1586 if (type == ZFS_TYPE_POOL) {
1587 proptype = zpool_prop_get_type(prop);
1588 propname = zpool_prop_to_name(prop);
1590 proptype = zfs_prop_get_type(prop);
1591 propname = zfs_prop_to_name(prop);
1595 * Convert any properties to the internal DSL value types.
1601 case PROP_TYPE_STRING:
1602 if (datatype != DATA_TYPE_STRING) {
1603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1604 "'%s' must be a string"), nvpair_name(elem));
1607 err = nvpair_value_string(elem, svalp);
1609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1610 "'%s' is invalid"), nvpair_name(elem));
1613 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1615 "'%s' is too long"), nvpair_name(elem));
1620 case PROP_TYPE_NUMBER:
1621 if (datatype == DATA_TYPE_STRING) {
1622 (void) nvpair_value_string(elem, &value);
1623 if (strcmp(value, "none") == 0) {
1625 } else if (strcmp(value, "auto") == 0) {
1627 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1630 } else if (datatype == DATA_TYPE_UINT64) {
1631 (void) nvpair_value_uint64(elem, ivalp);
1633 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1634 "'%s' must be a number"), nvpair_name(elem));
1639 * Quota special: force 'none' and don't allow 0.
1641 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1642 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1644 "use 'none' to disable quota/refquota"));
1649 * Special handling for "*_limit=none". In this case it's not
1652 if ((type & ZFS_TYPE_DATASET) && isnone &&
1653 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1654 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1655 *ivalp = UINT64_MAX;
1659 * Special handling for setting 'refreservation' to 'auto'. Use
1660 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1661 * 'auto' is only allowed on volumes.
1665 case ZFS_PROP_REFRESERVATION:
1666 if ((type & ZFS_TYPE_VOLUME) == 0) {
1667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1668 "'%s=auto' only allowed on "
1669 "volumes"), nvpair_name(elem));
1672 *ivalp = UINT64_MAX;
1675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1676 "'auto' is invalid value for '%s'"),
1684 case PROP_TYPE_INDEX:
1685 if (datatype != DATA_TYPE_STRING) {
1686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1687 "'%s' must be a string"), nvpair_name(elem));
1691 (void) nvpair_value_string(elem, &value);
1693 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1695 "'%s' must be one of '%s'"), propname,
1696 zprop_values(prop, type));
1706 * Add the result to our return set of properties.
1708 if (*svalp != NULL) {
1709 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1710 (void) no_memory(hdl);
1714 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1715 (void) no_memory(hdl);
1722 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1727 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1731 zprop_list_t *entry;
1733 prop = zprop_name_to_prop(propname, type);
1735 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1739 * When no property table entry can be found, return failure if
1740 * this is a pool property or if this isn't a user-defined
1743 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1744 !zpool_prop_feature(propname) &&
1745 !zpool_prop_unsupported(propname)) ||
1746 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1747 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1749 "invalid property '%s'"), propname);
1750 return (zfs_error(hdl, EZFS_BADPROP,
1751 dgettext(TEXT_DOMAIN, "bad property list")));
1754 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1757 entry->pl_prop = prop;
1758 if (prop == ZPROP_INVAL) {
1759 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1764 entry->pl_width = strlen(propname);
1766 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1776 * Given a comma-separated list of properties, construct a property list
1777 * containing both user-defined and native properties. This function will
1778 * return a NULL list if 'all' is specified, which can later be expanded
1779 * by zprop_expand_list().
1782 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1788 * If 'all' is specified, return a NULL list.
1790 if (strcmp(props, "all") == 0)
1794 * If no props were specified, return an error.
1796 if (props[0] == '\0') {
1797 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1798 "no properties specified"));
1799 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1800 "bad property list")));
1804 * It would be nice to use getsubopt() here, but the inclusion of column
1805 * aliases makes this more effort than it's worth.
1807 while (*props != '\0') {
1812 if ((p = strchr(props, ',')) == NULL) {
1813 len = strlen(props);
1820 * Check for empty options.
1823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1824 "empty property name"));
1825 return (zfs_error(hdl, EZFS_BADPROP,
1826 dgettext(TEXT_DOMAIN, "bad property list")));
1830 * Check all regular property names.
1835 if (strcmp(props, "space") == 0) {
1836 static char *spaceprops[] = {
1837 "name", "avail", "used", "usedbysnapshots",
1838 "usedbydataset", "usedbyrefreservation",
1839 "usedbychildren", NULL
1843 for (i = 0; spaceprops[i]; i++) {
1844 if (addlist(hdl, spaceprops[i], listp, type))
1846 listp = &(*listp)->pl_next;
1849 if (addlist(hdl, props, listp, type))
1851 listp = &(*listp)->pl_next;
1863 zprop_free_list(zprop_list_t *pl)
1867 while (pl != NULL) {
1869 free(pl->pl_user_prop);
1875 typedef struct expand_data {
1876 zprop_list_t **last;
1877 libzfs_handle_t *hdl;
1882 zprop_expand_list_cb(int prop, void *cb)
1884 zprop_list_t *entry;
1885 expand_data_t *edp = cb;
1887 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1888 return (ZPROP_INVAL);
1890 entry->pl_prop = prop;
1891 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1892 entry->pl_all = B_TRUE;
1894 *(edp->last) = entry;
1895 edp->last = &entry->pl_next;
1897 return (ZPROP_CONT);
1901 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1903 zprop_list_t *entry;
1904 zprop_list_t **last;
1909 * If this is the very first time we've been called for an 'all'
1910 * specification, expand the list to include all native
1919 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1920 B_FALSE, type) == ZPROP_INVAL)
1924 * Add 'name' to the beginning of the list, which is handled
1927 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1930 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1932 entry->pl_width = zprop_width(entry->pl_prop,
1933 &entry->pl_fixed, type);
1934 entry->pl_all = B_TRUE;
1935 entry->pl_next = *plp;
1942 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1945 return (zprop_iter_common(func, cb, show_all, ordered, type));
1949 * Fill given version buffer with zfs userland version
1952 zfs_version_userland(char *version, int len)
1954 (void) strlcpy(version, ZFS_META_ALIAS, len);
1958 * Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
1959 * Returns 0 on success, and -1 on error (with errno set)
1962 zfs_version_kernel(char *version, int len)
1968 if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY)) == -1)
1971 if ((rlen = read(fd, version, len)) == -1) {
1979 version[rlen-1] = '\0'; /* discard '\n' */
1981 if (close(fd) == -1)
1988 * Prints both zfs userland and kernel versions
1989 * Returns 0 on success, and -1 on error (with errno set)
1992 zfs_version_print(void)
1994 char zver_userland[128];
1995 char zver_kernel[128];
1997 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
1998 fprintf(stderr, "zfs_version_kernel() failed: %s\n",
2003 zfs_version_userland(zver_userland, sizeof (zver_userland));
2005 (void) printf("%s\n", zver_userland);
2006 (void) printf("zfs-kmod-%s\n", zver_kernel);