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"
17 #include "afs/sysincludes.h" /*Standard vendor system headers */
18 #include "afsincludes.h" /*AFS-based standard headers */
19 #include "afs/afs_stats.h" /* statistics */
20 #include "afs/afs_cbqueue.h"
21 #include "afs/afs_osidnlc.h"
23 afs_uint32 afs_stampValue = 0;
29 * Send a truncation request to a FileServer.
35 * We're write-locked upon entry.
39 afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
41 register struct afs_conn *tc;
42 struct AFSStoreStatus InStatus;
43 struct AFSFetchStatus OutStatus;
44 struct AFSVolSync tsync;
45 register afs_int32 code;
46 register struct rx_call *tcall;
47 afs_size_t tlen, xlen = 0;
49 AFS_STATCNT(afs_StoreMini);
50 afs_Trace2(afs_iclSetp, CM_TRACE_STOREMINI, ICL_TYPE_POINTER, avc,
51 ICL_TYPE_INT32, avc->f.m.Length);
52 tlen = avc->f.m.Length;
53 if (avc->f.truncPos < tlen)
54 tlen = avc->f.truncPos;
55 avc->f.truncPos = AFS_NOTRUNC;
56 avc->f.states &= ~CExtendedFile;
59 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
61 #ifdef AFS_64BIT_CLIENT
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->f.m.Date;
77 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
78 afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64, ICL_TYPE_FID,
79 &avc->f.fid.Fid, ICL_TYPE_OFFSET,
80 ICL_HANDLE_OFFSET(avc->f.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->f.fid.Fid,
89 &InStatus, avc->f.m.Length,
90 (afs_size_t) 0, tlen);
95 if ((avc->f.m.Length > 0x7fffffff) ||
96 (tlen > 0x7fffffff) ||
97 ((0x7fffffff - tlen) < avc->f.m.Length))
100 StartRXAFS_StoreData(tcall,
101 (struct AFSFid *)&avc->f.fid.Fid,
102 &InStatus, l1, 0, l2);
104 #else /* AFS_64BIT_CLIENT */
106 StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
107 &InStatus, avc->f.m.Length, 0, tlen);
108 #endif /* AFS_64BIT_CLIENT */
110 code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
112 code = rx_EndCall(tcall, code);
115 #ifdef AFS_64BIT_CLIENT
116 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
117 afs_serverSetNo64Bit(tc);
120 #endif /* AFS_64BIT_CLIENT */
124 (tc, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STOREDATA,
128 afs_ProcessFS(avc, &OutStatus, areq);
131 afs_InvalidateAllSegments(avc);
137 unsigned int storeallmissing = 0;
138 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
140 * afs_StoreAllSegments
143 * Stores all modified segments back to server
146 * avc : Pointer to vcache entry.
147 * areq : Pointer to request structure.
150 * Called with avc write-locked.
152 #if defined (AFS_HPUX_ENV)
153 int NCHUNKSATONCE = 3;
155 int NCHUNKSATONCE = 64;
161 afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
164 register struct dcache *tdc;
165 register afs_int32 code = 0;
166 register afs_int32 index;
167 register afs_int32 origCBs, foreign = 0;
169 afs_hyper_t newDV, oldDV; /* DV when we start, and finish, respectively */
170 struct dcache **dcList, **dclist;
171 unsigned int i, j, minj, moredata, high, off;
173 afs_size_t maxStoredLength; /* highest offset we've written to server. */
176 struct afs_stats_xferData *xferP; /* Ptr to this op's xfer struct */
177 osi_timeval_t xferStartTime, /*FS xfer start time */
178 xferStopTime; /*FS xfer stop time */
179 afs_size_t bytesToXfer; /* # bytes to xfer */
180 afs_size_t bytesXferred; /* # bytes actually xferred */
181 #endif /* AFS_NOSTATS */
184 AFS_STATCNT(afs_StoreAllSegments);
186 hset(oldDV, avc->f.m.DataVersion);
187 hset(newDV, avc->f.m.DataVersion);
188 hash = DVHash(&avc->f.fid);
189 foreign = (avc->f.states & CForeign);
190 dcList = (struct dcache **)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
191 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
192 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
193 #if !defined(AFS_AIX32_ENV) && !defined(AFS_SGI65_ENV)
194 /* In the aix vm implementation we need to do the vm_writep even
195 * on the memcache case since that's we adjust the file's size
196 * and finish flushing partial vm pages.
198 if ((cacheDiskType != AFS_FCACHE_TYPE_MEM) || (sync & AFS_LASTSTORE))
199 #endif /* !AFS_AIX32_ENV && !AFS_SGI65_ENV */
201 /* If we're not diskless, reading a file may stress the VM
202 * system enough to cause a pageout, and this vnode would be
203 * locked when the pageout occurs. We can prevent this problem
204 * by making sure all dirty pages are already flushed. We don't
205 * do this when diskless because reading a diskless (i.e.
206 * memory-resident) chunk doesn't require using new VM, and we
207 * also don't want to dump more dirty data into a diskless cache,
208 * since they're smaller, and we might exceed its available
211 #if defined(AFS_SUN5_ENV)
212 if (sync & AFS_VMSYNC_INVAL) /* invalidate VM pages */
213 osi_VM_TryToSmush(avc, CRED(), 1);
216 osi_VM_StoreAllSegments(avc);
218 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
219 /* This will probably make someone sad ... */
220 /*printf("Net down in afs_StoreSegments\n");*/
223 ConvertWToSLock(&avc->lock);
226 * Subsequent code expects a sorted list, and it expects all the
227 * chunks in the list to be contiguous, so we need a sort and a
228 * while loop in here, too - but this will work for a first pass...
229 * 92.10.05 - OK, there's a sort in here now. It's kind of a modified
230 * bin sort, I guess. Chunk numbers start with 0
232 * - Have to get a write lock on xdcache because GetDSlot might need it (if
233 * the chunk doesn't have a dcache struct).
234 * This seems like overkill in most cases.
235 * - I'm not sure that it's safe to do "index = .hvNextp", then unlock
236 * xdcache, then relock xdcache and try to use index. It is done
237 * a lot elsewhere in the CM, but I'm not buying that argument.
238 * - should be able to check IFDataMod without doing the GetDSlot (just
239 * hold afs_xdcache). That way, it's easy to do this without the
240 * writelock on afs_xdcache, and we save unneccessary disk
241 * operations. I don't think that works, 'cuz the next pointers
244 origCBs = afs_allCBs;
247 tlen = avc->f.m.Length;
251 memset((char *)dcList, 0, NCHUNKSATONCE * sizeof(struct dcache *));
255 /* lock and start over from beginning of hash chain
256 * in order to avoid a race condition. */
257 MObtainWriteLock(&afs_xdcache, 284);
258 index = afs_dvhashTbl[hash];
260 for (j = 0; index != NULLIDX;) {
261 if ((afs_indexFlags[index] & IFDataMod)
262 && (afs_indexUnique[index] == avc->f.fid.Fid.Unique)) {
263 tdc = afs_GetDSlot(index, 0); /* refcount+1. */
264 ReleaseReadLock(&tdc->tlock);
265 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && tdc->f.chunk >= minj) {
266 off = tdc->f.chunk - minj;
267 if (off < NCHUNKSATONCE) {
269 osi_Panic("dclist slot already in use!");
274 /* DCLOCKXXX: chunkBytes is protected by tdc->lock which we
275 * can't grab here, due to lock ordering with afs_xdcache.
276 * So, disable this shortcut for now. -- kolya 2001-10-13
278 /* shortcut: big win for little files */
279 /* tlen -= tdc->f.chunkBytes;
286 if (j == NCHUNKSATONCE)
293 index = afs_dvnextTbl[index];
295 MReleaseWriteLock(&afs_xdcache);
297 /* this guy writes chunks, puts back dcache structs, and bumps newDV */
298 /* "moredata" just says "there are more dirty chunks yet to come".
302 static afs_uint32 lp1 = 10000, lp2 = 10000;
304 struct AFSStoreStatus InStatus;
305 struct AFSFetchStatus OutStatus;
307 afs_size_t base, bytes;
310 unsigned int first = 0;
313 struct osi_file *tfile;
314 struct rx_call *tcall;
316 for (bytes = 0, j = 0; !code && j <= high; j++) {
318 ObtainSharedLock(&(dcList[j]->lock), 629);
321 bytes += dcList[j]->f.chunkBytes;
322 if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
323 && (dcList[j]->f.chunk - minj < high)
325 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
329 if (bytes && (j == high || !dcList[j + 1])) {
330 /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
331 base = AFS_CHUNKTOBASE(first + minj);
334 * take a list of dcache structs and send them all off to the server
335 * the list must be in order, and the chunks contiguous.
336 * Note - there is no locking done by this code currently. For
337 * safety's sake, xdcache could be locked over the entire call.
338 * However, that pretty well ties up all the threads. Meantime, all
339 * the chunks _MUST_ have their refcounts bumped.
340 * The writes done before a store back will clear setuid-ness
342 * We can permit CacheStoreProc to wake up the user process IFF we
343 * are doing the last RPC for this close, ie, storing back the last
344 * set of contiguous chunks of a file.
347 dclist = &dcList[first];
348 nchunks = 1 + j - first;
349 nomore = !(moredata || (j != high));
350 InStatus.ClientModTime = avc->f.m.Date;
351 InStatus.Mask = AFS_SETMODTIME;
352 if (sync & AFS_SYNC) {
353 InStatus.Mask |= AFS_FSYNC;
355 tlen = lmin(avc->f.m.Length, avc->f.truncPos);
356 afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
357 ICL_TYPE_FID, &avc->f.fid.Fid, ICL_TYPE_OFFSET,
358 ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
359 ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
360 ICL_HANDLE_OFFSET(tlen));
364 tc = afs_Conn(&avc->f.fid, areq, 0);
366 #ifdef AFS_64BIT_CLIENT
370 tcall = rx_NewCall(tc->id);
371 #ifdef AFS_64BIT_CLIENT
372 if (!afs_serverHasNo64Bit(tc)) {
374 StartRXAFS_StoreData64(tcall,
380 if (tlen > 0xFFFFFFFF) {
383 afs_int32 t1, t2, t3;
388 StartRXAFS_StoreData(tcall,
395 #else /* AFS_64BIT_CLIENT */
397 StartRXAFS_StoreData(tcall,
398 (struct AFSFid *)&avc->
399 f.fid.Fid, &InStatus, base,
401 #endif /* AFS_64BIT_CLIENT */
408 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
409 avc->f.truncPos = AFS_NOTRUNC;
411 for (i = 0; i < nchunks && !code; i++) {
414 afs_warn("afs: missing dcache!\n");
416 continue; /* panic? */
418 afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2,
419 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32,
420 tdc->f.chunk, ICL_TYPE_INT32,
421 tdc->index, ICL_TYPE_INT32,
422 afs_inode2trace(&tdc->f.inode));
425 if (avc->asynchrony == -1) {
426 if (afs_defaultAsynchrony >
428 shouldwake = &nomore;
430 } else if ((afs_uint32) avc->asynchrony >=
432 shouldwake = &nomore;
435 tfile = afs_CFileOpen(&tdc->f.inode);
438 &(afs_stats_cmfullperf.rpc.
440 [AFS_STATS_FS_XFERIDX_STOREDATA]);
441 osi_GetuTime(&xferStartTime);
444 afs_CacheStoreProc(tcall, tfile,
445 tdc->f.chunkBytes, avc,
446 shouldwake, &bytesToXfer,
449 osi_GetuTime(&xferStopTime);
452 (xferP->numSuccesses)++;
453 afs_stats_XferSumBytes
454 [AFS_STATS_FS_XFERIDX_STOREDATA] +=
457 (afs_stats_XferSumBytes
458 [AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
459 afs_stats_XferSumBytes
460 [AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
461 if (bytesXferred < xferP->minBytes)
462 xferP->minBytes = bytesXferred;
463 if (bytesXferred > xferP->maxBytes)
464 xferP->maxBytes = bytesXferred;
467 * Tally the size of the object. Note: we tally the actual size,
468 * NOT the number of bytes that made it out over the wire.
470 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
472 else if (bytesToXfer <=
473 AFS_STATS_MAXBYTES_BUCKET1)
475 else if (bytesToXfer <=
476 AFS_STATS_MAXBYTES_BUCKET2)
478 else if (bytesToXfer <=
479 AFS_STATS_MAXBYTES_BUCKET3)
481 else if (bytesToXfer <=
482 AFS_STATS_MAXBYTES_BUCKET4)
484 else if (bytesToXfer <=
485 AFS_STATS_MAXBYTES_BUCKET5)
487 else if (bytesToXfer <=
488 AFS_STATS_MAXBYTES_BUCKET6)
490 else if (bytesToXfer <=
491 AFS_STATS_MAXBYTES_BUCKET7)
496 afs_stats_GetDiff(elapsedTime, xferStartTime,
498 afs_stats_AddTo((xferP->sumTime),
500 afs_stats_SquareAddTo((xferP->sqrTime),
502 if (afs_stats_TimeLessThan
503 (elapsedTime, (xferP->minTime))) {
504 afs_stats_TimeAssign((xferP->minTime),
507 if (afs_stats_TimeGreaterThan
508 (elapsedTime, (xferP->maxTime))) {
509 afs_stats_TimeAssign((xferP->maxTime),
515 afs_CacheStoreProc(tcall, tfile,
516 tdc->f.chunkBytes, avc,
517 shouldwake, &lp1, &lp2);
518 #endif /* AFS_NOSTATS */
519 afs_CFileClose(tfile);
520 if ((tdc->f.chunkBytes < afs_OtherCSize)
521 && (i < (nchunks - 1)) && code == 0) {
522 int bsent, tlen, sbytes =
523 afs_OtherCSize - tdc->f.chunkBytes;
525 osi_AllocLargeSpace(AFS_LRALLOCSIZ);
530 AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ :
532 memset(tbuffer, 0, tlen);
534 bsent = rx_Write(tcall, tbuffer, tlen);
538 code = -33; /* XXX */
543 osi_FreeLargeSpace(tbuffer);
545 stored += tdc->f.chunkBytes;
547 /* ideally, I'd like to unlock the dcache and turn
548 * off the writing bit here, but that would
549 * require being able to retry StoreAllSegments in
550 * the event of a failure. It only really matters
551 * if user can't read from a 'locked' dcache or
552 * one which has the writing bit turned on. */
555 struct AFSVolSync tsync;
558 EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
563 doProcessFS = 1; /* Flag to run afs_ProcessFS() later on */
568 code2 = rx_EndCall(tcall, code);
573 #ifdef AFS_64BIT_CLIENT
574 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
575 afs_serverSetNo64Bit(tc);
578 #endif /* AFS_64BIT_CLIENT */
580 (tc, code, &avc->f.fid, areq,
581 AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
584 /* put back all remaining locked dcache entries */
585 for (i = 0; i < nchunks; i++) {
588 if (afs_indexFlags[tdc->index] & IFDataMod) {
590 * LOCKXXX -- should hold afs_xdcache(W) when
591 * modifying afs_indexFlags.
593 afs_indexFlags[tdc->index] &= ~IFDataMod;
594 afs_stats_cmperf.cacheCurrDirtyChunks--;
595 afs_indexFlags[tdc->index] &= ~IFDirtyPages;
596 if (sync & AFS_VMSYNC_INVAL) {
597 /* since we have invalidated all the pages of this
598 ** vnode by calling osi_VM_TryToSmush, we can
599 ** safely mark this dcache entry as not having
600 ** any pages. This vnode now becomes eligible for
601 ** reclamation by getDownD.
603 afs_indexFlags[tdc->index] &= ~IFAnyPages;
607 UpgradeSToWLock(&tdc->lock, 628);
608 tdc->f.states &= ~DWriting; /* correct? */
609 tdc->dflags |= DFEntryMod;
610 ReleaseWriteLock(&tdc->lock);
612 /* Mark the entry as released */
617 /* Now copy out return params */
618 UpgradeSToWLock(&avc->lock, 28); /* keep out others for a while */
619 afs_ProcessFS(avc, &OutStatus, areq);
620 /* Keep last (max) size of file on server to see if
621 * we need to call afs_StoreMini to extend the file.
624 maxStoredLength = OutStatus.Length;
625 ConvertWToSLock(&avc->lock);
630 for (j++; j <= high; j++) {
632 ReleaseSharedLock(&(dcList[j]->lock));
633 afs_PutDCache(dcList[j]);
634 /* Releasing entry */
640 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
641 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
646 /* Release any zero-length dcache entries in our interval
647 * that we locked but didn't store back above.
649 for (j = 0; j <= high; j++) {
652 osi_Assert(tdc->f.chunkBytes == 0);
653 ReleaseSharedLock(&tdc->lock);
659 minj += NCHUNKSATONCE;
660 } while (!code && moredata);
662 UpgradeSToWLock(&avc->lock, 29);
664 /* send a trivial truncation store if did nothing else */
667 * Call StoreMini if we haven't written enough data to extend the
668 * file at the fileserver to the client's notion of the file length.
670 if ((avc->f.truncPos != AFS_NOTRUNC)
671 || ((avc->f.states & CExtendedFile)
672 && (maxStoredLength < avc->f.m.Length))) {
673 code = afs_StoreMini(avc, areq);
675 hadd32(newDV, 1); /* just bumped here, too */
677 avc->f.states &= ~CExtendedFile;
681 * Finally, turn off DWriting, turn on DFEntryMod,
682 * update f.versionNo.
683 * A lot of this could be integrated into the loop above
693 memset((char *)dcList, 0,
694 NCHUNKSATONCE * sizeof(struct dcache *));
696 /* overkill, but it gets the lock in case GetDSlot needs it */
697 MObtainWriteLock(&afs_xdcache, 285);
699 for (j = 0, safety = 0, index = afs_dvhashTbl[hash];
700 index != NULLIDX && safety < afs_cacheFiles + 2;) {
702 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
703 tdc = afs_GetDSlot(index, 0);
704 ReleaseReadLock(&tdc->tlock);
706 if (!FidCmp(&tdc->f.fid, &avc->f.fid)
707 && tdc->f.chunk >= minj) {
708 off = tdc->f.chunk - minj;
709 if (off < NCHUNKSATONCE) {
710 /* this is the file, and the correct chunk range */
711 if (j >= NCHUNKSATONCE)
713 ("Too many dcache entries in range\n");
718 if (j == NCHUNKSATONCE)
726 index = afs_dvnextTbl[index];
728 MReleaseWriteLock(&afs_xdcache);
730 for (i = 0; i < j; i++) {
731 /* Iterate over the dcache entries we collected above */
733 ObtainSharedLock(&tdc->lock, 677);
735 /* was code here to clear IFDataMod, but it should only be done
736 * in storedcache and storealldcache.
738 /* Only increase DV if we had up-to-date data to start with.
739 * Otherwise, we could be falsely upgrading an old chunk
740 * (that we never read) into one labelled with the current
741 * DV #. Also note that we check that no intervening stores
742 * occurred, otherwise we might mislabel cache information
743 * for a chunk that we didn't store this time
745 /* Don't update the version number if it's not yet set. */
746 if (!hsame(tdc->f.versionNo, h_unset)
747 && hcmp(tdc->f.versionNo, oldDV) >= 0) {
749 if ((!(afs_dvhack || foreign)
750 && hsame(avc->f.m.DataVersion, newDV))
751 || ((afs_dvhack || foreign)
752 && (origCBs == afs_allCBs))) {
753 /* no error, this is the DV */
755 UpgradeSToWLock(&tdc->lock, 678);
756 hset(tdc->f.versionNo, avc->f.m.DataVersion);
757 tdc->dflags |= DFEntryMod;
758 ConvertWToSLock(&tdc->lock);
762 ReleaseSharedLock(&tdc->lock);
766 minj += NCHUNKSATONCE;
773 * Invalidate chunks after an error for ccores files since
774 * afs_inactive won't be called for these and they won't be
775 * invalidated. Also discard data if it's a permanent error from the
778 if (areq->permWriteError || (avc->f.states & (CCore1 | CCore))) {
779 afs_InvalidateAllSegments(avc);
782 afs_Trace3(afs_iclSetp, CM_TRACE_STOREALLDONE, ICL_TYPE_POINTER, avc,
783 ICL_TYPE_INT32, avc->f.m.Length, ICL_TYPE_INT32, code);
784 /* would like a Trace5, but it doesn't exist... */
785 afs_Trace3(afs_iclSetp, CM_TRACE_AVCLOCKER, ICL_TYPE_POINTER, avc,
786 ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
787 avc->lock.excl_locked);
788 afs_Trace4(afs_iclSetp, CM_TRACE_AVCLOCKEE, ICL_TYPE_POINTER, avc,
789 ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
790 avc->lock.readers_reading, ICL_TYPE_INT32,
791 avc->lock.num_waiting);
794 * Finally, if updated DataVersion matches newDV, we did all of the
795 * stores. If mapDV indicates that the page cache was flushed up
796 * to when we started the store, then we can relabel them as flushed
797 * as recently as newDV.
798 * Turn off CDirty bit because the stored data is now in sync with server.
800 if (code == 0 && hcmp(avc->mapDV, oldDV) >= 0) {
801 if ((!(afs_dvhack || foreign) && hsame(avc->f.m.DataVersion, newDV))
802 || ((afs_dvhack || foreign) && (origCBs == afs_allCBs))) {
803 hset(avc->mapDV, newDV);
804 avc->f.states &= ~CDirty;
807 osi_FreeLargeSpace(dcList);
809 /* If not the final write a temporary error is ok. */
810 if (code && !areq->permWriteError && !(sync & AFS_LASTSTORE))
815 } /*afs_StoreAllSegments (new 03/02/94) */
819 * afs_InvalidateAllSegments
822 * Invalidates all chunks for a given file
825 * avc : Pointer to vcache entry.
828 * For example, called after an error has been detected. Called
829 * with avc write-locked, and afs_xdcache unheld.
833 afs_InvalidateAllSegments(struct vcache *avc)
838 struct dcache **dcList;
839 int i, dcListMax, dcListCount;
841 AFS_STATCNT(afs_InvalidateAllSegments);
842 afs_Trace2(afs_iclSetp, CM_TRACE_INVALL, ICL_TYPE_POINTER, avc,
843 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
844 hash = DVHash(&avc->f.fid);
845 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate later */
846 avc->f.states &= ~CExtendedFile; /* not any more */
847 ObtainWriteLock(&afs_xcbhash, 459);
848 afs_DequeueCallback(avc);
849 avc->f.states &= ~(CStatd | CDirty); /* mark status information as bad, too */
850 ReleaseWriteLock(&afs_xcbhash);
851 if (avc->f.fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
852 osi_dnlc_purgedp(avc);
853 /* Blow away pages; for now, only for Solaris */
854 #if (defined(AFS_SUN5_ENV))
855 if (WriteLocked(&avc->lock))
856 osi_ReleaseVM(avc, (struct AFS_UCRED *)0);
859 * Block out others from screwing with this table; is a read lock
862 MObtainWriteLock(&afs_xdcache, 286);
865 for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
866 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
867 tdc = afs_GetDSlot(index, 0);
868 ReleaseReadLock(&tdc->tlock);
869 if (!FidCmp(&tdc->f.fid, &avc->f.fid))
873 index = afs_dvnextTbl[index];
876 dcList = osi_Alloc(dcListMax * sizeof(struct dcache *));
879 for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
880 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
881 tdc = afs_GetDSlot(index, 0);
882 ReleaseReadLock(&tdc->tlock);
883 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
884 /* same file? we'll zap it */
885 if (afs_indexFlags[index] & IFDataMod) {
886 afs_stats_cmperf.cacheCurrDirtyChunks--;
887 /* don't write it back */
888 afs_indexFlags[index] &= ~IFDataMod;
890 afs_indexFlags[index] &= ~IFAnyPages;
891 if (dcListCount < dcListMax)
892 dcList[dcListCount++] = tdc;
899 index = afs_dvnextTbl[index];
901 MReleaseWriteLock(&afs_xdcache);
903 for (i = 0; i < dcListCount; i++) {
906 ObtainWriteLock(&tdc->lock, 679);
908 if (vType(avc) == VDIR)
910 ReleaseWriteLock(&tdc->lock);
914 osi_Free(dcList, dcListMax * sizeof(struct dcache *));
921 * Extend a cache file
923 * \param avc pointer to vcache to extend data for
924 * \param alen Length to extend file to
927 * \note avc must be write locked. May release and reobtain avc and GLOCK
930 afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq) {
931 afs_size_t offset, toAdd;
932 struct osi_file *tfile;
937 zeros = (void *) afs_osi_Alloc(AFS_PAGESIZE);
940 memset(zeros, 0, AFS_PAGESIZE);
942 while (avc->f.m.Length < alen) {
943 tdc = afs_ObtainDCacheForWriting(avc, avc->f.m.Length, alen - avc->f.m.Length, areq, 0);
949 toAdd = alen - avc->f.m.Length;
951 offset = avc->f.m.Length - AFS_CHUNKTOBASE(tdc->f.chunk);
952 if (offset + toAdd > AFS_CHUNKTOSIZE(tdc->f.chunk)) {
953 toAdd = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset;
955 tfile = afs_CFileOpen(&tdc->f.inode);
956 while(tdc->validPos < avc->f.m.Length + toAdd) {
959 towrite = (avc->f.m.Length + toAdd) - tdc->validPos;
960 if (towrite > AFS_PAGESIZE) towrite = AFS_PAGESIZE;
962 code = afs_CFileWrite(tfile,
963 tdc->validPos - AFS_CHUNKTOBASE(tdc->f.chunk),
965 tdc->validPos += towrite;
967 afs_CFileClose(tfile);
968 afs_AdjustSize(tdc, offset + toAdd );
969 avc->f.m.Length += toAdd;
970 ReleaseWriteLock(&tdc->lock);
974 afs_osi_Free(zeros, AFS_PAGESIZE);
979 * afs_TruncateAllSegments
982 * Truncate a cache file.
985 * avc : Ptr to vcache entry to truncate.
986 * alen : Number of bytes to make the file.
987 * areq : Ptr to request structure.
990 * Called with avc write-locked; in VFS40 systems, pvnLock is also
994 afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
995 struct vrequest *areq, struct AFS_UCRED *acred)
997 register struct dcache *tdc;
998 register afs_int32 code;
999 register afs_int32 index;
1003 struct dcache **tdcArray;
1005 AFS_STATCNT(afs_TruncateAllSegments);
1006 avc->f.m.Date = osi_Time();
1007 afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL, ICL_TYPE_POINTER, avc,
1008 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1009 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(alen));
1010 if (alen >= avc->f.m.Length) {
1012 * Special speedup since Sun's vm extends the file this way;
1013 * we've never written to the file thus we can just set the new
1014 * length and avoid the needless calls below.
1015 * Also used for ftruncate calls which can extend the file.
1016 * To completely minimize the possible extra StoreMini RPC, we really
1017 * should keep the ExtendedPos as well and clear this flag if we
1018 * truncate below that value before we store the file back.
1020 avc->f.states |= CExtendedFile;
1021 avc->f.m.Length = alen;
1024 #if (defined(AFS_SUN5_ENV))
1026 /* Zero unused portion of last page */
1027 osi_VM_PreTruncate(avc, alen, acred);
1031 #if (defined(AFS_SUN5_ENV))
1032 ObtainWriteLock(&avc->vlock, 546);
1033 avc->activeV++; /* Block new getpages */
1034 ReleaseWriteLock(&avc->vlock);
1037 ReleaseWriteLock(&avc->lock);
1040 /* Flush pages beyond end-of-file. */
1041 osi_VM_Truncate(avc, alen, acred);
1044 ObtainWriteLock(&avc->lock, 79);
1046 avc->f.m.Length = alen;
1048 if (alen < avc->f.truncPos)
1049 avc->f.truncPos = alen;
1050 code = DVHash(&avc->f.fid);
1052 /* block out others from screwing with this table */
1053 MObtainWriteLock(&afs_xdcache, 287);
1056 for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
1057 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1058 tdc = afs_GetDSlot(index, 0);
1059 ReleaseReadLock(&tdc->tlock);
1060 if (!FidCmp(&tdc->f.fid, &avc->f.fid))
1064 index = afs_dvnextTbl[index];
1067 /* Now allocate space where we can save those dcache entries, and
1068 * do a second pass over them.. Since we're holding xdcache, it
1069 * shouldn't be changing.
1071 tdcArray = osi_Alloc(dcCount * sizeof(struct dcache *));
1074 for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
1075 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1076 tdc = afs_GetDSlot(index, 0);
1077 ReleaseReadLock(&tdc->tlock);
1078 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1079 /* same file, and modified, we'll store it back */
1080 if (dcPos < dcCount) {
1081 tdcArray[dcPos++] = tdc;
1089 index = afs_dvnextTbl[index];
1092 MReleaseWriteLock(&afs_xdcache);
1094 /* Now we loop over the array of dcache entries and truncate them */
1095 for (index = 0; index < dcPos; index++) {
1096 struct osi_file *tfile;
1098 tdc = tdcArray[index];
1100 newSize = alen - AFS_CHUNKTOBASE(tdc->f.chunk);
1103 ObtainSharedLock(&tdc->lock, 672);
1104 if (newSize < tdc->f.chunkBytes) {
1105 UpgradeSToWLock(&tdc->lock, 673);
1106 tfile = afs_CFileOpen(&tdc->f.inode);
1107 afs_CFileTruncate(tfile, newSize);
1108 afs_CFileClose(tfile);
1109 afs_AdjustSize(tdc, newSize);
1110 if (alen < tdc->validPos) {
1111 if (alen < AFS_CHUNKTOBASE(tdc->f.chunk))
1114 tdc->validPos = alen;
1116 ConvertWToSLock(&tdc->lock);
1118 ReleaseSharedLock(&tdc->lock);
1122 osi_Free(tdcArray, dcCount * sizeof(struct dcache *));
1124 #if (defined(AFS_SUN5_ENV))
1125 ObtainWriteLock(&avc->vlock, 547);
1126 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1127 avc->vstates &= ~VRevokeWait;
1128 afs_osi_Wakeup((char *)&avc->vstates);
1130 ReleaseWriteLock(&avc->vlock);