]> git.proxmox.com Git - mirror_zfs.git/blob - zfs/lib/libzfs/libzfs_util.c
Initial Linux ZFS GIT Repo
[mirror_zfs.git] / zfs / 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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #pragma ident "@(#)libzfs_util.c 1.29 08/04/01 SMI"
27
28 /*
29 * Internal utility routines for the ZFS library.
30 */
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <libintl.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <math.h>
42 #include <sys/mnttab.h>
43 #include <sys/mntent.h>
44 #include <sys/types.h>
45
46 #include <libzfs.h>
47
48 #include "libzfs_impl.h"
49 #include "zfs_prop.h"
50
51 int
52 libzfs_errno(libzfs_handle_t *hdl)
53 {
54 return (hdl->libzfs_error);
55 }
56
57 const char *
58 libzfs_error_action(libzfs_handle_t *hdl)
59 {
60 return (hdl->libzfs_action);
61 }
62
63 const char *
64 libzfs_error_description(libzfs_handle_t *hdl)
65 {
66 if (hdl->libzfs_desc[0] != '\0')
67 return (hdl->libzfs_desc);
68
69 switch (hdl->libzfs_error) {
70 case EZFS_NOMEM:
71 return (dgettext(TEXT_DOMAIN, "out of memory"));
72 case EZFS_BADPROP:
73 return (dgettext(TEXT_DOMAIN, "invalid property value"));
74 case EZFS_PROPREADONLY:
75 return (dgettext(TEXT_DOMAIN, "read only property"));
76 case EZFS_PROPTYPE:
77 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
78 "datasets of this type"));
79 case EZFS_PROPNONINHERIT:
80 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
81 case EZFS_PROPSPACE:
82 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
83 case EZFS_BADTYPE:
84 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
85 "datasets of this type"));
86 case EZFS_BUSY:
87 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
88 case EZFS_EXISTS:
89 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
90 case EZFS_NOENT:
91 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
92 case EZFS_BADSTREAM:
93 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
94 case EZFS_DSREADONLY:
95 return (dgettext(TEXT_DOMAIN, "dataset is read only"));
96 case EZFS_VOLTOOBIG:
97 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
98 "this system"));
99 case EZFS_VOLHASDATA:
100 return (dgettext(TEXT_DOMAIN, "volume has data"));
101 case EZFS_INVALIDNAME:
102 return (dgettext(TEXT_DOMAIN, "invalid name"));
103 case EZFS_BADRESTORE:
104 return (dgettext(TEXT_DOMAIN, "unable to restore to "
105 "destination"));
106 case EZFS_BADBACKUP:
107 return (dgettext(TEXT_DOMAIN, "backup failed"));
108 case EZFS_BADTARGET:
109 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
110 case EZFS_NODEVICE:
111 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
112 case EZFS_BADDEV:
113 return (dgettext(TEXT_DOMAIN, "invalid device"));
114 case EZFS_NOREPLICAS:
115 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
116 case EZFS_RESILVERING:
117 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
118 case EZFS_BADVERSION:
119 return (dgettext(TEXT_DOMAIN, "unsupported version"));
120 case EZFS_POOLUNAVAIL:
121 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
122 case EZFS_DEVOVERFLOW:
123 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
124 case EZFS_BADPATH:
125 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
126 case EZFS_CROSSTARGET:
127 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
128 "pools"));
129 case EZFS_ZONED:
130 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
131 case EZFS_MOUNTFAILED:
132 return (dgettext(TEXT_DOMAIN, "mount failed"));
133 case EZFS_UMOUNTFAILED:
134 return (dgettext(TEXT_DOMAIN, "umount failed"));
135 case EZFS_UNSHARENFSFAILED:
136 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
137 case EZFS_SHARENFSFAILED:
138 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
139 case EZFS_UNSHARESMBFAILED:
140 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
141 case EZFS_SHARESMBFAILED:
142 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
143 case EZFS_ISCSISVCUNAVAIL:
144 return (dgettext(TEXT_DOMAIN,
145 "iscsitgt service need to be enabled by "
146 "a privileged user"));
147 case EZFS_DEVLINKS:
148 return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
149 case EZFS_PERM:
150 return (dgettext(TEXT_DOMAIN, "permission denied"));
151 case EZFS_NOSPC:
152 return (dgettext(TEXT_DOMAIN, "out of space"));
153 case EZFS_IO:
154 return (dgettext(TEXT_DOMAIN, "I/O error"));
155 case EZFS_INTR:
156 return (dgettext(TEXT_DOMAIN, "signal received"));
157 case EZFS_ISSPARE:
158 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
159 "spare"));
160 case EZFS_INVALCONFIG:
161 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
162 case EZFS_RECURSIVE:
163 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
164 case EZFS_NOHISTORY:
165 return (dgettext(TEXT_DOMAIN, "no history available"));
166 case EZFS_UNSHAREISCSIFAILED:
167 return (dgettext(TEXT_DOMAIN,
168 "iscsitgtd failed request to unshare"));
169 case EZFS_SHAREISCSIFAILED:
170 return (dgettext(TEXT_DOMAIN,
171 "iscsitgtd failed request to share"));
172 case EZFS_POOLPROPS:
173 return (dgettext(TEXT_DOMAIN, "failed to retrieve "
174 "pool properties"));
175 case EZFS_POOL_NOTSUP:
176 return (dgettext(TEXT_DOMAIN, "operation not supported "
177 "on this type of pool"));
178 case EZFS_POOL_INVALARG:
179 return (dgettext(TEXT_DOMAIN, "invalid argument for "
180 "this pool operation"));
181 case EZFS_NAMETOOLONG:
182 return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
183 case EZFS_OPENFAILED:
184 return (dgettext(TEXT_DOMAIN, "open failed"));
185 case EZFS_NOCAP:
186 return (dgettext(TEXT_DOMAIN,
187 "disk capacity information could not be retrieved"));
188 case EZFS_LABELFAILED:
189 return (dgettext(TEXT_DOMAIN, "write of label failed"));
190 case EZFS_BADWHO:
191 return (dgettext(TEXT_DOMAIN, "invalid user/group"));
192 case EZFS_BADPERM:
193 return (dgettext(TEXT_DOMAIN, "invalid permission"));
194 case EZFS_BADPERMSET:
195 return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
196 case EZFS_NODELEGATION:
197 return (dgettext(TEXT_DOMAIN, "delegated administration is "
198 "disabled on pool"));
199 case EZFS_PERMRDONLY:
200 return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
201 " modified"));
202 case EZFS_BADCACHE:
203 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
204 case EZFS_ISL2CACHE:
205 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
206 case EZFS_VDEVNOTSUP:
207 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
208 "supported"));
209 case EZFS_UNKNOWN:
210 return (dgettext(TEXT_DOMAIN, "unknown error"));
211 default:
212 assert(hdl->libzfs_error == 0);
213 return (dgettext(TEXT_DOMAIN, "no error"));
214 }
215 }
216
217 /*PRINTFLIKE2*/
218 void
219 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
220 {
221 va_list ap;
222
223 va_start(ap, fmt);
224
225 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
226 fmt, ap);
227 hdl->libzfs_desc_active = 1;
228
229 va_end(ap);
230 }
231
232 static void
233 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
234 {
235 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
236 fmt, ap);
237 hdl->libzfs_error = error;
238
239 if (hdl->libzfs_desc_active)
240 hdl->libzfs_desc_active = 0;
241 else
242 hdl->libzfs_desc[0] = '\0';
243
244 if (hdl->libzfs_printerr) {
245 if (error == EZFS_UNKNOWN) {
246 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
247 "error: %s\n"), libzfs_error_description(hdl));
248 abort();
249 }
250
251 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
252 libzfs_error_description(hdl));
253 if (error == EZFS_NOMEM)
254 exit(1);
255 }
256 }
257
258 int
259 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
260 {
261 return (zfs_error_fmt(hdl, error, "%s", msg));
262 }
263
264 /*PRINTFLIKE3*/
265 int
266 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
267 {
268 va_list ap;
269
270 va_start(ap, fmt);
271
272 zfs_verror(hdl, error, fmt, ap);
273
274 va_end(ap);
275
276 return (-1);
277 }
278
279 static int
280 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
281 va_list ap)
282 {
283 switch (error) {
284 case EPERM:
285 case EACCES:
286 zfs_verror(hdl, EZFS_PERM, fmt, ap);
287 return (-1);
288
289 case ECANCELED:
290 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
291 return (-1);
292
293 case EIO:
294 zfs_verror(hdl, EZFS_IO, fmt, ap);
295 return (-1);
296
297 case EINTR:
298 zfs_verror(hdl, EZFS_INTR, fmt, ap);
299 return (-1);
300 }
301
302 return (0);
303 }
304
305 int
306 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
307 {
308 return (zfs_standard_error_fmt(hdl, error, "%s", msg));
309 }
310
311 /*PRINTFLIKE3*/
312 int
313 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
314 {
315 va_list ap;
316
317 va_start(ap, fmt);
318
319 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
320 va_end(ap);
321 return (-1);
322 }
323
324 switch (error) {
325 case ENXIO:
326 case ENODEV:
327 zfs_verror(hdl, EZFS_IO, fmt, ap);
328 break;
329
330 case ENOENT:
331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
332 "dataset does not exist"));
333 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
334 break;
335
336 case ENOSPC:
337 case EDQUOT:
338 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
339 return (-1);
340
341 case EEXIST:
342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
343 "dataset already exists"));
344 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
345 break;
346
347 case EBUSY:
348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
349 "dataset is busy"));
350 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
351 break;
352 case EROFS:
353 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
354 "snapshot permissions cannot be modified"));
355 zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
356 break;
357 case ENAMETOOLONG:
358 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
359 break;
360 case ENOTSUP:
361 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
362 break;
363 default:
364 zfs_error_aux(hdl, strerror(errno));
365 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
366 break;
367 }
368
369 va_end(ap);
370 return (-1);
371 }
372
373 int
374 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
375 {
376 return (zpool_standard_error_fmt(hdl, error, "%s", msg));
377 }
378
379 /*PRINTFLIKE3*/
380 int
381 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
382 {
383 va_list ap;
384
385 va_start(ap, fmt);
386
387 if (zfs_common_error(hdl, error, fmt, ap) != 0) {
388 va_end(ap);
389 return (-1);
390 }
391
392 switch (error) {
393 case ENODEV:
394 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
395 break;
396
397 case ENOENT:
398 zfs_error_aux(hdl,
399 dgettext(TEXT_DOMAIN, "no such pool or dataset"));
400 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
401 break;
402
403 case EEXIST:
404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
405 "pool already exists"));
406 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
407 break;
408
409 case EBUSY:
410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
411 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
412 break;
413
414 case ENXIO:
415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
416 "one or more devices is currently unavailable"));
417 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
418 break;
419
420 case ENAMETOOLONG:
421 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
422 break;
423
424 case ENOTSUP:
425 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
426 break;
427
428 case EINVAL:
429 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
430 break;
431
432 case ENOSPC:
433 case EDQUOT:
434 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
435 return (-1);
436
437 default:
438 zfs_error_aux(hdl, strerror(error));
439 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
440 }
441
442 va_end(ap);
443 return (-1);
444 }
445
446 /*
447 * Display an out of memory error message and abort the current program.
448 */
449 int
450 no_memory(libzfs_handle_t *hdl)
451 {
452 return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
453 }
454
455 /*
456 * A safe form of malloc() which will die if the allocation fails.
457 */
458 void *
459 zfs_alloc(libzfs_handle_t *hdl, size_t size)
460 {
461 void *data;
462
463 if ((data = calloc(1, size)) == NULL)
464 (void) no_memory(hdl);
465
466 return (data);
467 }
468
469 /*
470 * A safe form of realloc(), which also zeroes newly allocated space.
471 */
472 void *
473 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
474 {
475 void *ret;
476
477 if ((ret = realloc(ptr, newsize)) == NULL) {
478 (void) no_memory(hdl);
479 free(ptr);
480 return (NULL);
481 }
482
483 bzero((char *)ret + oldsize, (newsize - oldsize));
484 return (ret);
485 }
486
487 /*
488 * A safe form of strdup() which will die if the allocation fails.
489 */
490 char *
491 zfs_strdup(libzfs_handle_t *hdl, const char *str)
492 {
493 char *ret;
494
495 if ((ret = strdup(str)) == NULL)
496 (void) no_memory(hdl);
497
498 return (ret);
499 }
500
501 /*
502 * Convert a number to an appropriately human-readable output.
503 */
504 void
505 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
506 {
507 uint64_t n = num;
508 int index = 0;
509 char u;
510
511 while (n >= 1024) {
512 n /= 1024;
513 index++;
514 }
515
516 u = " KMGTPE"[index];
517
518 if (index == 0) {
519 (void) snprintf(buf, buflen, "%llu", n);
520 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
521 /*
522 * If this is an even multiple of the base, always display
523 * without any decimal precision.
524 */
525 (void) snprintf(buf, buflen, "%llu%c", n, u);
526 } else {
527 /*
528 * We want to choose a precision that reflects the best choice
529 * for fitting in 5 characters. This can get rather tricky when
530 * we have numbers that are very close to an order of magnitude.
531 * For example, when displaying 10239 (which is really 9.999K),
532 * we want only a single place of precision for 10.0K. We could
533 * develop some complex heuristics for this, but it's much
534 * easier just to try each combination in turn.
535 */
536 int i;
537 for (i = 2; i >= 0; i--) {
538 if (snprintf(buf, buflen, "%.*f%c", i,
539 (double)num / (1ULL << 10 * index), u) <= 5)
540 break;
541 }
542 }
543 }
544
545 void
546 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
547 {
548 hdl->libzfs_printerr = printerr;
549 }
550
551 libzfs_handle_t *
552 libzfs_init(void)
553 {
554 libzfs_handle_t *hdl;
555
556 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
557 return (NULL);
558 }
559
560 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
561 free(hdl);
562 return (NULL);
563 }
564
565 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
566 (void) close(hdl->libzfs_fd);
567 free(hdl);
568 return (NULL);
569 }
570
571 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
572
573 zfs_prop_init();
574 zpool_prop_init();
575
576 return (hdl);
577 }
578
579 void
580 libzfs_fini(libzfs_handle_t *hdl)
581 {
582 (void) close(hdl->libzfs_fd);
583 if (hdl->libzfs_mnttab)
584 (void) fclose(hdl->libzfs_mnttab);
585 if (hdl->libzfs_sharetab)
586 (void) fclose(hdl->libzfs_sharetab);
587 zfs_uninit_libshare(hdl);
588 if (hdl->libzfs_log_str)
589 (void) free(hdl->libzfs_log_str);
590 namespace_clear(hdl);
591 free(hdl);
592 }
593
594 libzfs_handle_t *
595 zpool_get_handle(zpool_handle_t *zhp)
596 {
597 return (zhp->zpool_hdl);
598 }
599
600 libzfs_handle_t *
601 zfs_get_handle(zfs_handle_t *zhp)
602 {
603 return (zhp->zfs_hdl);
604 }
605
606 /*
607 * Given a name, determine whether or not it's a valid path
608 * (starts with '/' or "./"). If so, walk the mnttab trying
609 * to match the device number. If not, treat the path as an
610 * fs/vol/snap name.
611 */
612 zfs_handle_t *
613 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
614 {
615 struct stat64 statbuf;
616 struct extmnttab entry;
617 int ret;
618
619 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
620 /*
621 * It's not a valid path, assume it's a name of type 'argtype'.
622 */
623 return (zfs_open(hdl, path, argtype));
624 }
625
626 if (stat64(path, &statbuf) != 0) {
627 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
628 return (NULL);
629 }
630
631 rewind(hdl->libzfs_mnttab);
632 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
633 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
634 statbuf.st_dev) {
635 break;
636 }
637 }
638 if (ret != 0) {
639 return (NULL);
640 }
641
642 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
643 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
644 path);
645 return (NULL);
646 }
647
648 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
649 }
650
651 /*
652 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
653 * an ioctl().
654 */
655 int
656 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
657 {
658 if (len == 0)
659 len = 2048;
660 zc->zc_nvlist_dst_size = len;
661 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
662 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
663 return (-1);
664
665 return (0);
666 }
667
668 /*
669 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
670 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
671 * filled in by the kernel to indicate the actual required size.
672 */
673 int
674 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
675 {
676 free((void *)(uintptr_t)zc->zc_nvlist_dst);
677 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
678 zfs_alloc(hdl, zc->zc_nvlist_dst_size))
679 == NULL)
680 return (-1);
681
682 return (0);
683 }
684
685 /*
686 * Called to free the src and dst nvlists stored in the command structure.
687 */
688 void
689 zcmd_free_nvlists(zfs_cmd_t *zc)
690 {
691 free((void *)(uintptr_t)zc->zc_nvlist_conf);
692 free((void *)(uintptr_t)zc->zc_nvlist_src);
693 free((void *)(uintptr_t)zc->zc_nvlist_dst);
694 }
695
696 static int
697 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
698 nvlist_t *nvl)
699 {
700 char *packed;
701 size_t len;
702
703 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
704
705 if ((packed = zfs_alloc(hdl, len)) == NULL)
706 return (-1);
707
708 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
709
710 *outnv = (uint64_t)(uintptr_t)packed;
711 *outlen = len;
712
713 return (0);
714 }
715
716 int
717 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
718 {
719 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
720 &zc->zc_nvlist_conf_size, nvl));
721 }
722
723 int
724 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
725 {
726 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
727 &zc->zc_nvlist_src_size, nvl));
728 }
729
730 /*
731 * Unpacks an nvlist from the ZFS ioctl command structure.
732 */
733 int
734 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
735 {
736 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
737 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
738 return (no_memory(hdl));
739
740 return (0);
741 }
742
743 int
744 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
745 {
746 int error;
747
748 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
749 error = ioctl(hdl->libzfs_fd, request, zc);
750 if (hdl->libzfs_log_str) {
751 free(hdl->libzfs_log_str);
752 hdl->libzfs_log_str = NULL;
753 }
754 zc->zc_history = 0;
755
756 return (error);
757 }
758
759 /*
760 * ================================================================
761 * API shared by zfs and zpool property management
762 * ================================================================
763 */
764
765 static void
766 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
767 {
768 zprop_list_t *pl = cbp->cb_proplist;
769 int i;
770 char *title;
771 size_t len;
772
773 cbp->cb_first = B_FALSE;
774 if (cbp->cb_scripted)
775 return;
776
777 /*
778 * Start with the length of the column headers.
779 */
780 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
781 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
782 "PROPERTY"));
783 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
784 "VALUE"));
785 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
786 "SOURCE"));
787
788 /*
789 * Go through and calculate the widths for each column. For the
790 * 'source' column, we kludge it up by taking the worst-case scenario of
791 * inheriting from the longest name. This is acceptable because in the
792 * majority of cases 'SOURCE' is the last column displayed, and we don't
793 * use the width anyway. Note that the 'VALUE' column can be oversized,
794 * if the name of the property is much longer the any values we find.
795 */
796 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
797 /*
798 * 'PROPERTY' column
799 */
800 if (pl->pl_prop != ZPROP_INVAL) {
801 const char *propname = (type == ZFS_TYPE_POOL) ?
802 zpool_prop_to_name(pl->pl_prop) :
803 zfs_prop_to_name(pl->pl_prop);
804
805 len = strlen(propname);
806 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
807 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
808 } else {
809 len = strlen(pl->pl_user_prop);
810 if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
811 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
812 }
813
814 /*
815 * 'VALUE' column
816 */
817 if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
818 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
819 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
820
821 /*
822 * 'NAME' and 'SOURCE' columns
823 */
824 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
825 ZFS_PROP_NAME) &&
826 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
827 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
828 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
829 strlen(dgettext(TEXT_DOMAIN, "inherited from"));
830 }
831 }
832
833 /*
834 * Now go through and print the headers.
835 */
836 for (i = 0; i < 4; i++) {
837 switch (cbp->cb_columns[i]) {
838 case GET_COL_NAME:
839 title = dgettext(TEXT_DOMAIN, "NAME");
840 break;
841 case GET_COL_PROPERTY:
842 title = dgettext(TEXT_DOMAIN, "PROPERTY");
843 break;
844 case GET_COL_VALUE:
845 title = dgettext(TEXT_DOMAIN, "VALUE");
846 break;
847 case GET_COL_SOURCE:
848 title = dgettext(TEXT_DOMAIN, "SOURCE");
849 break;
850 default:
851 title = NULL;
852 }
853
854 if (title != NULL) {
855 if (i == 3 || cbp->cb_columns[i + 1] == 0)
856 (void) printf("%s", title);
857 else
858 (void) printf("%-*s ",
859 cbp->cb_colwidths[cbp->cb_columns[i]],
860 title);
861 }
862 }
863 (void) printf("\n");
864 }
865
866 /*
867 * Display a single line of output, according to the settings in the callback
868 * structure.
869 */
870 void
871 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
872 const char *propname, const char *value, zprop_source_t sourcetype,
873 const char *source)
874 {
875 int i;
876 const char *str;
877 char buf[128];
878
879 /*
880 * Ignore those source types that the user has chosen to ignore.
881 */
882 if ((sourcetype & cbp->cb_sources) == 0)
883 return;
884
885 if (cbp->cb_first)
886 zprop_print_headers(cbp, cbp->cb_type);
887
888 for (i = 0; i < 4; i++) {
889 switch (cbp->cb_columns[i]) {
890 case GET_COL_NAME:
891 str = name;
892 break;
893
894 case GET_COL_PROPERTY:
895 str = propname;
896 break;
897
898 case GET_COL_VALUE:
899 str = value;
900 break;
901
902 case GET_COL_SOURCE:
903 switch (sourcetype) {
904 case ZPROP_SRC_NONE:
905 str = "-";
906 break;
907
908 case ZPROP_SRC_DEFAULT:
909 str = "default";
910 break;
911
912 case ZPROP_SRC_LOCAL:
913 str = "local";
914 break;
915
916 case ZPROP_SRC_TEMPORARY:
917 str = "temporary";
918 break;
919
920 case ZPROP_SRC_INHERITED:
921 (void) snprintf(buf, sizeof (buf),
922 "inherited from %s", source);
923 str = buf;
924 break;
925 }
926 break;
927
928 default:
929 continue;
930 }
931
932 if (cbp->cb_columns[i + 1] == 0)
933 (void) printf("%s", str);
934 else if (cbp->cb_scripted)
935 (void) printf("%s\t", str);
936 else
937 (void) printf("%-*s ",
938 cbp->cb_colwidths[cbp->cb_columns[i]],
939 str);
940
941 }
942
943 (void) printf("\n");
944 }
945
946 /*
947 * Given a numeric suffix, convert the value into a number of bits that the
948 * resulting value must be shifted.
949 */
950 static int
951 str2shift(libzfs_handle_t *hdl, const char *buf)
952 {
953 const char *ends = "BKMGTPEZ";
954 int i;
955
956 if (buf[0] == '\0')
957 return (0);
958 for (i = 0; i < strlen(ends); i++) {
959 if (toupper(buf[0]) == ends[i])
960 break;
961 }
962 if (i == strlen(ends)) {
963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 "invalid numeric suffix '%s'"), buf);
965 return (-1);
966 }
967
968 /*
969 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
970 * allow 'BB' - that's just weird.
971 */
972 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
973 toupper(buf[0]) != 'B'))
974 return (10*i);
975
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "invalid numeric suffix '%s'"), buf);
978 return (-1);
979 }
980
981 /*
982 * Convert a string of the form '100G' into a real number. Used when setting
983 * properties or creating a volume. 'buf' is used to place an extended error
984 * message for the caller to use.
985 */
986 int
987 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
988 {
989 char *end;
990 int shift;
991
992 *num = 0;
993
994 /* Check to see if this looks like a number. */
995 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
996 if (hdl)
997 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998 "bad numeric value '%s'"), value);
999 return (-1);
1000 }
1001
1002 /* Rely on stroll() to process the numeric portion. */
1003 errno = 0;
1004 *num = strtoll(value, &end, 10);
1005
1006 /*
1007 * Check for ERANGE, which indicates that the value is too large to fit
1008 * in a 64-bit value.
1009 */
1010 if (errno == ERANGE) {
1011 if (hdl)
1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013 "numeric value is too large"));
1014 return (-1);
1015 }
1016
1017 /*
1018 * If we have a decimal value, then do the computation with floating
1019 * point arithmetic. Otherwise, use standard arithmetic.
1020 */
1021 if (*end == '.') {
1022 double fval = strtod(value, &end);
1023
1024 if ((shift = str2shift(hdl, end)) == -1)
1025 return (-1);
1026
1027 fval *= pow(2, shift);
1028
1029 if (fval > UINT64_MAX) {
1030 if (hdl)
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "numeric value is too large"));
1033 return (-1);
1034 }
1035
1036 *num = (uint64_t)fval;
1037 } else {
1038 if ((shift = str2shift(hdl, end)) == -1)
1039 return (-1);
1040
1041 /* Check for overflow */
1042 if (shift >= 64 || (*num << shift) >> shift != *num) {
1043 if (hdl)
1044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1045 "numeric value is too large"));
1046 return (-1);
1047 }
1048
1049 *num <<= shift;
1050 }
1051
1052 return (0);
1053 }
1054
1055 /*
1056 * Given a propname=value nvpair to set, parse any numeric properties
1057 * (index, boolean, etc) if they are specified as strings and add the
1058 * resulting nvpair to the returned nvlist.
1059 *
1060 * At the DSL layer, all properties are either 64-bit numbers or strings.
1061 * We want the user to be able to ignore this fact and specify properties
1062 * as native values (numbers, for example) or as strings (to simplify
1063 * command line utilities). This also handles converting index types
1064 * (compression, checksum, etc) from strings to their on-disk index.
1065 */
1066 int
1067 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1068 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1069 const char *errbuf)
1070 {
1071 data_type_t datatype = nvpair_type(elem);
1072 zprop_type_t proptype;
1073 const char *propname;
1074 char *value;
1075 boolean_t isnone = B_FALSE;
1076
1077 if (type == ZFS_TYPE_POOL) {
1078 proptype = zpool_prop_get_type(prop);
1079 propname = zpool_prop_to_name(prop);
1080 } else {
1081 proptype = zfs_prop_get_type(prop);
1082 propname = zfs_prop_to_name(prop);
1083 }
1084
1085 /*
1086 * Convert any properties to the internal DSL value types.
1087 */
1088 *svalp = NULL;
1089 *ivalp = 0;
1090
1091 switch (proptype) {
1092 case PROP_TYPE_STRING:
1093 if (datatype != DATA_TYPE_STRING) {
1094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1095 "'%s' must be a string"), nvpair_name(elem));
1096 goto error;
1097 }
1098 (void) nvpair_value_string(elem, svalp);
1099 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1101 "'%s' is too long"), nvpair_name(elem));
1102 goto error;
1103 }
1104 break;
1105
1106 case PROP_TYPE_NUMBER:
1107 if (datatype == DATA_TYPE_STRING) {
1108 (void) nvpair_value_string(elem, &value);
1109 if (strcmp(value, "none") == 0) {
1110 isnone = B_TRUE;
1111 } else if (zfs_nicestrtonum(hdl, value, ivalp)
1112 != 0) {
1113 goto error;
1114 }
1115 } else if (datatype == DATA_TYPE_UINT64) {
1116 (void) nvpair_value_uint64(elem, ivalp);
1117 } else {
1118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1119 "'%s' must be a number"), nvpair_name(elem));
1120 goto error;
1121 }
1122
1123 /*
1124 * Quota special: force 'none' and don't allow 0.
1125 */
1126 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1127 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129 "use 'none' to disable quota/refquota"));
1130 goto error;
1131 }
1132 break;
1133
1134 case PROP_TYPE_INDEX:
1135 if (datatype != DATA_TYPE_STRING) {
1136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1137 "'%s' must be a string"), nvpair_name(elem));
1138 goto error;
1139 }
1140
1141 (void) nvpair_value_string(elem, &value);
1142
1143 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1145 "'%s' must be one of '%s'"), propname,
1146 zprop_values(prop, type));
1147 goto error;
1148 }
1149 break;
1150
1151 default:
1152 abort();
1153 }
1154
1155 /*
1156 * Add the result to our return set of properties.
1157 */
1158 if (*svalp != NULL) {
1159 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1160 (void) no_memory(hdl);
1161 return (-1);
1162 }
1163 } else {
1164 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1165 (void) no_memory(hdl);
1166 return (-1);
1167 }
1168 }
1169
1170 return (0);
1171 error:
1172 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1173 return (-1);
1174 }
1175
1176 /*
1177 * Given a comma-separated list of properties, construct a property list
1178 * containing both user-defined and native properties. This function will
1179 * return a NULL list if 'all' is specified, which can later be expanded
1180 * by zprop_expand_list().
1181 */
1182 int
1183 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1184 zfs_type_t type)
1185 {
1186 size_t len;
1187 char *s, *p;
1188 char c;
1189 int prop;
1190 zprop_list_t *entry;
1191 zprop_list_t **last;
1192
1193 *listp = NULL;
1194 last = listp;
1195
1196 /*
1197 * If 'all' is specified, return a NULL list.
1198 */
1199 if (strcmp(props, "all") == 0)
1200 return (0);
1201
1202 /*
1203 * If no props were specified, return an error.
1204 */
1205 if (props[0] == '\0') {
1206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1207 "no properties specified"));
1208 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1209 "bad property list")));
1210 }
1211
1212 /*
1213 * It would be nice to use getsubopt() here, but the inclusion of column
1214 * aliases makes this more effort than it's worth.
1215 */
1216 s = props;
1217 while (*s != '\0') {
1218 if ((p = strchr(s, ',')) == NULL) {
1219 len = strlen(s);
1220 p = s + len;
1221 } else {
1222 len = p - s;
1223 }
1224
1225 /*
1226 * Check for empty options.
1227 */
1228 if (len == 0) {
1229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1230 "empty property name"));
1231 return (zfs_error(hdl, EZFS_BADPROP,
1232 dgettext(TEXT_DOMAIN, "bad property list")));
1233 }
1234
1235 /*
1236 * Check all regular property names.
1237 */
1238 c = s[len];
1239 s[len] = '\0';
1240 prop = zprop_name_to_prop(s, type);
1241
1242 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1243 prop = ZPROP_INVAL;
1244
1245 /*
1246 * When no property table entry can be found, return failure if
1247 * this is a pool property or if this isn't a user-defined
1248 * dataset property,
1249 */
1250 if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1251 !zfs_prop_user(s))) {
1252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253 "invalid property '%s'"), s);
1254 return (zfs_error(hdl, EZFS_BADPROP,
1255 dgettext(TEXT_DOMAIN, "bad property list")));
1256 }
1257
1258 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1259 return (-1);
1260
1261 entry->pl_prop = prop;
1262 if (prop == ZPROP_INVAL) {
1263 if ((entry->pl_user_prop = zfs_strdup(hdl, s))
1264 == NULL) {
1265 free(entry);
1266 return (-1);
1267 }
1268 entry->pl_width = strlen(s);
1269 } else {
1270 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1271 type);
1272 }
1273
1274 *last = entry;
1275 last = &entry->pl_next;
1276
1277 s = p;
1278 if (c == ',')
1279 s++;
1280 }
1281
1282 return (0);
1283 }
1284
1285 void
1286 zprop_free_list(zprop_list_t *pl)
1287 {
1288 zprop_list_t *next;
1289
1290 while (pl != NULL) {
1291 next = pl->pl_next;
1292 free(pl->pl_user_prop);
1293 free(pl);
1294 pl = next;
1295 }
1296 }
1297
1298 typedef struct expand_data {
1299 zprop_list_t **last;
1300 libzfs_handle_t *hdl;
1301 zfs_type_t type;
1302 } expand_data_t;
1303
1304 int
1305 zprop_expand_list_cb(int prop, void *cb)
1306 {
1307 zprop_list_t *entry;
1308 expand_data_t *edp = cb;
1309
1310 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1311 return (ZPROP_INVAL);
1312
1313 entry->pl_prop = prop;
1314 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1315 entry->pl_all = B_TRUE;
1316
1317 *(edp->last) = entry;
1318 edp->last = &entry->pl_next;
1319
1320 return (ZPROP_CONT);
1321 }
1322
1323 int
1324 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1325 {
1326 zprop_list_t *entry;
1327 zprop_list_t **last;
1328 expand_data_t exp;
1329
1330 if (*plp == NULL) {
1331 /*
1332 * If this is the very first time we've been called for an 'all'
1333 * specification, expand the list to include all native
1334 * properties.
1335 */
1336 last = plp;
1337
1338 exp.last = last;
1339 exp.hdl = hdl;
1340 exp.type = type;
1341
1342 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1343 B_FALSE, type) == ZPROP_INVAL)
1344 return (-1);
1345
1346 /*
1347 * Add 'name' to the beginning of the list, which is handled
1348 * specially.
1349 */
1350 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1351 return (-1);
1352
1353 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME :
1354 ZFS_PROP_NAME;
1355 entry->pl_width = zprop_width(entry->pl_prop,
1356 &entry->pl_fixed, type);
1357 entry->pl_all = B_TRUE;
1358 entry->pl_next = *plp;
1359 *plp = entry;
1360 }
1361 return (0);
1362 }
1363
1364 int
1365 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1366 zfs_type_t type)
1367 {
1368 return (zprop_iter_common(func, cb, show_all, ordered, type));
1369 }