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