2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * --------------------- Required definitions ---------------------
13 #include <afsconfig.h>
14 #include "afs/param.h"
19 #include "afs/sysincludes.h" /*Standard vendor system headers */
20 #include "afsincludes.h" /*AFS-based standard headers */
21 #include "afs/afs_stats.h" /* statistics */
22 #include "afs/afs_cbqueue.h"
23 #include "afs/afs_osidnlc.h"
25 afs_uint32 afs_stampValue = 0;
31 * Send a truncation request to a FileServer.
37 * We're write-locked upon entry.
41 afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
43 register struct conn *tc;
44 struct AFSStoreStatus InStatus;
45 struct AFSFetchStatus OutStatus;
46 struct AFSVolSync tsync;
47 register afs_int32 code;
48 register struct rx_call *tcall;
49 afs_size_t tlen, xlen = 0;
51 AFS_STATCNT(afs_StoreMini);
52 afs_Trace2(afs_iclSetp, CM_TRACE_STOREMINI, ICL_TYPE_POINTER, avc,
53 ICL_TYPE_INT32, avc->m.Length);
55 if (avc->truncPos < tlen)
57 avc->truncPos = AFS_NOTRUNC;
58 avc->states &= ~CExtendedFile;
61 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
65 tcall = rx_NewCall(tc->id);
67 /* Set the client mod time since we always want the file
68 * to have the client's mod time and not the server's one
69 * (to avoid problems with make, etc.) It almost always
70 * works fine with standard afs because them server/client
71 * times are in sync and more importantly this storemini
72 * it's a special call that would typically be followed by
73 * the proper store-data or store-status calls.
75 InStatus.Mask = AFS_SETMODTIME;
76 InStatus.ClientModTime = avc->m.Date;
77 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
78 afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64, ICL_TYPE_FID,
79 &avc->fid.Fid, ICL_TYPE_OFFSET,
80 ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_OFFSET,
81 ICL_HANDLE_OFFSET(xlen), ICL_TYPE_OFFSET,
82 ICL_HANDLE_OFFSET(tlen));
84 #ifdef AFS_64BIT_CLIENT
85 if (!afs_serverHasNo64Bit(tc)) {
87 StartRXAFS_StoreData64(tcall,
88 (struct AFSFid *)&avc->fid.Fid,
89 &InStatus, avc->m.Length,
90 (afs_size_t) 0, tlen);
96 StartRXAFS_StoreData(tcall,
97 (struct AFSFid *)&avc->fid.Fid,
98 &InStatus, l1, 0, l2);
100 #else /* AFS_64BIT_CLIENT */
102 StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->fid.Fid,
103 &InStatus, avc->m.Length, 0, tlen);
104 #endif /* AFS_64BIT_CLIENT */
106 code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
107 #ifdef AFS_64BIT_CLIENT
108 if (code == RXGEN_OPCODE) {
109 afs_serverSetNo64Bit(tc);
110 code = rx_EndCall(tcall, code);
113 #endif /* AFS_64BIT_CLIENT */
115 code = rx_EndCall(tcall, code);
121 (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STOREDATA,
125 afs_ProcessFS(avc, &OutStatus, areq);
128 afs_InvalidateAllSegments(avc);
134 unsigned int storeallmissing = 0;
135 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
137 * afs_StoreAllSegments
140 * Stores all modified segments back to server
143 * avc : Pointer to vcache entry.
144 * areq : Pointer to request structure.
147 * Called with avc write-locked.
149 #if defined (AFS_HPUX_ENV) || defined(AFS_ULTRIX_ENV)
150 int NCHUNKSATONCE = 3;
152 int NCHUNKSATONCE = 64;
158 afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
161 register struct dcache *tdc;
162 register afs_int32 code = 0;
163 register afs_int32 index;
164 register afs_int32 origCBs, foreign = 0;
166 afs_hyper_t newDV, oldDV; /* DV when we start, and finish, respectively */
167 struct dcache **dcList, **dclist;
168 unsigned int i, j, minj, moredata, high, off;
170 afs_size_t maxStoredLength; /* highest offset we've written to server. */
173 struct afs_stats_xferData *xferP; /* Ptr to this op's xfer struct */
174 osi_timeval_t xferStartTime, /*FS xfer start time */
175 xferStopTime; /*FS xfer stop time */
176 afs_size_t bytesToXfer; /* # bytes to xfer */
177 afs_size_t bytesXferred; /* # bytes actually xferred */
178 #endif /* AFS_NOSTATS */
181 AFS_STATCNT(afs_StoreAllSegments);
183 hset(oldDV, avc->m.DataVersion);
184 hset(newDV, avc->m.DataVersion);
185 hash = DVHash(&avc->fid);
186 foreign = (avc->states & CForeign);
187 dcList = (struct dcache **)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
188 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
189 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
190 #if !defined(AFS_AIX32_ENV) && !defined(AFS_SGI65_ENV)
191 /* In the aix vm implementation we need to do the vm_writep even
192 * on the memcache case since that's we adjust the file's size
193 * and finish flushing partial vm pages.
195 if (cacheDiskType != AFS_FCACHE_TYPE_MEM)
196 #endif /* !AFS_AIX32_ENV && !AFS_SGI65_ENV */
198 /* If we're not diskless, reading a file may stress the VM
199 * system enough to cause a pageout, and this vnode would be
200 * locked when the pageout occurs. We can prevent this problem
201 * by making sure all dirty pages are already flushed. We don't
202 * do this when diskless because reading a diskless (i.e.
203 * memory-resident) chunk doesn't require using new VM, and we
204 * also don't want to dump more dirty data into a diskless cache,
205 * since they're smaller, and we might exceed its available
208 #if defined(AFS_SUN5_ENV)
209 if (sync & AFS_VMSYNC_INVAL) /* invalidate VM pages */
210 osi_VM_TryToSmush(avc, CRED(), 1);
213 osi_VM_StoreAllSegments(avc);
216 ConvertWToSLock(&avc->lock);
219 * Subsequent code expects a sorted list, and it expects all the
220 * chunks in the list to be contiguous, so we need a sort and a
221 * while loop in here, too - but this will work for a first pass...
222 * 92.10.05 - OK, there's a sort in here now. It's kind of a modified
223 * bin sort, I guess. Chunk numbers start with 0
225 * - Have to get a write lock on xdcache because GetDSlot might need it (if
226 * the chunk doesn't have a dcache struct).
227 * This seems like overkill in most cases.
228 * - I'm not sure that it's safe to do "index = .hvNextp", then unlock
229 * xdcache, then relock xdcache and try to use index. It is done
230 * a lot elsewhere in the CM, but I'm not buying that argument.
231 * - should be able to check IFDataMod without doing the GetDSlot (just
232 * hold afs_xdcache). That way, it's easy to do this without the
233 * writelock on afs_xdcache, and we save unneccessary disk
234 * operations. I don't think that works, 'cuz the next pointers
237 origCBs = afs_allCBs;
240 tlen = avc->m.Length;
244 memset((char *)dcList, 0, NCHUNKSATONCE * sizeof(struct dcache *));
248 /* lock and start over from beginning of hash chain
249 * in order to avoid a race condition. */
250 MObtainWriteLock(&afs_xdcache, 284);
251 index = afs_dvhashTbl[hash];
253 for (j = 0; index != NULLIDX;) {
254 if ((afs_indexFlags[index] & IFDataMod)
255 && (afs_indexUnique[index] == avc->fid.Fid.Unique)) {
256 tdc = afs_GetDSlot(index, 0); /* refcount+1. */
257 ReleaseReadLock(&tdc->tlock);
258 if (!FidCmp(&tdc->f.fid, &avc->fid) && tdc->f.chunk >= minj) {
259 off = tdc->f.chunk - minj;
260 if (off < NCHUNKSATONCE) {
262 osi_Panic("dclist slot already in use!");
267 /* DCLOCKXXX: chunkBytes is protected by tdc->lock which we
268 * can't grab here, due to lock ordering with afs_xdcache.
269 * So, disable this shortcut for now. -- kolya 2001-10-13
271 /* shortcut: big win for little files */
272 /* tlen -= tdc->f.chunkBytes;
279 if (j == NCHUNKSATONCE)
286 index = afs_dvnextTbl[index];
288 MReleaseWriteLock(&afs_xdcache);
290 /* this guy writes chunks, puts back dcache structs, and bumps newDV */
291 /* "moredata" just says "there are more dirty chunks yet to come".
295 static afs_uint32 lp1 = 10000, lp2 = 10000;
297 struct AFSStoreStatus InStatus;
298 struct AFSFetchStatus OutStatus;
300 afs_size_t base, bytes;
303 unsigned int first = 0;
306 struct osi_file *tfile;
307 struct rx_call *tcall;
309 for (bytes = 0, j = 0; !code && j <= high; j++) {
311 ObtainSharedLock(&(dcList[j]->lock), 629);
314 bytes += dcList[j]->f.chunkBytes;
315 if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
316 && (dcList[j]->f.chunk - minj < high)
318 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
322 if (bytes && (j == high || !dcList[j + 1])) {
323 /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
324 base = AFS_CHUNKTOBASE(first + minj);
327 * take a list of dcache structs and send them all off to the server
328 * the list must be in order, and the chunks contiguous.
329 * Note - there is no locking done by this code currently. For
330 * safety's sake, xdcache could be locked over the entire call.
331 * However, that pretty well ties up all the threads. Meantime, all
332 * the chunks _MUST_ have their refcounts bumped.
333 * The writes done before a store back will clear setuid-ness
335 * We can permit CacheStoreProc to wake up the user process IFF we
336 * are doing the last RPC for this close, ie, storing back the last
337 * set of contiguous chunks of a file.
340 dclist = &dcList[first];
341 nchunks = 1 + j - first;
342 nomore = !(moredata || (j != high));
343 InStatus.ClientModTime = avc->m.Date;
344 InStatus.Mask = AFS_SETMODTIME;
345 if (sync & AFS_SYNC) {
346 InStatus.Mask |= AFS_FSYNC;
348 tlen = lmin(avc->m.Length, avc->truncPos);
349 afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
350 ICL_TYPE_FID, &avc->fid.Fid, ICL_TYPE_OFFSET,
351 ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
352 ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
353 ICL_HANDLE_OFFSET(tlen));
357 tc = afs_Conn(&avc->fid, areq, 0);
361 tcall = rx_NewCall(tc->id);
362 #ifdef AFS_64BIT_CLIENT
363 if (!afs_serverHasNo64Bit(tc)) {
365 StartRXAFS_StoreData64(tcall,
371 if (tlen > 0xFFFFFFFF) {
374 afs_int32 t1, t2, t3;
379 StartRXAFS_StoreData(tcall,
386 #else /* AFS_64BIT_CLIENT */
388 StartRXAFS_StoreData(tcall,
389 (struct AFSFid *)&avc->
390 fid.Fid, &InStatus, base,
392 #endif /* AFS_64BIT_CLIENT */
399 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
400 avc->truncPos = AFS_NOTRUNC;
402 for (i = 0; i < nchunks && !code; i++) {
405 afs_warn("afs: missing dcache!\n");
407 continue; /* panic? */
409 afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2,
410 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32,
411 tdc->f.chunk, ICL_TYPE_INT32,
412 tdc->index, ICL_TYPE_INT32,
416 if (avc->asynchrony == -1) {
417 if (afs_defaultAsynchrony >
419 shouldwake = &nomore;
421 } else if ((afs_uint32) avc->asynchrony >=
423 shouldwake = &nomore;
426 tfile = afs_CFileOpen(tdc->f.inode);
429 &(afs_stats_cmfullperf.rpc.
431 [AFS_STATS_FS_XFERIDX_STOREDATA]);
432 osi_GetuTime(&xferStartTime);
435 afs_CacheStoreProc(tcall, tfile,
436 tdc->f.chunkBytes, avc,
437 shouldwake, &bytesToXfer,
440 osi_GetuTime(&xferStopTime);
443 (xferP->numSuccesses)++;
444 afs_stats_XferSumBytes
445 [AFS_STATS_FS_XFERIDX_STOREDATA] +=
448 (afs_stats_XferSumBytes
449 [AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
450 afs_stats_XferSumBytes
451 [AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
452 if (bytesXferred < xferP->minBytes)
453 xferP->minBytes = bytesXferred;
454 if (bytesXferred > xferP->maxBytes)
455 xferP->maxBytes = bytesXferred;
458 * Tally the size of the object. Note: we tally the actual size,
459 * NOT the number of bytes that made it out over the wire.
461 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
463 else if (bytesToXfer <=
464 AFS_STATS_MAXBYTES_BUCKET1)
466 else if (bytesToXfer <=
467 AFS_STATS_MAXBYTES_BUCKET2)
469 else if (bytesToXfer <=
470 AFS_STATS_MAXBYTES_BUCKET3)
472 else if (bytesToXfer <=
473 AFS_STATS_MAXBYTES_BUCKET4)
475 else if (bytesToXfer <=
476 AFS_STATS_MAXBYTES_BUCKET5)
478 else if (bytesToXfer <=
479 AFS_STATS_MAXBYTES_BUCKET6)
481 else if (bytesToXfer <=
482 AFS_STATS_MAXBYTES_BUCKET7)
487 afs_stats_GetDiff(elapsedTime, xferStartTime,
489 afs_stats_AddTo((xferP->sumTime),
491 afs_stats_SquareAddTo((xferP->sqrTime),
493 if (afs_stats_TimeLessThan
494 (elapsedTime, (xferP->minTime))) {
495 afs_stats_TimeAssign((xferP->minTime),
498 if (afs_stats_TimeGreaterThan
499 (elapsedTime, (xferP->maxTime))) {
500 afs_stats_TimeAssign((xferP->maxTime),
506 afs_CacheStoreProc(tcall, tfile,
507 tdc->f.chunkBytes, avc,
508 shouldwake, &lp1, &lp2);
509 #endif /* AFS_NOSTATS */
510 afs_CFileClose(tfile);
511 #ifdef AFS_64BIT_CLIENT
512 if (code == RXGEN_OPCODE) {
513 afs_serverSetNo64Bit(tc);
516 #endif /* AFS_64BIT_CLIENT */
517 if ((tdc->f.chunkBytes < afs_OtherCSize)
518 && (i < (nchunks - 1))) {
519 int bsent, tlen, sbytes =
520 afs_OtherCSize - tdc->f.chunkBytes;
522 osi_AllocLargeSpace(AFS_LRALLOCSIZ);
527 AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ :
529 memset(tbuffer, 0, tlen);
531 bsent = rx_Write(tcall, tbuffer, tlen);
535 code = -33; /* XXX */
540 osi_FreeLargeSpace(tbuffer);
542 stored += tdc->f.chunkBytes;
544 /* ideally, I'd like to unlock the dcache and turn
545 * off the writing bit here, but that would
546 * require being able to retry StoreAllSegments in
547 * the event of a failure. It only really matters
548 * if user can't read from a 'locked' dcache or
549 * one which has the writing bit turned on. */
552 struct AFSVolSync tsync;
555 EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
560 doProcessFS = 1; /* Flag to run afs_ProcessFS() later on */
565 code2 = rx_EndCall(tcall, code);
571 (tc, code, &avc->fid, areq,
572 AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
575 /* put back all remaining locked dcache entries */
576 for (i = 0; i < nchunks; i++) {
579 if (afs_indexFlags[tdc->index] & IFDataMod) {
581 * LOCKXXX -- should hold afs_xdcache(W) when
582 * modifying afs_indexFlags.
584 afs_indexFlags[tdc->index] &= ~IFDataMod;
585 afs_stats_cmperf.cacheCurrDirtyChunks--;
586 afs_indexFlags[tdc->index] &= ~IFDirtyPages;
587 if (sync & AFS_VMSYNC_INVAL) {
588 /* since we have invalidated all the pages of this
589 ** vnode by calling osi_VM_TryToSmush, we can
590 ** safely mark this dcache entry as not having
591 ** any pages. This vnode now becomes eligible for
592 ** reclamation by getDownD.
594 afs_indexFlags[tdc->index] &= ~IFAnyPages;
598 UpgradeSToWLock(&tdc->lock, 628);
599 tdc->f.states &= ~DWriting; /* correct? */
600 tdc->dflags |= DFEntryMod;
601 ReleaseWriteLock(&tdc->lock);
603 /* Mark the entry as released */
608 /* Now copy out return params */
609 UpgradeSToWLock(&avc->lock, 28); /* keep out others for a while */
610 afs_ProcessFS(avc, &OutStatus, areq);
611 /* Keep last (max) size of file on server to see if
612 * we need to call afs_StoreMini to extend the file.
615 maxStoredLength = OutStatus.Length;
616 ConvertWToSLock(&avc->lock);
621 for (j++; j <= high; j++) {
623 ReleaseSharedLock(&(dcList[j]->lock));
624 afs_PutDCache(dcList[j]);
625 /* Releasing entry */
631 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
632 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
637 /* Release any zero-length dcache entries in our interval
638 * that we locked but didn't store back above.
640 for (j = 0; j <= high; j++) {
643 osi_Assert(tdc->f.chunkBytes == 0);
644 ReleaseSharedLock(&tdc->lock);
650 minj += NCHUNKSATONCE;
651 } while (!code && moredata);
653 UpgradeSToWLock(&avc->lock, 29);
655 /* send a trivial truncation store if did nothing else */
658 * Call StoreMini if we haven't written enough data to extend the
659 * file at the fileserver to the client's notion of the file length.
661 if ((avc->truncPos != AFS_NOTRUNC) || ((avc->states & CExtendedFile)
662 && (maxStoredLength <
664 code = afs_StoreMini(avc, areq);
666 hadd32(newDV, 1); /* just bumped here, too */
668 avc->states &= ~CExtendedFile;
672 * Finally, turn off DWriting, turn on DFEntryMod,
673 * update f.versionNo.
674 * A lot of this could be integrated into the loop above
684 memset((char *)dcList, 0,
685 NCHUNKSATONCE * sizeof(struct dcache *));
687 /* overkill, but it gets the lock in case GetDSlot needs it */
688 MObtainWriteLock(&afs_xdcache, 285);
690 for (j = 0, safety = 0, index = afs_dvhashTbl[hash];
691 index != NULLIDX && safety < afs_cacheFiles + 2;) {
693 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
694 tdc = afs_GetDSlot(index, 0);
695 ReleaseReadLock(&tdc->tlock);
697 if (!FidCmp(&tdc->f.fid, &avc->fid)
698 && tdc->f.chunk >= minj) {
699 off = tdc->f.chunk - minj;
700 if (off < NCHUNKSATONCE) {
701 /* this is the file, and the correct chunk range */
702 if (j >= NCHUNKSATONCE)
704 ("Too many dcache entries in range\n");
709 if (j == NCHUNKSATONCE)
717 index = afs_dvnextTbl[index];
719 MReleaseWriteLock(&afs_xdcache);
721 for (i = 0; i < j; i++) {
722 /* Iterate over the dcache entries we collected above */
724 ObtainSharedLock(&tdc->lock, 677);
726 /* was code here to clear IFDataMod, but it should only be done
727 * in storedcache and storealldcache.
729 /* Only increase DV if we had up-to-date data to start with.
730 * Otherwise, we could be falsely upgrading an old chunk
731 * (that we never read) into one labelled with the current
732 * DV #. Also note that we check that no intervening stores
733 * occurred, otherwise we might mislabel cache information
734 * for a chunk that we didn't store this time
736 /* Don't update the version number if it's not yet set. */
737 if (!hsame(tdc->f.versionNo, h_unset)
738 && hcmp(tdc->f.versionNo, oldDV) >= 0) {
740 if ((!(afs_dvhack || foreign)
741 && hsame(avc->m.DataVersion, newDV))
742 || ((afs_dvhack || foreign)
743 && (origCBs == afs_allCBs))) {
744 /* no error, this is the DV */
746 UpgradeSToWLock(&tdc->lock, 678);
747 hset(tdc->f.versionNo, avc->m.DataVersion);
748 tdc->dflags |= DFEntryMod;
749 ConvertWToSLock(&tdc->lock);
753 ReleaseSharedLock(&tdc->lock);
757 minj += NCHUNKSATONCE;
764 * Invalidate chunks after an error for ccores files since
765 * afs_inactive won't be called for these and they won't be
766 * invalidated. Also discard data if it's a permanent error from the
769 if (areq->permWriteError || (avc->states & (CCore1 | CCore))) {
770 afs_InvalidateAllSegments(avc);
773 afs_Trace3(afs_iclSetp, CM_TRACE_STOREALLDONE, ICL_TYPE_POINTER, avc,
774 ICL_TYPE_INT32, avc->m.Length, ICL_TYPE_INT32, code);
775 /* would like a Trace5, but it doesn't exist... */
776 afs_Trace3(afs_iclSetp, CM_TRACE_AVCLOCKER, ICL_TYPE_POINTER, avc,
777 ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
778 avc->lock.excl_locked);
779 afs_Trace4(afs_iclSetp, CM_TRACE_AVCLOCKEE, ICL_TYPE_POINTER, avc,
780 ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
781 avc->lock.readers_reading, ICL_TYPE_INT32,
782 avc->lock.num_waiting);
785 * Finally, if updated DataVersion matches newDV, we did all of the
786 * stores. If mapDV indicates that the page cache was flushed up
787 * to when we started the store, then we can relabel them as flushed
788 * as recently as newDV.
789 * Turn off CDirty bit because the stored data is now in sync with server.
791 if (code == 0 && hcmp(avc->mapDV, oldDV) >= 0) {
792 if ((!(afs_dvhack || foreign) && hsame(avc->m.DataVersion, newDV))
793 || ((afs_dvhack || foreign) && (origCBs == afs_allCBs))) {
794 hset(avc->mapDV, newDV);
795 avc->states &= ~CDirty;
798 osi_FreeLargeSpace(dcList);
800 /* If not the final write a temporary error is ok. */
801 if (code && !areq->permWriteError && !(sync & AFS_LASTSTORE))
806 } /*afs_StoreAllSegments (new 03/02/94) */
810 * afs_InvalidateAllSegments
813 * Invalidates all chunks for a given file
816 * avc : Pointer to vcache entry.
819 * For example, called after an error has been detected. Called
820 * with avc write-locked, and afs_xdcache unheld.
824 afs_InvalidateAllSegments(struct vcache *avc)
829 struct dcache **dcList;
830 int i, dcListMax, dcListCount;
832 AFS_STATCNT(afs_InvalidateAllSegments);
833 afs_Trace2(afs_iclSetp, CM_TRACE_INVALL, ICL_TYPE_POINTER, avc,
834 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
835 hash = DVHash(&avc->fid);
836 avc->truncPos = AFS_NOTRUNC; /* don't truncate later */
837 avc->states &= ~CExtendedFile; /* not any more */
838 ObtainWriteLock(&afs_xcbhash, 459);
839 afs_DequeueCallback(avc);
840 avc->states &= ~(CStatd | CDirty); /* mark status information as bad, too */
841 ReleaseWriteLock(&afs_xcbhash);
842 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
843 osi_dnlc_purgedp(avc);
844 /* Blow away pages; for now, only for Solaris */
845 #if (defined(AFS_SUN5_ENV))
846 if (WriteLocked(&avc->lock))
847 osi_ReleaseVM(avc, (struct AFS_UCRED *)0);
850 * Block out others from screwing with this table; is a read lock
853 MObtainWriteLock(&afs_xdcache, 286);
856 for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
857 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
858 tdc = afs_GetDSlot(index, 0);
859 ReleaseReadLock(&tdc->tlock);
860 if (!FidCmp(&tdc->f.fid, &avc->fid))
864 index = afs_dvnextTbl[index];
867 dcList = osi_Alloc(dcListMax * sizeof(struct dcache *));
870 for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
871 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
872 tdc = afs_GetDSlot(index, 0);
873 ReleaseReadLock(&tdc->tlock);
874 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
875 /* same file? we'll zap it */
876 if (afs_indexFlags[index] & IFDataMod) {
877 afs_stats_cmperf.cacheCurrDirtyChunks--;
878 /* don't write it back */
879 afs_indexFlags[index] &= ~IFDataMod;
881 afs_indexFlags[index] &= ~IFAnyPages;
882 if (dcListCount < dcListMax)
883 dcList[dcListCount++] = tdc;
890 index = afs_dvnextTbl[index];
892 MReleaseWriteLock(&afs_xdcache);
894 for (i = 0; i < dcListCount; i++) {
897 ObtainWriteLock(&tdc->lock, 679);
899 if (vType(avc) == VDIR)
901 ReleaseWriteLock(&tdc->lock);
905 osi_Free(dcList, dcListMax * sizeof(struct dcache *));
912 * afs_TruncateAllSegments
915 * Truncate a cache file.
918 * avc : Ptr to vcache entry to truncate.
919 * alen : Number of bytes to make the file.
920 * areq : Ptr to request structure.
923 * Called with avc write-locked; in VFS40 systems, pvnLock is also
927 afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
928 struct vrequest *areq, struct AFS_UCRED *acred)
930 register struct dcache *tdc;
931 register afs_int32 code;
932 register afs_int32 index;
936 struct dcache **tdcArray;
938 AFS_STATCNT(afs_TruncateAllSegments);
939 avc->m.Date = osi_Time();
940 afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL, ICL_TYPE_POINTER, avc,
941 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
942 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(alen));
943 if (alen >= avc->m.Length) {
945 * Special speedup since Sun's vm extends the file this way;
946 * we've never written to the file thus we can just set the new
947 * length and avoid the needless calls below.
948 * Also used for ftruncate calls which can extend the file.
949 * To completely minimize the possible extra StoreMini RPC, we really
950 * should keep the ExtendedPos as well and clear this flag if we
951 * truncate below that value before we store the file back.
953 avc->states |= CExtendedFile;
954 avc->m.Length = alen;
957 #if (defined(AFS_SUN5_ENV))
959 /* Zero unused portion of last page */
960 osi_VM_PreTruncate(avc, alen, acred);
964 #if (defined(AFS_SUN5_ENV))
965 ObtainWriteLock(&avc->vlock, 546);
966 avc->activeV++; /* Block new getpages */
967 ReleaseWriteLock(&avc->vlock);
970 ReleaseWriteLock(&avc->lock);
973 /* Flush pages beyond end-of-file. */
974 osi_VM_Truncate(avc, alen, acred);
977 ObtainWriteLock(&avc->lock, 79);
979 avc->m.Length = alen;
981 if (alen < avc->truncPos)
982 avc->truncPos = alen;
983 code = DVHash(&avc->fid);
985 /* block out others from screwing with this table */
986 MObtainWriteLock(&afs_xdcache, 287);
989 for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
990 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
991 tdc = afs_GetDSlot(index, 0);
992 ReleaseReadLock(&tdc->tlock);
993 if (!FidCmp(&tdc->f.fid, &avc->fid))
997 index = afs_dvnextTbl[index];
1000 /* Now allocate space where we can save those dcache entries, and
1001 * do a second pass over them.. Since we're holding xdcache, it
1002 * shouldn't be changing.
1004 tdcArray = osi_Alloc(dcCount * sizeof(struct dcache *));
1007 for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
1008 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1009 tdc = afs_GetDSlot(index, 0);
1010 ReleaseReadLock(&tdc->tlock);
1011 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1012 /* same file, and modified, we'll store it back */
1013 if (dcPos < dcCount) {
1014 tdcArray[dcPos++] = tdc;
1022 index = afs_dvnextTbl[index];
1025 MReleaseWriteLock(&afs_xdcache);
1027 /* Now we loop over the array of dcache entries and truncate them */
1028 for (index = 0; index < dcPos; index++) {
1029 struct osi_file *tfile;
1031 tdc = tdcArray[index];
1033 newSize = alen - AFS_CHUNKTOBASE(tdc->f.chunk);
1036 ObtainSharedLock(&tdc->lock, 672);
1037 if (newSize < tdc->f.chunkBytes) {
1038 UpgradeSToWLock(&tdc->lock, 673);
1039 tfile = afs_CFileOpen(tdc->f.inode);
1040 afs_CFileTruncate(tfile, newSize);
1041 afs_CFileClose(tfile);
1042 afs_AdjustSize(tdc, newSize);
1043 if (alen < tdc->validPos) {
1044 if (alen < AFS_CHUNKTOBASE(tdc->f.chunk))
1047 tdc->validPos = alen;
1049 ConvertWToSLock(&tdc->lock);
1051 ReleaseSharedLock(&tdc->lock);
1055 osi_Free(tdcArray, dcCount * sizeof(struct dcache *));
1057 #if (defined(AFS_SUN5_ENV))
1058 ObtainWriteLock(&avc->vlock, 547);
1059 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1060 avc->vstates &= ~VRevokeWait;
1061 afs_osi_Wakeup((char *)&avc->vstates);
1063 ReleaseWriteLock(&avc->vlock);