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) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
30 * 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>
58 libzfs_errno(libzfs_handle_t
*hdl
)
60 return (hdl
->libzfs_error
);
64 libzfs_error_init(int error
)
68 return (dgettext(TEXT_DOMAIN
, "The ZFS modules are not "
69 "loaded.\nTry running '/sbin/modprobe zfs' as root "
72 return (dgettext(TEXT_DOMAIN
, "/dev/zfs and /proc/self/mounts "
73 "are required.\nTry running 'udevadm trigger' and 'mount "
74 "-t proc proc /proc' as root.\n"));
76 return (dgettext(TEXT_DOMAIN
, "The ZFS modules cannot be "
77 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
78 "root to manually load them.\n"));
80 return (dgettext(TEXT_DOMAIN
, "Permission denied the "
81 "ZFS utilities must be run as root.\n"));
83 return (dgettext(TEXT_DOMAIN
, "Failed to initialize the "
84 "libzfs library.\n"));
89 libzfs_error_action(libzfs_handle_t
*hdl
)
91 return (hdl
->libzfs_action
);
95 libzfs_error_description(libzfs_handle_t
*hdl
)
97 if (hdl
->libzfs_desc
[0] != '\0')
98 return (hdl
->libzfs_desc
);
100 switch (hdl
->libzfs_error
) {
102 return (dgettext(TEXT_DOMAIN
, "out of memory"));
104 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
105 case EZFS_PROPREADONLY
:
106 return (dgettext(TEXT_DOMAIN
, "read-only property"));
108 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
109 "datasets of this type"));
110 case EZFS_PROPNONINHERIT
:
111 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
113 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
115 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
116 "datasets of this type"));
118 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
120 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
122 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
124 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
125 case EZFS_DSREADONLY
:
126 return (dgettext(TEXT_DOMAIN
, "dataset is read-only"));
128 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
130 case EZFS_INVALIDNAME
:
131 return (dgettext(TEXT_DOMAIN
, "invalid name"));
132 case EZFS_BADRESTORE
:
133 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
136 return (dgettext(TEXT_DOMAIN
, "backup failed"));
138 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
140 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
142 return (dgettext(TEXT_DOMAIN
, "invalid device"));
143 case EZFS_NOREPLICAS
:
144 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
145 case EZFS_RESILVERING
:
146 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
147 case EZFS_BADVERSION
:
148 return (dgettext(TEXT_DOMAIN
, "unsupported version or "
150 case EZFS_POOLUNAVAIL
:
151 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
152 case EZFS_DEVOVERFLOW
:
153 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
155 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
156 case EZFS_CROSSTARGET
:
157 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
160 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
161 case EZFS_MOUNTFAILED
:
162 return (dgettext(TEXT_DOMAIN
, "mount failed"));
163 case EZFS_UMOUNTFAILED
:
164 return (dgettext(TEXT_DOMAIN
, "umount failed"));
165 case EZFS_UNSHARENFSFAILED
:
166 return (dgettext(TEXT_DOMAIN
, "unshare(1M) failed"));
167 case EZFS_SHARENFSFAILED
:
168 return (dgettext(TEXT_DOMAIN
, "share(1M) failed"));
169 case EZFS_UNSHARESMBFAILED
:
170 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
171 case EZFS_SHARESMBFAILED
:
172 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
174 return (dgettext(TEXT_DOMAIN
, "permission denied"));
176 return (dgettext(TEXT_DOMAIN
, "out of space"));
178 return (dgettext(TEXT_DOMAIN
, "bad address"));
180 return (dgettext(TEXT_DOMAIN
, "I/O error"));
182 return (dgettext(TEXT_DOMAIN
, "signal received"));
184 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
186 case EZFS_INVALCONFIG
:
187 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
189 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
191 return (dgettext(TEXT_DOMAIN
, "no history available"));
193 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
195 case EZFS_POOL_NOTSUP
:
196 return (dgettext(TEXT_DOMAIN
, "operation not supported "
197 "on this type of pool"));
198 case EZFS_POOL_INVALARG
:
199 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
200 "this pool operation"));
201 case EZFS_NAMETOOLONG
:
202 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
203 case EZFS_OPENFAILED
:
204 return (dgettext(TEXT_DOMAIN
, "open failed"));
206 return (dgettext(TEXT_DOMAIN
,
207 "disk capacity information could not be retrieved"));
208 case EZFS_LABELFAILED
:
209 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
211 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
213 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
214 case EZFS_BADPERMSET
:
215 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
216 case EZFS_NODELEGATION
:
217 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
218 "disabled on pool"));
220 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
222 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
223 case EZFS_VDEVNOTSUP
:
224 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
227 return (dgettext(TEXT_DOMAIN
, "operation not supported "
229 case EZFS_ACTIVE_SPARE
:
230 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
232 case EZFS_UNPLAYED_LOGS
:
233 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
235 case EZFS_REFTAG_RELE
:
236 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
237 case EZFS_REFTAG_HOLD
:
238 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
240 case EZFS_TAGTOOLONG
:
241 return (dgettext(TEXT_DOMAIN
, "tag too long"));
242 case EZFS_PIPEFAILED
:
243 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
244 case EZFS_THREADCREATEFAILED
:
245 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
246 case EZFS_POSTSPLIT_ONLINE
:
247 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
250 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
251 "use 'zpool scrub -s' to cancel current scrub"));
253 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
255 return (dgettext(TEXT_DOMAIN
, "unable to generate diffs"));
257 return (dgettext(TEXT_DOMAIN
, "invalid diff data"));
258 case EZFS_POOLREADONLY
:
259 return (dgettext(TEXT_DOMAIN
, "pool is read-only"));
261 return (dgettext(TEXT_DOMAIN
, "unknown error"));
263 assert(hdl
->libzfs_error
== 0);
264 return (dgettext(TEXT_DOMAIN
, "no error"));
270 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
276 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
278 hdl
->libzfs_desc_active
= 1;
284 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
286 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
288 hdl
->libzfs_error
= error
;
290 if (hdl
->libzfs_desc_active
)
291 hdl
->libzfs_desc_active
= 0;
293 hdl
->libzfs_desc
[0] = '\0';
295 if (hdl
->libzfs_printerr
) {
296 if (error
== EZFS_UNKNOWN
) {
297 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
298 "error: %s\n"), libzfs_error_description(hdl
));
302 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
303 libzfs_error_description(hdl
));
304 if (error
== EZFS_NOMEM
)
310 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
312 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
317 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
323 zfs_verror(hdl
, error
, fmt
, ap
);
331 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
337 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
341 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
345 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
349 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
353 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
361 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
363 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
368 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
374 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
383 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
387 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
388 "dataset does not exist"));
389 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
394 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
398 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
399 "dataset already exists"));
400 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
406 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
409 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
412 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
415 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
418 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
419 "pool I/O is currently suspended"));
420 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
423 zfs_error_aux(hdl
, strerror(error
));
424 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
433 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
435 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
440 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
446 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
453 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
458 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
459 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
463 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
464 "pool already exists"));
465 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
469 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
470 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
474 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
475 "one or more devices is currently unavailable"));
476 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
480 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
484 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
488 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
493 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
498 "pool I/O is currently suspended"));
499 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
503 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
506 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
507 "block size out of range or does not match"));
508 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
512 zfs_error_aux(hdl
, strerror(error
));
513 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
521 * Display an out of memory error message and abort the current program.
524 no_memory(libzfs_handle_t
*hdl
)
526 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
530 * A safe form of malloc() which will die if the allocation fails.
533 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
537 if ((data
= calloc(1, size
)) == NULL
)
538 (void) no_memory(hdl
);
544 * A safe form of asprintf() which will die if the allocation fails.
548 zfs_asprintf(libzfs_handle_t
*hdl
, const char *fmt
, ...)
556 err
= vasprintf(&ret
, fmt
, ap
);
561 (void) no_memory(hdl
);
567 * A safe form of realloc(), which also zeroes newly allocated space.
570 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
574 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
575 (void) no_memory(hdl
);
579 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
584 * A safe form of strdup() which will die if the allocation fails.
587 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
591 if ((ret
= strdup(str
)) == NULL
)
592 (void) no_memory(hdl
);
598 * Convert a number to an appropriately human-readable output.
601 zfs_nicenum_format(uint64_t num
, char *buf
, size_t buflen
,
602 enum zfs_nicenum_format format
)
607 const char *units
[3][7] = {
608 [ZFS_NICENUM_1024
] = {"", "K", "M", "G", "T", "P", "E"},
609 [ZFS_NICENUM_TIME
] = {"ns", "us", "ms", "s", "?", "?", "?"}
612 const int units_len
[] = {[ZFS_NICENUM_1024
] = 6,
613 [ZFS_NICENUM_TIME
] = 4};
615 const int k_unit
[] = { [ZFS_NICENUM_1024
] = 1024,
616 [ZFS_NICENUM_TIME
] = 1000};
620 if (format
== ZFS_NICENUM_RAW
) {
621 snprintf(buf
, buflen
, "%llu", (u_longlong_t
)num
);
626 while (n
>= k_unit
[format
] && index
< units_len
[format
]) {
631 u
= units
[format
][index
];
633 /* Don't print 0ns times */
634 if ((format
== ZFS_NICENUM_TIME
) && (num
== 0)) {
635 (void) snprintf(buf
, buflen
, "-");
636 } else if ((index
== 0) || ((num
%
637 (uint64_t)powl(k_unit
[format
], index
)) == 0)) {
639 * If this is an even multiple of the base, always display
640 * without any decimal precision.
642 (void) snprintf(buf
, buflen
, "%llu%s", (u_longlong_t
)n
, u
);
646 * We want to choose a precision that reflects the best choice
647 * for fitting in 5 characters. This can get rather tricky when
648 * we have numbers that are very close to an order of magnitude.
649 * For example, when displaying 10239 (which is really 9.999K),
650 * we want only a single place of precision for 10.0K. We could
651 * develop some complex heuristics for this, but it's much
652 * easier just to try each combination in turn.
655 for (i
= 2; i
>= 0; i
--) {
657 (uint64_t)powl(k_unit
[format
], index
);
660 * Don't print floating point values for time. Note,
661 * we use floor() instead of round() here, since
662 * round can result in undesirable results. For
663 * example, if "num" is in the range of
664 * 999500-999999, it will print out "1000us". This
665 * doesn't happen if we use floor().
667 if (format
== ZFS_NICENUM_TIME
) {
668 if (snprintf(buf
, buflen
, "%d%s",
669 (unsigned int) floor(val
), u
) <= 5)
673 if (snprintf(buf
, buflen
, "%.*f%s", i
,
682 * Convert a number to an appropriately human-readable output.
685 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
687 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_1024
);
691 * Convert a time to an appropriately human-readable output.
692 * @num: Time in nanoseconds
695 zfs_nicetime(uint64_t num
, char *buf
, size_t buflen
)
697 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_TIME
);
701 * Print out a raw number with correct column spacing
704 zfs_niceraw(uint64_t num
, char *buf
, size_t buflen
)
706 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_RAW
);
712 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
714 hdl
->libzfs_printerr
= printerr
;
718 libzfs_module_loaded(const char *module
)
720 const char path_prefix
[] = "/sys/module/";
723 memcpy(path
, path_prefix
, sizeof (path_prefix
) - 1);
724 strcpy(path
+ sizeof (path_prefix
) - 1, module
);
726 return (access(path
, F_OK
) == 0);
731 * Read lines from an open file descriptor and store them in an array of
732 * strings until EOF. lines[] will be allocated and populated with all the
733 * lines read. All newlines are replaced with NULL terminators for
734 * convenience. lines[] must be freed after use with libzfs_free_str_array().
736 * Returns the number of lines read.
739 libzfs_read_stdout_from_fd(int fd
, char **lines
[])
746 char **tmp_lines
= NULL
, **tmp
;
750 fp
= fdopen(fd
, "r");
754 rc
= getline(&line
, &len
, fp
);
758 tmp
= realloc(tmp_lines
, sizeof (*tmp_lines
) * (lines_cnt
+ 1));
760 /* Return the lines we were able to process */
765 /* Terminate newlines */
766 if ((nl
= strchr(line
, '\n')) != NULL
)
768 tmp_lines
[lines_cnt
] = line
;
778 libzfs_run_process_impl(const char *path
, char *argv
[], char *env
[], int flags
,
779 char **lines
[], int *lines_cnt
)
782 int error
, devnull_fd
;
786 * Setup a pipe between our child and parent process if we're
789 if ((lines
!= NULL
) && pipe(link
) == -1)
795 devnull_fd
= open("/dev/null", O_WRONLY
);
800 if (!(flags
& STDOUT_VERBOSE
) && (lines
== NULL
))
801 (void) dup2(devnull_fd
, STDOUT_FILENO
);
802 else if (lines
!= NULL
) {
803 /* Save the output to lines[] */
804 dup2(link
[1], STDOUT_FILENO
);
809 if (!(flags
& STDERR_VERBOSE
))
810 (void) dup2(devnull_fd
, STDERR_FILENO
);
814 if (flags
& NO_DEFAULT_PATH
) {
818 execve(path
, argv
, env
);
823 execvpe(path
, argv
, env
);
827 } else if (pid
> 0) {
831 while ((error
= waitpid(pid
, &status
, 0)) == -1 &&
833 if (error
< 0 || !WIFEXITED(status
))
838 *lines_cnt
= libzfs_read_stdout_from_fd(link
[0], lines
);
840 return (WEXITSTATUS(status
));
847 libzfs_run_process(const char *path
, char *argv
[], int flags
)
849 return (libzfs_run_process_impl(path
, argv
, NULL
, flags
, NULL
, NULL
));
853 * Run a command and store its stdout lines in an array of strings (lines[]).
854 * lines[] is allocated and populated for you, and the number of lines is set in
855 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
856 * All newlines (\n) in lines[] are terminated for convenience.
859 libzfs_run_process_get_stdout(const char *path
, char *argv
[], char *env
[],
860 char **lines
[], int *lines_cnt
)
862 return (libzfs_run_process_impl(path
, argv
, env
, 0, lines
, lines_cnt
));
866 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
867 * means that *path needs to be the full path to the executable.
870 libzfs_run_process_get_stdout_nopath(const char *path
, char *argv
[],
871 char *env
[], char **lines
[], int *lines_cnt
)
873 return (libzfs_run_process_impl(path
, argv
, env
, NO_DEFAULT_PATH
,
878 * Free an array of strings. Free both the strings contained in the array and
882 libzfs_free_str_array(char **strs
, int count
)
891 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
894 * Returns 0 otherwise.
897 libzfs_envvar_is_set(char *envvar
)
899 char *env
= getenv(envvar
);
900 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
901 (!strncasecmp(env
, "YES", 3) && strnlen(env
, 4) == 3) ||
902 (!strncasecmp(env
, "ON", 2) && strnlen(env
, 3) == 2)))
909 * Verify the required ZFS_DEV device is available and optionally attempt
910 * to load the ZFS modules. Under normal circumstances the modules
911 * should already have been loaded by some external mechanism.
913 * Environment variables:
914 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
915 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
918 libzfs_load_module(const char *module
)
920 char *argv
[4] = {"/sbin/modprobe", "-q", (char *)module
, (char *)0};
921 char *load_str
, *timeout_str
;
922 long timeout
= 10; /* seconds */
923 long busy_timeout
= 10; /* milliseconds */
927 /* Optionally request module loading */
928 if (!libzfs_module_loaded(module
)) {
929 load_str
= getenv("ZFS_MODULE_LOADING");
931 if (!strncasecmp(load_str
, "YES", strlen("YES")) ||
932 !strncasecmp(load_str
, "ON", strlen("ON")))
938 if (load
&& libzfs_run_process("/sbin/modprobe", argv
, 0))
942 /* Module loading is synchronous it must be available */
943 if (!libzfs_module_loaded(module
))
947 * Device creation by udev is asynchronous and waiting may be
948 * required. Busy wait for 10ms and then fall back to polling every
949 * 10ms for the allowed timeout (default 10s, max 10m). This is
950 * done to optimize for the common case where the device is
951 * immediately available and to avoid penalizing the possible
952 * case where udev is slow or unable to create the device.
954 timeout_str
= getenv("ZFS_MODULE_TIMEOUT");
956 timeout
= strtol(timeout_str
, NULL
, 0);
957 timeout
= MAX(MIN(timeout
, (10 * 60)), 0); /* 0 <= N <= 600 */
962 fd
= open(ZFS_DEV
, O_RDWR
);
966 } else if (errno
!= ENOENT
) {
968 } else if (NSEC2MSEC(gethrtime() - start
) < busy_timeout
) {
971 usleep(10 * MILLISEC
);
973 } while (NSEC2MSEC(gethrtime() - start
) < (timeout
* MILLISEC
));
981 libzfs_handle_t
*hdl
;
984 error
= libzfs_load_module(ZFS_DRIVER
);
990 if ((hdl
= calloc(1, sizeof (libzfs_handle_t
))) == NULL
) {
994 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
999 #ifdef HAVE_SETMNTENT
1000 if ((hdl
->libzfs_mnttab
= setmntent(MNTTAB
, "r")) == NULL
) {
1002 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "r")) == NULL
) {
1004 (void) close(hdl
->libzfs_fd
);
1009 hdl
->libzfs_sharetab
= fopen(ZFS_SHARETAB
, "r");
1011 if (libzfs_core_init() != 0) {
1012 (void) close(hdl
->libzfs_fd
);
1013 (void) fclose(hdl
->libzfs_mnttab
);
1014 if (hdl
->libzfs_sharetab
)
1015 (void) fclose(hdl
->libzfs_sharetab
);
1022 zpool_feature_init();
1023 libzfs_mnttab_init(hdl
);
1030 libzfs_fini(libzfs_handle_t
*hdl
)
1032 (void) close(hdl
->libzfs_fd
);
1033 if (hdl
->libzfs_mnttab
)
1034 #ifdef HAVE_SETMNTENT
1035 (void) endmntent(hdl
->libzfs_mnttab
);
1037 (void) fclose(hdl
->libzfs_mnttab
);
1039 if (hdl
->libzfs_sharetab
)
1040 (void) fclose(hdl
->libzfs_sharetab
);
1041 zfs_uninit_libshare(hdl
);
1042 zpool_free_handles(hdl
);
1043 libzfs_fru_clear(hdl
, B_TRUE
);
1044 namespace_clear(hdl
);
1045 libzfs_mnttab_fini(hdl
);
1052 zpool_get_handle(zpool_handle_t
*zhp
)
1054 return (zhp
->zpool_hdl
);
1058 zfs_get_handle(zfs_handle_t
*zhp
)
1060 return (zhp
->zfs_hdl
);
1064 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
1066 return (zhp
->zpool_hdl
);
1070 * Given a name, determine whether or not it's a valid path
1071 * (starts with '/' or "./"). If so, walk the mnttab trying
1072 * to match the device number. If not, treat the path as an
1073 * fs/vol/snap/bkmark name.
1076 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
1078 struct stat64 statbuf
;
1079 struct extmnttab entry
;
1082 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
1084 * It's not a valid path, assume it's a name of type 'argtype'.
1086 return (zfs_open(hdl
, path
, argtype
));
1089 if (stat64(path
, &statbuf
) != 0) {
1090 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
1094 /* Reopen MNTTAB to prevent reading stale data from open file */
1095 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
1098 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
1099 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
1108 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
1109 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
1114 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
1118 * Append partition suffix to an otherwise fully qualified device path.
1119 * This is used to generate the name the full path as its stored in
1120 * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length
1121 * of 'path' will be returned on error a negative value is returned.
1124 zfs_append_partition(char *path
, size_t max_len
)
1126 int len
= strlen(path
);
1128 if ((strncmp(path
, UDISK_ROOT
, strlen(UDISK_ROOT
)) == 0) ||
1129 (strncmp(path
, ZVOL_ROOT
, strlen(ZVOL_ROOT
)) == 0)) {
1130 if (len
+ 6 >= max_len
)
1133 (void) strcat(path
, "-part1");
1136 if (len
+ 2 >= max_len
)
1139 if (isdigit(path
[len
-1])) {
1140 (void) strcat(path
, "p1");
1143 (void) strcat(path
, "1");
1152 * Given a shorthand device name check if a file by that name exists in any
1153 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
1154 * one is found, store its fully qualified path in the 'path' buffer passed
1155 * by the caller and return 0, otherwise return an error.
1158 zfs_resolve_shortname(const char *name
, char *path
, size_t len
)
1161 char *dir
, *env
, *envdup
;
1163 env
= getenv("ZPOOL_IMPORT_PATH");
1167 envdup
= strdup(env
);
1168 dir
= strtok(envdup
, ":");
1169 while (dir
&& error
) {
1170 (void) snprintf(path
, len
, "%s/%s", dir
, name
);
1171 error
= access(path
, F_OK
);
1172 dir
= strtok(NULL
, ":");
1176 for (i
= 0; i
< DEFAULT_IMPORT_PATH_SIZE
&& error
< 0; i
++) {
1177 (void) snprintf(path
, len
, "%s/%s",
1178 zpool_default_import_path
[i
], name
);
1179 error
= access(path
, F_OK
);
1183 return (error
? ENOENT
: 0);
1187 * Given a shorthand device name look for a match against 'cmp_name'. This
1188 * is done by checking all prefix expansions using either the default
1189 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
1190 * variable. Proper partition suffixes will be appended if this is a
1191 * whole disk. When a match is found 0 is returned otherwise ENOENT.
1194 zfs_strcmp_shortname(char *name
, char *cmp_name
, int wholedisk
)
1196 int path_len
, cmp_len
, i
= 0, error
= ENOENT
;
1197 char *dir
, *env
, *envdup
= NULL
;
1198 char path_name
[MAXPATHLEN
];
1200 cmp_len
= strlen(cmp_name
);
1201 env
= getenv("ZPOOL_IMPORT_PATH");
1204 envdup
= strdup(env
);
1205 dir
= strtok(envdup
, ":");
1207 dir
= zpool_default_import_path
[i
];
1211 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
1212 while (dir
[strlen(dir
)-1] == '/')
1213 dir
[strlen(dir
)-1] = '\0';
1215 path_len
= snprintf(path_name
, MAXPATHLEN
, "%s/%s", dir
, name
);
1217 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1219 if ((path_len
== cmp_len
) && strcmp(path_name
, cmp_name
) == 0) {
1225 dir
= strtok(NULL
, ":");
1226 } else if (++i
< DEFAULT_IMPORT_PATH_SIZE
) {
1227 dir
= zpool_default_import_path
[i
];
1240 * Given either a shorthand or fully qualified path name look for a match
1241 * against 'cmp'. The passed name will be expanded as needed for comparison
1242 * purposes and redundant slashes stripped to ensure an accurate match.
1245 zfs_strcmp_pathname(char *name
, char *cmp
, int wholedisk
)
1247 int path_len
, cmp_len
;
1248 char path_name
[MAXPATHLEN
];
1249 char cmp_name
[MAXPATHLEN
];
1252 /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
1253 memset(cmp_name
, 0, MAXPATHLEN
);
1255 dir
= strtok(dup
, "/");
1257 strlcat(cmp_name
, "/", sizeof (cmp_name
));
1258 strlcat(cmp_name
, dir
, sizeof (cmp_name
));
1259 dir
= strtok(NULL
, "/");
1264 return (zfs_strcmp_shortname(name
, cmp_name
, wholedisk
));
1266 (void) strlcpy(path_name
, name
, MAXPATHLEN
);
1267 path_len
= strlen(path_name
);
1268 cmp_len
= strlen(cmp_name
);
1271 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1276 if ((path_len
!= cmp_len
) || strcmp(path_name
, cmp_name
))
1283 * Given a full path to a device determine if that device appears in the
1284 * import search path. If it does return the first match and store the
1285 * index in the passed 'order' variable, otherwise return an error.
1288 zfs_path_order(char *name
, int *order
)
1290 int i
= 0, error
= ENOENT
;
1291 char *dir
, *env
, *envdup
;
1293 env
= getenv("ZPOOL_IMPORT_PATH");
1295 envdup
= strdup(env
);
1296 dir
= strtok(envdup
, ":");
1298 if (strncmp(name
, dir
, strlen(dir
)) == 0) {
1303 dir
= strtok(NULL
, ":");
1308 for (i
= 0; i
< DEFAULT_IMPORT_PATH_SIZE
; i
++) {
1309 if (strncmp(name
, zpool_default_import_path
[i
],
1310 strlen(zpool_default_import_path
[i
])) == 0) {
1322 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1326 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
1330 zc
->zc_nvlist_dst_size
= len
;
1332 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1333 if (zc
->zc_nvlist_dst
== 0)
1340 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1341 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1342 * filled in by the kernel to indicate the actual required size.
1345 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
1347 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1349 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1350 if (zc
->zc_nvlist_dst
== 0)
1357 * Called to free the src and dst nvlists stored in the command structure.
1360 zcmd_free_nvlists(zfs_cmd_t
*zc
)
1362 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
1363 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
1364 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1365 zc
->zc_nvlist_conf
= 0;
1366 zc
->zc_nvlist_src
= 0;
1367 zc
->zc_nvlist_dst
= 0;
1371 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
1377 verify(nvlist_size(nvl
, &len
, NV_ENCODE_NATIVE
) == 0);
1379 if ((packed
= zfs_alloc(hdl
, len
)) == NULL
)
1382 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
1384 *outnv
= (uint64_t)(uintptr_t)packed
;
1391 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1393 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
1394 &zc
->zc_nvlist_conf_size
, nvl
));
1398 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1400 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
1401 &zc
->zc_nvlist_src_size
, nvl
));
1405 * Unpacks an nvlist from the ZFS ioctl command structure.
1408 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
1410 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
1411 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
1412 return (no_memory(hdl
));
1418 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
1420 return (ioctl(hdl
->libzfs_fd
, request
, zc
));
1424 * ================================================================
1425 * API shared by zfs and zpool property management
1426 * ================================================================
1430 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
1432 zprop_list_t
*pl
= cbp
->cb_proplist
;
1437 cbp
->cb_first
= B_FALSE
;
1438 if (cbp
->cb_scripted
)
1442 * Start with the length of the column headers.
1444 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
1445 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
1447 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
1449 cbp
->cb_colwidths
[GET_COL_RECVD
] = strlen(dgettext(TEXT_DOMAIN
,
1451 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
1454 /* first property is always NAME */
1455 assert(cbp
->cb_proplist
->pl_prop
==
1456 ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
: ZFS_PROP_NAME
));
1459 * Go through and calculate the widths for each column. For the
1460 * 'source' column, we kludge it up by taking the worst-case scenario of
1461 * inheriting from the longest name. This is acceptable because in the
1462 * majority of cases 'SOURCE' is the last column displayed, and we don't
1463 * use the width anyway. Note that the 'VALUE' column can be oversized,
1464 * if the name of the property is much longer than any values we find.
1466 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
1470 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1471 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
1472 zpool_prop_to_name(pl
->pl_prop
) :
1473 zfs_prop_to_name(pl
->pl_prop
);
1475 len
= strlen(propname
);
1476 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1477 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1479 len
= strlen(pl
->pl_user_prop
);
1480 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1481 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1485 * 'VALUE' column. The first property is always the 'name'
1486 * property that was tacked on either by /sbin/zfs's
1487 * zfs_do_get() or when calling zprop_expand_list(), so we
1488 * ignore its width. If the user specified the name property
1489 * to display, then it will be later in the list in any case.
1491 if (pl
!= cbp
->cb_proplist
&&
1492 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
1493 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
1495 /* 'RECEIVED' column. */
1496 if (pl
!= cbp
->cb_proplist
&&
1497 pl
->pl_recvd_width
> cbp
->cb_colwidths
[GET_COL_RECVD
])
1498 cbp
->cb_colwidths
[GET_COL_RECVD
] = pl
->pl_recvd_width
;
1501 * 'NAME' and 'SOURCE' columns
1503 if (pl
->pl_prop
== (type
== ZFS_TYPE_POOL
? ZPOOL_PROP_NAME
:
1505 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_NAME
]) {
1506 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
1507 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
1508 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
1513 * Now go through and print the headers.
1515 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1516 switch (cbp
->cb_columns
[i
]) {
1518 title
= dgettext(TEXT_DOMAIN
, "NAME");
1520 case GET_COL_PROPERTY
:
1521 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
1524 title
= dgettext(TEXT_DOMAIN
, "VALUE");
1527 title
= dgettext(TEXT_DOMAIN
, "RECEIVED");
1529 case GET_COL_SOURCE
:
1530 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
1536 if (title
!= NULL
) {
1537 if (i
== (ZFS_GET_NCOLS
- 1) ||
1538 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1539 (void) printf("%s", title
);
1541 (void) printf("%-*s ",
1542 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1546 (void) printf("\n");
1550 * Display a single line of output, according to the settings in the callback
1554 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
1555 const char *propname
, const char *value
, zprop_source_t sourcetype
,
1556 const char *source
, const char *recvd_value
)
1559 const char *str
= NULL
;
1563 * Ignore those source types that the user has chosen to ignore.
1565 if ((sourcetype
& cbp
->cb_sources
) == 0)
1569 zprop_print_headers(cbp
, cbp
->cb_type
);
1571 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1572 switch (cbp
->cb_columns
[i
]) {
1577 case GET_COL_PROPERTY
:
1585 case GET_COL_SOURCE
:
1586 switch (sourcetype
) {
1587 case ZPROP_SRC_NONE
:
1591 case ZPROP_SRC_DEFAULT
:
1595 case ZPROP_SRC_LOCAL
:
1599 case ZPROP_SRC_TEMPORARY
:
1603 case ZPROP_SRC_INHERITED
:
1604 (void) snprintf(buf
, sizeof (buf
),
1605 "inherited from %s", source
);
1608 case ZPROP_SRC_RECEIVED
:
1614 assert(!"unhandled zprop_source_t");
1619 str
= (recvd_value
== NULL
? "-" : recvd_value
);
1626 if (i
== (ZFS_GET_NCOLS
- 1) ||
1627 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1628 (void) printf("%s", str
);
1629 else if (cbp
->cb_scripted
)
1630 (void) printf("%s\t", str
);
1632 (void) printf("%-*s ",
1633 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1637 (void) printf("\n");
1641 * Given a numeric suffix, convert the value into a number of bits that the
1642 * resulting value must be shifted.
1645 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1647 const char *ends
= "BKMGTPEZ";
1652 for (i
= 0; i
< strlen(ends
); i
++) {
1653 if (toupper(buf
[0]) == ends
[i
])
1656 if (i
== strlen(ends
)) {
1658 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1659 "invalid numeric suffix '%s'"), buf
);
1664 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1665 * However, 'BB' and 'BiB' are disallowed.
1667 if (buf
[1] == '\0' ||
1668 (toupper(buf
[0]) != 'B' &&
1669 ((toupper(buf
[1]) == 'B' && buf
[2] == '\0') ||
1670 (toupper(buf
[1]) == 'I' && toupper(buf
[2]) == 'B' &&
1675 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1676 "invalid numeric suffix '%s'"), buf
);
1681 * Convert a string of the form '100G' into a real number. Used when setting
1682 * properties or creating a volume. 'buf' is used to place an extended error
1683 * message for the caller to use.
1686 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1693 /* Check to see if this looks like a number. */
1694 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1696 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1697 "bad numeric value '%s'"), value
);
1701 /* Rely on strtoull() to process the numeric portion. */
1703 *num
= strtoull(value
, &end
, 10);
1706 * Check for ERANGE, which indicates that the value is too large to fit
1707 * in a 64-bit value.
1709 if (errno
== ERANGE
) {
1711 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1712 "numeric value is too large"));
1717 * If we have a decimal value, then do the computation with floating
1718 * point arithmetic. Otherwise, use standard arithmetic.
1721 double fval
= strtod(value
, &end
);
1723 if ((shift
= str2shift(hdl
, end
)) == -1)
1726 fval
*= pow(2, shift
);
1728 if (fval
> UINT64_MAX
) {
1730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1731 "numeric value is too large"));
1735 *num
= (uint64_t)fval
;
1737 if ((shift
= str2shift(hdl
, end
)) == -1)
1740 /* Check for overflow */
1741 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1744 "numeric value is too large"));
1755 * Given a propname=value nvpair to set, parse any numeric properties
1756 * (index, boolean, etc) if they are specified as strings and add the
1757 * resulting nvpair to the returned nvlist.
1759 * At the DSL layer, all properties are either 64-bit numbers or strings.
1760 * We want the user to be able to ignore this fact and specify properties
1761 * as native values (numbers, for example) or as strings (to simplify
1762 * command line utilities). This also handles converting index types
1763 * (compression, checksum, etc) from strings to their on-disk index.
1766 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1767 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1770 data_type_t datatype
= nvpair_type(elem
);
1771 zprop_type_t proptype
;
1772 const char *propname
;
1774 boolean_t isnone
= B_FALSE
;
1777 if (type
== ZFS_TYPE_POOL
) {
1778 proptype
= zpool_prop_get_type(prop
);
1779 propname
= zpool_prop_to_name(prop
);
1781 proptype
= zfs_prop_get_type(prop
);
1782 propname
= zfs_prop_to_name(prop
);
1786 * Convert any properties to the internal DSL value types.
1792 case PROP_TYPE_STRING
:
1793 if (datatype
!= DATA_TYPE_STRING
) {
1794 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1795 "'%s' must be a string"), nvpair_name(elem
));
1798 err
= nvpair_value_string(elem
, svalp
);
1800 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1801 "'%s' is invalid"), nvpair_name(elem
));
1804 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1805 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1806 "'%s' is too long"), nvpair_name(elem
));
1811 case PROP_TYPE_NUMBER
:
1812 if (datatype
== DATA_TYPE_STRING
) {
1813 (void) nvpair_value_string(elem
, &value
);
1814 if (strcmp(value
, "none") == 0) {
1816 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1820 } else if (datatype
== DATA_TYPE_UINT64
) {
1821 (void) nvpair_value_uint64(elem
, ivalp
);
1823 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1824 "'%s' must be a number"), nvpair_name(elem
));
1829 * Quota special: force 'none' and don't allow 0.
1831 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1832 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1833 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1834 "use 'none' to disable quota/refquota"));
1839 * Special handling for "*_limit=none". In this case it's not
1842 if ((type
& ZFS_TYPE_DATASET
) && isnone
&&
1843 (prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
1844 prop
== ZFS_PROP_SNAPSHOT_LIMIT
)) {
1845 *ivalp
= UINT64_MAX
;
1849 case PROP_TYPE_INDEX
:
1850 if (datatype
!= DATA_TYPE_STRING
) {
1851 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1852 "'%s' must be a string"), nvpair_name(elem
));
1856 (void) nvpair_value_string(elem
, &value
);
1858 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1859 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1860 "'%s' must be one of '%s'"), propname
,
1861 zprop_values(prop
, type
));
1871 * Add the result to our return set of properties.
1873 if (*svalp
!= NULL
) {
1874 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1875 (void) no_memory(hdl
);
1879 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1880 (void) no_memory(hdl
);
1887 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1892 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1896 zprop_list_t
*entry
;
1898 prop
= zprop_name_to_prop(propname
, type
);
1900 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
, B_FALSE
))
1904 * When no property table entry can be found, return failure if
1905 * this is a pool property or if this isn't a user-defined
1908 if (prop
== ZPROP_INVAL
&& ((type
== ZFS_TYPE_POOL
&&
1909 !zpool_prop_feature(propname
) &&
1910 !zpool_prop_unsupported(propname
)) ||
1911 (type
== ZFS_TYPE_DATASET
&& !zfs_prop_user(propname
) &&
1912 !zfs_prop_userquota(propname
) && !zfs_prop_written(propname
)))) {
1913 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1914 "invalid property '%s'"), propname
);
1915 return (zfs_error(hdl
, EZFS_BADPROP
,
1916 dgettext(TEXT_DOMAIN
, "bad property list")));
1919 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1922 entry
->pl_prop
= prop
;
1923 if (prop
== ZPROP_INVAL
) {
1924 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) ==
1929 entry
->pl_width
= strlen(propname
);
1931 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1941 * Given a comma-separated list of properties, construct a property list
1942 * containing both user-defined and native properties. This function will
1943 * return a NULL list if 'all' is specified, which can later be expanded
1944 * by zprop_expand_list().
1947 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1953 * If 'all' is specified, return a NULL list.
1955 if (strcmp(props
, "all") == 0)
1959 * If no props were specified, return an error.
1961 if (props
[0] == '\0') {
1962 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1963 "no properties specified"));
1964 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1965 "bad property list")));
1969 * It would be nice to use getsubopt() here, but the inclusion of column
1970 * aliases makes this more effort than it's worth.
1972 while (*props
!= '\0') {
1977 if ((p
= strchr(props
, ',')) == NULL
) {
1978 len
= strlen(props
);
1985 * Check for empty options.
1988 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1989 "empty property name"));
1990 return (zfs_error(hdl
, EZFS_BADPROP
,
1991 dgettext(TEXT_DOMAIN
, "bad property list")));
1995 * Check all regular property names.
2000 if (strcmp(props
, "space") == 0) {
2001 static char *spaceprops
[] = {
2002 "name", "avail", "used", "usedbysnapshots",
2003 "usedbydataset", "usedbyrefreservation",
2004 "usedbychildren", NULL
2008 for (i
= 0; spaceprops
[i
]; i
++) {
2009 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
2011 listp
= &(*listp
)->pl_next
;
2014 if (addlist(hdl
, props
, listp
, type
))
2016 listp
= &(*listp
)->pl_next
;
2028 zprop_free_list(zprop_list_t
*pl
)
2032 while (pl
!= NULL
) {
2034 free(pl
->pl_user_prop
);
2040 typedef struct expand_data
{
2041 zprop_list_t
**last
;
2042 libzfs_handle_t
*hdl
;
2047 zprop_expand_list_cb(int prop
, void *cb
)
2049 zprop_list_t
*entry
;
2050 expand_data_t
*edp
= cb
;
2052 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
2053 return (ZPROP_INVAL
);
2055 entry
->pl_prop
= prop
;
2056 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
2057 entry
->pl_all
= B_TRUE
;
2059 *(edp
->last
) = entry
;
2060 edp
->last
= &entry
->pl_next
;
2062 return (ZPROP_CONT
);
2066 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
2068 zprop_list_t
*entry
;
2069 zprop_list_t
**last
;
2074 * If this is the very first time we've been called for an 'all'
2075 * specification, expand the list to include all native
2084 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
2085 B_FALSE
, type
) == ZPROP_INVAL
)
2089 * Add 'name' to the beginning of the list, which is handled
2092 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
2095 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
2097 entry
->pl_width
= zprop_width(entry
->pl_prop
,
2098 &entry
->pl_fixed
, type
);
2099 entry
->pl_all
= B_TRUE
;
2100 entry
->pl_next
= *plp
;
2107 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
2110 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));