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, 2017 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
31 * Internal utility routines for the ZFS library.
44 #include <sys/filio.h>
45 #include <sys/mnttab.h>
46 #include <sys/mntent.h>
47 #include <sys/types.h>
48 #include <libcmdutils.h>
51 #include <libzfs_core.h>
53 #include "libzfs_impl.h"
55 #include "zfeature_common.h"
58 libzfs_errno(libzfs_handle_t
*hdl
)
60 return (hdl
->libzfs_error
);
64 libzfs_error_action(libzfs_handle_t
*hdl
)
66 return (hdl
->libzfs_action
);
70 libzfs_error_description(libzfs_handle_t
*hdl
)
72 if (hdl
->libzfs_desc
[0] != '\0')
73 return (hdl
->libzfs_desc
);
75 switch (hdl
->libzfs_error
) {
77 return (dgettext(TEXT_DOMAIN
, "out of memory"));
79 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
80 case EZFS_PROPREADONLY
:
81 return (dgettext(TEXT_DOMAIN
, "read-only property"));
83 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
84 "datasets of this type"));
85 case EZFS_PROPNONINHERIT
:
86 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
88 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
90 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
91 "datasets of this type"));
93 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
95 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
97 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
99 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
100 case EZFS_DSREADONLY
:
101 return (dgettext(TEXT_DOMAIN
, "dataset is read-only"));
103 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
105 case EZFS_INVALIDNAME
:
106 return (dgettext(TEXT_DOMAIN
, "invalid name"));
107 case EZFS_BADRESTORE
:
108 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
111 return (dgettext(TEXT_DOMAIN
, "backup failed"));
113 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
115 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
117 return (dgettext(TEXT_DOMAIN
, "invalid device"));
118 case EZFS_NOREPLICAS
:
119 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
120 case EZFS_RESILVERING
:
121 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
122 case EZFS_BADVERSION
:
123 return (dgettext(TEXT_DOMAIN
, "unsupported version or "
125 case EZFS_POOLUNAVAIL
:
126 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
127 case EZFS_DEVOVERFLOW
:
128 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
130 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
131 case EZFS_CROSSTARGET
:
132 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
135 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
136 case EZFS_MOUNTFAILED
:
137 return (dgettext(TEXT_DOMAIN
, "mount failed"));
138 case EZFS_UMOUNTFAILED
:
139 return (dgettext(TEXT_DOMAIN
, "umount failed"));
140 case EZFS_UNSHARENFSFAILED
:
141 return (dgettext(TEXT_DOMAIN
, "unshare(8) failed"));
142 case EZFS_SHARENFSFAILED
:
143 return (dgettext(TEXT_DOMAIN
, "share(8) failed"));
144 case EZFS_UNSHARESMBFAILED
:
145 return (dgettext(TEXT_DOMAIN
, "smb remove share failed"));
146 case EZFS_SHARESMBFAILED
:
147 return (dgettext(TEXT_DOMAIN
, "smb add share failed"));
149 return (dgettext(TEXT_DOMAIN
, "permission denied"));
151 return (dgettext(TEXT_DOMAIN
, "out of space"));
153 return (dgettext(TEXT_DOMAIN
, "bad address"));
155 return (dgettext(TEXT_DOMAIN
, "I/O error"));
157 return (dgettext(TEXT_DOMAIN
, "signal received"));
159 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
161 case EZFS_INVALCONFIG
:
162 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
164 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
166 return (dgettext(TEXT_DOMAIN
, "no history available"));
168 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
170 case EZFS_POOL_NOTSUP
:
171 return (dgettext(TEXT_DOMAIN
, "operation not supported "
172 "on this type of pool"));
173 case EZFS_POOL_INVALARG
:
174 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
175 "this pool operation"));
176 case EZFS_NAMETOOLONG
:
177 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
178 case EZFS_OPENFAILED
:
179 return (dgettext(TEXT_DOMAIN
, "open failed"));
181 return (dgettext(TEXT_DOMAIN
,
182 "disk capacity information could not be retrieved"));
183 case EZFS_LABELFAILED
:
184 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
186 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
188 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
189 case EZFS_BADPERMSET
:
190 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
191 case EZFS_NODELEGATION
:
192 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
193 "disabled on pool"));
195 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
197 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
198 case EZFS_VDEVNOTSUP
:
199 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
202 return (dgettext(TEXT_DOMAIN
, "operation not supported "
204 case EZFS_ACTIVE_SPARE
:
205 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
207 case EZFS_UNPLAYED_LOGS
:
208 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
210 case EZFS_REFTAG_RELE
:
211 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
212 case EZFS_REFTAG_HOLD
:
213 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
215 case EZFS_TAGTOOLONG
:
216 return (dgettext(TEXT_DOMAIN
, "tag too long"));
217 case EZFS_PIPEFAILED
:
218 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
219 case EZFS_THREADCREATEFAILED
:
220 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
221 case EZFS_POSTSPLIT_ONLINE
:
222 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
224 case EZFS_SCRUB_PAUSED
:
225 return (dgettext(TEXT_DOMAIN
, "scrub is paused; "
226 "use 'zpool scrub' to resume"));
228 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
229 "use 'zpool scrub -s' to cancel current scrub"));
231 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
233 return (dgettext(TEXT_DOMAIN
, "unable to generate diffs"));
235 return (dgettext(TEXT_DOMAIN
, "invalid diff data"));
236 case EZFS_POOLREADONLY
:
237 return (dgettext(TEXT_DOMAIN
, "pool is read-only"));
238 case EZFS_NO_PENDING
:
239 return (dgettext(TEXT_DOMAIN
, "operation is not "
241 case EZFS_CHECKPOINT_EXISTS
:
242 return (dgettext(TEXT_DOMAIN
, "checkpoint exists"));
243 case EZFS_DISCARDING_CHECKPOINT
:
244 return (dgettext(TEXT_DOMAIN
, "currently discarding "
246 case EZFS_NO_CHECKPOINT
:
247 return (dgettext(TEXT_DOMAIN
, "checkpoint does not exist"));
248 case EZFS_DEVRM_IN_PROGRESS
:
249 return (dgettext(TEXT_DOMAIN
, "device removal in progress"));
250 case EZFS_VDEV_TOO_BIG
:
251 return (dgettext(TEXT_DOMAIN
, "device exceeds supported size"));
253 return (dgettext(TEXT_DOMAIN
, "unknown error"));
255 assert(hdl
->libzfs_error
== 0);
256 return (dgettext(TEXT_DOMAIN
, "no error"));
262 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
268 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
270 hdl
->libzfs_desc_active
= 1;
276 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
278 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
280 hdl
->libzfs_error
= error
;
282 if (hdl
->libzfs_desc_active
)
283 hdl
->libzfs_desc_active
= 0;
285 hdl
->libzfs_desc
[0] = '\0';
287 if (hdl
->libzfs_printerr
) {
288 if (error
== EZFS_UNKNOWN
) {
289 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
290 "error: %s\n"), libzfs_error_description(hdl
));
294 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
295 libzfs_error_description(hdl
));
296 if (error
== EZFS_NOMEM
)
302 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
304 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
309 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
315 zfs_verror(hdl
, error
, fmt
, ap
);
323 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
329 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
333 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
337 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
341 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
345 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
353 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
355 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
360 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
366 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
375 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
379 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
380 "dataset does not exist"));
381 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
386 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
390 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
391 "dataset already exists"));
392 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
396 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
398 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
401 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
404 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
407 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
411 "pool I/O is currently suspended"));
412 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
415 zfs_error_aux(hdl
, strerror(error
));
416 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
425 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
427 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
432 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
438 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
445 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
450 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
451 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
456 "pool already exists"));
457 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
461 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
462 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
466 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
467 "one or more devices is currently unavailable"));
468 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
472 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
476 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
480 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
485 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
489 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
490 "pool I/O is currently suspended"));
491 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
495 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
497 /* There is no pending operation to cancel */
499 zfs_verror(hdl
, EZFS_NO_PENDING
, fmt
, ap
);
501 case ZFS_ERR_CHECKPOINT_EXISTS
:
502 zfs_verror(hdl
, EZFS_CHECKPOINT_EXISTS
, fmt
, ap
);
504 case ZFS_ERR_DISCARDING_CHECKPOINT
:
505 zfs_verror(hdl
, EZFS_DISCARDING_CHECKPOINT
, fmt
, ap
);
507 case ZFS_ERR_NO_CHECKPOINT
:
508 zfs_verror(hdl
, EZFS_NO_CHECKPOINT
, fmt
, ap
);
510 case ZFS_ERR_DEVRM_IN_PROGRESS
:
511 zfs_verror(hdl
, EZFS_DEVRM_IN_PROGRESS
, fmt
, ap
);
513 case ZFS_ERR_VDEV_TOO_BIG
:
514 zfs_verror(hdl
, EZFS_VDEV_TOO_BIG
, fmt
, ap
);
517 zfs_error_aux(hdl
, strerror(error
));
518 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
526 * Display an out of memory error message and abort the current program.
529 no_memory(libzfs_handle_t
*hdl
)
531 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
535 * A safe form of malloc() which will die if the allocation fails.
538 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
542 if ((data
= calloc(1, size
)) == NULL
)
543 (void) no_memory(hdl
);
549 * A safe form of asprintf() which will die if the allocation fails.
553 zfs_asprintf(libzfs_handle_t
*hdl
, const char *fmt
, ...)
561 err
= vasprintf(&ret
, fmt
, ap
);
566 (void) no_memory(hdl
);
572 * A safe form of realloc(), which also zeroes newly allocated space.
575 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
579 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
580 (void) no_memory(hdl
);
584 bzero((char *)ret
+ oldsize
, (newsize
- oldsize
));
589 * A safe form of strdup() which will die if the allocation fails.
592 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
596 if ((ret
= strdup(str
)) == NULL
)
597 (void) no_memory(hdl
);
603 * Convert a number to an appropriately human-readable output.
606 zfs_nicenum(uint64_t num
, char *buf
, size_t buflen
)
608 nicenum(num
, buf
, buflen
);
612 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
614 hdl
->libzfs_printerr
= printerr
;
620 libzfs_handle_t
*hdl
;
622 if ((hdl
= calloc(1, sizeof (libzfs_handle_t
))) == NULL
) {
626 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
)) < 0) {
631 if ((hdl
->libzfs_mnttab
= fopen(MNTTAB
, "rF")) == NULL
) {
632 (void) close(hdl
->libzfs_fd
);
637 hdl
->libzfs_sharetab
= fopen("/etc/dfs/sharetab", "rF");
639 if (libzfs_core_init() != 0) {
640 (void) close(hdl
->libzfs_fd
);
641 (void) fclose(hdl
->libzfs_mnttab
);
642 (void) fclose(hdl
->libzfs_sharetab
);
649 zpool_feature_init();
650 libzfs_mnttab_init(hdl
);
652 if (getenv("ZFS_PROP_DEBUG") != NULL
) {
653 hdl
->libzfs_prop_debug
= B_TRUE
;
660 libzfs_fini(libzfs_handle_t
*hdl
)
662 (void) close(hdl
->libzfs_fd
);
663 if (hdl
->libzfs_mnttab
)
664 (void) fclose(hdl
->libzfs_mnttab
);
665 if (hdl
->libzfs_sharetab
)
666 (void) fclose(hdl
->libzfs_sharetab
);
667 zfs_uninit_libshare(hdl
);
668 zpool_free_handles(hdl
);
669 libzfs_fru_clear(hdl
, B_TRUE
);
670 namespace_clear(hdl
);
671 libzfs_mnttab_fini(hdl
);
677 zpool_get_handle(zpool_handle_t
*zhp
)
679 return (zhp
->zpool_hdl
);
683 zfs_get_handle(zfs_handle_t
*zhp
)
685 return (zhp
->zfs_hdl
);
689 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
691 return (zhp
->zpool_hdl
);
695 * Given a name, determine whether or not it's a valid path
696 * (starts with '/' or "./"). If so, walk the mnttab trying
697 * to match the device number. If not, treat the path as an
698 * fs/vol/snap/bkmark name.
701 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, char *path
, zfs_type_t argtype
)
703 struct stat64 statbuf
;
704 struct extmnttab entry
;
707 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
709 * It's not a valid path, assume it's a name of type 'argtype'.
711 return (zfs_open(hdl
, path
, argtype
));
714 if (stat64(path
, &statbuf
) != 0) {
715 (void) fprintf(stderr
, "%s: %s\n", path
, strerror(errno
));
719 rewind(hdl
->libzfs_mnttab
);
720 while ((ret
= getextmntent(hdl
->libzfs_mnttab
, &entry
, 0)) == 0) {
721 if (makedevice(entry
.mnt_major
, entry
.mnt_minor
) ==
730 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
731 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
736 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
740 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
744 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
748 zc
->zc_nvlist_dst_size
= len
;
750 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
751 if (zc
->zc_nvlist_dst
== 0)
758 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
759 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
760 * filled in by the kernel to indicate the actual required size.
763 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
765 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
767 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
768 if (zc
->zc_nvlist_dst
== 0)
775 * Called to free the src and dst nvlists stored in the command structure.
778 zcmd_free_nvlists(zfs_cmd_t
*zc
)
780 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
781 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
782 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
783 zc
->zc_nvlist_conf
= (uintptr_t)NULL
;
784 zc
->zc_nvlist_src
= (uintptr_t)NULL
;
785 zc
->zc_nvlist_dst
= (uintptr_t)NULL
;
789 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
795 verify(nvlist_size(nvl
, &len
, NV_ENCODE_NATIVE
) == 0);
797 if ((packed
= zfs_alloc(hdl
, len
)) == NULL
)
800 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
802 *outnv
= (uint64_t)(uintptr_t)packed
;
809 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
811 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
812 &zc
->zc_nvlist_conf_size
, nvl
));
816 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
818 return (zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
819 &zc
->zc_nvlist_src_size
, nvl
));
823 * Unpacks an nvlist from the ZFS ioctl command structure.
826 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
828 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
829 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
830 return (no_memory(hdl
));
836 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
838 return (ioctl(hdl
->libzfs_fd
, request
, zc
));
842 * ================================================================
843 * API shared by zfs and zpool property management
844 * ================================================================
848 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
850 zprop_list_t
*pl
= cbp
->cb_proplist
;
855 cbp
->cb_first
= B_FALSE
;
856 if (cbp
->cb_scripted
)
860 * Start with the length of the column headers.
862 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
863 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
865 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
867 cbp
->cb_colwidths
[GET_COL_RECVD
] = strlen(dgettext(TEXT_DOMAIN
,
869 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
872 /* first property is always NAME */
873 assert(cbp
->cb_proplist
->pl_prop
==
874 ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
: ZFS_PROP_NAME
));
877 * Go through and calculate the widths for each column. For the
878 * 'source' column, we kludge it up by taking the worst-case scenario of
879 * inheriting from the longest name. This is acceptable because in the
880 * majority of cases 'SOURCE' is the last column displayed, and we don't
881 * use the width anyway. Note that the 'VALUE' column can be oversized,
882 * if the name of the property is much longer than any values we find.
884 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
888 if (pl
->pl_prop
!= ZPROP_INVAL
) {
889 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
890 zpool_prop_to_name(pl
->pl_prop
) :
891 zfs_prop_to_name(pl
->pl_prop
);
893 len
= strlen(propname
);
894 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
895 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
897 len
= strlen(pl
->pl_user_prop
);
898 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
899 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
903 * 'VALUE' column. The first property is always the 'name'
904 * property that was tacked on either by /sbin/zfs's
905 * zfs_do_get() or when calling zprop_expand_list(), so we
906 * ignore its width. If the user specified the name property
907 * to display, then it will be later in the list in any case.
909 if (pl
!= cbp
->cb_proplist
&&
910 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
911 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
913 /* 'RECEIVED' column. */
914 if (pl
!= cbp
->cb_proplist
&&
915 pl
->pl_recvd_width
> cbp
->cb_colwidths
[GET_COL_RECVD
])
916 cbp
->cb_colwidths
[GET_COL_RECVD
] = pl
->pl_recvd_width
;
919 * 'NAME' and 'SOURCE' columns
921 if (pl
->pl_prop
== (type
== ZFS_TYPE_POOL
? ZPOOL_PROP_NAME
:
923 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_NAME
]) {
924 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
925 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
926 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
931 * Now go through and print the headers.
933 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
934 switch (cbp
->cb_columns
[i
]) {
936 title
= dgettext(TEXT_DOMAIN
, "NAME");
938 case GET_COL_PROPERTY
:
939 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
942 title
= dgettext(TEXT_DOMAIN
, "VALUE");
945 title
= dgettext(TEXT_DOMAIN
, "RECEIVED");
948 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
955 if (i
== (ZFS_GET_NCOLS
- 1) ||
956 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
957 (void) printf("%s", title
);
959 (void) printf("%-*s ",
960 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
968 * Display a single line of output, according to the settings in the callback
972 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
973 const char *propname
, const char *value
, zprop_source_t sourcetype
,
974 const char *source
, const char *recvd_value
)
977 const char *str
= NULL
;
981 * Ignore those source types that the user has chosen to ignore.
983 if ((sourcetype
& cbp
->cb_sources
) == 0)
987 zprop_print_headers(cbp
, cbp
->cb_type
);
989 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
990 switch (cbp
->cb_columns
[i
]) {
995 case GET_COL_PROPERTY
:
1003 case GET_COL_SOURCE
:
1004 switch (sourcetype
) {
1005 case ZPROP_SRC_NONE
:
1009 case ZPROP_SRC_DEFAULT
:
1013 case ZPROP_SRC_LOCAL
:
1017 case ZPROP_SRC_TEMPORARY
:
1021 case ZPROP_SRC_INHERITED
:
1022 (void) snprintf(buf
, sizeof (buf
),
1023 "inherited from %s", source
);
1026 case ZPROP_SRC_RECEIVED
:
1032 assert(!"unhandled zprop_source_t");
1037 str
= (recvd_value
== NULL
? "-" : recvd_value
);
1044 if (cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1045 (void) printf("%s", str
);
1046 else if (cbp
->cb_scripted
)
1047 (void) printf("%s\t", str
);
1049 (void) printf("%-*s ",
1050 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1054 (void) printf("\n");
1058 * Given a numeric suffix, convert the value into a number of bits that the
1059 * resulting value must be shifted.
1062 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1064 const char *ends
= "BKMGTPEZ";
1069 for (i
= 0; i
< strlen(ends
); i
++) {
1070 if (toupper(buf
[0]) == ends
[i
])
1073 if (i
== strlen(ends
)) {
1074 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1075 "invalid numeric suffix '%s'"), buf
);
1080 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
1081 * allow 'BB' - that's just weird.
1083 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0' &&
1084 toupper(buf
[0]) != 'B'))
1087 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1088 "invalid numeric suffix '%s'"), buf
);
1093 * Convert a string of the form '100G' into a real number. Used when setting
1094 * properties or creating a volume. 'buf' is used to place an extended error
1095 * message for the caller to use.
1098 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1105 /* Check to see if this looks like a number. */
1106 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1109 "bad numeric value '%s'"), value
);
1113 /* Rely on strtoull() to process the numeric portion. */
1115 *num
= strtoull(value
, &end
, 10);
1118 * Check for ERANGE, which indicates that the value is too large to fit
1119 * in a 64-bit value.
1121 if (errno
== ERANGE
) {
1123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1124 "numeric value is too large"));
1129 * If we have a decimal value, then do the computation with floating
1130 * point arithmetic. Otherwise, use standard arithmetic.
1133 double fval
= strtod(value
, &end
);
1135 if ((shift
= str2shift(hdl
, end
)) == -1)
1138 fval
*= pow(2, shift
);
1140 if (fval
> UINT64_MAX
) {
1142 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1143 "numeric value is too large"));
1147 *num
= (uint64_t)fval
;
1149 if ((shift
= str2shift(hdl
, end
)) == -1)
1152 /* Check for overflow */
1153 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1156 "numeric value is too large"));
1167 * Given a propname=value nvpair to set, parse any numeric properties
1168 * (index, boolean, etc) if they are specified as strings and add the
1169 * resulting nvpair to the returned nvlist.
1171 * At the DSL layer, all properties are either 64-bit numbers or strings.
1172 * We want the user to be able to ignore this fact and specify properties
1173 * as native values (numbers, for example) or as strings (to simplify
1174 * command line utilities). This also handles converting index types
1175 * (compression, checksum, etc) from strings to their on-disk index.
1178 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1179 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1182 data_type_t datatype
= nvpair_type(elem
);
1183 zprop_type_t proptype
;
1184 const char *propname
;
1186 boolean_t isnone
= B_FALSE
;
1188 if (type
== ZFS_TYPE_POOL
) {
1189 proptype
= zpool_prop_get_type(prop
);
1190 propname
= zpool_prop_to_name(prop
);
1192 proptype
= zfs_prop_get_type(prop
);
1193 propname
= zfs_prop_to_name(prop
);
1197 * Convert any properties to the internal DSL value types.
1203 case PROP_TYPE_STRING
:
1204 if (datatype
!= DATA_TYPE_STRING
) {
1205 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1206 "'%s' must be a string"), nvpair_name(elem
));
1209 (void) nvpair_value_string(elem
, svalp
);
1210 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1211 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1212 "'%s' is too long"), nvpair_name(elem
));
1217 case PROP_TYPE_NUMBER
:
1218 if (datatype
== DATA_TYPE_STRING
) {
1219 (void) nvpair_value_string(elem
, &value
);
1220 if (strcmp(value
, "none") == 0) {
1222 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
)
1226 } else if (datatype
== DATA_TYPE_UINT64
) {
1227 (void) nvpair_value_uint64(elem
, ivalp
);
1229 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1230 "'%s' must be a number"), nvpair_name(elem
));
1235 * Quota special: force 'none' and don't allow 0.
1237 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1238 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1239 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1240 "use 'none' to disable quota/refquota"));
1245 * Special handling for "*_limit=none". In this case it's not
1248 if ((type
& ZFS_TYPE_DATASET
) && isnone
&&
1249 (prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
1250 prop
== ZFS_PROP_SNAPSHOT_LIMIT
)) {
1251 *ivalp
= UINT64_MAX
;
1255 case PROP_TYPE_INDEX
:
1256 if (datatype
!= DATA_TYPE_STRING
) {
1257 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1258 "'%s' must be a string"), nvpair_name(elem
));
1262 (void) nvpair_value_string(elem
, &value
);
1264 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1265 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1266 "'%s' must be one of '%s'"), propname
,
1267 zprop_values(prop
, type
));
1277 * Add the result to our return set of properties.
1279 if (*svalp
!= NULL
) {
1280 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1281 (void) no_memory(hdl
);
1285 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1286 (void) no_memory(hdl
);
1293 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1298 addlist(libzfs_handle_t
*hdl
, char *propname
, zprop_list_t
**listp
,
1302 zprop_list_t
*entry
;
1304 prop
= zprop_name_to_prop(propname
, type
);
1306 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
))
1310 * When no property table entry can be found, return failure if
1311 * this is a pool property or if this isn't a user-defined
1314 if (prop
== ZPROP_INVAL
&& ((type
== ZFS_TYPE_POOL
&&
1315 !zpool_prop_feature(propname
) &&
1316 !zpool_prop_unsupported(propname
)) ||
1317 (type
== ZFS_TYPE_DATASET
&& !zfs_prop_user(propname
) &&
1318 !zfs_prop_userquota(propname
) && !zfs_prop_written(propname
)))) {
1319 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1320 "invalid property '%s'"), propname
);
1321 return (zfs_error(hdl
, EZFS_BADPROP
,
1322 dgettext(TEXT_DOMAIN
, "bad property list")));
1325 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1328 entry
->pl_prop
= prop
;
1329 if (prop
== ZPROP_INVAL
) {
1330 if ((entry
->pl_user_prop
= zfs_strdup(hdl
, propname
)) ==
1335 entry
->pl_width
= strlen(propname
);
1337 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1347 * Given a comma-separated list of properties, construct a property list
1348 * containing both user-defined and native properties. This function will
1349 * return a NULL list if 'all' is specified, which can later be expanded
1350 * by zprop_expand_list().
1353 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1359 * If 'all' is specified, return a NULL list.
1361 if (strcmp(props
, "all") == 0)
1365 * If no props were specified, return an error.
1367 if (props
[0] == '\0') {
1368 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1369 "no properties specified"));
1370 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1371 "bad property list")));
1375 * It would be nice to use getsubopt() here, but the inclusion of column
1376 * aliases makes this more effort than it's worth.
1378 while (*props
!= '\0') {
1383 if ((p
= strchr(props
, ',')) == NULL
) {
1384 len
= strlen(props
);
1391 * Check for empty options.
1394 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1395 "empty property name"));
1396 return (zfs_error(hdl
, EZFS_BADPROP
,
1397 dgettext(TEXT_DOMAIN
, "bad property list")));
1401 * Check all regular property names.
1406 if (strcmp(props
, "space") == 0) {
1407 static char *spaceprops
[] = {
1408 "name", "avail", "used", "usedbysnapshots",
1409 "usedbydataset", "usedbyrefreservation",
1410 "usedbychildren", NULL
1414 for (i
= 0; spaceprops
[i
]; i
++) {
1415 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
1417 listp
= &(*listp
)->pl_next
;
1420 if (addlist(hdl
, props
, listp
, type
))
1422 listp
= &(*listp
)->pl_next
;
1434 zprop_free_list(zprop_list_t
*pl
)
1438 while (pl
!= NULL
) {
1440 free(pl
->pl_user_prop
);
1446 typedef struct expand_data
{
1447 zprop_list_t
**last
;
1448 libzfs_handle_t
*hdl
;
1453 zprop_expand_list_cb(int prop
, void *cb
)
1455 zprop_list_t
*entry
;
1456 expand_data_t
*edp
= cb
;
1458 if ((entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
))) == NULL
)
1459 return (ZPROP_INVAL
);
1461 entry
->pl_prop
= prop
;
1462 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
1463 entry
->pl_all
= B_TRUE
;
1465 *(edp
->last
) = entry
;
1466 edp
->last
= &entry
->pl_next
;
1468 return (ZPROP_CONT
);
1472 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
1474 zprop_list_t
*entry
;
1475 zprop_list_t
**last
;
1480 * If this is the very first time we've been called for an 'all'
1481 * specification, expand the list to include all native
1490 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
1491 B_FALSE
, type
) == ZPROP_INVAL
)
1495 * Add 'name' to the beginning of the list, which is handled
1498 if ((entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
))) == NULL
)
1501 entry
->pl_prop
= (type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1503 entry
->pl_width
= zprop_width(entry
->pl_prop
,
1504 &entry
->pl_fixed
, type
);
1505 entry
->pl_all
= B_TRUE
;
1506 entry
->pl_next
= *plp
;
1513 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
1516 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));
1520 * zfs_get_hole_count retrieves the number of holes (blocks which are
1521 * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It
1522 * also optionally fetches the block size when bs is non-NULL. With hole count
1523 * and block size the full space consumed by the holes of a file can be
1526 * On success, zero is returned, the count argument is set to the
1527 * number of holes, and the bs argument is set to the block size (if it is
1528 * not NULL). On error, a non-zero errno is returned and the values in count
1529 * and bs are undefined.
1532 zfs_get_hole_count(const char *path
, uint64_t *count
, uint64_t *bs
)
1538 fd
= open(path
, O_RDONLY
| O_LARGEFILE
);
1542 if (ioctl(fd
, _FIO_COUNT_FILLED
, &fill
) == -1) {
1548 if (fstat64(fd
, &ss
) == -1) {
1554 *count
= (ss
.st_size
+ ss
.st_blksize
- 1) / ss
.st_blksize
- fill
;
1555 VERIFY3S(*count
, >=, 0);
1557 *bs
= ss
.st_blksize
;
1560 if (close(fd
) == -1) {