OPENAFS-SA-2016-002 AFSStoreStatus information leak
[openafs.git] / src / afs / afs_segments.c
index 6d107bc..69f7445 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
@@ -13,8 +13,6 @@
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
 #include "afs/sysincludes.h"   /*Standard vendor system headers */
 #include "afsincludes.h"       /*AFS-based standard headers */
@@ -37,32 +35,36 @@ afs_uint32 afs_stampValue = 0;
  *     We're write-locked upon entry.
  */
 
-int
-afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
+static int
+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;
-    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);
     afs_Trace2(afs_iclSetp, CM_TRACE_STOREMINI, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_INT32, avc->m.Length);
-    tlen = avc->m.Length;
-    if (avc->truncPos < tlen)
-       tlen = avc->truncPos;
-    avc->truncPos = AFS_NOTRUNC;
-    avc->states &= ~CExtendedFile;
+              ICL_TYPE_INT32, avc->f.m.Length);
+    tlen = avc->f.m.Length;
+    if (avc->f.truncPos < tlen)
+       tlen = avc->f.truncPos;
+    avc->f.truncPos = AFS_NOTRUNC;
+    avc->f.states &= ~CExtendedFile;
+    memset(&InStatus, 0, sizeof(InStatus));
 
     do {
-       tc = afs_Conn(&avc->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
@@ -73,11 +75,11 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
             * the proper store-data or store-status calls.
             */
            InStatus.Mask = AFS_SETMODTIME;
-           InStatus.ClientModTime = avc->m.Date;
+           InStatus.ClientModTime = avc->f.m.Date;
            XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
            afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64, ICL_TYPE_FID,
-                      &avc->fid.Fid, ICL_TYPE_OFFSET,
-                      ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_OFFSET,
+                      &avc->f.fid.Fid, ICL_TYPE_OFFSET,
+                      ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
                       ICL_HANDLE_OFFSET(xlen), ICL_TYPE_OFFSET,
                       ICL_HANDLE_OFFSET(tlen));
            RX_AFS_GUNLOCK();
@@ -85,30 +87,35 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
            if (!afs_serverHasNo64Bit(tc)) {
                code =
                    StartRXAFS_StoreData64(tcall,
-                                          (struct AFSFid *)&avc->fid.Fid,
-                                          &InStatus, avc->m.Length,
+                                          (struct AFSFid *)&avc->f.fid.Fid,
+                                          &InStatus, avc->f.m.Length,
                                           (afs_size_t) 0, tlen);
            } else {
                afs_int32 l1, l2;
-               l1 = avc->m.Length;
+               l1 = avc->f.m.Length;
                l2 = tlen;
-               if ((avc->m.Length > 0x7fffffff) ||
+               if ((avc->f.m.Length > 0x7fffffff) ||
                    (tlen > 0x7fffffff) ||
-                   ((0x7fffffff - tlen) < avc->m.Length))
-                   return EFBIG;
+                   ((0x7fffffff - tlen) < avc->f.m.Length)) {
+                   code = EFBIG;
+                   goto error;
+               }
                code =
                    StartRXAFS_StoreData(tcall,
-                                        (struct AFSFid *)&avc->fid.Fid,
+                                        (struct AFSFid *)&avc->f.fid.Fid,
                                         &InStatus, l1, 0, l2);
            }
 #else /* AFS_64BIT_CLIENT */
            code =
-               StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->fid.Fid,
-                                    &InStatus, avc->m.Length, 0, tlen);
+               StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
+                                    &InStatus, avc->f.m.Length, 0, tlen);
 #endif /* AFS_64BIT_CLIENT */
            if (code == 0) {
                code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
            }
+#ifdef AFS_64BIT_CLIENT
+       error:
+#endif
            code = rx_EndCall(tcall, code);
            RX_AFS_GLOCK();
            XSTATS_END_TIME;
@@ -121,21 +128,15 @@ afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
        } else
            code = -1;
     } while (afs_Analyze
-            (tc, code, &avc->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) {
+    if (code == 0)
        afs_ProcessFS(avc, &OutStatus, areq);
-    } else {
-       /* blew it away */
-       afs_InvalidateAllSegments(avc);
-    }
-    return code;
 
+    return code;
 }                              /*afs_StoreMini */
 
-unsigned int storeallmissing = 0;
-#define lmin(a,b) (((a) < (b)) ? (a) : (b))
 /*
  * afs_StoreAllSegments
  *
@@ -158,44 +159,35 @@ 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;
-    int hash, stored;
+    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, **dclist;
+    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;
-#ifndef AFS_NOSTATS
-    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;    /* # bytes to xfer */
-    afs_size_t bytesXferred;   /* # bytes actually xferred */
-#endif /* AFS_NOSTATS */
-
+    int safety, marineronce = 0;
 
     AFS_STATCNT(afs_StoreAllSegments);
 
-    hset(oldDV, avc->m.DataVersion);
-    hset(newDV, avc->m.DataVersion);
-    hash = DVHash(&avc->fid);
-    foreign = (avc->states & CForeign);
-    dcList = (struct dcache **)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
+    hash = DVHash(&avc->f.fid);
+    foreign = (avc->f.states & CForeign);
+    dcList = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
     afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
 #if !defined(AFS_AIX32_ENV) && !defined(AFS_SGI65_ENV)
     /* In the aix vm implementation we need to do the vm_writep even
      * 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)
+    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
@@ -220,6 +212,14 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
        /*printf("Net down in afs_StoreSegments\n");*/
        return ENETDOWN;
     }
+
+    /*
+     * Can't do this earlier because osi_VM_StoreAllSegments drops locks
+     * and can indirectly do some stores that increase the DV.
+     */
+    hset(oldDV, avc->f.m.DataVersion);
+    hset(newDV, avc->f.m.DataVersion);
+
     ConvertWToSLock(&avc->lock);
 
     /*
@@ -232,41 +232,50 @@ 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->m.Length;
     minj = 0;
 
     do {
-       memset((char *)dcList, 0, NCHUNKSATONCE * sizeof(struct dcache *));
+       memset(dcList, 0, NCHUNKSATONCE * sizeof(struct dcache *));
        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. */
-       MObtainWriteLock(&afs_xdcache, 284);
+       ObtainWriteLock(&afs_xdcache, 284);
        index = afs_dvhashTbl[hash];
 
        for (j = 0; index != NULLIDX;) {
            if ((afs_indexFlags[index] & IFDataMod)
-               && (afs_indexUnique[index] == avc->fid.Fid.Unique)) {
-               tdc = afs_GetDSlot(index, 0);   /* refcount+1. */
+               && (afs_indexUnique[index] == avc->f.fid.Fid.Unique)) {
+               tdc = afs_GetValidDSlot(index); /* refcount+1. */
+               if (!tdc) {
+                   ReleaseWriteLock(&afs_xdcache);
+                   code = EIO;
+                   goto done;
+               }
                ReleaseReadLock(&tdc->tlock);
-               if (!FidCmp(&tdc->f.fid, &avc->fid) && tdc->f.chunk >= minj) {
+               if (!FidCmp(&tdc->f.fid, &avc->f.fid) && tdc->f.chunk >= minj) {
                    off = tdc->f.chunk - minj;
                    if (off < NCHUNKSATONCE) {
                        if (dcList[off])
                            osi_Panic("dclist slot already in use!");
+                       if (afs_mariner && !marineronce) {
+                           /* first chunk only */
+                           afs_MarinerLog("store$Storing", avc);
+                           marineronce++;
+                       }
                        dcList[off] = tdc;
                        if (off > high)
                            high = off;
@@ -292,359 +301,16 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
            }
            index = afs_dvnextTbl[index];
        }
-       MReleaseWriteLock(&afs_xdcache);
+       ReleaseWriteLock(&afs_xdcache);
 
        /* this guy writes chunks, puts back dcache structs, and bumps newDV */
        /* "moredata" just says "there are more dirty chunks yet to come".
         */
        if (j) {
-#ifdef AFS_NOSTATS
-           static afs_uint32 lp1 = 10000, lp2 = 10000;
-#endif
-           struct AFSStoreStatus InStatus;
-           struct AFSFetchStatus OutStatus;
-           int doProcessFS = 0;
-           afs_size_t base, bytes;
-           afs_uint32 nchunks;
-           int nomore;
-           unsigned int first = 0;
-           int *shouldwake;
-           struct afs_conn *tc;
-           struct osi_file *tfile;
-           struct rx_call *tcall;
-           XSTATS_DECLS;
-           for (bytes = 0, j = 0; !code && j <= high; j++) {
-               if (dcList[j]) {
-                   ObtainSharedLock(&(dcList[j]->lock), 629);
-                   if (!bytes)
-                       first = j;
-                   bytes += dcList[j]->f.chunkBytes;
-                   if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
-                       && (dcList[j]->f.chunk - minj < high)
-                       && dcList[j + 1]) {
-                       int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
-                       bytes += sbytes;
-                   }
-               }
-               if (bytes && (j == high || !dcList[j + 1])) {
-                   /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
-                   base = AFS_CHUNKTOBASE(first + minj);
-                   /*
-                    * 
-                    * take a list of dcache structs and send them all off to the server
-                    * the list must be in order, and the chunks contiguous.
-                    * Note - there is no locking done by this code currently.  For
-                    * safety's sake, xdcache could be locked over the entire call.
-                    * However, that pretty well ties up all the threads.  Meantime, all
-                    * the chunks _MUST_ have their refcounts bumped.
-                    * The writes done before a store back will clear setuid-ness
-                    * in cache file.
-                    * We can permit CacheStoreProc to wake up the user process IFF we 
-                    * are doing the last RPC for this close, ie, storing back the last 
-                    * set of contiguous chunks of a file.
-                    */
-
-                   dclist = &dcList[first];
-                   nchunks = 1 + j - first;
-                   nomore = !(moredata || (j != high));
-                   InStatus.ClientModTime = avc->m.Date;
-                   InStatus.Mask = AFS_SETMODTIME;
-                   if (sync & AFS_SYNC) {
-                       InStatus.Mask |= AFS_FSYNC;
-                   }
-                   tlen = lmin(avc->m.Length, avc->truncPos);
-                   afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
-                              ICL_TYPE_FID, &avc->fid.Fid, ICL_TYPE_OFFSET,
-                              ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
-                              ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
-                              ICL_HANDLE_OFFSET(tlen));
-
-                   do {
-                       stored = 0;
-                       tc = afs_Conn(&avc->fid, areq, 0);
-                       if (tc) {
-                         restart:
-                           RX_AFS_GUNLOCK();
-                           tcall = rx_NewCall(tc->id);
-#ifdef AFS_64BIT_CLIENT
-                           if (!afs_serverHasNo64Bit(tc)) {
-                               code =
-                                   StartRXAFS_StoreData64(tcall,
-                                                          (struct AFSFid *)
-                                                          &avc->fid.Fid,
-                                                          &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->fid.Fid,
-                                                            &InStatus, t1,
-                                                            t2, t3);
-                               }
-                           }
-#else /* AFS_64BIT_CLIENT */
-                           code =
-                               StartRXAFS_StoreData(tcall,
-                                                    (struct AFSFid *)&avc->
-                                                    fid.Fid, &InStatus, base,
-                                                    bytes, tlen);
-#endif /* AFS_64BIT_CLIENT */
-                           RX_AFS_GLOCK();
-                       } else {
-                           code = -1;
-                           tcall = NULL;
-                       }
-                       if (!code) {
-                           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
-                           avc->truncPos = AFS_NOTRUNC;
-                       }
-                       for (i = 0; i < nchunks && !code; i++) {
-                           tdc = dclist[i];
-                           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,
-                                      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;
-                               }
-                           }
-#if defined(LINUX_USE_FH)
-                           tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
-#else
-                           tfile = afs_CFileOpen(tdc->f.inode);
-#endif
-#ifndef AFS_NOSTATS
-                           xferP =
-                               &(afs_stats_cmfullperf.rpc.
-                                 fsXferTimes
-                                 [AFS_STATS_FS_XFERIDX_STOREDATA]);
-                           osi_GetuTime(&xferStartTime);
-
-                           code =
-                               afs_CacheStoreProc(tcall, tfile,
-                                                  tdc->f.chunkBytes, avc,
-                                                  shouldwake, &bytesToXfer,
-                                                  &bytesXferred);
-
-                           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);
-                               }
-                               if (afs_stats_TimeGreaterThan
-                                   (elapsedTime, (xferP->maxTime))) {
-                                   afs_stats_TimeAssign((xferP->maxTime),
-                                                        elapsedTime);
-                               }
-                           }
-#else
-                           code =
-                               afs_CacheStoreProc(tcall, tfile,
-                                                  tdc->f.chunkBytes, avc,
-                                                  shouldwake, &lp1, &lp2);
-#endif /* AFS_NOSTATS */
-                           afs_CFileClose(tfile);
-                           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(tcall, 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) {
-                           struct AFSVolSync tsync;
-                           RX_AFS_GUNLOCK();
-                           code =
-                               EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
-                           RX_AFS_GLOCK();
-                           hadd32(newDV, 1);
-                           XSTATS_END_TIME;
-                           if (!code)
-                               doProcessFS = 1;        /* Flag to run afs_ProcessFS() later on */
-                       }
-                       if (tcall) {
-                           afs_int32 code2;
-                           RX_AFS_GUNLOCK();
-                           code2 = rx_EndCall(tcall, code);
-                           RX_AFS_GLOCK();
-                           if (code2)
-                               code = code2;
-                       }
-#ifdef AFS_64BIT_CLIENT
-                       if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
-                           afs_serverSetNo64Bit(tc);
-                           goto restart;
-                       }
-#endif /* AFS_64BIT_CLIENT */
-                   } while (afs_Analyze
-                            (tc, code, &avc->fid, areq,
-                             AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
-                             NULL));
-
-                   /* put back all remaining locked dcache entries */
-                   for (i = 0; i < nchunks; i++) {
-                       tdc = dclist[i];
-                       if (!code) {
-                           if (afs_indexFlags[tdc->index] & IFDataMod) {
-                               /*
-                                * LOCKXXX -- should hold afs_xdcache(W) when
-                                * modifying afs_indexFlags.
-                                */
-                               afs_indexFlags[tdc->index] &= ~IFDataMod;
-                               afs_stats_cmperf.cacheCurrDirtyChunks--;
-                               afs_indexFlags[tdc->index] &= ~IFDirtyPages;
-                               if (sync & AFS_VMSYNC_INVAL) {
-                                   /* since we have invalidated all the pages of this
-                                    ** vnode by calling osi_VM_TryToSmush, we can
-                                    ** safely mark this dcache entry as not having
-                                    ** any pages. This vnode now becomes eligible for
-                                    ** reclamation by getDownD.
-                                    */
-                                   afs_indexFlags[tdc->index] &= ~IFAnyPages;
-                               }
-                           }
-                       }
-                       UpgradeSToWLock(&tdc->lock, 628);
-                       tdc->f.states &= ~DWriting;     /* correct? */
-                       tdc->dflags |= DFEntryMod;
-                       ReleaseWriteLock(&tdc->lock);
-                       afs_PutDCache(tdc);
-                       /* Mark the entry as released */
-                       dclist[i] = NULL;
-                   }
-
-                   if (doProcessFS) {
-                       /* Now copy out return params */
-                       UpgradeSToWLock(&avc->lock, 28);        /* keep out others for a while */
-                       afs_ProcessFS(avc, &OutStatus, areq);
-                       /* Keep last (max) size of file on server to see if
-                        * we need to call afs_StoreMini to extend the file.
-                        */
-                       if (!moredata)
-                           maxStoredLength = OutStatus.Length;
-                       ConvertWToSLock(&avc->lock);
-                       doProcessFS = 0;
-                   }
-
-                   if (code) {
-                       for (j++; j <= high; j++) {
-                           if (dcList[j]) {
-                               ReleaseSharedLock(&(dcList[j]->lock));
-                               afs_PutDCache(dcList[j]);
-                               /* Releasing entry */
-                               dcList[j] = NULL;
-                           }
-                       }
-                   }
-
-                   afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
-                              ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
-                   bytes = 0;
-               }
-           }
-
+           code =
+               afs_CacheStoreVCache(dcList, avc, areq, sync,
+                                  minj, high, moredata,
+                                  &newDV, &maxStoredLength);
            /* Release any zero-length dcache entries in our interval
             * that we locked but didn't store back above.
             */
@@ -661,6 +327,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 */
@@ -669,20 +336,20 @@ 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->truncPos != AFS_NOTRUNC) || ((avc->states & CExtendedFile)
-                                              && (maxStoredLength <
-                                                  avc->m.Length))) {
+       if ((avc->f.truncPos != AFS_NOTRUNC)
+           || ((avc->f.states & CExtendedFile)
+               && (maxStoredLength < avc->f.m.Length))) {
            code = afs_StoreMini(avc, areq);
            if (code == 0)
                hadd32(newDV, 1);       /* just bumped here, too */
        }
-       avc->states &= ~CExtendedFile;
+       avc->f.states &= ~CExtendedFile;
     }
 
     /*
      * 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;
@@ -692,20 +359,32 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
 
        do {
            moredata = FALSE;
-           memset((char *)dcList, 0,
+           memset(dcList, 0,
                   NCHUNKSATONCE * sizeof(struct dcache *));
 
            /* overkill, but it gets the lock in case GetDSlot needs it */
-           MObtainWriteLock(&afs_xdcache, 285);
+           ObtainWriteLock(&afs_xdcache, 285);
 
            for (j = 0, safety = 0, index = afs_dvhashTbl[hash];
-                index != NULLIDX && safety < afs_cacheFiles + 2;) {
-
-               if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
-                   tdc = afs_GetDSlot(index, 0);
+                index != NULLIDX && safety < afs_cacheFiles + 2;
+                index = afs_dvnextTbl[index]) {
+
+               if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
+                   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->fid)
+                   if (!FidCmp(&tdc->f.fid, &avc->f.fid)
                        && tdc->f.chunk >= minj) {
                        off = tdc->f.chunk - minj;
                        if (off < NCHUNKSATONCE) {
@@ -724,10 +403,8 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
                        afs_PutDCache(tdc);
                    }
                }
-
-               index = afs_dvnextTbl[index];
            }
-           MReleaseWriteLock(&afs_xdcache);
+           ReleaseWriteLock(&afs_xdcache);
 
            for (i = 0; i < j; i++) {
                /* Iterate over the dcache entries we collected above */
@@ -749,14 +426,17 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
                    && hcmp(tdc->f.versionNo, oldDV) >= 0) {
 
                    if ((!(afs_dvhack || foreign)
-                        && hsame(avc->m.DataVersion, newDV))
+                        && hsame(avc->f.m.DataVersion, newDV))
                        || ((afs_dvhack || foreign)
                            && (origCBs == afs_allCBs))) {
                        /* no error, this is the DV */
 
                        UpgradeSToWLock(&tdc->lock, 678);
-                       hset(tdc->f.versionNo, avc->m.DataVersion);
+                       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);
                    }
                }
@@ -777,12 +457,12 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
         * invalidated. Also discard data if it's a permanent error from the
         * fileserver.
         */
-       if (areq->permWriteError || (avc->states & (CCore1 | CCore))) {
+       if (areq->permWriteError || (avc->f.states & CCore)) {
            afs_InvalidateAllSegments(avc);
        }
     }
     afs_Trace3(afs_iclSetp, CM_TRACE_STOREALLDONE, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_INT32, avc->m.Length, ICL_TYPE_INT32, code);
+              ICL_TYPE_INT32, avc->f.m.Length, ICL_TYPE_INT32, code);
     /* would like a Trace5, but it doesn't exist... */
     afs_Trace3(afs_iclSetp, CM_TRACE_AVCLOCKER, ICL_TYPE_POINTER, avc,
               ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
@@ -800,10 +480,10 @@ afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
      * Turn off CDirty bit because the stored data is now in sync with server.
      */
     if (code == 0 && hcmp(avc->mapDV, oldDV) >= 0) {
-       if ((!(afs_dvhack || foreign) && hsame(avc->m.DataVersion, newDV))
+       if ((!(afs_dvhack || foreign) && hsame(avc->f.m.DataVersion, newDV))
            || ((afs_dvhack || foreign) && (origCBs == afs_allCBs))) {
            hset(avc->mapDV, newDV);
-           avc->states &= ~CDirty;
+           avc->f.states &= ~CDirty;
        }
     }
     osi_FreeLargeSpace(dcList);
@@ -842,33 +522,46 @@ afs_InvalidateAllSegments(struct vcache *avc)
 
     AFS_STATCNT(afs_InvalidateAllSegments);
     afs_Trace2(afs_iclSetp, CM_TRACE_INVALL, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
-    hash = DVHash(&avc->fid);
-    avc->truncPos = AFS_NOTRUNC;       /* don't truncate later */
-    avc->states &= ~CExtendedFile;     /* not any more */
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
+    hash = DVHash(&avc->f.fid);
+    avc->f.truncPos = AFS_NOTRUNC;     /* don't truncate later */
+    avc->f.states &= ~CExtendedFile;   /* not any more */
     ObtainWriteLock(&afs_xcbhash, 459);
     afs_DequeueCallback(avc);
-    avc->states &= ~(CStatd | CDirty); /* mark status information as bad, too */
+    avc->f.states &= ~(CStatd | CDirty);       /* mark status information as bad, too */
     ReleaseWriteLock(&afs_xcbhash);
-    if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
+    if (avc->f.fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
        osi_dnlc_purgedp(avc);
     /* Blow away pages; for now, only for Solaris */
 #if    (defined(AFS_SUN5_ENV))
     if (WriteLocked(&avc->lock))
-       osi_ReleaseVM(avc, (struct AFS_UCRED *)0);
+       osi_ReleaseVM(avc, (afs_ucred_t *)0);
 #endif
     /*
      * Block out others from screwing with this table; is a read lock
      * sufficient?
      */
-    MObtainWriteLock(&afs_xdcache, 286);
+    ObtainWriteLock(&afs_xdcache, 286);
     dcListMax = 0;
 
     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
-       if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+       if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) {
+               /* In the case of fatal errors during stores, we MUST
+                * invalidate all of the relevant chunks. Otherwise, the chunks
+                * will be left with the 'new' data that was never successfully
+                * written to the server, but the DV in the dcache is still the
+                * old DV. So, we may indefinitely serve data to applications
+                * that is not actually in the file on the fileserver. If we
+                * cannot afs_GetValidDSlot the appropriate entries, currently
+                * there is no way to ensure the dcache is invalidated. So for
+                * now, to avoid risking serving bad data from the cache, panic
+                * instead. */
+               osi_Panic("afs_InvalidateAllSegments tdc count");
+           }
            ReleaseReadLock(&tdc->tlock);
-           if (!FidCmp(&tdc->f.fid, &avc->fid))
+           if (!FidCmp(&tdc->f.fid, &avc->f.fid))
                dcListMax++;
            afs_PutDCache(tdc);
        }
@@ -879,10 +572,17 @@ afs_InvalidateAllSegments(struct vcache *avc)
     dcListCount = 0;
 
     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
-       if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+       if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) {
+               /* We cannot proceed after getting this error; we risk serving
+                * incorrect data to applications. So panic instead. See the
+                * above comment next to the previous afs_GetValidDSlot call
+                * for details. */
+               osi_Panic("afs_InvalidateAllSegments tdc store");
+           }
            ReleaseReadLock(&tdc->tlock);
-           if (!FidCmp(&tdc->f.fid, &avc->fid)) {
+           if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
                /* same file? we'll zap it */
                if (afs_indexFlags[index] & IFDataMod) {
                    afs_stats_cmperf.cacheCurrDirtyChunks--;
@@ -900,7 +600,7 @@ afs_InvalidateAllSegments(struct vcache *avc)
        }
        index = afs_dvnextTbl[index];
     }
-    MReleaseWriteLock(&afs_xdcache);
+    ReleaseWriteLock(&afs_xdcache);
 
     for (i = 0; i < dcListCount; i++) {
        tdc = dcList[i];
@@ -918,8 +618,8 @@ afs_InvalidateAllSegments(struct vcache *avc)
     return 0;
 }
 
-/*! 
- * 
+/*!
+ *
  * Extend a cache file
  *
  * \param avc pointer to vcache to extend data for
@@ -929,50 +629,47 @@ afs_InvalidateAllSegments(struct vcache *avc)
  * \note avc must be write locked. May release and reobtain avc and GLOCK
  */
 int
-afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq) {
+afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq)
+{
     afs_size_t offset, toAdd;
     struct osi_file *tfile;
     afs_int32 code = 0;
     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);
 
-    while (avc->m.Length < alen) {
-        tdc = afs_ObtainDCacheForWriting(avc, avc->m.Length, alen - avc->m.Length, areq, 0);
+    while (avc->f.m.Length < alen) {
+        tdc = afs_ObtainDCacheForWriting(avc, avc->f.m.Length, alen - avc->f.m.Length, areq, 0);
         if (!tdc) {
            code = EIO;
            break;
         }
 
-       toAdd = alen - avc->m.Length;
+       toAdd = alen - avc->f.m.Length;
 
-        offset = avc->m.Length - AFS_CHUNKTOBASE(tdc->f.chunk);
+        offset = avc->f.m.Length - AFS_CHUNKTOBASE(tdc->f.chunk);
        if (offset + toAdd > AFS_CHUNKTOSIZE(tdc->f.chunk)) {
            toAdd = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset;
        }
-#if defined(LINUX_USE_FH)
-        tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
-#else
-        tfile = afs_CFileOpen(tdc->f.inode);
-#endif
-       while(tdc->validPos < avc->m.Length + toAdd) {
+        tfile = afs_CFileOpen(&tdc->f.inode);
+       while(tdc->validPos < avc->f.m.Length + toAdd) {
             afs_size_t towrite;
 
-            towrite = (avc->m.Length + toAdd) - tdc->validPos;
+            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;
        }
        afs_CFileClose(tfile);
        afs_AdjustSize(tdc, offset + toAdd );
-       avc->m.Length += toAdd;
+       avc->f.m.Length += toAdd;
        ReleaseWriteLock(&tdc->lock);
        afs_PutDCache(tdc);
     }
@@ -997,23 +694,23 @@ afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq) {
  *     held.
  */
 int
-afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
-                       struct vrequest *areq, struct AFS_UCRED *acred)
+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;
-    afs_int32 newSize;
+    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->m.Date = osi_Time();
+    avc->f.m.Date = osi_Time();
     afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL, ICL_TYPE_POINTER, avc,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
               ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(alen));
-    if (alen >= avc->m.Length) {
+    if (alen >= avc->f.m.Length) {
        /*
         * Special speedup since Sun's vm extends the file this way;
         * we've never written to the file thus we can just set the new
@@ -1023,8 +720,8 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
         * should keep the ExtendedPos as well and clear this flag if we
         * truncate below that value before we store the file back.
         */
-       avc->states |= CExtendedFile;
-       avc->m.Length = alen;
+       avc->f.states |= CExtendedFile;
+       avc->f.m.Length = alen;
        return 0;
     }
 #if    (defined(AFS_SUN5_ENV))
@@ -1049,21 +746,26 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
     AFS_GLOCK();
     ObtainWriteLock(&avc->lock, 79);
 
-    avc->m.Length = alen;
+    avc->f.m.Length = alen;
 
-    if (alen < avc->truncPos)
-       avc->truncPos = alen;
-    code = DVHash(&avc->fid);
+    if (alen < avc->f.truncPos)
+       avc->f.truncPos = alen;
+    code = DVHash(&avc->f.fid);
 
     /* block out others from screwing with this table */
-    MObtainWriteLock(&afs_xdcache, 287);
+    ObtainWriteLock(&afs_xdcache, 287);
 
     dcCount = 0;
     for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
-       if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+       if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
+           tdc = afs_GetValidDSlot(index);
+           if (!tdc) {
+               ReleaseWriteLock(&afs_xdcache);
+               code = EIO;
+               goto done;
+           }
            ReleaseReadLock(&tdc->tlock);
-           if (!FidCmp(&tdc->f.fid, &avc->fid))
+           if (!FidCmp(&tdc->f.fid, &avc->f.fid))
                dcCount++;
            afs_PutDCache(tdc);
        }
@@ -1077,11 +779,23 @@ 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;) {
-       if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
-           tdc = afs_GetDSlot(index, 0);
+    for (index = afs_dvhashTbl[code]; index != NULLIDX; index = afs_dvnextTbl[index]) {
+       if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
+           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->fid)) {
+           if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
                /* same file, and modified, we'll store it back */
                if (dcPos < dcCount) {
                    tdcArray[dcPos++] = tdc;
@@ -1092,10 +806,9 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
                afs_PutDCache(tdc);
            }
        }
-       index = afs_dvnextTbl[index];
     }
 
-    MReleaseWriteLock(&afs_xdcache);
+    ReleaseWriteLock(&afs_xdcache);
 
     /* Now we loop over the array of dcache entries and truncate them */
     for (index = 0; index < dcPos; index++) {
@@ -1107,16 +820,13 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
        if (newSize < 0)
            newSize = 0;
        ObtainSharedLock(&tdc->lock, 672);
-       if (newSize < tdc->f.chunkBytes) {
+       if (newSize < tdc->f.chunkBytes && newSize < MAX_AFS_UINT32) {
            UpgradeSToWLock(&tdc->lock, 673);
-#if defined(LINUX_USE_FH)
-           tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
-#else
-           tfile = afs_CFileOpen(tdc->f.inode);
-#endif
-           afs_CFileTruncate(tfile, newSize);
+           tdc->f.states |= DWriting;
+           tfile = afs_CFileOpen(&tdc->f.inode);
+           afs_CFileTruncate(tfile, (afs_int32)newSize);
            afs_CFileClose(tfile);
-           afs_AdjustSize(tdc, newSize);
+           afs_AdjustSize(tdc, (afs_int32)newSize);
            if (alen < tdc->validPos) {
                 if (alen < AFS_CHUNKTOBASE(tdc->f.chunk))
                     tdc->validPos = 0;
@@ -1129,8 +839,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)) {
@@ -1139,5 +853,6 @@ afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
     }
     ReleaseWriteLock(&avc->vlock);
 #endif
-    return 0;
+
+    return code;
 }