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