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"
18 #include "../afs/sysincludes.h" /*Standard vendor system headers*/
19 #include "../afs/afsincludes.h" /*AFS-based standard headers*/
20 #include "../afs/afs_stats.h" /* statistics */
21 #include "../afs/afs_cbqueue.h"
22 #include "../afs/afs_osidnlc.h"
26 /* Imported variables */
27 extern afs_rwlock_t afs_xserver;
28 extern afs_rwlock_t afs_xdcache;
29 extern afs_rwlock_t afs_xcbhash;
30 extern afs_lock_t afs_ftf;
31 extern struct server *afs_servers[NSERVERS];
32 extern afs_int32 afs_dhashsize;
33 extern afs_int32 *afs_dvhashTbl;
34 extern unsigned char *afs_indexFlags; /*(only one) Is there data there?*/
35 extern int cacheDiskType;
37 afs_uint32 afs_stampValue=0;
43 * Send a truncation request to a FileServer.
49 * We're write-locked upon entry.
52 int afs_StoreMini(avc, areq)
53 register struct vcache *avc;
54 struct vrequest *areq;
57 register struct conn *tc;
58 struct AFSStoreStatus InStatus;
59 struct AFSFetchStatus OutStatus;
60 struct AFSVolSync tsync;
61 register afs_int32 code;
62 register struct rx_call *tcall;
66 AFS_STATCNT(afs_StoreMini);
67 afs_Trace2(afs_iclSetp, CM_TRACE_STOREMINI, ICL_TYPE_POINTER, avc,
68 ICL_TYPE_INT32, avc->m.Length);
70 if (avc->truncPos < tlen) tlen = avc->truncPos;
71 avc->truncPos = AFS_NOTRUNC;
72 avc->states &= ~CExtendedFile;
75 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
77 #ifdef RX_ENABLE_LOCKS
79 #endif /* RX_ENABLE_LOCKS */
80 tcall = rx_NewCall(tc->id);
81 #ifdef RX_ENABLE_LOCKS
83 #endif /* RX_ENABLE_LOCKS */
84 /* Set the client mod time since we always want the file
85 * to have the client's mod time and not the server's one
86 * (to avoid problems with make, etc.) It almost always
87 * works fine with standard afs because them server/client
88 * times are in sync and more importantly this storemini
89 * it's a special call that would typically be followed by
90 * the proper store-data or store-status calls.
92 InStatus.Mask = AFS_SETMODTIME;
93 InStatus.ClientModTime = avc->m.Date;
94 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
95 #ifdef RX_ENABLE_LOCKS
97 #endif /* RX_ENABLE_LOCKS */
98 code = StartRXAFS_StoreData(tcall,
99 (struct AFSFid *)&avc->fid.Fid,
100 &InStatus, avc->m.Length, 0, tlen);
102 code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
104 code = rx_EndCall(tcall, code);
105 #ifdef RX_ENABLE_LOCKS
107 #endif /* RX_ENABLE_LOCKS */
112 (afs_Analyze(tc, code, &avc->fid, areq,
113 AFS_STATS_FS_RPCIDX_STOREDATA,
114 SHARED_LOCK, (struct cell *)0));
117 afs_ProcessFS(avc, &OutStatus, areq);
121 afs_InvalidateAllSegments(avc, 1);
127 unsigned int storeallmissing = 0;
128 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
130 * afs_StoreAllSegments
133 * Stores all modified segments back to server
136 * avc : Pointer to vcache entry.
137 * areq : Pointer to request structure.
140 * Called with avc write-locked.
142 #if defined (AFS_HPUX_ENV) || defined(AFS_ULTRIX_ENV)
143 int NCHUNKSATONCE = 3;
145 int NCHUNKSATONCE = 64 ;
150 afs_StoreAllSegments(avc, areq, sync)
151 register struct vcache *avc;
152 struct vrequest *areq;
155 { /*afs_StoreAllSegments*/
156 register struct dcache *tdc;
157 register afs_int32 code=0;
158 register afs_int32 index;
159 register afs_int32 origCBs, foreign=0;
161 afs_hyper_t newDV, oldDV; /* DV when we start, and finish, respectively */
162 struct dcache **dcList, **dclist;
163 unsigned int i, j, minj, maxj, moredata, high, off;
166 int maxStoredLength; /* highest offset we've written to server. */
168 struct afs_stats_xferData *xferP; /* Ptr to this op's xfer struct */
169 osi_timeval_t xferStartTime, /*FS xfer start time*/
170 xferStopTime; /*FS xfer stop time*/
171 afs_int32 bytesToXfer; /* # bytes to xfer*/
172 afs_int32 bytesXferred; /* # bytes actually xferred*/
173 #endif /* AFS_NOSTATS */
176 AFS_STATCNT(afs_StoreAllSegments);
178 hset(oldDV, avc->m.DataVersion);
179 hset(newDV, avc->m.DataVersion);
180 hash = DVHash(&avc->fid);
181 foreign = (avc->states & CForeign);
182 dcList = (struct dcache **) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
183 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
184 ICL_TYPE_INT32, avc->m.Length);
185 #ifndef AFS_AIX32_ENV
186 /* In the aix vm implementation we need to do the vm_writep even
187 * on the memcache case since that's we adjust the file's size
188 * and finish flushing partial vm pages.
190 if (cacheDiskType != AFS_FCACHE_TYPE_MEM)
191 #endif /* AFS_AIX32_ENV */
193 /* If we're not diskless, reading a file may stress the VM
194 * system enough to cause a pageout, and this vnode would be
195 * locked when the pageout occurs. We can prevent this problem
196 * by making sure all dirty pages are already flushed. We don't
197 * do this when diskless because reading a diskless (i.e.
198 * memory-resident) chunk doesn't require using new VM, and we
199 * also don't want to dump more dirty data into a diskless cache,
200 * since they're smaller, and we might exceed its available
203 #if defined(AFS_SUN5_ENV)
204 if ( sync & AFS_VMSYNC_INVAL) /* invalidate VM pages */
205 osi_VM_TryToSmush(avc, CRED() , 1 );
208 osi_VM_StoreAllSegments(avc);
211 ConvertWToSLock(&avc->lock);
214 * Subsequent code expects a sorted list, and it expects all the
215 * chunks in the list to be contiguous, so we need a sort and a
216 * while loop in here, too - but this will work for a first pass...
217 * 92.10.05 - OK, there's a sort in here now. It's kind of a modified
218 * bin sort, I guess. Chunk numbers start with 0
220 * - Have to get a write lock on xdcache because GetDSlot might need it (if
221 * the chunk doesn't have a dcache struct).
222 * This seems like overkill in most cases.
223 * - I'm not sure that it's safe to do "index = .hvNextp", then unlock
224 * xdcache, then relock xdcache and try to use index. It is done
225 * a lot elsewhere in the CM, but I'm not buying that argument.
226 * - should be able to check IFDataMod without doing the GetDSlot (just
227 * hold afs_xdcache). That way, it's easy to do this without the
228 * writelock on afs_xdcache, and we save unneccessary disk
229 * operations. I don't think that works, 'cuz the next pointers
232 origCBs = afs_allCBs;
236 tlen = avc->m.Length;
240 bzero ((char *)dcList, NCHUNKSATONCE * sizeof(struct dcache *));
244 /* lock and start over from beginning of hash chain
245 * in order to avoid a race condition. */
246 MObtainWriteLock(&afs_xdcache,284);
247 index = afs_dvhashTbl[hash];
249 for(j=0; index != NULLIDX;) {
250 if ((afs_indexFlags[index] & IFDataMod) &&
251 (afs_indexUnique[index] == avc->fid.Fid.Unique)) {
252 tdc = afs_GetDSlot(index, 0); /* refcount+1. */
253 if (!FidCmp( &tdc->f.fid, &avc->fid ) && tdc->f.chunk >= minj ) {
255 off = tdc->f.chunk - minj;
256 if (off < NCHUNKSATONCE) {
258 osi_Panic("dclist slot already in use!");
262 tlen -= tdc->f.chunkBytes; /* shortcut: big win for little files */
269 lockedPutDCache(tdc);
270 if (j == NCHUNKSATONCE)
274 lockedPutDCache(tdc);
277 index = afs_dvnextTbl[index];
280 MReleaseWriteLock(&afs_xdcache);
281 /* this guy writes chunks, puts back dcache structs, and bumps newDV */
282 /* "moredata" just says "there are more dirty chunks yet to come".
285 static afs_uint32 lp1 = 10000, lp2 = 10000;
286 struct AFSStoreStatus InStatus;
287 afs_uint32 base, bytes, nchunks;
292 struct osi_file * tfile;
293 struct rx_call * tcall;
294 extern int afs_defaultAsynchrony;
297 for (bytes = 0, j = 0; !code && j<=high; j++) {
301 bytes += dcList[j]->f.chunkBytes;
302 if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
303 && (dcList[j]->f.chunk - minj < high)
305 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
312 if (bytes && (j==high || !dcList[j+1])) {
313 /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
314 base = AFS_CHUNKTOBASE(first + minj) ;
317 * take a list of dcache structs and send them all off to the server
318 * the list must be in order, and the chunks contiguous.
319 * Note - there is no locking done by this code currently. For
320 * safety's sake, xdcache could be locked over the entire call.
321 * However, that pretty well ties up all the threads. Meantime, all
322 * the chunks _MUST_ have their refcounts bumped.
323 * The writes done before a store back will clear setuid-ness
325 * We can permit CacheStoreProc to wake up the user process IFF we
326 * are doing the last RPC for this close, ie, storing back the last
327 * set of contiguous chunks of a file.
330 dclist = &dcList[first];
332 nomore = !(moredata || (j!=high));
333 InStatus.ClientModTime = avc->m.Date;
334 InStatus.Mask = AFS_SETMODTIME;
335 if (sync & AFS_SYNC) {
336 InStatus.Mask |= AFS_FSYNC;
338 tlen = lmin(avc->m.Length, avc->truncPos);
342 tc = afs_Conn(&avc->fid, areq);
344 #ifdef RX_ENABLE_LOCKS
346 #endif /* RX_ENABLE_LOCKS */
347 tcall = rx_NewCall(tc->id);
348 code = StartRXAFS_StoreData(tcall, (struct AFSFid *) &avc->fid.Fid,
349 &InStatus, base, bytes, tlen);
350 #ifdef RX_ENABLE_LOCKS
352 #endif /* RX_ENABLE_LOCKS */
358 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
359 avc->truncPos = AFS_NOTRUNC;
361 for (i = 0; i<nchunks && !code;i++) {
364 afs_warn("afs: missing dcache!\n");
366 continue; /* panic? */
370 if (avc->asynchrony == -1) {
371 if (afs_defaultAsynchrony > (bytes-stored)) {
372 shouldwake = &nomore;
374 } else if ((afs_uint32)avc->asynchrony >= (bytes-stored)) {
375 shouldwake = &nomore;
378 tfile = afs_CFileOpen(tdc->f.inode);
380 xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_STOREDATA]);
381 osi_GetuTime(&xferStartTime);
383 code = afs_CacheStoreProc(tcall, tfile, tdc->f.chunkBytes,
384 avc, shouldwake, &bytesToXfer,
387 osi_GetuTime(&xferStopTime);
390 (xferP->numSuccesses)++;
391 afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] += bytesXferred;
392 (xferP->sumBytes) += (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
393 afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
394 if (bytesXferred < xferP->minBytes)
395 xferP->minBytes = bytesXferred;
396 if (bytesXferred > xferP->maxBytes)
397 xferP->maxBytes = bytesXferred;
400 * Tally the size of the object. Note: we tally the actual size,
401 * NOT the number of bytes that made it out over the wire.
403 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
406 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
409 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
412 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
415 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
418 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
421 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
424 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
429 afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
430 afs_stats_AddTo((xferP->sumTime), elapsedTime);
431 afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
432 if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
433 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
435 if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
436 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
440 code = afs_CacheStoreProc(tcall, tfile, tdc->f.chunkBytes, avc,
441 shouldwake, &lp1, &lp2);
442 #endif /* AFS_NOSTATS */
443 afs_CFileClose(tfile);
444 if ((tdc->f.chunkBytes < afs_OtherCSize) &&
446 int bsent, tlen, tlen1=0, sbytes = afs_OtherCSize - tdc->f.chunkBytes;
447 char *tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
450 tlen = (sbytes > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : sbytes);
451 bzero(tbuffer, tlen);
452 #ifdef RX_ENABLE_LOCKS
454 #endif /* RX_ENABLE_LOCKS */
455 bsent = rx_Write(tcall, tbuffer, tlen);
456 #ifdef RX_ENABLE_LOCKS
458 #endif /* RX_ENABLE_LOCKS */
461 code = -33; /* XXX */
466 osi_FreeLargeSpace(tbuffer);
468 stored += tdc->f.chunkBytes;
470 /* ideally, I'd like to unlock the dcache and turn
471 * off the writing bit here, but that would
472 * require being able to retry StoreAllSegments in
473 * the event of a failure. It only really matters
474 * if user can't read from a 'locked' dcache or
475 * one which has the writing bit turned on. */
478 struct AFSFetchStatus OutStatus;
479 struct AFSVolSync tsync;
480 #ifdef RX_ENABLE_LOCKS
482 #endif /* RX_ENABLE_LOCKS */
483 code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
484 #ifdef RX_ENABLE_LOCKS
486 #endif /* RX_ENABLE_LOCKS */
490 /* Now copy out return params */
491 UpgradeSToWLock(&avc->lock,28); /* keep out others for a while */
492 if (!code) { /* must wait til RPC completes to be sure of this info */
493 afs_ProcessFS(avc, &OutStatus, areq);
494 /* Keep last (max) size of file on server to see if
495 * we need to call afs_StoreMini to extend the file.
498 maxStoredLength = OutStatus.Length;
501 ConvertWToSLock(&avc->lock);
504 #ifdef RX_ENABLE_LOCKS
506 #endif /* RX_ENABLE_LOCKS */
507 code = rx_EndCall(tcall, code, avc, base);
508 #ifdef RX_ENABLE_LOCKS
510 #endif /* RX_ENABLE_LOCKS */
512 } while (afs_Analyze(tc, code, &avc->fid, areq,
513 AFS_STATS_FS_RPCIDX_STOREDATA,
514 SHARED_LOCK, (struct cell *)0));
516 /* put back all remaining locked dcache entries */
517 for (i=0; i<nchunks; i++) {
520 if (afs_indexFlags[tdc->index] & IFDataMod) {
521 afs_indexFlags[tdc->index] &= ~IFDataMod;
522 afs_stats_cmperf.cacheCurrDirtyChunks--;
523 afs_indexFlags[tdc->index] &= ~IFDirtyPages;
524 if ( sync & AFS_VMSYNC_INVAL )
526 /* since we have invalidated all the pages of this
527 ** vnode by calling osi_VM_TryToSmush, we can
528 ** safely mark this dcache entry as not having
529 ** any pages. This vnode now becomes eligible for
530 ** reclamation by getDownD.
532 afs_indexFlags[tdc->index] &= ~IFAnyPages;
536 tdc->f.states &= ~DWriting; /* correct?*/
537 tdc->flags |= DFEntryMod;
538 lockedPutDCache(tdc);
542 for (j++; j<=high; j++)
544 lockedPutDCache(dcList[j]);
547 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
548 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
554 minj += NCHUNKSATONCE;
555 } while ( !code && moredata );
557 UpgradeSToWLock(&avc->lock,29);
559 /* send a trivial truncation store if did nothing else */
562 * Call StoreMini if we haven't written enough data to extend the
563 * file at the fileserver to the client's notion of the file length.
565 if ((avc->truncPos != AFS_NOTRUNC) ||
566 ((avc->states & CExtendedFile) && (maxStoredLength < avc->m.Length))) {
567 code = afs_StoreMini(avc, areq);
569 hadd32(newDV, 1); /* just bumped here, too */
571 avc->states &= ~CExtendedFile;
575 * Finally, turn off DWriting, turn on DFEntryMod,
576 * update f.versionNo.
577 * A lot of this could be integrated into the loop above
582 MObtainWriteLock(&afs_xdcache,285); /* overkill, but it gets the
583 * lock in case GetDSlot needs it */
584 for(safety = 0, index = afs_dvhashTbl[hash];
585 index != NULLIDX && safety < afs_cacheFiles+2;) {
587 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
588 tdc = afs_GetDSlot(index, 0);
590 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
591 /* this is the file */
592 /* was code here to clear IFDataMod, but it should only be done
593 * in storedcache and storealldcache.
595 /* Only increase DV if we had up-to-date data to start with.
596 * Otherwise, we could be falsely upgrading an old chunk
597 * (that we never read) into one labelled with the current
598 * DV #. Also note that we check that no intervening stores
599 * occurred, otherwise we might mislabel cache information
600 * for a chunk that we didn't store this time
602 /* Don't update the version number if it's not yet set. */
603 if (code == 0 && (!hsame(tdc->f.versionNo, h_unset))
604 && (hcmp(tdc->f.versionNo, oldDV) >= 0)) {
605 if ((!(afs_dvhack || foreign) && hsame(avc->m.DataVersion, newDV))
606 || ((afs_dvhack || foreign) && (origCBs == afs_allCBs)) ) {
607 /* no error, this is the DV */
608 hset(tdc->f.versionNo, avc->m.DataVersion);
609 tdc->flags |= DFEntryMod;
613 lockedPutDCache(tdc);
615 index = afs_dvnextTbl[index];
617 MReleaseWriteLock(&afs_xdcache);
622 * Invalidate chunks after an error for ccores files since
623 * afs_inactive won't be called for these and they won't be
624 * invalidated. Also discard data if it's a permanent error from the
627 if (areq->permWriteError || (avc->states & (CCore1 | CCore))) {
628 afs_InvalidateAllSegments(avc, 1);
631 afs_Trace3(afs_iclSetp, CM_TRACE_STOREALLDONE, ICL_TYPE_POINTER, avc,
632 ICL_TYPE_INT32, avc->m.Length, ICL_TYPE_INT32, code);
633 /* would like a Trace5, but it doesn't exist...*/
634 afs_Trace3(afs_iclSetp, CM_TRACE_AVCLOCKER, ICL_TYPE_POINTER, avc,
635 ICL_TYPE_INT32, avc->lock.wait_states,
636 ICL_TYPE_INT32, avc->lock.excl_locked);
637 afs_Trace4(afs_iclSetp, CM_TRACE_AVCLOCKEE, ICL_TYPE_POINTER, avc,
638 ICL_TYPE_INT32, avc->lock.wait_states,
639 ICL_TYPE_INT32, avc->lock.readers_reading,
640 ICL_TYPE_INT32, avc->lock.num_waiting );
643 * Finally, if updated DataVersion matches newDV, we did all of the
644 * stores. If mapDV indicates that the page cache was flushed up
645 * to when we started the store, then we can relabel them as flushed
646 * as recently as newDV.
647 * Turn off CDirty bit because the stored data is now in sync with server.
649 if (code == 0 && hcmp(avc->mapDV, oldDV) >= 0) {
650 if ((!(afs_dvhack || foreign) && hsame(avc->m.DataVersion, newDV))
651 || ((afs_dvhack || foreign) && (origCBs == afs_allCBs)) ) {
652 hset(avc->mapDV, newDV);
653 avc->states &= ~CDirty;
656 osi_FreeLargeSpace(dcList);
658 /* If not the final write a temporary error is ok. */
659 if (code && !areq->permWriteError && !(sync & AFS_LASTSTORE))
664 } /*afs_StoreAllSegments (new 03/02/94)*/
668 * afs_InvalidateAllSegments
671 * Invalidates all chunks for a given file
674 * avc : Pointer to vcache entry.
675 * asetLock : If true, we are to set the afs_xdcache lock; otherwise,
676 * the caller has already done it.
679 * For example, called after an error has been detected. Called
680 * with avc write-locked.
683 afs_InvalidateAllSegments(avc, asetLock)
687 { /*afs_InvalidateAllSegments*/
693 AFS_STATCNT(afs_InvalidateAllSegments);
694 afs_Trace2(afs_iclSetp, CM_TRACE_INVALL, ICL_TYPE_POINTER, avc,
695 ICL_TYPE_INT32, avc->m.Length);
696 hash = DVHash(&avc->fid);
697 avc->truncPos = AFS_NOTRUNC; /* don't truncate later */
698 avc->states &= ~CExtendedFile; /* not any more */
699 ObtainWriteLock(&afs_xcbhash, 459);
700 afs_DequeueCallback(avc);
701 avc->states &= ~(CStatd|CDirty); /* mark status information as bad, too */
702 ReleaseWriteLock(&afs_xcbhash);
703 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
704 osi_dnlc_purgedp(avc);
705 /* Blow away pages; for now, only for Solaris */
706 #if (defined(AFS_SUN5_ENV))
707 if (WriteLocked(&avc->lock))
708 osi_ReleaseVM(avc, (struct AFS_UCRED *)0);
711 * Block out others from screwing with this table; is a read lock
714 if (asetLock) MObtainWriteLock(&afs_xdcache,286);
715 for(index = afs_dvhashTbl[hash]; index != NULLIDX;) {
716 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
717 tdc = afs_GetDSlot(index, 0);
718 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
719 /* same file? we'll zap it */
720 if (afs_indexFlags[index] & IFDataMod) {
721 afs_stats_cmperf.cacheCurrDirtyChunks--;
722 /* don't write it back */
723 afs_indexFlags[index] &= ~IFDataMod;
725 afs_indexFlags[index] &= ~IFAnyPages;
727 if (vType(avc) == VDIR) {
731 lockedPutDCache(tdc);
733 index = afs_dvnextTbl[index];
735 if (asetLock) MReleaseWriteLock(&afs_xdcache);
738 } /*afs_InvalidateAllSegments*/
742 * afs_TruncateAllSegments
745 * Truncate a cache file.
748 * avc : Ptr to vcache entry to truncate.
749 * alen : Number of bytes to make the file.
750 * areq : Ptr to request structure.
753 * Called with avc write-locked; in VFS40 systems, pvnLock is also
756 afs_TruncateAllSegments(avc, alen, areq, acred)
758 register struct vcache *avc;
759 struct vrequest *areq;
760 struct AFS_UCRED *acred;
761 { /*afs_TruncateAllSegments*/
763 register struct dcache *tdc;
764 register afs_int32 code;
765 register afs_int32 index;
768 AFS_STATCNT(afs_TruncateAllSegments);
769 avc->m.Date = osi_Time();
770 if (alen >= avc->m.Length) {
772 * Special speedup since Sun's vm extends the file this way;
773 * we've never written to the file thus we can just set the new
774 * length and avoid the needless calls below.
775 * Also used for ftruncate calls which can extend the file.
776 * To completely minimize the possible extra StoreMini RPC, we really
777 * should keep the ExtendedPos as well and clear this flag if we
778 * truncate below that value before we store the file back.
780 avc->states |= CExtendedFile;
781 avc->m.Length = alen;
782 afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL1, ICL_TYPE_POINTER, avc,
783 ICL_TYPE_INT32, avc->m.Length, ICL_TYPE_INT32, alen);
787 afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL2, ICL_TYPE_POINTER, avc,
788 ICL_TYPE_INT32, avc->m.Length, ICL_TYPE_INT32, alen);
790 #if (defined(AFS_SUN5_ENV))
792 /* Zero unused portion of last page */
793 osi_VM_PreTruncate(avc, alen, acred);
797 #if (defined(AFS_SUN5_ENV))
798 ObtainWriteLock(&avc->vlock, 546);
799 avc->activeV++; /* Block new getpages */
800 ReleaseWriteLock(&avc->vlock);
803 ReleaseWriteLock(&avc->lock);
806 /* Flush pages beyond end-of-file. */
807 osi_VM_Truncate(avc, alen, acred);
810 ObtainWriteLock(&avc->lock,79);
812 avc->m.Length = alen;
814 if (alen < avc->truncPos) avc->truncPos = alen;
815 code = DVHash(&avc->fid);
816 /* block out others from screwing with this table */
817 MObtainWriteLock(&afs_xdcache,287);
818 for(index = afs_dvhashTbl[code]; index != NULLIDX;) {
819 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
820 tdc = afs_GetDSlot(index, 0);
821 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
822 /* same file, and modified, we'll store it back */
823 newSize = alen - AFS_CHUNKTOBASE(tdc->f.chunk);
824 if (newSize < 0) newSize = 0;
825 if (newSize < tdc->f.chunkBytes) {
826 register struct osi_file *tfile;
827 tfile = afs_CFileOpen(tdc->f.inode);
828 afs_CFileTruncate(tfile, newSize);
829 afs_CFileClose(tfile);
830 afs_AdjustSize(tdc, newSize);
833 lockedPutDCache(tdc);
835 index = afs_dvnextTbl[index];
837 #if (defined(AFS_SUN5_ENV))
838 ObtainWriteLock(&avc->vlock, 547);
839 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
840 avc->vstates &= ~VRevokeWait;
841 afs_osi_Wakeup((char *)&avc->vstates);
843 ReleaseWriteLock(&avc->vlock);
845 MReleaseWriteLock(&afs_xdcache);
848 } /*afs_TruncateAllSegments*/