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