xref: /aosp_15_r20/external/mtools/scsi_io.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 2021 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  *
17*d5c9a868SElliott Hughes  * I/O to a SCSI device
18*d5c9a868SElliott Hughes  *
19*d5c9a868SElliott Hughes  * written by:
20*d5c9a868SElliott Hughes  *
21*d5c9a868SElliott Hughes  * Alain L. Knaff
22*d5c9a868SElliott Hughes  * [email protected]
23*d5c9a868SElliott Hughes  *
24*d5c9a868SElliott Hughes  */
25*d5c9a868SElliott Hughes 
26*d5c9a868SElliott Hughes #include "sysincludes.h"
27*d5c9a868SElliott Hughes #include "stream.h"
28*d5c9a868SElliott Hughes #include "mtools.h"
29*d5c9a868SElliott Hughes #include "msdos.h"
30*d5c9a868SElliott Hughes #include "llong.h"
31*d5c9a868SElliott Hughes 
32*d5c9a868SElliott Hughes #include "open_image.h"
33*d5c9a868SElliott Hughes 
34*d5c9a868SElliott Hughes #include "scsi.h"
35*d5c9a868SElliott Hughes #include "plain_io.h"
36*d5c9a868SElliott Hughes #include "scsi_io.h"
37*d5c9a868SElliott Hughes 
38*d5c9a868SElliott Hughes typedef struct ScsiDevice_t {
39*d5c9a868SElliott Hughes 	struct Stream_t head;
40*d5c9a868SElliott Hughes 
41*d5c9a868SElliott Hughes 	int fd;
42*d5c9a868SElliott Hughes 	int privileged;
43*d5c9a868SElliott Hughes 
44*d5c9a868SElliott Hughes 	uint32_t scsi_sector_size;
45*d5c9a868SElliott Hughes 	mt_off_t device_size;
46*d5c9a868SElliott Hughes 	uint32_t tot_sectors;
47*d5c9a868SElliott Hughes 	void *extra_data; /* extra system dependent information for scsi.
48*d5c9a868SElliott Hughes 			     On some platforms, filled in by scsi_open, and to
49*d5c9a868SElliott Hughes 			     be supplied to scsi_cmd */
50*d5c9a868SElliott Hughes } ScsiDevice_t;
51*d5c9a868SElliott Hughes 
52*d5c9a868SElliott Hughes /* ZIP or other scsi device on Solaris or SunOS system.
53*d5c9a868SElliott Hughes    Since Sun won't accept a non-Sun label on a scsi disk, we must
54*d5c9a868SElliott Hughes    bypass Sun's disk interface and use low-level SCSI commands to read
55*d5c9a868SElliott Hughes    or write the ZIP drive.  We thus replace the file_read and file_write
56*d5c9a868SElliott Hughes    routines with our own scsi_read and scsi_write routines, that use the
57*d5c9a868SElliott Hughes    uscsi ioctl interface.  By James Dugal, [email protected], 11-96.  Tested
58*d5c9a868SElliott Hughes    under Solaris 2.5 and SunOS 4.3.1_u1 using GCC.
59*d5c9a868SElliott Hughes 
60*d5c9a868SElliott Hughes    Note: the mtools.conf entry for a ZIP drive would look like this:
61*d5c9a868SElliott Hughes (solaris) drive C: file="/dev/rdsk/c0t5d0s2" partition=4  FAT=16 nodelay  exclusive scsi=1
62*d5c9a868SElliott Hughes (sunos) drive C: file="/dev/rsd5c" partition=4  FAT=16 nodelay  exclusive scsi=1
63*d5c9a868SElliott Hughes 
64*d5c9a868SElliott Hughes    Note 2: Sol 2.5 wants mtools to be suid-root, to use the ioctl.  SunOS is
65*d5c9a868SElliott Hughes    happy if we just have access to the device, so making mtools sgid to a
66*d5c9a868SElliott Hughes    group called, say, "ziprw" which has rw permission on /dev/rsd5c, is fine.
67*d5c9a868SElliott Hughes  */
68*d5c9a868SElliott Hughes 
scsi_init(ScsiDevice_t * This)69*d5c9a868SElliott Hughes static int scsi_init(ScsiDevice_t *This)
70*d5c9a868SElliott Hughes {
71*d5c9a868SElliott Hughes    int fd = This->fd;
72*d5c9a868SElliott Hughes    unsigned char cdb[10],buf[8];
73*d5c9a868SElliott Hughes 
74*d5c9a868SElliott Hughes    memset(cdb, 0, sizeof cdb);
75*d5c9a868SElliott Hughes    memset(buf,0, sizeof(buf));
76*d5c9a868SElliott Hughes    cdb[0]=SCSI_READ_CAPACITY;
77*d5c9a868SElliott Hughes    if (scsi_cmd(fd, (unsigned char *)cdb,
78*d5c9a868SElliott Hughes 		sizeof(cdb), SCSI_IO_READ, buf,
79*d5c9a868SElliott Hughes 		sizeof(buf), This->extra_data)==0)
80*d5c9a868SElliott Hughes    {
81*d5c9a868SElliott Hughes 	   This->tot_sectors=
82*d5c9a868SElliott Hughes 		   ((unsigned)buf[0]<<24)|
83*d5c9a868SElliott Hughes 		   ((unsigned)buf[1]<<16)|
84*d5c9a868SElliott Hughes 		   ((unsigned)buf[2]<<8)|
85*d5c9a868SElliott Hughes 		   (unsigned)buf[3];
86*d5c9a868SElliott Hughes 	   if(This->tot_sectors < UINT32_MAX)
87*d5c9a868SElliott Hughes 		   This->tot_sectors++;
88*d5c9a868SElliott Hughes 
89*d5c9a868SElliott Hughes 	   This->scsi_sector_size=
90*d5c9a868SElliott Hughes 		   ((unsigned)buf[5]<<16)|
91*d5c9a868SElliott Hughes 		   ((unsigned)buf[6]<<8)|
92*d5c9a868SElliott Hughes 		   (unsigned)buf[7];
93*d5c9a868SElliott Hughes 	   if (This->scsi_sector_size != 512)
94*d5c9a868SElliott Hughes 		   fprintf(stderr,"  (scsi_sector_size=%d)\n",This->scsi_sector_size);
95*d5c9a868SElliott Hughes 	   return 0;
96*d5c9a868SElliott Hughes    } else
97*d5c9a868SElliott Hughes 	   return -1;
98*d5c9a868SElliott Hughes }
99*d5c9a868SElliott Hughes 
100*d5c9a868SElliott Hughes 
101*d5c9a868SElliott Hughes /**
102*d5c9a868SElliott Hughes  * Overflow-safe conversion of bytes to sectors
103*d5c9a868SElliott Hughes  */
bytesToSectors(size_t bytes,uint32_t sector_size)104*d5c9a868SElliott Hughes static uint32_t bytesToSectors(size_t bytes, uint32_t sector_size) {
105*d5c9a868SElliott Hughes 	size_t sectors = bytes / sector_size;
106*d5c9a868SElliott Hughes 	if(bytes % sector_size)
107*d5c9a868SElliott Hughes 		sectors++;
108*d5c9a868SElliott Hughes 	if(sectors > UINT32_MAX)
109*d5c9a868SElliott Hughes 		return UINT32_MAX;
110*d5c9a868SElliott Hughes 	else
111*d5c9a868SElliott Hughes 		return (uint32_t) sectors;
112*d5c9a868SElliott Hughes }
113*d5c9a868SElliott Hughes 
scsi_io(Stream_t * Stream,char * buf,mt_off_t where,size_t len,scsi_io_mode_t rwcmd)114*d5c9a868SElliott Hughes static ssize_t scsi_io(Stream_t *Stream, char *buf,
115*d5c9a868SElliott Hughes 		       mt_off_t where, size_t len, scsi_io_mode_t rwcmd)
116*d5c9a868SElliott Hughes {
117*d5c9a868SElliott Hughes 	unsigned int firstblock, nsect;
118*d5c9a868SElliott Hughes 	uint8_t clen;
119*d5c9a868SElliott Hughes 	int r;
120*d5c9a868SElliott Hughes 	unsigned int max;
121*d5c9a868SElliott Hughes 	uint32_t offset;
122*d5c9a868SElliott Hughes 	unsigned char cdb[10];
123*d5c9a868SElliott Hughes 	DeclareThis(ScsiDevice_t);
124*d5c9a868SElliott Hughes 
125*d5c9a868SElliott Hughes 	firstblock=truncMtOffTo32u(where/(mt_off_t)This->scsi_sector_size);
126*d5c9a868SElliott Hughes 	/* 512,1024,2048,... bytes/sector supported */
127*d5c9a868SElliott Hughes 	offset=(smt_off_t) where % This->scsi_sector_size;
128*d5c9a868SElliott Hughes 	nsect=bytesToSectors(offset+len, This->scsi_sector_size);
129*d5c9a868SElliott Hughes #if defined(OS_sun) && defined(OS_i386)
130*d5c9a868SElliott Hughes 	if (This->scsi_sector_size>512)
131*d5c9a868SElliott Hughes 		firstblock*=This->scsi_sector_size/512; /* work around a uscsi bug */
132*d5c9a868SElliott Hughes #endif /* sun && i386 */
133*d5c9a868SElliott Hughes 
134*d5c9a868SElliott Hughes 	if (len>512) {
135*d5c9a868SElliott Hughes 		/* avoid buffer overruns. The transfer MUST be smaller or
136*d5c9a868SElliott Hughes 		* equal to the requested size! */
137*d5c9a868SElliott Hughes 		while (nsect*This->scsi_sector_size>len)
138*d5c9a868SElliott Hughes 			--nsect;
139*d5c9a868SElliott Hughes 		if(!nsect) {
140*d5c9a868SElliott Hughes 			fprintf(stderr,"Scsi buffer too small\n");
141*d5c9a868SElliott Hughes 			exit(1);
142*d5c9a868SElliott Hughes 		}
143*d5c9a868SElliott Hughes 		if(rwcmd == SCSI_IO_WRITE && offset) {
144*d5c9a868SElliott Hughes 			/* there seems to be no memmove before a write */
145*d5c9a868SElliott Hughes 			fprintf(stderr,"Unaligned write\n");
146*d5c9a868SElliott Hughes 			exit(1);
147*d5c9a868SElliott Hughes 		}
148*d5c9a868SElliott Hughes 		/* a better implementation should use bounce buffers.
149*d5c9a868SElliott Hughes 		 * However, in normal operation no buffer overruns or
150*d5c9a868SElliott Hughes 		 * unaligned writes should happen anyways, as the logical
151*d5c9a868SElliott Hughes 		 * sector size is (hopefully!) equal to the physical one
152*d5c9a868SElliott Hughes 		 */
153*d5c9a868SElliott Hughes 	}
154*d5c9a868SElliott Hughes 
155*d5c9a868SElliott Hughes 
156*d5c9a868SElliott Hughes 	max = scsi_max_length();
157*d5c9a868SElliott Hughes 
158*d5c9a868SElliott Hughes 	if (nsect > max)
159*d5c9a868SElliott Hughes 		nsect=max;
160*d5c9a868SElliott Hughes 
161*d5c9a868SElliott Hughes 	/* set up SCSI READ/WRITE command */
162*d5c9a868SElliott Hughes 	memset(cdb, 0, sizeof cdb);
163*d5c9a868SElliott Hughes 
164*d5c9a868SElliott Hughes 	switch(rwcmd) {
165*d5c9a868SElliott Hughes 		case SCSI_IO_READ:
166*d5c9a868SElliott Hughes 			cdb[0] = SCSI_READ;
167*d5c9a868SElliott Hughes 			break;
168*d5c9a868SElliott Hughes 		case SCSI_IO_WRITE:
169*d5c9a868SElliott Hughes 			cdb[0] = SCSI_WRITE;
170*d5c9a868SElliott Hughes 			break;
171*d5c9a868SElliott Hughes 	}
172*d5c9a868SElliott Hughes 
173*d5c9a868SElliott Hughes 	cdb[1] = 0;
174*d5c9a868SElliott Hughes 
175*d5c9a868SElliott Hughes 	if (firstblock > 0x1fffff || nsect > 0xff) {
176*d5c9a868SElliott Hughes 		/* I suspect that the ZIP drive also understands Group 1
177*d5c9a868SElliott Hughes 		 * commands. If that is indeed true, we may chose Group 1
178*d5c9a868SElliott Hughes 		 * more aggressively in the future */
179*d5c9a868SElliott Hughes 
180*d5c9a868SElliott Hughes 		cdb[0] |= SCSI_GROUP1;
181*d5c9a868SElliott Hughes 		clen=10; /* SCSI Group 1 cmd */
182*d5c9a868SElliott Hughes 
183*d5c9a868SElliott Hughes 		/* this is one of the rare case where explicit coding is
184*d5c9a868SElliott Hughes 		 * more portable than macros... The meaning of scsi command
185*d5c9a868SElliott Hughes 		 * bytes is standardised, whereas the preprocessor macros
186*d5c9a868SElliott Hughes 		 * handling it might be not... */
187*d5c9a868SElliott Hughes 
188*d5c9a868SElliott Hughes 		cdb[2] = (unsigned char) (firstblock >> 24) & 0xff;
189*d5c9a868SElliott Hughes 		cdb[3] = (unsigned char) (firstblock >> 16) & 0xff;
190*d5c9a868SElliott Hughes 		cdb[4] = (unsigned char) (firstblock >> 8) & 0xff;
191*d5c9a868SElliott Hughes 		cdb[5] = (unsigned char) firstblock & 0xff;
192*d5c9a868SElliott Hughes 		cdb[6] = 0;
193*d5c9a868SElliott Hughes 		cdb[7] = (unsigned char) (nsect >> 8) & 0xff;
194*d5c9a868SElliott Hughes 		cdb[8] = (unsigned char) nsect & 0xff;
195*d5c9a868SElliott Hughes 		cdb[9] = 0;
196*d5c9a868SElliott Hughes 	} else {
197*d5c9a868SElliott Hughes 		clen = 6; /* SCSI Group 0 cmd */
198*d5c9a868SElliott Hughes 		cdb[1] |= (unsigned char) ((firstblock >> 16) & 0x1f);
199*d5c9a868SElliott Hughes 		cdb[2] = (unsigned char) ((firstblock >> 8) & 0xff);
200*d5c9a868SElliott Hughes 		cdb[3] = (unsigned char) firstblock & 0xff;
201*d5c9a868SElliott Hughes 		cdb[4] = (unsigned char) nsect;
202*d5c9a868SElliott Hughes 		cdb[5] = 0;
203*d5c9a868SElliott Hughes 	}
204*d5c9a868SElliott Hughes 
205*d5c9a868SElliott Hughes 	if(This->privileged)
206*d5c9a868SElliott Hughes 		reclaim_privs();
207*d5c9a868SElliott Hughes 
208*d5c9a868SElliott Hughes 	r=scsi_cmd(This->fd, (unsigned char *)cdb, clen, rwcmd, buf,
209*d5c9a868SElliott Hughes 		   nsect*This->scsi_sector_size, This->extra_data);
210*d5c9a868SElliott Hughes 
211*d5c9a868SElliott Hughes 	if(This->privileged)
212*d5c9a868SElliott Hughes 		drop_privs();
213*d5c9a868SElliott Hughes 
214*d5c9a868SElliott Hughes 	if(r) {
215*d5c9a868SElliott Hughes 		perror(rwcmd == SCSI_IO_READ ? "SCMD_READ" : "SCMD_WRITE");
216*d5c9a868SElliott Hughes 		return -1;
217*d5c9a868SElliott Hughes 	}
218*d5c9a868SElliott Hughes #ifdef JPD
219*d5c9a868SElliott Hughes 	printf("finished %u for %u\n", firstblock, nsect);
220*d5c9a868SElliott Hughes #endif
221*d5c9a868SElliott Hughes 
222*d5c9a868SElliott Hughes #ifdef JPD
223*d5c9a868SElliott Hughes 	printf("zip: read or write OK\n");
224*d5c9a868SElliott Hughes #endif
225*d5c9a868SElliott Hughes 	if (offset>0)
226*d5c9a868SElliott Hughes 		memmove(buf,buf+offset,	nsect*This->scsi_sector_size-offset);
227*d5c9a868SElliott Hughes 	if (len==256) return 256;
228*d5c9a868SElliott Hughes 	else if (len==512) return 512;
229*d5c9a868SElliott Hughes 	else return (ssize_t)(nsect*This->scsi_sector_size-offset);
230*d5c9a868SElliott Hughes }
231*d5c9a868SElliott Hughes 
scsi_pread(Stream_t * Stream,char * buf,mt_off_t where,size_t len)232*d5c9a868SElliott Hughes static ssize_t scsi_pread(Stream_t *Stream, char *buf,
233*d5c9a868SElliott Hughes 			  mt_off_t where, size_t len)
234*d5c9a868SElliott Hughes {
235*d5c9a868SElliott Hughes #ifdef JPD
236*d5c9a868SElliott Hughes 	printf("zip: to read %d bytes at %d\n", len, where);
237*d5c9a868SElliott Hughes #endif
238*d5c9a868SElliott Hughes 	return scsi_io(Stream, buf, where, len, SCSI_IO_READ);
239*d5c9a868SElliott Hughes }
240*d5c9a868SElliott Hughes 
scsi_pwrite(Stream_t * Stream,char * buf,mt_off_t where,size_t len)241*d5c9a868SElliott Hughes static ssize_t scsi_pwrite(Stream_t *Stream, char *buf,
242*d5c9a868SElliott Hughes 			   mt_off_t where, size_t len)
243*d5c9a868SElliott Hughes {
244*d5c9a868SElliott Hughes #ifdef JPD
245*d5c9a868SElliott Hughes 	Printf("zip: to write %d bytes at %d\n", len, where);
246*d5c9a868SElliott Hughes #endif
247*d5c9a868SElliott Hughes 	return scsi_io(Stream, buf, where, len, SCSI_IO_WRITE);
248*d5c9a868SElliott Hughes }
249*d5c9a868SElliott Hughes 
scsi_get_data(Stream_t * Stream,time_t * date,mt_off_t * size,int * type,uint32_t * address)250*d5c9a868SElliott Hughes static int scsi_get_data(Stream_t *Stream, time_t *date, mt_off_t *size,
251*d5c9a868SElliott Hughes 			 int *type, uint32_t *address)
252*d5c9a868SElliott Hughes {
253*d5c9a868SElliott Hughes 	DeclareThis(ScsiDevice_t);
254*d5c9a868SElliott Hughes 
255*d5c9a868SElliott Hughes 	if(date || type || address)
256*d5c9a868SElliott Hughes 		fprintf(stderr, "Get_data call not supported\n");
257*d5c9a868SElliott Hughes 	if(size)
258*d5c9a868SElliott Hughes 		*size = This->device_size;
259*d5c9a868SElliott Hughes 	return 0;
260*d5c9a868SElliott Hughes }
261*d5c9a868SElliott Hughes 
262*d5c9a868SElliott Hughes 
263*d5c9a868SElliott Hughes 
264*d5c9a868SElliott Hughes static Class_t ScsiDeviceClass = {
265*d5c9a868SElliott Hughes 	0,
266*d5c9a868SElliott Hughes 	0,
267*d5c9a868SElliott Hughes 	scsi_pread,
268*d5c9a868SElliott Hughes 	scsi_pwrite,
269*d5c9a868SElliott Hughes 	0,
270*d5c9a868SElliott Hughes 	0,
271*d5c9a868SElliott Hughes 	set_geom_noop,
272*d5c9a868SElliott Hughes 	scsi_get_data, /* get_data */
273*d5c9a868SElliott Hughes 	0, /* pre-allocate */
274*d5c9a868SElliott Hughes 	0, /* dos-convert */
275*d5c9a868SElliott Hughes 	0 /* discard */
276*d5c9a868SElliott Hughes };
277*d5c9a868SElliott Hughes 
OpenScsi(struct device * dev,const char * name,int mode,char * errmsg,int mode2,int locked,int lockMode,mt_off_t * maxSize)278*d5c9a868SElliott Hughes Stream_t *OpenScsi(struct device *dev,
279*d5c9a868SElliott Hughes 		   const char *name, int mode, char *errmsg,
280*d5c9a868SElliott Hughes 		   int mode2, int locked, int lockMode,
281*d5c9a868SElliott Hughes 		   mt_off_t *maxSize)
282*d5c9a868SElliott Hughes {
283*d5c9a868SElliott Hughes 	int ret;
284*d5c9a868SElliott Hughes 	ScsiDevice_t *This;
285*d5c9a868SElliott Hughes 	if (!IS_SCSI(dev))
286*d5c9a868SElliott Hughes 		return NULL;
287*d5c9a868SElliott Hughes 
288*d5c9a868SElliott Hughes 	This = New(ScsiDevice_t);
289*d5c9a868SElliott Hughes 	if (!This){
290*d5c9a868SElliott Hughes 		printOom();
291*d5c9a868SElliott Hughes 		return 0;
292*d5c9a868SElliott Hughes 	}
293*d5c9a868SElliott Hughes 	memset((void*)This, 0, sizeof(ScsiDevice_t));
294*d5c9a868SElliott Hughes 	init_head(&This->head, &ScsiDeviceClass, NULL);
295*d5c9a868SElliott Hughes 	This->scsi_sector_size = 512;
296*d5c9a868SElliott Hughes 
297*d5c9a868SElliott Hughes 	if(dev) {
298*d5c9a868SElliott Hughes 		if(!(mode2 & NO_PRIV))
299*d5c9a868SElliott Hughes 			This->privileged = IS_PRIVILEGED(dev);
300*d5c9a868SElliott Hughes 		mode |= dev->mode;
301*d5c9a868SElliott Hughes 	}
302*d5c9a868SElliott Hughes 
303*d5c9a868SElliott Hughes 	precmd(dev);
304*d5c9a868SElliott Hughes 	if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
305*d5c9a868SElliott Hughes 		reclaim_privs();
306*d5c9a868SElliott Hughes 
307*d5c9a868SElliott Hughes 	/* End of stuff copied from top of plain_io.c before actual open */
308*d5c9a868SElliott Hughes 
309*d5c9a868SElliott Hughes 	This->fd = scsi_open(name, mode, IS_NOLOCK(dev)?0444:0666,
310*d5c9a868SElliott Hughes 			     &This->extra_data);
311*d5c9a868SElliott Hughes 
312*d5c9a868SElliott Hughes 	if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
313*d5c9a868SElliott Hughes 		drop_privs();
314*d5c9a868SElliott Hughes 
315*d5c9a868SElliott Hughes 	if (This->fd < 0) {
316*d5c9a868SElliott Hughes 		if(errmsg) {
317*d5c9a868SElliott Hughes #ifdef HAVE_SNPRINTF
318*d5c9a868SElliott Hughes 			snprintf(errmsg, 199, "Can't open %s: %s",
319*d5c9a868SElliott Hughes 				name, strerror(errno));
320*d5c9a868SElliott Hughes #else
321*d5c9a868SElliott Hughes 			sprintf(errmsg, "Can't open %s: %s",
322*d5c9a868SElliott Hughes 				name, strerror(errno));
323*d5c9a868SElliott Hughes #endif
324*d5c9a868SElliott Hughes 		}
325*d5c9a868SElliott Hughes 		goto exit_1;
326*d5c9a868SElliott Hughes 	}
327*d5c9a868SElliott Hughes 
328*d5c9a868SElliott Hughes 	if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
329*d5c9a868SElliott Hughes 		closeExec(This->fd);
330*d5c9a868SElliott Hughes 
331*d5c9a868SElliott Hughes 	if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
332*d5c9a868SElliott Hughes 		goto exit_0;
333*d5c9a868SElliott Hughes 
334*d5c9a868SElliott Hughes 	if(maxSize)
335*d5c9a868SElliott Hughes 		*maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
336*d5c9a868SElliott Hughes 	if(This->privileged)
337*d5c9a868SElliott Hughes 		reclaim_privs();
338*d5c9a868SElliott Hughes 	ret=scsi_init(This);
339*d5c9a868SElliott Hughes 	if(This->privileged)
340*d5c9a868SElliott Hughes 		drop_privs();
341*d5c9a868SElliott Hughes 	if(ret < 0)
342*d5c9a868SElliott Hughes 		goto exit_0;
343*d5c9a868SElliott Hughes 	dev->tot_sectors = This->tot_sectors;
344*d5c9a868SElliott Hughes 	return &This->head;
345*d5c9a868SElliott Hughes  exit_0:
346*d5c9a868SElliott Hughes 	close(This->fd);
347*d5c9a868SElliott Hughes  exit_1:
348*d5c9a868SElliott Hughes 	Free(This);
349*d5c9a868SElliott Hughes 	return NULL;
350*d5c9a868SElliott Hughes }
351