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