xref: /titanic_41/usr/src/lib/libzfs/common/libzfs_util.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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_UNKNOWN:
150 		return (dgettext(TEXT_DOMAIN, "unknown error"));
151 	default:
152 		abort();
153 	}
154 
155 	/* NOTREACHED */
156 }
157 
158 /*PRINTFLIKE2*/
159 void
160 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
161 {
162 	va_list ap;
163 
164 	va_start(ap, fmt);
165 
166 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
167 	    fmt, ap);
168 	hdl->libzfs_desc_active = 1;
169 
170 	va_end(ap);
171 }
172 
173 static void
174 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
175 {
176 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
177 	    fmt, ap);
178 	hdl->libzfs_error = error;
179 
180 	if (hdl->libzfs_desc_active)
181 		hdl->libzfs_desc_active = 0;
182 	else
183 		hdl->libzfs_desc[0] = '\0';
184 
185 	if (hdl->libzfs_printerr) {
186 		if (error == EZFS_UNKNOWN) {
187 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
188 			    "error: %s\n"), libzfs_error_description(hdl));
189 			abort();
190 		}
191 
192 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
193 		    libzfs_error_description(hdl));
194 		if (error == EZFS_NOMEM)
195 			exit(1);
196 	}
197 }
198 
199 /*PRINTFLIKE3*/
200 int
201 zfs_error(libzfs_handle_t *hdl, int error, const char *fmt, ...)
202 {
203 	va_list ap;
204 
205 	va_start(ap, fmt);
206 
207 	zfs_verror(hdl, error, fmt, ap);
208 
209 	va_end(ap);
210 
211 	return (-1);
212 }
213 
214 static int
215 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
216     va_list ap)
217 {
218 	switch (error) {
219 	case EPERM:
220 	case EACCES:
221 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
222 		return (-1);
223 
224 	case EIO:
225 		zfs_verror(hdl, EZFS_IO, fmt, ap);
226 		return (-1);
227 
228 	case EINTR:
229 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
230 		return (-1);
231 	}
232 
233 	return (0);
234 }
235 
236 /*PRINTFLIKE3*/
237 int
238 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *fmt, ...)
239 {
240 	va_list ap;
241 
242 	va_start(ap, fmt);
243 
244 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
245 		va_end(ap);
246 		return (-1);
247 	}
248 
249 
250 	switch (error) {
251 	case ENXIO:
252 		zfs_verror(hdl, EZFS_IO, fmt, ap);
253 		break;
254 
255 	case ENOENT:
256 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
257 		    "dataset does not exist"));
258 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
259 		break;
260 
261 	case ENOSPC:
262 	case EDQUOT:
263 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
264 		return (-1);
265 
266 	case EEXIST:
267 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
268 		    "dataset already exists"));
269 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
270 		break;
271 
272 	case EBUSY:
273 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
274 		    "dataset is busy"));
275 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
276 		break;
277 
278 	default:
279 		zfs_error_aux(hdl, strerror(errno));
280 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
281 		break;
282 	}
283 
284 	va_end(ap);
285 	return (-1);
286 }
287 
288 /*PRINTFLIKE3*/
289 int
290 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *fmt, ...)
291 {
292 	va_list ap;
293 
294 	va_start(ap, fmt);
295 
296 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
297 		va_end(ap);
298 		return (-1);
299 	}
300 
301 	switch (error) {
302 	case ENODEV:
303 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
304 		break;
305 
306 	case ENOENT:
307 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
308 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
309 		break;
310 
311 	case EEXIST:
312 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
313 		    "pool already exists"));
314 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
315 		break;
316 
317 	case EBUSY:
318 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
319 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
320 		break;
321 
322 	case ENXIO:
323 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
324 		    "one or more devices is currently unavailable"));
325 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
326 		break;
327 
328 	case ENAMETOOLONG:
329 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
330 		break;
331 
332 	default:
333 		zfs_error_aux(hdl, strerror(error));
334 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
335 	}
336 
337 	va_end(ap);
338 	return (-1);
339 }
340 
341 /*
342  * Display an out of memory error message and abort the current program.
343  */
344 int
345 no_memory(libzfs_handle_t *hdl)
346 {
347 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
348 }
349 
350 /*
351  * A safe form of malloc() which will die if the allocation fails.
352  */
353 void *
354 zfs_alloc(libzfs_handle_t *hdl, size_t size)
355 {
356 	void *data;
357 
358 	if ((data = calloc(1, size)) == NULL)
359 		(void) no_memory(hdl);
360 
361 	return (data);
362 }
363 
364 /*
365  * A safe form of strdup() which will die if the allocation fails.
366  */
367 char *
368 zfs_strdup(libzfs_handle_t *hdl, const char *str)
369 {
370 	char *ret;
371 
372 	if ((ret = strdup(str)) == NULL)
373 		(void) no_memory(hdl);
374 
375 	return (ret);
376 }
377 
378 /*
379  * Convert a number to an appropriately human-readable output.
380  */
381 void
382 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
383 {
384 	uint64_t n = num;
385 	int index = 0;
386 	char u;
387 
388 	while (n >= 1024) {
389 		n /= 1024;
390 		index++;
391 	}
392 
393 	u = " KMGTPE"[index];
394 
395 	if (index == 0) {
396 		(void) snprintf(buf, buflen, "%llu", n);
397 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
398 		/*
399 		 * If this is an even multiple of the base, always display
400 		 * without any decimal precision.
401 		 */
402 		(void) snprintf(buf, buflen, "%llu%c", n, u);
403 	} else {
404 		/*
405 		 * We want to choose a precision that reflects the best choice
406 		 * for fitting in 5 characters.  This can get rather tricky when
407 		 * we have numbers that are very close to an order of magnitude.
408 		 * For example, when displaying 10239 (which is really 9.999K),
409 		 * we want only a single place of precision for 10.0K.  We could
410 		 * develop some complex heuristics for this, but it's much
411 		 * easier just to try each combination in turn.
412 		 */
413 		int i;
414 		for (i = 2; i >= 0; i--) {
415 			(void) snprintf(buf, buflen, "%.*f%c", i,
416 			    (double)num / (1ULL << 10 * index), u);
417 			if (strlen(buf) <= 5)
418 				break;
419 		}
420 	}
421 }
422 
423 void
424 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
425 {
426 	hdl->libzfs_printerr = printerr;
427 }
428 
429 libzfs_handle_t *
430 libzfs_init(void)
431 {
432 	libzfs_handle_t *hdl;
433 
434 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
435 		return (NULL);
436 	}
437 
438 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) == NULL) {
439 		free(hdl);
440 		return (NULL);
441 	}
442 
443 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
444 		(void) close(hdl->libzfs_fd);
445 		free(hdl);
446 		return (NULL);
447 	}
448 
449 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
450 
451 	return (hdl);
452 }
453 
454 void
455 libzfs_fini(libzfs_handle_t *hdl)
456 {
457 	(void) close(hdl->libzfs_fd);
458 	if (hdl->libzfs_mnttab)
459 		(void) fclose(hdl->libzfs_mnttab);
460 	if (hdl->libzfs_sharetab)
461 		(void) fclose(hdl->libzfs_sharetab);
462 	namespace_clear(hdl);
463 	free(hdl);
464 }
465 
466 libzfs_handle_t *
467 zpool_get_handle(zpool_handle_t *zhp)
468 {
469 	return (zhp->zpool_hdl);
470 }
471 
472 libzfs_handle_t *
473 zfs_get_handle(zfs_handle_t *zhp)
474 {
475 	return (zhp->zfs_hdl);
476 }
477