static long
sdbio(Chan* c, int write, char* a, long len, uvlong off)
{
- int nchange, hard, allocd;
+ int nchange, hard, allocd, locked;
long l;
uchar *b;
SDpart *pp;
poperror();
return 0;
}
- if(!(unit->inquiry[1] & 0x80)){
+ locked = (unit->inquiry[1] & 0x80) != 0;
+ if(!locked){
qunlock(&unit->ctl);
poperror();
}
b = (uchar*)a;
allocd = 0;
}else{
- b = sdmalloc(nb*unit->secsize);
- if(b == nil)
- error(Enomem);
+ while((b = sdmalloc(nb*unit->secsize)) == nil){
+ if(!waserror()){
+ resrcwait("no memory for sdbio");
+ poperror();
+ }
+ }
allocd = 1;
}
if(waserror()){
if(allocd)
sdfree(b);
- if(!(unit->inquiry[1] & 0x80))
+ if(!locked)
decref(&sdev->r); /* gadverdamme! */
nexterror();
}
sdfree(b);
poperror();
- if(unit->inquiry[1] & 0x80){
+ if(locked){
qunlock(&unit->ctl);
poperror();
}
}
data = nil;
- if(n > 0 && (data = sdmalloc(n)) == nil)
- error(Enomem);
+ while(n > 0 && (data = sdmalloc(n)) == nil){
+ if(!waserror()){
+ resrcwait("no memory for sdrio");
+ poperror();
+ }
+ }
if(waserror()){
sdfree(data);
r->data = nil;
SDunit *unit;
unit = r->unit;
+ unit->sense[0] = 0x80 | 0x70; /* valid; fixed-format */
unit->sense[2] = key;
unit->sense[12] = asc;
unit->sense[13] = ascq;
* Read capacity returns the LBA of the last sector.
*/
len = unit->sectors;
- if(len > 0)
+ if(len >= 0xffffffff)
+ len = 0xffffffff;
+ else if(len > 0)
len--;
p = r->data;
*p++ = len>>24;
}
if(n < 6 || n > sizeof(req->cmd))
error(Ebadarg);
- if((req = malloc(sizeof(SDreq))) == nil)
- error(Enomem);
+ req = smalloc(sizeof(SDreq));
req->unit = unit;
if(waserror()){
free(req);
d = nil;
if(waserror()){
- free(d);
qunlock(&unit->ctl);
if(sdev != nil)
decref(&sdev->r);
+ free(d);
nexterror();
}
error(Eperm);
if(d[0].mode != ~0UL)
perm->perm = (perm->perm & ~0777) | (d[0].mode & 0777);
-
- free(d);
- d = nil; USED(d);
+ poperror();
qunlock(&unit->ctl);
if(sdev != nil)
decref(&sdev->r);
- poperror();
+ free(d);
return n;
}