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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Internal utility routines for the ZFS library.
40 #include <sys/mnttab.h>
41 #include <sys/mntent.h>
42 #include <sys/types.h>
46 #include "libzfs_impl.h"
50 libzfs_errno(libzfs_handle_t
*hdl
)
52 return (hdl
->libzfs_error
);
56 libzfs_error_action(libzfs_handle_t
*hdl
)
58 return (hdl
->libzfs_action
);
62 libzfs_error_description(libzfs_handle_t
*hdl
)
64 if (hdl
->libzfs_desc
[0] != '\0')
65 return (hdl
->libzfs_desc
);
67 switch (hdl
->libzfs_error
) {
69 return (dgettext(TEXT_DOMAIN
, "out of memory"));
71 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
72 case EZFS_PROPREADONLY
:
73 return (dgettext(TEXT_DOMAIN
, "read only property"));
75 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
76 "datasets of this type"));
77 case EZFS_PROPNONINHERIT
:
78 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
80 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
82 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
83 "datasets of this type"));
85 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
87 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
89 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
91 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
93 return (dgettext(TEXT_DOMAIN
, "dataset is read only"));
95 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
98 return (dgettext(TEXT_DOMAIN
, "volume has data"));
99 case EZFS_INVALIDNAME
:
100 return (dgettext(TEXT_DOMAIN
, "invalid name"));
101 case EZFS_BADRESTORE
:
102 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
105 return (dgettext(TEXT_DOMAIN
, "backup failed"));
107 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
109 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
111 return (dgettext(TEXT_DOMAIN
, "invalid device"));
112 case EZFS_NOREPLICAS
:
113 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
114 case EZFS_RESILVERING
:
115 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
116 case EZFS_BADVERSION
:
117 return (dgettext(TEXT_DOMAIN
, "unsupported version"));
118 case EZFS_POOLUNAVAIL
:
119 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
120 case EZFS_DEVOVERFLOW
:
121 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
123 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
124 case EZFS_CROSSTARGET
:
125 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
128 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
129 case EZFS_MOUNTFAILED
:
130 return (dgettext(TEXT_DOMAIN
, "mount failed"));
131 case EZFS_UMOUNTFAILED
:
132 return (dgettext(TEXT_DOMAIN
, "umount failed"));
133 case EZFS_UNSHARENFSFAILED
:
134 return (dgettext(TEXT_DOMAIN
, "unshare(1M) failed"));
135 case EZFS_SHARENFSFAILED
:
136 return (dgettext(TEXT_DOMAIN
, "share(1M) failed"));
137 case EZFS_UNSHARESMBFAILED
:
138 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
139 case EZFS_SHARESMBFAILED
:
140 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
141 case EZFS_ISCSISVCUNAVAIL
:
142 return (dgettext(TEXT_DOMAIN
,
143 "iscsitgt service need to be enabled by "
144 "a privileged user"));
146 return (dgettext(TEXT_DOMAIN
, "failed to create /dev links"));
148 return (dgettext(TEXT_DOMAIN
, "permission denied"));
150 return (dgettext(TEXT_DOMAIN
, "out of space"));
152 return (dgettext(TEXT_DOMAIN
, "I/O error"));
154 return (dgettext(TEXT_DOMAIN
, "signal received"));
156 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
158 case EZFS_INVALCONFIG
:
159 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
161 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
163 return (dgettext(TEXT_DOMAIN
, "no history available"));
164 case EZFS_UNSHAREISCSIFAILED
:
165 return (dgettext(TEXT_DOMAIN
,
166 "iscsitgtd failed request to unshare"));
167 case EZFS_SHAREISCSIFAILED
:
168 return (dgettext(TEXT_DOMAIN
,
169 "iscsitgtd failed request to share"));
171 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
173 case EZFS_POOL_NOTSUP
:
174 return (dgettext(TEXT_DOMAIN
, "operation not supported "
175 "on this type of pool"));
176 case EZFS_POOL_INVALARG
:
177 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
178 "this pool operation"));
179 case EZFS_NAMETOOLONG
:
180 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
181 case EZFS_OPENFAILED
:
182 return (dgettext(TEXT_DOMAIN
, "open failed"));
184 return (dgettext(TEXT_DOMAIN
,
185 "disk capacity information could not be retrieved"));
186 case EZFS_LABELFAILED
:
187 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
189 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
191 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
192 case EZFS_BADPERMSET
:
193 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
194 case EZFS_NODELEGATION
:
195 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
196 "disabled on pool"));
197 case EZFS_PERMRDONLY
:
198 return (dgettext(TEXT_DOMAIN
, "snapshot permissions cannot be"
201 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
203 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
204 case EZFS_VDEVNOTSUP
:
205 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
208 return (dgettext(TEXT_DOMAIN
, "operation not supported "
210 case EZFS_ACTIVE_SPARE
:
211 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
214 return (dgettext(TEXT_DOMAIN
, "unknown error"));
216 assert(hdl
->libzfs_error
== 0);
217 return (dgettext(TEXT_DOMAIN
, "no error"));
223 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
229 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
231 hdl
->libzfs_desc_active
= 1;
237 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
239 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
241 hdl
->libzfs_error
= error
;
243 if (hdl
->libzfs_desc_active
)
244 hdl
->libzfs_desc_active
= 0;
246 hdl
->libzfs_desc
[0] = '\0';
248 if (hdl
->libzfs_printerr
) {
249 if (error
== EZFS_UNKNOWN
) {
250 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
251 "error: %s\n"), libzfs_error_description(hdl
));
255 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
256 libzfs_error_description(hdl
));
257 if (error
== EZFS_NOMEM
)
263 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
265 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
270 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
276 zfs_verror(hdl
, error
, fmt
, ap
);
284 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
290 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
294 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
298 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
302 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
310 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
312 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
317 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
323 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
331 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
335 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
336 "dataset does not exist"));
337 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
342 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
346 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
347 "dataset already exists"));
348 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
352 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
354 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
358 "snapshot permissions cannot be modified"));
359 zfs_verror(hdl
, EZFS_PERMRDONLY
, fmt
, ap
);
362 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
365 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
368 zfs_error_aux(hdl
, strerror(errno
));
369 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
378 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
380 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
385 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
391 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
398 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
403 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
404 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
408 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
409 "pool already exists"));
410 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
414 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
415 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
420 "one or more devices is currently unavailable"));
421 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
425 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
429 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
433 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
438 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
442 zfs_error_aux(hdl
, strerror(error
));
443 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
451 * Display an out of memory error message and abort the current program.
454 no_memory(libzfs_handle_t
*hdl
)
456 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
460 * A safe form of malloc() which will die if the allocation fails.
463 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
467 if ((data
= calloc(1, size
)) == NULL
)
468 (void) no_memory(hdl
);
474 * A safe form of realloc(), which also zeroes newly allocated space.
477 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
481 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
482 (void) no_memory(hdl
);
487 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
492 * A safe form of strdup() which will die if the allocation fails.
495 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
499 if ((ret
= strdup(str
)) == NULL
)
500 (void) no_memory(hdl
);
506 * Convert a number to an appropriately human-readable output.
509 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
520 u
= " KMGTPE"[index
];
523 (void) snprintf(buf
, buflen
, "%llu", n
);
524 } else if ((num
& ((1ULL << 10 * index
) - 1)) == 0) {
526 * If this is an even multiple of the base, always display
527 * without any decimal precision.
529 (void) snprintf(buf
, buflen
, "%llu%c", n
, u
);
532 * We want to choose a precision that reflects the best choice
533 * for fitting in 5 characters. This can get rather tricky when
534 * we have numbers that are very close to an order of magnitude.
535 * For example, when displaying 10239 (which is really 9.999K),
536 * we want only a single place of precision for 10.0K. We could
537 * develop some complex heuristics for this, but it's much
538 * easier just to try each combination in turn.
541 for (i
= 2; i
>= 0; i
--) {
542 if (snprintf(buf
, buflen
, "%.*f%c", i
,
543 (double)num
/ (1ULL << 10 * index
), u
) <= 5)
550 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
552 hdl
->libzfs_printerr
= printerr
;
558 libzfs_handle_t
*hdl
;
560 if ((hdl
= calloc(sizeof (libzfs_handle_t
), 1)) == NULL
) {
564 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
569 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "r")) == NULL
) {
570 (void) close(hdl
->libzfs_fd
);
575 hdl
->libzfs_sharetab
= fopen("/etc/dfs/sharetab", "r");
584 libzfs_fini(libzfs_handle_t
*hdl
)
586 (void) close(hdl
->libzfs_fd
);
587 if (hdl
->libzfs_mnttab
)
588 (void) fclose(hdl
->libzfs_mnttab
);
589 if (hdl
->libzfs_sharetab
)
590 (void) fclose(hdl
->libzfs_sharetab
);
591 zfs_uninit_libshare(hdl
);
592 if (hdl
->libzfs_log_str
)
593 (void) free(hdl
->libzfs_log_str
);
594 zpool_free_handles(hdl
);
595 namespace_clear(hdl
);
600 zpool_get_handle(zpool_handle_t
*zhp
)
602 return (zhp
->zpool_hdl
);
606 zfs_get_handle(zfs_handle_t
*zhp
)
608 return (zhp
->zfs_hdl
);
612 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
614 return (zhp
->zpool_hdl
);
618 * Given a name, determine whether or not it's a valid path
619 * (starts with '/' or "./"). If so, walk the mnttab trying
620 * to match the device number. If not, treat the path as an
624 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
626 struct stat64 statbuf
;
627 struct extmnttab entry
;
630 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
632 * It's not a valid path, assume it's a name of type 'argtype'.
634 return (zfs_open(hdl
, path
, argtype
));
637 if (stat64(path
, &statbuf
) != 0) {
638 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
642 rewind(hdl
->libzfs_mnttab
);
643 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
644 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
653 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
654 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
659 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
663 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
667 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
671 zc
->zc_nvlist_dst_size
= len
;
672 if ((zc
->zc_nvlist_dst
= (uint64_t)(uintptr_t)
673 zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
)) == NULL
)
680 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
681 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
682 * filled in by the kernel to indicate the actual required size.
685 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
687 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
688 if ((zc
->zc_nvlist_dst
= (uint64_t)(uintptr_t)
689 zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
))
697 * Called to free the src and dst nvlists stored in the command structure.
700 zcmd_free_nvlists(zfs_cmd_t
*zc
)
702 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
703 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
704 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
708 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
714 verify(nvlist_size(nvl
, &len
, NV_ENCODE_NATIVE
) == 0);
716 if ((packed
= zfs_alloc(hdl
, len
)) == NULL
)
719 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
721 *outnv
= (uint64_t)(uintptr_t)packed
;
728 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
730 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
731 &zc
->zc_nvlist_conf_size
, nvl
));
735 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
737 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
738 &zc
->zc_nvlist_src_size
, nvl
));
742 * Unpacks an nvlist from the ZFS ioctl command structure.
745 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
747 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
748 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
749 return (no_memory(hdl
));
755 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
759 zc
->zc_history
= (uint64_t)(uintptr_t)hdl
->libzfs_log_str
;
760 error
= ioctl(hdl
->libzfs_fd
, request
, zc
);
761 if (hdl
->libzfs_log_str
) {
762 free(hdl
->libzfs_log_str
);
763 hdl
->libzfs_log_str
= NULL
;
771 * ================================================================
772 * API shared by zfs and zpool property management
773 * ================================================================
777 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
779 zprop_list_t
*pl
= cbp
->cb_proplist
;
784 cbp
->cb_first
= B_FALSE
;
785 if (cbp
->cb_scripted
)
789 * Start with the length of the column headers.
791 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
792 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
794 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
796 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
800 * Go through and calculate the widths for each column. For the
801 * 'source' column, we kludge it up by taking the worst-case scenario of
802 * inheriting from the longest name. This is acceptable because in the
803 * majority of cases 'SOURCE' is the last column displayed, and we don't
804 * use the width anyway. Note that the 'VALUE' column can be oversized,
805 * if the name of the property is much longer the any values we find.
807 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
811 if (pl
->pl_prop
!= ZPROP_INVAL
) {
812 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
813 zpool_prop_to_name(pl
->pl_prop
) :
814 zfs_prop_to_name(pl
->pl_prop
);
816 len
= strlen(propname
);
817 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
818 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
820 len
= strlen(pl
->pl_user_prop
);
821 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
822 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
828 if ((pl
->pl_prop
!= ZFS_PROP_NAME
|| !pl
->pl_all
) &&
829 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
830 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
833 * 'NAME' and 'SOURCE' columns
835 if (pl
->pl_prop
== (type
== ZFS_TYPE_POOL
? ZPOOL_PROP_NAME
:
837 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_NAME
]) {
838 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
839 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
840 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
845 * Now go through and print the headers.
847 for (i
= 0; i
< 4; i
++) {
848 switch (cbp
->cb_columns
[i
]) {
850 title
= dgettext(TEXT_DOMAIN
, "NAME");
852 case GET_COL_PROPERTY
:
853 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
856 title
= dgettext(TEXT_DOMAIN
, "VALUE");
859 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
866 if (i
== 3 || cbp
->cb_columns
[i
+ 1] == 0)
867 (void) printf("%s", title
);
869 (void) printf("%-*s ",
870 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
878 * Display a single line of output, according to the settings in the callback
882 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
883 const char *propname
, const char *value
, zprop_source_t sourcetype
,
891 * Ignore those source types that the user has chosen to ignore.
893 if ((sourcetype
& cbp
->cb_sources
) == 0)
897 zprop_print_headers(cbp
, cbp
->cb_type
);
899 for (i
= 0; i
< 4; i
++) {
900 switch (cbp
->cb_columns
[i
]) {
905 case GET_COL_PROPERTY
:
914 switch (sourcetype
) {
919 case ZPROP_SRC_DEFAULT
:
923 case ZPROP_SRC_LOCAL
:
927 case ZPROP_SRC_TEMPORARY
:
931 case ZPROP_SRC_INHERITED
:
932 (void) snprintf(buf
, sizeof (buf
),
933 "inherited from %s", source
);
943 if (cbp
->cb_columns
[i
+ 1] == 0)
944 (void) printf("%s", str
);
945 else if (cbp
->cb_scripted
)
946 (void) printf("%s\t", str
);
948 (void) printf("%-*s ",
949 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
958 * Given a numeric suffix, convert the value into a number of bits that the
959 * resulting value must be shifted.
962 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
964 const char *ends
= "BKMGTPEZ";
969 for (i
= 0; i
< strlen(ends
); i
++) {
970 if (toupper(buf
[0]) == ends
[i
])
973 if (i
== strlen(ends
)) {
974 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
975 "invalid numeric suffix '%s'"), buf
);
980 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
981 * allow 'BB' - that's just weird.
983 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0' &&
984 toupper(buf
[0]) != 'B'))
987 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
988 "invalid numeric suffix '%s'"), buf
);
993 * Convert a string of the form '100G' into a real number. Used when setting
994 * properties or creating a volume. 'buf' is used to place an extended error
995 * message for the caller to use.
998 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1005 /* Check to see if this looks like a number. */
1006 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1008 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1009 "bad numeric value '%s'"), value
);
1013 /* Rely on stroll() to process the numeric portion. */
1015 *num
= strtoll(value
, &end
, 10);
1018 * Check for ERANGE, which indicates that the value is too large to fit
1019 * in a 64-bit value.
1021 if (errno
== ERANGE
) {
1023 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1024 "numeric value is too large"));
1029 * If we have a decimal value, then do the computation with floating
1030 * point arithmetic. Otherwise, use standard arithmetic.
1033 double fval
= strtod(value
, &end
);
1035 if ((shift
= str2shift(hdl
, end
)) == -1)
1038 fval
*= pow(2, shift
);
1040 if (fval
> UINT64_MAX
) {
1042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1043 "numeric value is too large"));
1047 *num
= (uint64_t)fval
;
1049 if ((shift
= str2shift(hdl
, end
)) == -1)
1052 /* Check for overflow */
1053 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1055 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1056 "numeric value is too large"));
1067 * Given a propname=value nvpair to set, parse any numeric properties
1068 * (index, boolean, etc) if they are specified as strings and add the
1069 * resulting nvpair to the returned nvlist.
1071 * At the DSL layer, all properties are either 64-bit numbers or strings.
1072 * We want the user to be able to ignore this fact and specify properties
1073 * as native values (numbers, for example) or as strings (to simplify
1074 * command line utilities). This also handles converting index types
1075 * (compression, checksum, etc) from strings to their on-disk index.
1078 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1079 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1082 data_type_t datatype
= nvpair_type(elem
);
1083 zprop_type_t proptype
;
1084 const char *propname
;
1086 boolean_t isnone
= B_FALSE
;
1088 if (type
== ZFS_TYPE_POOL
) {
1089 proptype
= zpool_prop_get_type(prop
);
1090 propname
= zpool_prop_to_name(prop
);
1092 proptype
= zfs_prop_get_type(prop
);
1093 propname
= zfs_prop_to_name(prop
);
1097 * Convert any properties to the internal DSL value types.
1103 case PROP_TYPE_STRING
:
1104 if (datatype
!= DATA_TYPE_STRING
) {
1105 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1106 "'%s' must be a string"), nvpair_name(elem
));
1109 (void) nvpair_value_string(elem
, svalp
);
1110 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1112 "'%s' is too long"), nvpair_name(elem
));
1117 case PROP_TYPE_NUMBER
:
1118 if (datatype
== DATA_TYPE_STRING
) {
1119 (void) nvpair_value_string(elem
, &value
);
1120 if (strcmp(value
, "none") == 0) {
1122 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1126 } else if (datatype
== DATA_TYPE_UINT64
) {
1127 (void) nvpair_value_uint64(elem
, ivalp
);
1129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1130 "'%s' must be a number"), nvpair_name(elem
));
1135 * Quota special: force 'none' and don't allow 0.
1137 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1138 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1140 "use 'none' to disable quota/refquota"));
1145 case PROP_TYPE_INDEX
:
1146 if (datatype
!= DATA_TYPE_STRING
) {
1147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1148 "'%s' must be a string"), nvpair_name(elem
));
1152 (void) nvpair_value_string(elem
, &value
);
1154 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1156 "'%s' must be one of '%s'"), propname
,
1157 zprop_values(prop
, type
));
1167 * Add the result to our return set of properties.
1169 if (*svalp
!= NULL
) {
1170 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1171 (void) no_memory(hdl
);
1175 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1176 (void) no_memory(hdl
);
1183 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1188 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1192 zprop_list_t
*entry
;
1194 prop
= zprop_name_to_prop(propname
, type
);
1196 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
))
1200 * When no property table entry can be found, return failure if
1201 * this is a pool property or if this isn't a user-defined
1204 if (prop
== ZPROP_INVAL
&& (type
== ZFS_TYPE_POOL
||
1205 !zfs_prop_user(propname
))) {
1206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1207 "invalid property '%s'"), propname
);
1208 return (zfs_error(hdl
, EZFS_BADPROP
,
1209 dgettext(TEXT_DOMAIN
, "bad property list")));
1212 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1215 entry
->pl_prop
= prop
;
1216 if (prop
== ZPROP_INVAL
) {
1217 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) == NULL
) {
1221 entry
->pl_width
= strlen(propname
);
1223 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1233 * Given a comma-separated list of properties, construct a property list
1234 * containing both user-defined and native properties. This function will
1235 * return a NULL list if 'all' is specified, which can later be expanded
1236 * by zprop_expand_list().
1239 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1245 * If 'all' is specified, return a NULL list.
1247 if (strcmp(props
, "all") == 0)
1251 * If no props were specified, return an error.
1253 if (props
[0] == '\0') {
1254 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1255 "no properties specified"));
1256 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1257 "bad property list")));
1261 * It would be nice to use getsubopt() here, but the inclusion of column
1262 * aliases makes this more effort than it's worth.
1264 while (*props
!= '\0') {
1269 if ((p
= strchr(props
, ',')) == NULL
) {
1270 len
= strlen(props
);
1277 * Check for empty options.
1280 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1281 "empty property name"));
1282 return (zfs_error(hdl
, EZFS_BADPROP
,
1283 dgettext(TEXT_DOMAIN
, "bad property list")));
1287 * Check all regular property names.
1292 if (strcmp(props
, "space") == 0) {
1293 static char *spaceprops
[] = {
1294 "name", "avail", "used", "usedbysnapshots",
1295 "usedbydataset", "usedbyrefreservation",
1296 "usedbychildren", NULL
1300 for (i
= 0; spaceprops
[i
]; i
++) {
1301 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
1303 listp
= &(*listp
)->pl_next
;
1306 if (addlist(hdl
, props
, listp
, type
))
1308 listp
= &(*listp
)->pl_next
;
1320 zprop_free_list(zprop_list_t
*pl
)
1324 while (pl
!= NULL
) {
1326 free(pl
->pl_user_prop
);
1332 typedef struct expand_data
{
1333 zprop_list_t
**last
;
1334 libzfs_handle_t
*hdl
;
1339 zprop_expand_list_cb(int prop
, void *cb
)
1341 zprop_list_t
*entry
;
1342 expand_data_t
*edp
= cb
;
1344 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
1345 return (ZPROP_INVAL
);
1347 entry
->pl_prop
= prop
;
1348 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
1349 entry
->pl_all
= B_TRUE
;
1351 *(edp
->last
) = entry
;
1352 edp
->last
= &entry
->pl_next
;
1354 return (ZPROP_CONT
);
1358 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
1360 zprop_list_t
*entry
;
1361 zprop_list_t
**last
;
1366 * If this is the very first time we've been called for an 'all'
1367 * specification, expand the list to include all native
1376 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
1377 B_FALSE
, type
) == ZPROP_INVAL
)
1381 * Add 'name' to the beginning of the list, which is handled
1384 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1387 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1389 entry
->pl_width
= zprop_width(entry
->pl_prop
,
1390 &entry
->pl_fixed
, type
);
1391 entry
->pl_all
= B_TRUE
;
1392 entry
->pl_next
= *plp
;
1399 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
1402 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));