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 https://opensource.org/licenses/CDDL-1.0.
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 2020 Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2020 The FreeBSD Foundation
30 * Portions of this software were developed by Allan Jude
31 * under sponsorship from the FreeBSD Foundation.
35 * Internal utility routines for the ZFS library.
51 #include <sys/mnttab.h>
52 #include <sys/mntent.h>
53 #include <sys/types.h>
57 #include <libzfs_core.h>
59 #include "libzfs_impl.h"
61 #include "zfeature_common.h"
62 #include <zfs_fletcher.h>
66 * We only care about the scheme in order to match the scheme
67 * with the handler. Each handler should validate the full URI
70 #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):"
73 libzfs_errno(libzfs_handle_t
*hdl
)
75 return (hdl
->libzfs_error
);
79 libzfs_error_action(libzfs_handle_t
*hdl
)
81 return (hdl
->libzfs_action
);
85 libzfs_error_description(libzfs_handle_t
*hdl
)
87 if (hdl
->libzfs_desc
[0] != '\0')
88 return (hdl
->libzfs_desc
);
90 switch (hdl
->libzfs_error
) {
92 return (dgettext(TEXT_DOMAIN
, "out of memory"));
94 return (dgettext(TEXT_DOMAIN
, "invalid property value"));
95 case EZFS_PROPREADONLY
:
96 return (dgettext(TEXT_DOMAIN
, "read-only property"));
98 return (dgettext(TEXT_DOMAIN
, "property doesn't apply to "
99 "datasets of this type"));
100 case EZFS_PROPNONINHERIT
:
101 return (dgettext(TEXT_DOMAIN
, "property cannot be inherited"));
103 return (dgettext(TEXT_DOMAIN
, "invalid quota or reservation"));
105 return (dgettext(TEXT_DOMAIN
, "operation not applicable to "
106 "datasets of this type"));
108 return (dgettext(TEXT_DOMAIN
, "pool or dataset is busy"));
110 return (dgettext(TEXT_DOMAIN
, "pool or dataset exists"));
112 return (dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
114 return (dgettext(TEXT_DOMAIN
, "invalid backup stream"));
115 case EZFS_DSREADONLY
:
116 return (dgettext(TEXT_DOMAIN
, "dataset is read-only"));
118 return (dgettext(TEXT_DOMAIN
, "volume size exceeds limit for "
120 case EZFS_INVALIDNAME
:
121 return (dgettext(TEXT_DOMAIN
, "invalid name"));
122 case EZFS_BADRESTORE
:
123 return (dgettext(TEXT_DOMAIN
, "unable to restore to "
126 return (dgettext(TEXT_DOMAIN
, "backup failed"));
128 return (dgettext(TEXT_DOMAIN
, "invalid target vdev"));
130 return (dgettext(TEXT_DOMAIN
, "no such device in pool"));
132 return (dgettext(TEXT_DOMAIN
, "invalid device"));
133 case EZFS_NOREPLICAS
:
134 return (dgettext(TEXT_DOMAIN
, "no valid replicas"));
135 case EZFS_RESILVERING
:
136 return (dgettext(TEXT_DOMAIN
, "currently resilvering"));
137 case EZFS_BADVERSION
:
138 return (dgettext(TEXT_DOMAIN
, "unsupported version or "
140 case EZFS_POOLUNAVAIL
:
141 return (dgettext(TEXT_DOMAIN
, "pool is unavailable"));
142 case EZFS_DEVOVERFLOW
:
143 return (dgettext(TEXT_DOMAIN
, "too many devices in one vdev"));
145 return (dgettext(TEXT_DOMAIN
, "must be an absolute path"));
146 case EZFS_CROSSTARGET
:
147 return (dgettext(TEXT_DOMAIN
, "operation crosses datasets or "
150 return (dgettext(TEXT_DOMAIN
, "dataset in use by local zone"));
151 case EZFS_MOUNTFAILED
:
152 return (dgettext(TEXT_DOMAIN
, "mount failed"));
153 case EZFS_UMOUNTFAILED
:
154 return (dgettext(TEXT_DOMAIN
, "unmount failed"));
155 case EZFS_UNSHARENFSFAILED
:
156 return (dgettext(TEXT_DOMAIN
, "NFS share removal failed"));
157 case EZFS_SHARENFSFAILED
:
158 return (dgettext(TEXT_DOMAIN
, "NFS share creation failed"));
159 case EZFS_UNSHARESMBFAILED
:
160 return (dgettext(TEXT_DOMAIN
, "SMB share removal failed"));
161 case EZFS_SHARESMBFAILED
:
162 return (dgettext(TEXT_DOMAIN
, "SMB share creation failed"));
164 return (dgettext(TEXT_DOMAIN
, "permission denied"));
166 return (dgettext(TEXT_DOMAIN
, "out of space"));
168 return (dgettext(TEXT_DOMAIN
, "bad address"));
170 return (dgettext(TEXT_DOMAIN
, "I/O error"));
172 return (dgettext(TEXT_DOMAIN
, "signal received"));
174 return (dgettext(TEXT_DOMAIN
, "insufficient replicas"));
176 return (dgettext(TEXT_DOMAIN
, "device is reserved as a hot "
178 case EZFS_INVALCONFIG
:
179 return (dgettext(TEXT_DOMAIN
, "invalid vdev configuration"));
181 return (dgettext(TEXT_DOMAIN
, "recursive dataset dependency"));
183 return (dgettext(TEXT_DOMAIN
, "no history available"));
185 return (dgettext(TEXT_DOMAIN
, "failed to retrieve "
187 case EZFS_POOL_NOTSUP
:
188 return (dgettext(TEXT_DOMAIN
, "operation not supported "
189 "on this type of pool"));
190 case EZFS_POOL_INVALARG
:
191 return (dgettext(TEXT_DOMAIN
, "invalid argument for "
192 "this pool operation"));
193 case EZFS_NAMETOOLONG
:
194 return (dgettext(TEXT_DOMAIN
, "dataset name is too long"));
195 case EZFS_OPENFAILED
:
196 return (dgettext(TEXT_DOMAIN
, "open failed"));
198 return (dgettext(TEXT_DOMAIN
,
199 "disk capacity information could not be retrieved"));
200 case EZFS_LABELFAILED
:
201 return (dgettext(TEXT_DOMAIN
, "write of label failed"));
203 return (dgettext(TEXT_DOMAIN
, "invalid user/group"));
205 return (dgettext(TEXT_DOMAIN
, "invalid permission"));
206 case EZFS_BADPERMSET
:
207 return (dgettext(TEXT_DOMAIN
, "invalid permission set name"));
208 case EZFS_NODELEGATION
:
209 return (dgettext(TEXT_DOMAIN
, "delegated administration is "
210 "disabled on pool"));
212 return (dgettext(TEXT_DOMAIN
, "invalid or missing cache file"));
214 return (dgettext(TEXT_DOMAIN
, "device is in use as a cache"));
215 case EZFS_VDEVNOTSUP
:
216 return (dgettext(TEXT_DOMAIN
, "vdev specification is not "
219 return (dgettext(TEXT_DOMAIN
, "operation not supported "
221 case EZFS_IOC_NOTSUPPORTED
:
222 return (dgettext(TEXT_DOMAIN
, "operation not supported by "
223 "zfs kernel module"));
224 case EZFS_ACTIVE_SPARE
:
225 return (dgettext(TEXT_DOMAIN
, "pool has active shared spare "
227 case EZFS_UNPLAYED_LOGS
:
228 return (dgettext(TEXT_DOMAIN
, "log device has unplayed intent "
230 case EZFS_REFTAG_RELE
:
231 return (dgettext(TEXT_DOMAIN
, "no such tag on this dataset"));
232 case EZFS_REFTAG_HOLD
:
233 return (dgettext(TEXT_DOMAIN
, "tag already exists on this "
235 case EZFS_TAGTOOLONG
:
236 return (dgettext(TEXT_DOMAIN
, "tag too long"));
237 case EZFS_PIPEFAILED
:
238 return (dgettext(TEXT_DOMAIN
, "pipe create failed"));
239 case EZFS_THREADCREATEFAILED
:
240 return (dgettext(TEXT_DOMAIN
, "thread create failed"));
241 case EZFS_POSTSPLIT_ONLINE
:
242 return (dgettext(TEXT_DOMAIN
, "disk was split from this pool "
244 case EZFS_SCRUB_PAUSED
:
245 return (dgettext(TEXT_DOMAIN
, "scrub is paused; "
246 "use 'zpool scrub' to resume"));
248 return (dgettext(TEXT_DOMAIN
, "currently scrubbing; "
249 "use 'zpool scrub -s' to cancel current scrub"));
251 return (dgettext(TEXT_DOMAIN
, "there is no active scrub"));
253 return (dgettext(TEXT_DOMAIN
, "unable to generate diffs"));
255 return (dgettext(TEXT_DOMAIN
, "invalid diff data"));
256 case EZFS_POOLREADONLY
:
257 return (dgettext(TEXT_DOMAIN
, "pool is read-only"));
258 case EZFS_NO_PENDING
:
259 return (dgettext(TEXT_DOMAIN
, "operation is not "
261 case EZFS_CHECKPOINT_EXISTS
:
262 return (dgettext(TEXT_DOMAIN
, "checkpoint exists"));
263 case EZFS_DISCARDING_CHECKPOINT
:
264 return (dgettext(TEXT_DOMAIN
, "currently discarding "
266 case EZFS_NO_CHECKPOINT
:
267 return (dgettext(TEXT_DOMAIN
, "checkpoint does not exist"));
268 case EZFS_DEVRM_IN_PROGRESS
:
269 return (dgettext(TEXT_DOMAIN
, "device removal in progress"));
270 case EZFS_VDEV_TOO_BIG
:
271 return (dgettext(TEXT_DOMAIN
, "device exceeds supported size"));
272 case EZFS_ACTIVE_POOL
:
273 return (dgettext(TEXT_DOMAIN
, "pool is imported on a "
275 case EZFS_CRYPTOFAILED
:
276 return (dgettext(TEXT_DOMAIN
, "encryption failure"));
278 return (dgettext(TEXT_DOMAIN
, "argument list too long"));
279 case EZFS_INITIALIZING
:
280 return (dgettext(TEXT_DOMAIN
, "currently initializing"));
281 case EZFS_NO_INITIALIZE
:
282 return (dgettext(TEXT_DOMAIN
, "there is no active "
284 case EZFS_WRONG_PARENT
:
285 return (dgettext(TEXT_DOMAIN
, "invalid parent dataset"));
287 return (dgettext(TEXT_DOMAIN
, "currently trimming"));
289 return (dgettext(TEXT_DOMAIN
, "there is no active trim"));
290 case EZFS_TRIM_NOTSUP
:
291 return (dgettext(TEXT_DOMAIN
, "trim operations are not "
292 "supported by this device"));
293 case EZFS_NO_RESILVER_DEFER
:
294 return (dgettext(TEXT_DOMAIN
, "this action requires the "
295 "resilver_defer feature"));
296 case EZFS_EXPORT_IN_PROGRESS
:
297 return (dgettext(TEXT_DOMAIN
, "pool export in progress"));
298 case EZFS_REBUILDING
:
299 return (dgettext(TEXT_DOMAIN
, "currently sequentially "
301 case EZFS_VDEV_NOTSUP
:
302 return (dgettext(TEXT_DOMAIN
, "operation not supported "
303 "on this type of vdev"));
304 case EZFS_NOT_USER_NAMESPACE
:
305 return (dgettext(TEXT_DOMAIN
, "the provided file "
306 "was not a user namespace file"));
307 case EZFS_RESUME_EXISTS
:
308 return (dgettext(TEXT_DOMAIN
, "Resuming recv on existing "
309 "dataset without force"));
311 return (dgettext(TEXT_DOMAIN
, "unknown error"));
313 assert(hdl
->libzfs_error
== 0);
314 return (dgettext(TEXT_DOMAIN
, "no error"));
319 zfs_error_aux(libzfs_handle_t
*hdl
, const char *fmt
, ...)
325 (void) vsnprintf(hdl
->libzfs_desc
, sizeof (hdl
->libzfs_desc
),
327 hdl
->libzfs_desc_active
= 1;
333 zfs_verror(libzfs_handle_t
*hdl
, int error
, const char *fmt
, va_list ap
)
335 (void) vsnprintf(hdl
->libzfs_action
, sizeof (hdl
->libzfs_action
),
337 hdl
->libzfs_error
= error
;
339 if (hdl
->libzfs_desc_active
)
340 hdl
->libzfs_desc_active
= 0;
342 hdl
->libzfs_desc
[0] = '\0';
344 if (hdl
->libzfs_printerr
) {
345 if (error
== EZFS_UNKNOWN
) {
346 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "internal "
347 "error: %s: %s\n"), hdl
->libzfs_action
,
348 libzfs_error_description(hdl
));
352 (void) fprintf(stderr
, "%s: %s\n", hdl
->libzfs_action
,
353 libzfs_error_description(hdl
));
354 if (error
== EZFS_NOMEM
)
360 zfs_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
362 return (zfs_error_fmt(hdl
, error
, "%s", msg
));
366 zfs_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
372 zfs_verror(hdl
, error
, fmt
, ap
);
380 zfs_common_error(libzfs_handle_t
*hdl
, int error
, const char *fmt
,
386 zfs_verror(hdl
, EZFS_PERM
, fmt
, ap
);
390 zfs_verror(hdl
, EZFS_NODELEGATION
, fmt
, ap
);
394 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
398 zfs_verror(hdl
, EZFS_FAULT
, fmt
, ap
);
402 zfs_verror(hdl
, EZFS_INTR
, fmt
, ap
);
406 zfs_verror(hdl
, EZFS_CKSUM
, fmt
, ap
);
414 zfs_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
416 return (zfs_standard_error_fmt(hdl
, error
, "%s", msg
));
420 zfs_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
426 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
435 zfs_verror(hdl
, EZFS_IO
, fmt
, ap
);
439 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
440 "dataset does not exist"));
441 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
446 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
450 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
451 "dataset already exists"));
452 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
456 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
458 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
461 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
464 zfs_verror(hdl
, EZFS_NAMETOOLONG
, fmt
, ap
);
467 zfs_verror(hdl
, EZFS_BADVERSION
, fmt
, ap
);
470 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
471 "pool I/O is currently suspended"));
472 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
475 zfs_verror(hdl
, EZFS_ACTIVE_POOL
, fmt
, ap
);
477 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE
:
478 case ZFS_ERR_IOC_CMD_UNAVAIL
:
479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "the loaded zfs "
480 "module does not support this operation. A reboot may "
481 "be required to enable this operation."));
482 zfs_verror(hdl
, EZFS_IOC_NOTSUPPORTED
, fmt
, ap
);
484 case ZFS_ERR_IOC_ARG_UNAVAIL
:
485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "the loaded zfs "
486 "module does not support an option for this operation. "
487 "A reboot may be required to enable this option."));
488 zfs_verror(hdl
, EZFS_IOC_NOTSUPPORTED
, fmt
, ap
);
490 case ZFS_ERR_IOC_ARG_REQUIRED
:
491 case ZFS_ERR_IOC_ARG_BADTYPE
:
492 zfs_verror(hdl
, EZFS_IOC_NOTSUPPORTED
, fmt
, ap
);
494 case ZFS_ERR_WRONG_PARENT
:
495 zfs_verror(hdl
, EZFS_WRONG_PARENT
, fmt
, ap
);
497 case ZFS_ERR_BADPROP
:
498 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
500 case ZFS_ERR_NOT_USER_NAMESPACE
:
501 zfs_verror(hdl
, EZFS_NOT_USER_NAMESPACE
, fmt
, ap
);
504 zfs_error_aux(hdl
, "%s", strerror(error
));
505 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
514 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
521 * For quotas and reservations, ENOSPC indicates
522 * something different; setting a quota or reservation
523 * doesn't use any disk space.
527 case ZFS_PROP_REFQUOTA
:
528 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
529 "size is less than current used or "
531 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
534 case ZFS_PROP_RESERVATION
:
535 case ZFS_PROP_REFRESERVATION
:
536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
537 "size is greater than available space"));
538 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
542 (void) zfs_standard_error(hdl
, err
, errbuf
);
548 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
552 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
557 "property value too long"));
558 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
563 "pool and or dataset must be upgraded to set this "
564 "property or value"));
565 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
569 if (prop
== ZFS_PROP_COMPRESSION
||
570 prop
== ZFS_PROP_DNODESIZE
||
571 prop
== ZFS_PROP_RECORDSIZE
) {
572 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
573 "property setting is not allowed on "
574 "bootable datasets"));
575 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
576 } else if (prop
== ZFS_PROP_CHECKSUM
||
577 prop
== ZFS_PROP_DEDUP
) {
578 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
579 "property setting is not allowed on "
581 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
583 (void) zfs_standard_error(hdl
, err
, errbuf
);
588 if (prop
== ZPROP_INVAL
) {
589 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
591 (void) zfs_standard_error(hdl
, err
, errbuf
);
595 case ZFS_ERR_BADPROP
:
596 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
600 if (prop
== ZFS_PROP_KEYLOCATION
) {
601 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
602 "keylocation may only be set on encryption roots"));
603 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
605 (void) zfs_standard_error(hdl
, err
, errbuf
);
611 * This platform can't address a volume this big.
614 if (prop
== ZFS_PROP_VOLSIZE
) {
615 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
621 (void) zfs_standard_error(hdl
, err
, errbuf
);
626 zpool_standard_error(libzfs_handle_t
*hdl
, int error
, const char *msg
)
628 return (zpool_standard_error_fmt(hdl
, error
, "%s", msg
));
632 zpool_standard_error_fmt(libzfs_handle_t
*hdl
, int error
, const char *fmt
, ...)
638 if (zfs_common_error(hdl
, error
, fmt
, ap
) != 0) {
645 zfs_verror(hdl
, EZFS_NODEVICE
, fmt
, ap
);
650 dgettext(TEXT_DOMAIN
, "no such pool or dataset"));
651 zfs_verror(hdl
, EZFS_NOENT
, fmt
, ap
);
655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
656 "pool already exists"));
657 zfs_verror(hdl
, EZFS_EXISTS
, fmt
, ap
);
661 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool is busy"));
662 zfs_verror(hdl
, EZFS_BUSY
, fmt
, ap
);
665 /* There is no pending operation to cancel */
667 zfs_verror(hdl
, EZFS_NO_PENDING
, fmt
, ap
);
671 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
672 "one or more devices is currently unavailable"));
673 zfs_verror(hdl
, EZFS_BADDEV
, fmt
, ap
);
677 zfs_verror(hdl
, EZFS_DEVOVERFLOW
, fmt
, ap
);
681 zfs_verror(hdl
, EZFS_POOL_NOTSUP
, fmt
, ap
);
685 zfs_verror(hdl
, EZFS_POOL_INVALARG
, fmt
, ap
);
690 zfs_verror(hdl
, EZFS_NOSPC
, fmt
, ap
);
694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
695 "pool I/O is currently suspended"));
696 zfs_verror(hdl
, EZFS_POOLUNAVAIL
, fmt
, ap
);
700 zfs_verror(hdl
, EZFS_POOLREADONLY
, fmt
, ap
);
703 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
704 "block size out of range or does not match"));
705 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
708 zfs_verror(hdl
, EZFS_ACTIVE_POOL
, fmt
, ap
);
710 case ZFS_ERR_CHECKPOINT_EXISTS
:
711 zfs_verror(hdl
, EZFS_CHECKPOINT_EXISTS
, fmt
, ap
);
713 case ZFS_ERR_DISCARDING_CHECKPOINT
:
714 zfs_verror(hdl
, EZFS_DISCARDING_CHECKPOINT
, fmt
, ap
);
716 case ZFS_ERR_NO_CHECKPOINT
:
717 zfs_verror(hdl
, EZFS_NO_CHECKPOINT
, fmt
, ap
);
719 case ZFS_ERR_DEVRM_IN_PROGRESS
:
720 zfs_verror(hdl
, EZFS_DEVRM_IN_PROGRESS
, fmt
, ap
);
722 case ZFS_ERR_VDEV_TOO_BIG
:
723 zfs_verror(hdl
, EZFS_VDEV_TOO_BIG
, fmt
, ap
);
725 case ZFS_ERR_EXPORT_IN_PROGRESS
:
726 zfs_verror(hdl
, EZFS_EXPORT_IN_PROGRESS
, fmt
, ap
);
728 case ZFS_ERR_RESILVER_IN_PROGRESS
:
729 zfs_verror(hdl
, EZFS_RESILVERING
, fmt
, ap
);
731 case ZFS_ERR_REBUILD_IN_PROGRESS
:
732 zfs_verror(hdl
, EZFS_REBUILDING
, fmt
, ap
);
734 case ZFS_ERR_BADPROP
:
735 zfs_verror(hdl
, EZFS_BADPROP
, fmt
, ap
);
737 case ZFS_ERR_VDEV_NOTSUP
:
738 zfs_verror(hdl
, EZFS_VDEV_NOTSUP
, fmt
, ap
);
740 case ZFS_ERR_IOC_CMD_UNAVAIL
:
741 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "the loaded zfs "
742 "module does not support this operation. A reboot may "
743 "be required to enable this operation."));
744 zfs_verror(hdl
, EZFS_IOC_NOTSUPPORTED
, fmt
, ap
);
746 case ZFS_ERR_IOC_ARG_UNAVAIL
:
747 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "the loaded zfs "
748 "module does not support an option for this operation. "
749 "A reboot may be required to enable this option."));
750 zfs_verror(hdl
, EZFS_IOC_NOTSUPPORTED
, fmt
, ap
);
752 case ZFS_ERR_IOC_ARG_REQUIRED
:
753 case ZFS_ERR_IOC_ARG_BADTYPE
:
754 zfs_verror(hdl
, EZFS_IOC_NOTSUPPORTED
, fmt
, ap
);
757 zfs_error_aux(hdl
, "%s", strerror(error
));
758 zfs_verror(hdl
, EZFS_UNKNOWN
, fmt
, ap
);
766 * Display an out of memory error message and abort the current program.
769 no_memory(libzfs_handle_t
*hdl
)
771 return (zfs_error(hdl
, EZFS_NOMEM
, "internal error"));
775 * A safe form of malloc() which will die if the allocation fails.
778 zfs_alloc(libzfs_handle_t
*hdl
, size_t size
)
782 if ((data
= calloc(1, size
)) == NULL
)
783 (void) no_memory(hdl
);
789 * A safe form of asprintf() which will die if the allocation fails.
792 zfs_asprintf(libzfs_handle_t
*hdl
, const char *fmt
, ...)
800 err
= vasprintf(&ret
, fmt
, ap
);
805 (void) no_memory(hdl
);
813 * A safe form of realloc(), which also zeroes newly allocated space.
816 zfs_realloc(libzfs_handle_t
*hdl
, void *ptr
, size_t oldsize
, size_t newsize
)
820 if ((ret
= realloc(ptr
, newsize
)) == NULL
) {
821 (void) no_memory(hdl
);
825 memset((char *)ret
+ oldsize
, 0, newsize
- oldsize
);
830 * A safe form of strdup() which will die if the allocation fails.
833 zfs_strdup(libzfs_handle_t
*hdl
, const char *str
)
837 if ((ret
= strdup(str
)) == NULL
)
838 (void) no_memory(hdl
);
844 libzfs_print_on_error(libzfs_handle_t
*hdl
, boolean_t printerr
)
846 hdl
->libzfs_printerr
= printerr
;
850 * Read lines from an open file descriptor and store them in an array of
851 * strings until EOF. lines[] will be allocated and populated with all the
852 * lines read. All newlines are replaced with NULL terminators for
853 * convenience. lines[] must be freed after use with libzfs_free_str_array().
855 * Returns the number of lines read.
858 libzfs_read_stdout_from_fd(int fd
, char **lines
[])
865 char **tmp_lines
= NULL
, **tmp
;
867 fp
= fdopen(fd
, "r");
872 while (getline(&line
, &len
, fp
) != -1) {
873 tmp
= realloc(tmp_lines
, sizeof (*tmp_lines
) * (lines_cnt
+ 1));
875 /* Return the lines we were able to process */
880 /* Remove newline if not EOF */
881 if (line
[strlen(line
) - 1] == '\n')
882 line
[strlen(line
) - 1] = '\0';
884 tmp_lines
[lines_cnt
] = strdup(line
);
885 if (tmp_lines
[lines_cnt
] == NULL
)
896 libzfs_run_process_impl(const char *path
, char *argv
[], char *env
[], int flags
,
897 char **lines
[], int *lines_cnt
)
900 int error
, devnull_fd
;
904 * Setup a pipe between our child and parent process if we're
907 if (lines
!= NULL
&& pipe2(link
, O_NONBLOCK
| O_CLOEXEC
) == -1)
913 devnull_fd
= open("/dev/null", O_WRONLY
| O_CLOEXEC
);
918 if (!(flags
& STDOUT_VERBOSE
) && (lines
== NULL
))
919 (void) dup2(devnull_fd
, STDOUT_FILENO
);
920 else if (lines
!= NULL
) {
921 /* Save the output to lines[] */
922 dup2(link
[1], STDOUT_FILENO
);
925 if (!(flags
& STDERR_VERBOSE
))
926 (void) dup2(devnull_fd
, STDERR_FILENO
);
928 if (flags
& NO_DEFAULT_PATH
) {
932 execve(path
, argv
, env
);
937 execvpe(path
, argv
, env
);
941 } else if (pid
> 0) {
945 while ((error
= waitpid(pid
, &status
, 0)) == -1 &&
948 if (error
< 0 || !WIFEXITED(status
))
953 *lines_cnt
= libzfs_read_stdout_from_fd(link
[0], lines
);
955 return (WEXITSTATUS(status
));
962 libzfs_run_process(const char *path
, char *argv
[], int flags
)
964 return (libzfs_run_process_impl(path
, argv
, NULL
, flags
, NULL
, NULL
));
968 * Run a command and store its stdout lines in an array of strings (lines[]).
969 * lines[] is allocated and populated for you, and the number of lines is set in
970 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
971 * All newlines (\n) in lines[] are terminated for convenience.
974 libzfs_run_process_get_stdout(const char *path
, char *argv
[], char *env
[],
975 char **lines
[], int *lines_cnt
)
977 return (libzfs_run_process_impl(path
, argv
, env
, 0, lines
, lines_cnt
));
981 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
982 * means that *path needs to be the full path to the executable.
985 libzfs_run_process_get_stdout_nopath(const char *path
, char *argv
[],
986 char *env
[], char **lines
[], int *lines_cnt
)
988 return (libzfs_run_process_impl(path
, argv
, env
, NO_DEFAULT_PATH
,
993 * Free an array of strings. Free both the strings contained in the array and
997 libzfs_free_str_array(char **strs
, int count
)
1006 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
1007 * a non-zero number.
1009 * Returns 0 otherwise.
1012 libzfs_envvar_is_set(const char *envvar
)
1014 char *env
= getenv(envvar
);
1015 return (env
&& (strtoul(env
, NULL
, 0) > 0 ||
1016 (!strncasecmp(env
, "YES", 3) && strnlen(env
, 4) == 3) ||
1017 (!strncasecmp(env
, "ON", 2) && strnlen(env
, 3) == 2)));
1023 libzfs_handle_t
*hdl
;
1027 if ((error
= libzfs_load_module()) != 0) {
1032 if ((hdl
= calloc(1, sizeof (libzfs_handle_t
))) == NULL
) {
1036 if (regcomp(&hdl
->libzfs_urire
, URI_REGEX
, 0) != 0) {
1041 if ((hdl
->libzfs_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
|O_CLOEXEC
)) < 0) {
1046 if (libzfs_core_init() != 0) {
1047 (void) close(hdl
->libzfs_fd
);
1054 zpool_feature_init();
1056 libzfs_mnttab_init(hdl
);
1059 if (getenv("ZFS_PROP_DEBUG") != NULL
) {
1060 hdl
->libzfs_prop_debug
= B_TRUE
;
1062 if ((env
= getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL
) {
1063 if ((error
= zfs_nicestrtonum(hdl
, env
,
1064 &hdl
->libzfs_max_nvlist
))) {
1066 (void) close(hdl
->libzfs_fd
);
1071 hdl
->libzfs_max_nvlist
= (SPA_MAXBLOCKSIZE
* 4);
1075 * For testing, remove some settable properties and features
1077 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1078 zprop_desc_t
*proptbl
;
1080 proptbl
= zpool_prop_get_table();
1081 proptbl
[ZPOOL_PROP_COMMENT
].pd_zfs_mod_supported
= B_FALSE
;
1083 proptbl
= zfs_prop_get_table();
1084 proptbl
[ZFS_PROP_DNODESIZE
].pd_zfs_mod_supported
= B_FALSE
;
1086 zfeature_info_t
*ftbl
= spa_feature_table
;
1087 ftbl
[SPA_FEATURE_LARGE_BLOCKS
].fi_zfs_mod_supported
= B_FALSE
;
1094 libzfs_fini(libzfs_handle_t
*hdl
)
1096 (void) close(hdl
->libzfs_fd
);
1097 zpool_free_handles(hdl
);
1098 namespace_clear(hdl
);
1099 libzfs_mnttab_fini(hdl
);
1101 regfree(&hdl
->libzfs_urire
);
1103 #if LIBFETCH_DYNAMIC
1104 if (hdl
->libfetch
!= (void *)-1 && hdl
->libfetch
!= NULL
)
1105 (void) dlclose(hdl
->libfetch
);
1106 free(hdl
->libfetch_load_error
);
1112 zpool_get_handle(zpool_handle_t
*zhp
)
1114 return (zhp
->zpool_hdl
);
1118 zfs_get_handle(zfs_handle_t
*zhp
)
1120 return (zhp
->zfs_hdl
);
1124 zfs_get_pool_handle(const zfs_handle_t
*zhp
)
1126 return (zhp
->zpool_hdl
);
1130 * Given a name, determine whether or not it's a valid path
1131 * (starts with '/' or "./"). If so, walk the mnttab trying
1132 * to match the device number. If not, treat the path as an
1133 * fs/vol/snap/bkmark name.
1136 zfs_path_to_zhandle(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t argtype
)
1138 struct stat64 statbuf
;
1139 struct extmnttab entry
;
1141 if (path
[0] != '/' && strncmp(path
, "./", strlen("./")) != 0) {
1143 * It's not a valid path, assume it's a name of type 'argtype'.
1145 return (zfs_open(hdl
, path
, argtype
));
1148 if (getextmntent(path
, &entry
, &statbuf
) != 0)
1151 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
1152 (void) fprintf(stderr
, gettext("'%s': not a ZFS filesystem\n"),
1157 return (zfs_open(hdl
, entry
.mnt_special
, ZFS_TYPE_FILESYSTEM
));
1161 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1165 zcmd_alloc_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, size_t len
)
1169 zc
->zc_nvlist_dst_size
= len
;
1171 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1175 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1176 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1177 * filled in by the kernel to indicate the actual required size.
1180 zcmd_expand_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
1182 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1184 (uint64_t)(uintptr_t)zfs_alloc(hdl
, zc
->zc_nvlist_dst_size
);
1188 * Called to free the src and dst nvlists stored in the command structure.
1191 zcmd_free_nvlists(zfs_cmd_t
*zc
)
1193 free((void *)(uintptr_t)zc
->zc_nvlist_conf
);
1194 free((void *)(uintptr_t)zc
->zc_nvlist_src
);
1195 free((void *)(uintptr_t)zc
->zc_nvlist_dst
);
1196 zc
->zc_nvlist_conf
= 0;
1197 zc
->zc_nvlist_src
= 0;
1198 zc
->zc_nvlist_dst
= 0;
1202 zcmd_write_nvlist_com(libzfs_handle_t
*hdl
, uint64_t *outnv
, uint64_t *outlen
,
1207 size_t len
= fnvlist_size(nvl
);
1208 packed
= zfs_alloc(hdl
, len
);
1210 verify(nvlist_pack(nvl
, &packed
, &len
, NV_ENCODE_NATIVE
, 0) == 0);
1212 *outnv
= (uint64_t)(uintptr_t)packed
;
1217 zcmd_write_conf_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1219 zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_conf
,
1220 &zc
->zc_nvlist_conf_size
, nvl
);
1224 zcmd_write_src_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1226 zcmd_write_nvlist_com(hdl
, &zc
->zc_nvlist_src
,
1227 &zc
->zc_nvlist_src_size
, nvl
);
1231 * Unpacks an nvlist from the ZFS ioctl command structure.
1234 zcmd_read_dst_nvlist(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
, nvlist_t
**nvlp
)
1236 if (nvlist_unpack((void *)(uintptr_t)zc
->zc_nvlist_dst
,
1237 zc
->zc_nvlist_dst_size
, nvlp
, 0) != 0)
1238 return (no_memory(hdl
));
1244 * ================================================================
1245 * API shared by zfs and zpool property management
1246 * ================================================================
1250 zprop_print_headers(zprop_get_cbdata_t
*cbp
, zfs_type_t type
)
1257 cbp
->cb_first
= B_FALSE
;
1258 if (cbp
->cb_scripted
)
1262 * Start with the length of the column headers.
1264 cbp
->cb_colwidths
[GET_COL_NAME
] = strlen(dgettext(TEXT_DOMAIN
, "NAME"));
1265 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = strlen(dgettext(TEXT_DOMAIN
,
1267 cbp
->cb_colwidths
[GET_COL_VALUE
] = strlen(dgettext(TEXT_DOMAIN
,
1269 cbp
->cb_colwidths
[GET_COL_RECVD
] = strlen(dgettext(TEXT_DOMAIN
,
1271 cbp
->cb_colwidths
[GET_COL_SOURCE
] = strlen(dgettext(TEXT_DOMAIN
,
1274 /* first property is always NAME */
1275 assert(cbp
->cb_proplist
->pl_prop
==
1276 ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1277 ((type
== ZFS_TYPE_VDEV
) ? VDEV_PROP_NAME
: ZFS_PROP_NAME
)));
1280 * Go through and calculate the widths for each column. For the
1281 * 'source' column, we kludge it up by taking the worst-case scenario of
1282 * inheriting from the longest name. This is acceptable because in the
1283 * majority of cases 'SOURCE' is the last column displayed, and we don't
1284 * use the width anyway. Note that the 'VALUE' column can be oversized,
1285 * if the name of the property is much longer than any values we find.
1287 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
1291 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
1292 const char *propname
= (type
== ZFS_TYPE_POOL
) ?
1293 zpool_prop_to_name(pl
->pl_prop
) :
1294 ((type
== ZFS_TYPE_VDEV
) ?
1295 vdev_prop_to_name(pl
->pl_prop
) :
1296 zfs_prop_to_name(pl
->pl_prop
));
1298 assert(propname
!= NULL
);
1299 len
= strlen(propname
);
1300 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1301 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1303 assert(pl
->pl_user_prop
!= NULL
);
1304 len
= strlen(pl
->pl_user_prop
);
1305 if (len
> cbp
->cb_colwidths
[GET_COL_PROPERTY
])
1306 cbp
->cb_colwidths
[GET_COL_PROPERTY
] = len
;
1310 * 'VALUE' column. The first property is always the 'name'
1311 * property that was tacked on either by /sbin/zfs's
1312 * zfs_do_get() or when calling zprop_expand_list(), so we
1313 * ignore its width. If the user specified the name property
1314 * to display, then it will be later in the list in any case.
1316 if (pl
!= cbp
->cb_proplist
&&
1317 pl
->pl_width
> cbp
->cb_colwidths
[GET_COL_VALUE
])
1318 cbp
->cb_colwidths
[GET_COL_VALUE
] = pl
->pl_width
;
1320 /* 'RECEIVED' column. */
1321 if (pl
!= cbp
->cb_proplist
&&
1322 pl
->pl_recvd_width
> cbp
->cb_colwidths
[GET_COL_RECVD
])
1323 cbp
->cb_colwidths
[GET_COL_RECVD
] = pl
->pl_recvd_width
;
1326 * 'NAME' and 'SOURCE' columns
1328 if (pl
->pl_prop
== ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1329 ((type
== ZFS_TYPE_VDEV
) ? VDEV_PROP_NAME
:
1330 ZFS_PROP_NAME
)) && pl
->pl_width
>
1331 cbp
->cb_colwidths
[GET_COL_NAME
]) {
1332 cbp
->cb_colwidths
[GET_COL_NAME
] = pl
->pl_width
;
1333 cbp
->cb_colwidths
[GET_COL_SOURCE
] = pl
->pl_width
+
1334 strlen(dgettext(TEXT_DOMAIN
, "inherited from"));
1339 * Now go through and print the headers.
1341 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1342 switch (cbp
->cb_columns
[i
]) {
1344 title
= dgettext(TEXT_DOMAIN
, "NAME");
1346 case GET_COL_PROPERTY
:
1347 title
= dgettext(TEXT_DOMAIN
, "PROPERTY");
1350 title
= dgettext(TEXT_DOMAIN
, "VALUE");
1353 title
= dgettext(TEXT_DOMAIN
, "RECEIVED");
1355 case GET_COL_SOURCE
:
1356 title
= dgettext(TEXT_DOMAIN
, "SOURCE");
1362 if (title
!= NULL
) {
1363 if (i
== (ZFS_GET_NCOLS
- 1) ||
1364 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1365 (void) printf("%s", title
);
1367 (void) printf("%-*s ",
1368 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1372 (void) printf("\n");
1376 * Display a single line of output, according to the settings in the callback
1380 zprop_print_one_property(const char *name
, zprop_get_cbdata_t
*cbp
,
1381 const char *propname
, const char *value
, zprop_source_t sourcetype
,
1382 const char *source
, const char *recvd_value
)
1385 const char *str
= NULL
;
1389 * Ignore those source types that the user has chosen to ignore.
1391 if ((sourcetype
& cbp
->cb_sources
) == 0)
1395 zprop_print_headers(cbp
, cbp
->cb_type
);
1397 for (i
= 0; i
< ZFS_GET_NCOLS
; i
++) {
1398 switch (cbp
->cb_columns
[i
]) {
1403 case GET_COL_PROPERTY
:
1411 case GET_COL_SOURCE
:
1412 switch (sourcetype
) {
1413 case ZPROP_SRC_NONE
:
1417 case ZPROP_SRC_DEFAULT
:
1421 case ZPROP_SRC_LOCAL
:
1425 case ZPROP_SRC_TEMPORARY
:
1429 case ZPROP_SRC_INHERITED
:
1430 (void) snprintf(buf
, sizeof (buf
),
1431 "inherited from %s", source
);
1434 case ZPROP_SRC_RECEIVED
:
1440 assert(!"unhandled zprop_source_t");
1445 str
= (recvd_value
== NULL
? "-" : recvd_value
);
1452 if (i
== (ZFS_GET_NCOLS
- 1) ||
1453 cbp
->cb_columns
[i
+ 1] == GET_COL_NONE
)
1454 (void) printf("%s", str
);
1455 else if (cbp
->cb_scripted
)
1456 (void) printf("%s\t", str
);
1458 (void) printf("%-*s ",
1459 cbp
->cb_colwidths
[cbp
->cb_columns
[i
]],
1463 (void) printf("\n");
1467 * Given a numeric suffix, convert the value into a number of bits that the
1468 * resulting value must be shifted.
1471 str2shift(libzfs_handle_t
*hdl
, const char *buf
)
1473 const char *ends
= "BKMGTPEZ";
1478 for (i
= 0; i
< strlen(ends
); i
++) {
1479 if (toupper(buf
[0]) == ends
[i
])
1482 if (i
== strlen(ends
)) {
1484 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1485 "invalid numeric suffix '%s'"), buf
);
1490 * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1491 * However, 'BB' and 'BiB' are disallowed.
1493 if (buf
[1] == '\0' ||
1494 (toupper(buf
[0]) != 'B' &&
1495 ((toupper(buf
[1]) == 'B' && buf
[2] == '\0') ||
1496 (toupper(buf
[1]) == 'I' && toupper(buf
[2]) == 'B' &&
1501 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1502 "invalid numeric suffix '%s'"), buf
);
1507 * Convert a string of the form '100G' into a real number. Used when setting
1508 * properties or creating a volume. 'buf' is used to place an extended error
1509 * message for the caller to use.
1512 zfs_nicestrtonum(libzfs_handle_t
*hdl
, const char *value
, uint64_t *num
)
1519 /* Check to see if this looks like a number. */
1520 if ((value
[0] < '0' || value
[0] > '9') && value
[0] != '.') {
1522 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1523 "bad numeric value '%s'"), value
);
1527 /* Rely on strtoull() to process the numeric portion. */
1529 *num
= strtoull(value
, &end
, 10);
1532 * Check for ERANGE, which indicates that the value is too large to fit
1533 * in a 64-bit value.
1535 if (errno
== ERANGE
) {
1537 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1538 "numeric value is too large"));
1543 * If we have a decimal value, then do the computation with floating
1544 * point arithmetic. Otherwise, use standard arithmetic.
1547 double fval
= strtod(value
, &end
);
1549 if ((shift
= str2shift(hdl
, end
)) == -1)
1552 fval
*= pow(2, shift
);
1555 * UINT64_MAX is not exactly representable as a double.
1556 * The closest representation is UINT64_MAX + 1, so we
1557 * use a >= comparison instead of > for the bounds check.
1559 if (fval
>= (double)UINT64_MAX
) {
1561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1562 "numeric value is too large"));
1566 *num
= (uint64_t)fval
;
1568 if ((shift
= str2shift(hdl
, end
)) == -1)
1571 /* Check for overflow */
1572 if (shift
>= 64 || (*num
<< shift
) >> shift
!= *num
) {
1574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1575 "numeric value is too large"));
1586 * Given a propname=value nvpair to set, parse any numeric properties
1587 * (index, boolean, etc) if they are specified as strings and add the
1588 * resulting nvpair to the returned nvlist.
1590 * At the DSL layer, all properties are either 64-bit numbers or strings.
1591 * We want the user to be able to ignore this fact and specify properties
1592 * as native values (numbers, for example) or as strings (to simplify
1593 * command line utilities). This also handles converting index types
1594 * (compression, checksum, etc) from strings to their on-disk index.
1597 zprop_parse_value(libzfs_handle_t
*hdl
, nvpair_t
*elem
, int prop
,
1598 zfs_type_t type
, nvlist_t
*ret
, char **svalp
, uint64_t *ivalp
,
1601 data_type_t datatype
= nvpair_type(elem
);
1602 zprop_type_t proptype
;
1603 const char *propname
;
1605 boolean_t isnone
= B_FALSE
;
1606 boolean_t isauto
= B_FALSE
;
1609 if (type
== ZFS_TYPE_POOL
) {
1610 proptype
= zpool_prop_get_type(prop
);
1611 propname
= zpool_prop_to_name(prop
);
1612 } else if (type
== ZFS_TYPE_VDEV
) {
1613 proptype
= vdev_prop_get_type(prop
);
1614 propname
= vdev_prop_to_name(prop
);
1616 proptype
= zfs_prop_get_type(prop
);
1617 propname
= zfs_prop_to_name(prop
);
1621 * Convert any properties to the internal DSL value types.
1627 case PROP_TYPE_STRING
:
1628 if (datatype
!= DATA_TYPE_STRING
) {
1629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1630 "'%s' must be a string"), nvpair_name(elem
));
1633 err
= nvpair_value_string(elem
, svalp
);
1635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1636 "'%s' is invalid"), nvpair_name(elem
));
1639 if (strlen(*svalp
) >= ZFS_MAXPROPLEN
) {
1640 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1641 "'%s' is too long"), nvpair_name(elem
));
1646 case PROP_TYPE_NUMBER
:
1647 if (datatype
== DATA_TYPE_STRING
) {
1648 (void) nvpair_value_string(elem
, &value
);
1649 if (strcmp(value
, "none") == 0) {
1651 } else if (strcmp(value
, "auto") == 0) {
1653 } else if (zfs_nicestrtonum(hdl
, value
, ivalp
) != 0) {
1656 } else if (datatype
== DATA_TYPE_UINT64
) {
1657 (void) nvpair_value_uint64(elem
, ivalp
);
1659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1660 "'%s' must be a number"), nvpair_name(elem
));
1665 * Quota special: force 'none' and don't allow 0.
1667 if ((type
& ZFS_TYPE_DATASET
) && *ivalp
== 0 && !isnone
&&
1668 (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_REFQUOTA
)) {
1669 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1670 "use 'none' to disable quota/refquota"));
1675 * Special handling for "*_limit=none". In this case it's not
1678 if ((type
& ZFS_TYPE_DATASET
) && isnone
&&
1679 (prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
1680 prop
== ZFS_PROP_SNAPSHOT_LIMIT
)) {
1681 *ivalp
= UINT64_MAX
;
1685 * Special handling for "checksum_*=none". In this case it's not
1688 if ((type
& ZFS_TYPE_VDEV
) && isnone
&&
1689 (prop
== VDEV_PROP_CHECKSUM_N
||
1690 prop
== VDEV_PROP_CHECKSUM_T
||
1691 prop
== VDEV_PROP_IO_N
||
1692 prop
== VDEV_PROP_IO_T
)) {
1693 *ivalp
= UINT64_MAX
;
1697 * Special handling for setting 'refreservation' to 'auto'. Use
1698 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1699 * 'auto' is only allowed on volumes.
1703 case ZFS_PROP_REFRESERVATION
:
1704 if ((type
& ZFS_TYPE_VOLUME
) == 0) {
1705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1706 "'%s=auto' only allowed on "
1707 "volumes"), nvpair_name(elem
));
1710 *ivalp
= UINT64_MAX
;
1713 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1714 "'auto' is invalid value for '%s'"),
1722 case PROP_TYPE_INDEX
:
1723 if (datatype
!= DATA_TYPE_STRING
) {
1724 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1725 "'%s' must be a string"), nvpair_name(elem
));
1729 (void) nvpair_value_string(elem
, &value
);
1731 if (zprop_string_to_index(prop
, value
, ivalp
, type
) != 0) {
1732 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1733 "'%s' must be one of '%s'"), propname
,
1734 zprop_values(prop
, type
));
1744 * Add the result to our return set of properties.
1746 if (*svalp
!= NULL
) {
1747 if (nvlist_add_string(ret
, propname
, *svalp
) != 0) {
1748 (void) no_memory(hdl
);
1752 if (nvlist_add_uint64(ret
, propname
, *ivalp
) != 0) {
1753 (void) no_memory(hdl
);
1760 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1765 addlist(libzfs_handle_t
*hdl
, const char *propname
, zprop_list_t
**listp
,
1768 int prop
= zprop_name_to_prop(propname
, type
);
1769 if (prop
!= ZPROP_INVAL
&& !zprop_valid_for_type(prop
, type
, B_FALSE
))
1773 * Return failure if no property table entry was found and this isn't
1774 * a user-defined property.
1776 if (prop
== ZPROP_USERPROP
&& ((type
== ZFS_TYPE_POOL
&&
1777 !zpool_prop_feature(propname
) &&
1778 !zpool_prop_unsupported(propname
)) ||
1779 ((type
== ZFS_TYPE_DATASET
) && !zfs_prop_user(propname
) &&
1780 !zfs_prop_userquota(propname
) && !zfs_prop_written(propname
)) ||
1781 ((type
== ZFS_TYPE_VDEV
) && !vdev_prop_user(propname
)))) {
1782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1783 "invalid property '%s'"), propname
);
1784 return (zfs_error(hdl
, EZFS_BADPROP
,
1785 dgettext(TEXT_DOMAIN
, "bad property list")));
1788 zprop_list_t
*entry
= zfs_alloc(hdl
, sizeof (*entry
));
1790 entry
->pl_prop
= prop
;
1791 if (prop
== ZPROP_USERPROP
) {
1792 entry
->pl_user_prop
= zfs_strdup(hdl
, propname
);
1793 entry
->pl_width
= strlen(propname
);
1795 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
,
1805 * Given a comma-separated list of properties, construct a property list
1806 * containing both user-defined and native properties. This function will
1807 * return a NULL list if 'all' is specified, which can later be expanded
1808 * by zprop_expand_list().
1811 zprop_get_list(libzfs_handle_t
*hdl
, char *props
, zprop_list_t
**listp
,
1817 * If 'all' is specified, return a NULL list.
1819 if (strcmp(props
, "all") == 0)
1823 * If no props were specified, return an error.
1825 if (props
[0] == '\0') {
1826 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1827 "no properties specified"));
1828 return (zfs_error(hdl
, EZFS_BADPROP
, dgettext(TEXT_DOMAIN
,
1829 "bad property list")));
1832 for (char *p
; (p
= strsep(&props
, ",")); )
1833 if (strcmp(p
, "space") == 0) {
1834 static const char *const spaceprops
[] = {
1835 "name", "avail", "used", "usedbysnapshots",
1836 "usedbydataset", "usedbyrefreservation",
1840 for (int i
= 0; i
< ARRAY_SIZE(spaceprops
); i
++) {
1841 if (addlist(hdl
, spaceprops
[i
], listp
, type
))
1843 listp
= &(*listp
)->pl_next
;
1846 if (addlist(hdl
, p
, listp
, type
))
1848 listp
= &(*listp
)->pl_next
;
1855 zprop_free_list(zprop_list_t
*pl
)
1859 while (pl
!= NULL
) {
1861 free(pl
->pl_user_prop
);
1867 typedef struct expand_data
{
1868 zprop_list_t
**last
;
1869 libzfs_handle_t
*hdl
;
1874 zprop_expand_list_cb(int prop
, void *cb
)
1876 zprop_list_t
*entry
;
1877 expand_data_t
*edp
= cb
;
1879 entry
= zfs_alloc(edp
->hdl
, sizeof (zprop_list_t
));
1881 entry
->pl_prop
= prop
;
1882 entry
->pl_width
= zprop_width(prop
, &entry
->pl_fixed
, edp
->type
);
1883 entry
->pl_all
= B_TRUE
;
1885 *(edp
->last
) = entry
;
1886 edp
->last
= &entry
->pl_next
;
1888 return (ZPROP_CONT
);
1892 zprop_expand_list(libzfs_handle_t
*hdl
, zprop_list_t
**plp
, zfs_type_t type
)
1894 zprop_list_t
*entry
;
1895 zprop_list_t
**last
;
1900 * If this is the very first time we've been called for an 'all'
1901 * specification, expand the list to include all native
1910 if (zprop_iter_common(zprop_expand_list_cb
, &exp
, B_FALSE
,
1911 B_FALSE
, type
) == ZPROP_INVAL
)
1915 * Add 'name' to the beginning of the list, which is handled
1918 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
1919 entry
->pl_prop
= ((type
== ZFS_TYPE_POOL
) ? ZPOOL_PROP_NAME
:
1920 ((type
== ZFS_TYPE_VDEV
) ? VDEV_PROP_NAME
: ZFS_PROP_NAME
));
1921 entry
->pl_width
= zprop_width(entry
->pl_prop
,
1922 &entry
->pl_fixed
, type
);
1923 entry
->pl_all
= B_TRUE
;
1924 entry
->pl_next
= *plp
;
1931 zprop_iter(zprop_func func
, void *cb
, boolean_t show_all
, boolean_t ordered
,
1934 return (zprop_iter_common(func
, cb
, show_all
, ordered
, type
));
1938 zfs_version_userland(void)
1940 return (ZFS_META_ALIAS
);
1944 * Prints both zfs userland and kernel versions
1945 * Returns 0 on success, and -1 on error
1948 zfs_version_print(void)
1950 (void) puts(ZFS_META_ALIAS
);
1952 char *kver
= zfs_version_kernel();
1954 fprintf(stderr
, "zfs_version_kernel() failed: %s\n",
1959 (void) printf("zfs-kmod-%s\n", kver
);
1965 * Return 1 if the user requested ANSI color output, and our terminal supports
1966 * it. Return 0 for no color.
1971 static int use_color
= -1;
1977 * For each zpool invocation, we do a single check to see if we should
1978 * be using color or not, and cache that value for the lifetime of the
1979 * the zpool command. That makes it cheap to call use_color() when
1980 * we're printing with color. We assume that the settings are not going
1981 * to change during the invocation of a zpool command (the user isn't
1982 * going to change the ZFS_COLOR value while zpool is running, for
1985 if (use_color
!= -1) {
1987 * We've already figured out if we should be using color or
1988 * not. Return the cached value.
1993 term
= getenv("TERM");
1995 * The user sets the ZFS_COLOR env var set to enable zpool ANSI color
1996 * output. However if NO_COLOR is set (https://no-color.org/) then
1997 * don't use it. Also, don't use color if terminal doesn't support
2000 if (libzfs_envvar_is_set("ZFS_COLOR") &&
2001 !libzfs_envvar_is_set("NO_COLOR") &&
2002 isatty(STDOUT_FILENO
) && term
&& strcmp("dumb", term
) != 0 &&
2003 strcmp("unknown", term
) != 0) {
2004 /* Color supported */
2014 * color_start() and color_end() are used for when you want to colorize a block
2015 * of text. For example:
2017 * color_start(ANSI_RED_FG)
2023 color_start(const char *color
)
2026 fputs(color
, stdout
);
2035 fputs(ANSI_RESET
, stdout
);
2041 /* printf() with a color. If color is NULL, then do a normal printf. */
2043 printf_color(const char *color
, const char *format
, ...)
2051 va_start(aptr
, format
);
2052 rc
= vprintf(format
, aptr
);