]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
9ae529ec | 21 | |
34dc7c2f | 22 | /* |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
c14ca145 | 24 | * Copyright 2020 Joyent, Inc. All rights reserved. |
c15d36c6 | 25 | * Copyright (c) 2011, 2020 by Delphix. All rights reserved. |
23d70cde | 26 | * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> |
0ea05c64 | 27 | * Copyright (c) 2017 Datto Inc. |
8fb79fdd AJ |
28 | * Copyright (c) 2020 The FreeBSD Foundation |
29 | * | |
30 | * Portions of this software were developed by Allan Jude | |
31 | * under sponsorship from the FreeBSD Foundation. | |
34dc7c2f BB |
32 | */ |
33 | ||
34dc7c2f BB |
34 | /* |
35 | * Internal utility routines for the ZFS library. | |
36 | */ | |
37 | ||
38 | #include <errno.h> | |
39 | #include <fcntl.h> | |
40 | #include <libintl.h> | |
41 | #include <stdarg.h> | |
42 | #include <stdio.h> | |
43 | #include <stdlib.h> | |
44 | #include <strings.h> | |
45 | #include <unistd.h> | |
34dc7c2f | 46 | #include <math.h> |
37086897 AZ |
47 | #if LIBFETCH_DYNAMIC |
48 | #include <dlfcn.h> | |
49 | #endif | |
d603ed6c | 50 | #include <sys/stat.h> |
34dc7c2f BB |
51 | #include <sys/mnttab.h> |
52 | #include <sys/mntent.h> | |
53 | #include <sys/types.h> | |
120ff39a | 54 | #include <sys/wait.h> |
34dc7c2f BB |
55 | |
56 | #include <libzfs.h> | |
6f1ffb06 | 57 | #include <libzfs_core.h> |
34dc7c2f BB |
58 | |
59 | #include "libzfs_impl.h" | |
60 | #include "zfs_prop.h" | |
9ae529ec | 61 | #include "zfeature_common.h" |
dc03fa30 | 62 | #include <zfs_fletcher.h> |
e89f1295 | 63 | #include <libzutil.h> |
34dc7c2f | 64 | |
c14ca145 JK |
65 | /* |
66 | * We only care about the scheme in order to match the scheme | |
67 | * with the handler. Each handler should validate the full URI | |
68 | * as necessary. | |
69 | */ | |
70 | #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):" | |
73cdcc63 | 71 | |
34dc7c2f BB |
72 | int |
73 | libzfs_errno(libzfs_handle_t *hdl) | |
74 | { | |
75 | return (hdl->libzfs_error); | |
76 | } | |
77 | ||
78 | const char * | |
79 | libzfs_error_action(libzfs_handle_t *hdl) | |
80 | { | |
81 | return (hdl->libzfs_action); | |
82 | } | |
83 | ||
84 | const char * | |
85 | libzfs_error_description(libzfs_handle_t *hdl) | |
86 | { | |
87 | if (hdl->libzfs_desc[0] != '\0') | |
88 | return (hdl->libzfs_desc); | |
89 | ||
90 | switch (hdl->libzfs_error) { | |
91 | case EZFS_NOMEM: | |
92 | return (dgettext(TEXT_DOMAIN, "out of memory")); | |
93 | case EZFS_BADPROP: | |
94 | return (dgettext(TEXT_DOMAIN, "invalid property value")); | |
95 | case EZFS_PROPREADONLY: | |
572e2857 | 96 | return (dgettext(TEXT_DOMAIN, "read-only property")); |
34dc7c2f BB |
97 | case EZFS_PROPTYPE: |
98 | return (dgettext(TEXT_DOMAIN, "property doesn't apply to " | |
99 | "datasets of this type")); | |
100 | case EZFS_PROPNONINHERIT: | |
101 | return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); | |
102 | case EZFS_PROPSPACE: | |
103 | return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); | |
104 | case EZFS_BADTYPE: | |
105 | return (dgettext(TEXT_DOMAIN, "operation not applicable to " | |
106 | "datasets of this type")); | |
107 | case EZFS_BUSY: | |
108 | return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); | |
109 | case EZFS_EXISTS: | |
110 | return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); | |
111 | case EZFS_NOENT: | |
112 | return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); | |
113 | case EZFS_BADSTREAM: | |
114 | return (dgettext(TEXT_DOMAIN, "invalid backup stream")); | |
115 | case EZFS_DSREADONLY: | |
572e2857 | 116 | return (dgettext(TEXT_DOMAIN, "dataset is read-only")); |
34dc7c2f BB |
117 | case EZFS_VOLTOOBIG: |
118 | return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " | |
119 | "this system")); | |
34dc7c2f BB |
120 | case EZFS_INVALIDNAME: |
121 | return (dgettext(TEXT_DOMAIN, "invalid name")); | |
122 | case EZFS_BADRESTORE: | |
123 | return (dgettext(TEXT_DOMAIN, "unable to restore to " | |
124 | "destination")); | |
125 | case EZFS_BADBACKUP: | |
126 | return (dgettext(TEXT_DOMAIN, "backup failed")); | |
127 | case EZFS_BADTARGET: | |
128 | return (dgettext(TEXT_DOMAIN, "invalid target vdev")); | |
129 | case EZFS_NODEVICE: | |
130 | return (dgettext(TEXT_DOMAIN, "no such device in pool")); | |
131 | case EZFS_BADDEV: | |
132 | return (dgettext(TEXT_DOMAIN, "invalid device")); | |
133 | case EZFS_NOREPLICAS: | |
134 | return (dgettext(TEXT_DOMAIN, "no valid replicas")); | |
135 | case EZFS_RESILVERING: | |
136 | return (dgettext(TEXT_DOMAIN, "currently resilvering")); | |
137 | case EZFS_BADVERSION: | |
9ae529ec CS |
138 | return (dgettext(TEXT_DOMAIN, "unsupported version or " |
139 | "feature")); | |
34dc7c2f BB |
140 | case EZFS_POOLUNAVAIL: |
141 | return (dgettext(TEXT_DOMAIN, "pool is unavailable")); | |
142 | case EZFS_DEVOVERFLOW: | |
143 | return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); | |
144 | case EZFS_BADPATH: | |
145 | return (dgettext(TEXT_DOMAIN, "must be an absolute path")); | |
146 | case EZFS_CROSSTARGET: | |
147 | return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " | |
148 | "pools")); | |
149 | case EZFS_ZONED: | |
150 | return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); | |
151 | case EZFS_MOUNTFAILED: | |
152 | return (dgettext(TEXT_DOMAIN, "mount failed")); | |
153 | case EZFS_UMOUNTFAILED: | |
76d04993 | 154 | return (dgettext(TEXT_DOMAIN, "unmount failed")); |
34dc7c2f | 155 | case EZFS_UNSHARENFSFAILED: |
76d04993 | 156 | return (dgettext(TEXT_DOMAIN, "NFS share removal failed")); |
34dc7c2f | 157 | case EZFS_SHARENFSFAILED: |
76d04993 | 158 | return (dgettext(TEXT_DOMAIN, "NFS share creation failed")); |
34dc7c2f | 159 | case EZFS_UNSHARESMBFAILED: |
76d04993 | 160 | return (dgettext(TEXT_DOMAIN, "SMB share removal failed")); |
34dc7c2f | 161 | case EZFS_SHARESMBFAILED: |
76d04993 | 162 | return (dgettext(TEXT_DOMAIN, "SMB share creation failed")); |
34dc7c2f BB |
163 | case EZFS_PERM: |
164 | return (dgettext(TEXT_DOMAIN, "permission denied")); | |
165 | case EZFS_NOSPC: | |
166 | return (dgettext(TEXT_DOMAIN, "out of space")); | |
428870ff BB |
167 | case EZFS_FAULT: |
168 | return (dgettext(TEXT_DOMAIN, "bad address")); | |
34dc7c2f BB |
169 | case EZFS_IO: |
170 | return (dgettext(TEXT_DOMAIN, "I/O error")); | |
171 | case EZFS_INTR: | |
172 | return (dgettext(TEXT_DOMAIN, "signal received")); | |
173 | case EZFS_ISSPARE: | |
174 | return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " | |
175 | "spare")); | |
176 | case EZFS_INVALCONFIG: | |
177 | return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); | |
178 | case EZFS_RECURSIVE: | |
179 | return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); | |
180 | case EZFS_NOHISTORY: | |
181 | return (dgettext(TEXT_DOMAIN, "no history available")); | |
34dc7c2f BB |
182 | case EZFS_POOLPROPS: |
183 | return (dgettext(TEXT_DOMAIN, "failed to retrieve " | |
184 | "pool properties")); | |
185 | case EZFS_POOL_NOTSUP: | |
186 | return (dgettext(TEXT_DOMAIN, "operation not supported " | |
187 | "on this type of pool")); | |
188 | case EZFS_POOL_INVALARG: | |
189 | return (dgettext(TEXT_DOMAIN, "invalid argument for " | |
190 | "this pool operation")); | |
191 | case EZFS_NAMETOOLONG: | |
192 | return (dgettext(TEXT_DOMAIN, "dataset name is too long")); | |
193 | case EZFS_OPENFAILED: | |
194 | return (dgettext(TEXT_DOMAIN, "open failed")); | |
195 | case EZFS_NOCAP: | |
196 | return (dgettext(TEXT_DOMAIN, | |
197 | "disk capacity information could not be retrieved")); | |
198 | case EZFS_LABELFAILED: | |
199 | return (dgettext(TEXT_DOMAIN, "write of label failed")); | |
200 | case EZFS_BADWHO: | |
201 | return (dgettext(TEXT_DOMAIN, "invalid user/group")); | |
202 | case EZFS_BADPERM: | |
203 | return (dgettext(TEXT_DOMAIN, "invalid permission")); | |
204 | case EZFS_BADPERMSET: | |
205 | return (dgettext(TEXT_DOMAIN, "invalid permission set name")); | |
206 | case EZFS_NODELEGATION: | |
207 | return (dgettext(TEXT_DOMAIN, "delegated administration is " | |
208 | "disabled on pool")); | |
34dc7c2f BB |
209 | case EZFS_BADCACHE: |
210 | return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); | |
211 | case EZFS_ISL2CACHE: | |
212 | return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); | |
213 | case EZFS_VDEVNOTSUP: | |
214 | return (dgettext(TEXT_DOMAIN, "vdev specification is not " | |
215 | "supported")); | |
b128c09f BB |
216 | case EZFS_NOTSUP: |
217 | return (dgettext(TEXT_DOMAIN, "operation not supported " | |
218 | "on this dataset")); | |
b83a0e2d DB |
219 | case EZFS_IOC_NOTSUPPORTED: |
220 | return (dgettext(TEXT_DOMAIN, "operation not supported by " | |
221 | "zfs kernel module")); | |
b128c09f BB |
222 | case EZFS_ACTIVE_SPARE: |
223 | return (dgettext(TEXT_DOMAIN, "pool has active shared spare " | |
224 | "device")); | |
9babb374 BB |
225 | case EZFS_UNPLAYED_LOGS: |
226 | return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " | |
227 | "logs")); | |
45d1cae3 BB |
228 | case EZFS_REFTAG_RELE: |
229 | return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); | |
230 | case EZFS_REFTAG_HOLD: | |
231 | return (dgettext(TEXT_DOMAIN, "tag already exists on this " | |
232 | "dataset")); | |
428870ff BB |
233 | case EZFS_TAGTOOLONG: |
234 | return (dgettext(TEXT_DOMAIN, "tag too long")); | |
235 | case EZFS_PIPEFAILED: | |
236 | return (dgettext(TEXT_DOMAIN, "pipe create failed")); | |
237 | case EZFS_THREADCREATEFAILED: | |
238 | return (dgettext(TEXT_DOMAIN, "thread create failed")); | |
239 | case EZFS_POSTSPLIT_ONLINE: | |
240 | return (dgettext(TEXT_DOMAIN, "disk was split from this pool " | |
241 | "into a new one")); | |
0ea05c64 AP |
242 | case EZFS_SCRUB_PAUSED: |
243 | return (dgettext(TEXT_DOMAIN, "scrub is paused; " | |
244 | "use 'zpool scrub' to resume")); | |
428870ff BB |
245 | case EZFS_SCRUBBING: |
246 | return (dgettext(TEXT_DOMAIN, "currently scrubbing; " | |
247 | "use 'zpool scrub -s' to cancel current scrub")); | |
248 | case EZFS_NO_SCRUB: | |
249 | return (dgettext(TEXT_DOMAIN, "there is no active scrub")); | |
572e2857 BB |
250 | case EZFS_DIFF: |
251 | return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); | |
252 | case EZFS_DIFFDATA: | |
253 | return (dgettext(TEXT_DOMAIN, "invalid diff data")); | |
254 | case EZFS_POOLREADONLY: | |
255 | return (dgettext(TEXT_DOMAIN, "pool is read-only")); | |
a1d477c2 MA |
256 | case EZFS_NO_PENDING: |
257 | return (dgettext(TEXT_DOMAIN, "operation is not " | |
258 | "in progress")); | |
d2734cce SD |
259 | case EZFS_CHECKPOINT_EXISTS: |
260 | return (dgettext(TEXT_DOMAIN, "checkpoint exists")); | |
261 | case EZFS_DISCARDING_CHECKPOINT: | |
262 | return (dgettext(TEXT_DOMAIN, "currently discarding " | |
263 | "checkpoint")); | |
264 | case EZFS_NO_CHECKPOINT: | |
265 | return (dgettext(TEXT_DOMAIN, "checkpoint does not exist")); | |
266 | case EZFS_DEVRM_IN_PROGRESS: | |
267 | return (dgettext(TEXT_DOMAIN, "device removal in progress")); | |
268 | case EZFS_VDEV_TOO_BIG: | |
269 | return (dgettext(TEXT_DOMAIN, "device exceeds supported size")); | |
379ca9cf OF |
270 | case EZFS_ACTIVE_POOL: |
271 | return (dgettext(TEXT_DOMAIN, "pool is imported on a " | |
272 | "different host")); | |
b5256303 TC |
273 | case EZFS_CRYPTOFAILED: |
274 | return (dgettext(TEXT_DOMAIN, "encryption failure")); | |
619f0976 GW |
275 | case EZFS_TOOMANY: |
276 | return (dgettext(TEXT_DOMAIN, "argument list too long")); | |
277 | case EZFS_INITIALIZING: | |
278 | return (dgettext(TEXT_DOMAIN, "currently initializing")); | |
279 | case EZFS_NO_INITIALIZE: | |
280 | return (dgettext(TEXT_DOMAIN, "there is no active " | |
281 | "initialization")); | |
d8d418ff | 282 | case EZFS_WRONG_PARENT: |
283 | return (dgettext(TEXT_DOMAIN, "invalid parent dataset")); | |
1b939560 BB |
284 | case EZFS_TRIMMING: |
285 | return (dgettext(TEXT_DOMAIN, "currently trimming")); | |
286 | case EZFS_NO_TRIM: | |
287 | return (dgettext(TEXT_DOMAIN, "there is no active trim")); | |
288 | case EZFS_TRIM_NOTSUP: | |
289 | return (dgettext(TEXT_DOMAIN, "trim operations are not " | |
290 | "supported by this device")); | |
fa241660 TC |
291 | case EZFS_NO_RESILVER_DEFER: |
292 | return (dgettext(TEXT_DOMAIN, "this action requires the " | |
293 | "resilver_defer feature")); | |
43a85362 SD |
294 | case EZFS_EXPORT_IN_PROGRESS: |
295 | return (dgettext(TEXT_DOMAIN, "pool export in progress")); | |
9a49d3f3 BB |
296 | case EZFS_REBUILDING: |
297 | return (dgettext(TEXT_DOMAIN, "currently sequentially " | |
298 | "resilvering")); | |
2a673e76 AJ |
299 | case EZFS_VDEV_NOTSUP: |
300 | return (dgettext(TEXT_DOMAIN, "operation not supported " | |
301 | "on this type of vdev")); | |
34dc7c2f BB |
302 | case EZFS_UNKNOWN: |
303 | return (dgettext(TEXT_DOMAIN, "unknown error")); | |
304 | default: | |
305 | assert(hdl->libzfs_error == 0); | |
306 | return (dgettext(TEXT_DOMAIN, "no error")); | |
307 | } | |
308 | } | |
309 | ||
34dc7c2f BB |
310 | void |
311 | zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) | |
312 | { | |
313 | va_list ap; | |
314 | ||
315 | va_start(ap, fmt); | |
316 | ||
317 | (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), | |
318 | fmt, ap); | |
319 | hdl->libzfs_desc_active = 1; | |
320 | ||
321 | va_end(ap); | |
322 | } | |
323 | ||
324 | static void | |
325 | zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) | |
326 | { | |
327 | (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), | |
328 | fmt, ap); | |
329 | hdl->libzfs_error = error; | |
330 | ||
331 | if (hdl->libzfs_desc_active) | |
332 | hdl->libzfs_desc_active = 0; | |
333 | else | |
334 | hdl->libzfs_desc[0] = '\0'; | |
335 | ||
336 | if (hdl->libzfs_printerr) { | |
337 | if (error == EZFS_UNKNOWN) { | |
338 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " | |
24f98ed3 AJ |
339 | "error: %s: %s\n"), hdl->libzfs_action, |
340 | libzfs_error_description(hdl)); | |
34dc7c2f BB |
341 | abort(); |
342 | } | |
343 | ||
344 | (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, | |
345 | libzfs_error_description(hdl)); | |
346 | if (error == EZFS_NOMEM) | |
347 | exit(1); | |
348 | } | |
349 | } | |
350 | ||
351 | int | |
352 | zfs_error(libzfs_handle_t *hdl, int error, const char *msg) | |
353 | { | |
354 | return (zfs_error_fmt(hdl, error, "%s", msg)); | |
355 | } | |
356 | ||
34dc7c2f BB |
357 | int |
358 | zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) | |
359 | { | |
360 | va_list ap; | |
361 | ||
362 | va_start(ap, fmt); | |
363 | ||
364 | zfs_verror(hdl, error, fmt, ap); | |
365 | ||
366 | va_end(ap); | |
367 | ||
368 | return (-1); | |
369 | } | |
370 | ||
371 | static int | |
372 | zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, | |
373 | va_list ap) | |
374 | { | |
375 | switch (error) { | |
376 | case EPERM: | |
377 | case EACCES: | |
378 | zfs_verror(hdl, EZFS_PERM, fmt, ap); | |
379 | return (-1); | |
380 | ||
381 | case ECANCELED: | |
382 | zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); | |
383 | return (-1); | |
384 | ||
385 | case EIO: | |
386 | zfs_verror(hdl, EZFS_IO, fmt, ap); | |
387 | return (-1); | |
388 | ||
428870ff BB |
389 | case EFAULT: |
390 | zfs_verror(hdl, EZFS_FAULT, fmt, ap); | |
391 | return (-1); | |
392 | ||
34dc7c2f BB |
393 | case EINTR: |
394 | zfs_verror(hdl, EZFS_INTR, fmt, ap); | |
395 | return (-1); | |
396 | } | |
397 | ||
398 | return (0); | |
399 | } | |
400 | ||
401 | int | |
402 | zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) | |
403 | { | |
404 | return (zfs_standard_error_fmt(hdl, error, "%s", msg)); | |
405 | } | |
406 | ||
34dc7c2f BB |
407 | int |
408 | zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) | |
409 | { | |
410 | va_list ap; | |
411 | ||
412 | va_start(ap, fmt); | |
413 | ||
414 | if (zfs_common_error(hdl, error, fmt, ap) != 0) { | |
415 | va_end(ap); | |
416 | return (-1); | |
417 | } | |
418 | ||
419 | switch (error) { | |
420 | case ENXIO: | |
421 | case ENODEV: | |
330d06f9 | 422 | case EPIPE: |
34dc7c2f BB |
423 | zfs_verror(hdl, EZFS_IO, fmt, ap); |
424 | break; | |
425 | ||
426 | case ENOENT: | |
427 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
428 | "dataset does not exist")); | |
429 | zfs_verror(hdl, EZFS_NOENT, fmt, ap); | |
430 | break; | |
431 | ||
432 | case ENOSPC: | |
433 | case EDQUOT: | |
434 | zfs_verror(hdl, EZFS_NOSPC, fmt, ap); | |
b264d9b3 | 435 | break; |
34dc7c2f BB |
436 | |
437 | case EEXIST: | |
438 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
439 | "dataset already exists")); | |
440 | zfs_verror(hdl, EZFS_EXISTS, fmt, ap); | |
441 | break; | |
442 | ||
443 | case EBUSY: | |
444 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
445 | "dataset is busy")); | |
446 | zfs_verror(hdl, EZFS_BUSY, fmt, ap); | |
447 | break; | |
448 | case EROFS: | |
572e2857 | 449 | zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); |
34dc7c2f BB |
450 | break; |
451 | case ENAMETOOLONG: | |
452 | zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); | |
453 | break; | |
454 | case ENOTSUP: | |
455 | zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); | |
456 | break; | |
9babb374 BB |
457 | case EAGAIN: |
458 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
459 | "pool I/O is currently suspended")); | |
460 | zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); | |
461 | break; | |
379ca9cf OF |
462 | case EREMOTEIO: |
463 | zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); | |
464 | break; | |
30af21b0 | 465 | case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE: |
b83a0e2d DB |
466 | case ZFS_ERR_IOC_CMD_UNAVAIL: |
467 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " | |
468 | "module does not support this operation. A reboot may " | |
469 | "be required to enable this operation.")); | |
470 | zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); | |
471 | break; | |
472 | case ZFS_ERR_IOC_ARG_UNAVAIL: | |
473 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " | |
474 | "module does not support an option for this operation. " | |
475 | "A reboot may be required to enable this option.")); | |
476 | zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); | |
477 | break; | |
478 | case ZFS_ERR_IOC_ARG_REQUIRED: | |
479 | case ZFS_ERR_IOC_ARG_BADTYPE: | |
480 | zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); | |
481 | break; | |
d8d418ff | 482 | case ZFS_ERR_WRONG_PARENT: |
483 | zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap); | |
484 | break; | |
8fb79fdd AJ |
485 | case ZFS_ERR_BADPROP: |
486 | zfs_verror(hdl, EZFS_BADPROP, fmt, ap); | |
487 | break; | |
34dc7c2f | 488 | default: |
f00f4690 | 489 | zfs_error_aux(hdl, "%s", strerror(error)); |
34dc7c2f BB |
490 | zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); |
491 | break; | |
492 | } | |
493 | ||
494 | va_end(ap); | |
495 | return (-1); | |
496 | } | |
497 | ||
0ce2de63 AS |
498 | void |
499 | zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, | |
500 | char *errbuf) | |
501 | { | |
502 | switch (err) { | |
503 | ||
504 | case ENOSPC: | |
505 | /* | |
506 | * For quotas and reservations, ENOSPC indicates | |
507 | * something different; setting a quota or reservation | |
508 | * doesn't use any disk space. | |
509 | */ | |
510 | switch (prop) { | |
511 | case ZFS_PROP_QUOTA: | |
512 | case ZFS_PROP_REFQUOTA: | |
513 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
514 | "size is less than current used or " | |
515 | "reserved space")); | |
516 | (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); | |
517 | break; | |
518 | ||
519 | case ZFS_PROP_RESERVATION: | |
520 | case ZFS_PROP_REFRESERVATION: | |
521 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
522 | "size is greater than available space")); | |
523 | (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); | |
524 | break; | |
525 | ||
526 | default: | |
527 | (void) zfs_standard_error(hdl, err, errbuf); | |
528 | break; | |
529 | } | |
530 | break; | |
531 | ||
532 | case EBUSY: | |
533 | (void) zfs_standard_error(hdl, EBUSY, errbuf); | |
534 | break; | |
535 | ||
536 | case EROFS: | |
537 | (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); | |
538 | break; | |
539 | ||
540 | case E2BIG: | |
541 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
542 | "property value too long")); | |
543 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
544 | break; | |
545 | ||
546 | case ENOTSUP: | |
547 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
548 | "pool and or dataset must be upgraded to set this " | |
549 | "property or value")); | |
550 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
551 | break; | |
552 | ||
553 | case ERANGE: | |
554 | if (prop == ZFS_PROP_COMPRESSION || | |
555 | prop == ZFS_PROP_DNODESIZE || | |
556 | prop == ZFS_PROP_RECORDSIZE) { | |
557 | (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
558 | "property setting is not allowed on " | |
559 | "bootable datasets")); | |
560 | (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); | |
561 | } else if (prop == ZFS_PROP_CHECKSUM || | |
562 | prop == ZFS_PROP_DEDUP) { | |
563 | (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
564 | "property setting is not allowed on " | |
565 | "root pools")); | |
566 | (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); | |
567 | } else { | |
568 | (void) zfs_standard_error(hdl, err, errbuf); | |
569 | } | |
570 | break; | |
571 | ||
572 | case EINVAL: | |
573 | if (prop == ZPROP_INVAL) { | |
574 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
575 | } else { | |
576 | (void) zfs_standard_error(hdl, err, errbuf); | |
577 | } | |
578 | break; | |
579 | ||
8fb79fdd AJ |
580 | case ZFS_ERR_BADPROP: |
581 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
582 | break; | |
583 | ||
0ce2de63 AS |
584 | case EACCES: |
585 | if (prop == ZFS_PROP_KEYLOCATION) { | |
586 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
587 | "keylocation may only be set on encryption roots")); | |
588 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
589 | } else { | |
590 | (void) zfs_standard_error(hdl, err, errbuf); | |
591 | } | |
592 | break; | |
593 | ||
594 | case EOVERFLOW: | |
595 | /* | |
596 | * This platform can't address a volume this big. | |
597 | */ | |
598 | #ifdef _ILP32 | |
599 | if (prop == ZFS_PROP_VOLSIZE) { | |
600 | (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); | |
601 | break; | |
602 | } | |
9a70e97f | 603 | zfs_fallthrough; |
0ce2de63 | 604 | #endif |
0ce2de63 AS |
605 | default: |
606 | (void) zfs_standard_error(hdl, err, errbuf); | |
607 | } | |
608 | } | |
609 | ||
34dc7c2f BB |
610 | int |
611 | zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) | |
612 | { | |
613 | return (zpool_standard_error_fmt(hdl, error, "%s", msg)); | |
614 | } | |
615 | ||
34dc7c2f BB |
616 | int |
617 | zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) | |
618 | { | |
619 | va_list ap; | |
620 | ||
621 | va_start(ap, fmt); | |
622 | ||
623 | if (zfs_common_error(hdl, error, fmt, ap) != 0) { | |
624 | va_end(ap); | |
625 | return (-1); | |
626 | } | |
627 | ||
628 | switch (error) { | |
629 | case ENODEV: | |
630 | zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); | |
631 | break; | |
632 | ||
633 | case ENOENT: | |
634 | zfs_error_aux(hdl, | |
635 | dgettext(TEXT_DOMAIN, "no such pool or dataset")); | |
636 | zfs_verror(hdl, EZFS_NOENT, fmt, ap); | |
637 | break; | |
638 | ||
639 | case EEXIST: | |
640 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
641 | "pool already exists")); | |
642 | zfs_verror(hdl, EZFS_EXISTS, fmt, ap); | |
643 | break; | |
644 | ||
645 | case EBUSY: | |
646 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); | |
b128c09f | 647 | zfs_verror(hdl, EZFS_BUSY, fmt, ap); |
34dc7c2f BB |
648 | break; |
649 | ||
a1d477c2 MA |
650 | /* There is no pending operation to cancel */ |
651 | case ENOTACTIVE: | |
652 | zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap); | |
653 | break; | |
654 | ||
34dc7c2f BB |
655 | case ENXIO: |
656 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
657 | "one or more devices is currently unavailable")); | |
658 | zfs_verror(hdl, EZFS_BADDEV, fmt, ap); | |
659 | break; | |
660 | ||
661 | case ENAMETOOLONG: | |
662 | zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); | |
663 | break; | |
664 | ||
665 | case ENOTSUP: | |
666 | zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); | |
667 | break; | |
668 | ||
669 | case EINVAL: | |
670 | zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); | |
671 | break; | |
672 | ||
673 | case ENOSPC: | |
674 | case EDQUOT: | |
675 | zfs_verror(hdl, EZFS_NOSPC, fmt, ap); | |
676 | return (-1); | |
572e2857 | 677 | |
9babb374 BB |
678 | case EAGAIN: |
679 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
680 | "pool I/O is currently suspended")); | |
681 | zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); | |
682 | break; | |
34dc7c2f | 683 | |
572e2857 BB |
684 | case EROFS: |
685 | zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); | |
686 | break; | |
385f9691 D |
687 | case EDOM: |
688 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
689 | "block size out of range or does not match")); | |
690 | zfs_verror(hdl, EZFS_BADPROP, fmt, ap); | |
691 | break; | |
379ca9cf OF |
692 | case EREMOTEIO: |
693 | zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); | |
694 | break; | |
d2734cce SD |
695 | case ZFS_ERR_CHECKPOINT_EXISTS: |
696 | zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap); | |
697 | break; | |
698 | case ZFS_ERR_DISCARDING_CHECKPOINT: | |
699 | zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap); | |
700 | break; | |
701 | case ZFS_ERR_NO_CHECKPOINT: | |
702 | zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap); | |
703 | break; | |
704 | case ZFS_ERR_DEVRM_IN_PROGRESS: | |
705 | zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap); | |
706 | break; | |
707 | case ZFS_ERR_VDEV_TOO_BIG: | |
708 | zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap); | |
709 | break; | |
43a85362 SD |
710 | case ZFS_ERR_EXPORT_IN_PROGRESS: |
711 | zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap); | |
712 | break; | |
9a49d3f3 BB |
713 | case ZFS_ERR_RESILVER_IN_PROGRESS: |
714 | zfs_verror(hdl, EZFS_RESILVERING, fmt, ap); | |
715 | break; | |
716 | case ZFS_ERR_REBUILD_IN_PROGRESS: | |
717 | zfs_verror(hdl, EZFS_REBUILDING, fmt, ap); | |
718 | break; | |
8fb79fdd AJ |
719 | case ZFS_ERR_BADPROP: |
720 | zfs_verror(hdl, EZFS_BADPROP, fmt, ap); | |
721 | break; | |
2a673e76 AJ |
722 | case ZFS_ERR_VDEV_NOTSUP: |
723 | zfs_verror(hdl, EZFS_VDEV_NOTSUP, fmt, ap); | |
724 | break; | |
b83a0e2d DB |
725 | case ZFS_ERR_IOC_CMD_UNAVAIL: |
726 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " | |
727 | "module does not support this operation. A reboot may " | |
728 | "be required to enable this operation.")); | |
729 | zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); | |
730 | break; | |
731 | case ZFS_ERR_IOC_ARG_UNAVAIL: | |
732 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " | |
733 | "module does not support an option for this operation. " | |
734 | "A reboot may be required to enable this option.")); | |
735 | zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); | |
736 | break; | |
737 | case ZFS_ERR_IOC_ARG_REQUIRED: | |
738 | case ZFS_ERR_IOC_ARG_BADTYPE: | |
739 | zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); | |
740 | break; | |
34dc7c2f | 741 | default: |
f00f4690 | 742 | zfs_error_aux(hdl, "%s", strerror(error)); |
34dc7c2f BB |
743 | zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); |
744 | } | |
745 | ||
746 | va_end(ap); | |
747 | return (-1); | |
748 | } | |
749 | ||
750 | /* | |
751 | * Display an out of memory error message and abort the current program. | |
752 | */ | |
753 | int | |
754 | no_memory(libzfs_handle_t *hdl) | |
755 | { | |
756 | return (zfs_error(hdl, EZFS_NOMEM, "internal error")); | |
757 | } | |
758 | ||
759 | /* | |
760 | * A safe form of malloc() which will die if the allocation fails. | |
761 | */ | |
762 | void * | |
763 | zfs_alloc(libzfs_handle_t *hdl, size_t size) | |
764 | { | |
765 | void *data; | |
766 | ||
767 | if ((data = calloc(1, size)) == NULL) | |
768 | (void) no_memory(hdl); | |
769 | ||
770 | return (data); | |
771 | } | |
772 | ||
572e2857 BB |
773 | /* |
774 | * A safe form of asprintf() which will die if the allocation fails. | |
775 | */ | |
572e2857 BB |
776 | char * |
777 | zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) | |
778 | { | |
779 | va_list ap; | |
780 | char *ret; | |
781 | int err; | |
782 | ||
783 | va_start(ap, fmt); | |
784 | ||
785 | err = vasprintf(&ret, fmt, ap); | |
786 | ||
787 | va_end(ap); | |
788 | ||
2babd200 | 789 | if (err < 0) { |
572e2857 | 790 | (void) no_memory(hdl); |
2babd200 AZ |
791 | ret = NULL; |
792 | } | |
572e2857 BB |
793 | |
794 | return (ret); | |
795 | } | |
796 | ||
34dc7c2f BB |
797 | /* |
798 | * A safe form of realloc(), which also zeroes newly allocated space. | |
799 | */ | |
800 | void * | |
801 | zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) | |
802 | { | |
803 | void *ret; | |
804 | ||
805 | if ((ret = realloc(ptr, newsize)) == NULL) { | |
806 | (void) no_memory(hdl); | |
34dc7c2f BB |
807 | return (NULL); |
808 | } | |
809 | ||
861166b0 | 810 | memset((char *)ret + oldsize, 0, newsize - oldsize); |
34dc7c2f BB |
811 | return (ret); |
812 | } | |
813 | ||
814 | /* | |
815 | * A safe form of strdup() which will die if the allocation fails. | |
816 | */ | |
817 | char * | |
818 | zfs_strdup(libzfs_handle_t *hdl, const char *str) | |
819 | { | |
820 | char *ret; | |
821 | ||
822 | if ((ret = strdup(str)) == NULL) | |
823 | (void) no_memory(hdl); | |
824 | ||
825 | return (ret); | |
826 | } | |
827 | ||
34dc7c2f BB |
828 | void |
829 | libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) | |
830 | { | |
831 | hdl->libzfs_printerr = printerr; | |
832 | } | |
833 | ||
d6418de0 TH |
834 | /* |
835 | * Read lines from an open file descriptor and store them in an array of | |
836 | * strings until EOF. lines[] will be allocated and populated with all the | |
837 | * lines read. All newlines are replaced with NULL terminators for | |
838 | * convenience. lines[] must be freed after use with libzfs_free_str_array(). | |
839 | * | |
840 | * Returns the number of lines read. | |
841 | */ | |
842 | static int | |
843 | libzfs_read_stdout_from_fd(int fd, char **lines[]) | |
844 | { | |
845 | ||
846 | FILE *fp; | |
847 | int lines_cnt = 0; | |
848 | size_t len = 0; | |
849 | char *line = NULL; | |
850 | char **tmp_lines = NULL, **tmp; | |
d6418de0 TH |
851 | |
852 | fp = fdopen(fd, "r"); | |
5da63539 AZ |
853 | if (fp == NULL) { |
854 | close(fd); | |
d6418de0 | 855 | return (0); |
5da63539 | 856 | } |
7c20ceeb | 857 | while (getline(&line, &len, fp) != -1) { |
d6418de0 TH |
858 | tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1)); |
859 | if (tmp == NULL) { | |
860 | /* Return the lines we were able to process */ | |
861 | break; | |
862 | } | |
863 | tmp_lines = tmp; | |
864 | ||
7c20ceeb AZ |
865 | /* Remove newline if not EOF */ |
866 | if (line[strlen(line) - 1] == '\n') | |
867 | line[strlen(line) - 1] = '\0'; | |
868 | ||
869 | tmp_lines[lines_cnt] = strdup(line); | |
870 | if (tmp_lines[lines_cnt] == NULL) | |
871 | break; | |
872 | ++lines_cnt; | |
d6418de0 | 873 | } |
7c20ceeb | 874 | free(line); |
d6418de0 TH |
875 | fclose(fp); |
876 | *lines = tmp_lines; | |
877 | return (lines_cnt); | |
878 | } | |
879 | ||
880 | static int | |
881 | libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags, | |
882 | char **lines[], int *lines_cnt) | |
4b1abce9 NB |
883 | { |
884 | pid_t pid; | |
65037d9b | 885 | int error, devnull_fd; |
d6418de0 TH |
886 | int link[2]; |
887 | ||
888 | /* | |
889 | * Setup a pipe between our child and parent process if we're | |
890 | * reading stdout. | |
891 | */ | |
30dadd5c | 892 | if (lines != NULL && pipe2(link, O_NONBLOCK | O_CLOEXEC) == -1) |
64b2e7d7 | 893 | return (-EPIPE); |
4b1abce9 | 894 | |
93ef5003 | 895 | pid = fork(); |
4b1abce9 | 896 | if (pid == 0) { |
d6418de0 | 897 | /* Child process */ |
10b575d0 | 898 | devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC); |
3132cb39 GB |
899 | |
900 | if (devnull_fd < 0) | |
901 | _exit(-1); | |
902 | ||
d6418de0 | 903 | if (!(flags & STDOUT_VERBOSE) && (lines == NULL)) |
3132cb39 | 904 | (void) dup2(devnull_fd, STDOUT_FILENO); |
d6418de0 TH |
905 | else if (lines != NULL) { |
906 | /* Save the output to lines[] */ | |
907 | dup2(link[1], STDOUT_FILENO); | |
d6418de0 | 908 | } |
9ac97c2a BB |
909 | |
910 | if (!(flags & STDERR_VERBOSE)) | |
3132cb39 GB |
911 | (void) dup2(devnull_fd, STDERR_FILENO); |
912 | ||
d6418de0 TH |
913 | if (flags & NO_DEFAULT_PATH) { |
914 | if (env == NULL) | |
915 | execv(path, argv); | |
916 | else | |
917 | execve(path, argv, env); | |
918 | } else { | |
919 | if (env == NULL) | |
920 | execvp(path, argv); | |
921 | else | |
922 | execvpe(path, argv, env); | |
923 | } | |
924 | ||
4b1abce9 NB |
925 | _exit(-1); |
926 | } else if (pid > 0) { | |
d6418de0 | 927 | /* Parent process */ |
4b1abce9 NB |
928 | int status; |
929 | ||
65037d9b | 930 | while ((error = waitpid(pid, &status, 0)) == -1 && |
30dadd5c AZ |
931 | errno == EINTR) |
932 | ; | |
65037d9b | 933 | if (error < 0 || !WIFEXITED(status)) |
d1d7e268 | 934 | return (-1); |
4b1abce9 | 935 | |
d6418de0 TH |
936 | if (lines != NULL) { |
937 | close(link[1]); | |
938 | *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines); | |
939 | } | |
d1d7e268 | 940 | return (WEXITSTATUS(status)); |
4b1abce9 NB |
941 | } |
942 | ||
d1d7e268 | 943 | return (-1); |
4b1abce9 NB |
944 | } |
945 | ||
d6418de0 TH |
946 | int |
947 | libzfs_run_process(const char *path, char *argv[], int flags) | |
948 | { | |
949 | return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL)); | |
950 | } | |
951 | ||
952 | /* | |
953 | * Run a command and store its stdout lines in an array of strings (lines[]). | |
954 | * lines[] is allocated and populated for you, and the number of lines is set in | |
955 | * lines_cnt. lines[] must be freed after use with libzfs_free_str_array(). | |
956 | * All newlines (\n) in lines[] are terminated for convenience. | |
957 | */ | |
958 | int | |
959 | libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[], | |
960 | char **lines[], int *lines_cnt) | |
961 | { | |
962 | return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt)); | |
963 | } | |
964 | ||
965 | /* | |
966 | * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This | |
967 | * means that *path needs to be the full path to the executable. | |
968 | */ | |
969 | int | |
970 | libzfs_run_process_get_stdout_nopath(const char *path, char *argv[], | |
971 | char *env[], char **lines[], int *lines_cnt) | |
972 | { | |
973 | return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH, | |
974 | lines, lines_cnt)); | |
975 | } | |
976 | ||
977 | /* | |
978 | * Free an array of strings. Free both the strings contained in the array and | |
979 | * the array itself. | |
980 | */ | |
981 | void | |
982 | libzfs_free_str_array(char **strs, int count) | |
983 | { | |
984 | while (--count >= 0) | |
985 | free(strs[count]); | |
986 | ||
987 | free(strs); | |
988 | } | |
989 | ||
990 | /* | |
991 | * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or | |
992 | * a non-zero number. | |
993 | * | |
994 | * Returns 0 otherwise. | |
995 | */ | |
996 | int | |
997 | libzfs_envvar_is_set(char *envvar) | |
998 | { | |
999 | char *env = getenv(envvar); | |
1000 | if (env && (strtoul(env, NULL, 0) > 0 || | |
1001 | (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) || | |
1002 | (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2))) | |
1003 | return (1); | |
1004 | ||
1005 | return (0); | |
1006 | } | |
1007 | ||
34dc7c2f BB |
1008 | libzfs_handle_t * |
1009 | libzfs_init(void) | |
1010 | { | |
1011 | libzfs_handle_t *hdl; | |
87abfcba | 1012 | int error; |
7a6c12fd | 1013 | char *env; |
34dc7c2f | 1014 | |
e191b60d | 1015 | if ((error = libzfs_load_module()) != 0) { |
65037d9b | 1016 | errno = error; |
4b1abce9 NB |
1017 | return (NULL); |
1018 | } | |
1019 | ||
572e2857 | 1020 | if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { |
34dc7c2f BB |
1021 | return (NULL); |
1022 | } | |
1023 | ||
c14ca145 JK |
1024 | if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) { |
1025 | free(hdl); | |
1026 | return (NULL); | |
1027 | } | |
1028 | ||
92ffd87a | 1029 | if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) { |
34dc7c2f BB |
1030 | free(hdl); |
1031 | return (NULL); | |
1032 | } | |
1033 | ||
6f1ffb06 MA |
1034 | if (libzfs_core_init() != 0) { |
1035 | (void) close(hdl->libzfs_fd); | |
6f1ffb06 MA |
1036 | free(hdl); |
1037 | return (NULL); | |
1038 | } | |
1039 | ||
34dc7c2f BB |
1040 | zfs_prop_init(); |
1041 | zpool_prop_init(); | |
9ae529ec | 1042 | zpool_feature_init(); |
2a673e76 | 1043 | vdev_prop_init(); |
9babb374 | 1044 | libzfs_mnttab_init(hdl); |
dc03fa30 | 1045 | fletcher_4_init(); |
34dc7c2f | 1046 | |
d99a0153 CW |
1047 | if (getenv("ZFS_PROP_DEBUG") != NULL) { |
1048 | hdl->libzfs_prop_debug = B_TRUE; | |
1049 | } | |
7a6c12fd AJ |
1050 | if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) { |
1051 | if ((error = zfs_nicestrtonum(hdl, env, | |
1052 | &hdl->libzfs_max_nvlist))) { | |
1053 | errno = error; | |
908d43d0 | 1054 | (void) close(hdl->libzfs_fd); |
908d43d0 | 1055 | free(hdl); |
7a6c12fd AJ |
1056 | return (NULL); |
1057 | } | |
1058 | } else { | |
1059 | hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4); | |
1060 | } | |
d99a0153 | 1061 | |
e8bcb693 DB |
1062 | /* |
1063 | * For testing, remove some settable properties and features | |
1064 | */ | |
1065 | if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) { | |
1066 | zprop_desc_t *proptbl; | |
1067 | ||
1068 | proptbl = zpool_prop_get_table(); | |
1069 | proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE; | |
1070 | ||
1071 | proptbl = zfs_prop_get_table(); | |
1072 | proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE; | |
1073 | ||
1074 | zfeature_info_t *ftbl = spa_feature_table; | |
1075 | ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE; | |
1076 | } | |
1077 | ||
34dc7c2f BB |
1078 | return (hdl); |
1079 | } | |
1080 | ||
1081 | void | |
1082 | libzfs_fini(libzfs_handle_t *hdl) | |
1083 | { | |
1084 | (void) close(hdl->libzfs_fd); | |
b128c09f | 1085 | zpool_free_handles(hdl); |
34dc7c2f | 1086 | namespace_clear(hdl); |
9babb374 | 1087 | libzfs_mnttab_fini(hdl); |
6f1ffb06 | 1088 | libzfs_core_fini(); |
c14ca145 | 1089 | regfree(&hdl->libzfs_urire); |
dc03fa30 | 1090 | fletcher_4_fini(); |
37086897 AZ |
1091 | #if LIBFETCH_DYNAMIC |
1092 | if (hdl->libfetch != (void *)-1 && hdl->libfetch != NULL) | |
1093 | (void) dlclose(hdl->libfetch); | |
1094 | free(hdl->libfetch_load_error); | |
1095 | #endif | |
34dc7c2f BB |
1096 | free(hdl); |
1097 | } | |
1098 | ||
1099 | libzfs_handle_t * | |
1100 | zpool_get_handle(zpool_handle_t *zhp) | |
1101 | { | |
1102 | return (zhp->zpool_hdl); | |
1103 | } | |
1104 | ||
1105 | libzfs_handle_t * | |
1106 | zfs_get_handle(zfs_handle_t *zhp) | |
1107 | { | |
1108 | return (zhp->zfs_hdl); | |
1109 | } | |
1110 | ||
b128c09f BB |
1111 | zpool_handle_t * |
1112 | zfs_get_pool_handle(const zfs_handle_t *zhp) | |
1113 | { | |
1114 | return (zhp->zpool_hdl); | |
1115 | } | |
1116 | ||
34dc7c2f BB |
1117 | /* |
1118 | * Given a name, determine whether or not it's a valid path | |
1119 | * (starts with '/' or "./"). If so, walk the mnttab trying | |
1120 | * to match the device number. If not, treat the path as an | |
aeacdefe | 1121 | * fs/vol/snap/bkmark name. |
34dc7c2f BB |
1122 | */ |
1123 | zfs_handle_t * | |
b197457c | 1124 | zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype) |
34dc7c2f BB |
1125 | { |
1126 | struct stat64 statbuf; | |
1127 | struct extmnttab entry; | |
34dc7c2f BB |
1128 | |
1129 | if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { | |
1130 | /* | |
1131 | * It's not a valid path, assume it's a name of type 'argtype'. | |
1132 | */ | |
1133 | return (zfs_open(hdl, path, argtype)); | |
1134 | } | |
1135 | ||
d31277ab | 1136 | if (getextmntent(path, &entry, &statbuf) != 0) |
34dc7c2f | 1137 | return (NULL); |
34dc7c2f BB |
1138 | |
1139 | if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { | |
1140 | (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), | |
1141 | path); | |
1142 | return (NULL); | |
1143 | } | |
1144 | ||
1145 | return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from | |
1150 | * an ioctl(). | |
1151 | */ | |
1152 | int | |
1153 | zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) | |
1154 | { | |
1155 | if (len == 0) | |
85ce79bb | 1156 | len = 256 * 1024; |
34dc7c2f | 1157 | zc->zc_nvlist_dst_size = len; |
23de906c CW |
1158 | zc->zc_nvlist_dst = |
1159 | (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); | |
1160 | if (zc->zc_nvlist_dst == 0) | |
34dc7c2f BB |
1161 | return (-1); |
1162 | ||
1163 | return (0); | |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will | |
1168 | * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was | |
1169 | * filled in by the kernel to indicate the actual required size. | |
1170 | */ | |
1171 | int | |
1172 | zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) | |
1173 | { | |
1174 | free((void *)(uintptr_t)zc->zc_nvlist_dst); | |
23de906c CW |
1175 | zc->zc_nvlist_dst = |
1176 | (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); | |
1177 | if (zc->zc_nvlist_dst == 0) | |
34dc7c2f BB |
1178 | return (-1); |
1179 | ||
1180 | return (0); | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | * Called to free the src and dst nvlists stored in the command structure. | |
1185 | */ | |
1186 | void | |
1187 | zcmd_free_nvlists(zfs_cmd_t *zc) | |
1188 | { | |
1189 | free((void *)(uintptr_t)zc->zc_nvlist_conf); | |
1190 | free((void *)(uintptr_t)zc->zc_nvlist_src); | |
1191 | free((void *)(uintptr_t)zc->zc_nvlist_dst); | |
23de906c CW |
1192 | zc->zc_nvlist_conf = 0; |
1193 | zc->zc_nvlist_src = 0; | |
1194 | zc->zc_nvlist_dst = 0; | |
34dc7c2f BB |
1195 | } |
1196 | ||
1197 | static int | |
1198 | zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, | |
1199 | nvlist_t *nvl) | |
1200 | { | |
1201 | char *packed; | |
34dc7c2f | 1202 | |
8bb9ecf4 | 1203 | size_t len = fnvlist_size(nvl); |
34dc7c2f BB |
1204 | if ((packed = zfs_alloc(hdl, len)) == NULL) |
1205 | return (-1); | |
1206 | ||
1207 | verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); | |
1208 | ||
1209 | *outnv = (uint64_t)(uintptr_t)packed; | |
1210 | *outlen = len; | |
1211 | ||
1212 | return (0); | |
1213 | } | |
1214 | ||
1215 | int | |
1216 | zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) | |
1217 | { | |
1218 | return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, | |
1219 | &zc->zc_nvlist_conf_size, nvl)); | |
1220 | } | |
1221 | ||
1222 | int | |
1223 | zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) | |
1224 | { | |
1225 | return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, | |
1226 | &zc->zc_nvlist_src_size, nvl)); | |
1227 | } | |
1228 | ||
1229 | /* | |
1230 | * Unpacks an nvlist from the ZFS ioctl command structure. | |
1231 | */ | |
1232 | int | |
1233 | zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) | |
1234 | { | |
1235 | if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, | |
1236 | zc->zc_nvlist_dst_size, nvlp, 0) != 0) | |
1237 | return (no_memory(hdl)); | |
1238 | ||
1239 | return (0); | |
1240 | } | |
1241 | ||
34dc7c2f BB |
1242 | /* |
1243 | * ================================================================ | |
1244 | * API shared by zfs and zpool property management | |
1245 | * ================================================================ | |
1246 | */ | |
1247 | ||
1248 | static void | |
1249 | zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) | |
1250 | { | |
1251 | zprop_list_t *pl = cbp->cb_proplist; | |
1252 | int i; | |
1253 | char *title; | |
1254 | size_t len; | |
1255 | ||
1256 | cbp->cb_first = B_FALSE; | |
1257 | if (cbp->cb_scripted) | |
1258 | return; | |
1259 | ||
1260 | /* | |
1261 | * Start with the length of the column headers. | |
1262 | */ | |
1263 | cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); | |
1264 | cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, | |
1265 | "PROPERTY")); | |
1266 | cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, | |
1267 | "VALUE")); | |
428870ff BB |
1268 | cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, |
1269 | "RECEIVED")); | |
34dc7c2f BB |
1270 | cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, |
1271 | "SOURCE")); | |
1272 | ||
fb5f0bc8 BB |
1273 | /* first property is always NAME */ |
1274 | assert(cbp->cb_proplist->pl_prop == | |
2a673e76 AJ |
1275 | ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : |
1276 | ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME))); | |
fb5f0bc8 | 1277 | |
34dc7c2f BB |
1278 | /* |
1279 | * Go through and calculate the widths for each column. For the | |
1280 | * 'source' column, we kludge it up by taking the worst-case scenario of | |
1281 | * inheriting from the longest name. This is acceptable because in the | |
1282 | * majority of cases 'SOURCE' is the last column displayed, and we don't | |
1283 | * use the width anyway. Note that the 'VALUE' column can be oversized, | |
428870ff | 1284 | * if the name of the property is much longer than any values we find. |
34dc7c2f BB |
1285 | */ |
1286 | for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { | |
1287 | /* | |
1288 | * 'PROPERTY' column | |
1289 | */ | |
1290 | if (pl->pl_prop != ZPROP_INVAL) { | |
1291 | const char *propname = (type == ZFS_TYPE_POOL) ? | |
1292 | zpool_prop_to_name(pl->pl_prop) : | |
2a673e76 AJ |
1293 | ((type == ZFS_TYPE_VDEV) ? |
1294 | vdev_prop_to_name(pl->pl_prop) : | |
1295 | zfs_prop_to_name(pl->pl_prop)); | |
34dc7c2f | 1296 | |
2a673e76 | 1297 | assert(propname != NULL); |
34dc7c2f BB |
1298 | len = strlen(propname); |
1299 | if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) | |
1300 | cbp->cb_colwidths[GET_COL_PROPERTY] = len; | |
1301 | } else { | |
2a673e76 | 1302 | assert(pl->pl_user_prop != NULL); |
34dc7c2f BB |
1303 | len = strlen(pl->pl_user_prop); |
1304 | if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) | |
1305 | cbp->cb_colwidths[GET_COL_PROPERTY] = len; | |
1306 | } | |
1307 | ||
1308 | /* | |
fb5f0bc8 BB |
1309 | * 'VALUE' column. The first property is always the 'name' |
1310 | * property that was tacked on either by /sbin/zfs's | |
1311 | * zfs_do_get() or when calling zprop_expand_list(), so we | |
1312 | * ignore its width. If the user specified the name property | |
1313 | * to display, then it will be later in the list in any case. | |
34dc7c2f | 1314 | */ |
fb5f0bc8 | 1315 | if (pl != cbp->cb_proplist && |
34dc7c2f BB |
1316 | pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) |
1317 | cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; | |
1318 | ||
428870ff BB |
1319 | /* 'RECEIVED' column. */ |
1320 | if (pl != cbp->cb_proplist && | |
1321 | pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) | |
1322 | cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; | |
1323 | ||
34dc7c2f BB |
1324 | /* |
1325 | * 'NAME' and 'SOURCE' columns | |
1326 | */ | |
2a673e76 AJ |
1327 | if (pl->pl_prop == ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : |
1328 | ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : | |
1329 | ZFS_PROP_NAME)) && pl->pl_width > | |
1330 | cbp->cb_colwidths[GET_COL_NAME]) { | |
34dc7c2f BB |
1331 | cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; |
1332 | cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + | |
1333 | strlen(dgettext(TEXT_DOMAIN, "inherited from")); | |
1334 | } | |
1335 | } | |
1336 | ||
1337 | /* | |
1338 | * Now go through and print the headers. | |
1339 | */ | |
428870ff | 1340 | for (i = 0; i < ZFS_GET_NCOLS; i++) { |
34dc7c2f BB |
1341 | switch (cbp->cb_columns[i]) { |
1342 | case GET_COL_NAME: | |
1343 | title = dgettext(TEXT_DOMAIN, "NAME"); | |
1344 | break; | |
1345 | case GET_COL_PROPERTY: | |
1346 | title = dgettext(TEXT_DOMAIN, "PROPERTY"); | |
1347 | break; | |
1348 | case GET_COL_VALUE: | |
1349 | title = dgettext(TEXT_DOMAIN, "VALUE"); | |
1350 | break; | |
428870ff BB |
1351 | case GET_COL_RECVD: |
1352 | title = dgettext(TEXT_DOMAIN, "RECEIVED"); | |
1353 | break; | |
34dc7c2f BB |
1354 | case GET_COL_SOURCE: |
1355 | title = dgettext(TEXT_DOMAIN, "SOURCE"); | |
1356 | break; | |
1357 | default: | |
1358 | title = NULL; | |
1359 | } | |
1360 | ||
1361 | if (title != NULL) { | |
428870ff BB |
1362 | if (i == (ZFS_GET_NCOLS - 1) || |
1363 | cbp->cb_columns[i + 1] == GET_COL_NONE) | |
34dc7c2f BB |
1364 | (void) printf("%s", title); |
1365 | else | |
1366 | (void) printf("%-*s ", | |
1367 | cbp->cb_colwidths[cbp->cb_columns[i]], | |
1368 | title); | |
1369 | } | |
1370 | } | |
1371 | (void) printf("\n"); | |
1372 | } | |
1373 | ||
1374 | /* | |
1375 | * Display a single line of output, according to the settings in the callback | |
1376 | * structure. | |
1377 | */ | |
1378 | void | |
1379 | zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, | |
1380 | const char *propname, const char *value, zprop_source_t sourcetype, | |
428870ff | 1381 | const char *source, const char *recvd_value) |
34dc7c2f BB |
1382 | { |
1383 | int i; | |
d4ed6673 | 1384 | const char *str = NULL; |
34dc7c2f BB |
1385 | char buf[128]; |
1386 | ||
1387 | /* | |
1388 | * Ignore those source types that the user has chosen to ignore. | |
1389 | */ | |
1390 | if ((sourcetype & cbp->cb_sources) == 0) | |
1391 | return; | |
1392 | ||
1393 | if (cbp->cb_first) | |
1394 | zprop_print_headers(cbp, cbp->cb_type); | |
1395 | ||
428870ff | 1396 | for (i = 0; i < ZFS_GET_NCOLS; i++) { |
34dc7c2f BB |
1397 | switch (cbp->cb_columns[i]) { |
1398 | case GET_COL_NAME: | |
1399 | str = name; | |
1400 | break; | |
1401 | ||
1402 | case GET_COL_PROPERTY: | |
1403 | str = propname; | |
1404 | break; | |
1405 | ||
1406 | case GET_COL_VALUE: | |
1407 | str = value; | |
1408 | break; | |
1409 | ||
1410 | case GET_COL_SOURCE: | |
1411 | switch (sourcetype) { | |
1412 | case ZPROP_SRC_NONE: | |
1413 | str = "-"; | |
1414 | break; | |
1415 | ||
1416 | case ZPROP_SRC_DEFAULT: | |
1417 | str = "default"; | |
1418 | break; | |
1419 | ||
1420 | case ZPROP_SRC_LOCAL: | |
1421 | str = "local"; | |
1422 | break; | |
1423 | ||
1424 | case ZPROP_SRC_TEMPORARY: | |
1425 | str = "temporary"; | |
1426 | break; | |
1427 | ||
1428 | case ZPROP_SRC_INHERITED: | |
1429 | (void) snprintf(buf, sizeof (buf), | |
1430 | "inherited from %s", source); | |
1431 | str = buf; | |
1432 | break; | |
428870ff BB |
1433 | case ZPROP_SRC_RECEIVED: |
1434 | str = "received"; | |
1435 | break; | |
23d70cde GM |
1436 | |
1437 | default: | |
1438 | str = NULL; | |
1439 | assert(!"unhandled zprop_source_t"); | |
34dc7c2f BB |
1440 | } |
1441 | break; | |
1442 | ||
428870ff BB |
1443 | case GET_COL_RECVD: |
1444 | str = (recvd_value == NULL ? "-" : recvd_value); | |
1445 | break; | |
1446 | ||
34dc7c2f BB |
1447 | default: |
1448 | continue; | |
1449 | } | |
1450 | ||
23827a4c G |
1451 | if (i == (ZFS_GET_NCOLS - 1) || |
1452 | cbp->cb_columns[i + 1] == GET_COL_NONE) | |
34dc7c2f BB |
1453 | (void) printf("%s", str); |
1454 | else if (cbp->cb_scripted) | |
1455 | (void) printf("%s\t", str); | |
1456 | else | |
1457 | (void) printf("%-*s ", | |
1458 | cbp->cb_colwidths[cbp->cb_columns[i]], | |
1459 | str); | |
34dc7c2f BB |
1460 | } |
1461 | ||
1462 | (void) printf("\n"); | |
1463 | } | |
1464 | ||
1465 | /* | |
1466 | * Given a numeric suffix, convert the value into a number of bits that the | |
1467 | * resulting value must be shifted. | |
1468 | */ | |
1469 | static int | |
1470 | str2shift(libzfs_handle_t *hdl, const char *buf) | |
1471 | { | |
1472 | const char *ends = "BKMGTPEZ"; | |
1473 | int i; | |
1474 | ||
1475 | if (buf[0] == '\0') | |
1476 | return (0); | |
1477 | for (i = 0; i < strlen(ends); i++) { | |
1478 | if (toupper(buf[0]) == ends[i]) | |
1479 | break; | |
1480 | } | |
1481 | if (i == strlen(ends)) { | |
53c2ec1d JL |
1482 | if (hdl) |
1483 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1484 | "invalid numeric suffix '%s'"), buf); | |
34dc7c2f BB |
1485 | return (-1); |
1486 | } | |
1487 | ||
1488 | /* | |
826ab7ad RL |
1489 | * Allow 'G' = 'GB' = 'GiB', case-insensitively. |
1490 | * However, 'BB' and 'BiB' are disallowed. | |
34dc7c2f | 1491 | */ |
826ab7ad RL |
1492 | if (buf[1] == '\0' || |
1493 | (toupper(buf[0]) != 'B' && | |
d1d7e268 MK |
1494 | ((toupper(buf[1]) == 'B' && buf[2] == '\0') || |
1495 | (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' && | |
1496 | buf[3] == '\0')))) | |
1497 | return (10 * i); | |
34dc7c2f | 1498 | |
53c2ec1d JL |
1499 | if (hdl) |
1500 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1501 | "invalid numeric suffix '%s'"), buf); | |
34dc7c2f BB |
1502 | return (-1); |
1503 | } | |
1504 | ||
1505 | /* | |
1506 | * Convert a string of the form '100G' into a real number. Used when setting | |
1507 | * properties or creating a volume. 'buf' is used to place an extended error | |
1508 | * message for the caller to use. | |
1509 | */ | |
1510 | int | |
1511 | zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) | |
1512 | { | |
1513 | char *end; | |
1514 | int shift; | |
1515 | ||
1516 | *num = 0; | |
1517 | ||
1518 | /* Check to see if this looks like a number. */ | |
1519 | if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { | |
1520 | if (hdl) | |
1521 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1522 | "bad numeric value '%s'"), value); | |
1523 | return (-1); | |
1524 | } | |
1525 | ||
428870ff | 1526 | /* Rely on strtoull() to process the numeric portion. */ |
34dc7c2f | 1527 | errno = 0; |
d164b209 | 1528 | *num = strtoull(value, &end, 10); |
34dc7c2f BB |
1529 | |
1530 | /* | |
1531 | * Check for ERANGE, which indicates that the value is too large to fit | |
1532 | * in a 64-bit value. | |
1533 | */ | |
1534 | if (errno == ERANGE) { | |
1535 | if (hdl) | |
1536 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1537 | "numeric value is too large")); | |
1538 | return (-1); | |
1539 | } | |
1540 | ||
1541 | /* | |
1542 | * If we have a decimal value, then do the computation with floating | |
1543 | * point arithmetic. Otherwise, use standard arithmetic. | |
1544 | */ | |
1545 | if (*end == '.') { | |
1546 | double fval = strtod(value, &end); | |
1547 | ||
1548 | if ((shift = str2shift(hdl, end)) == -1) | |
1549 | return (-1); | |
1550 | ||
1551 | fval *= pow(2, shift); | |
1552 | ||
4d32abaa RM |
1553 | /* |
1554 | * UINT64_MAX is not exactly representable as a double. | |
1555 | * The closest representation is UINT64_MAX + 1, so we | |
1556 | * use a >= comparison instead of > for the bounds check. | |
1557 | */ | |
1558 | if (fval >= (double)UINT64_MAX) { | |
34dc7c2f BB |
1559 | if (hdl) |
1560 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1561 | "numeric value is too large")); | |
1562 | return (-1); | |
1563 | } | |
1564 | ||
1565 | *num = (uint64_t)fval; | |
1566 | } else { | |
1567 | if ((shift = str2shift(hdl, end)) == -1) | |
1568 | return (-1); | |
1569 | ||
1570 | /* Check for overflow */ | |
1571 | if (shift >= 64 || (*num << shift) >> shift != *num) { | |
1572 | if (hdl) | |
1573 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1574 | "numeric value is too large")); | |
1575 | return (-1); | |
1576 | } | |
1577 | ||
1578 | *num <<= shift; | |
1579 | } | |
1580 | ||
1581 | return (0); | |
1582 | } | |
1583 | ||
1584 | /* | |
1585 | * Given a propname=value nvpair to set, parse any numeric properties | |
1586 | * (index, boolean, etc) if they are specified as strings and add the | |
1587 | * resulting nvpair to the returned nvlist. | |
1588 | * | |
1589 | * At the DSL layer, all properties are either 64-bit numbers or strings. | |
1590 | * We want the user to be able to ignore this fact and specify properties | |
1591 | * as native values (numbers, for example) or as strings (to simplify | |
1592 | * command line utilities). This also handles converting index types | |
1593 | * (compression, checksum, etc) from strings to their on-disk index. | |
1594 | */ | |
1595 | int | |
1596 | zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, | |
1597 | zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, | |
1598 | const char *errbuf) | |
1599 | { | |
1600 | data_type_t datatype = nvpair_type(elem); | |
1601 | zprop_type_t proptype; | |
1602 | const char *propname; | |
1603 | char *value; | |
1604 | boolean_t isnone = B_FALSE; | |
d22f3a82 | 1605 | boolean_t isauto = B_FALSE; |
b6ca6193 | 1606 | int err = 0; |
34dc7c2f BB |
1607 | |
1608 | if (type == ZFS_TYPE_POOL) { | |
1609 | proptype = zpool_prop_get_type(prop); | |
1610 | propname = zpool_prop_to_name(prop); | |
2a673e76 AJ |
1611 | } else if (type == ZFS_TYPE_VDEV) { |
1612 | proptype = vdev_prop_get_type(prop); | |
1613 | propname = vdev_prop_to_name(prop); | |
34dc7c2f BB |
1614 | } else { |
1615 | proptype = zfs_prop_get_type(prop); | |
1616 | propname = zfs_prop_to_name(prop); | |
1617 | } | |
1618 | ||
1619 | /* | |
1620 | * Convert any properties to the internal DSL value types. | |
1621 | */ | |
1622 | *svalp = NULL; | |
1623 | *ivalp = 0; | |
1624 | ||
1625 | switch (proptype) { | |
1626 | case PROP_TYPE_STRING: | |
1627 | if (datatype != DATA_TYPE_STRING) { | |
1628 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1629 | "'%s' must be a string"), nvpair_name(elem)); | |
1630 | goto error; | |
1631 | } | |
b6ca6193 | 1632 | err = nvpair_value_string(elem, svalp); |
1633 | if (err != 0) { | |
1634 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1635 | "'%s' is invalid"), nvpair_name(elem)); | |
1636 | goto error; | |
1637 | } | |
34dc7c2f BB |
1638 | if (strlen(*svalp) >= ZFS_MAXPROPLEN) { |
1639 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1640 | "'%s' is too long"), nvpair_name(elem)); | |
1641 | goto error; | |
1642 | } | |
1643 | break; | |
1644 | ||
1645 | case PROP_TYPE_NUMBER: | |
1646 | if (datatype == DATA_TYPE_STRING) { | |
1647 | (void) nvpair_value_string(elem, &value); | |
1648 | if (strcmp(value, "none") == 0) { | |
1649 | isnone = B_TRUE; | |
d22f3a82 MG |
1650 | } else if (strcmp(value, "auto") == 0) { |
1651 | isauto = B_TRUE; | |
1652 | } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) { | |
34dc7c2f BB |
1653 | goto error; |
1654 | } | |
1655 | } else if (datatype == DATA_TYPE_UINT64) { | |
1656 | (void) nvpair_value_uint64(elem, ivalp); | |
1657 | } else { | |
1658 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1659 | "'%s' must be a number"), nvpair_name(elem)); | |
1660 | goto error; | |
1661 | } | |
1662 | ||
1663 | /* | |
1664 | * Quota special: force 'none' and don't allow 0. | |
1665 | */ | |
1666 | if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && | |
1667 | (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { | |
1668 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1669 | "use 'none' to disable quota/refquota")); | |
1670 | goto error; | |
1671 | } | |
788eb90c JJ |
1672 | |
1673 | /* | |
1674 | * Special handling for "*_limit=none". In this case it's not | |
1675 | * 0 but UINT64_MAX. | |
1676 | */ | |
1677 | if ((type & ZFS_TYPE_DATASET) && isnone && | |
1678 | (prop == ZFS_PROP_FILESYSTEM_LIMIT || | |
1679 | prop == ZFS_PROP_SNAPSHOT_LIMIT)) { | |
1680 | *ivalp = UINT64_MAX; | |
1681 | } | |
d22f3a82 MG |
1682 | |
1683 | /* | |
1684 | * Special handling for setting 'refreservation' to 'auto'. Use | |
1685 | * UINT64_MAX to tell the caller to use zfs_fix_auto_resv(). | |
1686 | * 'auto' is only allowed on volumes. | |
1687 | */ | |
1688 | if (isauto) { | |
1689 | switch (prop) { | |
1690 | case ZFS_PROP_REFRESERVATION: | |
1691 | if ((type & ZFS_TYPE_VOLUME) == 0) { | |
1692 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1693 | "'%s=auto' only allowed on " | |
1694 | "volumes"), nvpair_name(elem)); | |
1695 | goto error; | |
1696 | } | |
1697 | *ivalp = UINT64_MAX; | |
1698 | break; | |
1699 | default: | |
1700 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1701 | "'auto' is invalid value for '%s'"), | |
1702 | nvpair_name(elem)); | |
1703 | goto error; | |
1704 | } | |
1705 | } | |
1706 | ||
34dc7c2f BB |
1707 | break; |
1708 | ||
1709 | case PROP_TYPE_INDEX: | |
1710 | if (datatype != DATA_TYPE_STRING) { | |
1711 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1712 | "'%s' must be a string"), nvpair_name(elem)); | |
1713 | goto error; | |
1714 | } | |
1715 | ||
1716 | (void) nvpair_value_string(elem, &value); | |
1717 | ||
1718 | if (zprop_string_to_index(prop, value, ivalp, type) != 0) { | |
1719 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1720 | "'%s' must be one of '%s'"), propname, | |
1721 | zprop_values(prop, type)); | |
1722 | goto error; | |
1723 | } | |
1724 | break; | |
1725 | ||
1726 | default: | |
1727 | abort(); | |
1728 | } | |
1729 | ||
1730 | /* | |
1731 | * Add the result to our return set of properties. | |
1732 | */ | |
1733 | if (*svalp != NULL) { | |
1734 | if (nvlist_add_string(ret, propname, *svalp) != 0) { | |
1735 | (void) no_memory(hdl); | |
1736 | return (-1); | |
1737 | } | |
1738 | } else { | |
1739 | if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { | |
1740 | (void) no_memory(hdl); | |
1741 | return (-1); | |
1742 | } | |
1743 | } | |
1744 | ||
1745 | return (0); | |
1746 | error: | |
1747 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1748 | return (-1); | |
1749 | } | |
1750 | ||
b128c09f | 1751 | static int |
539d16c3 | 1752 | addlist(libzfs_handle_t *hdl, const char *propname, zprop_list_t **listp, |
b128c09f BB |
1753 | zfs_type_t type) |
1754 | { | |
539d16c3 | 1755 | int prop = zprop_name_to_prop(propname, type); |
962d5242 | 1756 | if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE)) |
b128c09f BB |
1757 | prop = ZPROP_INVAL; |
1758 | ||
1759 | /* | |
2a673e76 AJ |
1760 | * Return failure if no property table entry was found and this isn't |
1761 | * a user-defined property. | |
b128c09f | 1762 | */ |
9ae529ec CS |
1763 | if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL && |
1764 | !zpool_prop_feature(propname) && | |
1765 | !zpool_prop_unsupported(propname)) || | |
2a673e76 AJ |
1766 | ((type == ZFS_TYPE_DATASET) && !zfs_prop_user(propname) && |
1767 | !zfs_prop_userquota(propname) && !zfs_prop_written(propname)) || | |
1768 | ((type == ZFS_TYPE_VDEV) && !vdev_prop_user(propname)))) { | |
b128c09f BB |
1769 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1770 | "invalid property '%s'"), propname); | |
1771 | return (zfs_error(hdl, EZFS_BADPROP, | |
1772 | dgettext(TEXT_DOMAIN, "bad property list"))); | |
1773 | } | |
1774 | ||
539d16c3 AZ |
1775 | zprop_list_t *entry = zfs_alloc(hdl, sizeof (*entry)); |
1776 | ||
b128c09f BB |
1777 | entry->pl_prop = prop; |
1778 | if (prop == ZPROP_INVAL) { | |
f4826a2d | 1779 | entry->pl_user_prop = zfs_strdup(hdl, propname); |
b128c09f BB |
1780 | entry->pl_width = strlen(propname); |
1781 | } else { | |
1782 | entry->pl_width = zprop_width(prop, &entry->pl_fixed, | |
1783 | type); | |
1784 | } | |
1785 | ||
1786 | *listp = entry; | |
1787 | ||
1788 | return (0); | |
1789 | } | |
1790 | ||
34dc7c2f BB |
1791 | /* |
1792 | * Given a comma-separated list of properties, construct a property list | |
1793 | * containing both user-defined and native properties. This function will | |
1794 | * return a NULL list if 'all' is specified, which can later be expanded | |
1795 | * by zprop_expand_list(). | |
1796 | */ | |
1797 | int | |
1798 | zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, | |
1799 | zfs_type_t type) | |
1800 | { | |
34dc7c2f | 1801 | *listp = NULL; |
34dc7c2f BB |
1802 | |
1803 | /* | |
1804 | * If 'all' is specified, return a NULL list. | |
1805 | */ | |
1806 | if (strcmp(props, "all") == 0) | |
1807 | return (0); | |
1808 | ||
1809 | /* | |
1810 | * If no props were specified, return an error. | |
1811 | */ | |
1812 | if (props[0] == '\0') { | |
1813 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1814 | "no properties specified")); | |
1815 | return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, | |
1816 | "bad property list"))); | |
1817 | } | |
1818 | ||
539d16c3 AZ |
1819 | for (char *p; (p = strsep(&props, ",")); ) |
1820 | if (strcmp(p, "space") == 0) { | |
1821 | static const char *const spaceprops[] = { | |
b128c09f BB |
1822 | "name", "avail", "used", "usedbysnapshots", |
1823 | "usedbydataset", "usedbyrefreservation", | |
539d16c3 | 1824 | "usedbychildren" |
b128c09f | 1825 | }; |
b128c09f | 1826 | |
539d16c3 | 1827 | for (int i = 0; i < ARRAY_SIZE(spaceprops); i++) { |
b128c09f BB |
1828 | if (addlist(hdl, spaceprops[i], listp, type)) |
1829 | return (-1); | |
1830 | listp = &(*listp)->pl_next; | |
34dc7c2f | 1831 | } |
34dc7c2f | 1832 | } else { |
539d16c3 | 1833 | if (addlist(hdl, p, listp, type)) |
b128c09f BB |
1834 | return (-1); |
1835 | listp = &(*listp)->pl_next; | |
34dc7c2f BB |
1836 | } |
1837 | ||
34dc7c2f BB |
1838 | return (0); |
1839 | } | |
1840 | ||
1841 | void | |
1842 | zprop_free_list(zprop_list_t *pl) | |
1843 | { | |
1844 | zprop_list_t *next; | |
1845 | ||
1846 | while (pl != NULL) { | |
1847 | next = pl->pl_next; | |
1848 | free(pl->pl_user_prop); | |
1849 | free(pl); | |
1850 | pl = next; | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | typedef struct expand_data { | |
1855 | zprop_list_t **last; | |
1856 | libzfs_handle_t *hdl; | |
1857 | zfs_type_t type; | |
1858 | } expand_data_t; | |
1859 | ||
65c7cc49 | 1860 | static int |
34dc7c2f BB |
1861 | zprop_expand_list_cb(int prop, void *cb) |
1862 | { | |
1863 | zprop_list_t *entry; | |
1864 | expand_data_t *edp = cb; | |
1865 | ||
1866 | if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) | |
1867 | return (ZPROP_INVAL); | |
1868 | ||
1869 | entry->pl_prop = prop; | |
1870 | entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); | |
1871 | entry->pl_all = B_TRUE; | |
1872 | ||
1873 | *(edp->last) = entry; | |
1874 | edp->last = &entry->pl_next; | |
1875 | ||
1876 | return (ZPROP_CONT); | |
1877 | } | |
1878 | ||
1879 | int | |
1880 | zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) | |
1881 | { | |
1882 | zprop_list_t *entry; | |
1883 | zprop_list_t **last; | |
1884 | expand_data_t exp; | |
1885 | ||
1886 | if (*plp == NULL) { | |
1887 | /* | |
1888 | * If this is the very first time we've been called for an 'all' | |
1889 | * specification, expand the list to include all native | |
1890 | * properties. | |
1891 | */ | |
1892 | last = plp; | |
1893 | ||
1894 | exp.last = last; | |
1895 | exp.hdl = hdl; | |
1896 | exp.type = type; | |
1897 | ||
1898 | if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, | |
1899 | B_FALSE, type) == ZPROP_INVAL) | |
1900 | return (-1); | |
1901 | ||
1902 | /* | |
1903 | * Add 'name' to the beginning of the list, which is handled | |
1904 | * specially. | |
1905 | */ | |
f4826a2d | 1906 | entry = zfs_alloc(hdl, sizeof (zprop_list_t)); |
2a673e76 AJ |
1907 | entry->pl_prop = ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : |
1908 | ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME)); | |
34dc7c2f BB |
1909 | entry->pl_width = zprop_width(entry->pl_prop, |
1910 | &entry->pl_fixed, type); | |
1911 | entry->pl_all = B_TRUE; | |
1912 | entry->pl_next = *plp; | |
1913 | *plp = entry; | |
1914 | } | |
1915 | return (0); | |
1916 | } | |
1917 | ||
1918 | int | |
1919 | zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, | |
1920 | zfs_type_t type) | |
1921 | { | |
1922 | return (zprop_iter_common(func, cb, show_all, ordered, type)); | |
1923 | } | |
50478c6d T |
1924 | |
1925 | /* | |
1926 | * Fill given version buffer with zfs userland version | |
1927 | */ | |
1928 | void | |
1929 | zfs_version_userland(char *version, int len) | |
1930 | { | |
1931 | (void) strlcpy(version, ZFS_META_ALIAS, len); | |
1932 | } | |
1933 | ||
50478c6d T |
1934 | /* |
1935 | * Prints both zfs userland and kernel versions | |
1936 | * Returns 0 on success, and -1 on error (with errno set) | |
1937 | */ | |
1938 | int | |
1939 | zfs_version_print(void) | |
1940 | { | |
1941 | char zver_userland[128]; | |
1942 | char zver_kernel[128]; | |
1943 | ||
2451a553 I |
1944 | zfs_version_userland(zver_userland, sizeof (zver_userland)); |
1945 | ||
1946 | (void) printf("%s\n", zver_userland); | |
1947 | ||
50478c6d T |
1948 | if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) { |
1949 | fprintf(stderr, "zfs_version_kernel() failed: %s\n", | |
1950 | strerror(errno)); | |
1951 | return (-1); | |
1952 | } | |
1953 | ||
50478c6d T |
1954 | (void) printf("zfs-kmod-%s\n", zver_kernel); |
1955 | ||
1956 | return (0); | |
1957 | } | |
9fb2771a TH |
1958 | |
1959 | /* | |
1960 | * Return 1 if the user requested ANSI color output, and our terminal supports | |
1961 | * it. Return 0 for no color. | |
1962 | */ | |
1963 | static int | |
1964 | use_color(void) | |
1965 | { | |
1966 | static int use_color = -1; | |
1967 | char *term; | |
1968 | ||
1969 | /* | |
1970 | * Optimization: | |
1971 | * | |
1972 | * For each zpool invocation, we do a single check to see if we should | |
1973 | * be using color or not, and cache that value for the lifetime of the | |
1974 | * the zpool command. That makes it cheap to call use_color() when | |
1975 | * we're printing with color. We assume that the settings are not going | |
1976 | * to change during the invocation of a zpool command (the user isn't | |
1977 | * going to change the ZFS_COLOR value while zpool is running, for | |
1978 | * example). | |
1979 | */ | |
1980 | if (use_color != -1) { | |
1981 | /* | |
1982 | * We've already figured out if we should be using color or | |
1983 | * not. Return the cached value. | |
1984 | */ | |
1985 | return (use_color); | |
1986 | } | |
1987 | ||
1988 | term = getenv("TERM"); | |
1989 | /* | |
1990 | * The user sets the ZFS_COLOR env var set to enable zpool ANSI color | |
1991 | * output. However if NO_COLOR is set (https://no-color.org/) then | |
1992 | * don't use it. Also, don't use color if terminal doesn't support | |
1993 | * it. | |
1994 | */ | |
1995 | if (libzfs_envvar_is_set("ZFS_COLOR") && | |
1996 | !libzfs_envvar_is_set("NO_COLOR") && | |
1997 | isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 && | |
1998 | strcmp("unknown", term) != 0) { | |
1999 | /* Color supported */ | |
2000 | use_color = 1; | |
2001 | } else { | |
2002 | use_color = 0; | |
2003 | } | |
2004 | ||
2005 | return (use_color); | |
2006 | } | |
2007 | ||
2008 | /* | |
2009 | * color_start() and color_end() are used for when you want to colorize a block | |
2010 | * of text. For example: | |
2011 | * | |
2012 | * color_start(ANSI_RED_FG) | |
2013 | * printf("hello"); | |
2014 | * printf("world"); | |
2015 | * color_end(); | |
2016 | */ | |
2017 | void | |
2018 | color_start(char *color) | |
2019 | { | |
2020 | if (use_color()) | |
2021 | printf("%s", color); | |
2022 | } | |
2023 | ||
2024 | void | |
2025 | color_end(void) | |
2026 | { | |
2027 | if (use_color()) | |
2028 | printf(ANSI_RESET); | |
2029 | } | |
2030 | ||
2031 | /* printf() with a color. If color is NULL, then do a normal printf. */ | |
2032 | int | |
2033 | printf_color(char *color, char *format, ...) | |
2034 | { | |
2035 | va_list aptr; | |
2036 | int rc; | |
2037 | ||
2038 | if (color) | |
2039 | color_start(color); | |
2040 | ||
2041 | va_start(aptr, format); | |
2042 | rc = vprintf(format, aptr); | |
2043 | va_end(aptr); | |
2044 | ||
2045 | if (color) | |
2046 | color_end(); | |
2047 | ||
2048 | return (rc); | |
2049 | } |