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