libafs: crash in a more useful way if nchunks is zero
[openafs.git] / src / afs / afs_fetchstore.c
index a4a372a..16c0550 100644 (file)
@@ -19,7 +19,6 @@
 #undef kmem_free
 #undef mem_alloc
 #undef mem_free
-#undef register
 #endif /* AFS_ALPHA_ENV */
 #include "afsincludes.h"       /* Afs-based standard headers */
 #include "afs/afs_stats.h"     /* statistics */
 
 extern int cacheDiskType;
 
-
 #ifndef AFS_NOSTATS
-void
-FillStoreStats(int code, int idx, osi_timeval_t *xferStartTime,
-                 afs_size_t bytesToXfer, afs_size_t bytesXferred)
+static void
+FillStoreStats(int code, int idx, osi_timeval_t xferStartTime,
+              afs_size_t bytesToXfer, afs_size_t bytesXferred)
 {
     struct afs_stats_xferData *xferP;
     osi_timeval_t xferStopTime;
-    XSTATS_DECLS;
+    osi_timeval_t elapsedTime;
 
     xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[idx]);
     osi_GetuTime(&xferStopTime);
@@ -65,7 +63,7 @@ FillStoreStats(int code, int idx, osi_timeval_t *xferStartTime,
        else
            (xferP->count[8])++;
 
-       afs_stats_GetDiff(elapsedTime, (*xferStartTime), xferStopTime);
+       afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
        afs_stats_AddTo((xferP->sumTime), elapsedTime);
        afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
        if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
@@ -80,14 +78,7 @@ FillStoreStats(int code, int idx, osi_timeval_t *xferStartTime,
 
 /* rock and operations for RX_FILESERVER */
 
-struct rxfs_storeVariables {
-    struct rx_call *call;
-    char *tbuffer;
-    struct iovec *tiov;
-    afs_uint32 tnio;
-    afs_int32 hasNo64bit;
-    struct AFSStoreStatus InStatus;
-};
+
 
 afs_int32
 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
@@ -102,13 +93,12 @@ rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
     afs_int32 code;
     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
 
-    *tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
     RX_AFS_GUNLOCK();
-    code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, *tlen);
+    code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, size);
     RX_AFS_GLOCK();
     if (code <= 0) {
        code = rx_Error(v->call);
-       if ( !code )
+       if (!code)
            code = -33;
     }
     else {
@@ -196,14 +186,14 @@ afs_int32
 rxfs_storePadd(void *rock, afs_uint32 size)
 {
     afs_int32 code = 0;
-    int bsent, tlen;
+    afs_uint32 tlen;
     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
 
-    if ( !v->tbuffer )
+    if (!v->tbuffer)
        v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
     memset(v->tbuffer, 0, AFS_LRALLOCSIZ);
 
-    while (size > 0) {
+    while (size) {
        tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
        RX_AFS_GUNLOCK();
        code = rx_Write(v->call, v->tbuffer, tlen);
@@ -213,7 +203,7 @@ rxfs_storePadd(void *rock, afs_uint32 size)
            return -33; /* XXX */
        size -= tlen;
     }
-    return code;
+    return 0;
 }
 
 afs_int32
@@ -233,8 +223,15 @@ rxfs_storeClose(void *r, struct AFSFetchStatus *OutStatus, int *doProcessFS)
     struct AFSVolSync tsync;
     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
 
+    if (!v->call)
+       return -1;
     RX_AFS_GUNLOCK();
-    code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
+#ifdef AFS_64BIT_CLIENT
+    if (!v->hasNo64bit)
+       code = EndRXAFS_StoreData64(v->call, OutStatus, &tsync);
+    else
+#endif
+       code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
     RX_AFS_GLOCK();
     if (!code)
        *doProcessFS = 1;       /* Flag to run afs_ProcessFS() later on */
@@ -250,12 +247,11 @@ rxfs_storeDestroy(void **r, afs_int32 error)
 
     *r = NULL;
     if (v->call) {
-       afs_int32 code2;
        RX_AFS_GUNLOCK();
-       code2 = rx_EndCall(v->call, code);
+       code = rx_EndCall(v->call, error);
        RX_AFS_GLOCK();
-       if (code2)
-           code = code2;
+       if (!code && error)
+           code = error;
     }
     if (v->tbuffer)
        osi_FreeLargeSpace(v->tbuffer);
@@ -265,31 +261,109 @@ rxfs_storeDestroy(void **r, afs_int32 error)
     return code;
 }
 
+afs_int32
+afs_GenericStoreProc(struct storeOps *ops, void *rock,
+                    struct dcache *tdc, int *shouldwake,
+                    afs_size_t *bytesXferred)
+{
+    struct rxfs_storeVariables *svar = rock;
+    afs_uint32 tlen, bytesread, byteswritten;
+    afs_int32 code = 0;
+    int offset = 0;
+    afs_size_t size;
+    struct osi_file *tfile;
+
+    size = tdc->f.chunkBytes;
+
+    tfile = afs_CFileOpen(&tdc->f.inode);
+
+    while ( size > 0 ) {
+       code = (*ops->prepare)(rock, size, &tlen);
+       if ( code )
+           break;
+
+       code = (*ops->read)(rock, tfile, offset, tlen, &bytesread);
+       if (code)
+           break;
+
+       tlen = bytesread;
+       code = (*ops->write)(rock, tlen, &byteswritten);
+       if (code)
+           break;
+#ifndef AFS_NOSTATS
+       *bytesXferred += byteswritten;
+#endif /* AFS_NOSTATS */
+
+       offset += tlen;
+       size -= tlen;
+       /*
+        * if file has been locked on server, can allow
+        * store to continue
+        */
+       if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
+           *shouldwake = 0;    /* only do this once */
+           afs_wakeup(svar->vcache);
+       }
+    }
+    afs_CFileClose(tfile);
+
+    return code;
+}
+
 static
 struct storeOps rxfs_storeUfsOps = {
+#ifndef HAVE_STRUCT_LABEL_SUPPORT
     rxfs_storeUfsPrepare,
     rxfs_storeUfsRead,
     rxfs_storeUfsWrite,
     rxfs_storeStatus,
     rxfs_storePadd,
     rxfs_storeClose,
-    rxfs_storeDestroy
+    rxfs_storeDestroy,
+    afs_GenericStoreProc
+#else
+    .prepare =         rxfs_storeUfsPrepare,
+    .read =    rxfs_storeUfsRead,
+    .write =   rxfs_storeUfsWrite,
+    .status =  rxfs_storeStatus,
+    .padd =    rxfs_storePadd,
+    .close =   rxfs_storeClose,
+    .destroy = rxfs_storeDestroy,
+#ifdef AFS_LINUX26_ENV
+    .storeproc = afs_linux_storeproc
+#else
+    .storeproc = afs_GenericStoreProc
+#endif
+#endif
 };
 
 static
 struct storeOps rxfs_storeMemOps = {
+#ifndef HAVE_STRUCT_LABEL_SUPPORT
     rxfs_storeMemPrepare,
     rxfs_storeMemRead,
     rxfs_storeMemWrite,
     rxfs_storeStatus,
     rxfs_storePadd,
     rxfs_storeClose,
-    rxfs_storeDestroy
+    rxfs_storeDestroy,
+    afs_GenericStoreProc
+#else
+    .prepare = rxfs_storeMemPrepare,
+    .read =    rxfs_storeMemRead,
+    .write =   rxfs_storeMemWrite,
+    .status =  rxfs_storeStatus,
+    .padd =    rxfs_storePadd,
+    .close =   rxfs_storeClose,
+    .destroy = rxfs_storeDestroy,
+    .storeproc = afs_GenericStoreProc
+#endif
 };
 
 afs_int32
-rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t tlen,
-               afs_size_t bytes, afs_size_t base,
+rxfs_storeInit(struct vcache *avc, struct afs_conn *tc,
+                struct rx_connection *rxconn, afs_size_t base,
+               afs_size_t bytes, afs_size_t length,
                int sync, struct storeOps **ops, void **rock)
 {
     afs_int32 code;
@@ -305,27 +379,29 @@ rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t tlen,
 
     v->InStatus.ClientModTime = avc->f.m.Date;
     v->InStatus.Mask = AFS_SETMODTIME;
+    v->vcache = avc;
     if (sync & AFS_SYNC)
         v->InStatus.Mask |= AFS_FSYNC;
     RX_AFS_GUNLOCK();
-    v->call = rx_NewCall(tc->id);
+    v->call = rx_NewCall(rxconn);
     if (v->call) {
 #ifdef AFS_64BIT_CLIENT
        if (!afs_serverHasNo64Bit(tc))
-           code = StartRXAFS_StoreData64(v->call,(struct AFSFid*)&avc->f.fid.Fid,
-                                      &v->InStatus, base, bytes, tlen);
+           code = StartRXAFS_StoreData64(
+                               v->call, (struct AFSFid*)&avc->f.fid.Fid,
+                               &v->InStatus, base, bytes, length);
        else
-           if (tlen > 0xFFFFFFFF)
+           if (length > 0xFFFFFFFF)
                code = EFBIG;
            else {
-               afs_int32 t1 = base, t2 = bytes, t3 = tlen;
+               afs_int32 t1 = base, t2 = bytes, t3 = length;
                code = StartRXAFS_StoreData(v->call,
                                        (struct AFSFid *) &avc->f.fid.Fid,
                                         &v->InStatus, t1, t2, t3);
            }
 #else /* AFS_64BIT_CLIENT */
        code = StartRXAFS_StoreData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
-                                   &v->InStatus, base, bytes, tlen);
+                                   &v->InStatus, base, bytes, length);
 #endif /* AFS_64BIT_CLIENT */
     } else
        code = -1;
@@ -351,9 +427,9 @@ rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t tlen,
        /* for now, only do 'continue from close' code if file fits in one
         * chunk.  Could clearly do better: if only one modified chunk
         * then can still do this.  can do this on *last* modified chunk */
-       tlen = avc->f.m.Length - 1;     /* byte position of last byte we'll store */
+       length = avc->f.m.Length - 1; /* byte position of last byte we'll store */
        if (shouldWake) {
-           if (AFS_CHUNK(tlen) != 0)
+           if (AFS_CHUNK(length) != 0)
                *shouldWake = 0;
            else
                *shouldWake = 1;
@@ -364,7 +440,6 @@ rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t tlen,
     *rock = (void *)v;
     return 0;
 }
-
 unsigned int storeallmissing = 0;
 /*!
  *     Called for each chunk upon store.
@@ -382,36 +457,33 @@ unsigned int storeallmissing = 0;
  */
 afs_int32
 afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
-                       afs_size_t bytes,
-                       afs_hyper_t *anewDV,
-                       int *doProcessFS,
-                       struct AFSFetchStatus *OutStatus,
-                       afs_uint32 nchunks,
-                       int nomore,
-                       struct storeOps *ops, void *rock)
+                     afs_size_t bytes, afs_hyper_t *anewDV, int *doProcessFS,
+                     struct AFSFetchStatus *OutStatus, afs_uint32 nchunks,
+                     int nomore, struct storeOps *ops, void *rock)
 {
     int *shouldwake = NULL;
     unsigned int i;
     afs_int32 code = 0;
+    afs_size_t bytesXferred;
 
 #ifndef AFS_NOSTATS
     osi_timeval_t xferStartTime;       /*FS xfer start time */
     afs_size_t bytesToXfer = 10000;    /* # bytes to xfer */
-    afs_size_t bytesXferred = 10000;   /* # bytes actually xferred */
 #endif /* AFS_NOSTATS */
     XSTATS_DECLS;
+    osi_Assert(nchunks != 0);
 
     for (i = 0; i < nchunks && !code; i++) {
        int stored = 0;
-       struct osi_file *fP;
-       int offset = 0;
        struct dcache *tdc = dclist[i];
-       afs_int32 alen = tdc->f.chunkBytes;
+       afs_int32 size;
+
        if (!tdc) {
            afs_warn("afs: missing dcache!\n");
            storeallmissing++;
            continue;   /* panic? */
        }
+       size = tdc->f.chunkBytes;
        afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2, ICL_TYPE_POINTER, avc,
                    ICL_TYPE_INT32, tdc->f.chunk, ICL_TYPE_INT32, tdc->index,
                    ICL_TYPE_INT32, afs_inode2trace(&tdc->f.inode));
@@ -424,11 +496,10 @@ afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
            else if ((afs_uint32) avc->asynchrony >= (bytes - stored))
                shouldwake = &nomore;
        }
-       fP = afs_CFileOpen(&tdc->f.inode);
 
        afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
                    ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
-                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
+                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
 
        AFS_STATCNT(CacheStoreProc);
 
@@ -436,75 +507,30 @@ afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
        avc->f.truncPos = AFS_NOTRUNC;
 #ifndef AFS_NOSTATS
        /*
-        * In this case, alen is *always* the amount of data we'll be trying
+        * In this case, size is *always* the amount of data we'll be trying
         * to ship here.
         */
-       bytesToXfer = alen;
-       bytesXferred = 0;
+       bytesToXfer = size;
 
        osi_GetuTime(&xferStartTime);
 #endif /* AFS_NOSTATS */
+       bytesXferred = 0;
 
-       while ( alen > 0 ) {
-           afs_uint32 tlen;
-           afs_int32 bytesread, byteswritten;
-           code = (*ops->prepare)(rock, alen, &tlen);
-           if ( code )
-               break;
-
-           code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
-           if (code)
-               break;
-
-           tlen = bytesread;
-           code = (*ops->write)(rock, tlen, &byteswritten);
-           if (code)
-               break;
-#ifndef AFS_NOSTATS
-           bytesXferred += byteswritten;
-#endif /* AFS_NOSTATS */
+       code = (*ops->storeproc)(ops, rock, tdc, shouldwake,
+                                    &bytesXferred);
 
-           offset += tlen;
-           alen -= tlen;
-           /*
-            * if file has been locked on server, can allow
-            * store to continue
-            */
-           if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
-               *shouldwake = 0;        /* only do this once */
-               afs_wakeup(avc);
-           }
-       }
        afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
                    ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
-                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
+                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
 
 #ifndef AFS_NOSTATS
        FillStoreStats(code, AFS_STATS_FS_XFERIDX_STOREDATA,
-                   &xferStartTime, bytesToXfer, bytesXferred);
+                   xferStartTime, bytesToXfer, bytesXferred);
 #endif /* AFS_NOSTATS */
 
-       afs_CFileClose(fP);
        if ((tdc->f.chunkBytes < afs_OtherCSize)
                && (i < (nchunks - 1)) && code == 0) {
-           int bsent, tlen, sbytes = afs_OtherCSize - tdc->f.chunkBytes;
-           char *tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
-
-           while (sbytes > 0) {
-               tlen = (sbytes > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : sbytes);
-               memset(tbuffer, 0, tlen);
-               RX_AFS_GUNLOCK();
-               bsent = rx_Write(
-                       ((struct rxfs_storeVariables*)rock)->call, tbuffer, tlen);
-               RX_AFS_GLOCK();
-
-               if (bsent != tlen) {
-                   code = -33; /* XXX */
-                   break;
-               }
-               sbytes -= tlen;
-           }
-           osi_FreeLargeSpace(tbuffer);
+           code = (*ops->padd)(rock, afs_OtherCSize - tdc->f.chunkBytes);
        }
        stored += tdc->f.chunkBytes;
        /* ideally, I'd like to unlock the dcache and turn
@@ -522,7 +548,8 @@ afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
        }
        XSTATS_END_TIME;
     }
-    code = (*ops->destroy)(&rock, code);
+    if (ops)
+       code = (*ops->destroy)(&rock, code);
     return code;
 }
 
@@ -544,24 +571,22 @@ afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
  */
 int
 afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
-                       struct vrequest *areq, int sync,
-                       unsigned int minj, unsigned int high,
-                       unsigned int moredata,
-                       afs_hyper_t *anewDV, afs_size_t *amaxStoredLength)
+                    struct vrequest *areq, int sync, unsigned int minj,
+                    unsigned int high, unsigned int moredata,
+                    afs_hyper_t *anewDV, afs_size_t *amaxStoredLength)
 {
     afs_int32 code = 0;
     struct storeOps *ops;
     void * rock = NULL;
     unsigned int i, j;
 
-    struct AFSStoreStatus InStatus;
     struct AFSFetchStatus OutStatus;
     int doProcessFS = 0;
     afs_size_t base, bytes, length;
-    afs_uint32 nchunks;
     int nomore;
     unsigned int first = 0;
     struct afs_conn *tc;
+    struct rx_connection *rxconn;
 
     for (bytes = 0, j = 0; !code && j <= high; j++) {
        if (dcList[j]) {
@@ -577,6 +602,7 @@ afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
            }
        }
        if (bytes && (j == high || !dcList[j + 1])) {
+           afs_uint32 nchunks;
            struct dcache **dclist = &dcList[first];
            /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
            base = AFS_CHUNKTOBASE(first + minj);
@@ -605,21 +631,19 @@ afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
                       ICL_HANDLE_OFFSET(length));
 
            do {
-               tc = afs_Conn(&avc->f.fid, areq, 0);
+               tc = afs_Conn(&avc->f.fid, areq, 0, &rxconn);
 
 #ifdef AFS_64BIT_CLIENT
              restart:
 #endif
-               code = rxfs_storeInit(avc, tc, length, bytes, base,
-                                       sync, &ops, &rock);
-               if ( code ) {
-                   osi_Panic(
-                   "afs_CacheStoreProc: rxfs_storeInit failed with %d", code);
+               code = rxfs_storeInit(avc, tc, rxconn, base, bytes, length,
+                                     sync, &ops, &rock);
+               if ( !code ) {
+                   code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
+                                                &doProcessFS, &OutStatus,
+                                                nchunks, nomore, ops, rock);
                }
 
-               code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
-                       &doProcessFS, &OutStatus, nchunks, nomore, ops, rock);
-
 #ifdef AFS_64BIT_CLIENT
                if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
                    afs_serverSetNo64Bit(tc);
@@ -627,7 +651,7 @@ afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
                }
 #endif /* AFS_64BIT_CLIENT */
            } while (afs_Analyze
-                    (tc, code, &avc->f.fid, areq,
+                    (tc, rxconn, code, &avc->f.fid, areq,
                      AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
                      NULL));
 
@@ -702,7 +726,7 @@ struct rxfs_fetchVariables {
     struct rx_call *call;
     char *tbuffer;
     struct iovec *iov;
-    afs_uint32 nio;
+    afs_int32 nio;
     afs_int32 hasNo64bit;
     afs_int32 iovno;
     afs_int32 iovmax;
@@ -744,9 +768,8 @@ rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
 
 
 afs_int32
-rxfs_fetchMemWrite(void *r, struct osi_file *fP,
-                       afs_uint32 offset, afs_uint32 tlen,
-                       afs_uint32 *byteswritten)
+rxfs_fetchMemWrite(void *r, struct osi_file *fP, afs_uint32 offset,
+                  afs_uint32 tlen, afs_uint32 *byteswritten)
 {
     afs_int32 code;
     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
@@ -761,9 +784,8 @@ rxfs_fetchMemWrite(void *r, struct osi_file *fP,
 }
 
 afs_int32
-rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
-                       afs_uint32 offset, afs_uint32 tlen,
-                       afs_uint32 *byteswritten)
+rxfs_fetchUfsWrite(void *r, struct osi_file *fP, afs_uint32 offset,
+                  afs_uint32 tlen, afs_uint32 *byteswritten)
 {
     afs_int32 code;
     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
@@ -779,7 +801,7 @@ rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
 
 afs_int32
 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
-                                       struct afs_FetchOutput *tsmall)
+               struct afs_FetchOutput *o)
 {
     afs_int32 code, code1 = 0;
     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
@@ -788,14 +810,15 @@ rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
        return -1;
 
     RX_AFS_GUNLOCK();
-    code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
-                             &tsmall->CallBack,
-                             &tsmall->tsync);
-    RX_AFS_GLOCK();
-
-    RX_AFS_GUNLOCK();
-    if (v->call)
-       code1 = rx_EndCall(v->call, code);
+#ifdef AFS_64BIT_CLIENT
+    if (!v->hasNo64bit)
+        code = EndRXAFS_FetchData64(v->call, &o->OutStatus, &o->CallBack,
+                               &o->tsync);
+    else
+#endif
+        code = EndRXAFS_FetchData(v->call, &o->OutStatus, &o->CallBack,
+                               &o->tsync);
+    code1 = rx_EndCall(v->call, code);
     RX_AFS_GLOCK();
     if (!code && code1)
        code = code1;
@@ -812,6 +835,13 @@ rxfs_fetchDestroy(void **r, afs_int32 error)
     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
 
     *r = NULL;
+    if (v->call) {
+        RX_AFS_GUNLOCK();
+       code = rx_EndCall(v->call, error);
+        RX_AFS_GLOCK();
+       if (error)
+           code = error;
+    }
     if (v->tbuffer)
        osi_FreeLargeSpace(v->tbuffer);
     if (v->iov)
@@ -821,20 +851,33 @@ rxfs_fetchDestroy(void **r, afs_int32 error)
 }
 
 afs_int32
-rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
+rxfs_fetchMore(void *r, afs_int32 *length, afs_uint32 *moredata)
 {
     afs_int32 code;
-    register struct rxfs_fetchVariables *v
-           = (struct rxfs_fetchVariables *)r;
+    struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
 
-    RX_AFS_GUNLOCK();
-    code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
-    *length = ntohl(*length);
-    RX_AFS_GLOCK();
-    if (code != sizeof(afs_int32)) {
-       code = rx_Error(v->call);
-       return (code ? code : -1);      /* try to return code, not -1 */
+    /*
+     * The fetch protocol is extended for the AFS/DFS translator
+     * to allow multiple blocks of data, each with its own length,
+     * to be returned. As long as the top bit is set, there are more
+     * blocks expected.
+     *
+     * We do not do this for AFS file servers because they sometimes
+     * return large negative numbers as the transfer size.
+     */
+    if (*moredata) {
+       RX_AFS_GUNLOCK();
+       code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
+       RX_AFS_GLOCK();
+       *length = ntohl(*length);
+       if (code != sizeof(afs_int32)) {
+           code = rx_Error(v->call);
+           *moredata = 0;
+           return (code ? code : -1);  /* try to return code, not -1 */
+        }
     }
+    *moredata = *length & 0x80000000;
+    *length &= ~0x80000000;
     return 0;
 }
 
@@ -857,104 +900,136 @@ struct fetchOps rxfs_fetchMemOps = {
 };
 
 afs_int32
-rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
-               afs_uint32 size, afs_uint32 *alength, struct dcache *adc,
-               struct osi_file *fP, struct fetchOps **ops, void **rock)
+rxfs_fetchInit(struct afs_conn *tc, struct rx_connection *rxconn,
+               struct vcache *avc, afs_offs_t base,
+              afs_uint32 size, afs_int32 *alength, struct dcache *adc,
+              struct osi_file *fP, struct fetchOps **ops, void **rock)
 {
     struct rxfs_fetchVariables *v;
-    int code, code1;
-    afs_int32 length_hi, length, bytes;
+    int code = 0, code1 = 0;
 #ifdef AFS_64BIT_CLIENT
-    afs_size_t tsize;
-    afs_size_t lengthFound;     /* as returned from server */
-#endif /* AFS_64BIT_CLIENT */
+    afs_uint32 length_hi = 0;
+#endif
+    afs_uint32 length, bytes;
 
-    v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
+    v = (struct rxfs_fetchVariables *)
+           osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
     if (!v)
         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
     memset(v, 0, sizeof(struct rxfs_fetchVariables));
 
     RX_AFS_GUNLOCK();
-    v->call = rx_NewCall(tc->id);
+    v->call = rx_NewCall(rxconn);
     RX_AFS_GLOCK();
-
+    if (v->call) {
 #ifdef AFS_64BIT_CLIENT
-    length_hi = code = 0;
-    if (!afs_serverHasNo64Bit(tc)) {
-       tsize = size;
-       RX_AFS_GUNLOCK();
-       code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
-                                       base, tsize);
-       if (code != 0) {
-           RX_AFS_GLOCK();
-           afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
-                           ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
-       } else {
-           bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
-           RX_AFS_GLOCK();
-           if (bytes == sizeof(afs_int32)) {
-               length_hi = ntohl(length_hi);
+       afs_size_t length64;     /* as returned from server */
+       if (!afs_serverHasNo64Bit(tc)) {
+           afs_uint64 llbytes = size;
+           RX_AFS_GUNLOCK();
+           code = StartRXAFS_FetchData64(v->call,
+                                         (struct AFSFid *) &avc->f.fid.Fid,
+                                         base, llbytes);
+           if (code != 0) {
+               RX_AFS_GLOCK();
+               afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
+                              ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
            } else {
-               length_hi = 0;
-               code = rx_Error(v->call);
+               bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
+               RX_AFS_GLOCK();
+               if (bytes == sizeof(afs_int32)) {
+                   length_hi = ntohl(length_hi);
+               } else {
+                   code = rx_Error(v->call);
+                   RX_AFS_GUNLOCK();
+                   code1 = rx_EndCall(v->call, code);
+                   RX_AFS_GLOCK();
+                   v->call = NULL;
+               }
+           }
+       }
+       if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
+           if (base > 0x7FFFFFFF) {
+               code = EFBIG;
+           } else {
+                afs_uint32 pos;
+               pos = base;
                RX_AFS_GUNLOCK();
-               code1 = rx_EndCall(v->call, code);
+               if (!v->call)
+                   v->call = rx_NewCall(rxconn);
+               code =
+                   StartRXAFS_FetchData(
+                               v->call, (struct AFSFid*)&avc->f.fid.Fid,
+                               pos, size);
                RX_AFS_GLOCK();
-               v->call = NULL;
            }
+           afs_serverSetNo64Bit(tc);
        }
-    }
-    if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
-       if (base > 0x7FFFFFFF) {
-           code = EFBIG;
-       } else {
-           afs_int32 pos;
-           pos = base;
+       if (!code) {
            RX_AFS_GUNLOCK();
-           if (!v->call)
-               v->call = rx_NewCall(tc->id);
-           code =
-               StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
-                                       pos, size);
+           bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
            RX_AFS_GLOCK();
+           if (bytes == sizeof(afs_int32))
+               length = ntohl(length);
+           else {
+               RX_AFS_GUNLOCK();
+               code = rx_Error(v->call);
+                code1 = rx_EndCall(v->call, code);
+               v->call = NULL;
+               RX_AFS_GLOCK();
+           }
        }
-       afs_serverSetNo64Bit(tc);
-    }
-    if (!code) {
+       FillInt64(length64, length_hi, length);
+       afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
+                  ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
+                  ICL_TYPE_OFFSET,
+                  ICL_HANDLE_OFFSET(length64));
+       *alength = length;
+#else /* AFS_64BIT_CLIENT */
        RX_AFS_GUNLOCK();
-       bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
+       code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
+                                    base, size);
        RX_AFS_GLOCK();
-       if (bytes == sizeof(afs_int32))
-           length = ntohl(length);
-       else {
-           code = rx_Error(v->call);
+       if (code == 0) {
+           RX_AFS_GUNLOCK();
+           bytes =
+               rx_Read(v->call, (char *)&length, sizeof(afs_int32));
+           RX_AFS_GLOCK();
+           if (bytes == sizeof(afs_int32)) {
+                *alength = ntohl(length);
+           } else {
+               code = rx_Error(v->call);
+                code1 = rx_EndCall(v->call, code);
+               v->call = NULL;
+           }
        }
-    }
-    FillInt64(lengthFound, length_hi, length);
-    afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
-              ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
-              ICL_TYPE_OFFSET,
-              ICL_HANDLE_OFFSET(lengthFound));
-#else /* AFS_64BIT_CLIENT */
-    RX_AFS_GUNLOCK();
-    code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
-                                base, size);
-    RX_AFS_GLOCK();
-    if (code == 0) {
+#endif /* AFS_64BIT_CLIENT */
+    } else
+       code = -1;
+
+    /* We need to cast here, in order to avoid issues if *alength is
+     * negative. Some, older, fileservers can return a negative length,
+     * which the rest of the code deals correctly with. */
+    if (code == 0 && *alength > (afs_int32) size) {
+       /* The fileserver told us it is going to send more data than we
+        * requested. It shouldn't do that, and accepting that much data
+        * can make us take up more cache space than we're supposed to,
+        * so error. */
+       code = rx_Error(v->call);
        RX_AFS_GUNLOCK();
-       bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
+       code1 = rx_EndCall(v->call, code);
        RX_AFS_GLOCK();
-       if (bytes == sizeof(afs_int32))
-           length = ntohl(length);
-       else
-           code = rx_Error(v->call);
+       v->call = NULL;
+       code = EIO;
     }
-#endif /* AFS_64BIT_CLIENT */
+
+    if (!code && code1)
+       code = code1;
+
     if (code) {
        osi_FreeSmallSpace(v);
         return code;
     }
-
     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
        v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
        if (!v->tbuffer)
@@ -977,7 +1052,6 @@ rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
        *ops = (struct fetchOps *) &rxfs_fetchMemOps;
     }
     *rock = (void *)v;
-    *alength = length;
     return 0;
 }
 
@@ -986,9 +1060,10 @@ rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
  * Routine called on fetch; also tells people waiting for data
  *     that more has arrived.
  *
- * \param tc Ptr to the Rx connection structure.
+ * \param tc Ptr to the AFS connection structure.
+ * \param rxconn Ptr to the Rx connection structure.
  * \param fP File descriptor for the cache file.
- * \param abase Base offset to fetch.
+ * \param base Base offset to fetch.
  * \param adc Ptr to the dcache entry for the file, write-locked.
  * \param avc Ptr to the vcache entry for the file.
  * \param size Amount of data that should be fetched.
@@ -997,19 +1072,18 @@ rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
  * \note Environment: Nothing interesting.
  */
 int
-afs_CacheFetchProc(register struct afs_conn *tc,
-                   register struct osi_file *fP, afs_size_t abase,
-                   struct dcache *adc, struct vcache *avc,
-                   afs_int32 size,
-                   struct afs_FetchOutput *tsmall)
+afs_CacheFetchProc(struct afs_conn *tc, struct rx_connection *rxconn,
+                   struct osi_file *fP, afs_size_t base,
+                  struct dcache *adc, struct vcache *avc, afs_int32 size,
+                  struct afs_FetchOutput *tsmall)
 {
-    register afs_int32 code;
-    afs_uint32 length;
+    afs_int32 code;
+    afs_int32 length;
     afs_uint32 bytesread, byteswritten;
     struct fetchOps *ops = NULL;
     void *rock = NULL;
-    int moredata = 0;
-    register int offset = 0;
+    afs_uint32 moredata = 0;
+    int offset = 0;
 
     XSTATS_DECLS;
 #ifndef AFS_NOSTATS
@@ -1021,35 +1095,29 @@ afs_CacheFetchProc(register struct afs_conn *tc,
 
     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
 
-    code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
+    /*
+     * Locks held:
+     * avc->lock(R) if setLocks && !slowPass
+     * avc->lock(W) if !setLocks || slowPass
+     * adc->lock(W)
+     */
+    code = rxfs_fetchInit(
+               tc, rxconn, avc, base, size, &length, adc, fP, &ops, &rock);
 
 #ifndef AFS_NOSTATS
     osi_GetuTime(&xferStartTime);
 #endif /* AFS_NOSTATS */
 
-    adc->validPos = abase;
+    if (adc) {
+       adc->validPos = base;
+    }
 
     if ( !code ) do {
-       if (moredata) {
+       if (avc->f.states & CForeign) {
            code = (*ops->more)(rock, &length, &moredata);
            if ( code )
                break;
        }
-       /*
-        * The fetch protocol is extended for the AFS/DFS translator
-        * to allow multiple blocks of data, each with its own length,
-        * to be returned. As long as the top bit is set, there are more
-        * blocks expected.
-        *
-        * We do not do this for AFS file servers because they sometimes
-        * return large negative numbers as the transfer size.
-        */
-       if (avc->f.states & CForeign) {
-           moredata = length & 0x80000000;
-           length &= ~0x80000000;
-       } else {
-           moredata = 0;
-       }
 #ifndef AFS_NOSTATS
        bytesToXfer += length;
 #endif /* AFS_NOSTATS */
@@ -1077,9 +1145,9 @@ afs_CacheFetchProc(register struct afs_conn *tc,
            if ( code )
                break;
            offset += bytesread;
-           abase += bytesread;
+           base += bytesread;
            length -= bytesread;
-           adc->validPos = abase;
+           adc->validPos = base;
            if (afs_osi_Wakeup(&adc->validPos) == 0)
                afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
                           __FILE__, ICL_TYPE_INT32, __LINE__,
@@ -1090,10 +1158,11 @@ afs_CacheFetchProc(register struct afs_conn *tc,
     } while (moredata);
     if (!code)
        code = (*ops->close)(rock, avc, adc, tsmall);
-    (*ops->destroy)(&rock, code);
+    if (ops)
+       (*ops->destroy)(&rock, code);
 
 #ifndef AFS_NOSTATS
-    FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA,&xferStartTime,
+    FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA, xferStartTime,
                        bytesToXfer, bytesXferred);
 #endif
     XSTATS_END_TIME;