1 /* 2 * Copyright (c) 1997, 1998, 1999, 2002 Kenneth D. Merry. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. The name of the author may not be used to endorse or promote products 11 * derived from this software without specific prior written permission. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <stdint.h> 34 #include <string.h> 35 #include <fcntl.h> 36 #include <unistd.h> 37 #include <errno.h> 38 #include <ctype.h> 39 40 #include <cam/cam.h> 41 #include <cam/scsi/scsi_all.h> 42 #include <cam/cam_ccb.h> 43 #include <cam/scsi/scsi_pass.h> 44 #include "camlib.h" 45 46 47 static const char *nonrewind_devs[] = { 48 "sa" 49 }; 50 51 char cam_errbuf[CAM_ERRBUF_SIZE]; 52 53 static struct cam_device *cam_real_open_device(const char *path, int flags, 54 struct cam_device *device, 55 const char *given_path, 56 const char *given_dev_name, 57 int given_unit_number); 58 static struct cam_device *cam_lookup_pass(const char *dev_name, int unit, 59 int flags, const char *given_path, 60 struct cam_device *device); 61 62 /* 63 * Send a ccb to a passthrough device. 64 */ 65 int 66 cam_send_ccb(struct cam_device *device, union ccb *ccb) 67 { 68 return(ioctl(device->fd, CAMIOCOMMAND, ccb)); 69 } 70 71 /* 72 * Malloc a CCB, zero out the header and set its path, target and lun ids. 73 */ 74 union ccb * 75 cam_getccb(struct cam_device *dev) 76 { 77 union ccb *ccb; 78 79 ccb = (union ccb *)malloc(sizeof(union ccb)); 80 if (ccb != NULL) { 81 bzero(&ccb->ccb_h, sizeof(struct ccb_hdr)); 82 ccb->ccb_h.path_id = dev->path_id; 83 ccb->ccb_h.target_id = dev->target_id; 84 ccb->ccb_h.target_lun = dev->target_lun; 85 } 86 87 return(ccb); 88 } 89 90 /* 91 * Free a CCB. 92 */ 93 void 94 cam_freeccb(union ccb *ccb) 95 { 96 free(ccb); 97 } 98 99 /* 100 * Take a device name or path passed in by the user, and attempt to figure 101 * out the device name and unit number. Some possible device name formats are: 102 * /dev/foo0 103 * foo0 104 * nfoo0 105 * 106 * Some peripheral drivers create separate device nodes with 'n' prefix for 107 * non-rewind operations. Currently only sa(4) tape driver has this feature. 108 * We extract pure peripheral name as device name for this special case. 109 * 110 * Input parameters: device name/path, length of devname string 111 * Output: device name, unit number 112 * Return values: returns 0 for success, -1 for failure 113 */ 114 int 115 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit) 116 { 117 char *tmpstr, *tmpstr2; 118 char *newpath; 119 int unit_offset; 120 int i; 121 122 if (path == NULL) { 123 snprintf(cam_errbuf, nitems(cam_errbuf), 124 "%s: device pathname was NULL", __func__); 125 return(-1); 126 } 127 128 /* 129 * We can be rather destructive to the path string. Make a copy of 130 * it so we don't hose the user's string. 131 */ 132 newpath = (char *)strdup(path); 133 tmpstr = newpath; 134 135 /* 136 * Check to see whether we have an absolute pathname. 137 */ 138 if (*tmpstr == '/') { 139 tmpstr2 = tmpstr; 140 tmpstr = strrchr(tmpstr2, '/'); 141 if ((tmpstr != NULL) && (*tmpstr != '\0')) 142 tmpstr++; 143 } 144 145 if (*tmpstr == '\0') { 146 snprintf(cam_errbuf, nitems(cam_errbuf), 147 "%s: no text after slash", __func__); 148 free(newpath); 149 return(-1); 150 } 151 152 /* 153 * Check to see whether the user has given us a nonrewound tape 154 * device. 155 */ 156 if (*tmpstr == 'n' || *tmpstr == 'e') { 157 for (i = 0; i < sizeof(nonrewind_devs)/sizeof(char *); i++) { 158 int len = strlen(nonrewind_devs[i]); 159 if (strncmp(tmpstr + 1, nonrewind_devs[i], len) == 0) { 160 if (isdigit(tmpstr[len + 1])) { 161 tmpstr++; 162 break; 163 } 164 } 165 } 166 } 167 168 /* 169 * We should now have just a device name and unit number. 170 * That means that there must be at least 2 characters. 171 * If we only have 1, we don't have a valid device name. 172 */ 173 if (strlen(tmpstr) < 2) { 174 snprintf(cam_errbuf, nitems(cam_errbuf), 175 "%s: must have both device name and unit number", 176 __func__); 177 free(newpath); 178 return(-1); 179 } 180 181 /* 182 * If the first character of the string is a digit, then the user 183 * has probably given us all numbers. Point out the error. 184 */ 185 if (isdigit(*tmpstr)) { 186 snprintf(cam_errbuf, nitems(cam_errbuf), 187 "%s: device name cannot begin with a number", 188 __func__); 189 free(newpath); 190 return(-1); 191 } 192 193 /* 194 * At this point, if the last character of the string isn't a 195 * number, we know the user either didn't give us a device number, 196 * or he gave us a device name/number format we don't recognize. 197 */ 198 if (!isdigit(tmpstr[strlen(tmpstr) - 1])) { 199 snprintf(cam_errbuf, nitems(cam_errbuf), 200 "%s: unable to find device unit number", __func__); 201 free(newpath); 202 return(-1); 203 } 204 205 /* 206 * Attempt to figure out where the device name ends and the unit 207 * number begins. As long as unit_offset is at least 1 less than 208 * the length of the string, we can still potentially have a device 209 * name at the front of the string. When we get to something that 210 * isn't a digit, we've hit the device name. Because of the check 211 * above, we know that this cannot happen when unit_offset == 1. 212 * Therefore it is okay to decrement unit_offset -- it won't cause 213 * us to go past the end of the character array. 214 */ 215 for (unit_offset = 1; 216 (unit_offset < (strlen(tmpstr))) 217 && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++); 218 219 unit_offset--; 220 221 /* 222 * Grab the unit number. 223 */ 224 *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]); 225 226 /* 227 * Put a null in place of the first number of the unit number so 228 * that all we have left is the device name. 229 */ 230 tmpstr[strlen(tmpstr) - unit_offset] = '\0'; 231 232 strlcpy(dev_name, tmpstr, devnamelen); 233 234 /* Clean up allocated memory */ 235 free(newpath); 236 237 return(0); 238 239 } 240 241 /* 242 * Backwards compatible wrapper for the real open routine. This translates 243 * a pathname into a device name and unit number for use with the real open 244 * routine. 245 */ 246 struct cam_device * 247 cam_open_device(const char *path, int flags) 248 { 249 int unit; 250 char dev_name[DEV_IDLEN + 1]; 251 252 /* 253 * cam_get_device() has already put an error message in cam_errbuf, 254 * so we don't need to. 255 */ 256 if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1) 257 return(NULL); 258 259 return(cam_lookup_pass(dev_name, unit, flags, path, NULL)); 260 } 261 262 /* 263 * Open the passthrough device for a given bus, target and lun, if the 264 * passthrough device exists. 265 */ 266 struct cam_device * 267 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun, 268 int flags, struct cam_device *device) 269 { 270 union ccb ccb; 271 struct periph_match_pattern *match_pat; 272 int fd, bufsize; 273 274 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 275 snprintf(cam_errbuf, nitems(cam_errbuf), 276 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE, 277 __func__, strerror(errno)); 278 return(NULL); 279 } 280 281 bzero(&ccb, sizeof(union ccb)); 282 ccb.ccb_h.func_code = XPT_DEV_MATCH; 283 ccb.ccb_h.path_id = CAM_XPT_PATH_ID; 284 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD; 285 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD; 286 287 /* Setup the result buffer */ 288 bufsize = sizeof(struct dev_match_result); 289 ccb.cdm.match_buf_len = bufsize; 290 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize); 291 if (ccb.cdm.matches == NULL) { 292 snprintf(cam_errbuf, nitems(cam_errbuf), 293 "%s: couldn't malloc match buffer", __func__); 294 close(fd); 295 return(NULL); 296 } 297 ccb.cdm.num_matches = 0; 298 299 /* Setup the pattern buffer */ 300 ccb.cdm.num_patterns = 1; 301 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern); 302 ccb.cdm.patterns = (struct dev_match_pattern *)malloc( 303 sizeof(struct dev_match_pattern)); 304 if (ccb.cdm.patterns == NULL) { 305 snprintf(cam_errbuf, nitems(cam_errbuf), 306 "%s: couldn't malloc pattern buffer", __func__); 307 free(ccb.cdm.matches); 308 ccb.cdm.matches = NULL; 309 close(fd); 310 return(NULL); 311 } 312 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH; 313 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern; 314 315 /* 316 * We're looking for the passthrough device associated with this 317 * particular bus/target/lun. 318 */ 319 sprintf(match_pat->periph_name, "pass"); 320 match_pat->path_id = path_id; 321 match_pat->target_id = target_id; 322 match_pat->target_lun = target_lun; 323 /* Now set the flags to indicate what we're looking for. */ 324 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET | 325 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME; 326 327 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 328 snprintf(cam_errbuf, nitems(cam_errbuf), 329 "%s: CAMIOCOMMAND ioctl failed\n" 330 "%s: %s", __func__, __func__, strerror(errno)); 331 goto btl_bailout; 332 } 333 334 /* 335 * Check for an outright error. 336 */ 337 if ((ccb.ccb_h.status != CAM_REQ_CMP) 338 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST) 339 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) { 340 snprintf(cam_errbuf, nitems(cam_errbuf), 341 "%s: CAM error %#x, CDM error %d " 342 "returned from XPT_DEV_MATCH ccb", __func__, 343 ccb.ccb_h.status, ccb.cdm.status); 344 goto btl_bailout; 345 } 346 347 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) { 348 snprintf(cam_errbuf, nitems(cam_errbuf), 349 "%s: CDM reported more than one" 350 " passthrough device at %d:%d:%jx!!\n", 351 __func__, path_id, target_id, (uintmax_t)target_lun); 352 goto btl_bailout; 353 } 354 355 if (ccb.cdm.num_matches == 0) { 356 snprintf(cam_errbuf, nitems(cam_errbuf), 357 "%s: no passthrough device found at" 358 " %d:%d:%jx", __func__, path_id, target_id, 359 (uintmax_t)target_lun); 360 goto btl_bailout; 361 } 362 363 switch(ccb.cdm.matches[0].type) { 364 case DEV_MATCH_PERIPH: { 365 int pass_unit; 366 char dev_path[256]; 367 struct periph_match_result *periph_result; 368 369 periph_result = &ccb.cdm.matches[0].result.periph_result; 370 pass_unit = periph_result->unit_number; 371 free(ccb.cdm.matches); 372 ccb.cdm.matches = NULL; 373 free(ccb.cdm.patterns); 374 ccb.cdm.patterns = NULL; 375 close(fd); 376 sprintf(dev_path, "/dev/pass%d", pass_unit); 377 return(cam_real_open_device(dev_path, flags, device, NULL, 378 NULL, 0)); 379 break; /* NOTREACHED */ 380 } 381 default: 382 snprintf(cam_errbuf, nitems(cam_errbuf), 383 "%s: asked for a peripheral match, but" 384 " got a bus or device match", __func__); 385 goto btl_bailout; 386 break; /* NOTREACHED */ 387 } 388 389 btl_bailout: 390 free(ccb.cdm.matches); 391 ccb.cdm.matches = NULL; 392 free(ccb.cdm.patterns); 393 ccb.cdm.patterns = NULL; 394 close(fd); 395 return(NULL); 396 } 397 398 struct cam_device * 399 cam_open_spec_device(const char *dev_name, int unit, int flags, 400 struct cam_device *device) 401 { 402 return(cam_lookup_pass(dev_name, unit, flags, NULL, device)); 403 } 404 405 struct cam_device * 406 cam_open_pass(const char *path, int flags, struct cam_device *device) 407 { 408 return(cam_real_open_device(path, flags, device, path, NULL, 0)); 409 } 410 411 static struct cam_device * 412 cam_lookup_pass(const char *dev_name, int unit, int flags, 413 const char *given_path, struct cam_device *device) 414 { 415 int fd; 416 union ccb ccb; 417 char dev_path[256]; 418 419 /* 420 * The flags argument above only applies to the actual passthrough 421 * device open, not our open of the given device to find the 422 * passthrough device. 423 */ 424 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 425 snprintf(cam_errbuf, nitems(cam_errbuf), 426 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE, 427 __func__, strerror(errno)); 428 return(NULL); 429 } 430 431 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */ 432 ccb.ccb_h.func_code = XPT_GDEVLIST; 433 434 /* These two are necessary for the GETPASSTHRU ioctl to work. */ 435 strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name)); 436 ccb.cgdl.unit_number = unit; 437 438 /* 439 * Attempt to get the passthrough device. This ioctl will fail if 440 * the device name is null, if the device doesn't exist, or if the 441 * passthrough driver isn't in the kernel. 442 */ 443 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) { 444 char tmpstr[256]; 445 446 /* 447 * If we get ENOENT from the transport layer version of 448 * the CAMGETPASSTHRU ioctl, it means one of two things: 449 * either the device name/unit number passed in doesn't 450 * exist, or the passthrough driver isn't in the kernel. 451 */ 452 if (errno == ENOENT) { 453 snprintf(tmpstr, sizeof(tmpstr), 454 "\n%s: either the pass driver isn't in " 455 "your kernel\n%s: or %s%d doesn't exist", 456 __func__, __func__, dev_name, unit); 457 } 458 snprintf(cam_errbuf, nitems(cam_errbuf), 459 "%s: CAMGETPASSTHRU ioctl failed\n" 460 "%s: %s%s", __func__, __func__, strerror(errno), 461 (errno == ENOENT) ? tmpstr : ""); 462 463 close(fd); 464 return(NULL); 465 } 466 467 close(fd); 468 469 /* 470 * If the ioctl returned the right status, but we got an error back 471 * in the ccb, that means that the kernel found the device the user 472 * passed in, but was unable to find the passthrough device for 473 * the device the user gave us. 474 */ 475 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) { 476 snprintf(cam_errbuf, nitems(cam_errbuf), 477 "%s: device %s%d does not exist!", 478 __func__, dev_name, unit); 479 return(NULL); 480 } 481 482 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name, 483 ccb.cgdl.unit_number); 484 485 return(cam_real_open_device(dev_path, flags, device, NULL, 486 dev_name, unit)); 487 } 488 489 /* 490 * Open a given device. The path argument isn't strictly necessary, but it 491 * is copied into the cam_device structure as a convenience to the user. 492 */ 493 static struct cam_device * 494 cam_real_open_device(const char *path, int flags, struct cam_device *device, 495 const char *given_path, const char *given_dev_name, 496 int given_unit_number) 497 { 498 union ccb ccb; 499 int fd = -1, malloced_device = 0; 500 501 /* 502 * See if the user wants us to malloc a device for him. 503 */ 504 if (device == NULL) { 505 if ((device = (struct cam_device *)malloc( 506 sizeof(struct cam_device))) == NULL) { 507 snprintf(cam_errbuf, nitems(cam_errbuf), 508 "%s: device structure malloc" 509 " failed\n%s: %s", __func__, __func__, 510 strerror(errno)); 511 return(NULL); 512 } 513 device->fd = -1; 514 malloced_device = 1; 515 } 516 517 /* 518 * If the user passed in a path, save it for him. 519 */ 520 if (given_path != NULL) 521 strlcpy(device->device_path, given_path, 522 sizeof(device->device_path)); 523 else 524 device->device_path[0] = '\0'; 525 526 /* 527 * If the user passed in a device name and unit number pair, save 528 * those as well. 529 */ 530 if (given_dev_name != NULL) 531 strlcpy(device->given_dev_name, given_dev_name, 532 sizeof(device->given_dev_name)); 533 else 534 device->given_dev_name[0] = '\0'; 535 device->given_unit_number = given_unit_number; 536 537 if ((fd = open(path, flags)) < 0) { 538 snprintf(cam_errbuf, nitems(cam_errbuf), 539 "%s: couldn't open passthrough device %s\n" 540 "%s: %s", __func__, path, __func__, 541 strerror(errno)); 542 goto crod_bailout; 543 } 544 545 device->fd = fd; 546 547 bzero(&ccb, sizeof(union ccb)); 548 549 /* 550 * Unlike the transport layer version of the GETPASSTHRU ioctl, 551 * we don't have to set any fields. 552 */ 553 ccb.ccb_h.func_code = XPT_GDEVLIST; 554 555 /* 556 * We're only doing this to get some information on the device in 557 * question. Otherwise, we'd have to pass in yet another 558 * parameter: the passthrough driver unit number. 559 */ 560 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) { 561 /* 562 * At this point we know the passthrough device must exist 563 * because we just opened it above. The only way this 564 * ioctl can fail is if the ccb size is wrong. 565 */ 566 snprintf(cam_errbuf, nitems(cam_errbuf), 567 "%s: CAMGETPASSTHRU ioctl failed\n" 568 "%s: %s", __func__, __func__, strerror(errno)); 569 goto crod_bailout; 570 } 571 572 /* 573 * If the ioctl returned the right status, but we got an error back 574 * in the ccb, that means that the kernel found the device the user 575 * passed in, but was unable to find the passthrough device for 576 * the device the user gave us. 577 */ 578 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) { 579 snprintf(cam_errbuf, nitems(cam_errbuf), 580 "%s: passthrough device does not exist!", __func__); 581 goto crod_bailout; 582 } 583 584 device->dev_unit_num = ccb.cgdl.unit_number; 585 strlcpy(device->device_name, ccb.cgdl.periph_name, 586 sizeof(device->device_name)); 587 device->path_id = ccb.ccb_h.path_id; 588 device->target_id = ccb.ccb_h.target_id; 589 device->target_lun = ccb.ccb_h.target_lun; 590 591 ccb.ccb_h.func_code = XPT_PATH_INQ; 592 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 593 snprintf(cam_errbuf, nitems(cam_errbuf), 594 "%s: Path Inquiry CCB failed\n" 595 "%s: %s", __func__, __func__, strerror(errno)); 596 goto crod_bailout; 597 } 598 strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name)); 599 device->sim_unit_number = ccb.cpi.unit_number; 600 device->bus_id = ccb.cpi.bus_id; 601 602 /* 603 * It doesn't really matter what is in the payload for a getdev 604 * CCB, the kernel doesn't look at it. 605 */ 606 ccb.ccb_h.func_code = XPT_GDEV_TYPE; 607 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 608 snprintf(cam_errbuf, nitems(cam_errbuf), 609 "%s: Get Device Type CCB failed\n" 610 "%s: %s", __func__, __func__, strerror(errno)); 611 goto crod_bailout; 612 } 613 device->pd_type = SID_TYPE(&ccb.cgd.inq_data); 614 bcopy(&ccb.cgd.inq_data, &device->inq_data, 615 sizeof(struct scsi_inquiry_data)); 616 device->serial_num_len = ccb.cgd.serial_num_len; 617 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len); 618 619 /* 620 * Zero the payload, the kernel does look at the flags. 621 */ 622 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts); 623 624 /* 625 * Get transfer settings for this device. 626 */ 627 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 628 629 ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS; 630 631 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 632 snprintf(cam_errbuf, nitems(cam_errbuf), 633 "%s: Get Transfer Settings CCB failed\n" 634 "%s: %s", __func__, __func__, strerror(errno)); 635 goto crod_bailout; 636 } 637 if (ccb.cts.transport == XPORT_SPI) { 638 struct ccb_trans_settings_spi *spi = 639 &ccb.cts.xport_specific.spi; 640 device->sync_period = spi->sync_period; 641 device->sync_offset = spi->sync_offset; 642 device->bus_width = spi->bus_width; 643 } else { 644 device->sync_period = 0; 645 device->sync_offset = 0; 646 device->bus_width = 0; 647 } 648 649 return(device); 650 651 crod_bailout: 652 653 if (fd >= 0) 654 close(fd); 655 656 if (malloced_device) 657 free(device); 658 659 return(NULL); 660 } 661 662 void 663 cam_close_device(struct cam_device *dev) 664 { 665 if (dev == NULL) 666 return; 667 668 cam_close_spec_device(dev); 669 670 free(dev); 671 } 672 673 void 674 cam_close_spec_device(struct cam_device *dev) 675 { 676 if (dev == NULL) 677 return; 678 679 if (dev->fd >= 0) { 680 close(dev->fd); 681 dev->fd = -1; 682 } 683 } 684 685 char * 686 cam_path_string(struct cam_device *dev, char *str, int len) 687 { 688 if (dev == NULL) { 689 snprintf(str, len, "No path"); 690 return(str); 691 } 692 693 snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ", 694 (dev->device_name[0] != '\0') ? dev->device_name : "pass", 695 dev->dev_unit_num, 696 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown", 697 dev->sim_unit_number, 698 dev->bus_id, 699 dev->target_id, 700 (uintmax_t)dev->target_lun); 701 702 return(str); 703 } 704 705 /* 706 * Malloc/duplicate a CAM device structure. 707 */ 708 struct cam_device * 709 cam_device_dup(struct cam_device *device) 710 { 711 struct cam_device *newdev; 712 713 if (device == NULL) { 714 snprintf(cam_errbuf, nitems(cam_errbuf), 715 "%s: device is NULL", __func__); 716 return (NULL); 717 } 718 719 newdev = malloc(sizeof(struct cam_device)); 720 if (newdev == NULL) { 721 snprintf(cam_errbuf, nitems(cam_errbuf), 722 "%s: couldn't malloc CAM device structure", __func__); 723 return (NULL); 724 } 725 726 bcopy(device, newdev, sizeof(struct cam_device)); 727 728 return(newdev); 729 } 730 731 /* 732 * Copy a CAM device structure. 733 */ 734 void 735 cam_device_copy(struct cam_device *src, struct cam_device *dst) 736 { 737 738 if (src == NULL) { 739 snprintf(cam_errbuf, nitems(cam_errbuf), 740 "%s: source device struct was NULL", __func__); 741 return; 742 } 743 744 if (dst == NULL) { 745 snprintf(cam_errbuf, nitems(cam_errbuf), 746 "%s: destination device struct was NULL", __func__); 747 return; 748 } 749 750 bcopy(src, dst, sizeof(struct cam_device)); 751 752 } 753