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