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