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