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