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