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"));
306 case EZFS_EXPORT_IN_PROGRESS:
307 return (dgettext(TEXT_DOMAIN, "pool export in progress"));
309 return (dgettext(TEXT_DOMAIN, "unknown error"));
311 assert(hdl->libzfs_error == 0);
312 return (dgettext(TEXT_DOMAIN, "no error"));
318 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
324 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
326 hdl->libzfs_desc_active = 1;
332 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
334 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
336 hdl->libzfs_error = error;
338 if (hdl->libzfs_desc_active)
339 hdl->libzfs_desc_active = 0;
341 hdl->libzfs_desc[0] = '\0';
343 if (hdl->libzfs_printerr) {
344 if (error == EZFS_UNKNOWN) {
345 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
346 "error: %s\n"), libzfs_error_description(hdl));
350 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
351 libzfs_error_description(hdl));
352 if (error == EZFS_NOMEM)
358 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
360 return (zfs_error_fmt(hdl, error, "%s", msg));
365 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
371 zfs_verror(hdl, error, fmt, ap);
379 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
385 zfs_verror(hdl, EZFS_PERM, fmt, ap);
389 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
393 zfs_verror(hdl, EZFS_IO, fmt, ap);
397 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
401 zfs_verror(hdl, EZFS_INTR, fmt, ap);
409 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
411 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
416 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
422 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
431 zfs_verror(hdl, EZFS_IO, fmt, ap);
435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
436 "dataset does not exist"));
437 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
442 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
447 "dataset already exists"));
448 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
454 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
457 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
460 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
463 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467 "pool I/O is currently suspended"));
468 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
471 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
473 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE:
474 case ZFS_ERR_IOC_CMD_UNAVAIL:
475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
476 "module does not support this operation. A reboot may "
477 "be required to enable this operation."));
478 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
480 case ZFS_ERR_IOC_ARG_UNAVAIL:
481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
482 "module does not support an option for this operation. "
483 "A reboot may be required to enable this option."));
484 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
486 case ZFS_ERR_IOC_ARG_REQUIRED:
487 case ZFS_ERR_IOC_ARG_BADTYPE:
488 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
490 case ZFS_ERR_WRONG_PARENT:
491 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
494 zfs_error_aux(hdl, strerror(error));
495 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
504 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
506 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
511 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
517 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
524 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
529 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
530 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
535 "pool already exists"));
536 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
541 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
544 /* There is no pending operation to cancel */
546 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551 "one or more devices is currently unavailable"));
552 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
556 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
560 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
564 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
569 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
574 "pool I/O is currently suspended"));
575 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
579 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
583 "block size out of range or does not match"));
584 zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
587 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
589 case ZFS_ERR_CHECKPOINT_EXISTS:
590 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
592 case ZFS_ERR_DISCARDING_CHECKPOINT:
593 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
595 case ZFS_ERR_NO_CHECKPOINT:
596 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
598 case ZFS_ERR_DEVRM_IN_PROGRESS:
599 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
601 case ZFS_ERR_VDEV_TOO_BIG:
602 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
604 case ZFS_ERR_EXPORT_IN_PROGRESS:
605 zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap);
607 case ZFS_ERR_IOC_CMD_UNAVAIL:
608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
609 "module does not support this operation. A reboot may "
610 "be required to enable this operation."));
611 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
613 case ZFS_ERR_IOC_ARG_UNAVAIL:
614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
615 "module does not support an option for this operation. "
616 "A reboot may be required to enable this option."));
617 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
619 case ZFS_ERR_IOC_ARG_REQUIRED:
620 case ZFS_ERR_IOC_ARG_BADTYPE:
621 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
624 zfs_error_aux(hdl, strerror(error));
625 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
633 * Display an out of memory error message and abort the current program.
636 no_memory(libzfs_handle_t *hdl)
638 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
642 * A safe form of malloc() which will die if the allocation fails.
645 zfs_alloc(libzfs_handle_t *hdl, size_t size)
649 if ((data = calloc(1, size)) == NULL)
650 (void) no_memory(hdl);
656 * A safe form of asprintf() which will die if the allocation fails.
660 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
668 err = vasprintf(&ret, fmt, ap);
673 (void) no_memory(hdl);
679 * A safe form of realloc(), which also zeroes newly allocated space.
682 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
686 if ((ret = realloc(ptr, newsize)) == NULL) {
687 (void) no_memory(hdl);
691 bzero((char *)ret + oldsize, (newsize - oldsize));
696 * A safe form of strdup() which will die if the allocation fails.
699 zfs_strdup(libzfs_handle_t *hdl, const char *str)
703 if ((ret = strdup(str)) == NULL)
704 (void) no_memory(hdl);
710 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
712 hdl->libzfs_printerr = printerr;
716 libzfs_module_loaded(const char *module)
718 const char path_prefix[] = "/sys/module/";
721 memcpy(path, path_prefix, sizeof (path_prefix) - 1);
722 strcpy(path + sizeof (path_prefix) - 1, module);
724 return (access(path, F_OK) == 0);
729 * Read lines from an open file descriptor and store them in an array of
730 * strings until EOF. lines[] will be allocated and populated with all the
731 * lines read. All newlines are replaced with NULL terminators for
732 * convenience. lines[] must be freed after use with libzfs_free_str_array().
734 * Returns the number of lines read.
737 libzfs_read_stdout_from_fd(int fd, char **lines[])
744 char **tmp_lines = NULL, **tmp;
748 fp = fdopen(fd, "r");
752 rc = getline(&line, &len, fp);
756 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
758 /* Return the lines we were able to process */
763 /* Terminate newlines */
764 if ((nl = strchr(line, '\n')) != NULL)
766 tmp_lines[lines_cnt] = line;
776 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
777 char **lines[], int *lines_cnt)
780 int error, devnull_fd;
784 * Setup a pipe between our child and parent process if we're
787 if ((lines != NULL) && pipe(link) == -1)
793 devnull_fd = open("/dev/null", O_WRONLY);
798 if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
799 (void) dup2(devnull_fd, STDOUT_FILENO);
800 else if (lines != NULL) {
801 /* Save the output to lines[] */
802 dup2(link[1], STDOUT_FILENO);
807 if (!(flags & STDERR_VERBOSE))
808 (void) dup2(devnull_fd, STDERR_FILENO);
812 if (flags & NO_DEFAULT_PATH) {
816 execve(path, argv, env);
821 execvpe(path, argv, env);
825 } else if (pid > 0) {
829 while ((error = waitpid(pid, &status, 0)) == -1 &&
831 if (error < 0 || !WIFEXITED(status))
836 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
838 return (WEXITSTATUS(status));
845 libzfs_run_process(const char *path, char *argv[], int flags)
847 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
851 * Run a command and store its stdout lines in an array of strings (lines[]).
852 * lines[] is allocated and populated for you, and the number of lines is set in
853 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
854 * All newlines (\n) in lines[] are terminated for convenience.
857 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
858 char **lines[], int *lines_cnt)
860 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
864 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
865 * means that *path needs to be the full path to the executable.
868 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
869 char *env[], char **lines[], int *lines_cnt)
871 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
876 * Free an array of strings. Free both the strings contained in the array and
880 libzfs_free_str_array(char **strs, int count)
889 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
892 * Returns 0 otherwise.
895 libzfs_envvar_is_set(char *envvar)
897 char *env = getenv(envvar);
898 if (env && (strtoul(env, NULL, 0) > 0 ||
899 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
900 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)))
907 * Verify the required ZFS_DEV device is available and optionally attempt
908 * to load the ZFS modules. Under normal circumstances the modules
909 * should already have been loaded by some external mechanism.
911 * Environment variables:
912 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
913 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
916 libzfs_load_module(const char *module)
918 char *argv[4] = {"/sbin/modprobe", "-q", (char *)module, (char *)0};
919 char *load_str, *timeout_str;
920 long timeout = 10; /* seconds */
921 long busy_timeout = 10; /* milliseconds */
925 /* Optionally request module loading */
926 if (!libzfs_module_loaded(module)) {
927 load_str = getenv("ZFS_MODULE_LOADING");
929 if (!strncasecmp(load_str, "YES", strlen("YES")) ||
930 !strncasecmp(load_str, "ON", strlen("ON")))
937 if (libzfs_run_process("/sbin/modprobe", argv, 0))
941 if (!libzfs_module_loaded(module))
946 * Device creation by udev is asynchronous and waiting may be
947 * required. Busy wait for 10ms and then fall back to polling every
948 * 10ms for the allowed timeout (default 10s, max 10m). This is
949 * done to optimize for the common case where the device is
950 * immediately available and to avoid penalizing the possible
951 * case where udev is slow or unable to create the device.
953 timeout_str = getenv("ZFS_MODULE_TIMEOUT");
955 timeout = strtol(timeout_str, NULL, 0);
956 timeout = MAX(MIN(timeout, (10 * 60)), 0); /* 0 <= N <= 600 */
961 fd = open(ZFS_DEV, O_RDWR);
965 } else if (errno != ENOENT) {
967 } else if (NSEC2MSEC(gethrtime() - start) < busy_timeout) {
970 usleep(10 * MILLISEC);
972 } while (NSEC2MSEC(gethrtime() - start) < (timeout * MILLISEC));
980 libzfs_handle_t *hdl;
983 error = libzfs_load_module(ZFS_DRIVER);
989 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
993 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
998 #ifdef HAVE_SETMNTENT
999 if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
1001 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
1003 (void) close(hdl->libzfs_fd);
1008 hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
1010 if (libzfs_core_init() != 0) {
1011 (void) close(hdl->libzfs_fd);
1012 (void) fclose(hdl->libzfs_mnttab);
1013 if (hdl->libzfs_sharetab)
1014 (void) fclose(hdl->libzfs_sharetab);
1021 zpool_feature_init();
1022 libzfs_mnttab_init(hdl);
1025 if (getenv("ZFS_PROP_DEBUG") != NULL) {
1026 hdl->libzfs_prop_debug = B_TRUE;
1030 * For testing, remove some settable properties and features
1032 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1033 zprop_desc_t *proptbl;
1035 proptbl = zpool_prop_get_table();
1036 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1038 proptbl = zfs_prop_get_table();
1039 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1041 zfeature_info_t *ftbl = spa_feature_table;
1042 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1049 libzfs_fini(libzfs_handle_t *hdl)
1051 (void) close(hdl->libzfs_fd);
1052 if (hdl->libzfs_mnttab)
1053 #ifdef HAVE_SETMNTENT
1054 (void) endmntent(hdl->libzfs_mnttab);
1056 (void) fclose(hdl->libzfs_mnttab);
1058 if (hdl->libzfs_sharetab)
1059 (void) fclose(hdl->libzfs_sharetab);
1060 zfs_uninit_libshare(hdl);
1061 zpool_free_handles(hdl);
1062 namespace_clear(hdl);
1063 libzfs_mnttab_fini(hdl);
1070 zpool_get_handle(zpool_handle_t *zhp)
1072 return (zhp->zpool_hdl);
1076 zfs_get_handle(zfs_handle_t *zhp)
1078 return (zhp->zfs_hdl);
1082 zfs_get_pool_handle(const zfs_handle_t *zhp)
1084 return (zhp->zpool_hdl);
1088 * Given a name, determine whether or not it's a valid path
1089 * (starts with '/' or "./"). If so, walk the mnttab trying
1090 * to match the device number. If not, treat the path as an
1091 * fs/vol/snap/bkmark name.
1094 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
1096 struct stat64 statbuf;
1097 struct extmnttab entry;
1100 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1102 * It's not a valid path, assume it's a name of type 'argtype'.
1104 return (zfs_open(hdl, path, argtype));
1107 if (stat64(path, &statbuf) != 0) {
1108 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
1112 /* Reopen MNTTAB to prevent reading stale data from open file */
1113 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1116 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
1117 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
1126 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1127 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1132 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1136 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1140 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1144 zc->zc_nvlist_dst_size = len;
1146 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1147 if (zc->zc_nvlist_dst == 0)
1154 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1155 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1156 * filled in by the kernel to indicate the actual required size.
1159 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1161 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1163 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1164 if (zc->zc_nvlist_dst == 0)
1171 * Called to free the src and dst nvlists stored in the command structure.
1174 zcmd_free_nvlists(zfs_cmd_t *zc)
1176 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1177 free((void *)(uintptr_t)zc->zc_nvlist_src);
1178 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1179 zc->zc_nvlist_conf = 0;
1180 zc->zc_nvlist_src = 0;
1181 zc->zc_nvlist_dst = 0;
1185 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1191 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1193 if ((packed = zfs_alloc(hdl, len)) == NULL)
1196 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1198 *outnv = (uint64_t)(uintptr_t)packed;
1205 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1207 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1208 &zc->zc_nvlist_conf_size, nvl));
1212 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1214 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1215 &zc->zc_nvlist_src_size, nvl));
1219 * Unpacks an nvlist from the ZFS ioctl command structure.
1222 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1224 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1225 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1226 return (no_memory(hdl));
1232 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
1234 return (ioctl(hdl->libzfs_fd, request, zc));
1238 * ================================================================
1239 * API shared by zfs and zpool property management
1240 * ================================================================
1244 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1246 zprop_list_t *pl = cbp->cb_proplist;
1251 cbp->cb_first = B_FALSE;
1252 if (cbp->cb_scripted)
1256 * Start with the length of the column headers.
1258 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1259 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1261 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1263 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1265 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1268 /* first property is always NAME */
1269 assert(cbp->cb_proplist->pl_prop ==
1270 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1273 * Go through and calculate the widths for each column. For the
1274 * 'source' column, we kludge it up by taking the worst-case scenario of
1275 * inheriting from the longest name. This is acceptable because in the
1276 * majority of cases 'SOURCE' is the last column displayed, and we don't
1277 * use the width anyway. Note that the 'VALUE' column can be oversized,
1278 * if the name of the property is much longer than any values we find.
1280 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1284 if (pl->pl_prop != ZPROP_INVAL) {
1285 const char *propname = (type == ZFS_TYPE_POOL) ?
1286 zpool_prop_to_name(pl->pl_prop) :
1287 zfs_prop_to_name(pl->pl_prop);
1289 len = strlen(propname);
1290 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1291 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1293 len = strlen(pl->pl_user_prop);
1294 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1295 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1299 * 'VALUE' column. The first property is always the 'name'
1300 * property that was tacked on either by /sbin/zfs's
1301 * zfs_do_get() or when calling zprop_expand_list(), so we
1302 * ignore its width. If the user specified the name property
1303 * to display, then it will be later in the list in any case.
1305 if (pl != cbp->cb_proplist &&
1306 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1307 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1309 /* 'RECEIVED' column. */
1310 if (pl != cbp->cb_proplist &&
1311 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1312 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1315 * 'NAME' and 'SOURCE' columns
1317 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1319 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1320 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1321 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1322 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1327 * Now go through and print the headers.
1329 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1330 switch (cbp->cb_columns[i]) {
1332 title = dgettext(TEXT_DOMAIN, "NAME");
1334 case GET_COL_PROPERTY:
1335 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1338 title = dgettext(TEXT_DOMAIN, "VALUE");
1341 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1343 case GET_COL_SOURCE:
1344 title = dgettext(TEXT_DOMAIN, "SOURCE");
1350 if (title != NULL) {
1351 if (i == (ZFS_GET_NCOLS - 1) ||
1352 cbp->cb_columns[i + 1] == GET_COL_NONE)
1353 (void) printf("%s", title);
1355 (void) printf("%-*s ",
1356 cbp->cb_colwidths[cbp->cb_columns[i]],
1360 (void) printf("\n");
1364 * Display a single line of output, according to the settings in the callback
1368 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1369 const char *propname, const char *value, zprop_source_t sourcetype,
1370 const char *source, const char *recvd_value)
1373 const char *str = NULL;
1377 * Ignore those source types that the user has chosen to ignore.
1379 if ((sourcetype & cbp->cb_sources) == 0)
1383 zprop_print_headers(cbp, cbp->cb_type);
1385 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1386 switch (cbp->cb_columns[i]) {
1391 case GET_COL_PROPERTY:
1399 case GET_COL_SOURCE:
1400 switch (sourcetype) {
1401 case ZPROP_SRC_NONE:
1405 case ZPROP_SRC_DEFAULT:
1409 case ZPROP_SRC_LOCAL:
1413 case ZPROP_SRC_TEMPORARY:
1417 case ZPROP_SRC_INHERITED:
1418 (void) snprintf(buf, sizeof (buf),
1419 "inherited from %s", source);
1422 case ZPROP_SRC_RECEIVED:
1428 assert(!"unhandled zprop_source_t");
1433 str = (recvd_value == NULL ? "-" : recvd_value);
1440 if (i == (ZFS_GET_NCOLS - 1) ||
1441 cbp->cb_columns[i + 1] == GET_COL_NONE)
1442 (void) printf("%s", str);
1443 else if (cbp->cb_scripted)
1444 (void) printf("%s\t", str);
1446 (void) printf("%-*s ",
1447 cbp->cb_colwidths[cbp->cb_columns[i]],
1451 (void) printf("\n");
1455 * Given a numeric suffix, convert the value into a number of bits that the
1456 * resulting value must be shifted.
1459 str2shift(libzfs_handle_t *hdl, const char *buf)
1461 const char *ends = "BKMGTPEZ";
1466 for (i = 0; i < strlen(ends); i++) {
1467 if (toupper(buf[0]) == ends[i])
1470 if (i == strlen(ends)) {
1472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1473 "invalid numeric suffix '%s'"), buf);
1478 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1479 * However, 'BB' and 'BiB' are disallowed.
1481 if (buf[1] == '\0' ||
1482 (toupper(buf[0]) != 'B' &&
1483 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1484 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1490 "invalid numeric suffix '%s'"), buf);
1495 * Convert a string of the form '100G' into a real number. Used when setting
1496 * properties or creating a volume. 'buf' is used to place an extended error
1497 * message for the caller to use.
1500 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1507 /* Check to see if this looks like a number. */
1508 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511 "bad numeric value '%s'"), value);
1515 /* Rely on strtoull() to process the numeric portion. */
1517 *num = strtoull(value, &end, 10);
1520 * Check for ERANGE, which indicates that the value is too large to fit
1521 * in a 64-bit value.
1523 if (errno == ERANGE) {
1525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1526 "numeric value is too large"));
1531 * If we have a decimal value, then do the computation with floating
1532 * point arithmetic. Otherwise, use standard arithmetic.
1535 double fval = strtod(value, &end);
1537 if ((shift = str2shift(hdl, end)) == -1)
1540 fval *= pow(2, shift);
1542 if (fval > UINT64_MAX) {
1544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1545 "numeric value is too large"));
1549 *num = (uint64_t)fval;
1551 if ((shift = str2shift(hdl, end)) == -1)
1554 /* Check for overflow */
1555 if (shift >= 64 || (*num << shift) >> shift != *num) {
1557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1558 "numeric value is too large"));
1569 * Given a propname=value nvpair to set, parse any numeric properties
1570 * (index, boolean, etc) if they are specified as strings and add the
1571 * resulting nvpair to the returned nvlist.
1573 * At the DSL layer, all properties are either 64-bit numbers or strings.
1574 * We want the user to be able to ignore this fact and specify properties
1575 * as native values (numbers, for example) or as strings (to simplify
1576 * command line utilities). This also handles converting index types
1577 * (compression, checksum, etc) from strings to their on-disk index.
1580 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1581 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1584 data_type_t datatype = nvpair_type(elem);
1585 zprop_type_t proptype;
1586 const char *propname;
1588 boolean_t isnone = B_FALSE;
1589 boolean_t isauto = B_FALSE;
1592 if (type == ZFS_TYPE_POOL) {
1593 proptype = zpool_prop_get_type(prop);
1594 propname = zpool_prop_to_name(prop);
1596 proptype = zfs_prop_get_type(prop);
1597 propname = zfs_prop_to_name(prop);
1601 * Convert any properties to the internal DSL value types.
1607 case PROP_TYPE_STRING:
1608 if (datatype != DATA_TYPE_STRING) {
1609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1610 "'%s' must be a string"), nvpair_name(elem));
1613 err = nvpair_value_string(elem, svalp);
1615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1616 "'%s' is invalid"), nvpair_name(elem));
1619 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1621 "'%s' is too long"), nvpair_name(elem));
1626 case PROP_TYPE_NUMBER:
1627 if (datatype == DATA_TYPE_STRING) {
1628 (void) nvpair_value_string(elem, &value);
1629 if (strcmp(value, "none") == 0) {
1631 } else if (strcmp(value, "auto") == 0) {
1633 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1636 } else if (datatype == DATA_TYPE_UINT64) {
1637 (void) nvpair_value_uint64(elem, ivalp);
1639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1640 "'%s' must be a number"), nvpair_name(elem));
1645 * Quota special: force 'none' and don't allow 0.
1647 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1648 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1650 "use 'none' to disable quota/refquota"));
1655 * Special handling for "*_limit=none". In this case it's not
1658 if ((type & ZFS_TYPE_DATASET) && isnone &&
1659 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1660 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1661 *ivalp = UINT64_MAX;
1665 * Special handling for setting 'refreservation' to 'auto'. Use
1666 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1667 * 'auto' is only allowed on volumes.
1671 case ZFS_PROP_REFRESERVATION:
1672 if ((type & ZFS_TYPE_VOLUME) == 0) {
1673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1674 "'%s=auto' only allowed on "
1675 "volumes"), nvpair_name(elem));
1678 *ivalp = UINT64_MAX;
1681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1682 "'auto' is invalid value for '%s'"),
1690 case PROP_TYPE_INDEX:
1691 if (datatype != DATA_TYPE_STRING) {
1692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1693 "'%s' must be a string"), nvpair_name(elem));
1697 (void) nvpair_value_string(elem, &value);
1699 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1701 "'%s' must be one of '%s'"), propname,
1702 zprop_values(prop, type));
1712 * Add the result to our return set of properties.
1714 if (*svalp != NULL) {
1715 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1716 (void) no_memory(hdl);
1720 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1721 (void) no_memory(hdl);
1728 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1733 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1737 zprop_list_t *entry;
1739 prop = zprop_name_to_prop(propname, type);
1741 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1745 * When no property table entry can be found, return failure if
1746 * this is a pool property or if this isn't a user-defined
1749 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1750 !zpool_prop_feature(propname) &&
1751 !zpool_prop_unsupported(propname)) ||
1752 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1753 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1755 "invalid property '%s'"), propname);
1756 return (zfs_error(hdl, EZFS_BADPROP,
1757 dgettext(TEXT_DOMAIN, "bad property list")));
1760 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1763 entry->pl_prop = prop;
1764 if (prop == ZPROP_INVAL) {
1765 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1770 entry->pl_width = strlen(propname);
1772 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1782 * Given a comma-separated list of properties, construct a property list
1783 * containing both user-defined and native properties. This function will
1784 * return a NULL list if 'all' is specified, which can later be expanded
1785 * by zprop_expand_list().
1788 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1794 * If 'all' is specified, return a NULL list.
1796 if (strcmp(props, "all") == 0)
1800 * If no props were specified, return an error.
1802 if (props[0] == '\0') {
1803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1804 "no properties specified"));
1805 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1806 "bad property list")));
1810 * It would be nice to use getsubopt() here, but the inclusion of column
1811 * aliases makes this more effort than it's worth.
1813 while (*props != '\0') {
1818 if ((p = strchr(props, ',')) == NULL) {
1819 len = strlen(props);
1826 * Check for empty options.
1829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1830 "empty property name"));
1831 return (zfs_error(hdl, EZFS_BADPROP,
1832 dgettext(TEXT_DOMAIN, "bad property list")));
1836 * Check all regular property names.
1841 if (strcmp(props, "space") == 0) {
1842 static char *spaceprops[] = {
1843 "name", "avail", "used", "usedbysnapshots",
1844 "usedbydataset", "usedbyrefreservation",
1845 "usedbychildren", NULL
1849 for (i = 0; spaceprops[i]; i++) {
1850 if (addlist(hdl, spaceprops[i], listp, type))
1852 listp = &(*listp)->pl_next;
1855 if (addlist(hdl, props, listp, type))
1857 listp = &(*listp)->pl_next;
1869 zprop_free_list(zprop_list_t *pl)
1873 while (pl != NULL) {
1875 free(pl->pl_user_prop);
1881 typedef struct expand_data {
1882 zprop_list_t **last;
1883 libzfs_handle_t *hdl;
1888 zprop_expand_list_cb(int prop, void *cb)
1890 zprop_list_t *entry;
1891 expand_data_t *edp = cb;
1893 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1894 return (ZPROP_INVAL);
1896 entry->pl_prop = prop;
1897 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1898 entry->pl_all = B_TRUE;
1900 *(edp->last) = entry;
1901 edp->last = &entry->pl_next;
1903 return (ZPROP_CONT);
1907 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1909 zprop_list_t *entry;
1910 zprop_list_t **last;
1915 * If this is the very first time we've been called for an 'all'
1916 * specification, expand the list to include all native
1925 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1926 B_FALSE, type) == ZPROP_INVAL)
1930 * Add 'name' to the beginning of the list, which is handled
1933 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1936 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1938 entry->pl_width = zprop_width(entry->pl_prop,
1939 &entry->pl_fixed, type);
1940 entry->pl_all = B_TRUE;
1941 entry->pl_next = *plp;
1948 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1951 return (zprop_iter_common(func, cb, show_all, ordered, type));
1955 * Fill given version buffer with zfs userland version
1958 zfs_version_userland(char *version, int len)
1960 (void) strlcpy(version, ZFS_META_ALIAS, len);
1964 * Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
1965 * Returns 0 on success, and -1 on error (with errno set)
1968 zfs_version_kernel(char *version, int len)
1974 if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY)) == -1)
1977 if ((rlen = read(fd, version, len)) == -1) {
1985 version[rlen-1] = '\0'; /* discard '\n' */
1987 if (close(fd) == -1)
1994 * Prints both zfs userland and kernel versions
1995 * Returns 0 on success, and -1 on error (with errno set)
1998 zfs_version_print(void)
2000 char zver_userland[128];
2001 char zver_kernel[128];
2003 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
2004 fprintf(stderr, "zfs_version_kernel() failed: %s\n",
2009 zfs_version_userland(zver_userland, sizeof (zver_userland));
2011 (void) printf("%s\n", zver_userland);
2012 (void) printf("zfs-kmod-%s\n", zver_kernel);