Use afs_DestroyReq in afs_PrefetchNoCache()
[openafs.git] / src / afs / afs_bypasscache.c
index bb1baf4..847eaf0 100644 (file)
@@ -60,9 +60,7 @@
 
 #include <afsconfig.h>
 #include "afs/param.h"
-
-#if defined(AFS_CACHE_BYPASS) && defined(AFS_LINUX24_ENV)
-
+#if defined(AFS_CACHE_BYPASS) || defined(UKERNEL)
 #include "afs/afs_bypasscache.h"
 
 /*
 
 
 int cache_bypass_strategy   =  NEVER_BYPASS_CACHE;
-int cache_bypass_threshold  =          AFS_CACHE_BYPASS_DISABLED; /* file size > threshold triggers bypass */
+afs_size_t cache_bypass_threshold  =   AFS_CACHE_BYPASS_DISABLED; /* file size > threshold triggers bypass */
 int cache_bypass_prefetch = 1; /* Should we do prefetching ? */
 
 extern afs_rwlock_t afs_xcbhash;
@@ -130,7 +128,6 @@ afs_TransitionToBypass(struct vcache *avc,
 {
 
     afs_int32 code;
-    struct vrequest treq;
     int setDesire = 0;
     int setManual = 0;
 
@@ -142,43 +139,38 @@ afs_TransitionToBypass(struct vcache *avc,
     if (aflags & TRANSSetManualBit)
        setManual = 1;
 
-#ifdef AFS_BOZONLOCK_ENV
-    afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
-#else
     AFS_GLOCK();
-#endif
 
     ObtainWriteLock(&avc->lock, 925);
     /*
      * Someone may have beat us to doing the transition - we had no lock
      * when we checked the flag earlier.  No cause to panic, just return.
      */
-    if (avc->f.states & FCSBypass)
+    if (avc->cachingStates & FCSBypass)
        goto done;
 
     /* If we never cached this, just change state */
     if (setDesire && (!(avc->cachingStates & FCSBypass))) {
-       avc->f.states |= FCSBypass;
+       avc->cachingStates |= FCSBypass;
        goto done;
     }
 
     /* cg2v, try to store any chunks not written 20071204 */
     if (avc->execsOrWriters > 0) {
-       code = afs_InitReq(&treq, acred);
-       if (!code)
-           code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE);
+       struct vrequest *treq = NULL;
+
+       code = afs_CreateReq(&treq, acred);
+       if (!code) {
+           code = afs_StoreAllSegments(avc, treq, AFS_SYNC | AFS_LASTSTORE);
+           afs_DestroyReq(treq);
+       }
     }
 
-#if 0
     /* also cg2v, don't dequeue the callback */
-    ObtainWriteLock(&afs_xcbhash, 956);
-    afs_DequeueCallback(avc);
-    ReleaseWriteLock(&afs_xcbhash);
-#endif
-    avc->f.states &= ~(CStatd | CDirty);      /* next reference will re-stat */
+    /* next reference will re-stat */
+    afs_StaleVCacheFlags(avc, AFS_STALEVC_NOCB, CDirty);
     /* now find the disk cache entries */
     afs_TryToSmush(avc, acred, 1);
-    osi_dnlc_purgedp(avc);
     if (avc->linkData && !(avc->f.states & CCore)) {
        afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
        avc->linkData = NULL;
@@ -193,11 +185,7 @@ afs_TransitionToBypass(struct vcache *avc,
 
 done:
     ReleaseWriteLock(&avc->lock);
-#ifdef AFS_BOZONLOCK_ENV
-    afs_BozonUnlock(&avc->pvnLock, avc);
-#else
     AFS_GUNLOCK();
-#endif
 }
 
 /*
@@ -223,27 +211,21 @@ afs_TransitionToCaching(struct vcache *avc,
     if (aflags & TRANSSetManualBit)
        setManual = 1;
 
-#ifdef AFS_BOZONLOCK_ENV
-    afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
-#else
     AFS_GLOCK();
-#endif
     ObtainWriteLock(&avc->lock, 926);
     /*
      * Someone may have beat us to doing the transition - we had no lock
      * when we checked the flag earlier.  No cause to panic, just return.
      */
-    if (!(avc->f.states & FCSBypass))
+    if (!(avc->cachingStates & FCSBypass))
        goto done;
 
     /* Ok, we actually do need to flush */
-    ObtainWriteLock(&afs_xcbhash, 957);
-    afs_DequeueCallback(avc);
-    avc->f.states &= ~(CStatd | CDirty);       /* next reference will re-stat cache entry */
-    ReleaseWriteLock(&afs_xcbhash);
+    /* next reference will re-stat cache entry */
+    afs_StaleVCacheFlags(avc, 0, CDirty);
+
     /* now find the disk cache entries */
     afs_TryToSmush(avc, acred, 1);
-    osi_dnlc_purgedp(avc);
     if (avc->linkData && !(avc->f.states & CCore)) {
        afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
        avc->linkData = NULL;
@@ -258,26 +240,31 @@ afs_TransitionToCaching(struct vcache *avc,
 
 done:
     ReleaseWriteLock(&avc->lock);
-#ifdef AFS_BOZONLOCK_ENV
-    afs_BozonUnlock(&avc->pvnLock, avc);
-#else
     AFS_GUNLOCK();
-#endif
 }
 
 /* In the case where there's an error in afs_NoCacheFetchProc or
  * afs_PrefetchNoCache, all of the pages they've been passed need
  * to be unlocked.
  */
+#ifdef UKERNEL
+typedef void * bypass_page_t;
+
+#define unlock_and_release_pages(auio)
+#define release_full_page(pp, pageoff)
+
+#else
+typedef struct page * bypass_page_t;
+
 #define unlock_and_release_pages(auio) \
     do { \
        struct iovec *ciov;     \
-       struct page *pp; \
+       bypass_page_t pp; \
        afs_int32 iovmax; \
        afs_int32 iovno = 0; \
        ciov = auio->uio_iov; \
        iovmax = auio->uio_iovcnt - 1;  \
-       pp = (struct page*) ciov->iov_base;     \
+       pp = (bypass_page_t) ciov->iov_base;    \
        while(1) { \
            if (pp) { \
                if (PageLocked(pp)) \
@@ -288,15 +275,59 @@ done:
            if(iovno > iovmax) \
                break; \
            ciov = (auio->uio_iov + iovno);     \
-           pp = (struct page*) ciov->iov_base; \
+           pp = (bypass_page_t) ciov->iov_base;        \
        } \
     } while(0)
 
+#define release_full_page(pp, pageoff)                 \
+    do { \
+       /* this is appropriate when no caller intends to unlock \
+        * and release the page */ \
+       SetPageUptodate(pp); \
+       if(PageLocked(pp)) \
+           unlock_page(pp); \
+       else \
+           afs_warn("afs_NoCacheFetchProc: page not locked!\n"); \
+       put_page(pp); /* decrement refcount */ \
+    } while(0)
+#endif
+
+static void
+afs_bypass_copy_page(bypass_page_t pp, int pageoff, struct iovec *rxiov,
+       int iovno, int iovoff, struct uio *auio, int curiov, int partial)
+{
+    char *address;
+    int dolen;
+
+    if (partial)
+       dolen = auio->uio_iov[curiov].iov_len - pageoff;
+    else
+       dolen = rxiov[iovno].iov_len - iovoff;
+
+#if !defined(UKERNEL)
+# if defined(KMAP_ATOMIC_TAKES_NO_KM_TYPE)
+    address = kmap_atomic(pp);
+# else
+    address = kmap_atomic(pp, KM_USER0);
+# endif
+#else
+    address = pp;
+#endif
+    memcpy(address + pageoff, (char *)(rxiov[iovno].iov_base) + iovoff, dolen);
+#if !defined(UKERNEL)
+# if defined(KMAP_ATOMIC_TAKES_NO_KM_TYPE)
+    kunmap_atomic(address);
+# else
+    kunmap_atomic(address, KM_USER0);
+# endif
+#endif
+}
+
 /* no-cache prefetch routine */
 static afs_int32
 afs_NoCacheFetchProc(struct rx_call *acall,
                      struct vcache *avc,
-                                        uio_t *auio,
+                    struct uio *auio,
                      afs_int32 release_pages,
                     afs_int32 size)
 {
@@ -305,16 +336,15 @@ afs_NoCacheFetchProc(struct rx_call *acall,
     int moredata, iovno, iovoff, iovmax, result, locked;
     struct iovec *ciov;
     struct iovec *rxiov;
-    int nio;
-    struct page *pp;
-    char *address;
+    int nio = 0;
+    bypass_page_t pp;
 
     int curpage, bytes;
     int pageoff;
 
     rxiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
     ciov = auio->uio_iov;
-    pp = (struct page*) ciov->iov_base;
+    pp = (bypass_page_t) ciov->iov_base;
     iovmax = auio->uio_iovcnt - 1;
     iovno = iovoff = result = 0;
 
@@ -323,7 +353,7 @@ afs_NoCacheFetchProc(struct rx_call *acall,
        code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
        COND_RE_GLOCK(locked);
        if (code != sizeof(afs_int32)) {
-           result = 0;
+           result = EIO;
            afs_warn("Preread error. code: %d instead of %d\n",
                code, (int)sizeof(afs_int32));
            unlock_and_release_pages(auio);
@@ -364,7 +394,8 @@ afs_NoCacheFetchProc(struct rx_call *acall,
 
        for (curpage = 0; curpage <= iovmax; curpage++) {
            pageoff = 0;
-           while (pageoff < 4096) {
+           /* properly, this should track uio_resid, not a fixed page size! */
+           while (pageoff < auio->uio_iov[curpage].iov_len) {
                /* If no more iovs, issue new read. */
                if (iovno >= nio) {
                    COND_GUNLOCK(locked);
@@ -372,53 +403,43 @@ afs_NoCacheFetchProc(struct rx_call *acall,
                    COND_RE_GLOCK(locked);
                    if (bytes < 0) {
                        afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", bytes);
-                       result = 0;
+                       result = bytes;
                        unlock_and_release_pages(auio);
                        goto done;
                    } else if (bytes == 0) {
-                       result = 0;
+                       /* we failed to read the full length */
+                       result = EIO;
                        afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
                        unlock_and_release_pages(auio);
                        goto done;
                    }
-                   length -= bytes;
+                   size -= bytes;
+                   auio->uio_resid -= bytes;
                    iovno = 0;
                }
-               pp = (struct page *)auio->uio_iov[curpage].iov_base;
-               if (pageoff + (rxiov[iovno].iov_len - iovoff) <= PAGE_CACHE_SIZE) {
+               pp = (bypass_page_t)auio->uio_iov[curpage].iov_base;
+               if (pageoff + (rxiov[iovno].iov_len - iovoff) <= auio->uio_iov[curpage].iov_len) {
                    /* Copy entire (or rest of) current iovec into current page */
-                   if (pp) {
-                       address = kmap_atomic(pp, KM_USER0);
-                       memcpy(address + pageoff, rxiov[iovno].iov_base + iovoff,
-                               rxiov[iovno].iov_len - iovoff);
-                       kunmap_atomic(address, KM_USER0);
-                   }
+                   if (pp)
+                       afs_bypass_copy_page(pp, pageoff, rxiov, iovno, iovoff, auio, curpage, 0);
+                   length -= (rxiov[iovno].iov_len - iovoff);
                    pageoff += rxiov[iovno].iov_len - iovoff;
                    iovno++;
                    iovoff = 0;
                } else {
                    /* Copy only what's needed to fill current page */
-                   if (pp) {
-                       address = kmap_atomic(pp, KM_USER0);
-                       memcpy(address + pageoff, rxiov[iovno].iov_base + iovoff,
-                               PAGE_CACHE_SIZE - pageoff);
-                       kunmap_atomic(address, KM_USER0);
-                   }
-                   iovoff += PAGE_CACHE_SIZE - pageoff;
-                   pageoff = PAGE_CACHE_SIZE;
+                   if (pp)
+                       afs_bypass_copy_page(pp, pageoff, rxiov, iovno, iovoff, auio, curpage, 1);
+                   length -= (auio->uio_iov[curpage].iov_len - pageoff);
+                   iovoff += auio->uio_iov[curpage].iov_len - pageoff;
+                   pageoff = auio->uio_iov[curpage].iov_len;
                }
+
                /* we filled a page, or this is the last page.  conditionally release it */
-               if (pp && ((pageoff == PAGE_CACHE_SIZE && release_pages)
-                               || (length == 0 && iovno >= nio))) {
-                   /* this is appropriate when no caller intends to unlock
-                    * and release the page */
-                    SetPageUptodate(pp);
-                    if(PageLocked(pp))
-                        unlock_page(pp);
-                    else
-                        afs_warn("afs_NoCacheFetchProc: page not locked!\n");
-                    put_page(pp); /* decrement refcount */
-               }
+               if (pp && ((pageoff == auio->uio_iov[curpage].iov_len &&
+                           release_pages) || (length == 0 && iovno >= nio)))
+                   release_full_page(pp, pageoff);
+
                if (length == 0 && iovno >= nio)
                    goto done;
            }
@@ -440,28 +461,29 @@ afs_ReadNoCache(struct vcache *avc,
     afs_int32 code;
     afs_int32 bcnt;
     struct brequest *breq;
-    struct vrequest *areq;
-
-    /* the reciever will free this */
-    areq = osi_Alloc(sizeof(struct vrequest));
+    struct vrequest *areq = NULL;
 
-    if (avc && avc->vc_error) {
+    if (avc->vc_error) {
        code = EIO;
        afs_warn("afs_ReadNoCache VCache Error!\n");
        goto cleanup;
     }
-    if ((code = afs_InitReq(areq, acred))) {
-       afs_warn("afs_ReadNoCache afs_InitReq error!\n");
-       goto cleanup;
-    }
 
     AFS_GLOCK();
-    code = afs_VerifyVCache(avc, areq);
+    /* the receiver will free areq */
+    code = afs_CreateReq(&areq, acred);
+    if (code) {
+       afs_warn("afs_ReadNoCache afs_CreateReq error!\n");
+    } else {
+       code = afs_VerifyVCache(avc, areq);
+       if (code) {
+           afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
+       }
+    }
     AFS_GUNLOCK();
 
     if (code) {
        code = afs_CheckCode(code, areq, 11);   /* failed to get it */
-       afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
        goto cleanup;
     }
 
@@ -494,10 +516,12 @@ cleanup:
      * processed, like unlocking the pages and freeing memory.
      */
     unlock_and_release_pages(bparms->auio);
-    osi_Free(areq, sizeof(struct vrequest));
+    AFS_GLOCK();
+    afs_DestroyReq(areq);
+    AFS_GUNLOCK();
     osi_Free(bparms->auio->uio_iov,
             bparms->auio->uio_iovcnt * sizeof(struct iovec));
-    osi_Free(bparms->auio, sizeof(uio_t));
+    osi_Free(bparms->auio, sizeof(struct uio));
     osi_Free(bparms, sizeof(struct nocache_read_request));
     return code;
 }
@@ -509,16 +533,18 @@ afs_PrefetchNoCache(struct vcache *avc,
                    afs_ucred_t *acred,
                    struct nocache_read_request *bparms)
 {
-    uio_t *auio;
+    struct uio *auio;
+#ifndef UKERNEL
     struct iovec *iovecp;
+#endif
     struct vrequest *areq;
     afs_int32 code = 0;
+    struct rx_connection *rxconn;
 #ifdef AFS_64BIT_CLIENT
     afs_int32 length_hi, bytes, locked;
 #endif
 
     struct afs_conn *tc;
-    afs_int32 i;
     struct rx_call *tcall;
     struct tlocal1 {
        struct AFSVolSync tsync;
@@ -529,15 +555,16 @@ afs_PrefetchNoCache(struct vcache *avc,
 
     auio = bparms->auio;
     areq = bparms->areq;
+#ifndef UKERNEL
     iovecp = auio->uio_iov;
+#endif
 
-    tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
+    tcallspec = osi_Alloc(sizeof(struct tlocal1));
     do {
-       tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */);
+       tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */, &rxconn);
        if (tc) {
-           avc->callback = tc->srvr->server;
-           i = osi_Time();
-           tcall = rx_NewCall(tc->id);
+           avc->callback = tc->parent->srvr->server;
+           tcall = rx_NewCall(rxconn);
 #ifdef AFS_64BIT_CLIENT
            if (!afs_serverHasNo64Bit(tc)) {
                code = StartRXAFS_FetchData64(tcall,
@@ -552,9 +579,8 @@ afs_PrefetchNoCache(struct vcache *avc,
 
                    if (bytes != sizeof(afs_int32)) {
                        length_hi = 0;
-                       code = rx_Error(tcall);
                        COND_GUNLOCK(locked);
-                       code = rx_EndCall(tcall, code);
+                       code = rx_EndCall(tcall, RX_PROTOCOL_ERROR);
                        COND_RE_GLOCK(locked);
                        tcall = NULL;
                    }
@@ -568,7 +594,7 @@ afs_PrefetchNoCache(struct vcache *avc,
                    pos = auio->uio_offset;
                    COND_GUNLOCK(locked);
                    if (!tcall)
-                       tcall = rx_NewCall(tc->id);
+                       tcall = rx_NewCall(rxconn);
                    code = StartRXAFS_FetchData(tcall,
                                        (struct AFSFid *) &avc->f.fid.Fid,
                                        pos, bparms->length);
@@ -588,6 +614,7 @@ afs_PrefetchNoCache(struct vcache *avc,
            } else {
                afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
                unlock_and_release_pages(auio);
+               afs_PutConn(tc, rxconn, SHARED_LOCK);
                goto done;
            }
            if (code == 0) {
@@ -604,7 +631,7 @@ afs_PrefetchNoCache(struct vcache *avc,
            unlock_and_release_pages(auio);
            goto done;
        }
-    } while (afs_Analyze(tc, code, &avc->f.fid, areq,
+    } while (afs_Analyze(tc, rxconn, code, &avc->f.fid, areq,
                                                 AFS_STATS_FS_RPCIDX_FETCHDATA,
                                                 SHARED_LOCK,0));
 done:
@@ -612,14 +639,17 @@ done:
      * Copy appropriate fields into vcache
      */
 
-    afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
+    if (!code)
+       afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
 
-    osi_Free(areq, sizeof(struct vrequest));
+    afs_DestroyReq(areq);
     osi_Free(tcallspec, sizeof(struct tlocal1));
-    osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));
     osi_Free(bparms, sizeof(struct nocache_read_request));
-    osi_Free(auio, sizeof(uio_t));
+#ifndef UKERNEL
+    /* in UKERNEL, the "pages" are passed in */
+    osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));
+    osi_Free(auio, sizeof(struct uio));
+#endif
     return code;
 }
-
-#endif /* AFS_CACHE_BYPASS && AFS_LINUX24_ENV */
+#endif