xref: /titanic_50/usr/src/lib/libzfs/common/libzfs_util.c (revision 8668df41d90e075636bc3817b28ad77cbd470959)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Internal utility routines for the ZFS library.
30  */
31 
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <libintl.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <sys/mnttab.h>
41 
42 #include <libzfs.h>
43 
44 #include "libzfs_impl.h"
45 
46 int
47 libzfs_errno(libzfs_handle_t *hdl)
48 {
49 	return (hdl->libzfs_error);
50 }
51 
52 const char *
53 libzfs_error_action(libzfs_handle_t *hdl)
54 {
55 	return (hdl->libzfs_action);
56 }
57 
58 const char *
59 libzfs_error_description(libzfs_handle_t *hdl)
60 {
61 	if (hdl->libzfs_desc[0] != '\0')
62 		return (hdl->libzfs_desc);
63 
64 	switch (hdl->libzfs_error) {
65 	case EZFS_NOMEM:
66 		return (dgettext(TEXT_DOMAIN, "out of memory"));
67 	case EZFS_BADPROP:
68 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
69 	case EZFS_PROPREADONLY:
70 		return (dgettext(TEXT_DOMAIN, "read only property"));
71 	case EZFS_PROPTYPE:
72 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
73 		    "datasets of this type"));
74 	case EZFS_PROPNONINHERIT:
75 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
76 	case EZFS_PROPSPACE:
77 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
78 	case EZFS_BADTYPE:
79 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
80 		    "datasets of this type"));
81 	case EZFS_BUSY:
82 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
83 	case EZFS_EXISTS:
84 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
85 	case EZFS_NOENT:
86 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
87 	case EZFS_BADSTREAM:
88 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
89 	case EZFS_DSREADONLY:
90 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
91 	case EZFS_VOLTOOBIG:
92 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
93 		    "this system"));
94 	case EZFS_VOLHASDATA:
95 		return (dgettext(TEXT_DOMAIN, "volume has data"));
96 	case EZFS_INVALIDNAME:
97 		return (dgettext(TEXT_DOMAIN, "invalid name"));
98 	case EZFS_BADRESTORE:
99 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
100 		    "destination"));
101 	case EZFS_BADBACKUP:
102 		return (dgettext(TEXT_DOMAIN, "backup failed"));
103 	case EZFS_BADTARGET:
104 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
105 	case EZFS_NODEVICE:
106 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
107 	case EZFS_BADDEV:
108 		return (dgettext(TEXT_DOMAIN, "invalid device"));
109 	case EZFS_NOREPLICAS:
110 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
111 	case EZFS_RESILVERING:
112 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
113 	case EZFS_BADVERSION:
114 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
115 	case EZFS_POOLUNAVAIL:
116 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
117 	case EZFS_DEVOVERFLOW:
118 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
119 	case EZFS_BADPATH:
120 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
121 	case EZFS_CROSSTARGET:
122 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
123 		    "pools"));
124 	case EZFS_ZONED:
125 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
126 	case EZFS_MOUNTFAILED:
127 		return (dgettext(TEXT_DOMAIN, "mount failed"));
128 	case EZFS_UMOUNTFAILED:
129 		return (dgettext(TEXT_DOMAIN, "umount failed"));
130 	case EZFS_UNSHARENFSFAILED:
131 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
132 	case EZFS_SHARENFSFAILED:
133 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
134 	case EZFS_DEVLINKS:
135 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
136 	case EZFS_PERM:
137 		return (dgettext(TEXT_DOMAIN, "permission denied"));
138 	case EZFS_NOSPC:
139 		return (dgettext(TEXT_DOMAIN, "out of space"));
140 	case EZFS_IO:
141 		return (dgettext(TEXT_DOMAIN, "I/O error"));
142 	case EZFS_INTR:
143 		return (dgettext(TEXT_DOMAIN, "signal received"));
144 	case EZFS_ISSPARE:
145 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
146 		    "spare"));
147 	case EZFS_INVALCONFIG:
148 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
149 	case EZFS_RECURSIVE:
150 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
151 	case EZFS_NOHISTORY:
152 		return (dgettext(TEXT_DOMAIN, "no history available"));
153 	case EZFS_UNSHAREISCSIFAILED:
154 		return (dgettext(TEXT_DOMAIN,
155 		    "iscsitgtd failed request to unshare"));
156 	case EZFS_SHAREISCSIFAILED:
157 		return (dgettext(TEXT_DOMAIN,
158 		    "iscsitgtd failed request to share"));
159 	case EZFS_UNKNOWN:
160 		return (dgettext(TEXT_DOMAIN, "unknown error"));
161 	default:
162 		assert(hdl->libzfs_error == 0);
163 		return (dgettext(TEXT_DOMAIN, "no error"));
164 	}
165 }
166 
167 /*PRINTFLIKE2*/
168 void
169 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
170 {
171 	va_list ap;
172 
173 	va_start(ap, fmt);
174 
175 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
176 	    fmt, ap);
177 	hdl->libzfs_desc_active = 1;
178 
179 	va_end(ap);
180 }
181 
182 static void
183 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
184 {
185 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
186 	    fmt, ap);
187 	hdl->libzfs_error = error;
188 
189 	if (hdl->libzfs_desc_active)
190 		hdl->libzfs_desc_active = 0;
191 	else
192 		hdl->libzfs_desc[0] = '\0';
193 
194 	if (hdl->libzfs_printerr) {
195 		if (error == EZFS_UNKNOWN) {
196 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
197 			    "error: %s\n"), libzfs_error_description(hdl));
198 			abort();
199 		}
200 
201 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
202 			libzfs_error_description(hdl));
203 		if (error == EZFS_NOMEM)
204 			exit(1);
205 	}
206 }
207 
208 int
209 zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
210 {
211 	return (zfs_error_fmt(hdl, error, "%s", msg));
212 }
213 
214 /*PRINTFLIKE3*/
215 int
216 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
217 {
218 	va_list ap;
219 
220 	va_start(ap, fmt);
221 
222 	zfs_verror(hdl, error, fmt, ap);
223 
224 	va_end(ap);
225 
226 	return (-1);
227 }
228 
229 static int
230 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
231     va_list ap)
232 {
233 	switch (error) {
234 	case EPERM:
235 	case EACCES:
236 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
237 		return (-1);
238 
239 	case EIO:
240 		zfs_verror(hdl, EZFS_IO, fmt, ap);
241 		return (-1);
242 
243 	case EINTR:
244 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
245 		return (-1);
246 	}
247 
248 	return (0);
249 }
250 
251 int
252 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
253 {
254 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
255 }
256 
257 /*PRINTFLIKE3*/
258 int
259 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
260 {
261 	va_list ap;
262 
263 	va_start(ap, fmt);
264 
265 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
266 		va_end(ap);
267 		return (-1);
268 	}
269 
270 
271 	switch (error) {
272 	case ENXIO:
273 		zfs_verror(hdl, EZFS_IO, fmt, ap);
274 		break;
275 
276 	case ENOENT:
277 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
278 		    "dataset does not exist"));
279 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
280 		break;
281 
282 	case ENOSPC:
283 	case EDQUOT:
284 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
285 		return (-1);
286 
287 	case EEXIST:
288 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
289 		    "dataset already exists"));
290 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
291 		break;
292 
293 	case EBUSY:
294 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
295 		    "dataset is busy"));
296 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
297 		break;
298 
299 	default:
300 		zfs_error_aux(hdl, strerror(errno));
301 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
302 		break;
303 	}
304 
305 	va_end(ap);
306 	return (-1);
307 }
308 
309 int
310 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
311 {
312 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
313 }
314 
315 /*PRINTFLIKE3*/
316 int
317 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
318 {
319 	va_list ap;
320 
321 	va_start(ap, fmt);
322 
323 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
324 		va_end(ap);
325 		return (-1);
326 	}
327 
328 	switch (error) {
329 	case ENODEV:
330 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
331 		break;
332 
333 	case ENOENT:
334 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
335 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
336 		break;
337 
338 	case EEXIST:
339 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
340 		    "pool already exists"));
341 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
342 		break;
343 
344 	case EBUSY:
345 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
346 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
347 		break;
348 
349 	case ENXIO:
350 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
351 		    "one or more devices is currently unavailable"));
352 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
353 		break;
354 
355 	case ENAMETOOLONG:
356 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
357 		break;
358 
359 	default:
360 		zfs_error_aux(hdl, strerror(error));
361 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
362 	}
363 
364 	va_end(ap);
365 	return (-1);
366 }
367 
368 /*
369  * Display an out of memory error message and abort the current program.
370  */
371 int
372 no_memory(libzfs_handle_t *hdl)
373 {
374 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
375 }
376 
377 /*
378  * A safe form of malloc() which will die if the allocation fails.
379  */
380 void *
381 zfs_alloc(libzfs_handle_t *hdl, size_t size)
382 {
383 	void *data;
384 
385 	if ((data = calloc(1, size)) == NULL)
386 		(void) no_memory(hdl);
387 
388 	return (data);
389 }
390 
391 /*
392  * A safe form of realloc(), which also zeroes newly allocated space.
393  */
394 void *
395 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
396 {
397 	void *ret;
398 
399 	if ((ret = realloc(ptr, newsize)) == NULL) {
400 		(void) no_memory(hdl);
401 		free(ptr);
402 		return (NULL);
403 	}
404 
405 	bzero((char *)ret + oldsize, (newsize - oldsize));
406 	return (ret);
407 }
408 
409 /*
410  * A safe form of strdup() which will die if the allocation fails.
411  */
412 char *
413 zfs_strdup(libzfs_handle_t *hdl, const char *str)
414 {
415 	char *ret;
416 
417 	if ((ret = strdup(str)) == NULL)
418 		(void) no_memory(hdl);
419 
420 	return (ret);
421 }
422 
423 /*
424  * Convert a number to an appropriately human-readable output.
425  */
426 void
427 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
428 {
429 	uint64_t n = num;
430 	int index = 0;
431 	char u;
432 
433 	while (n >= 1024) {
434 		n /= 1024;
435 		index++;
436 	}
437 
438 	u = " KMGTPE"[index];
439 
440 	if (index == 0) {
441 		(void) snprintf(buf, buflen, "%llu", n);
442 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
443 		/*
444 		 * If this is an even multiple of the base, always display
445 		 * without any decimal precision.
446 		 */
447 		(void) snprintf(buf, buflen, "%llu%c", n, u);
448 	} else {
449 		/*
450 		 * We want to choose a precision that reflects the best choice
451 		 * for fitting in 5 characters.  This can get rather tricky when
452 		 * we have numbers that are very close to an order of magnitude.
453 		 * For example, when displaying 10239 (which is really 9.999K),
454 		 * we want only a single place of precision for 10.0K.  We could
455 		 * develop some complex heuristics for this, but it's much
456 		 * easier just to try each combination in turn.
457 		 */
458 		int i;
459 		for (i = 2; i >= 0; i--) {
460 			(void) snprintf(buf, buflen, "%.*f%c", i,
461 			    (double)num / (1ULL << 10 * index), u);
462 			if (strlen(buf) <= 5)
463 				break;
464 		}
465 	}
466 }
467 
468 void
469 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
470 {
471 	hdl->libzfs_printerr = printerr;
472 }
473 
474 libzfs_handle_t *
475 libzfs_init(void)
476 {
477 	libzfs_handle_t *hdl;
478 
479 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
480 		return (NULL);
481 	}
482 
483 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
484 		free(hdl);
485 		return (NULL);
486 	}
487 
488 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
489 		(void) close(hdl->libzfs_fd);
490 		free(hdl);
491 		return (NULL);
492 	}
493 
494 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
495 
496 	return (hdl);
497 }
498 
499 void
500 libzfs_fini(libzfs_handle_t *hdl)
501 {
502 	(void) close(hdl->libzfs_fd);
503 	if (hdl->libzfs_mnttab)
504 		(void) fclose(hdl->libzfs_mnttab);
505 	if (hdl->libzfs_sharetab)
506 		(void) fclose(hdl->libzfs_sharetab);
507 	namespace_clear(hdl);
508 	free(hdl);
509 }
510 
511 libzfs_handle_t *
512 zpool_get_handle(zpool_handle_t *zhp)
513 {
514 	return (zhp->zpool_hdl);
515 }
516 
517 libzfs_handle_t *
518 zfs_get_handle(zfs_handle_t *zhp)
519 {
520 	return (zhp->zfs_hdl);
521 }
522 
523 /*
524  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
525  * an ioctl().
526  */
527 int
528 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
529 {
530 	if (len == 0)
531 		len = 2048;
532 	zc->zc_nvlist_dst_size = len;
533 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
534 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
535 		return (-1);
536 
537 	return (0);
538 }
539 
540 /*
541  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
542  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
543  * filled in by the kernel to indicate the actual required size.
544  */
545 int
546 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
547 {
548 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
549 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
550 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
551 	    == NULL)
552 		return (-1);
553 
554 	return (0);
555 }
556 
557 /*
558  * Called to free the src and dst nvlists stored in the command structure.
559  */
560 void
561 zcmd_free_nvlists(zfs_cmd_t *zc)
562 {
563 	free((void *)(uintptr_t)zc->zc_nvlist_src);
564 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
565 }
566 
567 int
568 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl,
569     size_t *size)
570 {
571 	char *packed;
572 	size_t len;
573 
574 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
575 
576 	if ((packed = zfs_alloc(hdl, len)) == NULL)
577 		return (-1);
578 
579 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
580 
581 	zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
582 	zc->zc_nvlist_src_size = len;
583 
584 	if (size)
585 		*size = len;
586 	return (0);
587 }
588 
589 /*
590  * Unpacks an nvlist from the ZFS ioctl command structure.
591  */
592 int
593 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
594 {
595 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
596 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
597 		return (no_memory(hdl));
598 
599 	return (0);
600 }
601