Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / lib / libzfs / common / libzfs_util.c
blob1260a08140a73a171cd17c8e8c477974ce7e228a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Internal utility routines for the ZFS library.
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <libintl.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <math.h>
40 #include <sys/mnttab.h>
41 #include <sys/mntent.h>
42 #include <sys/types.h>
44 #include <libzfs.h>
46 #include "libzfs_impl.h"
47 #include "zfs_prop.h"
49 int
50 libzfs_errno(libzfs_handle_t *hdl)
52 return (hdl->libzfs_error);
55 const char *
56 libzfs_error_action(libzfs_handle_t *hdl)
58 return (hdl->libzfs_action);
61 const char *
62 libzfs_error_description(libzfs_handle_t *hdl)
64 if (hdl->libzfs_desc[0] != '\0')
65 return (hdl->libzfs_desc);
67 switch (hdl->libzfs_error) {
68 case EZFS_NOMEM:
69 return (dgettext(TEXT_DOMAIN, "out of memory"));
70 case EZFS_BADPROP:
71 return (dgettext(TEXT_DOMAIN, "invalid property value"));
72 case EZFS_PROPREADONLY:
73 return (dgettext(TEXT_DOMAIN, "read only property"));
74 case EZFS_PROPTYPE:
75 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
76 "datasets of this type"));
77 case EZFS_PROPNONINHERIT:
78 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
79 case EZFS_PROPSPACE:
80 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
81 case EZFS_BADTYPE:
82 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
83 "datasets of this type"));
84 case EZFS_BUSY:
85 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
86 case EZFS_EXISTS:
87 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
88 case EZFS_NOENT:
89 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
90 case EZFS_BADSTREAM:
91 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
92 case EZFS_DSREADONLY:
93 return (dgettext(TEXT_DOMAIN, "dataset is read only"));
94 case EZFS_VOLTOOBIG:
95 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
96 "this system"));
97 case EZFS_VOLHASDATA:
98 return (dgettext(TEXT_DOMAIN, "volume has data"));
99 case EZFS_INVALIDNAME:
100 return (dgettext(TEXT_DOMAIN, "invalid name"));
101 case EZFS_BADRESTORE:
102 return (dgettext(TEXT_DOMAIN, "unable to restore to "
103 "destination"));
104 case EZFS_BADBACKUP:
105 return (dgettext(TEXT_DOMAIN, "backup failed"));
106 case EZFS_BADTARGET:
107 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
108 case EZFS_NODEVICE:
109 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
110 case EZFS_BADDEV:
111 return (dgettext(TEXT_DOMAIN, "invalid device"));
112 case EZFS_NOREPLICAS:
113 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
114 case EZFS_RESILVERING:
115 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
116 case EZFS_BADVERSION:
117 return (dgettext(TEXT_DOMAIN, "unsupported version"));
118 case EZFS_POOLUNAVAIL:
119 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
120 case EZFS_DEVOVERFLOW:
121 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
122 case EZFS_BADPATH:
123 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
124 case EZFS_CROSSTARGET:
125 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
126 "pools"));
127 case EZFS_ZONED:
128 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
129 case EZFS_MOUNTFAILED:
130 return (dgettext(TEXT_DOMAIN, "mount failed"));
131 case EZFS_UMOUNTFAILED:
132 return (dgettext(TEXT_DOMAIN, "umount failed"));
133 case EZFS_UNSHARENFSFAILED:
134 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
135 case EZFS_SHARENFSFAILED:
136 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
137 case EZFS_UNSHARESMBFAILED:
138 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
139 case EZFS_SHARESMBFAILED:
140 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
141 case EZFS_ISCSISVCUNAVAIL:
142 return (dgettext(TEXT_DOMAIN,
143 "iscsitgt service need to be enabled by "
144 "a privileged user"));
145 case EZFS_DEVLINKS:
146 return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
147 case EZFS_PERM:
148 return (dgettext(TEXT_DOMAIN, "permission denied"));
149 case EZFS_NOSPC:
150 return (dgettext(TEXT_DOMAIN, "out of space"));
151 case EZFS_IO:
152 return (dgettext(TEXT_DOMAIN, "I/O error"));
153 case EZFS_INTR:
154 return (dgettext(TEXT_DOMAIN, "signal received"));
155 case EZFS_ISSPARE:
156 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
157 "spare"));
158 case EZFS_INVALCONFIG:
159 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
160 case EZFS_RECURSIVE:
161 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
162 case EZFS_NOHISTORY:
163 return (dgettext(TEXT_DOMAIN, "no history available"));
164 case EZFS_UNSHAREISCSIFAILED:
165 return (dgettext(TEXT_DOMAIN,
166 "iscsitgtd failed request to unshare"));
167 case EZFS_SHAREISCSIFAILED:
168 return (dgettext(TEXT_DOMAIN,
169 "iscsitgtd failed request to share"));
170 case EZFS_POOLPROPS:
171 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
172 "pool properties"));
173 case EZFS_POOL_NOTSUP:
174 return (dgettext(TEXT_DOMAIN, "operation not supported "
175 "on this type of pool"));
176 case EZFS_POOL_INVALARG:
177 return (dgettext(TEXT_DOMAIN, "invalid argument for "
178 "this pool operation"));
179 case EZFS_NAMETOOLONG:
180 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
181 case EZFS_OPENFAILED:
182 return (dgettext(TEXT_DOMAIN, "open failed"));
183 case EZFS_NOCAP:
184 return (dgettext(TEXT_DOMAIN,
185 "disk capacity information could not be retrieved"));
186 case EZFS_LABELFAILED:
187 return (dgettext(TEXT_DOMAIN, "write of label failed"));
188 case EZFS_BADWHO:
189 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
190 case EZFS_BADPERM:
191 return (dgettext(TEXT_DOMAIN, "invalid permission"));
192 case EZFS_BADPERMSET:
193 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
194 case EZFS_NODELEGATION:
195 return (dgettext(TEXT_DOMAIN, "delegated administration is "
196 "disabled on pool"));
197 case EZFS_PERMRDONLY:
198 return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
199 " modified"));
200 case EZFS_BADCACHE:
201 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
202 case EZFS_ISL2CACHE:
203 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
204 case EZFS_VDEVNOTSUP:
205 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
206 "supported"));
207 case EZFS_NOTSUP:
208 return (dgettext(TEXT_DOMAIN, "operation not supported "
209 "on this dataset"));
210 case EZFS_ACTIVE_SPARE:
211 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
212 "device"));
213 case EZFS_UNKNOWN:
214 return (dgettext(TEXT_DOMAIN, "unknown error"));
215 default:
216 assert(hdl->libzfs_error == 0);
217 return (dgettext(TEXT_DOMAIN, "no error"));
221 /*PRINTFLIKE2*/
222 void
223 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
225 va_list ap;
227 va_start(ap, fmt);
229 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
230 fmt, ap);
231 hdl->libzfs_desc_active = 1;
233 va_end(ap);
236 static void
237 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
239 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
240 fmt, ap);
241 hdl->libzfs_error = error;
243 if (hdl->libzfs_desc_active)
244 hdl->libzfs_desc_active = 0;
245 else
246 hdl->libzfs_desc[0] = '\0';
248 if (hdl->libzfs_printerr) {
249 if (error == EZFS_UNKNOWN) {
250 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
251 "error: %s\n"), libzfs_error_description(hdl));
252 abort();
255 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
256 libzfs_error_description(hdl));
257 if (error == EZFS_NOMEM)
258 exit(1);
263 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
265 return (zfs_error_fmt(hdl, error, "%s", msg));
268 /*PRINTFLIKE3*/
270 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
272 va_list ap;
274 va_start(ap, fmt);
276 zfs_verror(hdl, error, fmt, ap);
278 va_end(ap);
280 return (-1);
283 static int
284 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
285 va_list ap)
287 switch (error) {
288 case EPERM:
289 case EACCES:
290 zfs_verror(hdl, EZFS_PERM, fmt, ap);
291 return (-1);
293 case ECANCELED:
294 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
295 return (-1);
297 case EIO:
298 zfs_verror(hdl, EZFS_IO, fmt, ap);
299 return (-1);
301 case EINTR:
302 zfs_verror(hdl, EZFS_INTR, fmt, ap);
303 return (-1);
306 return (0);
310 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
312 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
315 /*PRINTFLIKE3*/
317 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
319 va_list ap;
321 va_start(ap, fmt);
323 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
324 va_end(ap);
325 return (-1);
328 switch (error) {
329 case ENXIO:
330 case ENODEV:
331 zfs_verror(hdl, EZFS_IO, fmt, ap);
332 break;
334 case ENOENT:
335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
336 "dataset does not exist"));
337 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
338 break;
340 case ENOSPC:
341 case EDQUOT:
342 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
343 return (-1);
345 case EEXIST:
346 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
347 "dataset already exists"));
348 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
349 break;
351 case EBUSY:
352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
353 "dataset is busy"));
354 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
355 break;
356 case EROFS:
357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358 "snapshot permissions cannot be modified"));
359 zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
360 break;
361 case ENAMETOOLONG:
362 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
363 break;
364 case ENOTSUP:
365 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
366 break;
367 default:
368 zfs_error_aux(hdl, strerror(errno));
369 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
370 break;
373 va_end(ap);
374 return (-1);
378 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
380 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
383 /*PRINTFLIKE3*/
385 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
387 va_list ap;
389 va_start(ap, fmt);
391 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
392 va_end(ap);
393 return (-1);
396 switch (error) {
397 case ENODEV:
398 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
399 break;
401 case ENOENT:
402 zfs_error_aux(hdl,
403 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
404 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
405 break;
407 case EEXIST:
408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
409 "pool already exists"));
410 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
411 break;
413 case EBUSY:
414 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
415 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
416 break;
418 case ENXIO:
419 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
420 "one or more devices is currently unavailable"));
421 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
422 break;
424 case ENAMETOOLONG:
425 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
426 break;
428 case ENOTSUP:
429 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
430 break;
432 case EINVAL:
433 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
434 break;
436 case ENOSPC:
437 case EDQUOT:
438 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
439 return (-1);
441 default:
442 zfs_error_aux(hdl, strerror(error));
443 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
446 va_end(ap);
447 return (-1);
451 * Display an out of memory error message and abort the current program.
454 no_memory(libzfs_handle_t *hdl)
456 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
460 * A safe form of malloc() which will die if the allocation fails.
462 void *
463 zfs_alloc(libzfs_handle_t *hdl, size_t size)
465 void *data;
467 if ((data = calloc(1, size)) == NULL)
468 (void) no_memory(hdl);
470 return (data);
474 * A safe form of realloc(), which also zeroes newly allocated space.
476 void *
477 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
479 void *ret;
481 if ((ret = realloc(ptr, newsize)) == NULL) {
482 (void) no_memory(hdl);
483 free(ptr);
484 return (NULL);
487 bzero((char *)ret + oldsize, (newsize - oldsize));
488 return (ret);
492 * A safe form of strdup() which will die if the allocation fails.
494 char *
495 zfs_strdup(libzfs_handle_t *hdl, const char *str)
497 char *ret;
499 if ((ret = strdup(str)) == NULL)
500 (void) no_memory(hdl);
502 return (ret);
506 * Convert a number to an appropriately human-readable output.
508 void
509 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
511 uint64_t n = num;
512 int index = 0;
513 char u;
515 while (n >= 1024) {
516 n /= 1024;
517 index++;
520 u = " KMGTPE"[index];
522 if (index == 0) {
523 (void) snprintf(buf, buflen, "%llu", n);
524 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
526 * If this is an even multiple of the base, always display
527 * without any decimal precision.
529 (void) snprintf(buf, buflen, "%llu%c", n, u);
530 } else {
532 * We want to choose a precision that reflects the best choice
533 * for fitting in 5 characters. This can get rather tricky when
534 * we have numbers that are very close to an order of magnitude.
535 * For example, when displaying 10239 (which is really 9.999K),
536 * we want only a single place of precision for 10.0K. We could
537 * develop some complex heuristics for this, but it's much
538 * easier just to try each combination in turn.
540 int i;
541 for (i = 2; i >= 0; i--) {
542 if (snprintf(buf, buflen, "%.*f%c", i,
543 (double)num / (1ULL << 10 * index), u) <= 5)
544 break;
549 void
550 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
552 hdl->libzfs_printerr = printerr;
555 libzfs_handle_t *
556 libzfs_init(void)
558 libzfs_handle_t *hdl;
560 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
561 return (NULL);
564 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
565 free(hdl);
566 return (NULL);
569 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
570 (void) close(hdl->libzfs_fd);
571 free(hdl);
572 return (NULL);
575 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
577 zfs_prop_init();
578 zpool_prop_init();
580 return (hdl);
583 void
584 libzfs_fini(libzfs_handle_t *hdl)
586 (void) close(hdl->libzfs_fd);
587 if (hdl->libzfs_mnttab)
588 (void) fclose(hdl->libzfs_mnttab);
589 if (hdl->libzfs_sharetab)
590 (void) fclose(hdl->libzfs_sharetab);
591 zfs_uninit_libshare(hdl);
592 if (hdl->libzfs_log_str)
593 (void) free(hdl->libzfs_log_str);
594 zpool_free_handles(hdl);
595 namespace_clear(hdl);
596 free(hdl);
599 libzfs_handle_t *
600 zpool_get_handle(zpool_handle_t *zhp)
602 return (zhp->zpool_hdl);
605 libzfs_handle_t *
606 zfs_get_handle(zfs_handle_t *zhp)
608 return (zhp->zfs_hdl);
611 zpool_handle_t *
612 zfs_get_pool_handle(const zfs_handle_t *zhp)
614 return (zhp->zpool_hdl);
618 * Given a name, determine whether or not it's a valid path
619 * (starts with '/' or "./"). If so, walk the mnttab trying
620 * to match the device number. If not, treat the path as an
621 * fs/vol/snap name.
623 zfs_handle_t *
624 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
626 struct statvfs statbuf;
627 int ret;
629 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
631 * It's not a valid path, assume it's a name of type 'argtype'.
633 return (zfs_open(hdl, path, argtype));
636 if (getstatfs(&statbuf, path) != 0) {
637 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
638 return (NULL);
641 if (strcmp(statbuf.f_fstypename, MNTTYPE_ZFS) != 0) {
642 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
643 path);
644 return (NULL);
647 return (zfs_open(hdl, statbuf.f_mntfromname, ZFS_TYPE_FILESYSTEM));
651 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
652 * an ioctl().
655 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
657 if (len == 0)
658 len = 2048;
659 zc->zc_nvlist_dst_size = len;
660 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
661 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
662 return (-1);
664 return (0);
668 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
669 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
670 * filled in by the kernel to indicate the actual required size.
673 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
675 free((void *)(uintptr_t)zc->zc_nvlist_dst);
676 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
677 zfs_alloc(hdl, zc->zc_nvlist_dst_size))
678 == NULL)
679 return (-1);
681 return (0);
685 * Called to free the src and dst nvlists stored in the command structure.
687 void
688 zcmd_free_nvlists(zfs_cmd_t *zc)
690 free((void *)(uintptr_t)zc->zc_nvlist_conf);
691 free((void *)(uintptr_t)zc->zc_nvlist_src);
692 free((void *)(uintptr_t)zc->zc_nvlist_dst);
695 static int
696 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
697 nvlist_t *nvl)
699 char *packed;
700 size_t len;
702 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
704 if ((packed = zfs_alloc(hdl, len)) == NULL)
705 return (-1);
707 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
709 *outnv = (uint64_t)(uintptr_t)packed;
710 *outlen = len;
712 return (0);
716 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
718 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
719 &zc->zc_nvlist_conf_size, nvl));
723 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
725 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
726 &zc->zc_nvlist_src_size, nvl));
730 * Unpacks an nvlist from the ZFS ioctl command structure.
733 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
735 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
736 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
737 return (no_memory(hdl));
739 return (0);
743 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
745 int error;
747 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
748 error = ioctl(hdl->libzfs_fd, request, zc);
749 if (hdl->libzfs_log_str) {
750 free(hdl->libzfs_log_str);
751 hdl->libzfs_log_str = NULL;
753 zc->zc_history = 0;
755 return (error);
759 * ================================================================
760 * API shared by zfs and zpool property management
761 * ================================================================
764 static void
765 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
767 zprop_list_t *pl = cbp->cb_proplist;
768 int i;
769 char *title;
770 size_t len;
772 cbp->cb_first = B_FALSE;
773 if (cbp->cb_scripted)
774 return;
777 * Start with the length of the column headers.
779 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
780 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
781 "PROPERTY"));
782 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
783 "VALUE"));
784 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
785 "SOURCE"));
788 * Go through and calculate the widths for each column. For the
789 * 'source' column, we kludge it up by taking the worst-case scenario of
790 * inheriting from the longest name. This is acceptable because in the
791 * majority of cases 'SOURCE' is the last column displayed, and we don't
792 * use the width anyway. Note that the 'VALUE' column can be oversized,
793 * if the name of the property is much longer the any values we find.
795 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
797 * 'PROPERTY' column
799 if (pl->pl_prop != ZPROP_INVAL) {
800 const char *propname = (type == ZFS_TYPE_POOL) ?
801 zpool_prop_to_name(pl->pl_prop) :
802 zfs_prop_to_name(pl->pl_prop);
804 len = strlen(propname);
805 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
806 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
807 } else {
808 len = strlen(pl->pl_user_prop);
809 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
810 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
814 * 'VALUE' column
816 if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
817 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
818 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
821 * 'NAME' and 'SOURCE' columns
823 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
824 ZFS_PROP_NAME) &&
825 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
826 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
827 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
828 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
833 * Now go through and print the headers.
835 for (i = 0; i < 4; i++) {
836 switch (cbp->cb_columns[i]) {
837 case GET_COL_NAME:
838 title = dgettext(TEXT_DOMAIN, "NAME");
839 break;
840 case GET_COL_PROPERTY:
841 title = dgettext(TEXT_DOMAIN, "PROPERTY");
842 break;
843 case GET_COL_VALUE:
844 title = dgettext(TEXT_DOMAIN, "VALUE");
845 break;
846 case GET_COL_SOURCE:
847 title = dgettext(TEXT_DOMAIN, "SOURCE");
848 break;
849 default:
850 title = NULL;
853 if (title != NULL) {
854 if (i == 3 || cbp->cb_columns[i + 1] == 0)
855 (void) printf("%s", title);
856 else
857 (void) printf("%-*s ",
858 cbp->cb_colwidths[cbp->cb_columns[i]],
859 title);
862 (void) printf("\n");
866 * Display a single line of output, according to the settings in the callback
867 * structure.
869 void
870 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
871 const char *propname, const char *value, zprop_source_t sourcetype,
872 const char *source)
874 int i;
875 const char *str;
876 char buf[128];
879 * Ignore those source types that the user has chosen to ignore.
881 if ((sourcetype & cbp->cb_sources) == 0)
882 return;
884 if (cbp->cb_first)
885 zprop_print_headers(cbp, cbp->cb_type);
887 for (i = 0; i < 4; i++) {
888 switch (cbp->cb_columns[i]) {
889 case GET_COL_NAME:
890 str = name;
891 break;
893 case GET_COL_PROPERTY:
894 str = propname;
895 break;
897 case GET_COL_VALUE:
898 str = value;
899 break;
901 case GET_COL_SOURCE:
902 switch (sourcetype) {
903 case ZPROP_SRC_NONE:
904 str = "-";
905 break;
907 case ZPROP_SRC_DEFAULT:
908 str = "default";
909 break;
911 case ZPROP_SRC_LOCAL:
912 str = "local";
913 break;
915 case ZPROP_SRC_TEMPORARY:
916 str = "temporary";
917 break;
919 case ZPROP_SRC_INHERITED:
920 (void) snprintf(buf, sizeof (buf),
921 "inherited from %s", source);
922 str = buf;
923 break;
925 break;
927 default:
928 continue;
931 if (cbp->cb_columns[i + 1] == 0)
932 (void) printf("%s", str);
933 else if (cbp->cb_scripted)
934 (void) printf("%s\t", str);
935 else
936 (void) printf("%-*s ",
937 cbp->cb_colwidths[cbp->cb_columns[i]],
938 str);
942 (void) printf("\n");
946 * Given a numeric suffix, convert the value into a number of bits that the
947 * resulting value must be shifted.
949 static int
950 str2shift(libzfs_handle_t *hdl, const char *buf)
952 const char *ends = "BKMGTPEZ";
953 int i;
955 if (buf[0] == '\0')
956 return (0);
957 for (i = 0; i < strlen(ends); i++) {
958 if (toupper(buf[0]) == ends[i])
959 break;
961 if (i == strlen(ends)) {
962 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
963 "invalid numeric suffix '%s'"), buf);
964 return (-1);
968 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
969 * allow 'BB' - that's just weird.
971 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
972 toupper(buf[0]) != 'B'))
973 return (10*i);
975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
976 "invalid numeric suffix '%s'"), buf);
977 return (-1);
981 * Convert a string of the form '100G' into a real number. Used when setting
982 * properties or creating a volume. 'buf' is used to place an extended error
983 * message for the caller to use.
986 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
988 char *end;
989 int shift;
991 *num = 0;
993 /* Check to see if this looks like a number. */
994 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
995 if (hdl)
996 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997 "bad numeric value '%s'"), value);
998 return (-1);
1001 /* Rely on stroll() to process the numeric portion. */
1002 errno = 0;
1003 *num = strtoll(value, &end, 10);
1006 * Check for ERANGE, which indicates that the value is too large to fit
1007 * in a 64-bit value.
1009 if (errno == ERANGE) {
1010 if (hdl)
1011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1012 "numeric value is too large"));
1013 return (-1);
1017 * If we have a decimal value, then do the computation with floating
1018 * point arithmetic. Otherwise, use standard arithmetic.
1020 if (*end == '.') {
1021 double fval = strtod(value, &end);
1023 if ((shift = str2shift(hdl, end)) == -1)
1024 return (-1);
1026 fval *= pow(2, shift);
1028 if (fval > UINT64_MAX) {
1029 if (hdl)
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "numeric value is too large"));
1032 return (-1);
1035 *num = (uint64_t)fval;
1036 } else {
1037 if ((shift = str2shift(hdl, end)) == -1)
1038 return (-1);
1040 /* Check for overflow */
1041 if (shift >= 64 || (*num << shift) >> shift != *num) {
1042 if (hdl)
1043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1044 "numeric value is too large"));
1045 return (-1);
1048 *num <<= shift;
1051 return (0);
1055 * Given a propname=value nvpair to set, parse any numeric properties
1056 * (index, boolean, etc) if they are specified as strings and add the
1057 * resulting nvpair to the returned nvlist.
1059 * At the DSL layer, all properties are either 64-bit numbers or strings.
1060 * We want the user to be able to ignore this fact and specify properties
1061 * as native values (numbers, for example) or as strings (to simplify
1062 * command line utilities). This also handles converting index types
1063 * (compression, checksum, etc) from strings to their on-disk index.
1066 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1067 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1068 const char *errbuf)
1070 data_type_t datatype = nvpair_type(elem);
1071 zprop_type_t proptype;
1072 const char *propname;
1073 char *value;
1074 boolean_t isnone = B_FALSE;
1076 if (type == ZFS_TYPE_POOL) {
1077 proptype = zpool_prop_get_type(prop);
1078 propname = zpool_prop_to_name(prop);
1079 } else {
1080 proptype = zfs_prop_get_type(prop);
1081 propname = zfs_prop_to_name(prop);
1085 * Convert any properties to the internal DSL value types.
1087 *svalp = NULL;
1088 *ivalp = 0;
1090 switch (proptype) {
1091 case PROP_TYPE_STRING:
1092 if (datatype != DATA_TYPE_STRING) {
1093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1094 "'%s' must be a string"), nvpair_name(elem));
1095 goto error;
1097 (void) nvpair_value_string(elem, svalp);
1098 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1100 "'%s' is too long"), nvpair_name(elem));
1101 goto error;
1103 break;
1105 case PROP_TYPE_NUMBER:
1106 if (datatype == DATA_TYPE_STRING) {
1107 (void) nvpair_value_string(elem, &value);
1108 if (strcmp(value, "none") == 0) {
1109 isnone = B_TRUE;
1110 } else if (zfs_nicestrtonum(hdl, value, ivalp)
1111 != 0) {
1112 goto error;
1114 } else if (datatype == DATA_TYPE_UINT64) {
1115 (void) nvpair_value_uint64(elem, ivalp);
1116 } else {
1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118 "'%s' must be a number"), nvpair_name(elem));
1119 goto error;
1123 * Quota special: force 'none' and don't allow 0.
1125 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1126 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1128 "use 'none' to disable quota/refquota"));
1129 goto error;
1131 break;
1133 case PROP_TYPE_INDEX:
1134 if (datatype != DATA_TYPE_STRING) {
1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 "'%s' must be a string"), nvpair_name(elem));
1137 goto error;
1140 (void) nvpair_value_string(elem, &value);
1142 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1144 "'%s' must be one of '%s'"), propname,
1145 zprop_values(prop, type));
1146 goto error;
1148 break;
1150 default:
1151 abort();
1155 * Add the result to our return set of properties.
1157 if (*svalp != NULL) {
1158 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1159 (void) no_memory(hdl);
1160 return (-1);
1162 } else {
1163 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1164 (void) no_memory(hdl);
1165 return (-1);
1169 return (0);
1170 error:
1171 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1172 return (-1);
1175 static int
1176 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1177 zfs_type_t type)
1179 int prop;
1180 zprop_list_t *entry;
1182 prop = zprop_name_to_prop(propname, type);
1184 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1185 prop = ZPROP_INVAL;
1188 * When no property table entry can be found, return failure if
1189 * this is a pool property or if this isn't a user-defined
1190 * dataset property,
1192 if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1193 !zfs_prop_user(propname))) {
1194 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1195 "invalid property '%s'"), propname);
1196 return (zfs_error(hdl, EZFS_BADPROP,
1197 dgettext(TEXT_DOMAIN, "bad property list")));
1200 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1201 return (-1);
1203 entry->pl_prop = prop;
1204 if (prop == ZPROP_INVAL) {
1205 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) {
1206 free(entry);
1207 return (-1);
1209 entry->pl_width = strlen(propname);
1210 } else {
1211 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1212 type);
1215 *listp = entry;
1217 return (0);
1221 * Given a comma-separated list of properties, construct a property list
1222 * containing both user-defined and native properties. This function will
1223 * return a NULL list if 'all' is specified, which can later be expanded
1224 * by zprop_expand_list().
1227 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1228 zfs_type_t type)
1230 *listp = NULL;
1233 * If 'all' is specified, return a NULL list.
1235 if (strcmp(props, "all") == 0)
1236 return (0);
1239 * If no props were specified, return an error.
1241 if (props[0] == '\0') {
1242 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1243 "no properties specified"));
1244 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1245 "bad property list")));
1249 * It would be nice to use getsubopt() here, but the inclusion of column
1250 * aliases makes this more effort than it's worth.
1252 while (*props != '\0') {
1253 size_t len;
1254 char *p;
1255 char c;
1257 if ((p = strchr(props, ',')) == NULL) {
1258 len = strlen(props);
1259 p = props + len;
1260 } else {
1261 len = p - props;
1265 * Check for empty options.
1267 if (len == 0) {
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "empty property name"));
1270 return (zfs_error(hdl, EZFS_BADPROP,
1271 dgettext(TEXT_DOMAIN, "bad property list")));
1275 * Check all regular property names.
1277 c = props[len];
1278 props[len] = '\0';
1280 if (strcmp(props, "space") == 0) {
1281 static char *spaceprops[] = {
1282 "name", "avail", "used", "usedbysnapshots",
1283 "usedbydataset", "usedbyrefreservation",
1284 "usedbychildren", NULL
1286 int i;
1288 for (i = 0; spaceprops[i]; i++) {
1289 if (addlist(hdl, spaceprops[i], listp, type))
1290 return (-1);
1291 listp = &(*listp)->pl_next;
1293 } else {
1294 if (addlist(hdl, props, listp, type))
1295 return (-1);
1296 listp = &(*listp)->pl_next;
1299 props = p;
1300 if (c == ',')
1301 props++;
1304 return (0);
1307 void
1308 zprop_free_list(zprop_list_t *pl)
1310 zprop_list_t *next;
1312 while (pl != NULL) {
1313 next = pl->pl_next;
1314 free(pl->pl_user_prop);
1315 free(pl);
1316 pl = next;
1320 typedef struct expand_data {
1321 zprop_list_t **last;
1322 libzfs_handle_t *hdl;
1323 zfs_type_t type;
1324 } expand_data_t;
1327 zprop_expand_list_cb(int prop, void *cb)
1329 zprop_list_t *entry;
1330 expand_data_t *edp = cb;
1332 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1333 return (ZPROP_INVAL);
1335 entry->pl_prop = prop;
1336 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1337 entry->pl_all = B_TRUE;
1339 *(edp->last) = entry;
1340 edp->last = &entry->pl_next;
1342 return (ZPROP_CONT);
1346 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1348 zprop_list_t *entry;
1349 zprop_list_t **last;
1350 expand_data_t exp;
1352 if (*plp == NULL) {
1354 * If this is the very first time we've been called for an 'all'
1355 * specification, expand the list to include all native
1356 * properties.
1358 last = plp;
1360 exp.last = last;
1361 exp.hdl = hdl;
1362 exp.type = type;
1364 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1365 B_FALSE, type) == ZPROP_INVAL)
1366 return (-1);
1369 * Add 'name' to the beginning of the list, which is handled
1370 * specially.
1372 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1373 return (-1);
1375 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1376 ZFS_PROP_NAME;
1377 entry->pl_width = zprop_width(entry->pl_prop,
1378 &entry->pl_fixed, type);
1379 entry->pl_all = B_TRUE;
1380 entry->pl_next = *plp;
1381 *plp = entry;
1383 return (0);
1387 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1388 zfs_type_t type)
1390 return (zprop_iter_common(func, cb, show_all, ordered, type));