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