1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy * Copyright 2020 Joyent, Inc. All rights reserved.
25783d3ff6SMartin Matuska * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
26eda14cbcSMatt Macy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27eda14cbcSMatt Macy * Copyright (c) 2017 Datto Inc.
28eda14cbcSMatt Macy * Copyright (c) 2020 The FreeBSD Foundation
29eda14cbcSMatt Macy *
30eda14cbcSMatt Macy * Portions of this software were developed by Allan Jude
31eda14cbcSMatt Macy * under sponsorship from the FreeBSD Foundation.
32eda14cbcSMatt Macy */
33eda14cbcSMatt Macy
34eda14cbcSMatt Macy /*
35eda14cbcSMatt Macy * Internal utility routines for the ZFS library.
36eda14cbcSMatt Macy */
37eda14cbcSMatt Macy
38eda14cbcSMatt Macy #include <errno.h>
39eda14cbcSMatt Macy #include <fcntl.h>
40eda14cbcSMatt Macy #include <libintl.h>
41eda14cbcSMatt Macy #include <stdarg.h>
42eda14cbcSMatt Macy #include <stdio.h>
43eda14cbcSMatt Macy #include <stdlib.h>
44eda14cbcSMatt Macy #include <strings.h>
45eda14cbcSMatt Macy #include <unistd.h>
46eda14cbcSMatt Macy #include <math.h>
4716038816SMartin Matuska #if LIBFETCH_DYNAMIC
4816038816SMartin Matuska #include <dlfcn.h>
4916038816SMartin Matuska #endif
50eda14cbcSMatt Macy #include <sys/stat.h>
51eda14cbcSMatt Macy #include <sys/mnttab.h>
52eda14cbcSMatt Macy #include <sys/mntent.h>
53eda14cbcSMatt Macy #include <sys/types.h>
54eda14cbcSMatt Macy #include <sys/wait.h>
55eda14cbcSMatt Macy
56eda14cbcSMatt Macy #include <libzfs.h>
57eda14cbcSMatt Macy #include <libzfs_core.h>
58eda14cbcSMatt Macy
59eda14cbcSMatt Macy #include "libzfs_impl.h"
60eda14cbcSMatt Macy #include "zfs_prop.h"
61eda14cbcSMatt Macy #include "zfeature_common.h"
62eda14cbcSMatt Macy #include <zfs_fletcher.h>
63eda14cbcSMatt Macy #include <libzutil.h>
64eda14cbcSMatt Macy
65eda14cbcSMatt Macy /*
66eda14cbcSMatt Macy * We only care about the scheme in order to match the scheme
67eda14cbcSMatt Macy * with the handler. Each handler should validate the full URI
68eda14cbcSMatt Macy * as necessary.
69eda14cbcSMatt Macy */
70eda14cbcSMatt Macy #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):"
71ce4dcb97SMartin Matuska #define STR_NUMS "0123456789"
72eda14cbcSMatt Macy
73eda14cbcSMatt Macy int
libzfs_errno(libzfs_handle_t * hdl)74eda14cbcSMatt Macy libzfs_errno(libzfs_handle_t *hdl)
75eda14cbcSMatt Macy {
76eda14cbcSMatt Macy return (hdl->libzfs_error);
77eda14cbcSMatt Macy }
78eda14cbcSMatt Macy
79eda14cbcSMatt Macy const char *
libzfs_error_action(libzfs_handle_t * hdl)80eda14cbcSMatt Macy libzfs_error_action(libzfs_handle_t *hdl)
81eda14cbcSMatt Macy {
82eda14cbcSMatt Macy return (hdl->libzfs_action);
83eda14cbcSMatt Macy }
84eda14cbcSMatt Macy
85eda14cbcSMatt Macy const char *
libzfs_error_description(libzfs_handle_t * hdl)86eda14cbcSMatt Macy libzfs_error_description(libzfs_handle_t *hdl)
87eda14cbcSMatt Macy {
88eda14cbcSMatt Macy if (hdl->libzfs_desc[0] != '\0')
89eda14cbcSMatt Macy return (hdl->libzfs_desc);
90eda14cbcSMatt Macy
91eda14cbcSMatt Macy switch (hdl->libzfs_error) {
92eda14cbcSMatt Macy case EZFS_NOMEM:
93eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "out of memory"));
94eda14cbcSMatt Macy case EZFS_BADPROP:
95eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid property value"));
96eda14cbcSMatt Macy case EZFS_PROPREADONLY:
97eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "read-only property"));
98eda14cbcSMatt Macy case EZFS_PROPTYPE:
99eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
100eda14cbcSMatt Macy "datasets of this type"));
101eda14cbcSMatt Macy case EZFS_PROPNONINHERIT:
102eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
103eda14cbcSMatt Macy case EZFS_PROPSPACE:
104eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
105eda14cbcSMatt Macy case EZFS_BADTYPE:
106eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not applicable to "
107eda14cbcSMatt Macy "datasets of this type"));
108eda14cbcSMatt Macy case EZFS_BUSY:
109eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
110eda14cbcSMatt Macy case EZFS_EXISTS:
111eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
112eda14cbcSMatt Macy case EZFS_NOENT:
113eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
114eda14cbcSMatt Macy case EZFS_BADSTREAM:
115eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
116eda14cbcSMatt Macy case EZFS_DSREADONLY:
117eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
118eda14cbcSMatt Macy case EZFS_VOLTOOBIG:
119eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
120eda14cbcSMatt Macy "this system"));
121eda14cbcSMatt Macy case EZFS_INVALIDNAME:
122eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid name"));
123eda14cbcSMatt Macy case EZFS_BADRESTORE:
124eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unable to restore to "
125eda14cbcSMatt Macy "destination"));
126eda14cbcSMatt Macy case EZFS_BADBACKUP:
127eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "backup failed"));
128eda14cbcSMatt Macy case EZFS_BADTARGET:
129eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
130eda14cbcSMatt Macy case EZFS_NODEVICE:
131eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no such device in pool"));
132eda14cbcSMatt Macy case EZFS_BADDEV:
133eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid device"));
134eda14cbcSMatt Macy case EZFS_NOREPLICAS:
135eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no valid replicas"));
136eda14cbcSMatt Macy case EZFS_RESILVERING:
137eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently resilvering"));
138eda14cbcSMatt Macy case EZFS_BADVERSION:
139eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unsupported version or "
140eda14cbcSMatt Macy "feature"));
141eda14cbcSMatt Macy case EZFS_POOLUNAVAIL:
142eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
143eda14cbcSMatt Macy case EZFS_DEVOVERFLOW:
144eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
145eda14cbcSMatt Macy case EZFS_BADPATH:
146eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
147eda14cbcSMatt Macy case EZFS_CROSSTARGET:
148eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
149eda14cbcSMatt Macy "pools"));
150eda14cbcSMatt Macy case EZFS_ZONED:
151eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
152eda14cbcSMatt Macy case EZFS_MOUNTFAILED:
153eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "mount failed"));
154eda14cbcSMatt Macy case EZFS_UMOUNTFAILED:
1557877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "unmount failed"));
156eda14cbcSMatt Macy case EZFS_UNSHARENFSFAILED:
1577877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "NFS share removal failed"));
158eda14cbcSMatt Macy case EZFS_SHARENFSFAILED:
1597877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "NFS share creation failed"));
160eda14cbcSMatt Macy case EZFS_UNSHARESMBFAILED:
1617877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "SMB share removal failed"));
162eda14cbcSMatt Macy case EZFS_SHARESMBFAILED:
1637877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "SMB share creation failed"));
164eda14cbcSMatt Macy case EZFS_PERM:
165eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "permission denied"));
166eda14cbcSMatt Macy case EZFS_NOSPC:
167eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "out of space"));
168eda14cbcSMatt Macy case EZFS_FAULT:
169eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "bad address"));
170eda14cbcSMatt Macy case EZFS_IO:
171eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "I/O error"));
172eda14cbcSMatt Macy case EZFS_INTR:
173eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "signal received"));
174c7046f76SMartin Matuska case EZFS_CKSUM:
175c7046f76SMartin Matuska return (dgettext(TEXT_DOMAIN, "insufficient replicas"));
176eda14cbcSMatt Macy case EZFS_ISSPARE:
177eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
178eda14cbcSMatt Macy "spare"));
179eda14cbcSMatt Macy case EZFS_INVALCONFIG:
180eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
181eda14cbcSMatt Macy case EZFS_RECURSIVE:
182eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
183eda14cbcSMatt Macy case EZFS_NOHISTORY:
184eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no history available"));
185eda14cbcSMatt Macy case EZFS_POOLPROPS:
186eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "failed to retrieve "
187eda14cbcSMatt Macy "pool properties"));
188eda14cbcSMatt Macy case EZFS_POOL_NOTSUP:
189eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not supported "
190eda14cbcSMatt Macy "on this type of pool"));
191eda14cbcSMatt Macy case EZFS_POOL_INVALARG:
192eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid argument for "
193eda14cbcSMatt Macy "this pool operation"));
194eda14cbcSMatt Macy case EZFS_NAMETOOLONG:
195eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
196eda14cbcSMatt Macy case EZFS_OPENFAILED:
197eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "open failed"));
198eda14cbcSMatt Macy case EZFS_NOCAP:
199eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN,
200eda14cbcSMatt Macy "disk capacity information could not be retrieved"));
201eda14cbcSMatt Macy case EZFS_LABELFAILED:
202eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "write of label failed"));
203eda14cbcSMatt Macy case EZFS_BADWHO:
204eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid user/group"));
205eda14cbcSMatt Macy case EZFS_BADPERM:
206eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid permission"));
207eda14cbcSMatt Macy case EZFS_BADPERMSET:
208eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
209eda14cbcSMatt Macy case EZFS_NODELEGATION:
210eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "delegated administration is "
211eda14cbcSMatt Macy "disabled on pool"));
212eda14cbcSMatt Macy case EZFS_BADCACHE:
213eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
214eda14cbcSMatt Macy case EZFS_ISL2CACHE:
215eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
216eda14cbcSMatt Macy case EZFS_VDEVNOTSUP:
217eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "vdev specification is not "
218eda14cbcSMatt Macy "supported"));
219eda14cbcSMatt Macy case EZFS_NOTSUP:
220eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not supported "
221eda14cbcSMatt Macy "on this dataset"));
222eda14cbcSMatt Macy case EZFS_IOC_NOTSUPPORTED:
223eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not supported by "
224eda14cbcSMatt Macy "zfs kernel module"));
225eda14cbcSMatt Macy case EZFS_ACTIVE_SPARE:
226eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
227eda14cbcSMatt Macy "device"));
228eda14cbcSMatt Macy case EZFS_UNPLAYED_LOGS:
229eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
230eda14cbcSMatt Macy "logs"));
231eda14cbcSMatt Macy case EZFS_REFTAG_RELE:
232eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
233eda14cbcSMatt Macy case EZFS_REFTAG_HOLD:
234eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "tag already exists on this "
235eda14cbcSMatt Macy "dataset"));
236eda14cbcSMatt Macy case EZFS_TAGTOOLONG:
237eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "tag too long"));
238eda14cbcSMatt Macy case EZFS_PIPEFAILED:
239eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pipe create failed"));
240eda14cbcSMatt Macy case EZFS_THREADCREATEFAILED:
241eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "thread create failed"));
242eda14cbcSMatt Macy case EZFS_POSTSPLIT_ONLINE:
243eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
244eda14cbcSMatt Macy "into a new one"));
245eda14cbcSMatt Macy case EZFS_SCRUB_PAUSED:
246eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "scrub is paused; "
247c0a83fe0SMartin Matuska "use 'zpool scrub' to resume scrub"));
248c0a83fe0SMartin Matuska case EZFS_SCRUB_PAUSED_TO_CANCEL:
249c0a83fe0SMartin Matuska return (dgettext(TEXT_DOMAIN, "scrub is paused; "
250c0a83fe0SMartin Matuska "use 'zpool scrub' to resume or 'zpool scrub -s' to "
251c0a83fe0SMartin Matuska "cancel scrub"));
252eda14cbcSMatt Macy case EZFS_SCRUBBING:
253eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
254c0a83fe0SMartin Matuska "use 'zpool scrub -s' to cancel scrub"));
255c0a83fe0SMartin Matuska case EZFS_ERRORSCRUBBING:
256c0a83fe0SMartin Matuska return (dgettext(TEXT_DOMAIN, "currently error scrubbing; "
257c0a83fe0SMartin Matuska "use 'zpool scrub -s' to cancel error scrub"));
258c0a83fe0SMartin Matuska case EZFS_ERRORSCRUB_PAUSED:
259c0a83fe0SMartin Matuska return (dgettext(TEXT_DOMAIN, "error scrub is paused; "
260c0a83fe0SMartin Matuska "use 'zpool scrub -e' to resume error scrub"));
261eda14cbcSMatt Macy case EZFS_NO_SCRUB:
262eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
263eda14cbcSMatt Macy case EZFS_DIFF:
264eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
265eda14cbcSMatt Macy case EZFS_DIFFDATA:
266eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid diff data"));
267eda14cbcSMatt Macy case EZFS_POOLREADONLY:
268eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool is read-only"));
269eda14cbcSMatt Macy case EZFS_NO_PENDING:
270eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation is not "
271eda14cbcSMatt Macy "in progress"));
272eda14cbcSMatt Macy case EZFS_CHECKPOINT_EXISTS:
273eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
274eda14cbcSMatt Macy case EZFS_DISCARDING_CHECKPOINT:
275eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently discarding "
276eda14cbcSMatt Macy "checkpoint"));
277eda14cbcSMatt Macy case EZFS_NO_CHECKPOINT:
278eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
279eda14cbcSMatt Macy case EZFS_DEVRM_IN_PROGRESS:
280eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device removal in progress"));
281eda14cbcSMatt Macy case EZFS_VDEV_TOO_BIG:
282eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
283eda14cbcSMatt Macy case EZFS_ACTIVE_POOL:
284eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool is imported on a "
285eda14cbcSMatt Macy "different host"));
286eda14cbcSMatt Macy case EZFS_CRYPTOFAILED:
287eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "encryption failure"));
288eda14cbcSMatt Macy case EZFS_TOOMANY:
289eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "argument list too long"));
290eda14cbcSMatt Macy case EZFS_INITIALIZING:
291eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently initializing"));
292eda14cbcSMatt Macy case EZFS_NO_INITIALIZE:
293eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "there is no active "
294eda14cbcSMatt Macy "initialization"));
295eda14cbcSMatt Macy case EZFS_WRONG_PARENT:
296eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
297eda14cbcSMatt Macy case EZFS_TRIMMING:
298eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently trimming"));
299eda14cbcSMatt Macy case EZFS_NO_TRIM:
300eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "there is no active trim"));
301eda14cbcSMatt Macy case EZFS_TRIM_NOTSUP:
302eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "trim operations are not "
303eda14cbcSMatt Macy "supported by this device"));
304eda14cbcSMatt Macy case EZFS_NO_RESILVER_DEFER:
305eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "this action requires the "
306eda14cbcSMatt Macy "resilver_defer feature"));
307eda14cbcSMatt Macy case EZFS_EXPORT_IN_PROGRESS:
308eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool export in progress"));
309eda14cbcSMatt Macy case EZFS_REBUILDING:
310eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently sequentially "
311eda14cbcSMatt Macy "resilvering"));
312681ce946SMartin Matuska case EZFS_VDEV_NOTSUP:
313681ce946SMartin Matuska return (dgettext(TEXT_DOMAIN, "operation not supported "
314681ce946SMartin Matuska "on this type of vdev"));
3151f1e2261SMartin Matuska case EZFS_NOT_USER_NAMESPACE:
3161f1e2261SMartin Matuska return (dgettext(TEXT_DOMAIN, "the provided file "
3171f1e2261SMartin Matuska "was not a user namespace file"));
318be181ee2SMartin Matuska case EZFS_RESUME_EXISTS:
319be181ee2SMartin Matuska return (dgettext(TEXT_DOMAIN, "Resuming recv on existing "
320be181ee2SMartin Matuska "dataset without force"));
321e716630dSMartin Matuska case EZFS_RAIDZ_EXPAND_IN_PROGRESS:
322e716630dSMartin Matuska return (dgettext(TEXT_DOMAIN, "raidz expansion in progress"));
323783d3ff6SMartin Matuska case EZFS_ASHIFT_MISMATCH:
324783d3ff6SMartin Matuska return (dgettext(TEXT_DOMAIN, "adding devices with "
325783d3ff6SMartin Matuska "different physical sector sizes is not allowed"));
326eda14cbcSMatt Macy case EZFS_UNKNOWN:
327eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unknown error"));
328eda14cbcSMatt Macy default:
329eda14cbcSMatt Macy assert(hdl->libzfs_error == 0);
330eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no error"));
331eda14cbcSMatt Macy }
332eda14cbcSMatt Macy }
333eda14cbcSMatt Macy
334eda14cbcSMatt Macy void
zfs_error_aux(libzfs_handle_t * hdl,const char * fmt,...)335eda14cbcSMatt Macy zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
336eda14cbcSMatt Macy {
337eda14cbcSMatt Macy va_list ap;
338eda14cbcSMatt Macy
339eda14cbcSMatt Macy va_start(ap, fmt);
340eda14cbcSMatt Macy
341eda14cbcSMatt Macy (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
342eda14cbcSMatt Macy fmt, ap);
343eda14cbcSMatt Macy hdl->libzfs_desc_active = 1;
344eda14cbcSMatt Macy
345eda14cbcSMatt Macy va_end(ap);
346eda14cbcSMatt Macy }
347eda14cbcSMatt Macy
348eda14cbcSMatt Macy static void
zfs_verror(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)349eda14cbcSMatt Macy zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
350eda14cbcSMatt Macy {
351eda14cbcSMatt Macy (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
352eda14cbcSMatt Macy fmt, ap);
353eda14cbcSMatt Macy hdl->libzfs_error = error;
354eda14cbcSMatt Macy
355eda14cbcSMatt Macy if (hdl->libzfs_desc_active)
356eda14cbcSMatt Macy hdl->libzfs_desc_active = 0;
357eda14cbcSMatt Macy else
358eda14cbcSMatt Macy hdl->libzfs_desc[0] = '\0';
359eda14cbcSMatt Macy
360eda14cbcSMatt Macy if (hdl->libzfs_printerr) {
361eda14cbcSMatt Macy if (error == EZFS_UNKNOWN) {
362eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
363eda14cbcSMatt Macy "error: %s: %s\n"), hdl->libzfs_action,
364eda14cbcSMatt Macy libzfs_error_description(hdl));
365eda14cbcSMatt Macy abort();
366eda14cbcSMatt Macy }
367eda14cbcSMatt Macy
368eda14cbcSMatt Macy (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
369eda14cbcSMatt Macy libzfs_error_description(hdl));
370eda14cbcSMatt Macy if (error == EZFS_NOMEM)
371eda14cbcSMatt Macy exit(1);
372eda14cbcSMatt Macy }
373eda14cbcSMatt Macy }
374eda14cbcSMatt Macy
375eda14cbcSMatt Macy int
zfs_error(libzfs_handle_t * hdl,int error,const char * msg)376eda14cbcSMatt Macy zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
377eda14cbcSMatt Macy {
378eda14cbcSMatt Macy return (zfs_error_fmt(hdl, error, "%s", msg));
379eda14cbcSMatt Macy }
380eda14cbcSMatt Macy
381eda14cbcSMatt Macy int
zfs_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)382eda14cbcSMatt Macy zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
383eda14cbcSMatt Macy {
384eda14cbcSMatt Macy va_list ap;
385eda14cbcSMatt Macy
386eda14cbcSMatt Macy va_start(ap, fmt);
387eda14cbcSMatt Macy
388eda14cbcSMatt Macy zfs_verror(hdl, error, fmt, ap);
389eda14cbcSMatt Macy
390eda14cbcSMatt Macy va_end(ap);
391eda14cbcSMatt Macy
392eda14cbcSMatt Macy return (-1);
393eda14cbcSMatt Macy }
394eda14cbcSMatt Macy
395eda14cbcSMatt Macy static int
zfs_common_error(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)396eda14cbcSMatt Macy zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
397eda14cbcSMatt Macy va_list ap)
398eda14cbcSMatt Macy {
399eda14cbcSMatt Macy switch (error) {
400eda14cbcSMatt Macy case EPERM:
401eda14cbcSMatt Macy case EACCES:
402eda14cbcSMatt Macy zfs_verror(hdl, EZFS_PERM, fmt, ap);
403eda14cbcSMatt Macy return (-1);
404eda14cbcSMatt Macy
405eda14cbcSMatt Macy case ECANCELED:
406eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
407eda14cbcSMatt Macy return (-1);
408eda14cbcSMatt Macy
409eda14cbcSMatt Macy case EIO:
410eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IO, fmt, ap);
411eda14cbcSMatt Macy return (-1);
412eda14cbcSMatt Macy
413eda14cbcSMatt Macy case EFAULT:
414eda14cbcSMatt Macy zfs_verror(hdl, EZFS_FAULT, fmt, ap);
415eda14cbcSMatt Macy return (-1);
416eda14cbcSMatt Macy
417eda14cbcSMatt Macy case EINTR:
418eda14cbcSMatt Macy zfs_verror(hdl, EZFS_INTR, fmt, ap);
419eda14cbcSMatt Macy return (-1);
420c7046f76SMartin Matuska
421c7046f76SMartin Matuska case ECKSUM:
422c7046f76SMartin Matuska zfs_verror(hdl, EZFS_CKSUM, fmt, ap);
423c7046f76SMartin Matuska return (-1);
424eda14cbcSMatt Macy }
425eda14cbcSMatt Macy
426eda14cbcSMatt Macy return (0);
427eda14cbcSMatt Macy }
428eda14cbcSMatt Macy
429eda14cbcSMatt Macy int
zfs_standard_error(libzfs_handle_t * hdl,int error,const char * msg)430eda14cbcSMatt Macy zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
431eda14cbcSMatt Macy {
432eda14cbcSMatt Macy return (zfs_standard_error_fmt(hdl, error, "%s", msg));
433eda14cbcSMatt Macy }
434eda14cbcSMatt Macy
435eda14cbcSMatt Macy int
zfs_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)436eda14cbcSMatt Macy zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
437eda14cbcSMatt Macy {
438eda14cbcSMatt Macy va_list ap;
439eda14cbcSMatt Macy
440eda14cbcSMatt Macy va_start(ap, fmt);
441eda14cbcSMatt Macy
442eda14cbcSMatt Macy if (zfs_common_error(hdl, error, fmt, ap) != 0) {
443eda14cbcSMatt Macy va_end(ap);
444eda14cbcSMatt Macy return (-1);
445eda14cbcSMatt Macy }
446eda14cbcSMatt Macy
447eda14cbcSMatt Macy switch (error) {
448eda14cbcSMatt Macy case ENXIO:
449eda14cbcSMatt Macy case ENODEV:
450eda14cbcSMatt Macy case EPIPE:
451eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IO, fmt, ap);
452eda14cbcSMatt Macy break;
453eda14cbcSMatt Macy
454eda14cbcSMatt Macy case ENOENT:
455eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
456eda14cbcSMatt Macy "dataset does not exist"));
457eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOENT, fmt, ap);
458eda14cbcSMatt Macy break;
459eda14cbcSMatt Macy
460eda14cbcSMatt Macy case ENOSPC:
461eda14cbcSMatt Macy case EDQUOT:
462eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
463eda14cbcSMatt Macy break;
464eda14cbcSMatt Macy
465eda14cbcSMatt Macy case EEXIST:
466eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467eda14cbcSMatt Macy "dataset already exists"));
468eda14cbcSMatt Macy zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
469eda14cbcSMatt Macy break;
470eda14cbcSMatt Macy
471eda14cbcSMatt Macy case EBUSY:
472eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
473eda14cbcSMatt Macy "dataset is busy"));
474eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BUSY, fmt, ap);
475eda14cbcSMatt Macy break;
476eda14cbcSMatt Macy case EROFS:
477eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
478eda14cbcSMatt Macy break;
479eda14cbcSMatt Macy case ENAMETOOLONG:
480eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
481eda14cbcSMatt Macy break;
482eda14cbcSMatt Macy case ENOTSUP:
483eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
484eda14cbcSMatt Macy break;
485eda14cbcSMatt Macy case EAGAIN:
486eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
487eda14cbcSMatt Macy "pool I/O is currently suspended"));
488eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
489eda14cbcSMatt Macy break;
490eda14cbcSMatt Macy case EREMOTEIO:
491eda14cbcSMatt Macy zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
492eda14cbcSMatt Macy break;
493eda14cbcSMatt Macy case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE:
494eda14cbcSMatt Macy case ZFS_ERR_IOC_CMD_UNAVAIL:
495eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
496eda14cbcSMatt Macy "module does not support this operation. A reboot may "
497eda14cbcSMatt Macy "be required to enable this operation."));
498eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
499eda14cbcSMatt Macy break;
500eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_UNAVAIL:
501eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
502eda14cbcSMatt Macy "module does not support an option for this operation. "
503eda14cbcSMatt Macy "A reboot may be required to enable this option."));
504eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
505eda14cbcSMatt Macy break;
506eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_REQUIRED:
507eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_BADTYPE:
508eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
509eda14cbcSMatt Macy break;
510eda14cbcSMatt Macy case ZFS_ERR_WRONG_PARENT:
511eda14cbcSMatt Macy zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap);
512eda14cbcSMatt Macy break;
513eda14cbcSMatt Macy case ZFS_ERR_BADPROP:
514eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
515eda14cbcSMatt Macy break;
5161f1e2261SMartin Matuska case ZFS_ERR_NOT_USER_NAMESPACE:
5171f1e2261SMartin Matuska zfs_verror(hdl, EZFS_NOT_USER_NAMESPACE, fmt, ap);
5181f1e2261SMartin Matuska break;
519eda14cbcSMatt Macy default:
520fd45b686SMartin Matuska zfs_error_aux(hdl, "%s", zfs_strerror(error));
521eda14cbcSMatt Macy zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
522eda14cbcSMatt Macy break;
523eda14cbcSMatt Macy }
524eda14cbcSMatt Macy
525eda14cbcSMatt Macy va_end(ap);
526eda14cbcSMatt Macy return (-1);
527eda14cbcSMatt Macy }
528eda14cbcSMatt Macy
529eda14cbcSMatt Macy void
zfs_setprop_error(libzfs_handle_t * hdl,zfs_prop_t prop,int err,char * errbuf)530eda14cbcSMatt Macy zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
531eda14cbcSMatt Macy char *errbuf)
532eda14cbcSMatt Macy {
533eda14cbcSMatt Macy switch (err) {
534eda14cbcSMatt Macy
535eda14cbcSMatt Macy case ENOSPC:
536eda14cbcSMatt Macy /*
537eda14cbcSMatt Macy * For quotas and reservations, ENOSPC indicates
538eda14cbcSMatt Macy * something different; setting a quota or reservation
539eda14cbcSMatt Macy * doesn't use any disk space.
540eda14cbcSMatt Macy */
541eda14cbcSMatt Macy switch (prop) {
542eda14cbcSMatt Macy case ZFS_PROP_QUOTA:
543eda14cbcSMatt Macy case ZFS_PROP_REFQUOTA:
544eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
545eda14cbcSMatt Macy "size is less than current used or "
546eda14cbcSMatt Macy "reserved space"));
547eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
548eda14cbcSMatt Macy break;
549eda14cbcSMatt Macy
550eda14cbcSMatt Macy case ZFS_PROP_RESERVATION:
551eda14cbcSMatt Macy case ZFS_PROP_REFRESERVATION:
552eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
553eda14cbcSMatt Macy "size is greater than available space"));
554eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
555eda14cbcSMatt Macy break;
556eda14cbcSMatt Macy
557eda14cbcSMatt Macy default:
558eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf);
559eda14cbcSMatt Macy break;
560eda14cbcSMatt Macy }
561eda14cbcSMatt Macy break;
562eda14cbcSMatt Macy
563eda14cbcSMatt Macy case EBUSY:
564eda14cbcSMatt Macy (void) zfs_standard_error(hdl, EBUSY, errbuf);
565eda14cbcSMatt Macy break;
566eda14cbcSMatt Macy
567eda14cbcSMatt Macy case EROFS:
568eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
569eda14cbcSMatt Macy break;
570eda14cbcSMatt Macy
571eda14cbcSMatt Macy case E2BIG:
572eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
573eda14cbcSMatt Macy "property value too long"));
574eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
575eda14cbcSMatt Macy break;
576eda14cbcSMatt Macy
577eda14cbcSMatt Macy case ENOTSUP:
578eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
579eda14cbcSMatt Macy "pool and or dataset must be upgraded to set this "
580eda14cbcSMatt Macy "property or value"));
581eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
582eda14cbcSMatt Macy break;
583eda14cbcSMatt Macy
584eda14cbcSMatt Macy case ERANGE:
585eda14cbcSMatt Macy if (prop == ZFS_PROP_COMPRESSION ||
586eda14cbcSMatt Macy prop == ZFS_PROP_DNODESIZE ||
587eda14cbcSMatt Macy prop == ZFS_PROP_RECORDSIZE) {
588eda14cbcSMatt Macy (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
589eda14cbcSMatt Macy "property setting is not allowed on "
590eda14cbcSMatt Macy "bootable datasets"));
591eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
592eda14cbcSMatt Macy } else if (prop == ZFS_PROP_CHECKSUM ||
593eda14cbcSMatt Macy prop == ZFS_PROP_DEDUP) {
594eda14cbcSMatt Macy (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
595eda14cbcSMatt Macy "property setting is not allowed on "
596eda14cbcSMatt Macy "root pools"));
597eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
598eda14cbcSMatt Macy } else {
599eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf);
600eda14cbcSMatt Macy }
601eda14cbcSMatt Macy break;
602eda14cbcSMatt Macy
603eda14cbcSMatt Macy case EINVAL:
604eda14cbcSMatt Macy if (prop == ZPROP_INVAL) {
605eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
606eda14cbcSMatt Macy } else {
607eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf);
608eda14cbcSMatt Macy }
609eda14cbcSMatt Macy break;
610eda14cbcSMatt Macy
611eda14cbcSMatt Macy case ZFS_ERR_BADPROP:
612eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
613eda14cbcSMatt Macy break;
614eda14cbcSMatt Macy
615eda14cbcSMatt Macy case EACCES:
616eda14cbcSMatt Macy if (prop == ZFS_PROP_KEYLOCATION) {
617eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
618eda14cbcSMatt Macy "keylocation may only be set on encryption roots"));
619eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
620eda14cbcSMatt Macy } else {
621eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf);
622eda14cbcSMatt Macy }
623eda14cbcSMatt Macy break;
624eda14cbcSMatt Macy
625eda14cbcSMatt Macy case EOVERFLOW:
626eda14cbcSMatt Macy /*
627eda14cbcSMatt Macy * This platform can't address a volume this big.
628eda14cbcSMatt Macy */
629eda14cbcSMatt Macy #ifdef _ILP32
630eda14cbcSMatt Macy if (prop == ZFS_PROP_VOLSIZE) {
631eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
632eda14cbcSMatt Macy break;
633eda14cbcSMatt Macy }
634c03c5b1cSMartin Matuska zfs_fallthrough;
635eda14cbcSMatt Macy #endif
636eda14cbcSMatt Macy default:
637eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf);
638eda14cbcSMatt Macy }
639eda14cbcSMatt Macy }
640eda14cbcSMatt Macy
641eda14cbcSMatt Macy int
zpool_standard_error(libzfs_handle_t * hdl,int error,const char * msg)642eda14cbcSMatt Macy zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
643eda14cbcSMatt Macy {
644eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, error, "%s", msg));
645eda14cbcSMatt Macy }
646eda14cbcSMatt Macy
647eda14cbcSMatt Macy int
zpool_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)648eda14cbcSMatt Macy zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
649eda14cbcSMatt Macy {
650eda14cbcSMatt Macy va_list ap;
651eda14cbcSMatt Macy
652eda14cbcSMatt Macy va_start(ap, fmt);
653eda14cbcSMatt Macy
654eda14cbcSMatt Macy if (zfs_common_error(hdl, error, fmt, ap) != 0) {
655eda14cbcSMatt Macy va_end(ap);
656eda14cbcSMatt Macy return (-1);
657eda14cbcSMatt Macy }
658eda14cbcSMatt Macy
659eda14cbcSMatt Macy switch (error) {
660eda14cbcSMatt Macy case ENODEV:
661eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
662eda14cbcSMatt Macy break;
663eda14cbcSMatt Macy
664eda14cbcSMatt Macy case ENOENT:
665eda14cbcSMatt Macy zfs_error_aux(hdl,
666eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "no such pool or dataset"));
667eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOENT, fmt, ap);
668eda14cbcSMatt Macy break;
669eda14cbcSMatt Macy
670eda14cbcSMatt Macy case EEXIST:
671eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
672eda14cbcSMatt Macy "pool already exists"));
673eda14cbcSMatt Macy zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
674eda14cbcSMatt Macy break;
675eda14cbcSMatt Macy
676eda14cbcSMatt Macy case EBUSY:
677eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
678eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BUSY, fmt, ap);
679eda14cbcSMatt Macy break;
680eda14cbcSMatt Macy
681eda14cbcSMatt Macy /* There is no pending operation to cancel */
682eda14cbcSMatt Macy case ENOTACTIVE:
683eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
684eda14cbcSMatt Macy break;
685eda14cbcSMatt Macy
686eda14cbcSMatt Macy case ENXIO:
687eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
688eda14cbcSMatt Macy "one or more devices is currently unavailable"));
689eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
690eda14cbcSMatt Macy break;
691eda14cbcSMatt Macy
692eda14cbcSMatt Macy case ENAMETOOLONG:
693eda14cbcSMatt Macy zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
694eda14cbcSMatt Macy break;
695eda14cbcSMatt Macy
696eda14cbcSMatt Macy case ENOTSUP:
697eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
698eda14cbcSMatt Macy break;
699eda14cbcSMatt Macy
700eda14cbcSMatt Macy case EINVAL:
701eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
702eda14cbcSMatt Macy break;
703eda14cbcSMatt Macy
704eda14cbcSMatt Macy case ENOSPC:
705eda14cbcSMatt Macy case EDQUOT:
706eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
707c7046f76SMartin Matuska break;
708eda14cbcSMatt Macy
709eda14cbcSMatt Macy case EAGAIN:
710eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
711eda14cbcSMatt Macy "pool I/O is currently suspended"));
712eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
713eda14cbcSMatt Macy break;
714eda14cbcSMatt Macy
715eda14cbcSMatt Macy case EROFS:
716eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
717eda14cbcSMatt Macy break;
718eda14cbcSMatt Macy case EDOM:
719eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
720eda14cbcSMatt Macy "block size out of range or does not match"));
721eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
722eda14cbcSMatt Macy break;
723eda14cbcSMatt Macy case EREMOTEIO:
724eda14cbcSMatt Macy zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
725eda14cbcSMatt Macy break;
726eda14cbcSMatt Macy case ZFS_ERR_CHECKPOINT_EXISTS:
727eda14cbcSMatt Macy zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
728eda14cbcSMatt Macy break;
729eda14cbcSMatt Macy case ZFS_ERR_DISCARDING_CHECKPOINT:
730eda14cbcSMatt Macy zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
731eda14cbcSMatt Macy break;
732eda14cbcSMatt Macy case ZFS_ERR_NO_CHECKPOINT:
733eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
734eda14cbcSMatt Macy break;
735eda14cbcSMatt Macy case ZFS_ERR_DEVRM_IN_PROGRESS:
736eda14cbcSMatt Macy zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
737eda14cbcSMatt Macy break;
738eda14cbcSMatt Macy case ZFS_ERR_VDEV_TOO_BIG:
739eda14cbcSMatt Macy zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
740eda14cbcSMatt Macy break;
741eda14cbcSMatt Macy case ZFS_ERR_EXPORT_IN_PROGRESS:
742eda14cbcSMatt Macy zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap);
743eda14cbcSMatt Macy break;
744eda14cbcSMatt Macy case ZFS_ERR_RESILVER_IN_PROGRESS:
745eda14cbcSMatt Macy zfs_verror(hdl, EZFS_RESILVERING, fmt, ap);
746eda14cbcSMatt Macy break;
747eda14cbcSMatt Macy case ZFS_ERR_REBUILD_IN_PROGRESS:
748eda14cbcSMatt Macy zfs_verror(hdl, EZFS_REBUILDING, fmt, ap);
749eda14cbcSMatt Macy break;
750eda14cbcSMatt Macy case ZFS_ERR_BADPROP:
751eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADPROP, fmt, ap);
752eda14cbcSMatt Macy break;
753681ce946SMartin Matuska case ZFS_ERR_VDEV_NOTSUP:
754681ce946SMartin Matuska zfs_verror(hdl, EZFS_VDEV_NOTSUP, fmt, ap);
755681ce946SMartin Matuska break;
756eda14cbcSMatt Macy case ZFS_ERR_IOC_CMD_UNAVAIL:
757eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
758eda14cbcSMatt Macy "module does not support this operation. A reboot may "
759eda14cbcSMatt Macy "be required to enable this operation."));
760eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
761eda14cbcSMatt Macy break;
762eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_UNAVAIL:
763eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
764eda14cbcSMatt Macy "module does not support an option for this operation. "
765eda14cbcSMatt Macy "A reboot may be required to enable this option."));
766eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
767eda14cbcSMatt Macy break;
768eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_REQUIRED:
769eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_BADTYPE:
770eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
771eda14cbcSMatt Macy break;
772e716630dSMartin Matuska case ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS:
773e716630dSMartin Matuska zfs_verror(hdl, EZFS_RAIDZ_EXPAND_IN_PROGRESS, fmt, ap);
774e716630dSMartin Matuska break;
775783d3ff6SMartin Matuska case ZFS_ERR_ASHIFT_MISMATCH:
776783d3ff6SMartin Matuska zfs_verror(hdl, EZFS_ASHIFT_MISMATCH, fmt, ap);
777783d3ff6SMartin Matuska break;
778eda14cbcSMatt Macy default:
779fd45b686SMartin Matuska zfs_error_aux(hdl, "%s", zfs_strerror(error));
780eda14cbcSMatt Macy zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
781eda14cbcSMatt Macy }
782eda14cbcSMatt Macy
783eda14cbcSMatt Macy va_end(ap);
784eda14cbcSMatt Macy return (-1);
785eda14cbcSMatt Macy }
786eda14cbcSMatt Macy
787eda14cbcSMatt Macy /*
788eda14cbcSMatt Macy * Display an out of memory error message and abort the current program.
789eda14cbcSMatt Macy */
790eda14cbcSMatt Macy int
no_memory(libzfs_handle_t * hdl)791eda14cbcSMatt Macy no_memory(libzfs_handle_t *hdl)
792eda14cbcSMatt Macy {
793eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
794eda14cbcSMatt Macy }
795eda14cbcSMatt Macy
796eda14cbcSMatt Macy /*
797eda14cbcSMatt Macy * A safe form of malloc() which will die if the allocation fails.
798eda14cbcSMatt Macy */
799eda14cbcSMatt Macy void *
zfs_alloc(libzfs_handle_t * hdl,size_t size)800eda14cbcSMatt Macy zfs_alloc(libzfs_handle_t *hdl, size_t size)
801eda14cbcSMatt Macy {
802eda14cbcSMatt Macy void *data;
803eda14cbcSMatt Macy
804eda14cbcSMatt Macy if ((data = calloc(1, size)) == NULL)
805eda14cbcSMatt Macy (void) no_memory(hdl);
806eda14cbcSMatt Macy
807eda14cbcSMatt Macy return (data);
808eda14cbcSMatt Macy }
809eda14cbcSMatt Macy
810eda14cbcSMatt Macy /*
811eda14cbcSMatt Macy * A safe form of asprintf() which will die if the allocation fails.
812eda14cbcSMatt Macy */
813eda14cbcSMatt Macy char *
zfs_asprintf(libzfs_handle_t * hdl,const char * fmt,...)814eda14cbcSMatt Macy zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
815eda14cbcSMatt Macy {
816eda14cbcSMatt Macy va_list ap;
817eda14cbcSMatt Macy char *ret;
818eda14cbcSMatt Macy int err;
819eda14cbcSMatt Macy
820eda14cbcSMatt Macy va_start(ap, fmt);
821eda14cbcSMatt Macy
822eda14cbcSMatt Macy err = vasprintf(&ret, fmt, ap);
823eda14cbcSMatt Macy
824eda14cbcSMatt Macy va_end(ap);
825eda14cbcSMatt Macy
82616038816SMartin Matuska if (err < 0) {
827eda14cbcSMatt Macy (void) no_memory(hdl);
82816038816SMartin Matuska ret = NULL;
82916038816SMartin Matuska }
830eda14cbcSMatt Macy
831eda14cbcSMatt Macy return (ret);
832eda14cbcSMatt Macy }
833eda14cbcSMatt Macy
834eda14cbcSMatt Macy /*
835eda14cbcSMatt Macy * A safe form of realloc(), which also zeroes newly allocated space.
836eda14cbcSMatt Macy */
837eda14cbcSMatt Macy void *
zfs_realloc(libzfs_handle_t * hdl,void * ptr,size_t oldsize,size_t newsize)838eda14cbcSMatt Macy zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
839eda14cbcSMatt Macy {
840eda14cbcSMatt Macy void *ret;
841eda14cbcSMatt Macy
842eda14cbcSMatt Macy if ((ret = realloc(ptr, newsize)) == NULL) {
843eda14cbcSMatt Macy (void) no_memory(hdl);
844eda14cbcSMatt Macy return (NULL);
845eda14cbcSMatt Macy }
846eda14cbcSMatt Macy
847da5137abSMartin Matuska memset((char *)ret + oldsize, 0, newsize - oldsize);
848eda14cbcSMatt Macy return (ret);
849eda14cbcSMatt Macy }
850eda14cbcSMatt Macy
851eda14cbcSMatt Macy /*
852eda14cbcSMatt Macy * A safe form of strdup() which will die if the allocation fails.
853eda14cbcSMatt Macy */
854eda14cbcSMatt Macy char *
zfs_strdup(libzfs_handle_t * hdl,const char * str)855eda14cbcSMatt Macy zfs_strdup(libzfs_handle_t *hdl, const char *str)
856eda14cbcSMatt Macy {
857eda14cbcSMatt Macy char *ret;
858eda14cbcSMatt Macy
859eda14cbcSMatt Macy if ((ret = strdup(str)) == NULL)
860eda14cbcSMatt Macy (void) no_memory(hdl);
861eda14cbcSMatt Macy
862eda14cbcSMatt Macy return (ret);
863eda14cbcSMatt Macy }
864eda14cbcSMatt Macy
865eda14cbcSMatt Macy void
libzfs_print_on_error(libzfs_handle_t * hdl,boolean_t printerr)866eda14cbcSMatt Macy libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
867eda14cbcSMatt Macy {
868eda14cbcSMatt Macy hdl->libzfs_printerr = printerr;
869eda14cbcSMatt Macy }
870eda14cbcSMatt Macy
871eda14cbcSMatt Macy /*
872eda14cbcSMatt Macy * Read lines from an open file descriptor and store them in an array of
873eda14cbcSMatt Macy * strings until EOF. lines[] will be allocated and populated with all the
874eda14cbcSMatt Macy * lines read. All newlines are replaced with NULL terminators for
875eda14cbcSMatt Macy * convenience. lines[] must be freed after use with libzfs_free_str_array().
876eda14cbcSMatt Macy *
877eda14cbcSMatt Macy * Returns the number of lines read.
878eda14cbcSMatt Macy */
879eda14cbcSMatt Macy static int
libzfs_read_stdout_from_fd(int fd,char ** lines[])880eda14cbcSMatt Macy libzfs_read_stdout_from_fd(int fd, char **lines[])
881eda14cbcSMatt Macy {
882eda14cbcSMatt Macy
883eda14cbcSMatt Macy FILE *fp;
884eda14cbcSMatt Macy int lines_cnt = 0;
885eda14cbcSMatt Macy size_t len = 0;
886eda14cbcSMatt Macy char *line = NULL;
887eda14cbcSMatt Macy char **tmp_lines = NULL, **tmp;
888eda14cbcSMatt Macy
889eda14cbcSMatt Macy fp = fdopen(fd, "r");
89016038816SMartin Matuska if (fp == NULL) {
89116038816SMartin Matuska close(fd);
892eda14cbcSMatt Macy return (0);
89316038816SMartin Matuska }
89416038816SMartin Matuska while (getline(&line, &len, fp) != -1) {
895eda14cbcSMatt Macy tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1));
896eda14cbcSMatt Macy if (tmp == NULL) {
897eda14cbcSMatt Macy /* Return the lines we were able to process */
898eda14cbcSMatt Macy break;
899eda14cbcSMatt Macy }
900eda14cbcSMatt Macy tmp_lines = tmp;
901eda14cbcSMatt Macy
90216038816SMartin Matuska /* Remove newline if not EOF */
90316038816SMartin Matuska if (line[strlen(line) - 1] == '\n')
90416038816SMartin Matuska line[strlen(line) - 1] = '\0';
90516038816SMartin Matuska
90616038816SMartin Matuska tmp_lines[lines_cnt] = strdup(line);
90716038816SMartin Matuska if (tmp_lines[lines_cnt] == NULL)
90816038816SMartin Matuska break;
90916038816SMartin Matuska ++lines_cnt;
910eda14cbcSMatt Macy }
91116038816SMartin Matuska free(line);
912eda14cbcSMatt Macy fclose(fp);
913eda14cbcSMatt Macy *lines = tmp_lines;
914eda14cbcSMatt Macy return (lines_cnt);
915eda14cbcSMatt Macy }
916eda14cbcSMatt Macy
917eda14cbcSMatt Macy static int
libzfs_run_process_impl(const char * path,char * argv[],char * env[],int flags,char ** lines[],int * lines_cnt)918eda14cbcSMatt Macy libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
919eda14cbcSMatt Macy char **lines[], int *lines_cnt)
920eda14cbcSMatt Macy {
921eda14cbcSMatt Macy pid_t pid;
922eda14cbcSMatt Macy int error, devnull_fd;
923eda14cbcSMatt Macy int link[2];
924eda14cbcSMatt Macy
925eda14cbcSMatt Macy /*
926eda14cbcSMatt Macy * Setup a pipe between our child and parent process if we're
927eda14cbcSMatt Macy * reading stdout.
928eda14cbcSMatt Macy */
92916038816SMartin Matuska if (lines != NULL && pipe2(link, O_NONBLOCK | O_CLOEXEC) == -1)
930eda14cbcSMatt Macy return (-EPIPE);
931eda14cbcSMatt Macy
93216038816SMartin Matuska pid = fork();
933eda14cbcSMatt Macy if (pid == 0) {
934eda14cbcSMatt Macy /* Child process */
93516038816SMartin Matuska devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
936eda14cbcSMatt Macy
937eda14cbcSMatt Macy if (devnull_fd < 0)
938eda14cbcSMatt Macy _exit(-1);
939eda14cbcSMatt Macy
940eda14cbcSMatt Macy if (!(flags & STDOUT_VERBOSE) && (lines == NULL))
941eda14cbcSMatt Macy (void) dup2(devnull_fd, STDOUT_FILENO);
942eda14cbcSMatt Macy else if (lines != NULL) {
943eda14cbcSMatt Macy /* Save the output to lines[] */
944eda14cbcSMatt Macy dup2(link[1], STDOUT_FILENO);
945eda14cbcSMatt Macy }
946eda14cbcSMatt Macy
947eda14cbcSMatt Macy if (!(flags & STDERR_VERBOSE))
948eda14cbcSMatt Macy (void) dup2(devnull_fd, STDERR_FILENO);
949eda14cbcSMatt Macy
950eda14cbcSMatt Macy if (flags & NO_DEFAULT_PATH) {
951eda14cbcSMatt Macy if (env == NULL)
952eda14cbcSMatt Macy execv(path, argv);
953eda14cbcSMatt Macy else
954eda14cbcSMatt Macy execve(path, argv, env);
955eda14cbcSMatt Macy } else {
956eda14cbcSMatt Macy if (env == NULL)
957eda14cbcSMatt Macy execvp(path, argv);
958eda14cbcSMatt Macy else
959eda14cbcSMatt Macy execvpe(path, argv, env);
960eda14cbcSMatt Macy }
961eda14cbcSMatt Macy
962eda14cbcSMatt Macy _exit(-1);
963eda14cbcSMatt Macy } else if (pid > 0) {
964eda14cbcSMatt Macy /* Parent process */
965eda14cbcSMatt Macy int status;
966eda14cbcSMatt Macy
967eda14cbcSMatt Macy while ((error = waitpid(pid, &status, 0)) == -1 &&
96816038816SMartin Matuska errno == EINTR)
96916038816SMartin Matuska ;
970eda14cbcSMatt Macy if (error < 0 || !WIFEXITED(status))
971eda14cbcSMatt Macy return (-1);
972eda14cbcSMatt Macy
973eda14cbcSMatt Macy if (lines != NULL) {
974eda14cbcSMatt Macy close(link[1]);
975eda14cbcSMatt Macy *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines);
976eda14cbcSMatt Macy }
977eda14cbcSMatt Macy return (WEXITSTATUS(status));
978eda14cbcSMatt Macy }
979eda14cbcSMatt Macy
980eda14cbcSMatt Macy return (-1);
981eda14cbcSMatt Macy }
982eda14cbcSMatt Macy
983eda14cbcSMatt Macy int
libzfs_run_process(const char * path,char * argv[],int flags)984eda14cbcSMatt Macy libzfs_run_process(const char *path, char *argv[], int flags)
985eda14cbcSMatt Macy {
986eda14cbcSMatt Macy return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL));
987eda14cbcSMatt Macy }
988eda14cbcSMatt Macy
989eda14cbcSMatt Macy /*
990eda14cbcSMatt Macy * Run a command and store its stdout lines in an array of strings (lines[]).
991eda14cbcSMatt Macy * lines[] is allocated and populated for you, and the number of lines is set in
992eda14cbcSMatt Macy * lines_cnt. lines[] must be freed after use with libzfs_free_str_array().
993eda14cbcSMatt Macy * All newlines (\n) in lines[] are terminated for convenience.
994eda14cbcSMatt Macy */
995eda14cbcSMatt Macy int
libzfs_run_process_get_stdout(const char * path,char * argv[],char * env[],char ** lines[],int * lines_cnt)996eda14cbcSMatt Macy libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[],
997eda14cbcSMatt Macy char **lines[], int *lines_cnt)
998eda14cbcSMatt Macy {
999eda14cbcSMatt Macy return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt));
1000eda14cbcSMatt Macy }
1001eda14cbcSMatt Macy
1002eda14cbcSMatt Macy /*
1003eda14cbcSMatt Macy * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This
1004eda14cbcSMatt Macy * means that *path needs to be the full path to the executable.
1005eda14cbcSMatt Macy */
1006eda14cbcSMatt Macy int
libzfs_run_process_get_stdout_nopath(const char * path,char * argv[],char * env[],char ** lines[],int * lines_cnt)1007eda14cbcSMatt Macy libzfs_run_process_get_stdout_nopath(const char *path, char *argv[],
1008eda14cbcSMatt Macy char *env[], char **lines[], int *lines_cnt)
1009eda14cbcSMatt Macy {
1010eda14cbcSMatt Macy return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH,
1011eda14cbcSMatt Macy lines, lines_cnt));
1012eda14cbcSMatt Macy }
1013eda14cbcSMatt Macy
1014eda14cbcSMatt Macy /*
1015eda14cbcSMatt Macy * Free an array of strings. Free both the strings contained in the array and
1016eda14cbcSMatt Macy * the array itself.
1017eda14cbcSMatt Macy */
1018eda14cbcSMatt Macy void
libzfs_free_str_array(char ** strs,int count)1019eda14cbcSMatt Macy libzfs_free_str_array(char **strs, int count)
1020eda14cbcSMatt Macy {
1021eda14cbcSMatt Macy while (--count >= 0)
1022eda14cbcSMatt Macy free(strs[count]);
1023eda14cbcSMatt Macy
1024eda14cbcSMatt Macy free(strs);
1025eda14cbcSMatt Macy }
1026eda14cbcSMatt Macy
1027eda14cbcSMatt Macy /*
1028eda14cbcSMatt Macy * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or
1029eda14cbcSMatt Macy * a non-zero number.
1030eda14cbcSMatt Macy *
1031eda14cbcSMatt Macy * Returns 0 otherwise.
1032eda14cbcSMatt Macy */
1033a0b956f5SMartin Matuska boolean_t
libzfs_envvar_is_set(const char * envvar)1034a0b956f5SMartin Matuska libzfs_envvar_is_set(const char *envvar)
1035eda14cbcSMatt Macy {
1036eda14cbcSMatt Macy char *env = getenv(envvar);
1037a0b956f5SMartin Matuska return (env && (strtoul(env, NULL, 0) > 0 ||
1038eda14cbcSMatt Macy (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) ||
1039a0b956f5SMartin Matuska (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2)));
1040eda14cbcSMatt Macy }
1041eda14cbcSMatt Macy
1042eda14cbcSMatt Macy libzfs_handle_t *
libzfs_init(void)1043eda14cbcSMatt Macy libzfs_init(void)
1044eda14cbcSMatt Macy {
1045eda14cbcSMatt Macy libzfs_handle_t *hdl;
1046eda14cbcSMatt Macy int error;
1047ac0bf12eSMatt Macy char *env;
1048eda14cbcSMatt Macy
1049180f8225SMatt Macy if ((error = libzfs_load_module()) != 0) {
1050eda14cbcSMatt Macy errno = error;
1051eda14cbcSMatt Macy return (NULL);
1052eda14cbcSMatt Macy }
1053eda14cbcSMatt Macy
1054eda14cbcSMatt Macy if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
1055eda14cbcSMatt Macy return (NULL);
1056eda14cbcSMatt Macy }
1057eda14cbcSMatt Macy
1058eda14cbcSMatt Macy if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) {
1059eda14cbcSMatt Macy free(hdl);
1060eda14cbcSMatt Macy return (NULL);
1061eda14cbcSMatt Macy }
1062eda14cbcSMatt Macy
106316038816SMartin Matuska if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) {
1064eda14cbcSMatt Macy free(hdl);
1065eda14cbcSMatt Macy return (NULL);
1066eda14cbcSMatt Macy }
1067eda14cbcSMatt Macy
1068eda14cbcSMatt Macy if (libzfs_core_init() != 0) {
1069eda14cbcSMatt Macy (void) close(hdl->libzfs_fd);
1070eda14cbcSMatt Macy free(hdl);
1071eda14cbcSMatt Macy return (NULL);
1072eda14cbcSMatt Macy }
1073eda14cbcSMatt Macy
1074eda14cbcSMatt Macy zfs_prop_init();
1075eda14cbcSMatt Macy zpool_prop_init();
1076eda14cbcSMatt Macy zpool_feature_init();
1077681ce946SMartin Matuska vdev_prop_init();
1078eda14cbcSMatt Macy libzfs_mnttab_init(hdl);
1079eda14cbcSMatt Macy fletcher_4_init();
1080eda14cbcSMatt Macy
1081eda14cbcSMatt Macy if (getenv("ZFS_PROP_DEBUG") != NULL) {
1082eda14cbcSMatt Macy hdl->libzfs_prop_debug = B_TRUE;
1083eda14cbcSMatt Macy }
1084ac0bf12eSMatt Macy if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) {
1085ac0bf12eSMatt Macy if ((error = zfs_nicestrtonum(hdl, env,
1086ac0bf12eSMatt Macy &hdl->libzfs_max_nvlist))) {
1087ac0bf12eSMatt Macy errno = error;
10882c48331dSMatt Macy (void) close(hdl->libzfs_fd);
10892c48331dSMatt Macy free(hdl);
1090ac0bf12eSMatt Macy return (NULL);
1091ac0bf12eSMatt Macy }
1092ac0bf12eSMatt Macy } else {
1093ac0bf12eSMatt Macy hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4);
1094ac0bf12eSMatt Macy }
1095eda14cbcSMatt Macy
1096eda14cbcSMatt Macy /*
1097eda14cbcSMatt Macy * For testing, remove some settable properties and features
1098eda14cbcSMatt Macy */
1099eda14cbcSMatt Macy if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) {
1100eda14cbcSMatt Macy zprop_desc_t *proptbl;
1101eda14cbcSMatt Macy
1102eda14cbcSMatt Macy proptbl = zpool_prop_get_table();
1103eda14cbcSMatt Macy proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE;
1104eda14cbcSMatt Macy
1105eda14cbcSMatt Macy proptbl = zfs_prop_get_table();
1106eda14cbcSMatt Macy proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE;
1107eda14cbcSMatt Macy
1108eda14cbcSMatt Macy zfeature_info_t *ftbl = spa_feature_table;
1109eda14cbcSMatt Macy ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE;
1110eda14cbcSMatt Macy }
1111eda14cbcSMatt Macy
1112eda14cbcSMatt Macy return (hdl);
1113eda14cbcSMatt Macy }
1114eda14cbcSMatt Macy
1115eda14cbcSMatt Macy void
libzfs_fini(libzfs_handle_t * hdl)1116eda14cbcSMatt Macy libzfs_fini(libzfs_handle_t *hdl)
1117eda14cbcSMatt Macy {
1118eda14cbcSMatt Macy (void) close(hdl->libzfs_fd);
1119eda14cbcSMatt Macy zpool_free_handles(hdl);
1120eda14cbcSMatt Macy namespace_clear(hdl);
1121eda14cbcSMatt Macy libzfs_mnttab_fini(hdl);
1122eda14cbcSMatt Macy libzfs_core_fini();
1123eda14cbcSMatt Macy regfree(&hdl->libzfs_urire);
1124eda14cbcSMatt Macy fletcher_4_fini();
112516038816SMartin Matuska #if LIBFETCH_DYNAMIC
112616038816SMartin Matuska if (hdl->libfetch != (void *)-1 && hdl->libfetch != NULL)
112716038816SMartin Matuska (void) dlclose(hdl->libfetch);
112816038816SMartin Matuska free(hdl->libfetch_load_error);
112916038816SMartin Matuska #endif
1130eda14cbcSMatt Macy free(hdl);
1131eda14cbcSMatt Macy }
1132eda14cbcSMatt Macy
1133eda14cbcSMatt Macy libzfs_handle_t *
zpool_get_handle(zpool_handle_t * zhp)1134eda14cbcSMatt Macy zpool_get_handle(zpool_handle_t *zhp)
1135eda14cbcSMatt Macy {
1136eda14cbcSMatt Macy return (zhp->zpool_hdl);
1137eda14cbcSMatt Macy }
1138eda14cbcSMatt Macy
1139eda14cbcSMatt Macy libzfs_handle_t *
zfs_get_handle(zfs_handle_t * zhp)1140eda14cbcSMatt Macy zfs_get_handle(zfs_handle_t *zhp)
1141eda14cbcSMatt Macy {
1142eda14cbcSMatt Macy return (zhp->zfs_hdl);
1143eda14cbcSMatt Macy }
1144eda14cbcSMatt Macy
1145eda14cbcSMatt Macy zpool_handle_t *
zfs_get_pool_handle(const zfs_handle_t * zhp)1146eda14cbcSMatt Macy zfs_get_pool_handle(const zfs_handle_t *zhp)
1147eda14cbcSMatt Macy {
1148eda14cbcSMatt Macy return (zhp->zpool_hdl);
1149eda14cbcSMatt Macy }
1150eda14cbcSMatt Macy
1151eda14cbcSMatt Macy /*
1152eda14cbcSMatt Macy * Given a name, determine whether or not it's a valid path
1153eda14cbcSMatt Macy * (starts with '/' or "./"). If so, walk the mnttab trying
1154eda14cbcSMatt Macy * to match the device number. If not, treat the path as an
1155eda14cbcSMatt Macy * fs/vol/snap/bkmark name.
1156eda14cbcSMatt Macy */
1157eda14cbcSMatt Macy zfs_handle_t *
zfs_path_to_zhandle(libzfs_handle_t * hdl,const char * path,zfs_type_t argtype)1158eda14cbcSMatt Macy zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype)
1159eda14cbcSMatt Macy {
1160eda14cbcSMatt Macy struct stat64 statbuf;
1161eda14cbcSMatt Macy struct extmnttab entry;
1162eda14cbcSMatt Macy
1163eda14cbcSMatt Macy if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
1164eda14cbcSMatt Macy /*
1165eda14cbcSMatt Macy * It's not a valid path, assume it's a name of type 'argtype'.
1166eda14cbcSMatt Macy */
1167eda14cbcSMatt Macy return (zfs_open(hdl, path, argtype));
1168eda14cbcSMatt Macy }
1169eda14cbcSMatt Macy
1170eda14cbcSMatt Macy if (getextmntent(path, &entry, &statbuf) != 0)
1171eda14cbcSMatt Macy return (NULL);
1172eda14cbcSMatt Macy
1173eda14cbcSMatt Macy if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
1174eda14cbcSMatt Macy (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
1175eda14cbcSMatt Macy path);
1176eda14cbcSMatt Macy return (NULL);
1177eda14cbcSMatt Macy }
1178eda14cbcSMatt Macy
1179eda14cbcSMatt Macy return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
1180eda14cbcSMatt Macy }
1181eda14cbcSMatt Macy
1182eda14cbcSMatt Macy /*
1183eda14cbcSMatt Macy * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
1184eda14cbcSMatt Macy * an ioctl().
1185eda14cbcSMatt Macy */
1186716fd348SMartin Matuska void
zcmd_alloc_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,size_t len)1187eda14cbcSMatt Macy zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
1188eda14cbcSMatt Macy {
1189eda14cbcSMatt Macy if (len == 0)
1190eda14cbcSMatt Macy len = 256 * 1024;
1191eda14cbcSMatt Macy zc->zc_nvlist_dst_size = len;
1192eda14cbcSMatt Macy zc->zc_nvlist_dst =
1193eda14cbcSMatt Macy (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1194eda14cbcSMatt Macy }
1195eda14cbcSMatt Macy
1196eda14cbcSMatt Macy /*
1197eda14cbcSMatt Macy * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
1198eda14cbcSMatt Macy * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
1199eda14cbcSMatt Macy * filled in by the kernel to indicate the actual required size.
1200eda14cbcSMatt Macy */
1201716fd348SMartin Matuska void
zcmd_expand_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc)1202eda14cbcSMatt Macy zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
1203eda14cbcSMatt Macy {
1204eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_dst);
1205eda14cbcSMatt Macy zc->zc_nvlist_dst =
1206eda14cbcSMatt Macy (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
1207eda14cbcSMatt Macy }
1208eda14cbcSMatt Macy
1209eda14cbcSMatt Macy /*
1210eda14cbcSMatt Macy * Called to free the src and dst nvlists stored in the command structure.
1211eda14cbcSMatt Macy */
1212eda14cbcSMatt Macy void
zcmd_free_nvlists(zfs_cmd_t * zc)1213eda14cbcSMatt Macy zcmd_free_nvlists(zfs_cmd_t *zc)
1214eda14cbcSMatt Macy {
1215eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_conf);
1216eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_src);
1217eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_dst);
1218eda14cbcSMatt Macy zc->zc_nvlist_conf = 0;
1219eda14cbcSMatt Macy zc->zc_nvlist_src = 0;
1220eda14cbcSMatt Macy zc->zc_nvlist_dst = 0;
1221eda14cbcSMatt Macy }
1222eda14cbcSMatt Macy
1223716fd348SMartin Matuska static void
zcmd_write_nvlist_com(libzfs_handle_t * hdl,uint64_t * outnv,uint64_t * outlen,nvlist_t * nvl)1224eda14cbcSMatt Macy zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
1225eda14cbcSMatt Macy nvlist_t *nvl)
1226eda14cbcSMatt Macy {
1227eda14cbcSMatt Macy char *packed;
1228eda14cbcSMatt Macy
1229da5137abSMartin Matuska size_t len = fnvlist_size(nvl);
1230716fd348SMartin Matuska packed = zfs_alloc(hdl, len);
1231eda14cbcSMatt Macy
1232eda14cbcSMatt Macy verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
1233eda14cbcSMatt Macy
1234eda14cbcSMatt Macy *outnv = (uint64_t)(uintptr_t)packed;
1235eda14cbcSMatt Macy *outlen = len;
1236eda14cbcSMatt Macy }
1237eda14cbcSMatt Macy
1238716fd348SMartin Matuska void
zcmd_write_conf_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t * nvl)1239eda14cbcSMatt Macy zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1240eda14cbcSMatt Macy {
1241716fd348SMartin Matuska zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
1242716fd348SMartin Matuska &zc->zc_nvlist_conf_size, nvl);
1243eda14cbcSMatt Macy }
1244eda14cbcSMatt Macy
1245716fd348SMartin Matuska void
zcmd_write_src_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t * nvl)1246eda14cbcSMatt Macy zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
1247eda14cbcSMatt Macy {
1248716fd348SMartin Matuska zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
1249716fd348SMartin Matuska &zc->zc_nvlist_src_size, nvl);
1250eda14cbcSMatt Macy }
1251eda14cbcSMatt Macy
1252eda14cbcSMatt Macy /*
1253eda14cbcSMatt Macy * Unpacks an nvlist from the ZFS ioctl command structure.
1254eda14cbcSMatt Macy */
1255eda14cbcSMatt Macy int
zcmd_read_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t ** nvlp)1256eda14cbcSMatt Macy zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
1257eda14cbcSMatt Macy {
1258eda14cbcSMatt Macy if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
1259eda14cbcSMatt Macy zc->zc_nvlist_dst_size, nvlp, 0) != 0)
1260eda14cbcSMatt Macy return (no_memory(hdl));
1261eda14cbcSMatt Macy
1262eda14cbcSMatt Macy return (0);
1263eda14cbcSMatt Macy }
1264eda14cbcSMatt Macy
1265eda14cbcSMatt Macy /*
1266eda14cbcSMatt Macy * ================================================================
1267eda14cbcSMatt Macy * API shared by zfs and zpool property management
1268eda14cbcSMatt Macy * ================================================================
1269eda14cbcSMatt Macy */
1270eda14cbcSMatt Macy
1271ce4dcb97SMartin Matuska void
zcmd_print_json(nvlist_t * nvl)1272ce4dcb97SMartin Matuska zcmd_print_json(nvlist_t *nvl)
1273ce4dcb97SMartin Matuska {
1274ce4dcb97SMartin Matuska nvlist_print_json(stdout, nvl);
1275ce4dcb97SMartin Matuska (void) putchar('\n');
1276ce4dcb97SMartin Matuska nvlist_free(nvl);
1277ce4dcb97SMartin Matuska }
1278ce4dcb97SMartin Matuska
1279eda14cbcSMatt Macy static void
zprop_print_headers(zprop_get_cbdata_t * cbp,zfs_type_t type)1280eda14cbcSMatt Macy zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
1281eda14cbcSMatt Macy {
1282dbd5678dSMartin Matuska zprop_list_t *pl;
1283eda14cbcSMatt Macy int i;
1284eda14cbcSMatt Macy char *title;
1285eda14cbcSMatt Macy size_t len;
1286eda14cbcSMatt Macy
1287eda14cbcSMatt Macy cbp->cb_first = B_FALSE;
1288eda14cbcSMatt Macy if (cbp->cb_scripted)
1289eda14cbcSMatt Macy return;
1290eda14cbcSMatt Macy
1291eda14cbcSMatt Macy /*
1292eda14cbcSMatt Macy * Start with the length of the column headers.
1293eda14cbcSMatt Macy */
1294eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
1295eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
1296eda14cbcSMatt Macy "PROPERTY"));
1297eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
1298eda14cbcSMatt Macy "VALUE"));
1299eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
1300eda14cbcSMatt Macy "RECEIVED"));
1301eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
1302eda14cbcSMatt Macy "SOURCE"));
1303eda14cbcSMatt Macy
1304eda14cbcSMatt Macy /* first property is always NAME */
1305eda14cbcSMatt Macy assert(cbp->cb_proplist->pl_prop ==
1306681ce946SMartin Matuska ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1307681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME)));
1308eda14cbcSMatt Macy
1309eda14cbcSMatt Macy /*
1310eda14cbcSMatt Macy * Go through and calculate the widths for each column. For the
1311eda14cbcSMatt Macy * 'source' column, we kludge it up by taking the worst-case scenario of
1312eda14cbcSMatt Macy * inheriting from the longest name. This is acceptable because in the
1313eda14cbcSMatt Macy * majority of cases 'SOURCE' is the last column displayed, and we don't
1314eda14cbcSMatt Macy * use the width anyway. Note that the 'VALUE' column can be oversized,
1315eda14cbcSMatt Macy * if the name of the property is much longer than any values we find.
1316eda14cbcSMatt Macy */
1317eda14cbcSMatt Macy for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
1318eda14cbcSMatt Macy /*
1319eda14cbcSMatt Macy * 'PROPERTY' column
1320eda14cbcSMatt Macy */
13211f1e2261SMartin Matuska if (pl->pl_prop != ZPROP_USERPROP) {
1322eda14cbcSMatt Macy const char *propname = (type == ZFS_TYPE_POOL) ?
1323eda14cbcSMatt Macy zpool_prop_to_name(pl->pl_prop) :
1324681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ?
1325681ce946SMartin Matuska vdev_prop_to_name(pl->pl_prop) :
1326681ce946SMartin Matuska zfs_prop_to_name(pl->pl_prop));
1327eda14cbcSMatt Macy
1328681ce946SMartin Matuska assert(propname != NULL);
1329eda14cbcSMatt Macy len = strlen(propname);
1330eda14cbcSMatt Macy if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1331eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1332eda14cbcSMatt Macy } else {
1333681ce946SMartin Matuska assert(pl->pl_user_prop != NULL);
1334eda14cbcSMatt Macy len = strlen(pl->pl_user_prop);
1335eda14cbcSMatt Macy if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
1336eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_PROPERTY] = len;
1337eda14cbcSMatt Macy }
1338eda14cbcSMatt Macy
1339eda14cbcSMatt Macy /*
1340eda14cbcSMatt Macy * 'VALUE' column. The first property is always the 'name'
1341eda14cbcSMatt Macy * property that was tacked on either by /sbin/zfs's
1342eda14cbcSMatt Macy * zfs_do_get() or when calling zprop_expand_list(), so we
1343eda14cbcSMatt Macy * ignore its width. If the user specified the name property
1344eda14cbcSMatt Macy * to display, then it will be later in the list in any case.
1345eda14cbcSMatt Macy */
1346eda14cbcSMatt Macy if (pl != cbp->cb_proplist &&
1347eda14cbcSMatt Macy pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
1348eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
1349eda14cbcSMatt Macy
1350eda14cbcSMatt Macy /* 'RECEIVED' column. */
1351eda14cbcSMatt Macy if (pl != cbp->cb_proplist &&
1352eda14cbcSMatt Macy pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
1353eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
1354eda14cbcSMatt Macy
1355eda14cbcSMatt Macy /*
1356eda14cbcSMatt Macy * 'NAME' and 'SOURCE' columns
1357eda14cbcSMatt Macy */
1358681ce946SMartin Matuska if (pl->pl_prop == ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1359681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME :
1360681ce946SMartin Matuska ZFS_PROP_NAME)) && pl->pl_width >
1361681ce946SMartin Matuska cbp->cb_colwidths[GET_COL_NAME]) {
1362eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1363eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1364eda14cbcSMatt Macy strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1365eda14cbcSMatt Macy }
1366eda14cbcSMatt Macy }
1367eda14cbcSMatt Macy
1368eda14cbcSMatt Macy /*
1369eda14cbcSMatt Macy * Now go through and print the headers.
1370eda14cbcSMatt Macy */
1371eda14cbcSMatt Macy for (i = 0; i < ZFS_GET_NCOLS; i++) {
1372eda14cbcSMatt Macy switch (cbp->cb_columns[i]) {
1373eda14cbcSMatt Macy case GET_COL_NAME:
1374eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "NAME");
1375eda14cbcSMatt Macy break;
1376eda14cbcSMatt Macy case GET_COL_PROPERTY:
1377eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "PROPERTY");
1378eda14cbcSMatt Macy break;
1379eda14cbcSMatt Macy case GET_COL_VALUE:
1380eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "VALUE");
1381eda14cbcSMatt Macy break;
1382eda14cbcSMatt Macy case GET_COL_RECVD:
1383eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "RECEIVED");
1384eda14cbcSMatt Macy break;
1385eda14cbcSMatt Macy case GET_COL_SOURCE:
1386eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "SOURCE");
1387eda14cbcSMatt Macy break;
1388eda14cbcSMatt Macy default:
1389eda14cbcSMatt Macy title = NULL;
1390eda14cbcSMatt Macy }
1391eda14cbcSMatt Macy
1392eda14cbcSMatt Macy if (title != NULL) {
1393eda14cbcSMatt Macy if (i == (ZFS_GET_NCOLS - 1) ||
1394eda14cbcSMatt Macy cbp->cb_columns[i + 1] == GET_COL_NONE)
1395eda14cbcSMatt Macy (void) printf("%s", title);
1396eda14cbcSMatt Macy else
1397eda14cbcSMatt Macy (void) printf("%-*s ",
1398eda14cbcSMatt Macy cbp->cb_colwidths[cbp->cb_columns[i]],
1399eda14cbcSMatt Macy title);
1400eda14cbcSMatt Macy }
1401eda14cbcSMatt Macy }
1402eda14cbcSMatt Macy (void) printf("\n");
1403eda14cbcSMatt Macy }
1404eda14cbcSMatt Macy
1405eda14cbcSMatt Macy /*
1406ce4dcb97SMartin Matuska * Add property value and source to provided nvlist, according to
1407ce4dcb97SMartin Matuska * settings in cb structure. Later to be printed in JSON format.
1408ce4dcb97SMartin Matuska */
1409ce4dcb97SMartin Matuska int
zprop_nvlist_one_property(const char * propname,const char * value,zprop_source_t sourcetype,const char * source,const char * recvd_value,nvlist_t * nvl,boolean_t as_int)1410ce4dcb97SMartin Matuska zprop_nvlist_one_property(const char *propname,
1411ce4dcb97SMartin Matuska const char *value, zprop_source_t sourcetype, const char *source,
1412ce4dcb97SMartin Matuska const char *recvd_value, nvlist_t *nvl, boolean_t as_int)
1413ce4dcb97SMartin Matuska {
1414ce4dcb97SMartin Matuska int ret = 0;
1415ce4dcb97SMartin Matuska nvlist_t *src_nv, *prop;
1416ce4dcb97SMartin Matuska boolean_t all_numeric = strspn(value, STR_NUMS) == strlen(value);
1417ce4dcb97SMartin Matuska src_nv = prop = NULL;
1418ce4dcb97SMartin Matuska
1419ce4dcb97SMartin Matuska if ((nvlist_alloc(&prop, NV_UNIQUE_NAME, 0) != 0) ||
1420ce4dcb97SMartin Matuska (nvlist_alloc(&src_nv, NV_UNIQUE_NAME, 0) != 0)) {
1421ce4dcb97SMartin Matuska ret = -1;
1422ce4dcb97SMartin Matuska goto err;
1423ce4dcb97SMartin Matuska }
1424ce4dcb97SMartin Matuska
1425ce4dcb97SMartin Matuska if (as_int && all_numeric) {
1426ce4dcb97SMartin Matuska uint64_t val;
1427ce4dcb97SMartin Matuska sscanf(value, "%lld", (u_longlong_t *)&val);
1428ce4dcb97SMartin Matuska if (nvlist_add_uint64(prop, "value", val) != 0) {
1429ce4dcb97SMartin Matuska ret = -1;
1430ce4dcb97SMartin Matuska goto err;
1431ce4dcb97SMartin Matuska }
1432ce4dcb97SMartin Matuska } else {
1433ce4dcb97SMartin Matuska if (nvlist_add_string(prop, "value", value) != 0) {
1434ce4dcb97SMartin Matuska ret = -1;
1435ce4dcb97SMartin Matuska goto err;
1436ce4dcb97SMartin Matuska }
1437ce4dcb97SMartin Matuska }
1438ce4dcb97SMartin Matuska
1439ce4dcb97SMartin Matuska switch (sourcetype) {
1440ce4dcb97SMartin Matuska case ZPROP_SRC_NONE:
1441ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type", "NONE") != 0 ||
1442ce4dcb97SMartin Matuska (nvlist_add_string(src_nv, "data", "-") != 0)) {
1443ce4dcb97SMartin Matuska ret = -1;
1444ce4dcb97SMartin Matuska goto err;
1445ce4dcb97SMartin Matuska }
1446ce4dcb97SMartin Matuska break;
1447ce4dcb97SMartin Matuska case ZPROP_SRC_DEFAULT:
1448ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type", "DEFAULT") != 0 ||
1449ce4dcb97SMartin Matuska (nvlist_add_string(src_nv, "data", "-") != 0)) {
1450ce4dcb97SMartin Matuska ret = -1;
1451ce4dcb97SMartin Matuska goto err;
1452ce4dcb97SMartin Matuska }
1453ce4dcb97SMartin Matuska break;
1454ce4dcb97SMartin Matuska case ZPROP_SRC_LOCAL:
1455ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type", "LOCAL") != 0 ||
1456ce4dcb97SMartin Matuska (nvlist_add_string(src_nv, "data", "-") != 0)) {
1457ce4dcb97SMartin Matuska ret = -1;
1458ce4dcb97SMartin Matuska goto err;
1459ce4dcb97SMartin Matuska }
1460ce4dcb97SMartin Matuska break;
1461ce4dcb97SMartin Matuska case ZPROP_SRC_TEMPORARY:
1462ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type", "TEMPORARY") != 0 ||
1463ce4dcb97SMartin Matuska (nvlist_add_string(src_nv, "data", "-") != 0)) {
1464ce4dcb97SMartin Matuska ret = -1;
1465ce4dcb97SMartin Matuska goto err;
1466ce4dcb97SMartin Matuska }
1467ce4dcb97SMartin Matuska break;
1468ce4dcb97SMartin Matuska case ZPROP_SRC_INHERITED:
1469ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type", "INHERITED") != 0 ||
1470ce4dcb97SMartin Matuska (nvlist_add_string(src_nv, "data", source) != 0)) {
1471ce4dcb97SMartin Matuska ret = -1;
1472ce4dcb97SMartin Matuska goto err;
1473ce4dcb97SMartin Matuska }
1474ce4dcb97SMartin Matuska break;
1475ce4dcb97SMartin Matuska case ZPROP_SRC_RECEIVED:
1476ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type", "RECEIVED") != 0 ||
1477ce4dcb97SMartin Matuska (nvlist_add_string(src_nv, "data",
1478ce4dcb97SMartin Matuska (recvd_value == NULL ? "-" : recvd_value)) != 0)) {
1479ce4dcb97SMartin Matuska ret = -1;
1480ce4dcb97SMartin Matuska goto err;
1481ce4dcb97SMartin Matuska }
1482ce4dcb97SMartin Matuska break;
1483ce4dcb97SMartin Matuska default:
1484ce4dcb97SMartin Matuska assert(!"unhandled zprop_source_t");
1485ce4dcb97SMartin Matuska if (nvlist_add_string(src_nv, "type",
1486ce4dcb97SMartin Matuska "unhandled zprop_source_t") != 0) {
1487ce4dcb97SMartin Matuska ret = -1;
1488ce4dcb97SMartin Matuska goto err;
1489ce4dcb97SMartin Matuska }
1490ce4dcb97SMartin Matuska }
1491ce4dcb97SMartin Matuska if ((nvlist_add_nvlist(prop, "source", src_nv) != 0) ||
1492ce4dcb97SMartin Matuska (nvlist_add_nvlist(nvl, propname, prop)) != 0) {
1493ce4dcb97SMartin Matuska ret = -1;
1494ce4dcb97SMartin Matuska goto err;
1495ce4dcb97SMartin Matuska }
1496ce4dcb97SMartin Matuska err:
1497ce4dcb97SMartin Matuska nvlist_free(src_nv);
1498ce4dcb97SMartin Matuska nvlist_free(prop);
1499ce4dcb97SMartin Matuska return (ret);
1500ce4dcb97SMartin Matuska }
1501ce4dcb97SMartin Matuska
1502ce4dcb97SMartin Matuska /*
1503eda14cbcSMatt Macy * Display a single line of output, according to the settings in the callback
1504eda14cbcSMatt Macy * structure.
1505eda14cbcSMatt Macy */
1506eda14cbcSMatt Macy void
zprop_print_one_property(const char * name,zprop_get_cbdata_t * cbp,const char * propname,const char * value,zprop_source_t sourcetype,const char * source,const char * recvd_value)1507eda14cbcSMatt Macy zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1508eda14cbcSMatt Macy const char *propname, const char *value, zprop_source_t sourcetype,
1509eda14cbcSMatt Macy const char *source, const char *recvd_value)
1510eda14cbcSMatt Macy {
1511eda14cbcSMatt Macy int i;
1512eda14cbcSMatt Macy const char *str = NULL;
1513eda14cbcSMatt Macy char buf[128];
1514eda14cbcSMatt Macy
1515eda14cbcSMatt Macy /*
1516eda14cbcSMatt Macy * Ignore those source types that the user has chosen to ignore.
1517eda14cbcSMatt Macy */
1518eda14cbcSMatt Macy if ((sourcetype & cbp->cb_sources) == 0)
1519eda14cbcSMatt Macy return;
1520eda14cbcSMatt Macy
1521eda14cbcSMatt Macy if (cbp->cb_first)
1522eda14cbcSMatt Macy zprop_print_headers(cbp, cbp->cb_type);
1523eda14cbcSMatt Macy
1524eda14cbcSMatt Macy for (i = 0; i < ZFS_GET_NCOLS; i++) {
1525eda14cbcSMatt Macy switch (cbp->cb_columns[i]) {
1526eda14cbcSMatt Macy case GET_COL_NAME:
1527eda14cbcSMatt Macy str = name;
1528eda14cbcSMatt Macy break;
1529eda14cbcSMatt Macy
1530eda14cbcSMatt Macy case GET_COL_PROPERTY:
1531eda14cbcSMatt Macy str = propname;
1532eda14cbcSMatt Macy break;
1533eda14cbcSMatt Macy
1534eda14cbcSMatt Macy case GET_COL_VALUE:
1535eda14cbcSMatt Macy str = value;
1536eda14cbcSMatt Macy break;
1537eda14cbcSMatt Macy
1538eda14cbcSMatt Macy case GET_COL_SOURCE:
1539eda14cbcSMatt Macy switch (sourcetype) {
1540eda14cbcSMatt Macy case ZPROP_SRC_NONE:
1541eda14cbcSMatt Macy str = "-";
1542eda14cbcSMatt Macy break;
1543eda14cbcSMatt Macy
1544eda14cbcSMatt Macy case ZPROP_SRC_DEFAULT:
1545eda14cbcSMatt Macy str = "default";
1546eda14cbcSMatt Macy break;
1547eda14cbcSMatt Macy
1548eda14cbcSMatt Macy case ZPROP_SRC_LOCAL:
1549eda14cbcSMatt Macy str = "local";
1550eda14cbcSMatt Macy break;
1551eda14cbcSMatt Macy
1552eda14cbcSMatt Macy case ZPROP_SRC_TEMPORARY:
1553eda14cbcSMatt Macy str = "temporary";
1554eda14cbcSMatt Macy break;
1555eda14cbcSMatt Macy
1556eda14cbcSMatt Macy case ZPROP_SRC_INHERITED:
1557eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf),
1558eda14cbcSMatt Macy "inherited from %s", source);
1559eda14cbcSMatt Macy str = buf;
1560eda14cbcSMatt Macy break;
1561eda14cbcSMatt Macy case ZPROP_SRC_RECEIVED:
1562eda14cbcSMatt Macy str = "received";
1563eda14cbcSMatt Macy break;
1564eda14cbcSMatt Macy
1565eda14cbcSMatt Macy default:
1566eda14cbcSMatt Macy str = NULL;
1567eda14cbcSMatt Macy assert(!"unhandled zprop_source_t");
1568eda14cbcSMatt Macy }
1569eda14cbcSMatt Macy break;
1570eda14cbcSMatt Macy
1571eda14cbcSMatt Macy case GET_COL_RECVD:
1572eda14cbcSMatt Macy str = (recvd_value == NULL ? "-" : recvd_value);
1573eda14cbcSMatt Macy break;
1574eda14cbcSMatt Macy
1575eda14cbcSMatt Macy default:
1576eda14cbcSMatt Macy continue;
1577eda14cbcSMatt Macy }
1578eda14cbcSMatt Macy
1579eda14cbcSMatt Macy if (i == (ZFS_GET_NCOLS - 1) ||
1580eda14cbcSMatt Macy cbp->cb_columns[i + 1] == GET_COL_NONE)
1581eda14cbcSMatt Macy (void) printf("%s", str);
1582eda14cbcSMatt Macy else if (cbp->cb_scripted)
1583eda14cbcSMatt Macy (void) printf("%s\t", str);
1584eda14cbcSMatt Macy else
1585eda14cbcSMatt Macy (void) printf("%-*s ",
1586eda14cbcSMatt Macy cbp->cb_colwidths[cbp->cb_columns[i]],
1587eda14cbcSMatt Macy str);
1588eda14cbcSMatt Macy }
1589eda14cbcSMatt Macy
1590eda14cbcSMatt Macy (void) printf("\n");
1591eda14cbcSMatt Macy }
1592eda14cbcSMatt Macy
1593ce4dcb97SMartin Matuska int
zprop_collect_property(const char * name,zprop_get_cbdata_t * cbp,const char * propname,const char * value,zprop_source_t sourcetype,const char * source,const char * recvd_value,nvlist_t * nvl)1594ce4dcb97SMartin Matuska zprop_collect_property(const char *name, zprop_get_cbdata_t *cbp,
1595ce4dcb97SMartin Matuska const char *propname, const char *value, zprop_source_t sourcetype,
1596ce4dcb97SMartin Matuska const char *source, const char *recvd_value, nvlist_t *nvl)
1597ce4dcb97SMartin Matuska {
1598ce4dcb97SMartin Matuska if (cbp->cb_json) {
1599ce4dcb97SMartin Matuska if ((sourcetype & cbp->cb_sources) == 0)
1600ce4dcb97SMartin Matuska return (0);
1601ce4dcb97SMartin Matuska else {
1602ce4dcb97SMartin Matuska return (zprop_nvlist_one_property(propname, value,
1603ce4dcb97SMartin Matuska sourcetype, source, recvd_value, nvl,
1604ce4dcb97SMartin Matuska cbp->cb_json_as_int));
1605ce4dcb97SMartin Matuska }
1606ce4dcb97SMartin Matuska } else {
1607ce4dcb97SMartin Matuska zprop_print_one_property(name, cbp,
1608ce4dcb97SMartin Matuska propname, value, sourcetype, source, recvd_value);
1609ce4dcb97SMartin Matuska return (0);
1610ce4dcb97SMartin Matuska }
1611ce4dcb97SMartin Matuska }
1612ce4dcb97SMartin Matuska
1613eda14cbcSMatt Macy /*
1614eda14cbcSMatt Macy * Given a numeric suffix, convert the value into a number of bits that the
1615eda14cbcSMatt Macy * resulting value must be shifted.
1616eda14cbcSMatt Macy */
1617eda14cbcSMatt Macy static int
str2shift(libzfs_handle_t * hdl,const char * buf)1618eda14cbcSMatt Macy str2shift(libzfs_handle_t *hdl, const char *buf)
1619eda14cbcSMatt Macy {
1620eda14cbcSMatt Macy const char *ends = "BKMGTPEZ";
1621*7a7741afSMartin Matuska int i, len;
1622eda14cbcSMatt Macy
1623eda14cbcSMatt Macy if (buf[0] == '\0')
1624eda14cbcSMatt Macy return (0);
1625*7a7741afSMartin Matuska
1626*7a7741afSMartin Matuska len = strlen(ends);
1627*7a7741afSMartin Matuska for (i = 0; i < len; i++) {
1628eda14cbcSMatt Macy if (toupper(buf[0]) == ends[i])
1629eda14cbcSMatt Macy break;
1630eda14cbcSMatt Macy }
1631*7a7741afSMartin Matuska if (i == len) {
1632eda14cbcSMatt Macy if (hdl)
1633eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1634eda14cbcSMatt Macy "invalid numeric suffix '%s'"), buf);
1635eda14cbcSMatt Macy return (-1);
1636eda14cbcSMatt Macy }
1637eda14cbcSMatt Macy
1638eda14cbcSMatt Macy /*
1639eda14cbcSMatt Macy * Allow 'G' = 'GB' = 'GiB', case-insensitively.
1640eda14cbcSMatt Macy * However, 'BB' and 'BiB' are disallowed.
1641eda14cbcSMatt Macy */
1642eda14cbcSMatt Macy if (buf[1] == '\0' ||
1643eda14cbcSMatt Macy (toupper(buf[0]) != 'B' &&
1644eda14cbcSMatt Macy ((toupper(buf[1]) == 'B' && buf[2] == '\0') ||
1645eda14cbcSMatt Macy (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' &&
1646eda14cbcSMatt Macy buf[3] == '\0'))))
1647eda14cbcSMatt Macy return (10 * i);
1648eda14cbcSMatt Macy
1649eda14cbcSMatt Macy if (hdl)
1650eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1651eda14cbcSMatt Macy "invalid numeric suffix '%s'"), buf);
1652eda14cbcSMatt Macy return (-1);
1653eda14cbcSMatt Macy }
1654eda14cbcSMatt Macy
1655eda14cbcSMatt Macy /*
1656eda14cbcSMatt Macy * Convert a string of the form '100G' into a real number. Used when setting
1657eda14cbcSMatt Macy * properties or creating a volume. 'buf' is used to place an extended error
1658eda14cbcSMatt Macy * message for the caller to use.
1659eda14cbcSMatt Macy */
1660eda14cbcSMatt Macy int
zfs_nicestrtonum(libzfs_handle_t * hdl,const char * value,uint64_t * num)1661eda14cbcSMatt Macy zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1662eda14cbcSMatt Macy {
1663eda14cbcSMatt Macy char *end;
1664eda14cbcSMatt Macy int shift;
1665eda14cbcSMatt Macy
1666eda14cbcSMatt Macy *num = 0;
1667eda14cbcSMatt Macy
1668eda14cbcSMatt Macy /* Check to see if this looks like a number. */
1669eda14cbcSMatt Macy if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1670eda14cbcSMatt Macy if (hdl)
1671eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1672eda14cbcSMatt Macy "bad numeric value '%s'"), value);
1673eda14cbcSMatt Macy return (-1);
1674eda14cbcSMatt Macy }
1675eda14cbcSMatt Macy
1676eda14cbcSMatt Macy /* Rely on strtoull() to process the numeric portion. */
1677eda14cbcSMatt Macy errno = 0;
1678eda14cbcSMatt Macy *num = strtoull(value, &end, 10);
1679eda14cbcSMatt Macy
1680eda14cbcSMatt Macy /*
1681eda14cbcSMatt Macy * Check for ERANGE, which indicates that the value is too large to fit
1682eda14cbcSMatt Macy * in a 64-bit value.
1683eda14cbcSMatt Macy */
1684eda14cbcSMatt Macy if (errno == ERANGE) {
1685eda14cbcSMatt Macy if (hdl)
1686eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1687eda14cbcSMatt Macy "numeric value is too large"));
1688eda14cbcSMatt Macy return (-1);
1689eda14cbcSMatt Macy }
1690eda14cbcSMatt Macy
1691eda14cbcSMatt Macy /*
1692eda14cbcSMatt Macy * If we have a decimal value, then do the computation with floating
1693eda14cbcSMatt Macy * point arithmetic. Otherwise, use standard arithmetic.
1694eda14cbcSMatt Macy */
1695eda14cbcSMatt Macy if (*end == '.') {
1696eda14cbcSMatt Macy double fval = strtod(value, &end);
1697eda14cbcSMatt Macy
1698eda14cbcSMatt Macy if ((shift = str2shift(hdl, end)) == -1)
1699eda14cbcSMatt Macy return (-1);
1700eda14cbcSMatt Macy
1701eda14cbcSMatt Macy fval *= pow(2, shift);
1702eda14cbcSMatt Macy
1703eda14cbcSMatt Macy /*
1704eda14cbcSMatt Macy * UINT64_MAX is not exactly representable as a double.
1705eda14cbcSMatt Macy * The closest representation is UINT64_MAX + 1, so we
1706eda14cbcSMatt Macy * use a >= comparison instead of > for the bounds check.
1707eda14cbcSMatt Macy */
1708eda14cbcSMatt Macy if (fval >= (double)UINT64_MAX) {
1709eda14cbcSMatt Macy if (hdl)
1710eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1711eda14cbcSMatt Macy "numeric value is too large"));
1712eda14cbcSMatt Macy return (-1);
1713eda14cbcSMatt Macy }
1714eda14cbcSMatt Macy
1715eda14cbcSMatt Macy *num = (uint64_t)fval;
1716eda14cbcSMatt Macy } else {
1717eda14cbcSMatt Macy if ((shift = str2shift(hdl, end)) == -1)
1718eda14cbcSMatt Macy return (-1);
1719eda14cbcSMatt Macy
1720eda14cbcSMatt Macy /* Check for overflow */
1721eda14cbcSMatt Macy if (shift >= 64 || (*num << shift) >> shift != *num) {
1722eda14cbcSMatt Macy if (hdl)
1723eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1724eda14cbcSMatt Macy "numeric value is too large"));
1725eda14cbcSMatt Macy return (-1);
1726eda14cbcSMatt Macy }
1727eda14cbcSMatt Macy
1728eda14cbcSMatt Macy *num <<= shift;
1729eda14cbcSMatt Macy }
1730eda14cbcSMatt Macy
1731eda14cbcSMatt Macy return (0);
1732eda14cbcSMatt Macy }
1733eda14cbcSMatt Macy
1734eda14cbcSMatt Macy /*
1735eda14cbcSMatt Macy * Given a propname=value nvpair to set, parse any numeric properties
1736eda14cbcSMatt Macy * (index, boolean, etc) if they are specified as strings and add the
1737eda14cbcSMatt Macy * resulting nvpair to the returned nvlist.
1738eda14cbcSMatt Macy *
1739eda14cbcSMatt Macy * At the DSL layer, all properties are either 64-bit numbers or strings.
1740eda14cbcSMatt Macy * We want the user to be able to ignore this fact and specify properties
1741eda14cbcSMatt Macy * as native values (numbers, for example) or as strings (to simplify
1742eda14cbcSMatt Macy * command line utilities). This also handles converting index types
1743eda14cbcSMatt Macy * (compression, checksum, etc) from strings to their on-disk index.
1744eda14cbcSMatt Macy */
1745eda14cbcSMatt Macy int
zprop_parse_value(libzfs_handle_t * hdl,nvpair_t * elem,int prop,zfs_type_t type,nvlist_t * ret,const char ** svalp,uint64_t * ivalp,const char * errbuf)1746eda14cbcSMatt Macy zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
17472a58b312SMartin Matuska zfs_type_t type, nvlist_t *ret, const char **svalp, uint64_t *ivalp,
1748eda14cbcSMatt Macy const char *errbuf)
1749eda14cbcSMatt Macy {
1750eda14cbcSMatt Macy data_type_t datatype = nvpair_type(elem);
1751eda14cbcSMatt Macy zprop_type_t proptype;
1752eda14cbcSMatt Macy const char *propname;
17532a58b312SMartin Matuska const char *value;
1754eda14cbcSMatt Macy boolean_t isnone = B_FALSE;
1755eda14cbcSMatt Macy boolean_t isauto = B_FALSE;
1756eda14cbcSMatt Macy int err = 0;
1757eda14cbcSMatt Macy
1758eda14cbcSMatt Macy if (type == ZFS_TYPE_POOL) {
1759eda14cbcSMatt Macy proptype = zpool_prop_get_type(prop);
1760eda14cbcSMatt Macy propname = zpool_prop_to_name(prop);
1761681ce946SMartin Matuska } else if (type == ZFS_TYPE_VDEV) {
1762681ce946SMartin Matuska proptype = vdev_prop_get_type(prop);
1763681ce946SMartin Matuska propname = vdev_prop_to_name(prop);
1764eda14cbcSMatt Macy } else {
1765eda14cbcSMatt Macy proptype = zfs_prop_get_type(prop);
1766eda14cbcSMatt Macy propname = zfs_prop_to_name(prop);
1767eda14cbcSMatt Macy }
1768eda14cbcSMatt Macy
1769eda14cbcSMatt Macy /*
1770eda14cbcSMatt Macy * Convert any properties to the internal DSL value types.
1771eda14cbcSMatt Macy */
1772eda14cbcSMatt Macy *svalp = NULL;
1773eda14cbcSMatt Macy *ivalp = 0;
1774eda14cbcSMatt Macy
1775eda14cbcSMatt Macy switch (proptype) {
1776eda14cbcSMatt Macy case PROP_TYPE_STRING:
1777eda14cbcSMatt Macy if (datatype != DATA_TYPE_STRING) {
1778eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1779eda14cbcSMatt Macy "'%s' must be a string"), nvpair_name(elem));
1780eda14cbcSMatt Macy goto error;
1781eda14cbcSMatt Macy }
1782eda14cbcSMatt Macy err = nvpair_value_string(elem, svalp);
1783eda14cbcSMatt Macy if (err != 0) {
1784eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1785eda14cbcSMatt Macy "'%s' is invalid"), nvpair_name(elem));
1786eda14cbcSMatt Macy goto error;
1787eda14cbcSMatt Macy }
1788eda14cbcSMatt Macy if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1789eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1790eda14cbcSMatt Macy "'%s' is too long"), nvpair_name(elem));
1791eda14cbcSMatt Macy goto error;
1792eda14cbcSMatt Macy }
1793eda14cbcSMatt Macy break;
1794eda14cbcSMatt Macy
1795eda14cbcSMatt Macy case PROP_TYPE_NUMBER:
1796eda14cbcSMatt Macy if (datatype == DATA_TYPE_STRING) {
1797eda14cbcSMatt Macy (void) nvpair_value_string(elem, &value);
1798eda14cbcSMatt Macy if (strcmp(value, "none") == 0) {
1799eda14cbcSMatt Macy isnone = B_TRUE;
1800eda14cbcSMatt Macy } else if (strcmp(value, "auto") == 0) {
1801eda14cbcSMatt Macy isauto = B_TRUE;
1802eda14cbcSMatt Macy } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1803eda14cbcSMatt Macy goto error;
1804eda14cbcSMatt Macy }
1805eda14cbcSMatt Macy } else if (datatype == DATA_TYPE_UINT64) {
1806eda14cbcSMatt Macy (void) nvpair_value_uint64(elem, ivalp);
1807eda14cbcSMatt Macy } else {
1808eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1809eda14cbcSMatt Macy "'%s' must be a number"), nvpair_name(elem));
1810eda14cbcSMatt Macy goto error;
1811eda14cbcSMatt Macy }
1812eda14cbcSMatt Macy
1813eda14cbcSMatt Macy /*
1814eda14cbcSMatt Macy * Quota special: force 'none' and don't allow 0.
1815eda14cbcSMatt Macy */
1816eda14cbcSMatt Macy if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1817eda14cbcSMatt Macy (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1818eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1819eda14cbcSMatt Macy "use 'none' to disable quota/refquota"));
1820eda14cbcSMatt Macy goto error;
1821eda14cbcSMatt Macy }
1822ce4dcb97SMartin Matuska /*
1823ce4dcb97SMartin Matuska * Pool dedup table quota; force use of 'none' instead of 0
1824ce4dcb97SMartin Matuska */
1825ce4dcb97SMartin Matuska if ((type & ZFS_TYPE_POOL) && *ivalp == 0 &&
1826ce4dcb97SMartin Matuska (!isnone && !isauto) &&
1827ce4dcb97SMartin Matuska prop == ZPOOL_PROP_DEDUP_TABLE_QUOTA) {
1828ce4dcb97SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1829ce4dcb97SMartin Matuska "use 'none' to disable ddt table quota"));
1830ce4dcb97SMartin Matuska goto error;
1831ce4dcb97SMartin Matuska }
1832eda14cbcSMatt Macy
1833eda14cbcSMatt Macy /*
1834eda14cbcSMatt Macy * Special handling for "*_limit=none". In this case it's not
1835eda14cbcSMatt Macy * 0 but UINT64_MAX.
1836eda14cbcSMatt Macy */
1837eda14cbcSMatt Macy if ((type & ZFS_TYPE_DATASET) && isnone &&
1838eda14cbcSMatt Macy (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1839eda14cbcSMatt Macy prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1840eda14cbcSMatt Macy *ivalp = UINT64_MAX;
1841eda14cbcSMatt Macy }
1842eda14cbcSMatt Macy
1843eda14cbcSMatt Macy /*
184415f0b8c3SMartin Matuska * Special handling for "checksum_*=none". In this case it's not
184515f0b8c3SMartin Matuska * 0 but UINT64_MAX.
184615f0b8c3SMartin Matuska */
184715f0b8c3SMartin Matuska if ((type & ZFS_TYPE_VDEV) && isnone &&
184815f0b8c3SMartin Matuska (prop == VDEV_PROP_CHECKSUM_N ||
184915f0b8c3SMartin Matuska prop == VDEV_PROP_CHECKSUM_T ||
185015f0b8c3SMartin Matuska prop == VDEV_PROP_IO_N ||
1851e2257b31SMartin Matuska prop == VDEV_PROP_IO_T ||
1852e2257b31SMartin Matuska prop == VDEV_PROP_SLOW_IO_N ||
1853e2257b31SMartin Matuska prop == VDEV_PROP_SLOW_IO_T)) {
185415f0b8c3SMartin Matuska *ivalp = UINT64_MAX;
185515f0b8c3SMartin Matuska }
185615f0b8c3SMartin Matuska
185715f0b8c3SMartin Matuska /*
1858eda14cbcSMatt Macy * Special handling for setting 'refreservation' to 'auto'. Use
1859eda14cbcSMatt Macy * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1860eda14cbcSMatt Macy * 'auto' is only allowed on volumes.
1861eda14cbcSMatt Macy */
1862eda14cbcSMatt Macy if (isauto) {
1863eda14cbcSMatt Macy switch (prop) {
1864eda14cbcSMatt Macy case ZFS_PROP_REFRESERVATION:
1865eda14cbcSMatt Macy if ((type & ZFS_TYPE_VOLUME) == 0) {
1866eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1867eda14cbcSMatt Macy "'%s=auto' only allowed on "
1868eda14cbcSMatt Macy "volumes"), nvpair_name(elem));
1869eda14cbcSMatt Macy goto error;
1870eda14cbcSMatt Macy }
1871eda14cbcSMatt Macy *ivalp = UINT64_MAX;
1872eda14cbcSMatt Macy break;
1873ce4dcb97SMartin Matuska case ZPOOL_PROP_DEDUP_TABLE_QUOTA:
1874ce4dcb97SMartin Matuska ASSERT(type & ZFS_TYPE_POOL);
1875ce4dcb97SMartin Matuska *ivalp = UINT64_MAX;
1876ce4dcb97SMartin Matuska break;
1877eda14cbcSMatt Macy default:
1878eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1879eda14cbcSMatt Macy "'auto' is invalid value for '%s'"),
1880eda14cbcSMatt Macy nvpair_name(elem));
1881eda14cbcSMatt Macy goto error;
1882eda14cbcSMatt Macy }
1883eda14cbcSMatt Macy }
1884eda14cbcSMatt Macy
1885eda14cbcSMatt Macy break;
1886eda14cbcSMatt Macy
1887eda14cbcSMatt Macy case PROP_TYPE_INDEX:
1888eda14cbcSMatt Macy if (datatype != DATA_TYPE_STRING) {
1889eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1890eda14cbcSMatt Macy "'%s' must be a string"), nvpair_name(elem));
1891eda14cbcSMatt Macy goto error;
1892eda14cbcSMatt Macy }
1893eda14cbcSMatt Macy
1894eda14cbcSMatt Macy (void) nvpair_value_string(elem, &value);
1895eda14cbcSMatt Macy
1896eda14cbcSMatt Macy if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1897eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1898eda14cbcSMatt Macy "'%s' must be one of '%s'"), propname,
1899eda14cbcSMatt Macy zprop_values(prop, type));
1900eda14cbcSMatt Macy goto error;
1901eda14cbcSMatt Macy }
1902eda14cbcSMatt Macy break;
1903eda14cbcSMatt Macy
1904eda14cbcSMatt Macy default:
1905eda14cbcSMatt Macy abort();
1906eda14cbcSMatt Macy }
1907eda14cbcSMatt Macy
1908eda14cbcSMatt Macy /*
1909eda14cbcSMatt Macy * Add the result to our return set of properties.
1910eda14cbcSMatt Macy */
1911eda14cbcSMatt Macy if (*svalp != NULL) {
1912eda14cbcSMatt Macy if (nvlist_add_string(ret, propname, *svalp) != 0) {
1913eda14cbcSMatt Macy (void) no_memory(hdl);
1914eda14cbcSMatt Macy return (-1);
1915eda14cbcSMatt Macy }
1916eda14cbcSMatt Macy } else {
1917eda14cbcSMatt Macy if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1918eda14cbcSMatt Macy (void) no_memory(hdl);
1919eda14cbcSMatt Macy return (-1);
1920eda14cbcSMatt Macy }
1921eda14cbcSMatt Macy }
1922eda14cbcSMatt Macy
1923eda14cbcSMatt Macy return (0);
1924eda14cbcSMatt Macy error:
1925eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1926eda14cbcSMatt Macy return (-1);
1927eda14cbcSMatt Macy }
1928eda14cbcSMatt Macy
1929eda14cbcSMatt Macy static int
addlist(libzfs_handle_t * hdl,const char * propname,zprop_list_t ** listp,zfs_type_t type)1930da5137abSMartin Matuska addlist(libzfs_handle_t *hdl, const char *propname, zprop_list_t **listp,
1931eda14cbcSMatt Macy zfs_type_t type)
1932eda14cbcSMatt Macy {
1933da5137abSMartin Matuska int prop = zprop_name_to_prop(propname, type);
1934eda14cbcSMatt Macy if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
1935eda14cbcSMatt Macy prop = ZPROP_INVAL;
1936eda14cbcSMatt Macy
1937eda14cbcSMatt Macy /*
1938681ce946SMartin Matuska * Return failure if no property table entry was found and this isn't
1939681ce946SMartin Matuska * a user-defined property.
1940eda14cbcSMatt Macy */
19411f1e2261SMartin Matuska if (prop == ZPROP_USERPROP && ((type == ZFS_TYPE_POOL &&
1942c98ecfceSAllan Jude !zfs_prop_user(propname) &&
1943eda14cbcSMatt Macy !zpool_prop_feature(propname) &&
1944eda14cbcSMatt Macy !zpool_prop_unsupported(propname)) ||
1945681ce946SMartin Matuska ((type == ZFS_TYPE_DATASET) && !zfs_prop_user(propname) &&
1946681ce946SMartin Matuska !zfs_prop_userquota(propname) && !zfs_prop_written(propname)) ||
1947681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) && !vdev_prop_user(propname)))) {
1948eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1949eda14cbcSMatt Macy "invalid property '%s'"), propname);
1950eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP,
1951eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "bad property list")));
1952eda14cbcSMatt Macy }
1953eda14cbcSMatt Macy
1954da5137abSMartin Matuska zprop_list_t *entry = zfs_alloc(hdl, sizeof (*entry));
1955da5137abSMartin Matuska
1956eda14cbcSMatt Macy entry->pl_prop = prop;
19571f1e2261SMartin Matuska if (prop == ZPROP_USERPROP) {
1958c03c5b1cSMartin Matuska entry->pl_user_prop = zfs_strdup(hdl, propname);
1959eda14cbcSMatt Macy entry->pl_width = strlen(propname);
1960eda14cbcSMatt Macy } else {
1961eda14cbcSMatt Macy entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1962eda14cbcSMatt Macy type);
1963eda14cbcSMatt Macy }
1964eda14cbcSMatt Macy
1965eda14cbcSMatt Macy *listp = entry;
1966eda14cbcSMatt Macy
1967eda14cbcSMatt Macy return (0);
1968eda14cbcSMatt Macy }
1969eda14cbcSMatt Macy
1970eda14cbcSMatt Macy /*
1971eda14cbcSMatt Macy * Given a comma-separated list of properties, construct a property list
1972eda14cbcSMatt Macy * containing both user-defined and native properties. This function will
1973eda14cbcSMatt Macy * return a NULL list if 'all' is specified, which can later be expanded
1974eda14cbcSMatt Macy * by zprop_expand_list().
1975eda14cbcSMatt Macy */
1976eda14cbcSMatt Macy int
zprop_get_list(libzfs_handle_t * hdl,char * props,zprop_list_t ** listp,zfs_type_t type)1977eda14cbcSMatt Macy zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1978eda14cbcSMatt Macy zfs_type_t type)
1979eda14cbcSMatt Macy {
1980eda14cbcSMatt Macy *listp = NULL;
1981eda14cbcSMatt Macy
1982eda14cbcSMatt Macy /*
1983eda14cbcSMatt Macy * If 'all' is specified, return a NULL list.
1984eda14cbcSMatt Macy */
1985eda14cbcSMatt Macy if (strcmp(props, "all") == 0)
1986eda14cbcSMatt Macy return (0);
1987eda14cbcSMatt Macy
1988eda14cbcSMatt Macy /*
1989eda14cbcSMatt Macy * If no props were specified, return an error.
1990eda14cbcSMatt Macy */
1991eda14cbcSMatt Macy if (props[0] == '\0') {
1992eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1993eda14cbcSMatt Macy "no properties specified"));
1994eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1995eda14cbcSMatt Macy "bad property list")));
1996eda14cbcSMatt Macy }
1997eda14cbcSMatt Macy
1998da5137abSMartin Matuska for (char *p; (p = strsep(&props, ",")); )
1999da5137abSMartin Matuska if (strcmp(p, "space") == 0) {
2000da5137abSMartin Matuska static const char *const spaceprops[] = {
2001eda14cbcSMatt Macy "name", "avail", "used", "usedbysnapshots",
2002eda14cbcSMatt Macy "usedbydataset", "usedbyrefreservation",
2003da5137abSMartin Matuska "usedbychildren"
2004eda14cbcSMatt Macy };
2005eda14cbcSMatt Macy
2006da5137abSMartin Matuska for (int i = 0; i < ARRAY_SIZE(spaceprops); i++) {
2007eda14cbcSMatt Macy if (addlist(hdl, spaceprops[i], listp, type))
2008eda14cbcSMatt Macy return (-1);
2009eda14cbcSMatt Macy listp = &(*listp)->pl_next;
2010eda14cbcSMatt Macy }
2011eda14cbcSMatt Macy } else {
2012da5137abSMartin Matuska if (addlist(hdl, p, listp, type))
2013eda14cbcSMatt Macy return (-1);
2014eda14cbcSMatt Macy listp = &(*listp)->pl_next;
2015eda14cbcSMatt Macy }
2016eda14cbcSMatt Macy
2017eda14cbcSMatt Macy return (0);
2018eda14cbcSMatt Macy }
2019eda14cbcSMatt Macy
2020eda14cbcSMatt Macy void
zprop_free_list(zprop_list_t * pl)2021eda14cbcSMatt Macy zprop_free_list(zprop_list_t *pl)
2022eda14cbcSMatt Macy {
2023eda14cbcSMatt Macy zprop_list_t *next;
2024eda14cbcSMatt Macy
2025eda14cbcSMatt Macy while (pl != NULL) {
2026eda14cbcSMatt Macy next = pl->pl_next;
2027eda14cbcSMatt Macy free(pl->pl_user_prop);
2028eda14cbcSMatt Macy free(pl);
2029eda14cbcSMatt Macy pl = next;
2030eda14cbcSMatt Macy }
2031eda14cbcSMatt Macy }
2032eda14cbcSMatt Macy
2033eda14cbcSMatt Macy typedef struct expand_data {
2034eda14cbcSMatt Macy zprop_list_t **last;
2035eda14cbcSMatt Macy libzfs_handle_t *hdl;
2036eda14cbcSMatt Macy zfs_type_t type;
2037eda14cbcSMatt Macy } expand_data_t;
2038eda14cbcSMatt Macy
2039eda14cbcSMatt Macy static int
zprop_expand_list_cb(int prop,void * cb)2040eda14cbcSMatt Macy zprop_expand_list_cb(int prop, void *cb)
2041eda14cbcSMatt Macy {
2042eda14cbcSMatt Macy zprop_list_t *entry;
2043eda14cbcSMatt Macy expand_data_t *edp = cb;
2044eda14cbcSMatt Macy
2045716fd348SMartin Matuska entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t));
2046eda14cbcSMatt Macy
2047eda14cbcSMatt Macy entry->pl_prop = prop;
2048eda14cbcSMatt Macy entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
2049eda14cbcSMatt Macy entry->pl_all = B_TRUE;
2050eda14cbcSMatt Macy
2051eda14cbcSMatt Macy *(edp->last) = entry;
2052eda14cbcSMatt Macy edp->last = &entry->pl_next;
2053eda14cbcSMatt Macy
2054eda14cbcSMatt Macy return (ZPROP_CONT);
2055eda14cbcSMatt Macy }
2056eda14cbcSMatt Macy
2057eda14cbcSMatt Macy int
zprop_expand_list(libzfs_handle_t * hdl,zprop_list_t ** plp,zfs_type_t type)2058eda14cbcSMatt Macy zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
2059eda14cbcSMatt Macy {
2060eda14cbcSMatt Macy zprop_list_t *entry;
2061eda14cbcSMatt Macy zprop_list_t **last;
2062eda14cbcSMatt Macy expand_data_t exp;
2063eda14cbcSMatt Macy
2064eda14cbcSMatt Macy if (*plp == NULL) {
2065eda14cbcSMatt Macy /*
2066eda14cbcSMatt Macy * If this is the very first time we've been called for an 'all'
2067eda14cbcSMatt Macy * specification, expand the list to include all native
2068eda14cbcSMatt Macy * properties.
2069eda14cbcSMatt Macy */
2070eda14cbcSMatt Macy last = plp;
2071eda14cbcSMatt Macy
2072eda14cbcSMatt Macy exp.last = last;
2073eda14cbcSMatt Macy exp.hdl = hdl;
2074eda14cbcSMatt Macy exp.type = type;
2075eda14cbcSMatt Macy
2076eda14cbcSMatt Macy if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
2077eda14cbcSMatt Macy B_FALSE, type) == ZPROP_INVAL)
2078eda14cbcSMatt Macy return (-1);
2079eda14cbcSMatt Macy
2080eda14cbcSMatt Macy /*
2081eda14cbcSMatt Macy * Add 'name' to the beginning of the list, which is handled
2082eda14cbcSMatt Macy * specially.
2083eda14cbcSMatt Macy */
2084c03c5b1cSMartin Matuska entry = zfs_alloc(hdl, sizeof (zprop_list_t));
2085681ce946SMartin Matuska entry->pl_prop = ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
2086681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME));
2087eda14cbcSMatt Macy entry->pl_width = zprop_width(entry->pl_prop,
2088eda14cbcSMatt Macy &entry->pl_fixed, type);
2089eda14cbcSMatt Macy entry->pl_all = B_TRUE;
2090eda14cbcSMatt Macy entry->pl_next = *plp;
2091eda14cbcSMatt Macy *plp = entry;
2092eda14cbcSMatt Macy }
2093eda14cbcSMatt Macy return (0);
2094eda14cbcSMatt Macy }
2095eda14cbcSMatt Macy
2096eda14cbcSMatt Macy int
zprop_iter(zprop_func func,void * cb,boolean_t show_all,boolean_t ordered,zfs_type_t type)2097eda14cbcSMatt Macy zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
2098eda14cbcSMatt Macy zfs_type_t type)
2099eda14cbcSMatt Macy {
2100eda14cbcSMatt Macy return (zprop_iter_common(func, cb, show_all, ordered, type));
2101eda14cbcSMatt Macy }
2102eda14cbcSMatt Macy
2103e3aa18adSMartin Matuska const char *
zfs_version_userland(void)2104e3aa18adSMartin Matuska zfs_version_userland(void)
2105eda14cbcSMatt Macy {
2106e3aa18adSMartin Matuska return (ZFS_META_ALIAS);
2107eda14cbcSMatt Macy }
2108eda14cbcSMatt Macy
2109eda14cbcSMatt Macy /*
2110eda14cbcSMatt Macy * Prints both zfs userland and kernel versions
2111e3aa18adSMartin Matuska * Returns 0 on success, and -1 on error
2112eda14cbcSMatt Macy */
2113eda14cbcSMatt Macy int
zfs_version_print(void)2114eda14cbcSMatt Macy zfs_version_print(void)
2115eda14cbcSMatt Macy {
2116e3aa18adSMartin Matuska (void) puts(ZFS_META_ALIAS);
2117eda14cbcSMatt Macy
2118e3aa18adSMartin Matuska char *kver = zfs_version_kernel();
2119e3aa18adSMartin Matuska if (kver == NULL) {
2120eda14cbcSMatt Macy fprintf(stderr, "zfs_version_kernel() failed: %s\n",
2121fd45b686SMartin Matuska zfs_strerror(errno));
2122eda14cbcSMatt Macy return (-1);
2123eda14cbcSMatt Macy }
2124eda14cbcSMatt Macy
2125e3aa18adSMartin Matuska (void) printf("zfs-kmod-%s\n", kver);
2126e3aa18adSMartin Matuska free(kver);
2127eda14cbcSMatt Macy return (0);
2128eda14cbcSMatt Macy }
2129eda14cbcSMatt Macy
2130eda14cbcSMatt Macy /*
2131ce4dcb97SMartin Matuska * Returns an nvlist with both zfs userland and kernel versions.
2132ce4dcb97SMartin Matuska * Returns NULL on error.
2133ce4dcb97SMartin Matuska */
2134ce4dcb97SMartin Matuska nvlist_t *
zfs_version_nvlist(void)2135ce4dcb97SMartin Matuska zfs_version_nvlist(void)
2136ce4dcb97SMartin Matuska {
2137ce4dcb97SMartin Matuska nvlist_t *nvl;
2138ce4dcb97SMartin Matuska char kmod_ver[64];
2139ce4dcb97SMartin Matuska if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2140ce4dcb97SMartin Matuska return (NULL);
2141ce4dcb97SMartin Matuska if (nvlist_add_string(nvl, "userland", ZFS_META_ALIAS) != 0)
2142ce4dcb97SMartin Matuska goto err;
2143ce4dcb97SMartin Matuska char *kver = zfs_version_kernel();
2144ce4dcb97SMartin Matuska if (kver == NULL) {
2145ce4dcb97SMartin Matuska fprintf(stderr, "zfs_version_kernel() failed: %s\n",
2146ce4dcb97SMartin Matuska zfs_strerror(errno));
2147ce4dcb97SMartin Matuska goto err;
2148ce4dcb97SMartin Matuska }
2149ce4dcb97SMartin Matuska (void) snprintf(kmod_ver, 64, "zfs-kmod-%s", kver);
2150ce4dcb97SMartin Matuska if (nvlist_add_string(nvl, "kernel", kmod_ver) != 0)
2151ce4dcb97SMartin Matuska goto err;
2152ce4dcb97SMartin Matuska return (nvl);
2153ce4dcb97SMartin Matuska err:
2154ce4dcb97SMartin Matuska nvlist_free(nvl);
2155ce4dcb97SMartin Matuska return (NULL);
2156ce4dcb97SMartin Matuska }
2157ce4dcb97SMartin Matuska
2158ce4dcb97SMartin Matuska /*
2159eda14cbcSMatt Macy * Return 1 if the user requested ANSI color output, and our terminal supports
2160eda14cbcSMatt Macy * it. Return 0 for no color.
2161eda14cbcSMatt Macy */
2162d411c1d6SMartin Matuska int
use_color(void)2163eda14cbcSMatt Macy use_color(void)
2164eda14cbcSMatt Macy {
2165eda14cbcSMatt Macy static int use_color = -1;
2166eda14cbcSMatt Macy char *term;
2167eda14cbcSMatt Macy
2168eda14cbcSMatt Macy /*
2169eda14cbcSMatt Macy * Optimization:
2170eda14cbcSMatt Macy *
2171eda14cbcSMatt Macy * For each zpool invocation, we do a single check to see if we should
2172eda14cbcSMatt Macy * be using color or not, and cache that value for the lifetime of the
2173eda14cbcSMatt Macy * the zpool command. That makes it cheap to call use_color() when
2174eda14cbcSMatt Macy * we're printing with color. We assume that the settings are not going
2175eda14cbcSMatt Macy * to change during the invocation of a zpool command (the user isn't
2176eda14cbcSMatt Macy * going to change the ZFS_COLOR value while zpool is running, for
2177eda14cbcSMatt Macy * example).
2178eda14cbcSMatt Macy */
2179eda14cbcSMatt Macy if (use_color != -1) {
2180eda14cbcSMatt Macy /*
2181eda14cbcSMatt Macy * We've already figured out if we should be using color or
2182eda14cbcSMatt Macy * not. Return the cached value.
2183eda14cbcSMatt Macy */
2184eda14cbcSMatt Macy return (use_color);
2185eda14cbcSMatt Macy }
2186eda14cbcSMatt Macy
2187eda14cbcSMatt Macy term = getenv("TERM");
2188eda14cbcSMatt Macy /*
2189eda14cbcSMatt Macy * The user sets the ZFS_COLOR env var set to enable zpool ANSI color
2190eda14cbcSMatt Macy * output. However if NO_COLOR is set (https://no-color.org/) then
2191eda14cbcSMatt Macy * don't use it. Also, don't use color if terminal doesn't support
2192eda14cbcSMatt Macy * it.
2193eda14cbcSMatt Macy */
2194eda14cbcSMatt Macy if (libzfs_envvar_is_set("ZFS_COLOR") &&
2195eda14cbcSMatt Macy !libzfs_envvar_is_set("NO_COLOR") &&
2196eda14cbcSMatt Macy isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 &&
2197eda14cbcSMatt Macy strcmp("unknown", term) != 0) {
2198eda14cbcSMatt Macy /* Color supported */
2199eda14cbcSMatt Macy use_color = 1;
2200eda14cbcSMatt Macy } else {
2201eda14cbcSMatt Macy use_color = 0;
2202eda14cbcSMatt Macy }
2203eda14cbcSMatt Macy
2204eda14cbcSMatt Macy return (use_color);
2205eda14cbcSMatt Macy }
2206eda14cbcSMatt Macy
2207eda14cbcSMatt Macy /*
22082a58b312SMartin Matuska * The functions color_start() and color_end() are used for when you want
22092a58b312SMartin Matuska * to colorize a block of text.
2210eda14cbcSMatt Macy *
22112a58b312SMartin Matuska * For example:
22122a58b312SMartin Matuska * color_start(ANSI_RED)
2213eda14cbcSMatt Macy * printf("hello");
2214eda14cbcSMatt Macy * printf("world");
2215eda14cbcSMatt Macy * color_end();
2216eda14cbcSMatt Macy */
2217eda14cbcSMatt Macy void
color_start(const char * color)2218a0b956f5SMartin Matuska color_start(const char *color)
2219eda14cbcSMatt Macy {
22202a58b312SMartin Matuska if (color && use_color()) {
2221a0b956f5SMartin Matuska fputs(color, stdout);
222215f0b8c3SMartin Matuska fflush(stdout);
222315f0b8c3SMartin Matuska }
2224eda14cbcSMatt Macy }
2225eda14cbcSMatt Macy
2226eda14cbcSMatt Macy void
color_end(void)2227eda14cbcSMatt Macy color_end(void)
2228eda14cbcSMatt Macy {
222915f0b8c3SMartin Matuska if (use_color()) {
2230a0b956f5SMartin Matuska fputs(ANSI_RESET, stdout);
223115f0b8c3SMartin Matuska fflush(stdout);
223215f0b8c3SMartin Matuska }
223315f0b8c3SMartin Matuska
2234eda14cbcSMatt Macy }
2235eda14cbcSMatt Macy
22362a58b312SMartin Matuska /*
22372a58b312SMartin Matuska * printf() with a color. If color is NULL, then do a normal printf.
22382a58b312SMartin Matuska */
2239eda14cbcSMatt Macy int
printf_color(const char * color,const char * format,...)2240a0b956f5SMartin Matuska printf_color(const char *color, const char *format, ...)
2241eda14cbcSMatt Macy {
2242eda14cbcSMatt Macy va_list aptr;
2243eda14cbcSMatt Macy int rc;
2244eda14cbcSMatt Macy
2245eda14cbcSMatt Macy if (color)
2246eda14cbcSMatt Macy color_start(color);
2247eda14cbcSMatt Macy
2248eda14cbcSMatt Macy va_start(aptr, format);
2249eda14cbcSMatt Macy rc = vprintf(format, aptr);
2250eda14cbcSMatt Macy va_end(aptr);
2251eda14cbcSMatt Macy
2252eda14cbcSMatt Macy if (color)
2253eda14cbcSMatt Macy color_end();
2254eda14cbcSMatt Macy
2255eda14cbcSMatt Macy return (rc);
2256eda14cbcSMatt Macy }
2257abcdc1b9SMartin Matuska
2258abcdc1b9SMartin Matuska /* PATH + 5 env vars + a NULL entry = 7 */
2259abcdc1b9SMartin Matuska #define ZPOOL_VDEV_SCRIPT_ENV_COUNT 7
2260abcdc1b9SMartin Matuska
2261abcdc1b9SMartin Matuska /*
2262abcdc1b9SMartin Matuska * There's a few places where ZFS will call external scripts (like the script
2263abcdc1b9SMartin Matuska * in zpool.d/ and `zfs_prepare_disk`). These scripts are called with a
2264abcdc1b9SMartin Matuska * reduced $PATH, and some vdev specific environment vars set. This function
2265abcdc1b9SMartin Matuska * will allocate an populate the environment variable array that is passed to
2266abcdc1b9SMartin Matuska * these scripts. The user must free the arrays with zpool_vdev_free_env() when
2267abcdc1b9SMartin Matuska * they are done.
2268abcdc1b9SMartin Matuska *
2269abcdc1b9SMartin Matuska * The following env vars will be set (but value could be blank):
2270abcdc1b9SMartin Matuska *
2271abcdc1b9SMartin Matuska * POOL_NAME
2272abcdc1b9SMartin Matuska * VDEV_PATH
2273abcdc1b9SMartin Matuska * VDEV_UPATH
2274abcdc1b9SMartin Matuska * VDEV_ENC_SYSFS_PATH
2275abcdc1b9SMartin Matuska *
2276abcdc1b9SMartin Matuska * In addition, you can set an optional environment variable named 'opt_key'
2277abcdc1b9SMartin Matuska * to 'opt_val' if you want.
2278abcdc1b9SMartin Matuska *
2279abcdc1b9SMartin Matuska * Returns allocated env[] array on success, NULL otherwise.
2280abcdc1b9SMartin Matuska */
2281abcdc1b9SMartin Matuska char **
zpool_vdev_script_alloc_env(const char * pool_name,const char * vdev_path,const char * vdev_upath,const char * vdev_enc_sysfs_path,const char * opt_key,const char * opt_val)2282abcdc1b9SMartin Matuska zpool_vdev_script_alloc_env(const char *pool_name,
2283abcdc1b9SMartin Matuska const char *vdev_path, const char *vdev_upath,
2284abcdc1b9SMartin Matuska const char *vdev_enc_sysfs_path, const char *opt_key, const char *opt_val)
2285abcdc1b9SMartin Matuska {
2286abcdc1b9SMartin Matuska char **env = NULL;
2287abcdc1b9SMartin Matuska int rc;
2288abcdc1b9SMartin Matuska
2289abcdc1b9SMartin Matuska env = calloc(ZPOOL_VDEV_SCRIPT_ENV_COUNT, sizeof (*env));
2290abcdc1b9SMartin Matuska if (!env)
2291abcdc1b9SMartin Matuska return (NULL);
2292abcdc1b9SMartin Matuska
2293abcdc1b9SMartin Matuska env[0] = strdup("PATH=/bin:/sbin:/usr/bin:/usr/sbin");
2294abcdc1b9SMartin Matuska if (!env[0])
2295abcdc1b9SMartin Matuska goto error;
2296abcdc1b9SMartin Matuska
2297abcdc1b9SMartin Matuska /* Setup our custom environment variables */
2298abcdc1b9SMartin Matuska rc = asprintf(&env[1], "POOL_NAME=%s", pool_name ? pool_name : "");
2299abcdc1b9SMartin Matuska if (rc == -1) {
2300abcdc1b9SMartin Matuska env[1] = NULL;
2301abcdc1b9SMartin Matuska goto error;
2302abcdc1b9SMartin Matuska }
2303abcdc1b9SMartin Matuska
2304abcdc1b9SMartin Matuska rc = asprintf(&env[2], "VDEV_PATH=%s", vdev_path ? vdev_path : "");
2305abcdc1b9SMartin Matuska if (rc == -1) {
2306abcdc1b9SMartin Matuska env[2] = NULL;
2307abcdc1b9SMartin Matuska goto error;
2308abcdc1b9SMartin Matuska }
2309abcdc1b9SMartin Matuska
2310abcdc1b9SMartin Matuska rc = asprintf(&env[3], "VDEV_UPATH=%s", vdev_upath ? vdev_upath : "");
2311abcdc1b9SMartin Matuska if (rc == -1) {
2312abcdc1b9SMartin Matuska env[3] = NULL;
2313abcdc1b9SMartin Matuska goto error;
2314abcdc1b9SMartin Matuska }
2315abcdc1b9SMartin Matuska
2316abcdc1b9SMartin Matuska rc = asprintf(&env[4], "VDEV_ENC_SYSFS_PATH=%s",
2317abcdc1b9SMartin Matuska vdev_enc_sysfs_path ? vdev_enc_sysfs_path : "");
2318abcdc1b9SMartin Matuska if (rc == -1) {
2319abcdc1b9SMartin Matuska env[4] = NULL;
2320abcdc1b9SMartin Matuska goto error;
2321abcdc1b9SMartin Matuska }
2322abcdc1b9SMartin Matuska
2323abcdc1b9SMartin Matuska if (opt_key != NULL) {
2324abcdc1b9SMartin Matuska rc = asprintf(&env[5], "%s=%s", opt_key,
2325abcdc1b9SMartin Matuska opt_val ? opt_val : "");
2326abcdc1b9SMartin Matuska if (rc == -1) {
2327abcdc1b9SMartin Matuska env[5] = NULL;
2328abcdc1b9SMartin Matuska goto error;
2329abcdc1b9SMartin Matuska }
2330abcdc1b9SMartin Matuska }
2331abcdc1b9SMartin Matuska
2332abcdc1b9SMartin Matuska return (env);
2333abcdc1b9SMartin Matuska
2334abcdc1b9SMartin Matuska error:
2335abcdc1b9SMartin Matuska for (int i = 0; i < ZPOOL_VDEV_SCRIPT_ENV_COUNT; i++)
2336abcdc1b9SMartin Matuska free(env[i]);
2337abcdc1b9SMartin Matuska
2338abcdc1b9SMartin Matuska free(env);
2339abcdc1b9SMartin Matuska
2340abcdc1b9SMartin Matuska return (NULL);
2341abcdc1b9SMartin Matuska }
2342abcdc1b9SMartin Matuska
2343abcdc1b9SMartin Matuska /*
2344abcdc1b9SMartin Matuska * Free the env[] array that was allocated by zpool_vdev_script_alloc_env().
2345abcdc1b9SMartin Matuska */
2346abcdc1b9SMartin Matuska void
zpool_vdev_script_free_env(char ** env)2347abcdc1b9SMartin Matuska zpool_vdev_script_free_env(char **env)
2348abcdc1b9SMartin Matuska {
2349abcdc1b9SMartin Matuska for (int i = 0; i < ZPOOL_VDEV_SCRIPT_ENV_COUNT; i++)
2350abcdc1b9SMartin Matuska free(env[i]);
2351abcdc1b9SMartin Matuska
2352abcdc1b9SMartin Matuska free(env);
2353abcdc1b9SMartin Matuska }
2354abcdc1b9SMartin Matuska
2355abcdc1b9SMartin Matuska /*
2356abcdc1b9SMartin Matuska * Prepare a disk by (optionally) running a program before labeling the disk.
2357abcdc1b9SMartin Matuska * This can be useful for installing disk firmware or doing some pre-flight
2358abcdc1b9SMartin Matuska * checks on the disk before it becomes part of the pool. The program run is
2359abcdc1b9SMartin Matuska * located at ZFSEXECDIR/zfs_prepare_disk
2360abcdc1b9SMartin Matuska * (E.x: /usr/local/libexec/zfs/zfs_prepare_disk).
2361abcdc1b9SMartin Matuska *
2362abcdc1b9SMartin Matuska * Return 0 on success, non-zero on failure.
2363abcdc1b9SMartin Matuska */
2364abcdc1b9SMartin Matuska int
zpool_prepare_disk(zpool_handle_t * zhp,nvlist_t * vdev_nv,const char * prepare_str,char ** lines[],int * lines_cnt)2365abcdc1b9SMartin Matuska zpool_prepare_disk(zpool_handle_t *zhp, nvlist_t *vdev_nv,
2366abcdc1b9SMartin Matuska const char *prepare_str, char **lines[], int *lines_cnt)
2367abcdc1b9SMartin Matuska {
2368abcdc1b9SMartin Matuska const char *script_path = ZFSEXECDIR "/zfs_prepare_disk";
2369abcdc1b9SMartin Matuska const char *pool_name;
2370abcdc1b9SMartin Matuska int rc = 0;
2371abcdc1b9SMartin Matuska
2372abcdc1b9SMartin Matuska /* Path to script and a NULL entry */
2373abcdc1b9SMartin Matuska char *argv[2] = {(char *)script_path};
2374abcdc1b9SMartin Matuska char **env = NULL;
2375abcdc1b9SMartin Matuska const char *path = NULL, *enc_sysfs_path = NULL;
2376abcdc1b9SMartin Matuska char *upath;
2377abcdc1b9SMartin Matuska *lines_cnt = 0;
2378abcdc1b9SMartin Matuska
2379abcdc1b9SMartin Matuska if (access(script_path, X_OK) != 0) {
2380abcdc1b9SMartin Matuska /* No script, nothing to do */
2381abcdc1b9SMartin Matuska return (0);
2382abcdc1b9SMartin Matuska }
2383abcdc1b9SMartin Matuska
2384abcdc1b9SMartin Matuska (void) nvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_PATH, &path);
2385abcdc1b9SMartin Matuska (void) nvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
2386abcdc1b9SMartin Matuska &enc_sysfs_path);
2387abcdc1b9SMartin Matuska
2388abcdc1b9SMartin Matuska upath = zfs_get_underlying_path(path);
2389abcdc1b9SMartin Matuska pool_name = zhp ? zpool_get_name(zhp) : NULL;
2390abcdc1b9SMartin Matuska
2391abcdc1b9SMartin Matuska env = zpool_vdev_script_alloc_env(pool_name, path, upath,
2392abcdc1b9SMartin Matuska enc_sysfs_path, "VDEV_PREPARE", prepare_str);
2393abcdc1b9SMartin Matuska
2394abcdc1b9SMartin Matuska free(upath);
2395abcdc1b9SMartin Matuska
2396abcdc1b9SMartin Matuska if (env == NULL) {
2397abcdc1b9SMartin Matuska return (ENOMEM);
2398abcdc1b9SMartin Matuska }
2399abcdc1b9SMartin Matuska
2400abcdc1b9SMartin Matuska rc = libzfs_run_process_get_stdout(script_path, argv, env, lines,
2401abcdc1b9SMartin Matuska lines_cnt);
2402abcdc1b9SMartin Matuska
2403abcdc1b9SMartin Matuska zpool_vdev_script_free_env(env);
2404abcdc1b9SMartin Matuska
2405abcdc1b9SMartin Matuska return (rc);
2406abcdc1b9SMartin Matuska }
2407abcdc1b9SMartin Matuska
2408abcdc1b9SMartin Matuska /*
2409abcdc1b9SMartin Matuska * Optionally run a script and then label a disk. The script can be used to
2410abcdc1b9SMartin Matuska * prepare a disk for inclusion into the pool. For example, it might update
2411abcdc1b9SMartin Matuska * the disk's firmware or check its health.
2412abcdc1b9SMartin Matuska *
2413abcdc1b9SMartin Matuska * The 'name' provided is the short name, stripped of any leading
2414abcdc1b9SMartin Matuska * /dev path, and is passed to zpool_label_disk. vdev_nv is the nvlist for
2415abcdc1b9SMartin Matuska * the vdev. prepare_str is a string that gets passed as the VDEV_PREPARE
2416abcdc1b9SMartin Matuska * env variable to the script.
2417abcdc1b9SMartin Matuska *
2418abcdc1b9SMartin Matuska * The following env vars are passed to the script:
2419abcdc1b9SMartin Matuska *
2420abcdc1b9SMartin Matuska * POOL_NAME: The pool name (blank during zpool create)
2421abcdc1b9SMartin Matuska * VDEV_PREPARE: Reason why the disk is being prepared for inclusion:
2422abcdc1b9SMartin Matuska * "create", "add", "replace", or "autoreplace"
2423abcdc1b9SMartin Matuska * VDEV_PATH: Path to the disk
2424abcdc1b9SMartin Matuska * VDEV_UPATH: One of the 'underlying paths' to the disk. This is
2425abcdc1b9SMartin Matuska * useful for DM devices.
2426abcdc1b9SMartin Matuska * VDEV_ENC_SYSFS_PATH: Path to the disk's enclosure sysfs path, if available.
2427abcdc1b9SMartin Matuska *
2428abcdc1b9SMartin Matuska * Note, some of these values can be blank.
2429abcdc1b9SMartin Matuska *
2430abcdc1b9SMartin Matuska * Return 0 on success, non-zero otherwise.
2431abcdc1b9SMartin Matuska */
2432abcdc1b9SMartin Matuska int
zpool_prepare_and_label_disk(libzfs_handle_t * hdl,zpool_handle_t * zhp,const char * name,nvlist_t * vdev_nv,const char * prepare_str,char ** lines[],int * lines_cnt)2433abcdc1b9SMartin Matuska zpool_prepare_and_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp,
2434abcdc1b9SMartin Matuska const char *name, nvlist_t *vdev_nv, const char *prepare_str,
2435abcdc1b9SMartin Matuska char **lines[], int *lines_cnt)
2436abcdc1b9SMartin Matuska {
2437abcdc1b9SMartin Matuska int rc;
2438abcdc1b9SMartin Matuska char vdev_path[MAXPATHLEN];
2439abcdc1b9SMartin Matuska (void) snprintf(vdev_path, sizeof (vdev_path), "%s/%s", DISK_ROOT,
2440abcdc1b9SMartin Matuska name);
2441abcdc1b9SMartin Matuska
2442abcdc1b9SMartin Matuska /* zhp will be NULL when creating a pool */
2443abcdc1b9SMartin Matuska rc = zpool_prepare_disk(zhp, vdev_nv, prepare_str, lines, lines_cnt);
2444abcdc1b9SMartin Matuska if (rc != 0)
2445abcdc1b9SMartin Matuska return (rc);
2446abcdc1b9SMartin Matuska
2447abcdc1b9SMartin Matuska rc = zpool_label_disk(hdl, zhp, name);
2448abcdc1b9SMartin Matuska return (rc);
2449abcdc1b9SMartin Matuska }
2450