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