Limited the scope of some local variables in afs_CacheStoreProc
[openafs.git] / src / afs / afs_fetchstore.c
index 0f189af..66793d1 100644 (file)
@@ -69,17 +69,22 @@ rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
 
 afs_int32
 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
-                 afs_uint32 tlen, afs_uint32 *got)
+                 afs_uint32 tlen, afs_uint32 *bytesread)
 {
+    afs_int32 code;
     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
 
-    *got = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
-    if ((*got < 0)
+    *bytesread = 0;
+    code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
+    if (code < 0)
+       return EIO;
+    *bytesread = code;
+    if (code == tlen)
+        return 0;
 #if defined(KERNEL_HAVE_UERROR)
-               || (*got != tlen && getuerror())
-#endif
-               )
+    if (getuerror())
        return EIO;
+#endif
     return 0;
 }
 
@@ -147,12 +152,36 @@ rxfs_storeStatus(void *rock)
 }
 
 afs_int32
+rxfs_storeClose(void *r, struct AFSFetchStatus *OutStatus, int *doProcessFS)
+{
+    afs_int32 code;
+    struct AFSVolSync tsync;
+    struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
+
+    RX_AFS_GUNLOCK();
+    code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
+    RX_AFS_GLOCK();
+    if (!code)
+       *doProcessFS = 1;       /* Flag to run afs_ProcessFS() later on */
+
+    return code;
+}
+
+afs_int32
 rxfs_storeDestroy(void **r, afs_int32 error)
 {
     afs_int32 code = error;
     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
 
     *r = NULL;
+    if (v->call) {
+       afs_int32 code2;
+       RX_AFS_GUNLOCK();
+       code2 = rx_EndCall(v->call, code);
+       RX_AFS_GLOCK();
+       if (code2)
+           code = code2;
+    }
     if (v->tbuffer)
        osi_FreeLargeSpace(v->tbuffer);
     if (v->tiov)
@@ -167,6 +196,7 @@ struct storeOps rxfs_storeUfsOps = {
     rxfs_storeUfsRead,
     rxfs_storeUfsWrite,
     rxfs_storeStatus,
+    rxfs_storeClose,
     rxfs_storeDestroy
 };
 
@@ -176,13 +206,21 @@ struct storeOps rxfs_storeMemOps = {
     rxfs_storeMemRead,
     rxfs_storeMemWrite,
     rxfs_storeStatus,
+    rxfs_storeClose,
     rxfs_storeDestroy
 };
 
 afs_int32
-rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
+rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t tlen,
+               afs_size_t bytes, afs_size_t base,
+               struct storeOps **ops, void **rock)
 {
+    afs_int32 code;
     struct rxfs_storeVariables *v;
+    struct rx_call *tcall;
+
+    if ( !tc )
+       return -1;
 
     v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
     if (!v)
@@ -192,6 +230,31 @@ rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
     v->InStatus.ClientModTime = avc->f.m.Date;
     v->InStatus.Mask = AFS_SETMODTIME;
 
+    RX_AFS_GUNLOCK();
+    tcall = rx_NewCall(tc->id);
+#ifdef AFS_64BIT_CLIENT
+    if (!afs_serverHasNo64Bit(tc))
+       code = StartRXAFS_StoreData64(tcall, (struct AFSFid *) &avc->f.fid.Fid,
+                                  &v->InStatus, base, bytes, tlen);
+    else {
+       if (tlen > 0xFFFFFFFF) {
+           code = EFBIG;
+       }
+       else {
+           afs_int32 t1, t2, t3;
+           t1 = base;
+           t2 = bytes;
+           t3 = tlen;
+           code = StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
+                                    &v->InStatus, t1, t2, t3);
+       }
+    }
+#else /* AFS_64BIT_CLIENT */
+    code = StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
+                               &v->InStatus, base, bytes, tlen);
+#endif /* AFS_64BIT_CLIENT */
+    RX_AFS_GLOCK();
+
     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
        v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
        if (!v->tbuffer)
@@ -218,259 +281,583 @@ rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
        }
 #endif /* notdef */
     }
+
+    v->call = tcall;
     *rock = (void *)v;
     return 0;
 }
 
-/*
- * afs_UFSCacheFetchProc
- *
- * Description:
- *     Routine called on fetch; also tells people waiting for data
- *     that more has arrived.
+extern unsigned int storeallmissing;
+/*!
+ *     Called upon store.
  *
- * Parameters:
- *     acall : Ptr to the Rx call structure.
- *     afile : File descriptor for the cache file.
- *     abase : Base offset to fetch.
- *     adc   : Ptr to the dcache entry for the file, write-locked.
- *      avc   : Ptr to the vcache entry for the file.
- *     abytesToXferP  : Set to the number of bytes to xfer.
- *                      NOTE: This parameter is only used if AFS_NOSTATS
- *                             is not defined.
- *     abytesXferredP : Set to the number of bytes actually xferred.
- *                      NOTE: This parameter is only used if AFS_NOSTATS
- *                             is not defined.
+ * \param tc Ptr to the Rx connection structure involved.
+ * \param dclist pointer to the list of dcaches
+ * \param avc Ptr to the vcache entry.
+ * \param bytes per chunk
+ * \param base where to start the store
+ * \param length number of bytes to store
+ * \param anewDV Ptr to the dataversion after store
+ * \param doProcessFS Ptr to the processFS flag
+ * \param OutStatus Ptr to the OutStatus structure
+ * \param nchunks number of chunks to store
+ * \param nomoreP pointer to the "nomore" flag
  *
- * Environment:
- *     Nothing interesting.
+ * \note Environment: Nothing interesting.
  */
-
 int
-afs_UFSCacheFetchProc(register struct rx_call *acall, struct osi_file *afile,
-                     afs_size_t abase, struct dcache *adc,
-                     struct vcache *avc, afs_size_t * abytesToXferP,
-                     afs_size_t * abytesXferredP, afs_int32 lengthFound)
+afs_CacheStoreProc(register struct afs_conn *tc,
+                       struct dcache **dclist,
+                       struct vcache *avc,
+                       afs_size_t bytes,
+                       afs_size_t base,
+                       afs_size_t length,
+                       afs_hyper_t *anewDV,
+                       int *doProcessFS,
+                       struct AFSFetchStatus *OutStatus,
+                       afs_uint32 nchunks,
+                       int *nomoreP)
 {
-    afs_int32 length;
-    register afs_int32 code;
-    register char *tbuffer;
-    register int tlen;
-    int moredata = 0;
-
-    AFS_STATCNT(UFS_CacheFetchProc);
-    osi_Assert(WriteLocked(&adc->lock));
-    afile->offset = 0;         /* Each time start from the beginning */
-    length = lengthFound;
+    afs_int32 code = 0;
+    struct storeOps *ops;
+    void * rock = NULL;
+    int nomore = *nomoreP;
+    unsigned int i;
 #ifndef AFS_NOSTATS
-    (*abytesToXferP) = 0;
-    (*abytesXferredP) = 0;
+    struct afs_stats_xferData *xferP;  /* Ptr to this op's xfer struct */
+    osi_timeval_t xferStartTime,       /*FS xfer start time */
+      xferStopTime;                    /*FS xfer stop time */
+    afs_size_t bytesToXfer = 10000;    /* # bytes to xfer */
+    afs_size_t bytesXferred = 10000;   /* # bytes actually xferred */
 #endif /* AFS_NOSTATS */
-    tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
-    adc->validPos = abase;
-    do {
-       if (moredata) {
-           RX_AFS_GUNLOCK();
-           code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
-           RX_AFS_GLOCK();
-           length = ntohl(length);
-           if (code != sizeof(afs_int32)) {
-               osi_FreeLargeSpace(tbuffer);
-               code = rx_Error(acall);
-               return (code ? code : -1);      /* try to return code, not -1 */
+    XSTATS_DECLS;
+
+    code =  rxfs_storeInit(avc, tc, length, bytes, base, &ops, &rock);
+    if ( code ) {
+       osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed with %d", code);
+    }
+
+    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;
+       int *shouldwake;
+       if (!tdc) {
+           afs_warn("afs: missing dcache!\n");
+           storeallmissing++;
+           continue;   /* panic? */
+       }
+       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));
+       shouldwake = 0;
+       if (nomore) {
+           if (avc->asynchrony == -1) {
+               if (afs_defaultAsynchrony > (bytes - stored))
+                   shouldwake = &nomore;
            }
+           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);
+
+       AFS_STATCNT(CacheStoreProc);
+
+       XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
+       avc->f.truncPos = AFS_NOTRUNC;
+#ifndef AFS_NOSTATS
        /*
-        * 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.
+        * In this case, alen is *always* the amount of data we'll be trying
+        * to ship here.
         */
-       if (avc->f.states & CForeign) {
-           moredata = length & 0x80000000;
-           length &= ~0x80000000;
-       } else {
-           moredata = 0;
-       }
-#ifndef AFS_NOSTATS
-       (*abytesToXferP) += length;
+       bytesToXfer = alen;
+       bytesXferred = 0;
+
+       xferP = &(afs_stats_cmfullperf.rpc.
+                       fsXferTimes[AFS_STATS_FS_XFERIDX_STOREDATA]);
+       osi_GetuTime(&xferStartTime);
 #endif /* AFS_NOSTATS */
-       while (length > 0) {
-           tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
-#ifdef RX_KERNEL_TRACE
-           afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
-                      "before rx_Read");
-#endif
-           RX_AFS_GUNLOCK();
-           code = rx_Read(acall, tbuffer, tlen);
-           RX_AFS_GLOCK();
-#ifdef RX_KERNEL_TRACE
-           afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
-                      "after rx_Read");
-#endif
+
+       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
-           (*abytesXferredP) += code;
+           bytesXferred += byteswritten;
 #endif /* AFS_NOSTATS */
-           if (code != tlen) {
-               osi_FreeLargeSpace(tbuffer);
-               afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
-                          ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
-                          ICL_TYPE_INT32, length);
-               return -34;
+
+           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);
+
+#ifndef AFS_NOSTATS
+       osi_GetuTime(&xferStopTime);
+       (xferP->numXfers)++;
+       if (!code) {
+           (xferP->numSuccesses)++;
+           afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] +=
+               bytesXferred;
+           (xferP->sumBytes) +=
+               (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
+           afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
+           if (bytesXferred < xferP->minBytes)
+               xferP->minBytes = bytesXferred;
+           if (bytesXferred > xferP->maxBytes)
+               xferP->maxBytes = bytesXferred;
+
+           /*
+            * Tally the size of the object.  Note: we tally the actual size,
+            * NOT the number of bytes that made it out over the wire.
+            */
+           if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
+               (xferP->count[0])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
+               (xferP->count[1])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
+               (xferP->count[2])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
+               (xferP->count[3])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
+               (xferP->count[4])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
+               (xferP->count[5])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
+               (xferP->count[6])++;
+           else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
+               (xferP->count[7])++;
+           else
+               (xferP->count[8])++;
+
+           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))) {
+               afs_stats_TimeAssign((xferP->minTime), elapsedTime);
            }
-           code = afs_osi_Write(afile, -1, tbuffer, tlen);
-           if (code != tlen) {
-               osi_FreeLargeSpace(tbuffer);
-               return EIO;
+           if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
+               afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
            }
-           abase += tlen;
-           length -= tlen;
-           adc->validPos = abase;
-           if (afs_osi_Wakeup(&adc->validPos) == 0)
-               afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
-                          __FILE__, ICL_TYPE_INT32, __LINE__,
-                          ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
-                          adc->dflags);
        }
-    } while (moredata);
-    osi_FreeLargeSpace(tbuffer);
-    return 0;
+#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);
+       }
+       stored += tdc->f.chunkBytes;
+
+       /* ideally, I'd like to unlock the dcache and turn
+        * off the writing bit here, but that would
+        * require being able to retry StoreAllSegments in
+        * the event of a failure. It only really matters
+        * if user can't read from a 'locked' dcache or
+        * one which has the writing bit turned on. */
+    }
+    if (!code) {
+       code = (*ops->close)(rock, OutStatus, doProcessFS);
+       if (*doProcessFS) {
+           hadd32(*anewDV, 1);
+       }
+       XSTATS_END_TIME;
+    }
+    code = (*ops->destroy)(&rock, code);
 
-}                              /* afs_UFSCacheFetchProc */
+    *nomoreP = nomore;
+    return code;
+}
 
-/*!
- *     Called upon store.
- *
- * \param acall Ptr to the Rx call structure involved.
- * \param fP Ptr to the related file descriptor.
- * \param alen Size of the file in bytes.
- * \param avc Ptr to the vcache entry.
- * \param shouldWake is it "safe" to return early from close() ?
- * \param abytesToXferP Set to the number of bytes to xfer.
- *     NOTE: This parameter is only used if AFS_NOSTATS is not defined.
- * \param abytesXferredP Set to the number of bytes actually xferred.
- *     NOTE: This parameter is only used if AFS_NOSTATS is not defined.
- *
- * \note Environment: Nothing interesting.
- */
-int
-afs_CacheStoreProc(register struct rx_call *acall,
-                     register struct osi_file *fP,
-                     register afs_int32 alen, struct vcache *avc,
-                     int *shouldWake, afs_size_t * abytesToXferP,
-                     afs_size_t * abytesXferredP)
+/* rock and operations for RX_FILESERVER */
+
+struct rxfs_fetchVariables {
+    struct rx_call *call;
+    char *tbuffer;
+    struct iovec *iov;
+    afs_uint32 nio;
+    afs_int32 hasNo64bit;
+    afs_int32 iovno;
+    afs_int32 iovmax;
+};
+
+afs_int32
+rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
 {
     afs_int32 code;
     afs_uint32 tlen;
-    int offset = 0;
-    struct storeOps *ops;
-    void * rock = NULL;
+    struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
 
-    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);
-    code =  rxfs_storeInit(avc, &ops, &rock);
-    if ( code ) {
-       osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
+    *bytesread = 0;
+    tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
+    RX_AFS_GUNLOCK();
+    code = rx_Read(v->call, v->tbuffer, tlen);
+    RX_AFS_GLOCK();
+    if (code <= 0)
+       return -34;
+    *bytesread = code;
+    return 0;
+}
+
+afs_int32
+rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
+{
+    afs_int32 code;
+    struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
+
+    *bytesread = 0;
+    RX_AFS_GUNLOCK();
+    code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
+    RX_AFS_GLOCK();
+    if (code <= 0)
+       return -34;
+    *bytesread = code;
+    return 0;
+}
+
+
+afs_int32
+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;
+    struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
+
+    code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
+    if (code != tlen) {
+        return EIO;
     }
-    ((struct rxfs_storeVariables *)rock)->call = acall;
+    *byteswritten = code;
+    return 0;
+}
 
-    AFS_STATCNT(CacheStoreProc);
-#ifndef AFS_NOSTATS
-    /*
-     * In this case, alen is *always* the amount of data we'll be trying
-     * to ship here.
-     */
-    *(abytesToXferP) = alen;
-    *(abytesXferredP) = 0;
-#endif /* AFS_NOSTATS */
+afs_int32
+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;
+
+    code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
+    if (code != tlen) {
+        return EIO;
+    }
+    *byteswritten = code;
+    return 0;
+}
 
-    while ( alen > 0 ) {
-       afs_int32 bytesread, byteswritten;
-       code = (*ops->prepare)(rock, alen, &tlen);
-       if ( code )
-           break;
 
-       code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
-       if (code)
-           break;
+afs_int32
+rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
+                                       struct afs_FetchOutput *tsmall)
+{
+    afs_int32 code, code1 = 0;
+    struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
 
-       tlen = bytesread;
-       code = (*ops->write)(rock, tlen, &byteswritten);
-       if (code)
-           break;
-#ifndef AFS_NOSTATS
-       (*abytesXferredP) += byteswritten;
-#endif /* AFS_NOSTATS */
+    if (!v->call)
+       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);
+    RX_AFS_GLOCK();
+    if (!code && code1)
+       code = code1;
+
+    v->call = NULL;
+
+    return code;
+}
+
+afs_int32
+rxfs_fetchDestroy(void **r, afs_int32 error)
+{
+    afs_int32 code = error;
+    struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
+
+    *r = NULL;
+    if (v->tbuffer)
+       osi_FreeLargeSpace(v->tbuffer);
+    if (v->iov)
+       osi_FreeSmallSpace(v->iov);
+    osi_FreeSmallSpace(v);
+    return code;
+}
+
+afs_int32
+rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
+{
+    afs_int32 code;
+    register 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 */
+    }
+    return 0;
+}
+
+static
+struct fetchOps rxfs_fetchUfsOps = {
+    rxfs_fetchMore,
+    rxfs_fetchUfsRead,
+    rxfs_fetchUfsWrite,
+    rxfs_fetchClose,
+    rxfs_fetchDestroy
+};
+
+static
+struct fetchOps rxfs_fetchMemOps = {
+    rxfs_fetchMore,
+    rxfs_fetchMemRead,
+    rxfs_fetchMemWrite,
+    rxfs_fetchClose,
+    rxfs_fetchDestroy
+};
+
+afs_int32
+rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
+               afs_uint32 size, afs_uint32 *out_length, 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;
+#ifdef AFS_64BIT_CLIENT
+    afs_size_t tsize;
+    afs_size_t lengthFound;     /* as returned from server */
+#endif /* AFS_64BIT_CLIENT */
+
+    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);
+    RX_AFS_GLOCK();
+
+#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);
+           } else {
+               length_hi = 0;
+               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_int32 pos;
+           pos = base;
+           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);
+           RX_AFS_GLOCK();
+       }
+       afs_serverSetNo64Bit(tc);
+    }
+    if (!code) {
+       RX_AFS_GUNLOCK();
+       bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
+       RX_AFS_GLOCK();
+       if (bytes == sizeof(afs_int32))
+           length = ntohl(length);
+       else {
+           code = rx_Error(v->call);
+       }
+    }
+    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) {
+       RX_AFS_GUNLOCK();
+       bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
+       RX_AFS_GLOCK();
+       if (bytes == sizeof(afs_int32))
+           length = ntohl(length);
+       else
+           code = rx_Error(v->call);
+    }
+#endif /* AFS_64BIT_CLIENT */
+    if (code) {
+       osi_FreeSmallSpace(v);
+        return code;
+    }
 
-       offset += tlen;
-       alen -= tlen;
+    if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
+       v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
+       if (!v->tbuffer)
+           osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
+       osi_Assert(WriteLocked(&adc->lock));
+       fP->offset = 0;
+       *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
+    }
+    else {
+       afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
+                  ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
+                  ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
        /*
-        * if file has been locked on server, can allow
-        * store to continue
+        * We need to alloc the iovecs on the heap so that they are "pinned"
+        * rather than declare them on the stack - defect 11272
         */
-       if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
-           *shouldWake = 0;    /* only do this once */
-           afs_wakeup(avc);
-       }
+       v->iov = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
+                                               RX_MAXIOVECS);
+       if (!v->iov)
+           osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
+       *ops = (struct fetchOps *) &rxfs_fetchMemOps;
     }
-    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);
-    code = (*ops->destroy)(&rock, code);
-    return code;
+    *rock = (void *)v;
+    *out_length = length;
+    return 0;
 }
 
+
+/*!
+ * Routine called on fetch; also tells people waiting for data
+ *     that more has arrived.
+ *
+ * \param tc Ptr to the Rx connection structure.
+ * \param fP File descriptor for the cache file.
+ * \param abase 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.
+ * \param tsmall Ptr to the afs_FetchOutput structure.
+ *
+ * \note Environment: Nothing interesting.
+ */
 int
-afs_MemCacheFetchProc(register struct rx_call *acall,
-                     register struct osi_file *fP, afs_size_t abase,
-                     struct dcache *adc, struct vcache *avc,
-                     afs_size_t * abytesToXferP, afs_size_t * abytesXferredP,
-                     afs_int32 lengthFound)
+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)
 {
-    register struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
     register afs_int32 code;
-    afs_int32 length;
+    afs_uint32 length;
+    afs_uint32 bytesread, byteswritten;
+    struct fetchOps *ops = NULL;
+    void *rock = NULL;
     int moredata = 0;
-    struct iovec *tiov;                /* no data copying with iovec */
-    register int tlen, offset = 0;
-    int tnio;                  /* temp for iovec size */
-
-    AFS_STATCNT(afs_MemCacheFetchProc);
-    length = lengthFound;
-    afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_POINTER, mceP, ICL_TYPE_OFFSET,
-              ICL_HANDLE_OFFSET(abase), ICL_TYPE_INT32, length);
+    register int offset = 0;
+
+    XSTATS_DECLS;
 #ifndef AFS_NOSTATS
-    (*abytesToXferP) = 0;
-    (*abytesXferredP) = 0;
+    struct afs_stats_xferData *xferP;  /* Ptr to this op's xfer struct */
+    osi_timeval_t xferStartTime,       /*FS xfer start time */
+                   xferStopTime;       /*FS xfer stop time */
+    afs_size_t bytesToXfer = 0, bytesXferred = 0;
+#endif
+
+    AFS_STATCNT(CacheFetchProc);
+
+    XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
+
+    code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
+
+#ifndef AFS_NOSTATS
+    xferP =
+       &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
+    osi_GetuTime(&xferStartTime);
 #endif /* AFS_NOSTATS */
-    /*
-     * We need to alloc the iovecs on the heap so that they are "pinned" rather than
-     * declare them on the stack - defect 11272
-     */
-    tiov =
-       (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
-                                           RX_MAXIOVECS);
-    if (!tiov) {
-       osi_Panic
-           ("afs_MemCacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
-    }
+
     adc->validPos = abase;
-    do {
+
+    if ( !code ) do {
        if (moredata) {
-           RX_AFS_GUNLOCK();
-           code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
-           length = ntohl(length);
-           RX_AFS_GLOCK();
-           if (code != sizeof(afs_int32)) {
-               code = rx_Error(acall);
-               osi_FreeSmallSpace(tiov);
-               return (code ? code : -1);      /* try to return code, not -1 */
-           }
+           code = (*ops->more)(rock, &length, &moredata);
+           if ( code )
+               break;
        }
        /*
         * The fetch protocol is extended for the AFS/DFS translator
@@ -488,28 +875,34 @@ afs_MemCacheFetchProc(register struct rx_call *acall,
            moredata = 0;
        }
 #ifndef AFS_NOSTATS
-       (*abytesToXferP) += length;
+       bytesToXfer += length;
 #endif /* AFS_NOSTATS */
        while (length > 0) {
-           tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
-           RX_AFS_GUNLOCK();
-           code = rx_Readv(acall, tiov, &tnio, RX_MAXIOVECS, tlen);
-           RX_AFS_GLOCK();
+#ifdef RX_KERNEL_TRACE
+           afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
+                      "before rx_Read");
+#endif
+           code = (*ops->read)(rock, length, &bytesread);
+#ifdef RX_KERNEL_TRACE
+           afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
+                      "after rx_Read");
+#endif
 #ifndef AFS_NOSTATS
-           (*abytesXferredP) += code;
+           bytesXferred += bytesread;
 #endif /* AFS_NOSTATS */
-           if (code <= 0) {
+           if ( code ) {
                afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
                           ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
                           ICL_TYPE_INT32, length);
-               osi_FreeSmallSpace(tiov);
-               return -34;
+               code = -34;
+               break;
            }
-           tlen = code;
-           afs_MemWritevBlk(mceP, offset, tiov, tnio, tlen);
-           offset += tlen;
-           abase += tlen;
-           length -= tlen;
+           code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
+           if ( code )
+               break;
+           offset += bytesread;
+           abase += bytesread;
+           length -= bytesread;
            adc->validPos = abase;
            if (afs_osi_Wakeup(&adc->validPos) == 0)
                afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
@@ -517,9 +910,61 @@ afs_MemCacheFetchProc(register struct rx_call *acall,
                           ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
                           adc->dflags);
        }
+       code = 0;
     } while (moredata);
-    /* max of two sizes */
-    osi_FreeSmallSpace(tiov);
-    return 0;
+    if (!code)
+       code = (*ops->close)(rock, avc, adc, tsmall);
+    (*ops->destroy)(&rock, code);
+
+#ifndef AFS_NOSTATS
+    osi_GetuTime(&xferStopTime);
+    (xferP->numXfers)++;
+    if (!code) {
+       (xferP->numSuccesses)++;
+       afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
+       (xferP->sumBytes) +=
+               (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
+       afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
+       if (bytesXferred < xferP->minBytes)
+           xferP->minBytes = bytesXferred;
+       if (bytesXferred > xferP->maxBytes)
+           xferP->maxBytes = bytesXferred;
+
+       /*
+        * Tally the size of the object.  Note: we tally the actual size,
+        * NOT the number of bytes that made it out over the wire.
+        */
+       if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
+           (xferP->count[0])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
+           (xferP->count[1])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
+           (xferP->count[2])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
+           (xferP->count[3])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
+           (xferP->count[4])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
+           (xferP->count[5])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
+           (xferP->count[6])++;
+       else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
+           (xferP->count[7])++;
+       else
+           (xferP->count[8])++;
+
+       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))) {
+           afs_stats_TimeAssign((xferP->minTime), elapsedTime);
+       }
+       if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
+           afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
+       }
+    }
+#endif
+    XSTATS_END_TIME;
+    return code;
 }