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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Internal utility routines for the ZFS library.
39 #include <sys/mnttab.h>
40 #include <sys/mntent.h>
41 #include <sys/types.h>
45 #include "libzfs_impl.h"
49 libzfs_errno(libzfs_handle_t
*hdl
)
51 return (hdl
->libzfs_error
);
55 libzfs_error_action(libzfs_handle_t
*hdl
)
57 return (hdl
->libzfs_action
);
61 libzfs_error_description(libzfs_handle_t
*hdl
)
63 if (hdl
->libzfs_desc
[0] != '\0')
64 return (hdl
->libzfs_desc
);
66 switch (hdl
->libzfs_error
) {
68 return (dgettext(TEXT_DOMAIN
, "out of memory"));
70 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
71 case EZFS_PROPREADONLY
:
72 return (dgettext(TEXT_DOMAIN
, "read only property"));
74 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
75 "datasets of this type"));
76 case EZFS_PROPNONINHERIT
:
77 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
79 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
81 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
82 "datasets of this type"));
84 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
86 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
88 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
90 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
92 return (dgettext(TEXT_DOMAIN
, "dataset is read only"));
94 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
96 case EZFS_INVALIDNAME
:
97 return (dgettext(TEXT_DOMAIN
, "invalid name"));
99 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
102 return (dgettext(TEXT_DOMAIN
, "backup failed"));
104 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
106 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
108 return (dgettext(TEXT_DOMAIN
, "invalid device"));
109 case EZFS_NOREPLICAS
:
110 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
111 case EZFS_RESILVERING
:
112 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
113 case EZFS_BADVERSION
:
114 return (dgettext(TEXT_DOMAIN
, "unsupported version"));
115 case EZFS_POOLUNAVAIL
:
116 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
117 case EZFS_DEVOVERFLOW
:
118 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
120 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
121 case EZFS_CROSSTARGET
:
122 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
125 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
126 case EZFS_MOUNTFAILED
:
127 return (dgettext(TEXT_DOMAIN
, "mount failed"));
128 case EZFS_UMOUNTFAILED
:
129 return (dgettext(TEXT_DOMAIN
, "umount failed"));
130 case EZFS_UNSHARENFSFAILED
:
131 return (dgettext(TEXT_DOMAIN
, "unshare(1M) failed"));
132 case EZFS_SHARENFSFAILED
:
133 return (dgettext(TEXT_DOMAIN
, "share(1M) failed"));
134 case EZFS_UNSHARESMBFAILED
:
135 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
136 case EZFS_SHARESMBFAILED
:
137 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
139 return (dgettext(TEXT_DOMAIN
, "permission denied"));
141 return (dgettext(TEXT_DOMAIN
, "out of space"));
143 return (dgettext(TEXT_DOMAIN
, "bad address"));
145 return (dgettext(TEXT_DOMAIN
, "I/O error"));
147 return (dgettext(TEXT_DOMAIN
, "signal received"));
149 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
151 case EZFS_INVALCONFIG
:
152 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
154 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
156 return (dgettext(TEXT_DOMAIN
, "no history available"));
158 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
160 case EZFS_POOL_NOTSUP
:
161 return (dgettext(TEXT_DOMAIN
, "operation not supported "
162 "on this type of pool"));
163 case EZFS_POOL_INVALARG
:
164 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
165 "this pool operation"));
166 case EZFS_NAMETOOLONG
:
167 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
168 case EZFS_OPENFAILED
:
169 return (dgettext(TEXT_DOMAIN
, "open failed"));
171 return (dgettext(TEXT_DOMAIN
,
172 "disk capacity information could not be retrieved"));
173 case EZFS_LABELFAILED
:
174 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
176 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
178 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
179 case EZFS_BADPERMSET
:
180 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
181 case EZFS_NODELEGATION
:
182 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
183 "disabled on pool"));
184 case EZFS_PERMRDONLY
:
185 return (dgettext(TEXT_DOMAIN
, "snapshot permissions cannot be"
188 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
190 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
191 case EZFS_VDEVNOTSUP
:
192 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
195 return (dgettext(TEXT_DOMAIN
, "operation not supported "
197 case EZFS_ACTIVE_SPARE
:
198 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
200 case EZFS_UNPLAYED_LOGS
:
201 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
203 case EZFS_REFTAG_RELE
:
204 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
205 case EZFS_REFTAG_HOLD
:
206 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
208 case EZFS_TAGTOOLONG
:
209 return (dgettext(TEXT_DOMAIN
, "tag too long"));
210 case EZFS_PIPEFAILED
:
211 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
212 case EZFS_THREADCREATEFAILED
:
213 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
214 case EZFS_POSTSPLIT_ONLINE
:
215 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
218 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
219 "use 'zpool scrub -s' to cancel current scrub"));
221 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
223 return (dgettext(TEXT_DOMAIN
, "unknown error"));
225 assert(hdl
->libzfs_error
== 0);
226 return (dgettext(TEXT_DOMAIN
, "no error"));
232 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
238 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
240 hdl
->libzfs_desc_active
= 1;
246 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
248 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
250 hdl
->libzfs_error
= error
;
252 if (hdl
->libzfs_desc_active
)
253 hdl
->libzfs_desc_active
= 0;
255 hdl
->libzfs_desc
[0] = '\0';
257 if (hdl
->libzfs_printerr
) {
258 if (error
== EZFS_UNKNOWN
) {
259 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
260 "error: %s\n"), libzfs_error_description(hdl
));
264 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
265 libzfs_error_description(hdl
));
266 if (error
== EZFS_NOMEM
)
272 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
274 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
279 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
285 zfs_verror(hdl
, error
, fmt
, ap
);
293 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
299 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
303 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
307 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
311 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
315 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
323 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
325 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
330 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
336 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
344 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
348 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
349 "dataset does not exist"));
350 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
355 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
359 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
360 "dataset already exists"));
361 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
365 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
367 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
370 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
371 "snapshot permissions cannot be modified"));
372 zfs_verror(hdl
, EZFS_PERMRDONLY
, fmt
, ap
);
375 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
378 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
381 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
382 "pool I/O is currently suspended"));
383 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
386 zfs_error_aux(hdl
, strerror(error
));
387 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
396 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
398 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
403 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
409 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
416 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
421 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
422 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
426 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
427 "pool already exists"));
428 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
432 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
433 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
437 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
438 "one or more devices is currently unavailable"));
439 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
443 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
447 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
451 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
456 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
460 "pool I/O is currently suspended"));
461 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
465 zfs_error_aux(hdl
, strerror(error
));
466 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
474 * Display an out of memory error message and abort the current program.
477 no_memory(libzfs_handle_t
*hdl
)
479 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
483 * A safe form of malloc() which will die if the allocation fails.
486 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
490 if ((data
= calloc(1, size
)) == NULL
)
491 (void) no_memory(hdl
);
497 * A safe form of realloc(), which also zeroes newly allocated space.
500 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
504 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
505 (void) no_memory(hdl
);
509 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
514 * A safe form of strdup() which will die if the allocation fails.
517 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
521 if ((ret
= strdup(str
)) == NULL
)
522 (void) no_memory(hdl
);
528 * Convert a number to an appropriately human-readable output.
531 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
542 u
= " KMGTPE"[index
];
545 (void) snprintf(buf
, buflen
, "%llu", n
);
546 } else if ((num
& ((1ULL << 10 * index
) - 1)) == 0) {
548 * If this is an even multiple of the base, always display
549 * without any decimal precision.
551 (void) snprintf(buf
, buflen
, "%llu%c", n
, u
);
554 * We want to choose a precision that reflects the best choice
555 * for fitting in 5 characters. This can get rather tricky when
556 * we have numbers that are very close to an order of magnitude.
557 * For example, when displaying 10239 (which is really 9.999K),
558 * we want only a single place of precision for 10.0K. We could
559 * develop some complex heuristics for this, but it's much
560 * easier just to try each combination in turn.
563 for (i
= 2; i
>= 0; i
--) {
564 if (snprintf(buf
, buflen
, "%.*f%c", i
,
565 (double)num
/ (1ULL << 10 * index
), u
) <= 5)
572 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
574 hdl
->libzfs_printerr
= printerr
;
580 libzfs_handle_t
*hdl
;
582 if ((hdl
= calloc(sizeof (libzfs_handle_t
), 1)) == NULL
) {
586 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
591 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "r")) == NULL
) {
592 (void) close(hdl
->libzfs_fd
);
597 hdl
->libzfs_sharetab
= fopen("/etc/dfs/sharetab", "r");
601 libzfs_mnttab_init(hdl
);
607 libzfs_fini(libzfs_handle_t
*hdl
)
609 (void) close(hdl
->libzfs_fd
);
610 if (hdl
->libzfs_mnttab
)
611 (void) fclose(hdl
->libzfs_mnttab
);
612 if (hdl
->libzfs_sharetab
)
613 (void) fclose(hdl
->libzfs_sharetab
);
614 zfs_uninit_libshare(hdl
);
615 if (hdl
->libzfs_log_str
)
616 (void) free(hdl
->libzfs_log_str
);
617 zpool_free_handles(hdl
);
618 libzfs_fru_clear(hdl
, B_TRUE
);
619 namespace_clear(hdl
);
620 libzfs_mnttab_fini(hdl
);
625 zpool_get_handle(zpool_handle_t
*zhp
)
627 return (zhp
->zpool_hdl
);
631 zfs_get_handle(zfs_handle_t
*zhp
)
633 return (zhp
->zfs_hdl
);
637 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
639 return (zhp
->zpool_hdl
);
643 * Given a name, determine whether or not it's a valid path
644 * (starts with '/' or "./"). If so, walk the mnttab trying
645 * to match the device number. If not, treat the path as an
649 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
651 struct stat64 statbuf
;
652 struct extmnttab entry
;
655 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
657 * It's not a valid path, assume it's a name of type 'argtype'.
659 return (zfs_open(hdl
, path
, argtype
));
662 if (stat64(path
, &statbuf
) != 0) {
663 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
667 rewind(hdl
->libzfs_mnttab
);
668 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
669 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
678 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
679 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
684 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
688 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
692 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
696 zc
->zc_nvlist_dst_size
= len
;
697 if ((zc
->zc_nvlist_dst
= (uint64_t)(uintptr_t)
698 zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
)) == NULL
)
705 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
706 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
707 * filled in by the kernel to indicate the actual required size.
710 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
712 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
713 if ((zc
->zc_nvlist_dst
= (uint64_t)(uintptr_t)
714 zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
))
722 * Called to free the src and dst nvlists stored in the command structure.
725 zcmd_free_nvlists(zfs_cmd_t
*zc
)
727 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
728 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
729 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
733 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
739 verify(nvlist_size(nvl
, &len
, NV_ENCODE_NATIVE
) == 0);
741 if ((packed
= zfs_alloc(hdl
, len
)) == NULL
)
744 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
746 *outnv
= (uint64_t)(uintptr_t)packed
;
753 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
755 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
756 &zc
->zc_nvlist_conf_size
, nvl
));
760 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
762 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
763 &zc
->zc_nvlist_src_size
, nvl
));
767 * Unpacks an nvlist from the ZFS ioctl command structure.
770 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
772 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
773 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
774 return (no_memory(hdl
));
780 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
784 zc
->zc_history
= (uint64_t)(uintptr_t)hdl
->libzfs_log_str
;
785 error
= ioctl(hdl
->libzfs_fd
, request
, zc
);
786 if (hdl
->libzfs_log_str
) {
787 free(hdl
->libzfs_log_str
);
788 hdl
->libzfs_log_str
= NULL
;
796 * ================================================================
797 * API shared by zfs and zpool property management
798 * ================================================================
802 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
804 zprop_list_t
*pl
= cbp
->cb_proplist
;
809 cbp
->cb_first
= B_FALSE
;
810 if (cbp
->cb_scripted
)
814 * Start with the length of the column headers.
816 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
817 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
819 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
821 cbp
->cb_colwidths
[GET_COL_RECVD
] = strlen(dgettext(TEXT_DOMAIN
,
823 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
826 /* first property is always NAME */
827 assert(cbp
->cb_proplist
->pl_prop
==
828 ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
: ZFS_PROP_NAME
));
831 * Go through and calculate the widths for each column. For the
832 * 'source' column, we kludge it up by taking the worst-case scenario of
833 * inheriting from the longest name. This is acceptable because in the
834 * majority of cases 'SOURCE' is the last column displayed, and we don't
835 * use the width anyway. Note that the 'VALUE' column can be oversized,
836 * if the name of the property is much longer than any values we find.
838 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
842 if (pl
->pl_prop
!= ZPROP_INVAL
) {
843 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
844 zpool_prop_to_name(pl
->pl_prop
) :
845 zfs_prop_to_name(pl
->pl_prop
);
847 len
= strlen(propname
);
848 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
849 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
851 len
= strlen(pl
->pl_user_prop
);
852 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
853 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
857 * 'VALUE' column. The first property is always the 'name'
858 * property that was tacked on either by /sbin/zfs's
859 * zfs_do_get() or when calling zprop_expand_list(), so we
860 * ignore its width. If the user specified the name property
861 * to display, then it will be later in the list in any case.
863 if (pl
!= cbp
->cb_proplist
&&
864 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
865 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
867 /* 'RECEIVED' column. */
868 if (pl
!= cbp
->cb_proplist
&&
869 pl
->pl_recvd_width
> cbp
->cb_colwidths
[GET_COL_RECVD
])
870 cbp
->cb_colwidths
[GET_COL_RECVD
] = pl
->pl_recvd_width
;
873 * 'NAME' and 'SOURCE' columns
875 if (pl
->pl_prop
== (type
== ZFS_TYPE_POOL
? ZPOOL_PROP_NAME
:
877 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_NAME
]) {
878 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
879 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
880 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
885 * Now go through and print the headers.
887 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
888 switch (cbp
->cb_columns
[i
]) {
890 title
= dgettext(TEXT_DOMAIN
, "NAME");
892 case GET_COL_PROPERTY
:
893 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
896 title
= dgettext(TEXT_DOMAIN
, "VALUE");
899 title
= dgettext(TEXT_DOMAIN
, "RECEIVED");
902 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
909 if (i
== (ZFS_GET_NCOLS
- 1) ||
910 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
911 (void) printf("%s", title
);
913 (void) printf("%-*s ",
914 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
922 * Display a single line of output, according to the settings in the callback
926 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
927 const char *propname
, const char *value
, zprop_source_t sourcetype
,
928 const char *source
, const char *recvd_value
)
935 * Ignore those source types that the user has chosen to ignore.
937 if ((sourcetype
& cbp
->cb_sources
) == 0)
941 zprop_print_headers(cbp
, cbp
->cb_type
);
943 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
944 switch (cbp
->cb_columns
[i
]) {
949 case GET_COL_PROPERTY
:
958 switch (sourcetype
) {
963 case ZPROP_SRC_DEFAULT
:
967 case ZPROP_SRC_LOCAL
:
971 case ZPROP_SRC_TEMPORARY
:
975 case ZPROP_SRC_INHERITED
:
976 (void) snprintf(buf
, sizeof (buf
),
977 "inherited from %s", source
);
980 case ZPROP_SRC_RECEIVED
:
987 str
= (recvd_value
== NULL
? "-" : recvd_value
);
994 if (cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
995 (void) printf("%s", str
);
996 else if (cbp
->cb_scripted
)
997 (void) printf("%s\t", str
);
999 (void) printf("%-*s ",
1000 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1004 (void) printf("\n");
1008 * Given a numeric suffix, convert the value into a number of bits that the
1009 * resulting value must be shifted.
1012 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1014 const char *ends
= "BKMGTPEZ";
1019 for (i
= 0; i
< strlen(ends
); i
++) {
1020 if (toupper(buf
[0]) == ends
[i
])
1023 if (i
== strlen(ends
)) {
1024 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1025 "invalid numeric suffix '%s'"), buf
);
1030 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
1031 * allow 'BB' - that's just weird.
1033 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0' &&
1034 toupper(buf
[0]) != 'B'))
1037 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1038 "invalid numeric suffix '%s'"), buf
);
1043 * Convert a string of the form '100G' into a real number. Used when setting
1044 * properties or creating a volume. 'buf' is used to place an extended error
1045 * message for the caller to use.
1048 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1055 /* Check to see if this looks like a number. */
1056 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1058 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1059 "bad numeric value '%s'"), value
);
1063 /* Rely on strtoull() to process the numeric portion. */
1065 *num
= strtoull(value
, &end
, 10);
1068 * Check for ERANGE, which indicates that the value is too large to fit
1069 * in a 64-bit value.
1071 if (errno
== ERANGE
) {
1073 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1074 "numeric value is too large"));
1079 * If we have a decimal value, then do the computation with floating
1080 * point arithmetic. Otherwise, use standard arithmetic.
1083 double fval
= strtod(value
, &end
);
1085 if ((shift
= str2shift(hdl
, end
)) == -1)
1088 fval
*= pow(2, shift
);
1090 if (fval
> UINT64_MAX
) {
1092 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1093 "numeric value is too large"));
1097 *num
= (uint64_t)fval
;
1099 if ((shift
= str2shift(hdl
, end
)) == -1)
1102 /* Check for overflow */
1103 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1105 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1106 "numeric value is too large"));
1117 * Given a propname=value nvpair to set, parse any numeric properties
1118 * (index, boolean, etc) if they are specified as strings and add the
1119 * resulting nvpair to the returned nvlist.
1121 * At the DSL layer, all properties are either 64-bit numbers or strings.
1122 * We want the user to be able to ignore this fact and specify properties
1123 * as native values (numbers, for example) or as strings (to simplify
1124 * command line utilities). This also handles converting index types
1125 * (compression, checksum, etc) from strings to their on-disk index.
1128 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1129 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1132 data_type_t datatype
= nvpair_type(elem
);
1133 zprop_type_t proptype
;
1134 const char *propname
;
1136 boolean_t isnone
= B_FALSE
;
1138 if (type
== ZFS_TYPE_POOL
) {
1139 proptype
= zpool_prop_get_type(prop
);
1140 propname
= zpool_prop_to_name(prop
);
1142 proptype
= zfs_prop_get_type(prop
);
1143 propname
= zfs_prop_to_name(prop
);
1147 * Convert any properties to the internal DSL value types.
1153 case PROP_TYPE_STRING
:
1154 if (datatype
!= DATA_TYPE_STRING
) {
1155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1156 "'%s' must be a string"), nvpair_name(elem
));
1159 (void) nvpair_value_string(elem
, svalp
);
1160 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1162 "'%s' is too long"), nvpair_name(elem
));
1167 case PROP_TYPE_NUMBER
:
1168 if (datatype
== DATA_TYPE_STRING
) {
1169 (void) nvpair_value_string(elem
, &value
);
1170 if (strcmp(value
, "none") == 0) {
1172 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1176 } else if (datatype
== DATA_TYPE_UINT64
) {
1177 (void) nvpair_value_uint64(elem
, ivalp
);
1179 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1180 "'%s' must be a number"), nvpair_name(elem
));
1185 * Quota special: force 'none' and don't allow 0.
1187 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1188 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1189 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1190 "use 'none' to disable quota/refquota"));
1195 case PROP_TYPE_INDEX
:
1196 if (datatype
!= DATA_TYPE_STRING
) {
1197 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1198 "'%s' must be a string"), nvpair_name(elem
));
1202 (void) nvpair_value_string(elem
, &value
);
1204 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1205 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1206 "'%s' must be one of '%s'"), propname
,
1207 zprop_values(prop
, type
));
1217 * Add the result to our return set of properties.
1219 if (*svalp
!= NULL
) {
1220 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1221 (void) no_memory(hdl
);
1225 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1226 (void) no_memory(hdl
);
1233 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1238 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1242 zprop_list_t
*entry
;
1244 prop
= zprop_name_to_prop(propname
, type
);
1246 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
))
1250 * When no property table entry can be found, return failure if
1251 * this is a pool property or if this isn't a user-defined
1254 if (prop
== ZPROP_INVAL
&& (type
== ZFS_TYPE_POOL
||
1255 (!zfs_prop_user(propname
) && !zfs_prop_userquota(propname
)))) {
1256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1257 "invalid property '%s'"), propname
);
1258 return (zfs_error(hdl
, EZFS_BADPROP
,
1259 dgettext(TEXT_DOMAIN
, "bad property list")));
1262 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1265 entry
->pl_prop
= prop
;
1266 if (prop
== ZPROP_INVAL
) {
1267 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) == NULL
) {
1271 entry
->pl_width
= strlen(propname
);
1273 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1283 * Given a comma-separated list of properties, construct a property list
1284 * containing both user-defined and native properties. This function will
1285 * return a NULL list if 'all' is specified, which can later be expanded
1286 * by zprop_expand_list().
1289 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1295 * If 'all' is specified, return a NULL list.
1297 if (strcmp(props
, "all") == 0)
1301 * If no props were specified, return an error.
1303 if (props
[0] == '\0') {
1304 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1305 "no properties specified"));
1306 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1307 "bad property list")));
1311 * It would be nice to use getsubopt() here, but the inclusion of column
1312 * aliases makes this more effort than it's worth.
1314 while (*props
!= '\0') {
1319 if ((p
= strchr(props
, ',')) == NULL
) {
1320 len
= strlen(props
);
1327 * Check for empty options.
1330 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1331 "empty property name"));
1332 return (zfs_error(hdl
, EZFS_BADPROP
,
1333 dgettext(TEXT_DOMAIN
, "bad property list")));
1337 * Check all regular property names.
1342 if (strcmp(props
, "space") == 0) {
1343 static char *spaceprops
[] = {
1344 "name", "avail", "used", "usedbysnapshots",
1345 "usedbydataset", "usedbyrefreservation",
1346 "usedbychildren", NULL
1350 for (i
= 0; spaceprops
[i
]; i
++) {
1351 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
1353 listp
= &(*listp
)->pl_next
;
1356 if (addlist(hdl
, props
, listp
, type
))
1358 listp
= &(*listp
)->pl_next
;
1370 zprop_free_list(zprop_list_t
*pl
)
1374 while (pl
!= NULL
) {
1376 free(pl
->pl_user_prop
);
1382 typedef struct expand_data
{
1383 zprop_list_t
**last
;
1384 libzfs_handle_t
*hdl
;
1389 zprop_expand_list_cb(int prop
, void *cb
)
1391 zprop_list_t
*entry
;
1392 expand_data_t
*edp
= cb
;
1394 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
1395 return (ZPROP_INVAL
);
1397 entry
->pl_prop
= prop
;
1398 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
1399 entry
->pl_all
= B_TRUE
;
1401 *(edp
->last
) = entry
;
1402 edp
->last
= &entry
->pl_next
;
1404 return (ZPROP_CONT
);
1408 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
1410 zprop_list_t
*entry
;
1411 zprop_list_t
**last
;
1416 * If this is the very first time we've been called for an 'all'
1417 * specification, expand the list to include all native
1426 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
1427 B_FALSE
, type
) == ZPROP_INVAL
)
1431 * Add 'name' to the beginning of the list, which is handled
1434 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1437 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1439 entry
->pl_width
= zprop_width(entry
->pl_prop
,
1440 &entry
->pl_fixed
, type
);
1441 entry
->pl_all
= B_TRUE
;
1442 entry
->pl_next
= *plp
;
1449 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
1452 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));