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