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(ZFS_SHARETAB, "r");
868 if (libzfs_core_init() != 0) {
869 (void) close(hdl->libzfs_fd);
870 (void) fclose(hdl->libzfs_mnttab);
871 if (hdl->libzfs_sharetab)
872 (void) fclose(hdl->libzfs_sharetab);
879 zpool_feature_init();
880 libzfs_mnttab_init(hdl);
887 libzfs_fini(libzfs_handle_t *hdl)
889 (void) close(hdl->libzfs_fd);
890 if (hdl->libzfs_mnttab)
891 #ifdef HAVE_SETMNTENT
892 (void) endmntent(hdl->libzfs_mnttab);
894 (void) fclose(hdl->libzfs_mnttab);
896 if (hdl->libzfs_sharetab)
897 (void) fclose(hdl->libzfs_sharetab);
898 zfs_uninit_libshare(hdl);
899 zpool_free_handles(hdl);
900 libzfs_fru_clear(hdl, B_TRUE);
901 namespace_clear(hdl);
902 libzfs_mnttab_fini(hdl);
909 zpool_get_handle(zpool_handle_t *zhp)
911 return (zhp->zpool_hdl);
915 zfs_get_handle(zfs_handle_t *zhp)
917 return (zhp->zfs_hdl);
921 zfs_get_pool_handle(const zfs_handle_t *zhp)
923 return (zhp->zpool_hdl);
927 * Given a name, determine whether or not it's a valid path
928 * (starts with '/' or "./"). If so, walk the mnttab trying
929 * to match the device number. If not, treat the path as an
930 * fs/vol/snap/bkmark name.
933 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
935 struct stat64 statbuf;
936 struct extmnttab entry;
939 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
941 * It's not a valid path, assume it's a name of type 'argtype'.
943 return (zfs_open(hdl, path, argtype));
946 if (stat64(path, &statbuf) != 0) {
947 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
951 /* Reopen MNTTAB to prevent reading stale data from open file */
952 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
955 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
956 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
965 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
966 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
971 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
975 * Append partition suffix to an otherwise fully qualified device path.
976 * This is used to generate the name the full path as its stored in
977 * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length
978 * of 'path' will be returned on error a negative value is returned.
981 zfs_append_partition(char *path, size_t max_len)
983 int len = strlen(path);
985 if ((strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) ||
986 (strncmp(path, ZVOL_ROOT, strlen(ZVOL_ROOT)) == 0)) {
987 if (len + 6 >= max_len)
990 (void) strcat(path, "-part1");
993 if (len + 2 >= max_len)
996 if (isdigit(path[len-1])) {
997 (void) strcat(path, "p1");
1000 (void) strcat(path, "1");
1009 * Given a shorthand device name check if a file by that name exists in any
1010 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
1011 * one is found, store its fully qualified path in the 'path' buffer passed
1012 * by the caller and return 0, otherwise return an error.
1015 zfs_resolve_shortname(const char *name, char *path, size_t len)
1018 char *dir, *env, *envdup;
1020 env = getenv("ZPOOL_IMPORT_PATH");
1024 envdup = strdup(env);
1025 dir = strtok(envdup, ":");
1026 while (dir && error) {
1027 (void) snprintf(path, len, "%s/%s", dir, name);
1028 error = access(path, F_OK);
1029 dir = strtok(NULL, ":");
1033 for (i = 0; i < DEFAULT_IMPORT_PATH_SIZE && error < 0; i++) {
1034 (void) snprintf(path, len, "%s/%s",
1035 zpool_default_import_path[i], name);
1036 error = access(path, F_OK);
1040 return (error ? ENOENT : 0);
1044 * Given a shorthand device name look for a match against 'cmp_name'. This
1045 * is done by checking all prefix expansions using either the default
1046 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
1047 * variable. Proper partition suffixes will be appended if this is a
1048 * whole disk. When a match is found 0 is returned otherwise ENOENT.
1051 zfs_strcmp_shortname(char *name, char *cmp_name, int wholedisk)
1053 int path_len, cmp_len, i = 0, error = ENOENT;
1054 char *dir, *env, *envdup = NULL;
1055 char path_name[MAXPATHLEN];
1057 cmp_len = strlen(cmp_name);
1058 env = getenv("ZPOOL_IMPORT_PATH");
1061 envdup = strdup(env);
1062 dir = strtok(envdup, ":");
1064 dir = zpool_default_import_path[i];
1068 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
1069 while (dir[strlen(dir)-1] == '/')
1070 dir[strlen(dir)-1] = '\0';
1072 path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name);
1074 path_len = zfs_append_partition(path_name, MAXPATHLEN);
1076 if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) {
1082 dir = strtok(NULL, ":");
1083 } else if (++i < DEFAULT_IMPORT_PATH_SIZE) {
1084 dir = zpool_default_import_path[i];
1097 * Given either a shorthand or fully qualified path name look for a match
1098 * against 'cmp'. The passed name will be expanded as needed for comparison
1099 * purposes and redundant slashes stripped to ensure an accurate match.
1102 zfs_strcmp_pathname(char *name, char *cmp, int wholedisk)
1104 int path_len, cmp_len;
1105 char path_name[MAXPATHLEN];
1106 char cmp_name[MAXPATHLEN];
1109 /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
1110 memset(cmp_name, 0, MAXPATHLEN);
1112 dir = strtok(dup, "/");
1114 strlcat(cmp_name, "/", sizeof (cmp_name));
1115 strlcat(cmp_name, dir, sizeof (cmp_name));
1116 dir = strtok(NULL, "/");
1121 return (zfs_strcmp_shortname(name, cmp_name, wholedisk));
1123 (void) strlcpy(path_name, name, MAXPATHLEN);
1124 path_len = strlen(path_name);
1125 cmp_len = strlen(cmp_name);
1128 path_len = zfs_append_partition(path_name, MAXPATHLEN);
1133 if ((path_len != cmp_len) || strcmp(path_name, cmp_name))
1140 * Given a full path to a device determine if that device appears in the
1141 * import search path. If it does return the first match and store the
1142 * index in the passed 'order' variable, otherwise return an error.
1145 zfs_path_order(char *name, int *order)
1147 int i = 0, error = ENOENT;
1148 char *dir, *env, *envdup;
1150 env = getenv("ZPOOL_IMPORT_PATH");
1152 envdup = strdup(env);
1153 dir = strtok(envdup, ":");
1155 if (strncmp(name, dir, strlen(dir)) == 0) {
1160 dir = strtok(NULL, ":");
1165 for (i = 0; i < DEFAULT_IMPORT_PATH_SIZE; i++) {
1166 if (strncmp(name, zpool_default_import_path[i],
1167 strlen(zpool_default_import_path[i])) == 0) {
1179 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1183 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1187 zc->zc_nvlist_dst_size = len;
1189 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1190 if (zc->zc_nvlist_dst == 0)
1197 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1198 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1199 * filled in by the kernel to indicate the actual required size.
1202 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1204 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1206 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1207 if (zc->zc_nvlist_dst == 0)
1214 * Called to free the src and dst nvlists stored in the command structure.
1217 zcmd_free_nvlists(zfs_cmd_t *zc)
1219 free((void *)(uintptr_t)zc->zc_nvlist_conf);
1220 free((void *)(uintptr_t)zc->zc_nvlist_src);
1221 free((void *)(uintptr_t)zc->zc_nvlist_dst);
1222 zc->zc_nvlist_conf = 0;
1223 zc->zc_nvlist_src = 0;
1224 zc->zc_nvlist_dst = 0;
1228 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1234 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
1236 if ((packed = zfs_alloc(hdl, len)) == NULL)
1239 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1241 *outnv = (uint64_t)(uintptr_t)packed;
1248 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1250 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1251 &zc->zc_nvlist_conf_size, nvl));
1255 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1257 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1258 &zc->zc_nvlist_src_size, nvl));
1262 * Unpacks an nvlist from the ZFS ioctl command structure.
1265 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1267 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1268 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1269 return (no_memory(hdl));
1275 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
1277 return (ioctl(hdl->libzfs_fd, request, zc));
1281 * ================================================================
1282 * API shared by zfs and zpool property management
1283 * ================================================================
1287 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1289 zprop_list_t *pl = cbp->cb_proplist;
1294 cbp->cb_first = B_FALSE;
1295 if (cbp->cb_scripted)
1299 * Start with the length of the column headers.
1301 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1302 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1304 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1306 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1308 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1311 /* first property is always NAME */
1312 assert(cbp->cb_proplist->pl_prop ==
1313 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME));
1316 * Go through and calculate the widths for each column. For the
1317 * 'source' column, we kludge it up by taking the worst-case scenario of
1318 * inheriting from the longest name. This is acceptable because in the
1319 * majority of cases 'SOURCE' is the last column displayed, and we don't
1320 * use the width anyway. Note that the 'VALUE' column can be oversized,
1321 * if the name of the property is much longer than any values we find.
1323 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1327 if (pl->pl_prop != ZPROP_INVAL) {
1328 const char *propname = (type == ZFS_TYPE_POOL) ?
1329 zpool_prop_to_name(pl->pl_prop) :
1330 zfs_prop_to_name(pl->pl_prop);
1332 len = strlen(propname);
1333 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1334 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1336 len = strlen(pl->pl_user_prop);
1337 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1338 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1342 * 'VALUE' column. The first property is always the 'name'
1343 * property that was tacked on either by /sbin/zfs's
1344 * zfs_do_get() or when calling zprop_expand_list(), so we
1345 * ignore its width. If the user specified the name property
1346 * to display, then it will be later in the list in any case.
1348 if (pl != cbp->cb_proplist &&
1349 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1350 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1352 /* 'RECEIVED' column. */
1353 if (pl != cbp->cb_proplist &&
1354 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1355 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1358 * 'NAME' and 'SOURCE' columns
1360 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1362 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1363 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1364 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1365 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1370 * Now go through and print the headers.
1372 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1373 switch (cbp->cb_columns[i]) {
1375 title = dgettext(TEXT_DOMAIN, "NAME");
1377 case GET_COL_PROPERTY:
1378 title = dgettext(TEXT_DOMAIN, "PROPERTY");
1381 title = dgettext(TEXT_DOMAIN, "VALUE");
1384 title = dgettext(TEXT_DOMAIN, "RECEIVED");
1386 case GET_COL_SOURCE:
1387 title = dgettext(TEXT_DOMAIN, "SOURCE");
1393 if (title != NULL) {
1394 if (i == (ZFS_GET_NCOLS - 1) ||
1395 cbp->cb_columns[i + 1] == GET_COL_NONE)
1396 (void) printf("%s", title);
1398 (void) printf("%-*s ",
1399 cbp->cb_colwidths[cbp->cb_columns[i]],
1403 (void) printf("\n");
1407 * Display a single line of output, according to the settings in the callback
1411 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1412 const char *propname, const char *value, zprop_source_t sourcetype,
1413 const char *source, const char *recvd_value)
1416 const char *str = NULL;
1420 * Ignore those source types that the user has chosen to ignore.
1422 if ((sourcetype & cbp->cb_sources) == 0)
1426 zprop_print_headers(cbp, cbp->cb_type);
1428 for (i = 0; i < ZFS_GET_NCOLS; i++) {
1429 switch (cbp->cb_columns[i]) {
1434 case GET_COL_PROPERTY:
1442 case GET_COL_SOURCE:
1443 switch (sourcetype) {
1444 case ZPROP_SRC_NONE:
1448 case ZPROP_SRC_DEFAULT:
1452 case ZPROP_SRC_LOCAL:
1456 case ZPROP_SRC_TEMPORARY:
1460 case ZPROP_SRC_INHERITED:
1461 (void) snprintf(buf, sizeof (buf),
1462 "inherited from %s", source);
1465 case ZPROP_SRC_RECEIVED:
1472 str = (recvd_value == NULL ? "-" : recvd_value);
1479 if (i == (ZFS_GET_NCOLS - 1) ||
1480 cbp->cb_columns[i + 1] == GET_COL_NONE)
1481 (void) printf("%s", str);
1482 else if (cbp->cb_scripted)
1483 (void) printf("%s\t", str);
1485 (void) printf("%-*s ",
1486 cbp->cb_colwidths[cbp->cb_columns[i]],
1490 (void) printf("\n");
1494 * Given a numeric suffix, convert the value into a number of bits that the
1495 * resulting value must be shifted.
1498 str2shift(libzfs_handle_t *hdl, const char *buf)
1500 const char *ends = "BKMGTPEZ";
1505 for (i = 0; i < strlen(ends); i++) {
1506 if (toupper(buf[0]) == ends[i])
1509 if (i == strlen(ends)) {
1511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512 "invalid numeric suffix '%s'"), buf);
1517 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1518 * However, 'BB' and 'BiB' are disallowed.
1520 if (buf[1] == '\0' ||
1521 (toupper(buf[0]) != 'B' &&
1522 ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1523 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1529 "invalid numeric suffix '%s'"), buf);
1534 * Convert a string of the form '100G' into a real number. Used when setting
1535 * properties or creating a volume. 'buf' is used to place an extended error
1536 * message for the caller to use.
1539 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1546 /* Check to see if this looks like a number. */
1547 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1550 "bad numeric value '%s'"), value);
1554 /* Rely on strtoull() to process the numeric portion. */
1556 *num = strtoull(value, &end, 10);
1559 * Check for ERANGE, which indicates that the value is too large to fit
1560 * in a 64-bit value.
1562 if (errno == ERANGE) {
1564 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1565 "numeric value is too large"));
1570 * If we have a decimal value, then do the computation with floating
1571 * point arithmetic. Otherwise, use standard arithmetic.
1574 double fval = strtod(value, &end);
1576 if ((shift = str2shift(hdl, end)) == -1)
1579 fval *= pow(2, shift);
1581 if (fval > UINT64_MAX) {
1583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1584 "numeric value is too large"));
1588 *num = (uint64_t)fval;
1590 if ((shift = str2shift(hdl, end)) == -1)
1593 /* Check for overflow */
1594 if (shift >= 64 || (*num << shift) >> shift != *num) {
1596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1597 "numeric value is too large"));
1608 * Given a propname=value nvpair to set, parse any numeric properties
1609 * (index, boolean, etc) if they are specified as strings and add the
1610 * resulting nvpair to the returned nvlist.
1612 * At the DSL layer, all properties are either 64-bit numbers or strings.
1613 * We want the user to be able to ignore this fact and specify properties
1614 * as native values (numbers, for example) or as strings (to simplify
1615 * command line utilities). This also handles converting index types
1616 * (compression, checksum, etc) from strings to their on-disk index.
1619 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1620 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1623 data_type_t datatype = nvpair_type(elem);
1624 zprop_type_t proptype;
1625 const char *propname;
1627 boolean_t isnone = B_FALSE;
1630 if (type == ZFS_TYPE_POOL) {
1631 proptype = zpool_prop_get_type(prop);
1632 propname = zpool_prop_to_name(prop);
1634 proptype = zfs_prop_get_type(prop);
1635 propname = zfs_prop_to_name(prop);
1639 * Convert any properties to the internal DSL value types.
1645 case PROP_TYPE_STRING:
1646 if (datatype != DATA_TYPE_STRING) {
1647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1648 "'%s' must be a string"), nvpair_name(elem));
1651 err = nvpair_value_string(elem, svalp);
1653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1654 "'%s' is invalid"), nvpair_name(elem));
1657 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1658 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1659 "'%s' is too long"), nvpair_name(elem));
1664 case PROP_TYPE_NUMBER:
1665 if (datatype == DATA_TYPE_STRING) {
1666 (void) nvpair_value_string(elem, &value);
1667 if (strcmp(value, "none") == 0) {
1669 } else if (zfs_nicestrtonum(hdl, value, ivalp)
1673 } else if (datatype == DATA_TYPE_UINT64) {
1674 (void) nvpair_value_uint64(elem, ivalp);
1676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1677 "'%s' must be a number"), nvpair_name(elem));
1682 * Quota special: force 'none' and don't allow 0.
1684 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1685 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1687 "use 'none' to disable quota/refquota"));
1692 * Special handling for "*_limit=none". In this case it's not
1695 if ((type & ZFS_TYPE_DATASET) && isnone &&
1696 (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1697 prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1698 *ivalp = UINT64_MAX;
1702 case PROP_TYPE_INDEX:
1703 if (datatype != DATA_TYPE_STRING) {
1704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1705 "'%s' must be a string"), nvpair_name(elem));
1709 (void) nvpair_value_string(elem, &value);
1711 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1713 "'%s' must be one of '%s'"), propname,
1714 zprop_values(prop, type));
1724 * Add the result to our return set of properties.
1726 if (*svalp != NULL) {
1727 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1728 (void) no_memory(hdl);
1732 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1733 (void) no_memory(hdl);
1740 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1745 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1749 zprop_list_t *entry;
1751 prop = zprop_name_to_prop(propname, type);
1753 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1757 * When no property table entry can be found, return failure if
1758 * this is a pool property or if this isn't a user-defined
1761 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1762 !zpool_prop_feature(propname) &&
1763 !zpool_prop_unsupported(propname)) ||
1764 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1765 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1767 "invalid property '%s'"), propname);
1768 return (zfs_error(hdl, EZFS_BADPROP,
1769 dgettext(TEXT_DOMAIN, "bad property list")));
1772 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1775 entry->pl_prop = prop;
1776 if (prop == ZPROP_INVAL) {
1777 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1782 entry->pl_width = strlen(propname);
1784 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1794 * Given a comma-separated list of properties, construct a property list
1795 * containing both user-defined and native properties. This function will
1796 * return a NULL list if 'all' is specified, which can later be expanded
1797 * by zprop_expand_list().
1800 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1806 * If 'all' is specified, return a NULL list.
1808 if (strcmp(props, "all") == 0)
1812 * If no props were specified, return an error.
1814 if (props[0] == '\0') {
1815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1816 "no properties specified"));
1817 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1818 "bad property list")));
1822 * It would be nice to use getsubopt() here, but the inclusion of column
1823 * aliases makes this more effort than it's worth.
1825 while (*props != '\0') {
1830 if ((p = strchr(props, ',')) == NULL) {
1831 len = strlen(props);
1838 * Check for empty options.
1841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1842 "empty property name"));
1843 return (zfs_error(hdl, EZFS_BADPROP,
1844 dgettext(TEXT_DOMAIN, "bad property list")));
1848 * Check all regular property names.
1853 if (strcmp(props, "space") == 0) {
1854 static char *spaceprops[] = {
1855 "name", "avail", "used", "usedbysnapshots",
1856 "usedbydataset", "usedbyrefreservation",
1857 "usedbychildren", NULL
1861 for (i = 0; spaceprops[i]; i++) {
1862 if (addlist(hdl, spaceprops[i], listp, type))
1864 listp = &(*listp)->pl_next;
1867 if (addlist(hdl, props, listp, type))
1869 listp = &(*listp)->pl_next;
1881 zprop_free_list(zprop_list_t *pl)
1885 while (pl != NULL) {
1887 free(pl->pl_user_prop);
1893 typedef struct expand_data {
1894 zprop_list_t **last;
1895 libzfs_handle_t *hdl;
1900 zprop_expand_list_cb(int prop, void *cb)
1902 zprop_list_t *entry;
1903 expand_data_t *edp = cb;
1905 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1906 return (ZPROP_INVAL);
1908 entry->pl_prop = prop;
1909 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1910 entry->pl_all = B_TRUE;
1912 *(edp->last) = entry;
1913 edp->last = &entry->pl_next;
1915 return (ZPROP_CONT);
1919 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1921 zprop_list_t *entry;
1922 zprop_list_t **last;
1927 * If this is the very first time we've been called for an 'all'
1928 * specification, expand the list to include all native
1937 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1938 B_FALSE, type) == ZPROP_INVAL)
1942 * Add 'name' to the beginning of the list, which is handled
1945 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1948 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1950 entry->pl_width = zprop_width(entry->pl_prop,
1951 &entry->pl_fixed, type);
1952 entry->pl_all = B_TRUE;
1953 entry->pl_next = *plp;
1960 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1963 return (zprop_iter_common(func, cb, show_all, ordered, type));