afs: Handle easy GetValidDSlot errors
[openafs.git] / src / afs / afs_segments.c
index e358acd..9d89d51 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -36,14 +36,15 @@ afs_uint32 afs_stampValue = 0;
  */
 
 static int
-afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
+afs_StoreMini(struct vcache *avc, struct vrequest *areq)
 {
-    register struct afs_conn *tc;
+    struct afs_conn *tc;
     struct AFSStoreStatus InStatus;
     struct AFSFetchStatus OutStatus;
     struct AFSVolSync tsync;
-    register afs_int32 code, code2;
-    register struct rx_call *tcall;
+    afs_int32 code;
+    struct rx_call *tcall;
+    struct rx_connection *rxconn;
     afs_size_t tlen, xlen = 0;
     XSTATS_DECLS;
     AFS_STATCNT(afs_StoreMini);
@@ -56,13 +57,13 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
     avc->f.states &= ~CExtendedFile;
 
     do {
-       tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
+       tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
        if (tc) {
 #ifdef AFS_64BIT_CLIENT
          retry:
 #endif
            RX_AFS_GUNLOCK();
-           tcall = rx_NewCall(tc->id);
+           tcall = rx_NewCall(rxconn);
            RX_AFS_GLOCK();
            /* Set the client mod time since we always want the file
             * to have the client's mod time and not the server's one
@@ -95,8 +96,8 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
                if ((avc->f.m.Length > 0x7fffffff) ||
                    (tlen > 0x7fffffff) ||
                    ((0x7fffffff - tlen) < avc->f.m.Length)) {
-                   RX_AFS_GLOCK();
-                   return EFBIG;
+                   code = EFBIG;
+                   goto error;
                }
                code =
                    StartRXAFS_StoreData(tcall,
@@ -111,9 +112,10 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
            if (code == 0) {
                code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
            }
-           code2 = rx_EndCall(tcall, code);
-           if (code2 && !code)
-               code = code2;
+#ifdef AFS_64BIT_CLIENT
+       error:
+#endif
+           code = rx_EndCall(tcall, code);
            RX_AFS_GLOCK();
            XSTATS_END_TIME;
 #ifdef AFS_64BIT_CLIENT
@@ -125,7 +127,7 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
        } else
            code = -1;
     } while (afs_Analyze
-            (tc, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STOREDATA,
+            (tc, rxconn, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STOREDATA,
              SHARED_LOCK, NULL));
 
     if (code == 0)
@@ -156,18 +158,17 @@ int afs_dvhack = 0;
 
 
 int
-afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
+afs_StoreAllSegments(struct vcache *avc, struct vrequest *areq,
                     int sync)
 {
-    register struct dcache *tdc;
-    register afs_int32 code = 0;
-    register afs_int32 index;
-    register afs_int32 origCBs, foreign = 0;
+    struct dcache *tdc;
+    afs_int32 code = 0;
+    afs_int32 index;
+    afs_int32 origCBs, foreign = 0;
     int hash;
     afs_hyper_t newDV, oldDV;  /* DV when we start, and finish, respectively */
     struct dcache **dcList;
     unsigned int i, j, minj, moredata, high, off;
-    afs_size_t tlen;
     afs_size_t maxStoredLength;        /* highest offset we've written to server. */
     int safety, marineronce = 0;
 
@@ -177,7 +178,7 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
     hset(newDV, avc->f.m.DataVersion);
     hash = DVHash(&avc->f.fid);
     foreign = (avc->f.states & CForeign);
-    dcList = (struct dcache **)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
+    dcList = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
     afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
               ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
 #if !defined(AFS_AIX32_ENV) && !defined(AFS_SGI65_ENV)
@@ -185,7 +186,9 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
      * on the memcache case since that's we adjust the file's size
      * and finish flushing partial vm pages.
      */
-    if ((cacheDiskType != AFS_FCACHE_TYPE_MEM) || (sync & AFS_LASTSTORE))
+    if ((cacheDiskType != AFS_FCACHE_TYPE_MEM) ||
+       (sync & AFS_VMSYNC_INVAL) || (sync & AFS_VMSYNC) ||
+       (sync & AFS_LASTSTORE))
 #endif /* !AFS_AIX32_ENV && !AFS_SGI65_ENV */
     {
        /* If we're not diskless, reading a file may stress the VM
@@ -222,19 +225,18 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
      * - Have to get a write lock on xdcache because GetDSlot might need it (if
      *   the chunk doesn't have a dcache struct).
      *   This seems like overkill in most cases.
-     * - I'm not sure that it's safe to do "index = .hvNextp", then unlock 
+     * - I'm not sure that it's safe to do "index = .hvNextp", then unlock
      *   xdcache, then relock xdcache and try to use index.  It is done
      *   a lot elsewhere in the CM, but I'm not buying that argument.
      * - should be able to check IFDataMod without doing the GetDSlot (just
      *   hold afs_xdcache).  That way, it's easy to do this without the
      *   writelock on afs_xdcache, and we save unneccessary disk
-     *   operations. I don't think that works, 'cuz the next pointers 
+     *   operations. I don't think that works, 'cuz the next pointers
      *   are still on disk.
      */
     origCBs = afs_allCBs;
 
     maxStoredLength = 0;
-    tlen = avc->f.m.Length;
     minj = 0;
 
     do {
@@ -242,7 +244,7 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
        high = 0;
        moredata = FALSE;
 
-       /* lock and start over from beginning of hash chain 
+       /* lock and start over from beginning of hash chain
         * in order to avoid a race condition. */
        ObtainWriteLock(&afs_xdcache, 284);
        index = afs_dvhashTbl[hash];
@@ -250,7 +252,12 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
        for (j = 0; index != NULLIDX;) {
            if ((afs_indexFlags[index] & IFDataMod)
                && (afs_indexUnique[index] == avc->f.fid.Fid.Unique)) {
-               tdc = afs_GetDSlot(index, 0);   /* refcount+1. */
+               tdc = afs_GetValidDSlot(index); /* refcount+1. */
+               if (!tdc) {
+                   ReleaseWriteLock(&afs_xdcache);
+                   code = EIO;
+                   goto done;
+               }
                ReleaseReadLock(&tdc->tlock);
                if (!FidCmp(&tdc->f.fid, &avc->f.fid) && tdc->f.chunk >= minj) {
                    off = tdc->f.chunk - minj;
@@ -313,6 +320,7 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
        minj += NCHUNKSATONCE;
     } while (!code && moredata);
 
+ done:
     UpgradeSToWLock(&avc->lock, 29);
 
     /* send a trivial truncation store if did nothing else */
@@ -321,7 +329,7 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
         * Call StoreMini if we haven't written enough data to extend the
         * file at the fileserver to the client's notion of the file length.
         */
-       if ((avc->f.truncPos != AFS_NOTRUNC) 
+       if ((avc->f.truncPos != AFS_NOTRUNC)
            || ((avc->f.states & CExtendedFile)
                && (maxStoredLength < avc->f.m.Length))) {
            code = afs_StoreMini(avc, areq);
@@ -334,7 +342,7 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
     /*
      * Finally, turn off DWriting, turn on DFEntryMod,
      * update f.versionNo.
-     * A lot of this could be integrated into the loop above 
+     * A lot of this could be integrated into the loop above
      */
     if (!code) {
        afs_hyper_t h_unset;
@@ -351,10 +359,22 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
            ObtainWriteLock(&afs_xdcache, 285);
 
            for (j = 0, safety = 0, index = afs_dvhashTbl[hash];
-                index != NULLIDX && safety < afs_cacheFiles + 2;) {
+                index != NULLIDX && safety < afs_cacheFiles + 2;
+                index = afs_dvnextTbl[index]) {
 
                if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
-                   tdc = afs_GetDSlot(index, 0);
+                   tdc = afs_GetValidDSlot(index);
+                   if (!tdc) {
+                       /* This is okay; since manipulating the dcaches at this
+                        * point is best-effort. We only get a dcache here to
+                        * increment the dv and turn off DWriting. If we were
+                        * supposed to do that for a dcache, but could not
+                        * due to an I/O error, it just means the dv won't
+                        * be updated so we don't be able to use that cached
+                        * chunk in the future. That's inefficient, but not
+                        * an error. */
+                       continue;
+                   }
                    ReleaseReadLock(&tdc->tlock);
 
                    if (!FidCmp(&tdc->f.fid, &avc->f.fid)
@@ -376,8 +396,6 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
                        afs_PutDCache(tdc);
                    }
                }
-
-               index = afs_dvnextTbl[index];
            }
            ReleaseWriteLock(&afs_xdcache);
 
@@ -409,6 +427,9 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
                        UpgradeSToWLock(&tdc->lock, 678);
                        hset(tdc->f.versionNo, avc->f.m.DataVersion);
                        tdc->dflags |= DFEntryMod;
+                       /* DWriting may not have gotten cleared above, if all
+                        * we did was a StoreMini */
+                       tdc->f.states &= ~DWriting;
                        ConvertWToSLock(&tdc->lock);
                    }
                }
@@ -518,7 +539,8 @@ afs_InvalidateAllSegments(struct vcache *avc)
 
     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
        if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) osi_Panic("afs_InvalidateAllSegments tdc count");
            ReleaseReadLock(&tdc->tlock);
            if (!FidCmp(&tdc->f.fid, &avc->f.fid))
                dcListMax++;
@@ -532,7 +554,8 @@ afs_InvalidateAllSegments(struct vcache *avc)
 
     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
        if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) osi_Panic("afs_InvalidateAllSegments tdc store");
            ReleaseReadLock(&tdc->tlock);
            if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
                /* same file? we'll zap it */
@@ -570,8 +593,8 @@ afs_InvalidateAllSegments(struct vcache *avc)
     return 0;
 }
 
-/*! 
- * 
+/*!
+ *
  * Extend a cache file
  *
  * \param avc pointer to vcache to extend data for
@@ -589,7 +612,7 @@ afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq)
     struct dcache *tdc;
     void *zeros;
 
-    zeros = (void *) afs_osi_Alloc(AFS_PAGESIZE);
+    zeros = afs_osi_Alloc(AFS_PAGESIZE);
     if (zeros == NULL)
        return ENOMEM;
     memset(zeros, 0, AFS_PAGESIZE);
@@ -614,8 +637,8 @@ afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq)
             towrite = (avc->f.m.Length + toAdd) - tdc->validPos;
             if (towrite > AFS_PAGESIZE) towrite = AFS_PAGESIZE;
 
-            code = afs_CFileWrite(tfile, 
-                                  tdc->validPos - AFS_CHUNKTOBASE(tdc->f.chunk), 
+            code = afs_CFileWrite(tfile,
+                                  tdc->validPos - AFS_CHUNKTOBASE(tdc->f.chunk),
                                   zeros, towrite);
             tdc->validPos += towrite;
        }
@@ -646,16 +669,16 @@ afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq)
  *     held.
  */
 int
-afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
+afs_TruncateAllSegments(struct vcache *avc, afs_size_t alen,
                        struct vrequest *areq, afs_ucred_t *acred)
 {
-    register struct dcache *tdc;
-    register afs_int32 code;
-    register afs_int32 index;
+    struct dcache *tdc;
+    afs_int32 code;
+    afs_int32 index;
     afs_size_t newSize;
 
     int dcCount, dcPos;
-    struct dcache **tdcArray;
+    struct dcache **tdcArray = NULL;
 
     AFS_STATCNT(afs_TruncateAllSegments);
     avc->f.m.Date = osi_Time();
@@ -710,7 +733,12 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
     dcCount = 0;
     for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
        if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) {
+               ReleaseWriteLock(&afs_xdcache);
+               code = EIO;
+               goto done;
+           }
            ReleaseReadLock(&tdc->tlock);
            if (!FidCmp(&tdc->f.fid, &avc->f.fid))
                dcCount++;
@@ -726,9 +754,21 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
     tdcArray = osi_Alloc(dcCount * sizeof(struct dcache *));
     dcPos = 0;
 
-    for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
+    for (index = afs_dvhashTbl[code]; index != NULLIDX; index = afs_dvnextTbl[index]) {
        if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) {
+               /* make sure we put back all of the tdcArray members before
+                * bailing out */
+               /* remember, the last valid tdc is at dcPos-1, so start at
+                * dcPos-1, not at dcPos itself. */
+               for (dcPos = dcPos - 1; dcPos >= 0; dcPos--) {
+                   tdc = tdcArray[dcPos];
+                   afs_PutDCache(tdc);
+               }
+               code = EIO;
+               goto done;
+           }
            ReleaseReadLock(&tdc->tlock);
            if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
                /* same file, and modified, we'll store it back */
@@ -741,7 +781,6 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
                afs_PutDCache(tdc);
            }
        }
-       index = afs_dvnextTbl[index];
     }
 
     ReleaseWriteLock(&afs_xdcache);
@@ -758,6 +797,7 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
        ObtainSharedLock(&tdc->lock, 672);
        if (newSize < tdc->f.chunkBytes && newSize < MAX_AFS_UINT32) {
            UpgradeSToWLock(&tdc->lock, 673);
+           tdc->f.states |= DWriting;
            tfile = afs_CFileOpen(&tdc->f.inode);
            afs_CFileTruncate(tfile, (afs_int32)newSize);
            afs_CFileClose(tfile);
@@ -774,8 +814,12 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
        afs_PutDCache(tdc);
     }
 
-    osi_Free(tdcArray, dcCount * sizeof(struct dcache *));
+    code = 0;
 
+ done:
+    if (tdcArray) {
+       osi_Free(tdcArray, dcCount * sizeof(struct dcache *));
+    }
 #if    (defined(AFS_SUN5_ENV))
     ObtainWriteLock(&avc->vlock, 547);
     if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
@@ -785,5 +829,5 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
     ReleaseWriteLock(&avc->vlock);
 #endif
 
-    return 0;
+    return code;
 }