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