xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision f63f7506be0210195779706f51c58646e568cc40)
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_UNSHAREFAILED:
131 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
132 	case EZFS_SHAREFAILED:
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_UNKNOWN:
152 		return (dgettext(TEXT_DOMAIN, "unknown error"));
153 	default:
154 		assert(hdl->libzfs_error == 0);
155 		return (dgettext(TEXT_DOMAIN, "no error"));
156 	}
157 }
158 
159 /*PRINTFLIKE2*/
160 void
161 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
162 {
163 	va_list ap;
164 
165 	va_start(ap, fmt);
166 
167 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
168 	    fmt, ap);
169 	hdl->libzfs_desc_active = 1;
170 
171 	va_end(ap);
172 }
173 
174 static void
175 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
176 {
177 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
178 	    fmt, ap);
179 	hdl->libzfs_error = error;
180 
181 	if (hdl->libzfs_desc_active)
182 		hdl->libzfs_desc_active = 0;
183 	else
184 		hdl->libzfs_desc[0] = '\0';
185 
186 	if (hdl->libzfs_printerr) {
187 		if (error == EZFS_UNKNOWN) {
188 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
189 			    "error: %s\n"), libzfs_error_description(hdl));
190 			abort();
191 		}
192 
193 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
194 		    libzfs_error_description(hdl));
195 		if (error == EZFS_NOMEM)
196 			exit(1);
197 	}
198 }
199 
200 /*PRINTFLIKE3*/
201 int
202 zfs_error(libzfs_handle_t *hdl, int error, const char *fmt, ...)
203 {
204 	va_list ap;
205 
206 	va_start(ap, fmt);
207 
208 	zfs_verror(hdl, error, fmt, ap);
209 
210 	va_end(ap);
211 
212 	return (-1);
213 }
214 
215 static int
216 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
217     va_list ap)
218 {
219 	switch (error) {
220 	case EPERM:
221 	case EACCES:
222 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
223 		return (-1);
224 
225 	case EIO:
226 		zfs_verror(hdl, EZFS_IO, fmt, ap);
227 		return (-1);
228 
229 	case EINTR:
230 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
231 		return (-1);
232 	}
233 
234 	return (0);
235 }
236 
237 /*PRINTFLIKE3*/
238 int
239 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *fmt, ...)
240 {
241 	va_list ap;
242 
243 	va_start(ap, fmt);
244 
245 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
246 		va_end(ap);
247 		return (-1);
248 	}
249 
250 
251 	switch (error) {
252 	case ENXIO:
253 		zfs_verror(hdl, EZFS_IO, fmt, ap);
254 		break;
255 
256 	case ENOENT:
257 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
258 		    "dataset does not exist"));
259 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
260 		break;
261 
262 	case ENOSPC:
263 	case EDQUOT:
264 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
265 		return (-1);
266 
267 	case EEXIST:
268 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
269 		    "dataset already exists"));
270 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
271 		break;
272 
273 	case EBUSY:
274 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
275 		    "dataset is busy"));
276 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
277 		break;
278 
279 	default:
280 		zfs_error_aux(hdl, strerror(errno));
281 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
282 		break;
283 	}
284 
285 	va_end(ap);
286 	return (-1);
287 }
288 
289 /*PRINTFLIKE3*/
290 int
291 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *fmt, ...)
292 {
293 	va_list ap;
294 
295 	va_start(ap, fmt);
296 
297 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
298 		va_end(ap);
299 		return (-1);
300 	}
301 
302 	switch (error) {
303 	case ENODEV:
304 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
305 		break;
306 
307 	case ENOENT:
308 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
309 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
310 		break;
311 
312 	case EEXIST:
313 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
314 		    "pool already exists"));
315 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
316 		break;
317 
318 	case EBUSY:
319 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
320 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
321 		break;
322 
323 	case ENXIO:
324 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
325 		    "one or more devices is currently unavailable"));
326 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
327 		break;
328 
329 	case ENAMETOOLONG:
330 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
331 		break;
332 
333 	default:
334 		zfs_error_aux(hdl, strerror(error));
335 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
336 	}
337 
338 	va_end(ap);
339 	return (-1);
340 }
341 
342 /*
343  * Display an out of memory error message and abort the current program.
344  */
345 int
346 no_memory(libzfs_handle_t *hdl)
347 {
348 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
349 }
350 
351 /*
352  * A safe form of malloc() which will die if the allocation fails.
353  */
354 void *
355 zfs_alloc(libzfs_handle_t *hdl, size_t size)
356 {
357 	void *data;
358 
359 	if ((data = calloc(1, size)) == NULL)
360 		(void) no_memory(hdl);
361 
362 	return (data);
363 }
364 
365 /*
366  * A safe form of realloc(), which also zeroes newly allocated space.
367  */
368 void *
369 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
370 {
371 	void *ret;
372 
373 	if ((ret = realloc(ptr, newsize)) == NULL) {
374 		(void) no_memory(hdl);
375 		free(ptr);
376 		return (NULL);
377 	}
378 
379 	bzero((char *)ret + oldsize, (newsize - oldsize));
380 	return (ret);
381 }
382 
383 /*
384  * A safe form of strdup() which will die if the allocation fails.
385  */
386 char *
387 zfs_strdup(libzfs_handle_t *hdl, const char *str)
388 {
389 	char *ret;
390 
391 	if ((ret = strdup(str)) == NULL)
392 		(void) no_memory(hdl);
393 
394 	return (ret);
395 }
396 
397 /*
398  * Convert a number to an appropriately human-readable output.
399  */
400 void
401 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
402 {
403 	uint64_t n = num;
404 	int index = 0;
405 	char u;
406 
407 	while (n >= 1024) {
408 		n /= 1024;
409 		index++;
410 	}
411 
412 	u = " KMGTPE"[index];
413 
414 	if (index == 0) {
415 		(void) snprintf(buf, buflen, "%llu", n);
416 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
417 		/*
418 		 * If this is an even multiple of the base, always display
419 		 * without any decimal precision.
420 		 */
421 		(void) snprintf(buf, buflen, "%llu%c", n, u);
422 	} else {
423 		/*
424 		 * We want to choose a precision that reflects the best choice
425 		 * for fitting in 5 characters.  This can get rather tricky when
426 		 * we have numbers that are very close to an order of magnitude.
427 		 * For example, when displaying 10239 (which is really 9.999K),
428 		 * we want only a single place of precision for 10.0K.  We could
429 		 * develop some complex heuristics for this, but it's much
430 		 * easier just to try each combination in turn.
431 		 */
432 		int i;
433 		for (i = 2; i >= 0; i--) {
434 			(void) snprintf(buf, buflen, "%.*f%c", i,
435 			    (double)num / (1ULL << 10 * index), u);
436 			if (strlen(buf) <= 5)
437 				break;
438 		}
439 	}
440 }
441 
442 void
443 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
444 {
445 	hdl->libzfs_printerr = printerr;
446 }
447 
448 libzfs_handle_t *
449 libzfs_init(void)
450 {
451 	libzfs_handle_t *hdl;
452 
453 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
454 		return (NULL);
455 	}
456 
457 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
458 		free(hdl);
459 		return (NULL);
460 	}
461 
462 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
463 		(void) close(hdl->libzfs_fd);
464 		free(hdl);
465 		return (NULL);
466 	}
467 
468 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
469 
470 	return (hdl);
471 }
472 
473 void
474 libzfs_fini(libzfs_handle_t *hdl)
475 {
476 	(void) close(hdl->libzfs_fd);
477 	if (hdl->libzfs_mnttab)
478 		(void) fclose(hdl->libzfs_mnttab);
479 	if (hdl->libzfs_sharetab)
480 		(void) fclose(hdl->libzfs_sharetab);
481 	namespace_clear(hdl);
482 	free(hdl);
483 }
484 
485 libzfs_handle_t *
486 zpool_get_handle(zpool_handle_t *zhp)
487 {
488 	return (zhp->zpool_hdl);
489 }
490 
491 libzfs_handle_t *
492 zfs_get_handle(zfs_handle_t *zhp)
493 {
494 	return (zhp->zfs_hdl);
495 }
496 
497 /*
498  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
499  * an ioctl().
500  */
501 int
502 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
503 {
504 	if (len == 0)
505 		len = 1024;
506 	zc->zc_nvlist_dst_size = len;
507 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
508 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
509 		return (-1);
510 
511 	return (0);
512 }
513 
514 /*
515  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
516  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
517  * filled in by the kernel to indicate the actual required size.
518  */
519 int
520 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
521 {
522 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
523 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
524 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
525 	    == NULL)
526 		return (-1);
527 
528 	return (0);
529 }
530 
531 /*
532  * Called to free the destination nvlist stored in the command structure.  This
533  * is only needed if the caller must abort abnormally.  The various other
534  * zcmd_*() routines will free it on failure (or on success, for
535  * zcmd_read_nvlist).
536  */
537 void
538 zcmd_free_nvlists(zfs_cmd_t *zc)
539 {
540 	free((void *)(uintptr_t)zc->zc_nvlist_src);
541 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
542 }
543 
544 int
545 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl,
546     size_t *size)
547 {
548 	char *packed;
549 	size_t len;
550 
551 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
552 
553 	if ((packed = zfs_alloc(hdl, len)) == NULL)
554 		return (-1);
555 
556 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
557 
558 	zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
559 	zc->zc_nvlist_src_size = len;
560 
561 	if (size)
562 		*size = len;
563 	return (0);
564 }
565 
566 /*
567  * Unpacks an nvlist from the ZFS ioctl command structure.
568  */
569 int
570 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
571 {
572 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
573 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
574 		return (no_memory(hdl));
575 
576 	return (0);
577 }
578