afs: Ensure CDirty is set during afs_write loop
[openafs.git] / src / afs / VNOPS / afs_vnop_write.c
index 9d582f0..c720f6a 100644 (file)
@@ -9,8 +9,8 @@
 
 /*
  * Implements:
- * afs_UFSWrite
- * afs_MemWrite
+ * afs_write
+ * afs_UFSWriteUIO
  * afs_StoreOnLastReference
  * afs_close
  * afs_fsync
@@ -47,6 +47,8 @@ afs_StoreOnLastReference(struct vcache *avc,
      * flag will already be clear, so we don't have to worry about
      * clearing it twice. */
     if (avc->f.states & CCore) {
+       afs_ucred_t *cred;
+
        avc->f.states &= ~CCore;
 #if defined(AFS_SGI_ENV)
        osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
@@ -58,7 +60,8 @@ afs_StoreOnLastReference(struct vcache *avc,
        avc->opens--;
        avc->execsOrWriters--;
        AFS_RELE(AFSTOV(avc));  /* VN_HOLD at set CCore(afs_FakeClose) */
-       crfree((afs_ucred_t *)avc->linkData);   /* "crheld" in afs_FakeClose */
+       cred = (afs_ucred_t *)avc->linkData;    /* "crheld" in afs_FakeClose */
+       crfree(cred);
        avc->linkData = NULL;
     }
 
@@ -95,223 +98,109 @@ afs_StoreOnLastReference(struct vcache *avc,
 }
 
 int
-afs_MemWrite(struct vcache *avc, struct uio *auio, int aio,
-            afs_ucred_t *acred, int noLock)
+afs_UFSWriteUIO(struct vcache *avc, afs_dcache_id_t *inode, struct uio *tuiop)
 {
-    afs_size_t totalLength;
-    afs_size_t transferLength;
-    afs_size_t filePos;
-    afs_size_t offset, len;
-    afs_int32 tlen, trimlen;
-    afs_int32 startDate;
-    afs_int32 max;
-    struct dcache *tdc;
-#ifdef _HIGHC_
-    volatile
-#endif
-    afs_int32 error;
-#if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
-    struct vnode *vp = AFSTOV(avc);
-#endif
-#ifdef AFS_DARWIN80_ENV
-    uio_t tuiop = NULL;
-#else
-    struct uio tuio;
-    struct uio *tuiop = &tuio;
-    struct iovec *tvec;                /* again, should have define */
-#endif
-    afs_int32 code;
-    struct vrequest treq;
+    struct osi_file *tfile;
+    int code;
 
-    AFS_STATCNT(afs_MemWrite);
-    if (avc->vc_error)
-       return avc->vc_error;
+    tfile = (struct osi_file *)osi_UFSOpen(inode);
+    if (!tfile)
+       return EIO;
 
-    startDate = osi_Time();
-    if ((code = afs_InitReq(&treq, acred)))
-       return code;
-    /* otherwise we read */
-    totalLength = AFS_UIO_RESID(auio);
-    filePos = AFS_UIO_OFFSET(auio);
-    error = 0;
-    transferLength = 0;
-    afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
-              ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
-              ICL_HANDLE_OFFSET(avc->f.m.Length));
-    if (!noLock) {
-       afs_MaybeWakeupTruncateDaemon();
-       ObtainWriteLock(&avc->lock, 126);
-    }
-#if defined(AFS_SGI_ENV)
+#if defined(AFS_AIX41_ENV)
+    AFS_GUNLOCK();
+    code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL,
+                    NULL, afs_osi_credp);
+    AFS_GLOCK();
+#elif defined(AFS_AIX32_ENV)
+    code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL);
+#elif defined(AFS_AIX_ENV)
+    code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t) &offset,
+                    tuiop, NULL, NULL, -1);
+#elif defined(AFS_SUN5_ENV)
+    AFS_GUNLOCK();
+# ifdef AFS_SUN510_ENV
+    VOP_RWLOCK(tfile->vnode, 1, NULL);
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp, NULL);
+    VOP_RWUNLOCK(tfile->vnode, 1, NULL);
+# else
+    VOP_RWLOCK(tfile->vnode, 1);
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
+    VOP_RWUNLOCK(tfile->vnode, 1);
+# endif
+    AFS_GLOCK();
+    if (code == ENOSPC)
+        afs_WarnENOSPC();
+#elif defined(AFS_SGI_ENV)
+    AFS_GUNLOCK();
+    avc->f.states |= CWritingUFS;
+    AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
+    AFS_VOP_WRITE(tfile->vnode, tuiop, IO_ISLOCKED, afs_osi_credp, code);
+    AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
+    avc->f.states &= ~CWritingUFS;
+    AFS_GLOCK();
+#elif defined(AFS_HPUX100_ENV)
     {
-       off_t diff;
-       /*
-        * afs_xwrite handles setting m.Length
-        * and handles APPEND mode.
-        * Since we are called via strategy, we need to trim the write to
-        * the actual size of the file
-        */
-       osi_Assert(filePos <= avc->f.m.Length);
-       diff = avc->f.m.Length - filePos;
-       AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
-       totalLength = AFS_UIO_RESID(auio);
-    }
-#else
-    if (aio & IO_APPEND) {
-       /* append mode, start it at the right spot */
-#if    defined(AFS_SUN56_ENV)
-       auio->uio_loffset = 0;
-#endif
-       filePos = avc->f.m.Length;
-       AFS_UIO_SETOFFSET(auio, filePos);
-    }
-#endif
-    /*
-     * Note that we use startDate rather than calling osi_Time() here.
-     * This is to avoid counting lock-waiting time in file date (for ranlib).
-     */
-    avc->f.m.Date = startDate;
-
-#if    defined(AFS_HPUX_ENV)
-#if    defined(AFS_HPUX101_ENV)
-    if ((totalLength + filePos) >> 9 >
-       (p_rlimit(u.u_procp))[RLIMIT_FSIZE].rlim_cur) {
-#else
-    if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
-#endif
-       if (!noLock)
-           ReleaseWriteLock(&avc->lock);
-       return (EFBIG);
-    }
-#endif
-#ifdef AFS_VM_RDWR_ENV
-    /*
-     * If write is implemented via VM, afs_FakeOpen() is called from the
-     * high-level write op.
-     */
-    if (avc->execsOrWriters <= 0) {
-       afs_warn("WARNING: afs_ufswr vp=%lx, exOrW=%d\n", (unsigned long)avc,
-              avc->execsOrWriters);
+       AFS_GUNLOCK();
+       code = VOP_RDWR(tfile->vnode, tuiop, UIO_WRITE, 0, afs_osi_credp);
+       AFS_GLOCK();
     }
+#elif defined(AFS_LINUX20_ENV)
+    AFS_GUNLOCK();
+    code = osi_rdwr(tfile, tuiop, UIO_WRITE);
+    AFS_GLOCK();
+#elif defined(AFS_DARWIN80_ENV)
+    AFS_GUNLOCK();
+    code = VNOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
+    AFS_GLOCK();
+#elif defined(AFS_DARWIN_ENV)
+    AFS_GUNLOCK();
+    VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
+    VOP_UNLOCK(tfile->vnode, 0, current_proc());
+    AFS_GLOCK();
+#elif defined(AFS_FBSD80_ENV)
+    AFS_GUNLOCK();
+    VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
+    VOP_UNLOCK(tfile->vnode, 0);
+    AFS_GLOCK();
+#elif defined(AFS_FBSD_ENV)
+    AFS_GUNLOCK();
+    VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
+    VOP_UNLOCK(tfile->vnode, 0, curthread);
+    AFS_GLOCK();
+#elif defined(AFS_NBSD_ENV)
+    AFS_GUNLOCK();
+    VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
+#if defined(AFS_NBSD60_ENV)
+    VOP_UNLOCK(tfile->vnode);
 #else
-    afs_FakeOpen(avc);
+    VOP_UNLOCK(tfile->vnode, 0);
 #endif
-    avc->f.states |= CDirty;
-#ifndef AFS_DARWIN80_ENV
-    tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
-#endif
-    while (totalLength > 0) {
-       tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq, 
-                                        noLock);
-       if (!tdc) {
-           error = EIO;
-           break;
-       }
-
-       len = totalLength;      /* write this amount by default */
-       offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
-       max = AFS_CHUNKTOSIZE(tdc->f.chunk);    /* max size of this chunk */
-       if (max <= len + offset) {      /*if we'd go past the end of this chunk */
-           /* it won't all fit in this chunk, so write as much
-            * as will fit */
-           len = max - offset;
-       }
-
-#ifdef  AFS_DARWIN80_ENV
-        if (tuiop)
-           uio_free(tuiop);
-       trimlen = len;
-       tuiop = afsio_darwin_partialcopy(auio, trimlen);
+    AFS_GLOCK();
+#elif defined(AFS_XBSD_ENV)
+    AFS_GUNLOCK();
+    VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
+    code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
+    VOP_UNLOCK(tfile->vnode, 0, curproc);
+    AFS_GLOCK();
 #else
-       /* mung uio structure to be right for this transfer */
-       afsio_copy(auio, &tuio, tvec);
-       trimlen = len;
-       afsio_trim(&tuio, trimlen);
+# ifdef        AFS_HPUX_ENV
+    tuio.uio_fpflags &= ~FSYNCIO;      /* don't do sync io */
+# endif
+    code = VOP_RDWR(tfile->vnode, tuiop, UIO_WRITE, 0, afs_osi_credp);
 #endif
-       AFS_UIO_SETOFFSET(tuiop, offset);
+    osi_UFSClose(tfile);
 
-       code = afs_MemWriteUIO(&tdc->f.inode, tuiop);
-       if (code) {
-           void *mep;          /* XXX in prototype world is struct memCacheEntry * */
-           error = code;
-           ZapDCE(tdc);        /* bad data */
-           mep = afs_MemCacheOpen(&tdc->f.inode);
-           afs_MemCacheTruncate(mep, 0);
-           afs_MemCacheClose(mep);
-           afs_stats_cmperf.cacheCurrDirtyChunks--;
-           afs_indexFlags[tdc->index] &= ~IFDataMod;   /* so it does disappear */
-           ReleaseWriteLock(&tdc->lock);
-           afs_PutDCache(tdc);
-           break;
-       }
-       /* otherwise we've written some, fixup length, etc and continue with next seg */
-       len = len - AFS_UIO_RESID(tuiop);       /* compute amount really transferred */
-       tlen = len;
-       afsio_skip(auio, tlen); /* advance auio over data written */
-       /* compute new file size */
-       if (offset + len > tdc->f.chunkBytes) {
-           afs_int32 tlength = offset + len;
-           afs_AdjustSize(tdc, tlength);
-           if (tdc->validPos < filePos + len)
-               tdc->validPos = filePos + len;
-       }
-       totalLength -= len;
-       transferLength += len;
-       filePos += len;
-#if defined(AFS_SGI_ENV)
-       /* afs_xwrite handles setting m.Length */
-       osi_Assert(filePos <= avc->f.m.Length);
-#else
-       if (filePos > avc->f.m.Length) {
-           if (AFS_IS_DISCON_RW)
-               afs_PopulateDCache(avc, filePos, &treq);
-           afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
-                      __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
-                      ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
-                      ICL_HANDLE_OFFSET(filePos));
-           avc->f.m.Length = filePos;
-#if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
-            vnode_pager_setsize(vp, filePos);
-#endif
-       }
-#endif
-       ReleaseWriteLock(&tdc->lock);
-       afs_PutDCache(tdc);
-#if !defined(AFS_VM_RDWR_ENV)
-       /*
-        * If write is implemented via VM, afs_DoPartialWrite() is called from
-        * the high-level write op.
-        */
-       if (!noLock) {
-           code = afs_DoPartialWrite(avc, &treq);
-           if (code) {
-               error = code;
-               break;
-           }
-       }
-#endif
-    }
-#ifndef        AFS_VM_RDWR_ENV
-    afs_FakeClose(avc, acred);
-#endif
-    if (error && !avc->vc_error)
-       avc->vc_error = error;
-    if (!noLock)
-       ReleaseWriteLock(&avc->lock);
-#ifdef AFS_DARWIN80_ENV
-    uio_free(tuiop);
-#else
-    osi_FreeSmallSpace(tvec);
-#endif
-    error = afs_CheckCode(error, &treq, 6);
-    return error;
+    return code;
 }
 
-
 /* called on writes */
 int
-afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
+afs_write(struct vcache *avc, struct uio *auio, int aio,
             afs_ucred_t *acred, int noLock)
 {
     afs_size_t totalLength;
@@ -330,18 +219,12 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
     struct vnode *vp = AFSTOV(avc);
 #endif
-#ifdef AFS_DARWIN80_ENV
-    uio_t tuiop = NULL;
-#else
-    struct uio tuio;
-    struct uio *tuiop = &tuio;
-    struct iovec *tvec;                /* again, should have define */
-#endif
-    struct osi_file *tfile;
+    struct uio *tuiop = NULL;
     afs_int32 code;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
+
+    AFS_STATCNT(afs_write);
 
-    AFS_STATCNT(afs_UFSWrite);
     if (avc->vc_error)
        return avc->vc_error;
 
@@ -349,7 +232,7 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
        return ENETDOWN;
     
     startDate = osi_Time();
-    if ((code = afs_InitReq(&treq, acred)))
+    if ((code = afs_CreateReq(&treq, acred)))
        return code;
     /* otherwise we read */
     totalLength = AFS_UIO_RESID(auio);
@@ -381,7 +264,7 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
 #else
     if (aio & IO_APPEND) {
        /* append mode, start it at the right spot */
-#if     defined(AFS_SUN56_ENV)
+#if     defined(AFS_SUN5_ENV)
        auio->uio_loffset = 0;
 #endif
        filePos = avc->f.m.Length;
@@ -403,10 +286,11 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
 #endif
        if (!noLock)
            ReleaseWriteLock(&avc->lock);
+       afs_DestroyReq(treq);
        return (EFBIG);
     }
 #endif
-#ifdef AFS_VM_RDWR_ENV
+#if defined(AFS_VM_RDWR_ENV) && !defined(AFS_FAKEOPEN_ENV)
     /*
      * If write is implemented via VM, afs_FakeOpen() is called from the
      * high-level write op.
@@ -418,18 +302,22 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
 #else
     afs_FakeOpen(avc);
 #endif
-    avc->f.states |= CDirty;
-#ifndef AFS_DARWIN80_ENV
-    tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
-#endif
+
     while (totalLength > 0) {
-       tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq, 
+       /*
+        * Note that we must set CDirty for every iteration of this loop.
+        * CDirty may get cleared below (such as during afs_DoPartialStore),
+        * but we're still writing to the file, so make sure CDirty is set
+        * here.
+        */
+       avc->f.states |= CDirty;
+
+       tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, treq,
                                         noLock);
        if (!tdc) {
            error = EIO;
            break;
        }
-       tfile = (struct osi_file *)osi_UFSOpen(&tdc->f.inode);
        len = totalLength;      /* write this amount by default */
        offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
        max = AFS_CHUNKTOSIZE(tdc->f.chunk);    /* max size of this chunk */
@@ -439,116 +327,27 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
            len = max - offset;
        }
 
-#ifdef  AFS_DARWIN80_ENV
        if (tuiop)
-           uio_free(tuiop);
-       trimlen = len;
-       tuiop = afsio_darwin_partialcopy(auio, trimlen);
-#else
-       /* mung uio structure to be right for this transfer */
-       afsio_copy(auio, &tuio, tvec);
+           afsio_free(tuiop);
        trimlen = len;
-       afsio_trim(&tuio, trimlen);
-#endif
+       tuiop = afsio_partialcopy(auio, trimlen);
        AFS_UIO_SETOFFSET(tuiop, offset);
 
-#if defined(AFS_AIX41_ENV)
-       AFS_GUNLOCK();
-       code =
-           VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL,
-                     NULL, afs_osi_credp);
-       AFS_GLOCK();
-#elif defined(AFS_AIX32_ENV)
-       code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL);
-#elif defined(AFS_AIX_ENV)
-       code =
-           VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t) & offset,
-                     &tuio, NULL, NULL, -1);
-#elif defined(AFS_SUN5_ENV)
-       AFS_GUNLOCK();
-#ifdef AFS_SUN510_ENV
-       {
-           caller_context_t ct;
+        code = (*(afs_cacheType->vwriteUIO))(avc, &tdc->f.inode, tuiop);
 
-           VOP_RWLOCK(tfile->vnode, 1, &ct);
-           code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp, &ct);
-           VOP_RWUNLOCK(tfile->vnode, 1, &ct);
-       }
-#else
-       VOP_RWLOCK(tfile->vnode, 1);
-       code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
-       VOP_RWUNLOCK(tfile->vnode, 1);
-#endif
-       AFS_GLOCK();
-       if (code == ENOSPC)
-           afs_warnuser
-               ("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
-#elif defined(AFS_SGI_ENV)
-       AFS_GUNLOCK();
-       avc->f.states |= CWritingUFS;
-       AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
-       AFS_VOP_WRITE(tfile->vnode, &tuio, IO_ISLOCKED, afs_osi_credp, code);
-       AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
-       avc->f.states &= ~CWritingUFS;
-       AFS_GLOCK();
-#elif defined(AFS_HPUX100_ENV)
-       {
-           AFS_GUNLOCK();
-           code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, afs_osi_credp);
-           AFS_GLOCK();
-       }
-#elif defined(AFS_LINUX20_ENV)
-       AFS_GUNLOCK();
-       code = osi_rdwr(tfile, &tuio, UIO_WRITE);
-       AFS_GLOCK();
-#elif defined(AFS_DARWIN80_ENV)
-       AFS_GUNLOCK();
-       code = VNOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
-       AFS_GLOCK();
-#elif defined(AFS_DARWIN_ENV)
-       AFS_GUNLOCK();
-       VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
-       code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
-       VOP_UNLOCK(tfile->vnode, 0, current_proc());
-       AFS_GLOCK();
-#elif defined(AFS_FBSD80_ENV)
-       AFS_GUNLOCK();
-       VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
-       code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
-       VOP_UNLOCK(tfile->vnode, 0);
-       AFS_GLOCK();
-#elif defined(AFS_FBSD_ENV)
-       AFS_GUNLOCK();
-       VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
-       code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
-       VOP_UNLOCK(tfile->vnode, 0, curthread);
-       AFS_GLOCK();
-#elif defined(AFS_NBSD_ENV)
-       AFS_GUNLOCK();
-       VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
-       code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
-       VOP_UNLOCK(tfile->vnode, 0);
-       AFS_GLOCK();
-#elif defined(AFS_XBSD_ENV)
-       AFS_GUNLOCK();
-       VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
-       code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
-       VOP_UNLOCK(tfile->vnode, 0, curproc);
-       AFS_GLOCK();
-#else
-#ifdef AFS_HPUX_ENV
-       tuio.uio_fpflags &= ~FSYNCIO;   /* don't do sync io */
-#endif
-       code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, afs_osi_credp);
-#endif
        if (code) {
+           void *cfile;
+
            error = code;
            ZapDCE(tdc);        /* bad data */
-           osi_UFSTruncate(tfile, 0);  /* fake truncate the segment */
+           cfile = afs_CFileOpen(&tdc->f.inode);
+            osi_Assert(cfile);
+           afs_CFileTruncate(cfile, 0);
+           afs_CFileClose(cfile);
            afs_AdjustSize(tdc, 0);     /* sets f.chunkSize to 0 */
+
            afs_stats_cmperf.cacheCurrDirtyChunks--;
            afs_indexFlags[tdc->index] &= ~IFDataMod;   /* so it does disappear */
-           afs_CFileClose(tfile);
            ReleaseWriteLock(&tdc->lock);
            afs_PutDCache(tdc);
            break;
@@ -573,7 +372,7 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
 #else
        if (filePos > avc->f.m.Length) {
            if (AFS_IS_DISCON_RW)
-               afs_PopulateDCache(avc, filePos, &treq);
+               afs_PopulateDCache(avc, filePos, treq);
            afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
                       __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
                       ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
@@ -584,7 +383,6 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
 #endif
        }
 #endif
-       osi_UFSClose(tfile);
        ReleaseWriteLock(&tdc->lock);
        afs_PutDCache(tdc);
 #if !defined(AFS_VM_RDWR_ENV)
@@ -593,7 +391,7 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
         * the high-level write op.
         */
        if (!noLock) {
-           code = afs_DoPartialWrite(avc, &treq);
+           code = afs_DoPartialWrite(avc, treq);
            if (code) {
                error = code;
                break;
@@ -601,20 +399,18 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
        }
 #endif
     }
-#ifndef        AFS_VM_RDWR_ENV
+#if !defined(AFS_VM_RDWR_ENV) || defined(AFS_FAKEOPEN_ENV)
     afs_FakeClose(avc, acred);
 #endif
-    error = afs_CheckCode(error, &treq, 7);
+    error = afs_CheckCode(error, treq, 7);
     /* This set is here so we get the CheckCode. */
     if (error && !avc->vc_error)
        avc->vc_error = error;
     if (!noLock)
        ReleaseWriteLock(&avc->lock);
-#ifdef AFS_DARWIN80_ENV
-    uio_free(tuiop);
-#else
-    osi_FreeSmallSpace(tvec);
-#endif
+    if (tuiop)
+       afsio_free(tuiop);
+
 #ifndef        AFS_VM_RDWR_ENV
     /*
      * If write is implemented via VM, afs_fsync() is called from the high-level
@@ -637,6 +433,7 @@ afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
            afs_fsync(avc, acred);
     }
 #endif
+    afs_DestroyReq(treq);
     return error;
 }
 
@@ -681,8 +478,9 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
 #endif
 {
     afs_int32 code;
+    afs_int32 code_checkcode = 0;
     struct brequest *tb;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
 #ifdef AFS_SGI65_ENV
     struct flid flid;
 #endif
@@ -692,25 +490,27 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
     AFS_STATCNT(afs_close);
     afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
               ICL_TYPE_INT32, aflags);
-    code = afs_InitReq(&treq, acred);
+    code = afs_CreateReq(&treq, acred);
     if (code)
        return code;
     afs_InitFakeStat(&fakestat);
-    code = afs_EvalFakeStat(&avc, &fakestat, &treq);
+    code = afs_EvalFakeStat(&avc, &fakestat, treq);
     if (code) {
        afs_PutFakeStat(&fakestat);
+       afs_DestroyReq(treq);
        return code;
     }
     AFS_DISCON_LOCK();
 #ifdef AFS_SUN5_ENV
     if (avc->flockCount) {
-       HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
+       HandleFlock(avc, LOCK_UN, treq, 0, 1 /*onlymine */ );
     }
 #endif
 #if defined(AFS_SGI_ENV)
     if (!lastclose) {
        afs_PutFakeStat(&fakestat);
         AFS_DISCON_UNLOCK();
+       afs_DestroyReq(treq);
        return 0;
     }
     /* unlock any locks for pid - could be wrong for child .. */
@@ -718,14 +518,14 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
 # ifdef AFS_SGI65_ENV
     get_current_flid(&flid);
     cleanlocks((vnode_t *) avc, flid.fl_pid, flid.fl_sysid);
-    HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1 /*onlymine */ );
+    HandleFlock(avc, LOCK_UN, treq, flid.fl_pid, 1 /*onlymine */ );
 # else
 #  ifdef AFS_SGI64_ENV
     cleanlocks((vnode_t *) avc, flp);
 #  else /* AFS_SGI64_ENV */
     cleanlocks((vnode_t *) avc, u.u_procp->p_epid, u.u_procp->p_sysid);
 #  endif /* AFS_SGI64_ENV */
-    HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1 /*onlymine */ );
+    HandleFlock(avc, LOCK_UN, treq, OSI_GET_CURRENT_PID(), 1 /*onlymine */ );
 # endif /* AFS_SGI65_ENV */
     /* afs_chkpgoob will drop and re-acquire the global lock. */
     afs_chkpgoob(&avc->v, btoc(avc->f.m.Length));
@@ -736,18 +536,19 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
         * with the close. */
        afs_PutFakeStat(&fakestat);
        AFS_DISCON_UNLOCK();
+       afs_DestroyReq(treq);
        return 0;
     }
 #else
     if (avc->flockCount) {     /* Release Lock */
-       HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
+       HandleFlock(avc, LOCK_UN, treq, 0, 1 /*onlymine */ );
     }
 #endif
     if (aflags & (FWRITE | FTRUNC)) {
        if (afs_BBusy() || (AFS_NFSXLATORREQ(acred)) || AFS_IS_DISCONNECTED) {
            /* do it yourself if daemons are all busy */
            ObtainWriteLock(&avc->lock, 124);
-           code = afs_StoreOnLastReference(avc, &treq);
+           code = afs_StoreOnLastReference(avc, treq);
            ReleaseWriteLock(&avc->lock);
 #if defined(AFS_SGI_ENV)
            AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
@@ -766,14 +567,15 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
                tb->flags |= BUWAIT;
                afs_osi_Sleep(tb);
            }
-           code = tb->code;
+           code = tb->code_raw;
+           code_checkcode = tb->code_checkcode;
            afs_BRelease(tb);
        }
 
        /* VNOVNODE is "acceptable" error code from close, since
         * may happen when deleting a file on another machine while
-        * it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
-       if (code == VNOVNODE || code == ENOENT)
+        * it is open here. */
+       if (code == VNOVNODE)
            code = 0;
 
        /* Ensure last closer gets the error. If another thread caused
@@ -785,8 +587,13 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
 #ifdef AFS_AIX32_ENV
            osi_ReleaseVM(avc, acred);
 #endif
-           /* printf("avc->vc_error=%d\n", avc->vc_error); */
-           code = avc->vc_error;
+           /* We don't know what the original raw error code was, so set
+            * 'code' to 0. But we have the afs_CheckCode-translated error
+            * code, so put that in code_checkcode. We cannot just set code
+            * to avc->vc_error, since vc_error is a checkcode-translated
+            * error code, and 'code' is supposed to be a raw error code. */
+           code = 0;
+           code_checkcode = avc->vc_error;
            avc->vc_error = 0;
        }
        ReleaseWriteLock(&avc->lock);
@@ -796,19 +603,19 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
            afs_warnuser("afs: failed to store file (network problems)\n");
        }
 #ifdef AFS_SUN5_ENV
-       else if (code == ENOSPC) {
+       else if (code == ENOSPC || code_checkcode == ENOSPC) {
            afs_warnuser
                ("afs: failed to store file (over quota or partition full)\n");
        }
 #else
-       else if (code == ENOSPC) {
+       else if (code == ENOSPC || code_checkcode == ENOSPC) {
            afs_warnuser("afs: failed to store file (partition full)\n");
-       } else if (code == EDQUOT) {
+       } else if (code == EDQUOT || code_checkcode == EDQUOT) {
            afs_warnuser("afs: failed to store file (over quota)\n");
        }
 #endif
-       else if (code != 0)
-           afs_warnuser("afs: failed to store file (%d)\n", code);
+       else if (code || code_checkcode)
+           afs_warnuser("afs: failed to store file (%d/%d)\n", code, code_checkcode);
 
        /* finally, we flush any text pages lying around here */
        hzero(avc->flushDV);
@@ -824,7 +631,8 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
 #ifdef AFS_AIX32_ENV
            osi_ReleaseVM(avc, acred);
 #endif
-           code = avc->vc_error;
+           code = 0;
+           code_checkcode = avc->vc_error;
            avc->vc_error = 0;
        }
 #if defined(AFS_FBSD80_ENV)
@@ -848,24 +656,30 @@ afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
     }
     AFS_DISCON_UNLOCK();
     afs_PutFakeStat(&fakestat);
-    code = afs_CheckCode(code, &treq, 5);
+
+    if (code_checkcode) {
+       code = code_checkcode;
+    } else {
+       code = afs_CheckCode(code, treq, 5);
+    }
+    afs_DestroyReq(treq);
     return code;
 }
 
 
 int
-#if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
+#if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
 afs_fsync(OSI_VC_DECL(avc), int flag, afs_ucred_t *acred
 # ifdef AFS_SGI65_ENV
          , off_t start, off_t stop
 # endif /* AFS_SGI65_ENV */
     )
-#else /* !SUN53 && !SGI */
+#else /* !SUN5 && !SGI */
 afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
 #endif 
 {
     afs_int32 code;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
     OSI_VC_CONVERT(avc);
 
     if (avc->vc_error)
@@ -879,7 +693,7 @@ afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
 
     AFS_STATCNT(afs_fsync);
     afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
-    if ((code = afs_InitReq(&treq, acred)))
+    if ((code = afs_CreateReq(&treq, acred)))
        return code;
     AFS_DISCON_LOCK();
 #if defined(AFS_SGI_ENV)
@@ -896,7 +710,7 @@ afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
            
            /* put the file back */
            UpgradeSToWLock(&avc->lock, 41);
-           code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
+           code = afs_StoreAllSegments(avc, treq, AFS_SYNC);
            ConvertWToSLock(&avc->lock);
        } else {
            UpgradeSToWLock(&avc->lock, 711);
@@ -915,7 +729,8 @@ afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
     }
 #endif
     AFS_DISCON_UNLOCK();
-    code = afs_CheckCode(code, &treq, 33);
+    code = afs_CheckCode(code, treq, 33);
+    afs_DestroyReq(treq);
     ReleaseSharedLock(&avc->lock);
     return code;
 }