afs: Ensure CDirty is set during afs_write loop
[openafs.git] / src / afs / VNOPS / afs_vnop_write.c
index 5ef62aa..c720f6a 100644 (file)
@@ -104,6 +104,9 @@ afs_UFSWriteUIO(struct vcache *avc, afs_dcache_id_t *inode, struct uio *tuiop)
     int code;
 
     tfile = (struct osi_file *)osi_UFSOpen(inode);
+    if (!tfile)
+       return EIO;
+
 #if defined(AFS_AIX41_ENV)
     AFS_GUNLOCK();
     code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL,
@@ -117,13 +120,9 @@ afs_UFSWriteUIO(struct vcache *avc, afs_dcache_id_t *inode, struct uio *tuiop)
 #elif defined(AFS_SUN5_ENV)
     AFS_GUNLOCK();
 # ifdef AFS_SUN510_ENV
-    {
-       caller_context_t ct;
-
-       VOP_RWLOCK(tfile->vnode, 1, &ct);
-       code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp, &ct);
-       VOP_RWUNLOCK(tfile->vnode, 1, &ct);
-    }
+    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);
@@ -131,8 +130,7 @@ afs_UFSWriteUIO(struct vcache *avc, afs_dcache_id_t *inode, struct uio *tuiop)
 # endif
     AFS_GLOCK();
     if (code == ENOSPC)
-       afs_warnuser
-           ("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
+        afs_WarnENOSPC();
 #elif defined(AFS_SGI_ENV)
     AFS_GUNLOCK();
     avc->f.states |= CWritingUFS;
@@ -223,7 +221,7 @@ afs_write(struct vcache *avc, struct uio *auio, int aio,
 #endif
     struct uio *tuiop = NULL;
     afs_int32 code;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
 
     AFS_STATCNT(afs_write);
 
@@ -234,7 +232,7 @@ afs_write(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);
@@ -288,6 +286,7 @@ afs_write(struct vcache *avc, struct uio *auio, int aio,
 #endif
        if (!noLock)
            ReleaseWriteLock(&avc->lock);
+       afs_DestroyReq(treq);
        return (EFBIG);
     }
 #endif
@@ -303,10 +302,17 @@ afs_write(struct vcache *avc, struct uio *auio, int aio,
 #else
     afs_FakeOpen(avc);
 #endif
-    avc->f.states |= CDirty;
 
     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;
@@ -335,6 +341,7 @@ afs_write(struct vcache *avc, struct uio *auio, int aio,
            error = code;
            ZapDCE(tdc);        /* bad data */
            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 */
@@ -365,7 +372,7 @@ afs_write(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,
@@ -384,7 +391,7 @@ afs_write(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;
@@ -395,7 +402,7 @@ afs_write(struct vcache *avc, struct uio *auio, int aio,
 #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;
@@ -426,6 +433,7 @@ afs_write(struct vcache *avc, struct uio *auio, int aio,
            afs_fsync(avc, acred);
     }
 #endif
+    afs_DestroyReq(treq);
     return error;
 }
 
@@ -470,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
@@ -481,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 .. */
@@ -507,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));
@@ -525,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);
@@ -555,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
@@ -574,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);
@@ -585,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);
@@ -613,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)
@@ -637,7 +656,13 @@ 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;
 }
 
@@ -654,7 +679,7 @@ 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)
@@ -668,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)
@@ -685,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);
@@ -704,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;
 }