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