]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_util.c
MFC r336945: MFV r336944: 9286 want refreservation=auto
[FreeBSD/FreeBSD.git] / cddl / contrib / opensolaris / lib / libzfs / common / libzfs_util.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2018 Joyent, Inc.
25  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
26  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27  * Copyright (c) 2017 Datto Inc.
28  */
29
30 /*
31  * Internal utility routines for the ZFS library.
32  */
33
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
37 #include <sys/stat.h>
38
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <libintl.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <strings.h>
46 #include <unistd.h>
47 #include <ctype.h>
48 #include <math.h>
49 #include <sys/mnttab.h>
50 #include <sys/mntent.h>
51 #include <sys/types.h>
52 #include <libcmdutils.h>
53
54 #include <libzfs.h>
55 #include <libzfs_core.h>
56
57 #include "libzfs_impl.h"
58 #include "zfs_prop.h"
59 #include "zfeature_common.h"
60
61
62 int
63 libzfs_errno(libzfs_handle_t *hdl)
64 {
65         return (hdl->libzfs_error);
66 }
67
68 const char *
69 libzfs_error_action(libzfs_handle_t *hdl)
70 {
71         return (hdl->libzfs_action);
72 }
73
74 const char *
75 libzfs_error_description(libzfs_handle_t *hdl)
76 {
77         if (hdl->libzfs_desc[0] != '\0')
78                 return (hdl->libzfs_desc);
79
80         switch (hdl->libzfs_error) {
81         case EZFS_NOMEM:
82                 return (dgettext(TEXT_DOMAIN, "out of memory"));
83         case EZFS_BADPROP:
84                 return (dgettext(TEXT_DOMAIN, "invalid property value"));
85         case EZFS_PROPREADONLY:
86                 return (dgettext(TEXT_DOMAIN, "read-only property"));
87         case EZFS_PROPTYPE:
88                 return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
89                     "datasets of this type"));
90         case EZFS_PROPNONINHERIT:
91                 return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
92         case EZFS_PROPSPACE:
93                 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
94         case EZFS_BADTYPE:
95                 return (dgettext(TEXT_DOMAIN, "operation not applicable to "
96                     "datasets of this type"));
97         case EZFS_BUSY:
98                 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
99         case EZFS_EXISTS:
100                 return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
101         case EZFS_NOENT:
102                 return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
103         case EZFS_BADSTREAM:
104                 return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
105         case EZFS_DSREADONLY:
106                 return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
107         case EZFS_VOLTOOBIG:
108                 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
109                     "this system"));
110         case EZFS_INVALIDNAME:
111                 return (dgettext(TEXT_DOMAIN, "invalid name"));
112         case EZFS_BADRESTORE:
113                 return (dgettext(TEXT_DOMAIN, "unable to restore to "
114                     "destination"));
115         case EZFS_BADBACKUP:
116                 return (dgettext(TEXT_DOMAIN, "backup failed"));
117         case EZFS_BADTARGET:
118                 return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
119         case EZFS_NODEVICE:
120                 return (dgettext(TEXT_DOMAIN, "no such device in pool"));
121         case EZFS_BADDEV:
122                 return (dgettext(TEXT_DOMAIN, "invalid device"));
123         case EZFS_NOREPLICAS:
124                 return (dgettext(TEXT_DOMAIN, "no valid replicas"));
125         case EZFS_RESILVERING:
126                 return (dgettext(TEXT_DOMAIN, "currently resilvering"));
127         case EZFS_BADVERSION:
128                 return (dgettext(TEXT_DOMAIN, "unsupported version or "
129                     "feature"));
130         case EZFS_POOLUNAVAIL:
131                 return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
132         case EZFS_DEVOVERFLOW:
133                 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
134         case EZFS_BADPATH:
135                 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
136         case EZFS_CROSSTARGET:
137                 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
138                     "pools"));
139         case EZFS_ZONED:
140                 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
141         case EZFS_MOUNTFAILED:
142                 return (dgettext(TEXT_DOMAIN, "mount failed"));
143         case EZFS_UMOUNTFAILED:
144                 return (dgettext(TEXT_DOMAIN, "umount failed"));
145         case EZFS_UNSHARENFSFAILED:
146                 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
147         case EZFS_SHARENFSFAILED:
148                 return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
149         case EZFS_UNSHARESMBFAILED:
150                 return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
151         case EZFS_SHARESMBFAILED:
152                 return (dgettext(TEXT_DOMAIN, "smb add share failed"));
153         case EZFS_PERM:
154                 return (dgettext(TEXT_DOMAIN, "permission denied"));
155         case EZFS_NOSPC:
156                 return (dgettext(TEXT_DOMAIN, "out of space"));
157         case EZFS_FAULT:
158                 return (dgettext(TEXT_DOMAIN, "bad address"));
159         case EZFS_IO:
160                 return (dgettext(TEXT_DOMAIN, "I/O error"));
161         case EZFS_INTR:
162                 return (dgettext(TEXT_DOMAIN, "signal received"));
163         case EZFS_ISSPARE:
164                 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
165                     "spare"));
166         case EZFS_INVALCONFIG:
167                 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
168         case EZFS_RECURSIVE:
169                 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
170         case EZFS_NOHISTORY:
171                 return (dgettext(TEXT_DOMAIN, "no history available"));
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_BADCACHE:
200                 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
201         case EZFS_ISL2CACHE:
202                 return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
203         case EZFS_VDEVNOTSUP:
204                 return (dgettext(TEXT_DOMAIN, "vdev specification is not "
205                     "supported"));
206         case EZFS_NOTSUP:
207                 return (dgettext(TEXT_DOMAIN, "operation not supported "
208                     "on this dataset"));
209         case EZFS_ACTIVE_SPARE:
210                 return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
211                     "device"));
212         case EZFS_UNPLAYED_LOGS:
213                 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
214                     "logs"));
215         case EZFS_REFTAG_RELE:
216                 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
217         case EZFS_REFTAG_HOLD:
218                 return (dgettext(TEXT_DOMAIN, "tag already exists on this "
219                     "dataset"));
220         case EZFS_TAGTOOLONG:
221                 return (dgettext(TEXT_DOMAIN, "tag too long"));
222         case EZFS_PIPEFAILED:
223                 return (dgettext(TEXT_DOMAIN, "pipe create failed"));
224         case EZFS_THREADCREATEFAILED:
225                 return (dgettext(TEXT_DOMAIN, "thread create failed"));
226         case EZFS_POSTSPLIT_ONLINE:
227                 return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
228                     "into a new one"));
229         case EZFS_SCRUB_PAUSED:
230                 return (dgettext(TEXT_DOMAIN, "scrub is paused; "
231                     "use 'zpool scrub' to resume"));
232         case EZFS_SCRUBBING:
233                 return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
234                     "use 'zpool scrub -s' to cancel current scrub"));
235         case EZFS_NO_SCRUB:
236                 return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
237         case EZFS_DIFF:
238                 return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
239         case EZFS_DIFFDATA:
240                 return (dgettext(TEXT_DOMAIN, "invalid diff data"));
241         case EZFS_POOLREADONLY:
242                 return (dgettext(TEXT_DOMAIN, "pool is read-only"));
243         case EZFS_NO_PENDING:
244                 return (dgettext(TEXT_DOMAIN, "operation is not "
245                     "in progress"));
246         case EZFS_CHECKPOINT_EXISTS:
247                 return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
248         case EZFS_DISCARDING_CHECKPOINT:
249                 return (dgettext(TEXT_DOMAIN, "currently discarding "
250                     "checkpoint"));
251         case EZFS_NO_CHECKPOINT:
252                 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
253         case EZFS_DEVRM_IN_PROGRESS:
254                 return (dgettext(TEXT_DOMAIN, "device removal in progress"));
255         case EZFS_VDEV_TOO_BIG:
256                 return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
257         case EZFS_UNKNOWN:
258                 return (dgettext(TEXT_DOMAIN, "unknown error"));
259         default:
260                 assert(hdl->libzfs_error == 0);
261                 return (dgettext(TEXT_DOMAIN, "no error"));
262         }
263 }
264
265 /*PRINTFLIKE2*/
266 void
267 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
268 {
269         va_list ap;
270
271         va_start(ap, fmt);
272
273         (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
274             fmt, ap);
275         hdl->libzfs_desc_active = 1;
276
277         va_end(ap);
278 }
279
280 static void
281 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
282 {
283         (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
284             fmt, ap);
285         hdl->libzfs_error = error;
286
287         if (hdl->libzfs_desc_active)
288                 hdl->libzfs_desc_active = 0;
289         else
290                 hdl->libzfs_desc[0] = '\0';
291
292         if (hdl->libzfs_printerr) {
293                 if (error == EZFS_UNKNOWN) {
294                         (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
295                             "error: %s\n"), libzfs_error_description(hdl));
296                         abort();
297                 }
298
299                 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
300                     libzfs_error_description(hdl));
301                 if (error == EZFS_NOMEM)
302                         exit(1);
303         }
304 }
305
306 int
307 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
308 {
309         return (zfs_error_fmt(hdl, error, "%s", msg));
310 }
311
312 /*PRINTFLIKE3*/
313 int
314 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
315 {
316         va_list ap;
317
318         va_start(ap, fmt);
319
320         zfs_verror(hdl, error, fmt, ap);
321
322         va_end(ap);
323
324         return (-1);
325 }
326
327 static int
328 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
329     va_list ap)
330 {
331         switch (error) {
332         case EPERM:
333         case EACCES:
334                 zfs_verror(hdl, EZFS_PERM, fmt, ap);
335                 return (-1);
336
337         case ECANCELED:
338                 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
339                 return (-1);
340
341         case EIO:
342                 zfs_verror(hdl, EZFS_IO, fmt, ap);
343                 return (-1);
344
345         case EFAULT:
346                 zfs_verror(hdl, EZFS_FAULT, fmt, ap);
347                 return (-1);
348
349         case EINTR:
350                 zfs_verror(hdl, EZFS_INTR, fmt, ap);
351                 return (-1);
352         }
353
354         return (0);
355 }
356
357 int
358 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
359 {
360         return (zfs_standard_error_fmt(hdl, error, "%s", msg));
361 }
362
363 /*PRINTFLIKE3*/
364 int
365 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
366 {
367         va_list ap;
368
369         va_start(ap, fmt);
370
371         if (zfs_common_error(hdl, error, fmt, ap) != 0) {
372                 va_end(ap);
373                 return (-1);
374         }
375
376         switch (error) {
377         case ENXIO:
378         case ENODEV:
379         case EPIPE:
380                 zfs_verror(hdl, EZFS_IO, fmt, ap);
381                 break;
382
383         case ENOENT:
384                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
385                     "dataset does not exist"));
386                 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
387                 break;
388
389         case ENOSPC:
390         case EDQUOT:
391                 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
392                 va_end(ap);
393                 return (-1);
394
395         case EEXIST:
396                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
397                     "dataset already exists"));
398                 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
399                 break;
400
401         case EBUSY:
402                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
403                     "dataset is busy"));
404                 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
405                 break;
406         case EROFS:
407                 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
408                 break;
409         case ENAMETOOLONG:
410                 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
411                 break;
412         case ENOTSUP:
413                 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
414                 break;
415         case EAGAIN:
416                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
417                     "pool I/O is currently suspended"));
418                 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
419                 break;
420         default:
421                 zfs_error_aux(hdl, strerror(error));
422                 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
423                 break;
424         }
425
426         va_end(ap);
427         return (-1);
428 }
429
430 int
431 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
432 {
433         return (zpool_standard_error_fmt(hdl, error, "%s", msg));
434 }
435
436 /*PRINTFLIKE3*/
437 int
438 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
439 {
440         va_list ap;
441
442         va_start(ap, fmt);
443
444         if (zfs_common_error(hdl, error, fmt, ap) != 0) {
445                 va_end(ap);
446                 return (-1);
447         }
448
449         switch (error) {
450         case ENODEV:
451                 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
452                 break;
453
454         case ENOENT:
455                 zfs_error_aux(hdl,
456                     dgettext(TEXT_DOMAIN, "no such pool or dataset"));
457                 zfs_verror(hdl, EZFS_NOENT, fmt, ap);
458                 break;
459
460         case EEXIST:
461                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
462                     "pool already exists"));
463                 zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
464                 break;
465
466         case EBUSY:
467                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
468                 zfs_verror(hdl, EZFS_BUSY, fmt, ap);
469                 break;
470
471         case ENXIO:
472                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
473                     "one or more devices is currently unavailable"));
474                 zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
475                 break;
476
477         case ENAMETOOLONG:
478                 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
479                 break;
480
481         case ENOTSUP:
482                 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
483                 break;
484
485         case EINVAL:
486                 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
487                 break;
488
489         case ENOSPC:
490         case EDQUOT:
491                 zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
492                 va_end(ap);
493                 return (-1);
494
495         case EAGAIN:
496                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
497                     "pool I/O is currently suspended"));
498                 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
499                 break;
500
501         case EROFS:
502                 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
503                 break;
504         /* There is no pending operation to cancel */
505         case ESRCH:
506                 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
507                 break;
508         case ZFS_ERR_CHECKPOINT_EXISTS:
509                 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
510                 break;
511         case ZFS_ERR_DISCARDING_CHECKPOINT:
512                 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
513                 break;
514         case ZFS_ERR_NO_CHECKPOINT:
515                 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
516                 break;
517         case ZFS_ERR_DEVRM_IN_PROGRESS:
518                 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
519                 break;
520         case ZFS_ERR_VDEV_TOO_BIG:
521                 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
522                 break;
523         default:
524                 zfs_error_aux(hdl, strerror(error));
525                 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
526         }
527
528         va_end(ap);
529         return (-1);
530 }
531
532 /*
533  * Display an out of memory error message and abort the current program.
534  */
535 int
536 no_memory(libzfs_handle_t *hdl)
537 {
538         return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
539 }
540
541 /*
542  * A safe form of malloc() which will die if the allocation fails.
543  */
544 void *
545 zfs_alloc(libzfs_handle_t *hdl, size_t size)
546 {
547         void *data;
548
549         if ((data = calloc(1, size)) == NULL)
550                 (void) no_memory(hdl);
551
552         return (data);
553 }
554
555 /*
556  * A safe form of asprintf() which will die if the allocation fails.
557  */
558 /*PRINTFLIKE2*/
559 char *
560 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
561 {
562         va_list ap;
563         char *ret;
564         int err;
565
566         va_start(ap, fmt);
567
568         err = vasprintf(&ret, fmt, ap);
569
570         va_end(ap);
571
572         if (err < 0)
573                 (void) no_memory(hdl);
574
575         return (ret);
576 }
577
578 /*
579  * A safe form of realloc(), which also zeroes newly allocated space.
580  */
581 void *
582 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
583 {
584         void *ret;
585
586         if ((ret = realloc(ptr, newsize)) == NULL) {
587                 (void) no_memory(hdl);
588                 return (NULL);
589         }
590
591         bzero((char *)ret + oldsize, (newsize - oldsize));
592         return (ret);
593 }
594
595 /*
596  * A safe form of strdup() which will die if the allocation fails.
597  */
598 char *
599 zfs_strdup(libzfs_handle_t *hdl, const char *str)
600 {
601         char *ret;
602
603         if ((ret = strdup(str)) == NULL)
604                 (void) no_memory(hdl);
605
606         return (ret);
607 }
608
609 /*
610  * Convert a number to an appropriately human-readable output.
611  */
612 void
613 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
614 {
615         nicenum(num, buf, buflen);
616 }
617
618 void
619 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
620 {
621         hdl->libzfs_printerr = printerr;
622 }
623
624 static int
625 libzfs_load(void)
626 {
627         int error;
628
629         if (modfind("zfs") < 0) {
630                 /* Not present in kernel, try loading it. */
631                 if (kldload("zfs") < 0 || modfind("zfs") < 0) {
632                         if (errno != EEXIST)
633                                 return (-1);
634                 }
635         }
636         return (0);
637 }
638
639 libzfs_handle_t *
640 libzfs_init(void)
641 {
642         libzfs_handle_t *hdl;
643
644         if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
645                 return (NULL);
646         }
647
648         if (libzfs_load() < 0) {
649                 free(hdl);
650                 return (NULL);
651         }
652
653         if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
654                 free(hdl);
655                 return (NULL);
656         }
657
658         if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
659                 (void) close(hdl->libzfs_fd);
660                 free(hdl);
661                 return (NULL);
662         }
663
664         hdl->libzfs_sharetab = fopen(ZFS_EXPORTS_PATH, "r");
665
666         if (libzfs_core_init() != 0) {
667                 (void) close(hdl->libzfs_fd);
668                 (void) fclose(hdl->libzfs_mnttab);
669                 (void) fclose(hdl->libzfs_sharetab);
670                 free(hdl);
671                 return (NULL);
672         }
673
674         zfs_prop_init();
675         zpool_prop_init();
676         zpool_feature_init();
677         libzfs_mnttab_init(hdl);
678
679         if (getenv("ZFS_PROP_DEBUG") != NULL) {
680                 hdl->libzfs_prop_debug = B_TRUE;
681         }
682
683         return (hdl);
684 }
685
686 void
687 libzfs_fini(libzfs_handle_t *hdl)
688 {
689         (void) close(hdl->libzfs_fd);
690         if (hdl->libzfs_mnttab)
691                 (void) fclose(hdl->libzfs_mnttab);
692         if (hdl->libzfs_sharetab)
693                 (void) fclose(hdl->libzfs_sharetab);
694         zfs_uninit_libshare(hdl);
695         zpool_free_handles(hdl);
696 #ifdef illumos
697         libzfs_fru_clear(hdl, B_TRUE);
698 #endif
699         namespace_clear(hdl);
700         libzfs_mnttab_fini(hdl);
701         libzfs_core_fini();
702         free(hdl);
703 }
704
705 libzfs_handle_t *
706 zpool_get_handle(zpool_handle_t *zhp)
707 {
708         return (zhp->zpool_hdl);
709 }
710
711 libzfs_handle_t *
712 zfs_get_handle(zfs_handle_t *zhp)
713 {
714         return (zhp->zfs_hdl);
715 }
716
717 zpool_handle_t *
718 zfs_get_pool_handle(const zfs_handle_t *zhp)
719 {
720         return (zhp->zpool_hdl);
721 }
722
723 /*
724  * Given a name, determine whether or not it's a valid path
725  * (starts with '/' or "./").  If so, walk the mnttab trying
726  * to match the device number.  If not, treat the path as an
727  * fs/vol/snap/bkmark name.
728  */
729 zfs_handle_t *
730 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
731 {
732         struct stat64 statbuf;
733         struct extmnttab entry;
734         int ret;
735
736         if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
737                 /*
738                  * It's not a valid path, assume it's a name of type 'argtype'.
739                  */
740                 return (zfs_open(hdl, path, argtype));
741         }
742
743         if (stat64(path, &statbuf) != 0) {
744                 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
745                 return (NULL);
746         }
747
748 #ifdef illumos
749         rewind(hdl->libzfs_mnttab);
750         while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
751                 if (makedevice(entry.mnt_major, entry.mnt_minor) ==
752                     statbuf.st_dev) {
753                         break;
754                 }
755         }
756 #else
757         {
758                 struct statfs sfs;
759
760                 ret = statfs(path, &sfs);
761                 if (ret == 0)
762                         statfs2mnttab(&sfs, &entry);
763                 else {
764                         (void) fprintf(stderr, "%s: %s\n", path,
765                             strerror(errno));
766                 }
767         }
768 #endif  /* illumos */
769         if (ret != 0) {
770                 return (NULL);
771         }
772
773         if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
774                 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
775                     path);
776                 return (NULL);
777         }
778
779         return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
780 }
781
782 /*
783  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
784  * an ioctl().
785  */
786 int
787 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
788 {
789         if (len == 0)
790                 len = 16 * 1024;
791         zc->zc_nvlist_dst_size = len;
792         zc->zc_nvlist_dst =
793             (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
794         if (zc->zc_nvlist_dst == 0)
795                 return (-1);
796
797         return (0);
798 }
799
800 /*
801  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
802  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
803  * filled in by the kernel to indicate the actual required size.
804  */
805 int
806 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
807 {
808         free((void *)(uintptr_t)zc->zc_nvlist_dst);
809         zc->zc_nvlist_dst =
810             (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
811         if (zc->zc_nvlist_dst == 0)
812                 return (-1);
813
814         return (0);
815 }
816
817 /*
818  * Called to free the src and dst nvlists stored in the command structure.
819  */
820 void
821 zcmd_free_nvlists(zfs_cmd_t *zc)
822 {
823         free((void *)(uintptr_t)zc->zc_nvlist_conf);
824         free((void *)(uintptr_t)zc->zc_nvlist_src);
825         free((void *)(uintptr_t)zc->zc_nvlist_dst);
826         zc->zc_nvlist_conf = NULL;
827         zc->zc_nvlist_src = NULL;
828         zc->zc_nvlist_dst = NULL;
829 }
830
831 static int
832 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
833     nvlist_t *nvl)
834 {
835         char *packed;
836         size_t len;
837
838         verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
839
840         if ((packed = zfs_alloc(hdl, len)) == NULL)
841                 return (-1);
842
843         verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
844
845         *outnv = (uint64_t)(uintptr_t)packed;
846         *outlen = len;
847
848         return (0);
849 }
850
851 int
852 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
853 {
854         return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
855             &zc->zc_nvlist_conf_size, nvl));
856 }
857
858 int
859 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
860 {
861         return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
862             &zc->zc_nvlist_src_size, nvl));
863 }
864
865 /*
866  * Unpacks an nvlist from the ZFS ioctl command structure.
867  */
868 int
869 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
870 {
871         if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
872             zc->zc_nvlist_dst_size, nvlp, 0) != 0)
873                 return (no_memory(hdl));
874
875         return (0);
876 }
877
878 int
879 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
880 {
881         return (ioctl(hdl->libzfs_fd, request, zc));
882 }
883
884 /*
885  * ================================================================
886  * API shared by zfs and zpool property management
887  * ================================================================
888  */
889
890 static void
891 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
892 {
893         zprop_list_t *pl = cbp->cb_proplist;
894         int i;
895         char *title;
896         size_t len;
897
898         cbp->cb_first = B_FALSE;
899         if (cbp->cb_scripted)
900                 return;
901
902         /*
903          * Start with the length of the column headers.
904          */
905         cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
906         cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
907             "PROPERTY"));
908         cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
909             "VALUE"));
910         cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
911             "RECEIVED"));
912         cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
913             "SOURCE"));
914
915         /* first property is always NAME */
916         assert(cbp->cb_proplist->pl_prop ==
917             ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
918
919         /*
920          * Go through and calculate the widths for each column.  For the
921          * 'source' column, we kludge it up by taking the worst-case scenario of
922          * inheriting from the longest name.  This is acceptable because in the
923          * majority of cases 'SOURCE' is the last column displayed, and we don't
924          * use the width anyway.  Note that the 'VALUE' column can be oversized,
925          * if the name of the property is much longer than any values we find.
926          */
927         for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
928                 /*
929                  * 'PROPERTY' column
930                  */
931                 if (pl->pl_prop != ZPROP_INVAL) {
932                         const char *propname = (type == ZFS_TYPE_POOL) ?
933                             zpool_prop_to_name(pl->pl_prop) :
934                             zfs_prop_to_name(pl->pl_prop);
935
936                         len = strlen(propname);
937                         if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
938                                 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
939                 } else {
940                         len = strlen(pl->pl_user_prop);
941                         if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
942                                 cbp->cb_colwidths[GET_COL_PROPERTY] = len;
943                 }
944
945                 /*
946                  * 'VALUE' column.  The first property is always the 'name'
947                  * property that was tacked on either by /sbin/zfs's
948                  * zfs_do_get() or when calling zprop_expand_list(), so we
949                  * ignore its width.  If the user specified the name property
950                  * to display, then it will be later in the list in any case.
951                  */
952                 if (pl != cbp->cb_proplist &&
953                     pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
954                         cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
955
956                 /* 'RECEIVED' column. */
957                 if (pl != cbp->cb_proplist &&
958                     pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
959                         cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
960
961                 /*
962                  * 'NAME' and 'SOURCE' columns
963                  */
964                 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
965                     ZFS_PROP_NAME) &&
966                     pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
967                         cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
968                         cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
969                             strlen(dgettext(TEXT_DOMAIN, "inherited from"));
970                 }
971         }
972
973         /*
974          * Now go through and print the headers.
975          */
976         for (i = 0; i < ZFS_GET_NCOLS; i++) {
977                 switch (cbp->cb_columns[i]) {
978                 case GET_COL_NAME:
979                         title = dgettext(TEXT_DOMAIN, "NAME");
980                         break;
981                 case GET_COL_PROPERTY:
982                         title = dgettext(TEXT_DOMAIN, "PROPERTY");
983                         break;
984                 case GET_COL_VALUE:
985                         title = dgettext(TEXT_DOMAIN, "VALUE");
986                         break;
987                 case GET_COL_RECVD:
988                         title = dgettext(TEXT_DOMAIN, "RECEIVED");
989                         break;
990                 case GET_COL_SOURCE:
991                         title = dgettext(TEXT_DOMAIN, "SOURCE");
992                         break;
993                 default:
994                         title = NULL;
995                 }
996
997                 if (title != NULL) {
998                         if (i == (ZFS_GET_NCOLS - 1) ||
999                             cbp->cb_columns[i + 1] == GET_COL_NONE)
1000                                 (void) printf("%s", title);
1001                         else
1002                                 (void) printf("%-*s  ",
1003                                     cbp->cb_colwidths[cbp->cb_columns[i]],
1004                                     title);
1005                 }
1006         }
1007         (void) printf("\n");
1008 }
1009
1010 /*
1011  * Display a single line of output, according to the settings in the callback
1012  * structure.
1013  */
1014 void
1015 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1016     const char *propname, const char *value, zprop_source_t sourcetype,
1017     const char *source, const char *recvd_value)
1018 {
1019         int i;
1020         const char *str = NULL;
1021         char buf[128];
1022
1023         /*
1024          * Ignore those source types that the user has chosen to ignore.
1025          */
1026         if ((sourcetype & cbp->cb_sources) == 0)
1027                 return;
1028
1029         if (cbp->cb_first)
1030                 zprop_print_headers(cbp, cbp->cb_type);
1031
1032         for (i = 0; i < ZFS_GET_NCOLS; i++) {
1033                 switch (cbp->cb_columns[i]) {
1034                 case GET_COL_NAME:
1035                         str = name;
1036                         break;
1037
1038                 case GET_COL_PROPERTY:
1039                         str = propname;
1040                         break;
1041
1042                 case GET_COL_VALUE:
1043                         str = value;
1044                         break;
1045
1046                 case GET_COL_SOURCE:
1047                         switch (sourcetype) {
1048                         case ZPROP_SRC_NONE:
1049                                 str = "-";
1050                                 break;
1051
1052                         case ZPROP_SRC_DEFAULT:
1053                                 str = "default";
1054                                 break;
1055
1056                         case ZPROP_SRC_LOCAL:
1057                                 str = "local";
1058                                 break;
1059
1060                         case ZPROP_SRC_TEMPORARY:
1061                                 str = "temporary";
1062                                 break;
1063
1064                         case ZPROP_SRC_INHERITED:
1065                                 (void) snprintf(buf, sizeof (buf),
1066                                     "inherited from %s", source);
1067                                 str = buf;
1068                                 break;
1069                         case ZPROP_SRC_RECEIVED:
1070                                 str = "received";
1071                                 break;
1072
1073                         default:
1074                                 str = NULL;
1075                                 assert(!"unhandled zprop_source_t");
1076                         }
1077                         break;
1078
1079                 case GET_COL_RECVD:
1080                         str = (recvd_value == NULL ? "-" : recvd_value);
1081                         break;
1082
1083                 default:
1084                         continue;
1085                 }
1086
1087                 if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1088                         (void) printf("%s", str);
1089                 else if (cbp->cb_scripted)
1090                         (void) printf("%s\t", str);
1091                 else
1092                         (void) printf("%-*s  ",
1093                             cbp->cb_colwidths[cbp->cb_columns[i]],
1094                             str);
1095         }
1096
1097         (void) printf("\n");
1098 }
1099
1100 /*
1101  * Given a numeric suffix, convert the value into a number of bits that the
1102  * resulting value must be shifted.
1103  */
1104 static int
1105 str2shift(libzfs_handle_t *hdl, const char *buf)
1106 {
1107         const char *ends = "BKMGTPEZ";
1108         int i;
1109
1110         if (buf[0] == '\0')
1111                 return (0);
1112         for (i = 0; i < strlen(ends); i++) {
1113                 if (toupper(buf[0]) == ends[i])
1114                         break;
1115         }
1116         if (i == strlen(ends)) {
1117                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118                     "invalid numeric suffix '%s'"), buf);
1119                 return (-1);
1120         }
1121
1122         /*
1123          * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
1124          * allow 'BB' - that's just weird.
1125          */
1126         if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1127             toupper(buf[0]) != 'B'))
1128                 return (10*i);
1129
1130         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131             "invalid numeric suffix '%s'"), buf);
1132         return (-1);
1133 }
1134
1135 /*
1136  * Convert a string of the form '100G' into a real number.  Used when setting
1137  * properties or creating a volume.  'buf' is used to place an extended error
1138  * message for the caller to use.
1139  */
1140 int
1141 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1142 {
1143         char *end;
1144         int shift;
1145
1146         *num = 0;
1147
1148         /* Check to see if this looks like a number.  */
1149         if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1150                 if (hdl)
1151                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1152                             "bad numeric value '%s'"), value);
1153                 return (-1);
1154         }
1155
1156         /* Rely on strtoull() to process the numeric portion.  */
1157         errno = 0;
1158         *num = strtoull(value, &end, 10);
1159
1160         /*
1161          * Check for ERANGE, which indicates that the value is too large to fit
1162          * in a 64-bit value.
1163          */
1164         if (errno == ERANGE) {
1165                 if (hdl)
1166                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1167                             "numeric value is too large"));
1168                 return (-1);
1169         }
1170
1171         /*
1172          * If we have a decimal value, then do the computation with floating
1173          * point arithmetic.  Otherwise, use standard arithmetic.
1174          */
1175         if (*end == '.') {
1176                 double fval = strtod(value, &end);
1177
1178                 if ((shift = str2shift(hdl, end)) == -1)
1179                         return (-1);
1180
1181                 fval *= pow(2, shift);
1182
1183                 if (fval > UINT64_MAX) {
1184                         if (hdl)
1185                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1186                                     "numeric value is too large"));
1187                         return (-1);
1188                 }
1189
1190                 *num = (uint64_t)fval;
1191         } else {
1192                 if ((shift = str2shift(hdl, end)) == -1)
1193                         return (-1);
1194
1195                 /* Check for overflow */
1196                 if (shift >= 64 || (*num << shift) >> shift != *num) {
1197                         if (hdl)
1198                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1199                                     "numeric value is too large"));
1200                         return (-1);
1201                 }
1202
1203                 *num <<= shift;
1204         }
1205
1206         return (0);
1207 }
1208
1209 /*
1210  * Given a propname=value nvpair to set, parse any numeric properties
1211  * (index, boolean, etc) if they are specified as strings and add the
1212  * resulting nvpair to the returned nvlist.
1213  *
1214  * At the DSL layer, all properties are either 64-bit numbers or strings.
1215  * We want the user to be able to ignore this fact and specify properties
1216  * as native values (numbers, for example) or as strings (to simplify
1217  * command line utilities).  This also handles converting index types
1218  * (compression, checksum, etc) from strings to their on-disk index.
1219  */
1220 int
1221 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1222     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1223     const char *errbuf)
1224 {
1225         data_type_t datatype = nvpair_type(elem);
1226         zprop_type_t proptype;
1227         const char *propname;
1228         char *value;
1229         boolean_t isnone = B_FALSE;
1230         boolean_t isauto = B_FALSE;
1231
1232         if (type == ZFS_TYPE_POOL) {
1233                 proptype = zpool_prop_get_type(prop);
1234                 propname = zpool_prop_to_name(prop);
1235         } else {
1236                 proptype = zfs_prop_get_type(prop);
1237                 propname = zfs_prop_to_name(prop);
1238         }
1239
1240         /*
1241          * Convert any properties to the internal DSL value types.
1242          */
1243         *svalp = NULL;
1244         *ivalp = 0;
1245
1246         switch (proptype) {
1247         case PROP_TYPE_STRING:
1248                 if (datatype != DATA_TYPE_STRING) {
1249                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1250                             "'%s' must be a string"), nvpair_name(elem));
1251                         goto error;
1252                 }
1253                 (void) nvpair_value_string(elem, svalp);
1254                 if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1255                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1256                             "'%s' is too long"), nvpair_name(elem));
1257                         goto error;
1258                 }
1259                 break;
1260
1261         case PROP_TYPE_NUMBER:
1262                 if (datatype == DATA_TYPE_STRING) {
1263                         (void) nvpair_value_string(elem, &value);
1264                         if (strcmp(value, "none") == 0) {
1265                                 isnone = B_TRUE;
1266                         } else if (strcmp(value, "auto") == 0) {
1267                                 isauto = B_TRUE;
1268                         } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1269                                 goto error;
1270                         }
1271                 } else if (datatype == DATA_TYPE_UINT64) {
1272                         (void) nvpair_value_uint64(elem, ivalp);
1273                 } else {
1274                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1275                             "'%s' must be a number"), nvpair_name(elem));
1276                         goto error;
1277                 }
1278
1279                 /*
1280                  * Quota special: force 'none' and don't allow 0.
1281                  */
1282                 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1283                     (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1284                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1285                             "use 'none' to disable quota/refquota"));
1286                         goto error;
1287                 }
1288
1289                 /*
1290                  * Special handling for "*_limit=none". In this case it's not
1291                  * 0 but UINT64_MAX.
1292                  */
1293                 if ((type & ZFS_TYPE_DATASET) && isnone &&
1294                     (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1295                     prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1296                         *ivalp = UINT64_MAX;
1297                 }
1298
1299                 /*
1300                  * Special handling for setting 'refreservation' to 'auto'.  Use
1301                  * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
1302                  * 'auto' is only allowed on volumes.
1303                  */
1304                 if (isauto) {
1305                         switch (prop) {
1306                         case ZFS_PROP_REFRESERVATION:
1307                                 if ((type & ZFS_TYPE_VOLUME) == 0) {
1308                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1309                                             "'%s=auto' only allowed on "
1310                                             "volumes"), nvpair_name(elem));
1311                                         goto error;
1312                                 }
1313                                 *ivalp = UINT64_MAX;
1314                                 break;
1315                         default:
1316                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1317                                     "'auto' is invalid value for '%s'"),
1318                                     nvpair_name(elem));
1319                                 goto error;
1320                         }
1321                 }
1322
1323                 break;
1324
1325         case PROP_TYPE_INDEX:
1326                 if (datatype != DATA_TYPE_STRING) {
1327                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1328                             "'%s' must be a string"), nvpair_name(elem));
1329                         goto error;
1330                 }
1331
1332                 (void) nvpair_value_string(elem, &value);
1333
1334                 if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1335                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1336                             "'%s' must be one of '%s'"), propname,
1337                             zprop_values(prop, type));
1338                         goto error;
1339                 }
1340                 break;
1341
1342         default:
1343                 abort();
1344         }
1345
1346         /*
1347          * Add the result to our return set of properties.
1348          */
1349         if (*svalp != NULL) {
1350                 if (nvlist_add_string(ret, propname, *svalp) != 0) {
1351                         (void) no_memory(hdl);
1352                         return (-1);
1353                 }
1354         } else {
1355                 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1356                         (void) no_memory(hdl);
1357                         return (-1);
1358                 }
1359         }
1360
1361         return (0);
1362 error:
1363         (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1364         return (-1);
1365 }
1366
1367 static int
1368 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
1369     zfs_type_t type)
1370 {
1371         int prop;
1372         zprop_list_t *entry;
1373
1374         prop = zprop_name_to_prop(propname, type);
1375
1376         if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1377                 prop = ZPROP_INVAL;
1378
1379         /*
1380          * When no property table entry can be found, return failure if
1381          * this is a pool property or if this isn't a user-defined
1382          * dataset property,
1383          */
1384         if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1385             !zpool_prop_feature(propname) &&
1386             !zpool_prop_unsupported(propname)) ||
1387             (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1388             !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
1389                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1390                     "invalid property '%s'"), propname);
1391                 return (zfs_error(hdl, EZFS_BADPROP,
1392                     dgettext(TEXT_DOMAIN, "bad property list")));
1393         }
1394
1395         if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1396                 return (-1);
1397
1398         entry->pl_prop = prop;
1399         if (prop == ZPROP_INVAL) {
1400                 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1401                     NULL) {
1402                         free(entry);
1403                         return (-1);
1404                 }
1405                 entry->pl_width = strlen(propname);
1406         } else {
1407                 entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1408                     type);
1409         }
1410
1411         *listp = entry;
1412
1413         return (0);
1414 }
1415
1416 /*
1417  * Given a comma-separated list of properties, construct a property list
1418  * containing both user-defined and native properties.  This function will
1419  * return a NULL list if 'all' is specified, which can later be expanded
1420  * by zprop_expand_list().
1421  */
1422 int
1423 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1424     zfs_type_t type)
1425 {
1426         *listp = NULL;
1427
1428         /*
1429          * If 'all' is specified, return a NULL list.
1430          */
1431         if (strcmp(props, "all") == 0)
1432                 return (0);
1433
1434         /*
1435          * If no props were specified, return an error.
1436          */
1437         if (props[0] == '\0') {
1438                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1439                     "no properties specified"));
1440                 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1441                     "bad property list")));
1442         }
1443
1444         /*
1445          * It would be nice to use getsubopt() here, but the inclusion of column
1446          * aliases makes this more effort than it's worth.
1447          */
1448         while (*props != '\0') {
1449                 size_t len;
1450                 char *p;
1451                 char c;
1452
1453                 if ((p = strchr(props, ',')) == NULL) {
1454                         len = strlen(props);
1455                         p = props + len;
1456                 } else {
1457                         len = p - props;
1458                 }
1459
1460                 /*
1461                  * Check for empty options.
1462                  */
1463                 if (len == 0) {
1464                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465                             "empty property name"));
1466                         return (zfs_error(hdl, EZFS_BADPROP,
1467                             dgettext(TEXT_DOMAIN, "bad property list")));
1468                 }
1469
1470                 /*
1471                  * Check all regular property names.
1472                  */
1473                 c = props[len];
1474                 props[len] = '\0';
1475
1476                 if (strcmp(props, "space") == 0) {
1477                         static char *spaceprops[] = {
1478                                 "name", "avail", "used", "usedbysnapshots",
1479                                 "usedbydataset", "usedbyrefreservation",
1480                                 "usedbychildren", NULL
1481                         };
1482                         int i;
1483
1484                         for (i = 0; spaceprops[i]; i++) {
1485                                 if (addlist(hdl, spaceprops[i], listp, type))
1486                                         return (-1);
1487                                 listp = &(*listp)->pl_next;
1488                         }
1489                 } else {
1490                         if (addlist(hdl, props, listp, type))
1491                                 return (-1);
1492                         listp = &(*listp)->pl_next;
1493                 }
1494
1495                 props = p;
1496                 if (c == ',')
1497                         props++;
1498         }
1499
1500         return (0);
1501 }
1502
1503 void
1504 zprop_free_list(zprop_list_t *pl)
1505 {
1506         zprop_list_t *next;
1507
1508         while (pl != NULL) {
1509                 next = pl->pl_next;
1510                 free(pl->pl_user_prop);
1511                 free(pl);
1512                 pl = next;
1513         }
1514 }
1515
1516 typedef struct expand_data {
1517         zprop_list_t    **last;
1518         libzfs_handle_t *hdl;
1519         zfs_type_t type;
1520 } expand_data_t;
1521
1522 int
1523 zprop_expand_list_cb(int prop, void *cb)
1524 {
1525         zprop_list_t *entry;
1526         expand_data_t *edp = cb;
1527
1528         if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1529                 return (ZPROP_INVAL);
1530
1531         entry->pl_prop = prop;
1532         entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1533         entry->pl_all = B_TRUE;
1534
1535         *(edp->last) = entry;
1536         edp->last = &entry->pl_next;
1537
1538         return (ZPROP_CONT);
1539 }
1540
1541 int
1542 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1543 {
1544         zprop_list_t *entry;
1545         zprop_list_t **last;
1546         expand_data_t exp;
1547
1548         if (*plp == NULL) {
1549                 /*
1550                  * If this is the very first time we've been called for an 'all'
1551                  * specification, expand the list to include all native
1552                  * properties.
1553                  */
1554                 last = plp;
1555
1556                 exp.last = last;
1557                 exp.hdl = hdl;
1558                 exp.type = type;
1559
1560                 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1561                     B_FALSE, type) == ZPROP_INVAL)
1562                         return (-1);
1563
1564                 /*
1565                  * Add 'name' to the beginning of the list, which is handled
1566                  * specially.
1567                  */
1568                 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1569                         return (-1);
1570
1571                 entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1572                     ZFS_PROP_NAME;
1573                 entry->pl_width = zprop_width(entry->pl_prop,
1574                     &entry->pl_fixed, type);
1575                 entry->pl_all = B_TRUE;
1576                 entry->pl_next = *plp;
1577                 *plp = entry;
1578         }
1579         return (0);
1580 }
1581
1582 int
1583 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1584     zfs_type_t type)
1585 {
1586         return (zprop_iter_common(func, cb, show_all, ordered, type));
1587 }