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>
27 * Copyright (c) 2017 Datto Inc.
31 * Internal utility routines for the ZFS library.
45 #include <sys/mnttab.h>
46 #include <sys/mntent.h>
47 #include <sys/types.h>
51 #include <libzfs_core.h>
53 #include "libzfs_impl.h"
55 #include "zfeature_common.h"
56 #include <zfs_fletcher.h>
59 libzfs_errno(libzfs_handle_t
*hdl
)
61 return (hdl
->libzfs_error
);
65 libzfs_error_init(int error
)
69 return (dgettext(TEXT_DOMAIN
, "The ZFS modules are not "
70 "loaded.\nTry running '/sbin/modprobe zfs' as root "
73 return (dgettext(TEXT_DOMAIN
, "/dev/zfs and /proc/self/mounts "
74 "are required.\nTry running 'udevadm trigger' and 'mount "
75 "-t proc proc /proc' as root.\n"));
77 return (dgettext(TEXT_DOMAIN
, "The ZFS modules cannot be "
78 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
79 "root to manually load them.\n"));
81 return (dgettext(TEXT_DOMAIN
, "Permission denied the "
82 "ZFS utilities must be run as root.\n"));
84 return (dgettext(TEXT_DOMAIN
, "Failed to initialize the "
85 "libzfs library.\n"));
90 libzfs_error_action(libzfs_handle_t
*hdl
)
92 return (hdl
->libzfs_action
);
96 libzfs_error_description(libzfs_handle_t
*hdl
)
98 if (hdl
->libzfs_desc
[0] != '\0')
99 return (hdl
->libzfs_desc
);
101 switch (hdl
->libzfs_error
) {
103 return (dgettext(TEXT_DOMAIN
, "out of memory"));
105 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
106 case EZFS_PROPREADONLY
:
107 return (dgettext(TEXT_DOMAIN
, "read-only property"));
109 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
110 "datasets of this type"));
111 case EZFS_PROPNONINHERIT
:
112 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
114 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
116 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
117 "datasets of this type"));
119 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
121 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
123 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
125 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
126 case EZFS_DSREADONLY
:
127 return (dgettext(TEXT_DOMAIN
, "dataset is read-only"));
129 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
131 case EZFS_INVALIDNAME
:
132 return (dgettext(TEXT_DOMAIN
, "invalid name"));
133 case EZFS_BADRESTORE
:
134 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
137 return (dgettext(TEXT_DOMAIN
, "backup failed"));
139 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
141 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
143 return (dgettext(TEXT_DOMAIN
, "invalid device"));
144 case EZFS_NOREPLICAS
:
145 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
146 case EZFS_RESILVERING
:
147 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
148 case EZFS_BADVERSION
:
149 return (dgettext(TEXT_DOMAIN
, "unsupported version or "
151 case EZFS_POOLUNAVAIL
:
152 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
153 case EZFS_DEVOVERFLOW
:
154 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
156 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
157 case EZFS_CROSSTARGET
:
158 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
161 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
162 case EZFS_MOUNTFAILED
:
163 return (dgettext(TEXT_DOMAIN
, "mount failed"));
164 case EZFS_UMOUNTFAILED
:
165 return (dgettext(TEXT_DOMAIN
, "umount failed"));
166 case EZFS_UNSHARENFSFAILED
:
167 return (dgettext(TEXT_DOMAIN
, "unshare(1M) failed"));
168 case EZFS_SHARENFSFAILED
:
169 return (dgettext(TEXT_DOMAIN
, "share(1M) failed"));
170 case EZFS_UNSHARESMBFAILED
:
171 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
172 case EZFS_SHARESMBFAILED
:
173 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
175 return (dgettext(TEXT_DOMAIN
, "permission denied"));
177 return (dgettext(TEXT_DOMAIN
, "out of space"));
179 return (dgettext(TEXT_DOMAIN
, "bad address"));
181 return (dgettext(TEXT_DOMAIN
, "I/O error"));
183 return (dgettext(TEXT_DOMAIN
, "signal received"));
185 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
187 case EZFS_INVALCONFIG
:
188 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
190 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
192 return (dgettext(TEXT_DOMAIN
, "no history available"));
194 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
196 case EZFS_POOL_NOTSUP
:
197 return (dgettext(TEXT_DOMAIN
, "operation not supported "
198 "on this type of pool"));
199 case EZFS_POOL_INVALARG
:
200 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
201 "this pool operation"));
202 case EZFS_NAMETOOLONG
:
203 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
204 case EZFS_OPENFAILED
:
205 return (dgettext(TEXT_DOMAIN
, "open failed"));
207 return (dgettext(TEXT_DOMAIN
,
208 "disk capacity information could not be retrieved"));
209 case EZFS_LABELFAILED
:
210 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
212 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
214 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
215 case EZFS_BADPERMSET
:
216 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
217 case EZFS_NODELEGATION
:
218 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
219 "disabled on pool"));
221 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
223 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
224 case EZFS_VDEVNOTSUP
:
225 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
228 return (dgettext(TEXT_DOMAIN
, "operation not supported "
230 case EZFS_ACTIVE_SPARE
:
231 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
233 case EZFS_UNPLAYED_LOGS
:
234 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
236 case EZFS_REFTAG_RELE
:
237 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
238 case EZFS_REFTAG_HOLD
:
239 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
241 case EZFS_TAGTOOLONG
:
242 return (dgettext(TEXT_DOMAIN
, "tag too long"));
243 case EZFS_PIPEFAILED
:
244 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
245 case EZFS_THREADCREATEFAILED
:
246 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
247 case EZFS_POSTSPLIT_ONLINE
:
248 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
250 case EZFS_SCRUB_PAUSED
:
251 return (dgettext(TEXT_DOMAIN
, "scrub is paused; "
252 "use 'zpool scrub' to resume"));
254 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
255 "use 'zpool scrub -s' to cancel current scrub"));
257 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
259 return (dgettext(TEXT_DOMAIN
, "unable to generate diffs"));
261 return (dgettext(TEXT_DOMAIN
, "invalid diff data"));
262 case EZFS_POOLREADONLY
:
263 return (dgettext(TEXT_DOMAIN
, "pool is read-only"));
264 case EZFS_ACTIVE_POOL
:
265 return (dgettext(TEXT_DOMAIN
, "pool is imported on a "
267 case EZFS_CRYPTOFAILED
:
268 return (dgettext(TEXT_DOMAIN
, "encryption failure"));
270 return (dgettext(TEXT_DOMAIN
, "unknown error"));
272 assert(hdl
->libzfs_error
== 0);
273 return (dgettext(TEXT_DOMAIN
, "no error"));
279 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
285 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
287 hdl
->libzfs_desc_active
= 1;
293 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
295 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
297 hdl
->libzfs_error
= error
;
299 if (hdl
->libzfs_desc_active
)
300 hdl
->libzfs_desc_active
= 0;
302 hdl
->libzfs_desc
[0] = '\0';
304 if (hdl
->libzfs_printerr
) {
305 if (error
== EZFS_UNKNOWN
) {
306 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
307 "error: %s\n"), libzfs_error_description(hdl
));
311 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
312 libzfs_error_description(hdl
));
313 if (error
== EZFS_NOMEM
)
319 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
321 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
326 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
332 zfs_verror(hdl
, error
, fmt
, ap
);
340 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
346 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
350 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
354 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
358 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
362 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
370 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
372 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
377 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
383 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
392 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
396 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
397 "dataset does not exist"));
398 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
403 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
408 "dataset already exists"));
409 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
413 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
415 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
418 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
421 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
424 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
427 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
428 "pool I/O is currently suspended"));
429 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
432 zfs_verror(hdl
, EZFS_ACTIVE_POOL
, fmt
, ap
);
435 zfs_error_aux(hdl
, strerror(error
));
436 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
445 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
447 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
452 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
458 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
465 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
470 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
471 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
475 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
476 "pool already exists"));
477 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
481 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
482 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
486 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
487 "one or more devices is currently unavailable"));
488 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
492 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
496 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
500 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
505 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
509 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
510 "pool I/O is currently suspended"));
511 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
515 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
519 "block size out of range or does not match"));
520 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
523 zfs_verror(hdl
, EZFS_ACTIVE_POOL
, fmt
, ap
);
527 zfs_error_aux(hdl
, strerror(error
));
528 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
536 * Display an out of memory error message and abort the current program.
539 no_memory(libzfs_handle_t
*hdl
)
541 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
545 * A safe form of malloc() which will die if the allocation fails.
548 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
552 if ((data
= calloc(1, size
)) == NULL
)
553 (void) no_memory(hdl
);
559 * A safe form of asprintf() which will die if the allocation fails.
563 zfs_asprintf(libzfs_handle_t
*hdl
, const char *fmt
, ...)
571 err
= vasprintf(&ret
, fmt
, ap
);
576 (void) no_memory(hdl
);
582 * A safe form of realloc(), which also zeroes newly allocated space.
585 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
589 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
590 (void) no_memory(hdl
);
594 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
599 * A safe form of strdup() which will die if the allocation fails.
602 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
606 if ((ret
= strdup(str
)) == NULL
)
607 (void) no_memory(hdl
);
613 * Convert a number to an appropriately human-readable output.
616 zfs_nicenum_format(uint64_t num
, char *buf
, size_t buflen
,
617 enum zfs_nicenum_format format
)
622 const char *units
[3][7] = {
623 [ZFS_NICENUM_1024
] = {"", "K", "M", "G", "T", "P", "E"},
624 [ZFS_NICENUM_BYTES
] = {"B", "K", "M", "G", "T", "P", "E"},
625 [ZFS_NICENUM_TIME
] = {"ns", "us", "ms", "s", "?", "?", "?"}
628 const int units_len
[] = {[ZFS_NICENUM_1024
] = 6,
629 [ZFS_NICENUM_BYTES
] = 6,
630 [ZFS_NICENUM_TIME
] = 4};
632 const int k_unit
[] = { [ZFS_NICENUM_1024
] = 1024,
633 [ZFS_NICENUM_BYTES
] = 1024,
634 [ZFS_NICENUM_TIME
] = 1000};
638 if (format
== ZFS_NICENUM_RAW
) {
639 snprintf(buf
, buflen
, "%llu", (u_longlong_t
)num
);
641 } else if (format
== ZFS_NICENUM_RAWTIME
&& num
> 0) {
642 snprintf(buf
, buflen
, "%llu", (u_longlong_t
)num
);
644 } else if (format
== ZFS_NICENUM_RAWTIME
&& num
== 0) {
645 snprintf(buf
, buflen
, "%s", "-");
649 while (n
>= k_unit
[format
] && index
< units_len
[format
]) {
654 u
= units
[format
][index
];
656 /* Don't print zero latencies since they're invalid */
657 if ((format
== ZFS_NICENUM_TIME
) && (num
== 0)) {
658 (void) snprintf(buf
, buflen
, "-");
659 } else if ((index
== 0) || ((num
%
660 (uint64_t)powl(k_unit
[format
], index
)) == 0)) {
662 * If this is an even multiple of the base, always display
663 * without any decimal precision.
665 (void) snprintf(buf
, buflen
, "%llu%s", (u_longlong_t
)n
, u
);
669 * We want to choose a precision that reflects the best choice
670 * for fitting in 5 characters. This can get rather tricky when
671 * we have numbers that are very close to an order of magnitude.
672 * For example, when displaying 10239 (which is really 9.999K),
673 * we want only a single place of precision for 10.0K. We could
674 * develop some complex heuristics for this, but it's much
675 * easier just to try each combination in turn.
678 for (i
= 2; i
>= 0; i
--) {
680 (uint64_t)powl(k_unit
[format
], index
);
683 * Don't print floating point values for time. Note,
684 * we use floor() instead of round() here, since
685 * round can result in undesirable results. For
686 * example, if "num" is in the range of
687 * 999500-999999, it will print out "1000us". This
688 * doesn't happen if we use floor().
690 if (format
== ZFS_NICENUM_TIME
) {
691 if (snprintf(buf
, buflen
, "%d%s",
692 (unsigned int) floor(val
), u
) <= 5)
696 if (snprintf(buf
, buflen
, "%.*f%s", i
,
705 * Convert a number to an appropriately human-readable output.
708 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
710 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_1024
);
714 * Convert a time to an appropriately human-readable output.
715 * @num: Time in nanoseconds
718 zfs_nicetime(uint64_t num
, char *buf
, size_t buflen
)
720 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_TIME
);
724 * Print out a raw number with correct column spacing
727 zfs_niceraw(uint64_t num
, char *buf
, size_t buflen
)
729 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_RAW
);
733 * Convert a number of bytes to an appropriately human-readable output.
736 zfs_nicebytes(uint64_t num
, char *buf
, size_t buflen
)
738 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_BYTES
);
742 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
744 hdl
->libzfs_printerr
= printerr
;
748 libzfs_module_loaded(const char *module
)
750 const char path_prefix
[] = "/sys/module/";
753 memcpy(path
, path_prefix
, sizeof (path_prefix
) - 1);
754 strcpy(path
+ sizeof (path_prefix
) - 1, module
);
756 return (access(path
, F_OK
) == 0);
761 * Read lines from an open file descriptor and store them in an array of
762 * strings until EOF. lines[] will be allocated and populated with all the
763 * lines read. All newlines are replaced with NULL terminators for
764 * convenience. lines[] must be freed after use with libzfs_free_str_array().
766 * Returns the number of lines read.
769 libzfs_read_stdout_from_fd(int fd
, char **lines
[])
776 char **tmp_lines
= NULL
, **tmp
;
780 fp
= fdopen(fd
, "r");
784 rc
= getline(&line
, &len
, fp
);
788 tmp
= realloc(tmp_lines
, sizeof (*tmp_lines
) * (lines_cnt
+ 1));
790 /* Return the lines we were able to process */
795 /* Terminate newlines */
796 if ((nl
= strchr(line
, '\n')) != NULL
)
798 tmp_lines
[lines_cnt
] = line
;
808 libzfs_run_process_impl(const char *path
, char *argv
[], char *env
[], int flags
,
809 char **lines
[], int *lines_cnt
)
812 int error
, devnull_fd
;
816 * Setup a pipe between our child and parent process if we're
819 if ((lines
!= NULL
) && pipe(link
) == -1)
825 devnull_fd
= open("/dev/null", O_WRONLY
);
830 if (!(flags
& STDOUT_VERBOSE
) && (lines
== NULL
))
831 (void) dup2(devnull_fd
, STDOUT_FILENO
);
832 else if (lines
!= NULL
) {
833 /* Save the output to lines[] */
834 dup2(link
[1], STDOUT_FILENO
);
839 if (!(flags
& STDERR_VERBOSE
))
840 (void) dup2(devnull_fd
, STDERR_FILENO
);
844 if (flags
& NO_DEFAULT_PATH
) {
848 execve(path
, argv
, env
);
853 execvpe(path
, argv
, env
);
857 } else if (pid
> 0) {
861 while ((error
= waitpid(pid
, &status
, 0)) == -1 &&
863 if (error
< 0 || !WIFEXITED(status
))
868 *lines_cnt
= libzfs_read_stdout_from_fd(link
[0], lines
);
870 return (WEXITSTATUS(status
));
877 libzfs_run_process(const char *path
, char *argv
[], int flags
)
879 return (libzfs_run_process_impl(path
, argv
, NULL
, flags
, NULL
, NULL
));
883 * Run a command and store its stdout lines in an array of strings (lines[]).
884 * lines[] is allocated and populated for you, and the number of lines is set in
885 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
886 * All newlines (\n) in lines[] are terminated for convenience.
889 libzfs_run_process_get_stdout(const char *path
, char *argv
[], char *env
[],
890 char **lines
[], int *lines_cnt
)
892 return (libzfs_run_process_impl(path
, argv
, env
, 0, lines
, lines_cnt
));
896 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
897 * means that *path needs to be the full path to the executable.
900 libzfs_run_process_get_stdout_nopath(const char *path
, char *argv
[],
901 char *env
[], char **lines
[], int *lines_cnt
)
903 return (libzfs_run_process_impl(path
, argv
, env
, NO_DEFAULT_PATH
,
908 * Free an array of strings. Free both the strings contained in the array and
912 libzfs_free_str_array(char **strs
, int count
)
921 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
924 * Returns 0 otherwise.
927 libzfs_envvar_is_set(char *envvar
)
929 char *env
= getenv(envvar
);
930 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
931 (!strncasecmp(env
, "YES", 3) && strnlen(env
, 4) == 3) ||
932 (!strncasecmp(env
, "ON", 2) && strnlen(env
, 3) == 2)))
939 * Verify the required ZFS_DEV device is available and optionally attempt
940 * to load the ZFS modules. Under normal circumstances the modules
941 * should already have been loaded by some external mechanism.
943 * Environment variables:
944 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
945 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
948 libzfs_load_module(const char *module
)
950 char *argv
[4] = {"/sbin/modprobe", "-q", (char *)module
, (char *)0};
951 char *load_str
, *timeout_str
;
952 long timeout
= 10; /* seconds */
953 long busy_timeout
= 10; /* milliseconds */
957 /* Optionally request module loading */
958 if (!libzfs_module_loaded(module
)) {
959 load_str
= getenv("ZFS_MODULE_LOADING");
961 if (!strncasecmp(load_str
, "YES", strlen("YES")) ||
962 !strncasecmp(load_str
, "ON", strlen("ON")))
968 if (load
&& libzfs_run_process("/sbin/modprobe", argv
, 0))
972 /* Module loading is synchronous it must be available */
973 if (!libzfs_module_loaded(module
))
977 * Device creation by udev is asynchronous and waiting may be
978 * required. Busy wait for 10ms and then fall back to polling every
979 * 10ms for the allowed timeout (default 10s, max 10m). This is
980 * done to optimize for the common case where the device is
981 * immediately available and to avoid penalizing the possible
982 * case where udev is slow or unable to create the device.
984 timeout_str
= getenv("ZFS_MODULE_TIMEOUT");
986 timeout
= strtol(timeout_str
, NULL
, 0);
987 timeout
= MAX(MIN(timeout
, (10 * 60)), 0); /* 0 <= N <= 600 */
992 fd
= open(ZFS_DEV
, O_RDWR
);
996 } else if (errno
!= ENOENT
) {
998 } else if (NSEC2MSEC(gethrtime() - start
) < busy_timeout
) {
1001 usleep(10 * MILLISEC
);
1003 } while (NSEC2MSEC(gethrtime() - start
) < (timeout
* MILLISEC
));
1011 libzfs_handle_t
*hdl
;
1014 error
= libzfs_load_module(ZFS_DRIVER
);
1020 if ((hdl
= calloc(1, sizeof (libzfs_handle_t
))) == NULL
) {
1024 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
1029 #ifdef HAVE_SETMNTENT
1030 if ((hdl
->libzfs_mnttab
= setmntent(MNTTAB
, "r")) == NULL
) {
1032 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "r")) == NULL
) {
1034 (void) close(hdl
->libzfs_fd
);
1039 hdl
->libzfs_sharetab
= fopen(ZFS_SHARETAB
, "r");
1041 if (libzfs_core_init() != 0) {
1042 (void) close(hdl
->libzfs_fd
);
1043 (void) fclose(hdl
->libzfs_mnttab
);
1044 if (hdl
->libzfs_sharetab
)
1045 (void) fclose(hdl
->libzfs_sharetab
);
1052 zpool_feature_init();
1053 libzfs_mnttab_init(hdl
);
1060 libzfs_fini(libzfs_handle_t
*hdl
)
1062 (void) close(hdl
->libzfs_fd
);
1063 if (hdl
->libzfs_mnttab
)
1064 #ifdef HAVE_SETMNTENT
1065 (void) endmntent(hdl
->libzfs_mnttab
);
1067 (void) fclose(hdl
->libzfs_mnttab
);
1069 if (hdl
->libzfs_sharetab
)
1070 (void) fclose(hdl
->libzfs_sharetab
);
1071 zfs_uninit_libshare(hdl
);
1072 zpool_free_handles(hdl
);
1073 namespace_clear(hdl
);
1074 libzfs_mnttab_fini(hdl
);
1081 zpool_get_handle(zpool_handle_t
*zhp
)
1083 return (zhp
->zpool_hdl
);
1087 zfs_get_handle(zfs_handle_t
*zhp
)
1089 return (zhp
->zfs_hdl
);
1093 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
1095 return (zhp
->zpool_hdl
);
1099 * Given a name, determine whether or not it's a valid path
1100 * (starts with '/' or "./"). If so, walk the mnttab trying
1101 * to match the device number. If not, treat the path as an
1102 * fs/vol/snap/bkmark name.
1105 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
1107 struct stat64 statbuf
;
1108 struct extmnttab entry
;
1111 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
1113 * It's not a valid path, assume it's a name of type 'argtype'.
1115 return (zfs_open(hdl
, path
, argtype
));
1118 if (stat64(path
, &statbuf
) != 0) {
1119 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
1123 /* Reopen MNTTAB to prevent reading stale data from open file */
1124 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
1127 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
1128 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
1137 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
1138 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
1143 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
1147 * Append partition suffix to an otherwise fully qualified device path.
1148 * This is used to generate the name the full path as its stored in
1149 * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length
1150 * of 'path' will be returned on error a negative value is returned.
1153 zfs_append_partition(char *path
, size_t max_len
)
1155 int len
= strlen(path
);
1157 if ((strncmp(path
, UDISK_ROOT
, strlen(UDISK_ROOT
)) == 0) ||
1158 (strncmp(path
, ZVOL_ROOT
, strlen(ZVOL_ROOT
)) == 0)) {
1159 if (len
+ 6 >= max_len
)
1162 (void) strcat(path
, "-part1");
1165 if (len
+ 2 >= max_len
)
1168 if (isdigit(path
[len
-1])) {
1169 (void) strcat(path
, "p1");
1172 (void) strcat(path
, "1");
1181 * Given a shorthand device name check if a file by that name exists in any
1182 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
1183 * one is found, store its fully qualified path in the 'path' buffer passed
1184 * by the caller and return 0, otherwise return an error.
1187 zfs_resolve_shortname(const char *name
, char *path
, size_t len
)
1190 char *dir
, *env
, *envdup
;
1192 env
= getenv("ZPOOL_IMPORT_PATH");
1196 envdup
= strdup(env
);
1197 dir
= strtok(envdup
, ":");
1198 while (dir
&& error
) {
1199 (void) snprintf(path
, len
, "%s/%s", dir
, name
);
1200 error
= access(path
, F_OK
);
1201 dir
= strtok(NULL
, ":");
1205 for (i
= 0; i
< DEFAULT_IMPORT_PATH_SIZE
&& error
< 0; i
++) {
1206 (void) snprintf(path
, len
, "%s/%s",
1207 zpool_default_import_path
[i
], name
);
1208 error
= access(path
, F_OK
);
1212 return (error
? ENOENT
: 0);
1216 * Given a shorthand device name look for a match against 'cmp_name'. This
1217 * is done by checking all prefix expansions using either the default
1218 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
1219 * variable. Proper partition suffixes will be appended if this is a
1220 * whole disk. When a match is found 0 is returned otherwise ENOENT.
1223 zfs_strcmp_shortname(char *name
, char *cmp_name
, int wholedisk
)
1225 int path_len
, cmp_len
, i
= 0, error
= ENOENT
;
1226 char *dir
, *env
, *envdup
= NULL
;
1227 char path_name
[MAXPATHLEN
];
1229 cmp_len
= strlen(cmp_name
);
1230 env
= getenv("ZPOOL_IMPORT_PATH");
1233 envdup
= strdup(env
);
1234 dir
= strtok(envdup
, ":");
1236 dir
= zpool_default_import_path
[i
];
1240 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
1241 while (dir
[strlen(dir
)-1] == '/')
1242 dir
[strlen(dir
)-1] = '\0';
1244 path_len
= snprintf(path_name
, MAXPATHLEN
, "%s/%s", dir
, name
);
1246 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1248 if ((path_len
== cmp_len
) && strcmp(path_name
, cmp_name
) == 0) {
1254 dir
= strtok(NULL
, ":");
1255 } else if (++i
< DEFAULT_IMPORT_PATH_SIZE
) {
1256 dir
= zpool_default_import_path
[i
];
1269 * Given either a shorthand or fully qualified path name look for a match
1270 * against 'cmp'. The passed name will be expanded as needed for comparison
1271 * purposes and redundant slashes stripped to ensure an accurate match.
1274 zfs_strcmp_pathname(char *name
, char *cmp
, int wholedisk
)
1276 int path_len
, cmp_len
;
1277 char path_name
[MAXPATHLEN
];
1278 char cmp_name
[MAXPATHLEN
];
1281 /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
1282 memset(cmp_name
, 0, MAXPATHLEN
);
1284 dir
= strtok(dup
, "/");
1286 strlcat(cmp_name
, "/", sizeof (cmp_name
));
1287 strlcat(cmp_name
, dir
, sizeof (cmp_name
));
1288 dir
= strtok(NULL
, "/");
1293 return (zfs_strcmp_shortname(name
, cmp_name
, wholedisk
));
1295 (void) strlcpy(path_name
, name
, MAXPATHLEN
);
1296 path_len
= strlen(path_name
);
1297 cmp_len
= strlen(cmp_name
);
1300 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1305 if ((path_len
!= cmp_len
) || strcmp(path_name
, cmp_name
))
1312 * Given a full path to a device determine if that device appears in the
1313 * import search path. If it does return the first match and store the
1314 * index in the passed 'order' variable, otherwise return an error.
1317 zfs_path_order(char *name
, int *order
)
1319 int i
= 0, error
= ENOENT
;
1320 char *dir
, *env
, *envdup
;
1322 env
= getenv("ZPOOL_IMPORT_PATH");
1324 envdup
= strdup(env
);
1325 dir
= strtok(envdup
, ":");
1327 if (strncmp(name
, dir
, strlen(dir
)) == 0) {
1332 dir
= strtok(NULL
, ":");
1337 for (i
= 0; i
< DEFAULT_IMPORT_PATH_SIZE
; i
++) {
1338 if (strncmp(name
, zpool_default_import_path
[i
],
1339 strlen(zpool_default_import_path
[i
])) == 0) {
1351 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1355 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
1359 zc
->zc_nvlist_dst_size
= len
;
1361 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1362 if (zc
->zc_nvlist_dst
== 0)
1369 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1370 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1371 * filled in by the kernel to indicate the actual required size.
1374 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
1376 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1378 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1379 if (zc
->zc_nvlist_dst
== 0)
1386 * Called to free the src and dst nvlists stored in the command structure.
1389 zcmd_free_nvlists(zfs_cmd_t
*zc
)
1391 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
1392 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
1393 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1394 zc
->zc_nvlist_conf
= 0;
1395 zc
->zc_nvlist_src
= 0;
1396 zc
->zc_nvlist_dst
= 0;
1400 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
1406 verify(nvlist_size(nvl
, &len
, NV_ENCODE_NATIVE
) == 0);
1408 if ((packed
= zfs_alloc(hdl
, len
)) == NULL
)
1411 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
1413 *outnv
= (uint64_t)(uintptr_t)packed
;
1420 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1422 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
1423 &zc
->zc_nvlist_conf_size
, nvl
));
1427 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1429 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
1430 &zc
->zc_nvlist_src_size
, nvl
));
1434 * Unpacks an nvlist from the ZFS ioctl command structure.
1437 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
1439 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
1440 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
1441 return (no_memory(hdl
));
1447 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
1449 return (ioctl(hdl
->libzfs_fd
, request
, zc
));
1453 * ================================================================
1454 * API shared by zfs and zpool property management
1455 * ================================================================
1459 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
1461 zprop_list_t
*pl
= cbp
->cb_proplist
;
1466 cbp
->cb_first
= B_FALSE
;
1467 if (cbp
->cb_scripted
)
1471 * Start with the length of the column headers.
1473 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
1474 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
1476 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
1478 cbp
->cb_colwidths
[GET_COL_RECVD
] = strlen(dgettext(TEXT_DOMAIN
,
1480 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
1483 /* first property is always NAME */
1484 assert(cbp
->cb_proplist
->pl_prop
==
1485 ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
: ZFS_PROP_NAME
));
1488 * Go through and calculate the widths for each column. For the
1489 * 'source' column, we kludge it up by taking the worst-case scenario of
1490 * inheriting from the longest name. This is acceptable because in the
1491 * majority of cases 'SOURCE' is the last column displayed, and we don't
1492 * use the width anyway. Note that the 'VALUE' column can be oversized,
1493 * if the name of the property is much longer than any values we find.
1495 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
1499 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1500 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
1501 zpool_prop_to_name(pl
->pl_prop
) :
1502 zfs_prop_to_name(pl
->pl_prop
);
1504 len
= strlen(propname
);
1505 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1506 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1508 len
= strlen(pl
->pl_user_prop
);
1509 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1510 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1514 * 'VALUE' column. The first property is always the 'name'
1515 * property that was tacked on either by /sbin/zfs's
1516 * zfs_do_get() or when calling zprop_expand_list(), so we
1517 * ignore its width. If the user specified the name property
1518 * to display, then it will be later in the list in any case.
1520 if (pl
!= cbp
->cb_proplist
&&
1521 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
1522 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
1524 /* 'RECEIVED' column. */
1525 if (pl
!= cbp
->cb_proplist
&&
1526 pl
->pl_recvd_width
> cbp
->cb_colwidths
[GET_COL_RECVD
])
1527 cbp
->cb_colwidths
[GET_COL_RECVD
] = pl
->pl_recvd_width
;
1530 * 'NAME' and 'SOURCE' columns
1532 if (pl
->pl_prop
== (type
== ZFS_TYPE_POOL
? ZPOOL_PROP_NAME
:
1534 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_NAME
]) {
1535 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
1536 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
1537 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
1542 * Now go through and print the headers.
1544 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1545 switch (cbp
->cb_columns
[i
]) {
1547 title
= dgettext(TEXT_DOMAIN
, "NAME");
1549 case GET_COL_PROPERTY
:
1550 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
1553 title
= dgettext(TEXT_DOMAIN
, "VALUE");
1556 title
= dgettext(TEXT_DOMAIN
, "RECEIVED");
1558 case GET_COL_SOURCE
:
1559 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
1565 if (title
!= NULL
) {
1566 if (i
== (ZFS_GET_NCOLS
- 1) ||
1567 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1568 (void) printf("%s", title
);
1570 (void) printf("%-*s ",
1571 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1575 (void) printf("\n");
1579 * Display a single line of output, according to the settings in the callback
1583 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
1584 const char *propname
, const char *value
, zprop_source_t sourcetype
,
1585 const char *source
, const char *recvd_value
)
1588 const char *str
= NULL
;
1592 * Ignore those source types that the user has chosen to ignore.
1594 if ((sourcetype
& cbp
->cb_sources
) == 0)
1598 zprop_print_headers(cbp
, cbp
->cb_type
);
1600 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1601 switch (cbp
->cb_columns
[i
]) {
1606 case GET_COL_PROPERTY
:
1614 case GET_COL_SOURCE
:
1615 switch (sourcetype
) {
1616 case ZPROP_SRC_NONE
:
1620 case ZPROP_SRC_DEFAULT
:
1624 case ZPROP_SRC_LOCAL
:
1628 case ZPROP_SRC_TEMPORARY
:
1632 case ZPROP_SRC_INHERITED
:
1633 (void) snprintf(buf
, sizeof (buf
),
1634 "inherited from %s", source
);
1637 case ZPROP_SRC_RECEIVED
:
1643 assert(!"unhandled zprop_source_t");
1648 str
= (recvd_value
== NULL
? "-" : recvd_value
);
1655 if (i
== (ZFS_GET_NCOLS
- 1) ||
1656 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1657 (void) printf("%s", str
);
1658 else if (cbp
->cb_scripted
)
1659 (void) printf("%s\t", str
);
1661 (void) printf("%-*s ",
1662 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1666 (void) printf("\n");
1670 * Given a numeric suffix, convert the value into a number of bits that the
1671 * resulting value must be shifted.
1674 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1676 const char *ends
= "BKMGTPEZ";
1681 for (i
= 0; i
< strlen(ends
); i
++) {
1682 if (toupper(buf
[0]) == ends
[i
])
1685 if (i
== strlen(ends
)) {
1687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1688 "invalid numeric suffix '%s'"), buf
);
1693 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1694 * However, 'BB' and 'BiB' are disallowed.
1696 if (buf
[1] == '\0' ||
1697 (toupper(buf
[0]) != 'B' &&
1698 ((toupper(buf
[1]) == 'B' && buf
[2] == '\0') ||
1699 (toupper(buf
[1]) == 'I' && toupper(buf
[2]) == 'B' &&
1704 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1705 "invalid numeric suffix '%s'"), buf
);
1710 * Convert a string of the form '100G' into a real number. Used when setting
1711 * properties or creating a volume. 'buf' is used to place an extended error
1712 * message for the caller to use.
1715 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1722 /* Check to see if this looks like a number. */
1723 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1725 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1726 "bad numeric value '%s'"), value
);
1730 /* Rely on strtoull() to process the numeric portion. */
1732 *num
= strtoull(value
, &end
, 10);
1735 * Check for ERANGE, which indicates that the value is too large to fit
1736 * in a 64-bit value.
1738 if (errno
== ERANGE
) {
1740 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1741 "numeric value is too large"));
1746 * If we have a decimal value, then do the computation with floating
1747 * point arithmetic. Otherwise, use standard arithmetic.
1750 double fval
= strtod(value
, &end
);
1752 if ((shift
= str2shift(hdl
, end
)) == -1)
1755 fval
*= pow(2, shift
);
1757 if (fval
> UINT64_MAX
) {
1759 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1760 "numeric value is too large"));
1764 *num
= (uint64_t)fval
;
1766 if ((shift
= str2shift(hdl
, end
)) == -1)
1769 /* Check for overflow */
1770 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1772 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1773 "numeric value is too large"));
1784 * Given a propname=value nvpair to set, parse any numeric properties
1785 * (index, boolean, etc) if they are specified as strings and add the
1786 * resulting nvpair to the returned nvlist.
1788 * At the DSL layer, all properties are either 64-bit numbers or strings.
1789 * We want the user to be able to ignore this fact and specify properties
1790 * as native values (numbers, for example) or as strings (to simplify
1791 * command line utilities). This also handles converting index types
1792 * (compression, checksum, etc) from strings to their on-disk index.
1795 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1796 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1799 data_type_t datatype
= nvpair_type(elem
);
1800 zprop_type_t proptype
;
1801 const char *propname
;
1803 boolean_t isnone
= B_FALSE
;
1806 if (type
== ZFS_TYPE_POOL
) {
1807 proptype
= zpool_prop_get_type(prop
);
1808 propname
= zpool_prop_to_name(prop
);
1810 proptype
= zfs_prop_get_type(prop
);
1811 propname
= zfs_prop_to_name(prop
);
1815 * Convert any properties to the internal DSL value types.
1821 case PROP_TYPE_STRING
:
1822 if (datatype
!= DATA_TYPE_STRING
) {
1823 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1824 "'%s' must be a string"), nvpair_name(elem
));
1827 err
= nvpair_value_string(elem
, svalp
);
1829 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1830 "'%s' is invalid"), nvpair_name(elem
));
1833 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1834 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1835 "'%s' is too long"), nvpair_name(elem
));
1840 case PROP_TYPE_NUMBER
:
1841 if (datatype
== DATA_TYPE_STRING
) {
1842 (void) nvpair_value_string(elem
, &value
);
1843 if (strcmp(value
, "none") == 0) {
1845 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1849 } else if (datatype
== DATA_TYPE_UINT64
) {
1850 (void) nvpair_value_uint64(elem
, ivalp
);
1852 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1853 "'%s' must be a number"), nvpair_name(elem
));
1858 * Quota special: force 'none' and don't allow 0.
1860 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1861 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1862 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1863 "use 'none' to disable quota/refquota"));
1868 * Special handling for "*_limit=none". In this case it's not
1871 if ((type
& ZFS_TYPE_DATASET
) && isnone
&&
1872 (prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
1873 prop
== ZFS_PROP_SNAPSHOT_LIMIT
)) {
1874 *ivalp
= UINT64_MAX
;
1878 case PROP_TYPE_INDEX
:
1879 if (datatype
!= DATA_TYPE_STRING
) {
1880 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1881 "'%s' must be a string"), nvpair_name(elem
));
1885 (void) nvpair_value_string(elem
, &value
);
1887 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1888 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1889 "'%s' must be one of '%s'"), propname
,
1890 zprop_values(prop
, type
));
1900 * Add the result to our return set of properties.
1902 if (*svalp
!= NULL
) {
1903 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1904 (void) no_memory(hdl
);
1908 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1909 (void) no_memory(hdl
);
1916 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1921 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1925 zprop_list_t
*entry
;
1927 prop
= zprop_name_to_prop(propname
, type
);
1929 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
, B_FALSE
))
1933 * When no property table entry can be found, return failure if
1934 * this is a pool property or if this isn't a user-defined
1937 if (prop
== ZPROP_INVAL
&& ((type
== ZFS_TYPE_POOL
&&
1938 !zpool_prop_feature(propname
) &&
1939 !zpool_prop_unsupported(propname
)) ||
1940 (type
== ZFS_TYPE_DATASET
&& !zfs_prop_user(propname
) &&
1941 !zfs_prop_userquota(propname
) && !zfs_prop_written(propname
)))) {
1942 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1943 "invalid property '%s'"), propname
);
1944 return (zfs_error(hdl
, EZFS_BADPROP
,
1945 dgettext(TEXT_DOMAIN
, "bad property list")));
1948 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1951 entry
->pl_prop
= prop
;
1952 if (prop
== ZPROP_INVAL
) {
1953 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) ==
1958 entry
->pl_width
= strlen(propname
);
1960 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1970 * Given a comma-separated list of properties, construct a property list
1971 * containing both user-defined and native properties. This function will
1972 * return a NULL list if 'all' is specified, which can later be expanded
1973 * by zprop_expand_list().
1976 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1982 * If 'all' is specified, return a NULL list.
1984 if (strcmp(props
, "all") == 0)
1988 * If no props were specified, return an error.
1990 if (props
[0] == '\0') {
1991 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1992 "no properties specified"));
1993 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1994 "bad property list")));
1998 * It would be nice to use getsubopt() here, but the inclusion of column
1999 * aliases makes this more effort than it's worth.
2001 while (*props
!= '\0') {
2006 if ((p
= strchr(props
, ',')) == NULL
) {
2007 len
= strlen(props
);
2014 * Check for empty options.
2017 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2018 "empty property name"));
2019 return (zfs_error(hdl
, EZFS_BADPROP
,
2020 dgettext(TEXT_DOMAIN
, "bad property list")));
2024 * Check all regular property names.
2029 if (strcmp(props
, "space") == 0) {
2030 static char *spaceprops
[] = {
2031 "name", "avail", "used", "usedbysnapshots",
2032 "usedbydataset", "usedbyrefreservation",
2033 "usedbychildren", NULL
2037 for (i
= 0; spaceprops
[i
]; i
++) {
2038 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
2040 listp
= &(*listp
)->pl_next
;
2043 if (addlist(hdl
, props
, listp
, type
))
2045 listp
= &(*listp
)->pl_next
;
2057 zprop_free_list(zprop_list_t
*pl
)
2061 while (pl
!= NULL
) {
2063 free(pl
->pl_user_prop
);
2069 typedef struct expand_data
{
2070 zprop_list_t
**last
;
2071 libzfs_handle_t
*hdl
;
2076 zprop_expand_list_cb(int prop
, void *cb
)
2078 zprop_list_t
*entry
;
2079 expand_data_t
*edp
= cb
;
2081 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
2082 return (ZPROP_INVAL
);
2084 entry
->pl_prop
= prop
;
2085 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
2086 entry
->pl_all
= B_TRUE
;
2088 *(edp
->last
) = entry
;
2089 edp
->last
= &entry
->pl_next
;
2091 return (ZPROP_CONT
);
2095 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
2097 zprop_list_t
*entry
;
2098 zprop_list_t
**last
;
2103 * If this is the very first time we've been called for an 'all'
2104 * specification, expand the list to include all native
2113 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
2114 B_FALSE
, type
) == ZPROP_INVAL
)
2118 * Add 'name' to the beginning of the list, which is handled
2121 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
2124 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
2126 entry
->pl_width
= zprop_width(entry
->pl_prop
,
2127 &entry
->pl_fixed
, type
);
2128 entry
->pl_all
= B_TRUE
;
2129 entry
->pl_next
= *plp
;
2136 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
2139 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));