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.
29 * Internal utility routines for the ZFS library.
43 #include <sys/mnttab.h>
44 #include <sys/mntent.h>
45 #include <sys/types.h>
49 #include <libzfs_core.h>
51 #include "libzfs_impl.h"
53 #include "zfeature_common.h"
56 libzfs_errno(libzfs_handle_t
*hdl
)
58 return (hdl
->libzfs_error
);
62 libzfs_error_init(int error
)
66 return (dgettext(TEXT_DOMAIN
, "The ZFS modules are not "
67 "loaded.\nTry running '/sbin/modprobe zfs' as root "
70 return (dgettext(TEXT_DOMAIN
, "/dev/zfs and /proc/self/mounts "
71 "are required.\nTry running 'udevadm trigger' and 'mount "
72 "-t proc proc /proc' as root.\n"));
74 return (dgettext(TEXT_DOMAIN
, "The ZFS modules cannot be "
75 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
76 "root to manually load them.\n"));
78 return (dgettext(TEXT_DOMAIN
, "Permission denied the "
79 "ZFS utilities must be run as root.\n"));
81 return (dgettext(TEXT_DOMAIN
, "Failed to initialize the "
82 "libzfs library.\n"));
87 libzfs_error_action(libzfs_handle_t
*hdl
)
89 return (hdl
->libzfs_action
);
93 libzfs_error_description(libzfs_handle_t
*hdl
)
95 if (hdl
->libzfs_desc
[0] != '\0')
96 return (hdl
->libzfs_desc
);
98 switch (hdl
->libzfs_error
) {
100 return (dgettext(TEXT_DOMAIN
, "out of memory"));
102 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
103 case EZFS_PROPREADONLY
:
104 return (dgettext(TEXT_DOMAIN
, "read-only property"));
106 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
107 "datasets of this type"));
108 case EZFS_PROPNONINHERIT
:
109 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
111 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
113 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
114 "datasets of this type"));
116 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
118 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
120 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
122 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
123 case EZFS_DSREADONLY
:
124 return (dgettext(TEXT_DOMAIN
, "dataset is read-only"));
126 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
128 case EZFS_INVALIDNAME
:
129 return (dgettext(TEXT_DOMAIN
, "invalid name"));
130 case EZFS_BADRESTORE
:
131 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
134 return (dgettext(TEXT_DOMAIN
, "backup failed"));
136 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
138 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
140 return (dgettext(TEXT_DOMAIN
, "invalid device"));
141 case EZFS_NOREPLICAS
:
142 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
143 case EZFS_RESILVERING
:
144 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
145 case EZFS_BADVERSION
:
146 return (dgettext(TEXT_DOMAIN
, "unsupported version or "
148 case EZFS_POOLUNAVAIL
:
149 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
150 case EZFS_DEVOVERFLOW
:
151 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
153 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
154 case EZFS_CROSSTARGET
:
155 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
158 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
159 case EZFS_MOUNTFAILED
:
160 return (dgettext(TEXT_DOMAIN
, "mount failed"));
161 case EZFS_UMOUNTFAILED
:
162 return (dgettext(TEXT_DOMAIN
, "umount failed"));
163 case EZFS_UNSHARENFSFAILED
:
164 return (dgettext(TEXT_DOMAIN
, "unshare(1M) failed"));
165 case EZFS_SHARENFSFAILED
:
166 return (dgettext(TEXT_DOMAIN
, "share(1M) failed"));
167 case EZFS_UNSHARESMBFAILED
:
168 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
169 case EZFS_SHARESMBFAILED
:
170 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
172 return (dgettext(TEXT_DOMAIN
, "permission denied"));
174 return (dgettext(TEXT_DOMAIN
, "out of space"));
176 return (dgettext(TEXT_DOMAIN
, "bad address"));
178 return (dgettext(TEXT_DOMAIN
, "I/O error"));
180 return (dgettext(TEXT_DOMAIN
, "signal received"));
182 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
184 case EZFS_INVALCONFIG
:
185 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
187 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
189 return (dgettext(TEXT_DOMAIN
, "no history available"));
191 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
193 case EZFS_POOL_NOTSUP
:
194 return (dgettext(TEXT_DOMAIN
, "operation not supported "
195 "on this type of pool"));
196 case EZFS_POOL_INVALARG
:
197 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
198 "this pool operation"));
199 case EZFS_NAMETOOLONG
:
200 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
201 case EZFS_OPENFAILED
:
202 return (dgettext(TEXT_DOMAIN
, "open failed"));
204 return (dgettext(TEXT_DOMAIN
,
205 "disk capacity information could not be retrieved"));
206 case EZFS_LABELFAILED
:
207 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
209 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
211 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
212 case EZFS_BADPERMSET
:
213 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
214 case EZFS_NODELEGATION
:
215 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
216 "disabled on pool"));
218 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
220 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
221 case EZFS_VDEVNOTSUP
:
222 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
225 return (dgettext(TEXT_DOMAIN
, "operation not supported "
227 case EZFS_ACTIVE_SPARE
:
228 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
230 case EZFS_UNPLAYED_LOGS
:
231 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
233 case EZFS_REFTAG_RELE
:
234 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
235 case EZFS_REFTAG_HOLD
:
236 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
238 case EZFS_TAGTOOLONG
:
239 return (dgettext(TEXT_DOMAIN
, "tag too long"));
240 case EZFS_PIPEFAILED
:
241 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
242 case EZFS_THREADCREATEFAILED
:
243 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
244 case EZFS_POSTSPLIT_ONLINE
:
245 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
248 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
249 "use 'zpool scrub -s' to cancel current scrub"));
251 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
253 return (dgettext(TEXT_DOMAIN
, "unable to generate diffs"));
255 return (dgettext(TEXT_DOMAIN
, "invalid diff data"));
256 case EZFS_POOLREADONLY
:
257 return (dgettext(TEXT_DOMAIN
, "pool is read-only"));
259 return (dgettext(TEXT_DOMAIN
, "unknown error"));
261 assert(hdl
->libzfs_error
== 0);
262 return (dgettext(TEXT_DOMAIN
, "no error"));
268 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
274 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
276 hdl
->libzfs_desc_active
= 1;
282 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
284 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
286 hdl
->libzfs_error
= error
;
288 if (hdl
->libzfs_desc_active
)
289 hdl
->libzfs_desc_active
= 0;
291 hdl
->libzfs_desc
[0] = '\0';
293 if (hdl
->libzfs_printerr
) {
294 if (error
== EZFS_UNKNOWN
) {
295 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
296 "error: %s\n"), libzfs_error_description(hdl
));
300 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
301 libzfs_error_description(hdl
));
302 if (error
== EZFS_NOMEM
)
308 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
310 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
315 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
321 zfs_verror(hdl
, error
, fmt
, ap
);
329 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
335 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
339 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
343 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
347 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
351 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
359 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
361 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
366 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
372 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
381 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
385 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
386 "dataset does not exist"));
387 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
392 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
396 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
397 "dataset already exists"));
398 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
402 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
404 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
407 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
410 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
413 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
416 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
417 "pool I/O is currently suspended"));
418 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
421 zfs_error_aux(hdl
, strerror(error
));
422 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
431 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
433 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
438 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
444 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
451 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
456 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
457 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
461 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
462 "pool already exists"));
463 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
468 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
473 "one or more devices is currently unavailable"));
474 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
478 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
482 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
486 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
491 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
495 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
496 "pool I/O is currently suspended"));
497 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
501 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
505 "block size out of range or does not match"));
506 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
510 zfs_error_aux(hdl
, strerror(error
));
511 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
519 * Display an out of memory error message and abort the current program.
522 no_memory(libzfs_handle_t
*hdl
)
524 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
528 * A safe form of malloc() which will die if the allocation fails.
531 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
535 if ((data
= calloc(1, size
)) == NULL
)
536 (void) no_memory(hdl
);
542 * A safe form of asprintf() which will die if the allocation fails.
546 zfs_asprintf(libzfs_handle_t
*hdl
, const char *fmt
, ...)
554 err
= vasprintf(&ret
, fmt
, ap
);
559 (void) no_memory(hdl
);
565 * A safe form of realloc(), which also zeroes newly allocated space.
568 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
572 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
573 (void) no_memory(hdl
);
577 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
582 * A safe form of strdup() which will die if the allocation fails.
585 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
589 if ((ret
= strdup(str
)) == NULL
)
590 (void) no_memory(hdl
);
596 * Convert a number to an appropriately human-readable output.
599 zfs_nicenum_format(uint64_t num
, char *buf
, size_t buflen
,
600 enum zfs_nicenum_format format
)
605 const char *units
[3][7] = {
606 [ZFS_NICENUM_1024
] = {"", "K", "M", "G", "T", "P", "E"},
607 [ZFS_NICENUM_TIME
] = {"ns", "us", "ms", "s", "?", "?", "?"}
610 const int units_len
[] = {[ZFS_NICENUM_1024
] = 6,
611 [ZFS_NICENUM_TIME
] = 4};
613 const int k_unit
[] = { [ZFS_NICENUM_1024
] = 1024,
614 [ZFS_NICENUM_TIME
] = 1000};
618 if (format
== ZFS_NICENUM_RAW
) {
619 snprintf(buf
, buflen
, "%llu", (u_longlong_t
) num
);
624 while (n
>= k_unit
[format
] && index
< units_len
[format
]) {
629 u
= units
[format
][index
];
631 /* Don't print 0ns times */
632 if ((format
== ZFS_NICENUM_TIME
) && (num
== 0)) {
633 (void) snprintf(buf
, buflen
, "-");
634 } else if ((index
== 0) || ((num
%
635 (uint64_t) powl(k_unit
[format
], index
)) == 0)) {
637 * If this is an even multiple of the base, always display
638 * without any decimal precision.
640 (void) snprintf(buf
, buflen
, "%llu%s", (u_longlong_t
) n
, u
);
644 * We want to choose a precision that reflects the best choice
645 * for fitting in 5 characters. This can get rather tricky when
646 * we have numbers that are very close to an order of magnitude.
647 * For example, when displaying 10239 (which is really 9.999K),
648 * we want only a single place of precision for 10.0K. We could
649 * develop some complex heuristics for this, but it's much
650 * easier just to try each combination in turn.
653 for (i
= 2; i
>= 0; i
--) {
655 (uint64_t) powl(k_unit
[format
], index
);
658 * Don't print floating point values for time. Note,
659 * we use floor() instead of round() here, since
660 * round can result in undesirable results. For
661 * example, if "num" is in the range of
662 * 999500-999999, it will print out "1000us". This
663 * doesn't happen if we use floor().
665 if (format
== ZFS_NICENUM_TIME
) {
666 if (snprintf(buf
, buflen
, "%d%s",
667 (unsigned int) floor(val
), u
) <= 5)
671 if (snprintf(buf
, buflen
, "%.*f%s", i
,
680 * Convert a number to an appropriately human-readable output.
683 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
685 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_1024
);
689 * Convert a time to an appropriately human-readable output.
690 * @num: Time in nanoseconds
693 zfs_nicetime(uint64_t num
, char *buf
, size_t buflen
)
695 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_TIME
);
699 * Print out a raw number with correct column spacing
702 zfs_niceraw(uint64_t num
, char *buf
, size_t buflen
)
704 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_RAW
);
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);
728 libzfs_run_process(const char *path
, char *argv
[], int flags
)
731 int error
, devnull_fd
;
735 devnull_fd
= open("/dev/null", O_WRONLY
);
740 if (!(flags
& STDOUT_VERBOSE
))
741 (void) dup2(devnull_fd
, STDOUT_FILENO
);
743 if (!(flags
& STDERR_VERBOSE
))
744 (void) dup2(devnull_fd
, STDERR_FILENO
);
748 (void) execvp(path
, argv
);
750 } else if (pid
> 0) {
753 while ((error
= waitpid(pid
, &status
, 0)) == -1 &&
755 if (error
< 0 || !WIFEXITED(status
))
758 return (WEXITSTATUS(status
));
765 * Verify the required ZFS_DEV device is available and optionally attempt
766 * to load the ZFS modules. Under normal circumstances the modules
767 * should already have been loaded by some external mechanism.
769 * Environment variables:
770 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
771 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
774 libzfs_load_module(const char *module
)
776 char *argv
[4] = {"/sbin/modprobe", "-q", (char *)module
, (char *)0};
777 char *load_str
, *timeout_str
;
778 long timeout
= 10; /* seconds */
779 long busy_timeout
= 10; /* milliseconds */
783 /* Optionally request module loading */
784 if (!libzfs_module_loaded(module
)) {
785 load_str
= getenv("ZFS_MODULE_LOADING");
787 if (!strncasecmp(load_str
, "YES", strlen("YES")) ||
788 !strncasecmp(load_str
, "ON", strlen("ON")))
794 if (load
&& libzfs_run_process("/sbin/modprobe", argv
, 0))
798 /* Module loading is synchronous it must be available */
799 if (!libzfs_module_loaded(module
))
803 * Device creation by udev is asynchronous and waiting may be
804 * required. Busy wait for 10ms and then fall back to polling every
805 * 10ms for the allowed timeout (default 10s, max 10m). This is
806 * done to optimize for the common case where the device is
807 * immediately available and to avoid penalizing the possible
808 * case where udev is slow or unable to create the device.
810 timeout_str
= getenv("ZFS_MODULE_TIMEOUT");
812 timeout
= strtol(timeout_str
, NULL
, 0);
813 timeout
= MAX(MIN(timeout
, (10 * 60)), 0); /* 0 <= N <= 600 */
818 fd
= open(ZFS_DEV
, O_RDWR
);
822 } else if (errno
!= ENOENT
) {
824 } else if (NSEC2MSEC(gethrtime() - start
) < busy_timeout
) {
827 usleep(10 * MILLISEC
);
829 } while (NSEC2MSEC(gethrtime() - start
) < (timeout
* MILLISEC
));
837 libzfs_handle_t
*hdl
;
840 error
= libzfs_load_module(ZFS_DRIVER
);
846 if ((hdl
= calloc(1, sizeof (libzfs_handle_t
))) == NULL
) {
850 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
855 #ifdef HAVE_SETMNTENT
856 if ((hdl
->libzfs_mnttab
= setmntent(MNTTAB
, "r")) == NULL
) {
858 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "r")) == NULL
) {
860 (void) close(hdl
->libzfs_fd
);
865 hdl
->libzfs_sharetab
= fopen("/etc/dfs/sharetab", "r");
867 if (libzfs_core_init() != 0) {
868 (void) close(hdl
->libzfs_fd
);
869 (void) fclose(hdl
->libzfs_mnttab
);
870 (void) fclose(hdl
->libzfs_sharetab
);
877 zpool_feature_init();
878 libzfs_mnttab_init(hdl
);
884 libzfs_fini(libzfs_handle_t
*hdl
)
886 (void) close(hdl
->libzfs_fd
);
887 if (hdl
->libzfs_mnttab
)
888 #ifdef HAVE_SETMNTENT
889 (void) endmntent(hdl
->libzfs_mnttab
);
891 (void) fclose(hdl
->libzfs_mnttab
);
893 if (hdl
->libzfs_sharetab
)
894 (void) fclose(hdl
->libzfs_sharetab
);
895 zfs_uninit_libshare(hdl
);
896 zpool_free_handles(hdl
);
897 libzfs_fru_clear(hdl
, B_TRUE
);
898 namespace_clear(hdl
);
899 libzfs_mnttab_fini(hdl
);
905 zpool_get_handle(zpool_handle_t
*zhp
)
907 return (zhp
->zpool_hdl
);
911 zfs_get_handle(zfs_handle_t
*zhp
)
913 return (zhp
->zfs_hdl
);
917 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
919 return (zhp
->zpool_hdl
);
923 * Given a name, determine whether or not it's a valid path
924 * (starts with '/' or "./"). If so, walk the mnttab trying
925 * to match the device number. If not, treat the path as an
929 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
931 struct stat64 statbuf
;
932 struct extmnttab entry
;
935 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
937 * It's not a valid path, assume it's a name of type 'argtype'.
939 return (zfs_open(hdl
, path
, argtype
));
942 if (stat64(path
, &statbuf
) != 0) {
943 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
947 /* Reopen MNTTAB to prevent reading stale data from open file */
948 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
951 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
952 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
961 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
962 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
967 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
971 * Append partition suffix to an otherwise fully qualified device path.
972 * This is used to generate the name the full path as its stored in
973 * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length
974 * of 'path' will be returned on error a negative value is returned.
977 zfs_append_partition(char *path
, size_t max_len
)
979 int len
= strlen(path
);
981 if ((strncmp(path
, UDISK_ROOT
, strlen(UDISK_ROOT
)) == 0) ||
982 (strncmp(path
, ZVOL_ROOT
, strlen(ZVOL_ROOT
)) == 0)) {
983 if (len
+ 6 >= max_len
)
986 (void) strcat(path
, "-part1");
989 if (len
+ 2 >= max_len
)
992 if (isdigit(path
[len
-1])) {
993 (void) strcat(path
, "p1");
996 (void) strcat(path
, "1");
1005 * Given a shorthand device name check if a file by that name exists in any
1006 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
1007 * one is found, store its fully qualified path in the 'path' buffer passed
1008 * by the caller and return 0, otherwise return an error.
1011 zfs_resolve_shortname(const char *name
, char *path
, size_t len
)
1014 char *dir
, *env
, *envdup
;
1016 env
= getenv("ZPOOL_IMPORT_PATH");
1020 envdup
= strdup(env
);
1021 dir
= strtok(envdup
, ":");
1022 while (dir
&& error
) {
1023 (void) snprintf(path
, len
, "%s/%s", dir
, name
);
1024 error
= access(path
, F_OK
);
1025 dir
= strtok(NULL
, ":");
1029 for (i
= 0; i
< DEFAULT_IMPORT_PATH_SIZE
&& error
< 0; i
++) {
1030 (void) snprintf(path
, len
, "%s/%s",
1031 zpool_default_import_path
[i
], name
);
1032 error
= access(path
, F_OK
);
1036 return (error
? ENOENT
: 0);
1040 * Given a shorthand device name look for a match against 'cmp_name'. This
1041 * is done by checking all prefix expansions using either the default
1042 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
1043 * variable. Proper partition suffixes will be appended if this is a
1044 * whole disk. When a match is found 0 is returned otherwise ENOENT.
1047 zfs_strcmp_shortname(char *name
, char *cmp_name
, int wholedisk
)
1049 int path_len
, cmp_len
, i
= 0, error
= ENOENT
;
1050 char *dir
, *env
, *envdup
= NULL
;
1051 char path_name
[MAXPATHLEN
];
1053 cmp_len
= strlen(cmp_name
);
1054 env
= getenv("ZPOOL_IMPORT_PATH");
1057 envdup
= strdup(env
);
1058 dir
= strtok(envdup
, ":");
1060 dir
= zpool_default_import_path
[i
];
1064 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
1065 while (dir
[strlen(dir
)-1] == '/')
1066 dir
[strlen(dir
)-1] = '\0';
1068 path_len
= snprintf(path_name
, MAXPATHLEN
, "%s/%s", dir
, name
);
1070 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1072 if ((path_len
== cmp_len
) && strcmp(path_name
, cmp_name
) == 0) {
1078 dir
= strtok(NULL
, ":");
1079 } else if (++i
< DEFAULT_IMPORT_PATH_SIZE
) {
1080 dir
= zpool_default_import_path
[i
];
1093 * Given either a shorthand or fully qualified path name look for a match
1094 * against 'cmp'. The passed name will be expanded as needed for comparison
1095 * purposes and redundant slashes stripped to ensure an accurate match.
1098 zfs_strcmp_pathname(char *name
, char *cmp
, int wholedisk
)
1100 int path_len
, cmp_len
;
1101 char path_name
[MAXPATHLEN
];
1102 char cmp_name
[MAXPATHLEN
];
1105 /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
1106 memset(cmp_name
, 0, MAXPATHLEN
);
1108 dir
= strtok(dup
, "/");
1110 strlcat(cmp_name
, "/", sizeof (cmp_name
));
1111 strlcat(cmp_name
, dir
, sizeof (cmp_name
));
1112 dir
= strtok(NULL
, "/");
1117 return (zfs_strcmp_shortname(name
, cmp_name
, wholedisk
));
1119 (void) strlcpy(path_name
, name
, MAXPATHLEN
);
1120 path_len
= strlen(path_name
);
1121 cmp_len
= strlen(cmp_name
);
1124 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1129 if ((path_len
!= cmp_len
) || strcmp(path_name
, cmp_name
))
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
:
1429 str
= (recvd_value
== NULL
? "-" : recvd_value
);
1436 if (i
== (ZFS_GET_NCOLS
- 1) ||
1437 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1438 (void) printf("%s", str
);
1439 else if (cbp
->cb_scripted
)
1440 (void) printf("%s\t", str
);
1442 (void) printf("%-*s ",
1443 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1447 (void) printf("\n");
1451 * Given a numeric suffix, convert the value into a number of bits that the
1452 * resulting value must be shifted.
1455 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1457 const char *ends
= "BKMGTPEZ";
1462 for (i
= 0; i
< strlen(ends
); i
++) {
1463 if (toupper(buf
[0]) == ends
[i
])
1466 if (i
== strlen(ends
)) {
1468 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1469 "invalid numeric suffix '%s'"), buf
);
1474 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1475 * However, 'BB' and 'BiB' are disallowed.
1477 if (buf
[1] == '\0' ||
1478 (toupper(buf
[0]) != 'B' &&
1479 ((toupper(buf
[1]) == 'B' && buf
[2] == '\0') ||
1480 (toupper(buf
[1]) == 'I' && toupper(buf
[2]) == 'B' &&
1485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1486 "invalid numeric suffix '%s'"), buf
);
1491 * Convert a string of the form '100G' into a real number. Used when setting
1492 * properties or creating a volume. 'buf' is used to place an extended error
1493 * message for the caller to use.
1496 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1503 /* Check to see if this looks like a number. */
1504 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1506 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1507 "bad numeric value '%s'"), value
);
1511 /* Rely on strtoull() to process the numeric portion. */
1513 *num
= strtoull(value
, &end
, 10);
1516 * Check for ERANGE, which indicates that the value is too large to fit
1517 * in a 64-bit value.
1519 if (errno
== ERANGE
) {
1521 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1522 "numeric value is too large"));
1527 * If we have a decimal value, then do the computation with floating
1528 * point arithmetic. Otherwise, use standard arithmetic.
1531 double fval
= strtod(value
, &end
);
1533 if ((shift
= str2shift(hdl
, end
)) == -1)
1536 fval
*= pow(2, shift
);
1538 if (fval
> UINT64_MAX
) {
1540 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1541 "numeric value is too large"));
1545 *num
= (uint64_t)fval
;
1547 if ((shift
= str2shift(hdl
, end
)) == -1)
1550 /* Check for overflow */
1551 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1553 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1554 "numeric value is too large"));
1565 * Given a propname=value nvpair to set, parse any numeric properties
1566 * (index, boolean, etc) if they are specified as strings and add the
1567 * resulting nvpair to the returned nvlist.
1569 * At the DSL layer, all properties are either 64-bit numbers or strings.
1570 * We want the user to be able to ignore this fact and specify properties
1571 * as native values (numbers, for example) or as strings (to simplify
1572 * command line utilities). This also handles converting index types
1573 * (compression, checksum, etc) from strings to their on-disk index.
1576 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1577 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1580 data_type_t datatype
= nvpair_type(elem
);
1581 zprop_type_t proptype
;
1582 const char *propname
;
1584 boolean_t isnone
= B_FALSE
;
1586 if (type
== ZFS_TYPE_POOL
) {
1587 proptype
= zpool_prop_get_type(prop
);
1588 propname
= zpool_prop_to_name(prop
);
1590 proptype
= zfs_prop_get_type(prop
);
1591 propname
= zfs_prop_to_name(prop
);
1595 * Convert any properties to the internal DSL value types.
1601 case PROP_TYPE_STRING
:
1602 if (datatype
!= DATA_TYPE_STRING
) {
1603 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1604 "'%s' must be a string"), nvpair_name(elem
));
1607 (void) nvpair_value_string(elem
, svalp
);
1608 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1610 "'%s' is too long"), nvpair_name(elem
));
1615 case PROP_TYPE_NUMBER
:
1616 if (datatype
== DATA_TYPE_STRING
) {
1617 (void) nvpair_value_string(elem
, &value
);
1618 if (strcmp(value
, "none") == 0) {
1620 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1624 } else if (datatype
== DATA_TYPE_UINT64
) {
1625 (void) nvpair_value_uint64(elem
, ivalp
);
1627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1628 "'%s' must be a number"), nvpair_name(elem
));
1633 * Quota special: force 'none' and don't allow 0.
1635 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1636 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1638 "use 'none' to disable quota/refquota"));
1643 * Special handling for "*_limit=none". In this case it's not
1646 if ((type
& ZFS_TYPE_DATASET
) && isnone
&&
1647 (prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
1648 prop
== ZFS_PROP_SNAPSHOT_LIMIT
)) {
1649 *ivalp
= UINT64_MAX
;
1653 case PROP_TYPE_INDEX
:
1654 if (datatype
!= DATA_TYPE_STRING
) {
1655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1656 "'%s' must be a string"), nvpair_name(elem
));
1660 (void) nvpair_value_string(elem
, &value
);
1662 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1664 "'%s' must be one of '%s'"), propname
,
1665 zprop_values(prop
, type
));
1675 * Add the result to our return set of properties.
1677 if (*svalp
!= NULL
) {
1678 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1679 (void) no_memory(hdl
);
1683 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1684 (void) no_memory(hdl
);
1691 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1696 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1700 zprop_list_t
*entry
;
1702 prop
= zprop_name_to_prop(propname
, type
);
1704 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
, B_FALSE
))
1708 * When no property table entry can be found, return failure if
1709 * this is a pool property or if this isn't a user-defined
1712 if (prop
== ZPROP_INVAL
&& ((type
== ZFS_TYPE_POOL
&&
1713 !zpool_prop_feature(propname
) &&
1714 !zpool_prop_unsupported(propname
)) ||
1715 (type
== ZFS_TYPE_DATASET
&& !zfs_prop_user(propname
) &&
1716 !zfs_prop_userquota(propname
) && !zfs_prop_written(propname
)))) {
1717 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1718 "invalid property '%s'"), propname
);
1719 return (zfs_error(hdl
, EZFS_BADPROP
,
1720 dgettext(TEXT_DOMAIN
, "bad property list")));
1723 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1726 entry
->pl_prop
= prop
;
1727 if (prop
== ZPROP_INVAL
) {
1728 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) ==
1733 entry
->pl_width
= strlen(propname
);
1735 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1745 * Given a comma-separated list of properties, construct a property list
1746 * containing both user-defined and native properties. This function will
1747 * return a NULL list if 'all' is specified, which can later be expanded
1748 * by zprop_expand_list().
1751 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1757 * If 'all' is specified, return a NULL list.
1759 if (strcmp(props
, "all") == 0)
1763 * If no props were specified, return an error.
1765 if (props
[0] == '\0') {
1766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1767 "no properties specified"));
1768 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1769 "bad property list")));
1773 * It would be nice to use getsubopt() here, but the inclusion of column
1774 * aliases makes this more effort than it's worth.
1776 while (*props
!= '\0') {
1781 if ((p
= strchr(props
, ',')) == NULL
) {
1782 len
= strlen(props
);
1789 * Check for empty options.
1792 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1793 "empty property name"));
1794 return (zfs_error(hdl
, EZFS_BADPROP
,
1795 dgettext(TEXT_DOMAIN
, "bad property list")));
1799 * Check all regular property names.
1804 if (strcmp(props
, "space") == 0) {
1805 static char *spaceprops
[] = {
1806 "name", "avail", "used", "usedbysnapshots",
1807 "usedbydataset", "usedbyrefreservation",
1808 "usedbychildren", NULL
1812 for (i
= 0; spaceprops
[i
]; i
++) {
1813 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
1815 listp
= &(*listp
)->pl_next
;
1818 if (addlist(hdl
, props
, listp
, type
))
1820 listp
= &(*listp
)->pl_next
;
1832 zprop_free_list(zprop_list_t
*pl
)
1836 while (pl
!= NULL
) {
1838 free(pl
->pl_user_prop
);
1844 typedef struct expand_data
{
1845 zprop_list_t
**last
;
1846 libzfs_handle_t
*hdl
;
1851 zprop_expand_list_cb(int prop
, void *cb
)
1853 zprop_list_t
*entry
;
1854 expand_data_t
*edp
= cb
;
1856 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
1857 return (ZPROP_INVAL
);
1859 entry
->pl_prop
= prop
;
1860 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
1861 entry
->pl_all
= B_TRUE
;
1863 *(edp
->last
) = entry
;
1864 edp
->last
= &entry
->pl_next
;
1866 return (ZPROP_CONT
);
1870 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
1872 zprop_list_t
*entry
;
1873 zprop_list_t
**last
;
1878 * If this is the very first time we've been called for an 'all'
1879 * specification, expand the list to include all native
1888 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
1889 B_FALSE
, type
) == ZPROP_INVAL
)
1893 * Add 'name' to the beginning of the list, which is handled
1896 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1899 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1901 entry
->pl_width
= zprop_width(entry
->pl_prop
,
1902 &entry
->pl_fixed
, type
);
1903 entry
->pl_all
= B_TRUE
;
1904 entry
->pl_next
= *plp
;
1911 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
1914 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));