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"
54 #include <zfs_fletcher.h>
57 libzfs_errno(libzfs_handle_t
*hdl
)
59 return (hdl
->libzfs_error
);
63 libzfs_error_init(int error
)
67 return (dgettext(TEXT_DOMAIN
, "The ZFS modules are not "
68 "loaded.\nTry running '/sbin/modprobe zfs' as root "
71 return (dgettext(TEXT_DOMAIN
, "/dev/zfs and /proc/self/mounts "
72 "are required.\nTry running 'udevadm trigger' and 'mount "
73 "-t proc proc /proc' as root.\n"));
75 return (dgettext(TEXT_DOMAIN
, "The ZFS modules cannot be "
76 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
77 "root to manually load them.\n"));
79 return (dgettext(TEXT_DOMAIN
, "Permission denied the "
80 "ZFS utilities must be run as root.\n"));
82 return (dgettext(TEXT_DOMAIN
, "Failed to initialize the "
83 "libzfs library.\n"));
88 libzfs_error_action(libzfs_handle_t
*hdl
)
90 return (hdl
->libzfs_action
);
94 libzfs_error_description(libzfs_handle_t
*hdl
)
96 if (hdl
->libzfs_desc
[0] != '\0')
97 return (hdl
->libzfs_desc
);
99 switch (hdl
->libzfs_error
) {
101 return (dgettext(TEXT_DOMAIN
, "out of memory"));
103 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
104 case EZFS_PROPREADONLY
:
105 return (dgettext(TEXT_DOMAIN
, "read-only property"));
107 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
108 "datasets of this type"));
109 case EZFS_PROPNONINHERIT
:
110 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
112 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
114 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
115 "datasets of this type"));
117 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
119 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
121 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
123 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
124 case EZFS_DSREADONLY
:
125 return (dgettext(TEXT_DOMAIN
, "dataset is read-only"));
127 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
129 case EZFS_INVALIDNAME
:
130 return (dgettext(TEXT_DOMAIN
, "invalid name"));
131 case EZFS_BADRESTORE
:
132 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
135 return (dgettext(TEXT_DOMAIN
, "backup failed"));
137 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
139 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
141 return (dgettext(TEXT_DOMAIN
, "invalid device"));
142 case EZFS_NOREPLICAS
:
143 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
144 case EZFS_RESILVERING
:
145 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
146 case EZFS_BADVERSION
:
147 return (dgettext(TEXT_DOMAIN
, "unsupported version or "
149 case EZFS_POOLUNAVAIL
:
150 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
151 case EZFS_DEVOVERFLOW
:
152 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
154 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
155 case EZFS_CROSSTARGET
:
156 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
159 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
160 case EZFS_MOUNTFAILED
:
161 return (dgettext(TEXT_DOMAIN
, "mount failed"));
162 case EZFS_UMOUNTFAILED
:
163 return (dgettext(TEXT_DOMAIN
, "umount failed"));
164 case EZFS_UNSHARENFSFAILED
:
165 return (dgettext(TEXT_DOMAIN
, "unshare(1M) failed"));
166 case EZFS_SHARENFSFAILED
:
167 return (dgettext(TEXT_DOMAIN
, "share(1M) failed"));
168 case EZFS_UNSHARESMBFAILED
:
169 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
170 case EZFS_SHARESMBFAILED
:
171 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
173 return (dgettext(TEXT_DOMAIN
, "permission denied"));
175 return (dgettext(TEXT_DOMAIN
, "out of space"));
177 return (dgettext(TEXT_DOMAIN
, "bad address"));
179 return (dgettext(TEXT_DOMAIN
, "I/O error"));
181 return (dgettext(TEXT_DOMAIN
, "signal received"));
183 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
185 case EZFS_INVALCONFIG
:
186 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
188 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
190 return (dgettext(TEXT_DOMAIN
, "no history available"));
192 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
194 case EZFS_POOL_NOTSUP
:
195 return (dgettext(TEXT_DOMAIN
, "operation not supported "
196 "on this type of pool"));
197 case EZFS_POOL_INVALARG
:
198 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
199 "this pool operation"));
200 case EZFS_NAMETOOLONG
:
201 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
202 case EZFS_OPENFAILED
:
203 return (dgettext(TEXT_DOMAIN
, "open failed"));
205 return (dgettext(TEXT_DOMAIN
,
206 "disk capacity information could not be retrieved"));
207 case EZFS_LABELFAILED
:
208 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
210 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
212 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
213 case EZFS_BADPERMSET
:
214 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
215 case EZFS_NODELEGATION
:
216 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
217 "disabled on pool"));
219 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
221 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
222 case EZFS_VDEVNOTSUP
:
223 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
226 return (dgettext(TEXT_DOMAIN
, "operation not supported "
228 case EZFS_ACTIVE_SPARE
:
229 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
231 case EZFS_UNPLAYED_LOGS
:
232 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
234 case EZFS_REFTAG_RELE
:
235 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
236 case EZFS_REFTAG_HOLD
:
237 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
239 case EZFS_TAGTOOLONG
:
240 return (dgettext(TEXT_DOMAIN
, "tag too long"));
241 case EZFS_PIPEFAILED
:
242 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
243 case EZFS_THREADCREATEFAILED
:
244 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
245 case EZFS_POSTSPLIT_ONLINE
:
246 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
249 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
250 "use 'zpool scrub -s' to cancel current scrub"));
252 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
254 return (dgettext(TEXT_DOMAIN
, "unable to generate diffs"));
256 return (dgettext(TEXT_DOMAIN
, "invalid diff data"));
257 case EZFS_POOLREADONLY
:
258 return (dgettext(TEXT_DOMAIN
, "pool is read-only"));
260 return (dgettext(TEXT_DOMAIN
, "unknown error"));
262 assert(hdl
->libzfs_error
== 0);
263 return (dgettext(TEXT_DOMAIN
, "no error"));
269 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
275 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
277 hdl
->libzfs_desc_active
= 1;
283 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
285 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
287 hdl
->libzfs_error
= error
;
289 if (hdl
->libzfs_desc_active
)
290 hdl
->libzfs_desc_active
= 0;
292 hdl
->libzfs_desc
[0] = '\0';
294 if (hdl
->libzfs_printerr
) {
295 if (error
== EZFS_UNKNOWN
) {
296 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
297 "error: %s\n"), libzfs_error_description(hdl
));
301 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
302 libzfs_error_description(hdl
));
303 if (error
== EZFS_NOMEM
)
309 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
311 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
316 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
322 zfs_verror(hdl
, error
, fmt
, ap
);
330 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
336 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
340 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
344 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
348 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
352 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
360 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
362 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
367 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
373 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
382 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
386 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
387 "dataset does not exist"));
388 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
393 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
397 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
398 "dataset already exists"));
399 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
405 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
408 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
411 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
414 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
417 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
418 "pool I/O is currently suspended"));
419 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
422 zfs_error_aux(hdl
, strerror(error
));
423 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
432 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
434 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
439 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
445 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
452 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
457 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
458 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
462 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
463 "pool already exists"));
464 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
468 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
469 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
474 "one or more devices is currently unavailable"));
475 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
479 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
483 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
487 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
492 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
497 "pool I/O is currently suspended"));
498 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
502 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
505 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
506 "block size out of range or does not match"));
507 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
511 zfs_error_aux(hdl
, strerror(error
));
512 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
520 * Display an out of memory error message and abort the current program.
523 no_memory(libzfs_handle_t
*hdl
)
525 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
529 * A safe form of malloc() which will die if the allocation fails.
532 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
536 if ((data
= calloc(1, size
)) == NULL
)
537 (void) no_memory(hdl
);
543 * A safe form of asprintf() which will die if the allocation fails.
547 zfs_asprintf(libzfs_handle_t
*hdl
, const char *fmt
, ...)
555 err
= vasprintf(&ret
, fmt
, ap
);
560 (void) no_memory(hdl
);
566 * A safe form of realloc(), which also zeroes newly allocated space.
569 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
573 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
574 (void) no_memory(hdl
);
578 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
583 * A safe form of strdup() which will die if the allocation fails.
586 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
590 if ((ret
= strdup(str
)) == NULL
)
591 (void) no_memory(hdl
);
597 * Convert a number to an appropriately human-readable output.
600 zfs_nicenum_format(uint64_t num
, char *buf
, size_t buflen
,
601 enum zfs_nicenum_format format
)
606 const char *units
[3][7] = {
607 [ZFS_NICENUM_1024
] = {"", "K", "M", "G", "T", "P", "E"},
608 [ZFS_NICENUM_TIME
] = {"ns", "us", "ms", "s", "?", "?", "?"}
611 const int units_len
[] = {[ZFS_NICENUM_1024
] = 6,
612 [ZFS_NICENUM_TIME
] = 4};
614 const int k_unit
[] = { [ZFS_NICENUM_1024
] = 1024,
615 [ZFS_NICENUM_TIME
] = 1000};
619 if (format
== ZFS_NICENUM_RAW
) {
620 snprintf(buf
, buflen
, "%llu", (u_longlong_t
) num
);
625 while (n
>= k_unit
[format
] && index
< units_len
[format
]) {
630 u
= units
[format
][index
];
632 /* Don't print 0ns times */
633 if ((format
== ZFS_NICENUM_TIME
) && (num
== 0)) {
634 (void) snprintf(buf
, buflen
, "-");
635 } else if ((index
== 0) || ((num
%
636 (uint64_t) powl(k_unit
[format
], index
)) == 0)) {
638 * If this is an even multiple of the base, always display
639 * without any decimal precision.
641 (void) snprintf(buf
, buflen
, "%llu%s", (u_longlong_t
) n
, u
);
645 * We want to choose a precision that reflects the best choice
646 * for fitting in 5 characters. This can get rather tricky when
647 * we have numbers that are very close to an order of magnitude.
648 * For example, when displaying 10239 (which is really 9.999K),
649 * we want only a single place of precision for 10.0K. We could
650 * develop some complex heuristics for this, but it's much
651 * easier just to try each combination in turn.
654 for (i
= 2; i
>= 0; i
--) {
656 (uint64_t) powl(k_unit
[format
], index
);
659 * Don't print floating point values for time. Note,
660 * we use floor() instead of round() here, since
661 * round can result in undesirable results. For
662 * example, if "num" is in the range of
663 * 999500-999999, it will print out "1000us". This
664 * doesn't happen if we use floor().
666 if (format
== ZFS_NICENUM_TIME
) {
667 if (snprintf(buf
, buflen
, "%d%s",
668 (unsigned int) floor(val
), u
) <= 5)
672 if (snprintf(buf
, buflen
, "%.*f%s", i
,
681 * Convert a number to an appropriately human-readable output.
684 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
686 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_1024
);
690 * Convert a time to an appropriately human-readable output.
691 * @num: Time in nanoseconds
694 zfs_nicetime(uint64_t num
, char *buf
, size_t buflen
)
696 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_TIME
);
700 * Print out a raw number with correct column spacing
703 zfs_niceraw(uint64_t num
, char *buf
, size_t buflen
)
705 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_RAW
);
711 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
713 hdl
->libzfs_printerr
= printerr
;
717 libzfs_module_loaded(const char *module
)
719 const char path_prefix
[] = "/sys/module/";
722 memcpy(path
, path_prefix
, sizeof (path_prefix
) - 1);
723 strcpy(path
+ sizeof (path_prefix
) - 1, module
);
725 return (access(path
, F_OK
) == 0);
729 libzfs_run_process(const char *path
, char *argv
[], int flags
)
732 int error
, devnull_fd
;
736 devnull_fd
= open("/dev/null", O_WRONLY
);
741 if (!(flags
& STDOUT_VERBOSE
))
742 (void) dup2(devnull_fd
, STDOUT_FILENO
);
744 if (!(flags
& STDERR_VERBOSE
))
745 (void) dup2(devnull_fd
, STDERR_FILENO
);
749 (void) execvp(path
, argv
);
751 } else if (pid
> 0) {
754 while ((error
= waitpid(pid
, &status
, 0)) == -1 &&
756 if (error
< 0 || !WIFEXITED(status
))
759 return (WEXITSTATUS(status
));
766 * Verify the required ZFS_DEV device is available and optionally attempt
767 * to load the ZFS modules. Under normal circumstances the modules
768 * should already have been loaded by some external mechanism.
770 * Environment variables:
771 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
772 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
775 libzfs_load_module(const char *module
)
777 char *argv
[4] = {"/sbin/modprobe", "-q", (char *)module
, (char *)0};
778 char *load_str
, *timeout_str
;
779 long timeout
= 10; /* seconds */
780 long busy_timeout
= 10; /* milliseconds */
784 /* Optionally request module loading */
785 if (!libzfs_module_loaded(module
)) {
786 load_str
= getenv("ZFS_MODULE_LOADING");
788 if (!strncasecmp(load_str
, "YES", strlen("YES")) ||
789 !strncasecmp(load_str
, "ON", strlen("ON")))
795 if (load
&& libzfs_run_process("/sbin/modprobe", argv
, 0))
799 /* Module loading is synchronous it must be available */
800 if (!libzfs_module_loaded(module
))
804 * Device creation by udev is asynchronous and waiting may be
805 * required. Busy wait for 10ms and then fall back to polling every
806 * 10ms for the allowed timeout (default 10s, max 10m). This is
807 * done to optimize for the common case where the device is
808 * immediately available and to avoid penalizing the possible
809 * case where udev is slow or unable to create the device.
811 timeout_str
= getenv("ZFS_MODULE_TIMEOUT");
813 timeout
= strtol(timeout_str
, NULL
, 0);
814 timeout
= MAX(MIN(timeout
, (10 * 60)), 0); /* 0 <= N <= 600 */
819 fd
= open(ZFS_DEV
, O_RDWR
);
823 } else if (errno
!= ENOENT
) {
825 } else if (NSEC2MSEC(gethrtime() - start
) < busy_timeout
) {
828 usleep(10 * MILLISEC
);
830 } while (NSEC2MSEC(gethrtime() - start
) < (timeout
* MILLISEC
));
838 libzfs_handle_t
*hdl
;
841 error
= libzfs_load_module(ZFS_DRIVER
);
847 if ((hdl
= calloc(1, sizeof (libzfs_handle_t
))) == NULL
) {
851 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
856 #ifdef HAVE_SETMNTENT
857 if ((hdl
->libzfs_mnttab
= setmntent(MNTTAB
, "r")) == NULL
) {
859 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "r")) == NULL
) {
861 (void) close(hdl
->libzfs_fd
);
866 hdl
->libzfs_sharetab
= fopen("/etc/dfs/sharetab", "r");
868 if (libzfs_core_init() != 0) {
869 (void) close(hdl
->libzfs_fd
);
870 (void) fclose(hdl
->libzfs_mnttab
);
871 (void) fclose(hdl
->libzfs_sharetab
);
878 zpool_feature_init();
879 libzfs_mnttab_init(hdl
);
886 libzfs_fini(libzfs_handle_t
*hdl
)
888 (void) close(hdl
->libzfs_fd
);
889 if (hdl
->libzfs_mnttab
)
890 #ifdef HAVE_SETMNTENT
891 (void) endmntent(hdl
->libzfs_mnttab
);
893 (void) fclose(hdl
->libzfs_mnttab
);
895 if (hdl
->libzfs_sharetab
)
896 (void) fclose(hdl
->libzfs_sharetab
);
897 zfs_uninit_libshare(hdl
);
898 zpool_free_handles(hdl
);
899 libzfs_fru_clear(hdl
, B_TRUE
);
900 namespace_clear(hdl
);
901 libzfs_mnttab_fini(hdl
);
908 zpool_get_handle(zpool_handle_t
*zhp
)
910 return (zhp
->zpool_hdl
);
914 zfs_get_handle(zfs_handle_t
*zhp
)
916 return (zhp
->zfs_hdl
);
920 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
922 return (zhp
->zpool_hdl
);
926 * Given a name, determine whether or not it's a valid path
927 * (starts with '/' or "./"). If so, walk the mnttab trying
928 * to match the device number. If not, treat the path as an
932 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
934 struct stat64 statbuf
;
935 struct extmnttab entry
;
938 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
940 * It's not a valid path, assume it's a name of type 'argtype'.
942 return (zfs_open(hdl
, path
, argtype
));
945 if (stat64(path
, &statbuf
) != 0) {
946 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
950 /* Reopen MNTTAB to prevent reading stale data from open file */
951 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
954 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
955 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
964 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
965 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
970 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
974 * Append partition suffix to an otherwise fully qualified device path.
975 * This is used to generate the name the full path as its stored in
976 * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length
977 * of 'path' will be returned on error a negative value is returned.
980 zfs_append_partition(char *path
, size_t max_len
)
982 int len
= strlen(path
);
984 if ((strncmp(path
, UDISK_ROOT
, strlen(UDISK_ROOT
)) == 0) ||
985 (strncmp(path
, ZVOL_ROOT
, strlen(ZVOL_ROOT
)) == 0)) {
986 if (len
+ 6 >= max_len
)
989 (void) strcat(path
, "-part1");
992 if (len
+ 2 >= max_len
)
995 if (isdigit(path
[len
-1])) {
996 (void) strcat(path
, "p1");
999 (void) strcat(path
, "1");
1008 * Given a shorthand device name check if a file by that name exists in any
1009 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
1010 * one is found, store its fully qualified path in the 'path' buffer passed
1011 * by the caller and return 0, otherwise return an error.
1014 zfs_resolve_shortname(const char *name
, char *path
, size_t len
)
1017 char *dir
, *env
, *envdup
;
1019 env
= getenv("ZPOOL_IMPORT_PATH");
1023 envdup
= strdup(env
);
1024 dir
= strtok(envdup
, ":");
1025 while (dir
&& error
) {
1026 (void) snprintf(path
, len
, "%s/%s", dir
, name
);
1027 error
= access(path
, F_OK
);
1028 dir
= strtok(NULL
, ":");
1032 for (i
= 0; i
< DEFAULT_IMPORT_PATH_SIZE
&& error
< 0; i
++) {
1033 (void) snprintf(path
, len
, "%s/%s",
1034 zpool_default_import_path
[i
], name
);
1035 error
= access(path
, F_OK
);
1039 return (error
? ENOENT
: 0);
1043 * Given a shorthand device name look for a match against 'cmp_name'. This
1044 * is done by checking all prefix expansions using either the default
1045 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
1046 * variable. Proper partition suffixes will be appended if this is a
1047 * whole disk. When a match is found 0 is returned otherwise ENOENT.
1050 zfs_strcmp_shortname(char *name
, char *cmp_name
, int wholedisk
)
1052 int path_len
, cmp_len
, i
= 0, error
= ENOENT
;
1053 char *dir
, *env
, *envdup
= NULL
;
1054 char path_name
[MAXPATHLEN
];
1056 cmp_len
= strlen(cmp_name
);
1057 env
= getenv("ZPOOL_IMPORT_PATH");
1060 envdup
= strdup(env
);
1061 dir
= strtok(envdup
, ":");
1063 dir
= zpool_default_import_path
[i
];
1067 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
1068 while (dir
[strlen(dir
)-1] == '/')
1069 dir
[strlen(dir
)-1] = '\0';
1071 path_len
= snprintf(path_name
, MAXPATHLEN
, "%s/%s", dir
, name
);
1073 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1075 if ((path_len
== cmp_len
) && strcmp(path_name
, cmp_name
) == 0) {
1081 dir
= strtok(NULL
, ":");
1082 } else if (++i
< DEFAULT_IMPORT_PATH_SIZE
) {
1083 dir
= zpool_default_import_path
[i
];
1096 * Given either a shorthand or fully qualified path name look for a match
1097 * against 'cmp'. The passed name will be expanded as needed for comparison
1098 * purposes and redundant slashes stripped to ensure an accurate match.
1101 zfs_strcmp_pathname(char *name
, char *cmp
, int wholedisk
)
1103 int path_len
, cmp_len
;
1104 char path_name
[MAXPATHLEN
];
1105 char cmp_name
[MAXPATHLEN
];
1108 /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
1109 memset(cmp_name
, 0, MAXPATHLEN
);
1111 dir
= strtok(dup
, "/");
1113 strlcat(cmp_name
, "/", sizeof (cmp_name
));
1114 strlcat(cmp_name
, dir
, sizeof (cmp_name
));
1115 dir
= strtok(NULL
, "/");
1120 return (zfs_strcmp_shortname(name
, cmp_name
, wholedisk
));
1122 (void) strlcpy(path_name
, name
, MAXPATHLEN
);
1123 path_len
= strlen(path_name
);
1124 cmp_len
= strlen(cmp_name
);
1127 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
1132 if ((path_len
!= cmp_len
) || strcmp(path_name
, cmp_name
))
1139 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1143 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
1147 zc
->zc_nvlist_dst_size
= len
;
1149 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1150 if (zc
->zc_nvlist_dst
== 0)
1157 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1158 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1159 * filled in by the kernel to indicate the actual required size.
1162 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
1164 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1166 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1167 if (zc
->zc_nvlist_dst
== 0)
1174 * Called to free the src and dst nvlists stored in the command structure.
1177 zcmd_free_nvlists(zfs_cmd_t
*zc
)
1179 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
1180 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
1181 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1182 zc
->zc_nvlist_conf
= 0;
1183 zc
->zc_nvlist_src
= 0;
1184 zc
->zc_nvlist_dst
= 0;
1188 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
1194 verify(nvlist_size(nvl
, &len
, NV_ENCODE_NATIVE
) == 0);
1196 if ((packed
= zfs_alloc(hdl
, len
)) == NULL
)
1199 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
1201 *outnv
= (uint64_t)(uintptr_t)packed
;
1208 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1210 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
1211 &zc
->zc_nvlist_conf_size
, nvl
));
1215 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1217 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
1218 &zc
->zc_nvlist_src_size
, nvl
));
1222 * Unpacks an nvlist from the ZFS ioctl command structure.
1225 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
1227 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
1228 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
1229 return (no_memory(hdl
));
1235 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
1237 return (ioctl(hdl
->libzfs_fd
, request
, zc
));
1241 * ================================================================
1242 * API shared by zfs and zpool property management
1243 * ================================================================
1247 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
1249 zprop_list_t
*pl
= cbp
->cb_proplist
;
1254 cbp
->cb_first
= B_FALSE
;
1255 if (cbp
->cb_scripted
)
1259 * Start with the length of the column headers.
1261 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
1262 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
1264 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
1266 cbp
->cb_colwidths
[GET_COL_RECVD
] = strlen(dgettext(TEXT_DOMAIN
,
1268 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
1271 /* first property is always NAME */
1272 assert(cbp
->cb_proplist
->pl_prop
==
1273 ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
: ZFS_PROP_NAME
));
1276 * Go through and calculate the widths for each column. For the
1277 * 'source' column, we kludge it up by taking the worst-case scenario of
1278 * inheriting from the longest name. This is acceptable because in the
1279 * majority of cases 'SOURCE' is the last column displayed, and we don't
1280 * use the width anyway. Note that the 'VALUE' column can be oversized,
1281 * if the name of the property is much longer than any values we find.
1283 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
1287 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1288 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
1289 zpool_prop_to_name(pl
->pl_prop
) :
1290 zfs_prop_to_name(pl
->pl_prop
);
1292 len
= strlen(propname
);
1293 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1294 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1296 len
= strlen(pl
->pl_user_prop
);
1297 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1298 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1302 * 'VALUE' column. The first property is always the 'name'
1303 * property that was tacked on either by /sbin/zfs's
1304 * zfs_do_get() or when calling zprop_expand_list(), so we
1305 * ignore its width. If the user specified the name property
1306 * to display, then it will be later in the list in any case.
1308 if (pl
!= cbp
->cb_proplist
&&
1309 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
1310 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
1312 /* 'RECEIVED' column. */
1313 if (pl
!= cbp
->cb_proplist
&&
1314 pl
->pl_recvd_width
> cbp
->cb_colwidths
[GET_COL_RECVD
])
1315 cbp
->cb_colwidths
[GET_COL_RECVD
] = pl
->pl_recvd_width
;
1318 * 'NAME' and 'SOURCE' columns
1320 if (pl
->pl_prop
== (type
== ZFS_TYPE_POOL
? ZPOOL_PROP_NAME
:
1322 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_NAME
]) {
1323 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
1324 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
1325 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
1330 * Now go through and print the headers.
1332 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1333 switch (cbp
->cb_columns
[i
]) {
1335 title
= dgettext(TEXT_DOMAIN
, "NAME");
1337 case GET_COL_PROPERTY
:
1338 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
1341 title
= dgettext(TEXT_DOMAIN
, "VALUE");
1344 title
= dgettext(TEXT_DOMAIN
, "RECEIVED");
1346 case GET_COL_SOURCE
:
1347 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
1353 if (title
!= NULL
) {
1354 if (i
== (ZFS_GET_NCOLS
- 1) ||
1355 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1356 (void) printf("%s", title
);
1358 (void) printf("%-*s ",
1359 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1363 (void) printf("\n");
1367 * Display a single line of output, according to the settings in the callback
1371 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
1372 const char *propname
, const char *value
, zprop_source_t sourcetype
,
1373 const char *source
, const char *recvd_value
)
1376 const char *str
= NULL
;
1380 * Ignore those source types that the user has chosen to ignore.
1382 if ((sourcetype
& cbp
->cb_sources
) == 0)
1386 zprop_print_headers(cbp
, cbp
->cb_type
);
1388 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1389 switch (cbp
->cb_columns
[i
]) {
1394 case GET_COL_PROPERTY
:
1402 case GET_COL_SOURCE
:
1403 switch (sourcetype
) {
1404 case ZPROP_SRC_NONE
:
1408 case ZPROP_SRC_DEFAULT
:
1412 case ZPROP_SRC_LOCAL
:
1416 case ZPROP_SRC_TEMPORARY
:
1420 case ZPROP_SRC_INHERITED
:
1421 (void) snprintf(buf
, sizeof (buf
),
1422 "inherited from %s", source
);
1425 case ZPROP_SRC_RECEIVED
:
1432 str
= (recvd_value
== NULL
? "-" : recvd_value
);
1439 if (i
== (ZFS_GET_NCOLS
- 1) ||
1440 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1441 (void) printf("%s", str
);
1442 else if (cbp
->cb_scripted
)
1443 (void) printf("%s\t", str
);
1445 (void) printf("%-*s ",
1446 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1450 (void) printf("\n");
1454 * Given a numeric suffix, convert the value into a number of bits that the
1455 * resulting value must be shifted.
1458 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1460 const char *ends
= "BKMGTPEZ";
1465 for (i
= 0; i
< strlen(ends
); i
++) {
1466 if (toupper(buf
[0]) == ends
[i
])
1469 if (i
== strlen(ends
)) {
1471 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1472 "invalid numeric suffix '%s'"), buf
);
1477 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1478 * However, 'BB' and 'BiB' are disallowed.
1480 if (buf
[1] == '\0' ||
1481 (toupper(buf
[0]) != 'B' &&
1482 ((toupper(buf
[1]) == 'B' && buf
[2] == '\0') ||
1483 (toupper(buf
[1]) == 'I' && toupper(buf
[2]) == 'B' &&
1488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1489 "invalid numeric suffix '%s'"), buf
);
1494 * Convert a string of the form '100G' into a real number. Used when setting
1495 * properties or creating a volume. 'buf' is used to place an extended error
1496 * message for the caller to use.
1499 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1506 /* Check to see if this looks like a number. */
1507 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1509 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1510 "bad numeric value '%s'"), value
);
1514 /* Rely on strtoull() to process the numeric portion. */
1516 *num
= strtoull(value
, &end
, 10);
1519 * Check for ERANGE, which indicates that the value is too large to fit
1520 * in a 64-bit value.
1522 if (errno
== ERANGE
) {
1524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1525 "numeric value is too large"));
1530 * If we have a decimal value, then do the computation with floating
1531 * point arithmetic. Otherwise, use standard arithmetic.
1534 double fval
= strtod(value
, &end
);
1536 if ((shift
= str2shift(hdl
, end
)) == -1)
1539 fval
*= pow(2, shift
);
1541 if (fval
> UINT64_MAX
) {
1543 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1544 "numeric value is too large"));
1548 *num
= (uint64_t)fval
;
1550 if ((shift
= str2shift(hdl
, end
)) == -1)
1553 /* Check for overflow */
1554 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1557 "numeric value is too large"));
1568 * Given a propname=value nvpair to set, parse any numeric properties
1569 * (index, boolean, etc) if they are specified as strings and add the
1570 * resulting nvpair to the returned nvlist.
1572 * At the DSL layer, all properties are either 64-bit numbers or strings.
1573 * We want the user to be able to ignore this fact and specify properties
1574 * as native values (numbers, for example) or as strings (to simplify
1575 * command line utilities). This also handles converting index types
1576 * (compression, checksum, etc) from strings to their on-disk index.
1579 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1580 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1583 data_type_t datatype
= nvpair_type(elem
);
1584 zprop_type_t proptype
;
1585 const char *propname
;
1587 boolean_t isnone
= B_FALSE
;
1589 if (type
== ZFS_TYPE_POOL
) {
1590 proptype
= zpool_prop_get_type(prop
);
1591 propname
= zpool_prop_to_name(prop
);
1593 proptype
= zfs_prop_get_type(prop
);
1594 propname
= zfs_prop_to_name(prop
);
1598 * Convert any properties to the internal DSL value types.
1604 case PROP_TYPE_STRING
:
1605 if (datatype
!= DATA_TYPE_STRING
) {
1606 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1607 "'%s' must be a string"), nvpair_name(elem
));
1610 (void) nvpair_value_string(elem
, svalp
);
1611 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1613 "'%s' is too long"), nvpair_name(elem
));
1618 case PROP_TYPE_NUMBER
:
1619 if (datatype
== DATA_TYPE_STRING
) {
1620 (void) nvpair_value_string(elem
, &value
);
1621 if (strcmp(value
, "none") == 0) {
1623 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1627 } else if (datatype
== DATA_TYPE_UINT64
) {
1628 (void) nvpair_value_uint64(elem
, ivalp
);
1630 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1631 "'%s' must be a number"), nvpair_name(elem
));
1636 * Quota special: force 'none' and don't allow 0.
1638 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1639 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1640 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1641 "use 'none' to disable quota/refquota"));
1646 * Special handling for "*_limit=none". In this case it's not
1649 if ((type
& ZFS_TYPE_DATASET
) && isnone
&&
1650 (prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
1651 prop
== ZFS_PROP_SNAPSHOT_LIMIT
)) {
1652 *ivalp
= UINT64_MAX
;
1656 case PROP_TYPE_INDEX
:
1657 if (datatype
!= DATA_TYPE_STRING
) {
1658 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1659 "'%s' must be a string"), nvpair_name(elem
));
1663 (void) nvpair_value_string(elem
, &value
);
1665 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1666 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1667 "'%s' must be one of '%s'"), propname
,
1668 zprop_values(prop
, type
));
1678 * Add the result to our return set of properties.
1680 if (*svalp
!= NULL
) {
1681 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1682 (void) no_memory(hdl
);
1686 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1687 (void) no_memory(hdl
);
1694 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1699 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1703 zprop_list_t
*entry
;
1705 prop
= zprop_name_to_prop(propname
, type
);
1707 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
, B_FALSE
))
1711 * When no property table entry can be found, return failure if
1712 * this is a pool property or if this isn't a user-defined
1715 if (prop
== ZPROP_INVAL
&& ((type
== ZFS_TYPE_POOL
&&
1716 !zpool_prop_feature(propname
) &&
1717 !zpool_prop_unsupported(propname
)) ||
1718 (type
== ZFS_TYPE_DATASET
&& !zfs_prop_user(propname
) &&
1719 !zfs_prop_userquota(propname
) && !zfs_prop_written(propname
)))) {
1720 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1721 "invalid property '%s'"), propname
);
1722 return (zfs_error(hdl
, EZFS_BADPROP
,
1723 dgettext(TEXT_DOMAIN
, "bad property list")));
1726 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1729 entry
->pl_prop
= prop
;
1730 if (prop
== ZPROP_INVAL
) {
1731 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) ==
1736 entry
->pl_width
= strlen(propname
);
1738 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1748 * Given a comma-separated list of properties, construct a property list
1749 * containing both user-defined and native properties. This function will
1750 * return a NULL list if 'all' is specified, which can later be expanded
1751 * by zprop_expand_list().
1754 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1760 * If 'all' is specified, return a NULL list.
1762 if (strcmp(props
, "all") == 0)
1766 * If no props were specified, return an error.
1768 if (props
[0] == '\0') {
1769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1770 "no properties specified"));
1771 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1772 "bad property list")));
1776 * It would be nice to use getsubopt() here, but the inclusion of column
1777 * aliases makes this more effort than it's worth.
1779 while (*props
!= '\0') {
1784 if ((p
= strchr(props
, ',')) == NULL
) {
1785 len
= strlen(props
);
1792 * Check for empty options.
1795 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1796 "empty property name"));
1797 return (zfs_error(hdl
, EZFS_BADPROP
,
1798 dgettext(TEXT_DOMAIN
, "bad property list")));
1802 * Check all regular property names.
1807 if (strcmp(props
, "space") == 0) {
1808 static char *spaceprops
[] = {
1809 "name", "avail", "used", "usedbysnapshots",
1810 "usedbydataset", "usedbyrefreservation",
1811 "usedbychildren", NULL
1815 for (i
= 0; spaceprops
[i
]; i
++) {
1816 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
1818 listp
= &(*listp
)->pl_next
;
1821 if (addlist(hdl
, props
, listp
, type
))
1823 listp
= &(*listp
)->pl_next
;
1835 zprop_free_list(zprop_list_t
*pl
)
1839 while (pl
!= NULL
) {
1841 free(pl
->pl_user_prop
);
1847 typedef struct expand_data
{
1848 zprop_list_t
**last
;
1849 libzfs_handle_t
*hdl
;
1854 zprop_expand_list_cb(int prop
, void *cb
)
1856 zprop_list_t
*entry
;
1857 expand_data_t
*edp
= cb
;
1859 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
1860 return (ZPROP_INVAL
);
1862 entry
->pl_prop
= prop
;
1863 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
1864 entry
->pl_all
= B_TRUE
;
1866 *(edp
->last
) = entry
;
1867 edp
->last
= &entry
->pl_next
;
1869 return (ZPROP_CONT
);
1873 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
1875 zprop_list_t
*entry
;
1876 zprop_list_t
**last
;
1881 * If this is the very first time we've been called for an 'all'
1882 * specification, expand the list to include all native
1891 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
1892 B_FALSE
, type
) == ZPROP_INVAL
)
1896 * Add 'name' to the beginning of the list, which is handled
1899 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1902 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1904 entry
->pl_width
= zprop_width(entry
->pl_prop
,
1905 &entry
->pl_fixed
, type
);
1906 entry
->pl_all
= B_TRUE
;
1907 entry
->pl_next
= *plp
;
1914 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
1917 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));