]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/9/port/devsd.c
devproc: make sure /proc/n/wait waits for the right process children
[plan9front.git] / sys / src / 9 / port / devsd.c
index b4cbd71f529765b6429b7fdf2dd05e587673e34a..6474a68f096aa488c029f26142ad292d6a77a89c 100644 (file)
@@ -770,7 +770,7 @@ sdclose(Chan* c)
 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;
@@ -832,7 +832,8 @@ sdbio(Chan* c, int write, char* a, long len, uvlong off)
                poperror();
                return 0;
        }
-       if(!(unit->inquiry[1] & 0x80)){
+       locked = (unit->inquiry[1] & 0x80) != 0;
+       if(!locked){
                qunlock(&unit->ctl);
                poperror();
        }
@@ -846,15 +847,18 @@ sdbio(Chan* c, int write, char* a, long len, uvlong off)
                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();
        }
@@ -896,7 +900,7 @@ sdbio(Chan* c, int write, char* a, long len, uvlong off)
                sdfree(b);
        poperror();
 
-       if(unit->inquiry[1] & 0x80){
+       if(locked){
                qunlock(&unit->ctl);
                poperror();
        }
@@ -928,8 +932,12 @@ sdrio(SDreq* r, void* a, long n)
        }
 
        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;
@@ -979,6 +987,7 @@ sdsetsense(SDreq *r, int status, int key, int asc, int ascq)
        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;
@@ -1063,7 +1072,9 @@ sdfakescsi(SDreq *r)
                 * 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;
@@ -1482,8 +1493,7 @@ sdwrite(Chan* c, void* a, long n, vlong off)
                        }               
                        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);
@@ -1550,10 +1560,10 @@ sdwstat(Chan* c, uchar* dp, int n)
 
        d = nil;
        if(waserror()){
-               free(d);
                qunlock(&unit->ctl);
                if(sdev != nil)
                        decref(&sdev->r);
+               free(d);
                nexterror();
        }
 
@@ -1592,13 +1602,11 @@ sdwstat(Chan* c, uchar* dp, int n)
                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;
 }