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
10 #include <afs/param.h>
25 osi_mutex_t cm_bufGetMutex;
27 /* functions called back from the buffer package when reading or writing data,
28 * or when holding or releasing a vnode pointer.
30 long cm_BufWrite(void *vfidp, osi_hyper_t *offsetp, long length, long flags,
31 cm_user_t *userp, cm_req_t *reqp)
33 /* store the data back from this buffer; the buffer is locked and held,
34 * but the vnode involved isn't locked, yet. It is held by its
35 * reference from the buffer, which won't change until the buffer is
36 * released by our caller. Thus, we don't have to worry about holding
40 cm_fid_t *fidp = vfidp;
44 AFSFetchStatus outStatus;
45 AFSStoreStatus inStatus;
49 struct rx_call *callp;
56 cm_bulkIO_t biod; /* bulk IO descriptor */
58 osi_assert(userp != NULL);
60 /* now, the buffer may or may not be filled with good data (buf_GetNew
61 * drops lots of locks, and may indeed return a properly initialized
62 * buffer, although more likely it will just return a new, empty, buffer.
64 scp = cm_FindSCache(fidp);
66 return CM_ERROR_NOSUCHFILE; /* shouldn't happen */
68 cm_AFSFidFromFid(&tfid, fidp);
70 lock_ObtainMutex(&scp->mx);
72 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
74 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
75 lock_ReleaseMutex(&scp->mx);
76 cm_ReleaseSCache(scp);
80 if (biod.length == 0) {
81 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
82 lock_ReleaseMutex(&scp->mx);
83 cm_ReleaseBIOD(&biod, 1); /* should be a NOOP */
84 cm_ReleaseSCache(scp);
88 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
89 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
91 /* prepare the output status for the store */
92 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
93 cm_StatusFromAttr(&inStatus, scp, NULL);
94 truncPos = scp->length.LowPart;
95 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
96 && scp->truncPos.LowPart < (unsigned long) truncPos)
97 truncPos = scp->truncPos.LowPart;
98 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
100 /* compute how many bytes to write from this buffer */
101 thyper = LargeIntegerSubtract(scp->length, biod.offset);
102 if (LargeIntegerLessThanZero(thyper)) {
103 /* entire buffer is past EOF */
107 /* otherwise write out part of buffer before EOF, but not
108 * more than bufferSize bytes.
110 nbytes = thyper.LowPart;
111 if (nbytes > biod.length) nbytes = biod.length;
114 lock_ReleaseMutex(&scp->mx);
116 /* now we're ready to do the store operation */
118 code = cm_Conn(&scp->fid, userp, reqp, &connp);
121 callp = rx_NewCall(connp->callp);
123 osi_Log3(afsd_logp, "CALL StoreData vp %x, off 0x%x, size 0x%x",
124 (long) scp, biod.offset.LowPart, nbytes);
126 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
127 biod.offset.LowPart, nbytes, truncPos);
130 /* write the data from the the list of buffers */
134 qdp = biod.bufListEndp;
136 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
137 osi_assert(qdp != NULL);
138 bufp = osi_GetQData(qdp);
139 bufferp = bufp->datap;
141 if (wbytes > buf_bufferSize) wbytes = buf_bufferSize;
143 /* write out wbytes of data from bufferp */
144 temp = rx_Write(callp, bufferp, wbytes);
145 if (temp != wbytes) {
150 } /* while more bytes to write */
151 } /* if RPC started successfully */
154 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
155 code = rx_EndCall(callp, code);
156 osi_Log0(afsd_logp, "CALL StoreData DONE");
158 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, code));
159 code = cm_MapRPCError(code, reqp);
161 /* now, clean up our state */
162 lock_ObtainMutex(&scp->mx);
164 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
167 /* now, here's something a little tricky: in AFS 3, a dirty
168 * length can't be directly stored, instead, a dirty chunk is
169 * stored that sets the file's size (by writing and by using
170 * the truncate-first option in the store call).
172 * At this point, we've just finished a store, and so the trunc
173 * pos field is clean. If the file's size at the server is at
174 * least as big as we think it should be, then we turn off the
175 * length dirty bit, since all the other dirty buffers must
176 * precede this one in the file.
178 * The file's desired size shouldn't be smaller than what's
179 * stored at the server now, since we just did the trunc pos
182 * We have to turn off the length dirty bit as soon as we can,
183 * so that we see updates made by other machines.
185 if (outStatus.Length >= scp->length.LowPart)
186 scp->mask &= ~CM_SCACHEMASK_LENGTH;
187 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
189 if (code == CM_ERROR_SPACE)
190 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
191 else if (code == CM_ERROR_QUOTA)
192 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
194 lock_ReleaseMutex(&scp->mx);
195 cm_ReleaseBIOD(&biod, 1);
196 cm_ReleaseSCache(scp);
202 * Truncate the file, by sending a StoreData RPC with zero length.
204 * Called with scp locked. Releases and re-obtains the lock.
206 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
208 AFSFetchStatus outStatus;
209 AFSStoreStatus inStatus;
215 struct rx_call *callp;
217 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
218 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
219 CM_SCACHESYNC_STOREDATA_EXCL);
221 /* prepare the output status for the store */
222 inStatus.Mask = AFS_SETMODTIME;
223 inStatus.ClientModTime = scp->clientModTime;
224 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
226 /* calculate truncation position */
227 truncPos = scp->length.LowPart;
228 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
229 && scp->truncPos.LowPart < (unsigned long) truncPos)
230 truncPos = scp->truncPos.LowPart;
231 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
233 lock_ReleaseMutex(&scp->mx);
235 cm_AFSFidFromFid(&tfid, &scp->fid);
237 /* now we're ready to do the store operation */
239 code = cm_Conn(&scp->fid, userp, reqp, &connp);
242 callp = rx_NewCall(connp->callp);
244 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
248 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
249 code = rx_EndCall(callp, code);
251 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, code));
252 code = cm_MapRPCError(code, reqp);
254 /* now, clean up our state */
255 lock_ObtainMutex(&scp->mx);
257 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
261 * For explanation of handling of CM_SCACHEMASK_LENGTH,
264 if (outStatus.Length >= scp->length.LowPart)
265 scp->mask &= ~CM_SCACHEMASK_LENGTH;
266 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
272 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
274 *bytesReadp = buf_bufferSize;
276 /* now return a code that means that I/O is done */
280 /* stabilize scache entry, and return with it locked so
283 long cm_BufStabilize(void *parmp, cm_user_t *userp, cm_req_t *reqp)
290 lock_ObtainMutex(&scp->mx);
291 code = cm_SyncOp(scp, NULL, userp, reqp,
292 0, CM_SCACHESYNC_NEEDCALLBACK
293 | CM_SCACHESYNC_GETSTATUS
294 | CM_SCACHESYNC_SETSIZE);
296 lock_ReleaseMutex(&scp->mx);
303 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
304 long cm_BufUnstabilize(void *parmp, cm_user_t *userp)
310 lock_ReleaseMutex(&scp->mx);
312 /* always succeeds */
316 cm_buf_ops_t cm_bufOps = {
323 int cm_InitDCache(long chunkSize, long nbuffers)
325 lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
326 if (nbuffers) buf_nbuffers = nbuffers;
327 return buf_Init(&cm_bufOps);
330 /* check to see if we have an up-to-date buffer. The buffer must have
331 * previously been obtained by calling buf_Get.
333 * Make sure we have a callback, and that the dataversion matches.
335 * Scp must be locked.
337 * Bufp *may* be locked.
339 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
342 if (!cm_HaveCallback(scp))
345 & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
346 == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
348 if (bufp->dataVersion == scp->dataVersion)
351 code = lock_TryMutex(&bufp->mx);
353 /* don't have the lock, and can't lock it, then
360 /* remember dirty flag for later */
361 code = bufp->flags & CM_BUF_DIRTY;
363 /* release lock if we obtained it here */
364 if (!isBufLocked) lock_ReleaseMutex(&bufp->mx);
366 /* if buffer was dirty, buffer is acceptable for use */
371 /* used when deciding whether to do a prefetch or not */
372 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, long length,
373 cm_user_t *up, cm_req_t *reqp, osi_hyper_t *realBasep)
381 /* now scan all buffers in the range, looking for any that look like
386 lock_ObtainMutex(&scp->mx);
388 /* get callback so we can do a meaningful dataVersion comparison */
389 code = cm_SyncOp(scp, NULL, up, reqp, 0,
390 CM_SCACHESYNC_NEEDCALLBACK
391 | CM_SCACHESYNC_GETSTATUS);
393 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
394 lock_ReleaseMutex(&scp->mx);
398 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
399 /* we're past the end of file */
403 bp = buf_Find(scp, &tbase);
404 /* We cheat slightly by not locking the bp mutex. */
407 & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
408 && bp->dataVersion != scp->dataVersion)
414 /* if this buffer is essentially guaranteed to require a fetch,
415 * break out here and return this position.
419 toffset.LowPart = buf_bufferSize;
420 toffset.HighPart = 0;
421 tbase = LargeIntegerAdd(toffset, tbase);
422 length -= buf_bufferSize;
425 /* if we get here, either everything is fine or stop stopped us at a
426 * particular buffer in the range that definitely needs to be fetched.
429 /* return non-zero code since realBasep won't be valid */
430 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
434 /* successfully found a page that will need fetching */
438 lock_ReleaseMutex(&scp->mx);
442 void cm_BkgStore(cm_scache_t *scp, long p1, long p2, long p3, long p4,
450 req.flags |= CM_REQ_NORETRY;
452 toffset.LowPart = p1;
453 toffset.HighPart = p2;
456 osi_Log2(afsd_logp, "Starting BKG store vp 0x%x, base 0x%x", scp, p1);
458 cm_BufWrite(&scp->fid, &toffset, length, /* flags */ 0, userp, &req);
460 lock_ObtainMutex(&scp->mx);
461 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
462 lock_ReleaseMutex(&scp->mx);
465 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base)
470 thyper.LowPart = cm_chunkSize;
472 thyper = LargeIntegerAdd(*base, thyper);
473 thyper.LowPart &= (-cm_chunkSize);
474 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
475 scp->prefetch.base = *base;
476 if (LargeIntegerGreaterThan(thyper, scp->prefetch.end))
477 scp->prefetch.end = thyper;
479 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
482 /* do the prefetch */
483 void cm_BkgPrefetch(cm_scache_t *scp, long p1, long p2, long p3, long p4,
490 int cpff = 0; /* cleared prefetch flag */
494 req.flags |= CM_REQ_NORETRY;
500 osi_Log2(afsd_logp, "Starting BKG prefetch vp 0x%x, base 0x%x", scp, p1);
502 code = buf_Get(scp, &base, &bp);
504 lock_ObtainMutex(&scp->mx);
506 if (code || (bp->cmFlags & CM_BUF_CMFETCHING)) {
507 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
508 lock_ReleaseMutex(&scp->mx);
512 code = cm_GetBuffer(scp, bp, &cpff, userp, &req);
513 if (!cpff) cm_ClearPrefetchFlag(code, scp, &base);
514 lock_ReleaseMutex(&scp->mx);
519 /* a read was issued to offsetp, and we have to determine whether we should
522 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
523 cm_user_t *userp, cm_req_t *reqp)
526 osi_hyper_t realBase;
527 osi_hyper_t readBase;
530 /* round up to chunk boundary */
531 readBase.LowPart += (cm_chunkSize-1);
532 readBase.LowPart &= (-cm_chunkSize);
534 lock_ObtainMutex(&scp->mx);
535 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
536 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
537 lock_ReleaseMutex(&scp->mx);
540 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
542 /* start the scan at the latter of the end of this read or
543 * the end of the last fetched region.
545 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
546 readBase = scp->prefetch.end;
548 lock_ReleaseMutex(&scp->mx);
551 code = cm_CheckFetchRange(scp, &readBase, cm_chunkSize, userp, reqp,
553 if (code) return; /* can't find something to prefetch */
555 osi_Log2(afsd_logp, "BKG Prefetch request vp 0x%x, base 0x%x",
556 scp, realBase.LowPart);
558 cm_QueueBKGRequest(scp, cm_BkgPrefetch, realBase.LowPart,
559 realBase.HighPart, cm_chunkSize, 0, userp);
562 /* scp must be locked; temporarily unlocked during processing.
563 * If returns 0, returns buffers held in biop, and with
564 * CM_BUF_CMSTORING set.
566 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
567 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
568 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
569 * must be woken, and the event must be set when the I/O is done. All of this
570 * is required so that buf_WaitIO synchronizes properly with the buffer as it
571 * is being written out.
573 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
574 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
577 osi_queueData_t *qdp;
580 osi_hyper_t scanStart; /* where to start scan for dirty pages */
581 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
582 osi_hyper_t firstModOffset; /* offset of first modified page in range */
585 long flags; /* flags to cm_SyncOp */
587 /* clear things out */
588 biop->scp = scp; /* don't hold */
589 biop->offset = *inOffsetp;
591 biop->bufListp = NULL;
592 biop->bufListEndp = NULL;
595 /* reserve a chunk's worth of buffers */
596 lock_ReleaseMutex(&scp->mx);
597 buf_ReserveBuffers(cm_chunkSize / buf_bufferSize);
598 lock_ObtainMutex(&scp->mx);
601 for(temp = 0; temp < inSize; temp += buf_bufferSize, bufp = NULL) {
603 thyper.LowPart = temp;
604 tbase = LargeIntegerAdd(*inOffsetp, thyper);
606 bufp = buf_Find(scp, &tbase);
608 /* get buffer mutex and scp mutex safely */
609 lock_ReleaseMutex(&scp->mx);
610 lock_ObtainMutex(&bufp->mx);
611 lock_ObtainMutex(&scp->mx);
613 flags = CM_SCACHESYNC_NEEDCALLBACK
614 | CM_SCACHESYNC_GETSTATUS
615 | CM_SCACHESYNC_STOREDATA
616 | CM_SCACHESYNC_BUFLOCKED;
617 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
619 lock_ReleaseMutex(&bufp->mx);
621 buf_UnreserveBuffers(cm_chunkSize
626 /* if the buffer is dirty, we're done */
627 if (bufp->flags & CM_BUF_DIRTY) {
628 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
629 "WRITING w/o CMSTORING in SetupStoreBIOD");
630 bufp->flags |= CM_BUF_WRITING;
634 /* this buffer is clean, so there's no reason to process it */
635 cm_SyncOpDone(scp, bufp, flags);
636 lock_ReleaseMutex(&bufp->mx);
643 /* if we get here, if bufp is null, we didn't find any dirty buffers
644 * that weren't already being stored back, so we just quit now.
650 /* don't need buffer mutex any more */
651 lock_ReleaseMutex(&bufp->mx);
653 /* put this element in the list */
655 osi_SetQData(qdp, bufp);
656 /* don't have to hold bufp, since held by buf_Find above */
657 osi_QAddH((osi_queue_t **) &biop->bufListp,
658 (osi_queue_t **) &biop->bufListEndp,
660 biop->length = buf_bufferSize;
661 firstModOffset = bufp->offset;
662 biop->offset = firstModOffset;
664 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
665 scanStart = *inOffsetp;
666 scanStart.LowPart &= (-cm_chunkSize);
667 thyper.LowPart = cm_chunkSize;
669 scanEnd = LargeIntegerAdd(scanStart, thyper);
671 flags = CM_SCACHESYNC_NEEDCALLBACK
672 | CM_SCACHESYNC_GETSTATUS
673 | CM_SCACHESYNC_STOREDATA
674 | CM_SCACHESYNC_BUFLOCKED
675 | CM_SCACHESYNC_NOWAIT;
677 /* start by looking backwards until scanStart */
678 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
679 thyper.LowPart = buf_bufferSize;
680 tbase = LargeIntegerSubtract(firstModOffset, thyper);
681 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
682 /* see if we can find the buffer */
683 bufp = buf_Find(scp, &tbase);
686 /* try to lock it, and quit if we can't (simplifies locking) */
687 code = lock_TryMutex(&bufp->mx);
693 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
695 lock_ReleaseMutex(&bufp->mx);
700 if (!(bufp->flags & CM_BUF_DIRTY)) {
701 /* buffer is clean, so we shouldn't add it */
702 cm_SyncOpDone(scp, bufp, flags);
703 lock_ReleaseMutex(&bufp->mx);
708 /* don't need buffer mutex any more */
709 lock_ReleaseMutex(&bufp->mx);
711 /* we have a dirty buffer ready for storing. Add it to the tail
712 * of the list, since it immediately precedes all of the disk
713 * addresses we've already collected.
716 osi_SetQData(qdp, bufp);
717 /* no buf_hold necessary, since we have it held from buf_Find */
718 osi_QAddT((osi_queue_t **) &biop->bufListp,
719 (osi_queue_t **) &biop->bufListEndp,
722 /* update biod info describing the transfer */
723 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
724 biop->length += buf_bufferSize;
726 /* update loop pointer */
727 tbase = LargeIntegerSubtract(tbase, thyper);
728 } /* while loop looking for pages preceding the one we found */
730 /* now, find later dirty, contiguous pages, and add them to the list */
731 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
732 thyper.LowPart = buf_bufferSize;
733 tbase = LargeIntegerAdd(firstModOffset, thyper);
734 while(LargeIntegerLessThan(tbase, scanEnd)) {
735 /* see if we can find the buffer */
736 bufp = buf_Find(scp, &tbase);
739 /* try to lock it, and quit if we can't (simplifies locking) */
740 code = lock_TryMutex(&bufp->mx);
746 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
748 lock_ReleaseMutex(&bufp->mx);
753 if (!(bufp->flags & CM_BUF_DIRTY)) {
754 /* buffer is clean, so we shouldn't add it */
755 cm_SyncOpDone(scp, bufp, flags);
756 lock_ReleaseMutex(&bufp->mx);
761 /* don't need buffer mutex any more */
762 lock_ReleaseMutex(&bufp->mx);
764 /* we have a dirty buffer ready for storing. Add it to the head
765 * of the list, since it immediately follows all of the disk
766 * addresses we've already collected.
769 osi_SetQData(qdp, bufp);
770 /* no buf_hold necessary, since we have it held from buf_Find */
771 osi_QAddH((osi_queue_t **) &biop->bufListp,
772 (osi_queue_t **) &biop->bufListEndp,
775 /* update biod info describing the transfer */
776 biop->length += buf_bufferSize;
778 /* update loop pointer */
779 tbase = LargeIntegerAdd(tbase, thyper);
780 } /* while loop looking for pages following the first page we found */
782 /* finally, we're done */
786 /* scp must be locked; temporarily unlocked during processing.
787 * If returns 0, returns buffers held in biop, and with
788 * CM_BUF_CMFETCHING flags set.
789 * If an error is returned, we don't return any buffers.
791 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
792 cm_bulkIO_t *biop, cm_user_t *up, cm_req_t *reqp)
796 osi_hyper_t toffset; /* a long long temp variable */
797 osi_hyper_t pageBase; /* base offset we're looking at */
798 osi_queueData_t *qdp; /* one temp queue structure */
799 osi_queueData_t *tqdp; /* another temp queue structure */
800 long collected; /* how many bytes have been collected */
803 osi_hyper_t fileSize; /* the # of bytes in the file */
804 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
805 osi_queueData_t *heldBufListEndp; /* first one */
809 biop->offset = *offsetp;
810 /* null out the list of buffers */
811 biop->bufListp = biop->bufListEndp = NULL;
814 /* first lookup the file's length, so we know when to stop */
815 code = cm_SyncOp(scp, NULL, up, reqp, 0, CM_SCACHESYNC_NEEDCALLBACK
816 | CM_SCACHESYNC_GETSTATUS);
817 if (code) return code;
819 /* copy out size, since it may change */
820 fileSize = scp->serverLength;
822 lock_ReleaseMutex(&scp->mx);
825 collected = pageBase.LowPart & (cm_chunkSize - 1);
827 heldBufListEndp = NULL;
830 * Obtaining buffers can cause dirty buffers to be recycled, which
831 * can cause a storeback, so cannot be done while we have buffers
834 * To get around this, we get buffers twice. Before reserving buffers,
835 * we obtain and release each one individually. After reserving
836 * buffers, we try to obtain them again, but only by lookup, not by
837 * recycling. If a buffer has gone away while we were waiting for
838 * the others, we just use whatever buffers we already have.
840 * On entry to this function, we are already holding a buffer, so we
841 * can't wait for reservation. So we call buf_TryReserveBuffers()
842 * instead. Not only that, we can't really even call buf_Get(), for
843 * the same reason. We can't avoid that, though. To avoid deadlock
844 * we allow only one thread to be executing the buf_Get()-buf_Release()
845 * sequence at a time.
848 /* first hold all buffers, since we can't hold any locks in buf_Get */
850 /* stop at chunk boundary */
851 if (collected >= cm_chunkSize) break;
853 /* see if the next page would be past EOF */
854 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) break;
856 lock_ObtainMutex(&cm_bufGetMutex);
858 code = buf_Get(scp, &pageBase, &tbp);
860 lock_ReleaseMutex(&cm_bufGetMutex);
861 lock_ObtainMutex(&scp->mx);
867 lock_ReleaseMutex(&cm_bufGetMutex);
869 toffset.HighPart = 0;
870 toffset.LowPart = buf_bufferSize;
871 pageBase = LargeIntegerAdd(toffset, pageBase);
872 collected += buf_bufferSize;
875 /* reserve a chunk's worth of buffers if possible */
876 reserving = buf_TryReserveBuffers(cm_chunkSize / buf_bufferSize);
879 collected = pageBase.LowPart & (cm_chunkSize - 1);
881 /* now hold all buffers, if they are still there */
883 /* stop at chunk boundary */
884 if (collected >= cm_chunkSize) break;
886 /* see if the next page would be past EOF */
887 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) break;
889 tbp = buf_Find(scp, &pageBase);
892 /* add the buffer to the list */
894 osi_SetQData(qdp, tbp);
895 osi_QAdd((osi_queue_t **)&heldBufListp, &qdp->q);
896 if (!heldBufListEndp) heldBufListEndp = qdp;
897 /* leave tbp held (from buf_Get) */
899 if (!reserving) break;
901 collected += buf_bufferSize;
902 toffset.HighPart = 0;
903 toffset.LowPart = buf_bufferSize;
904 pageBase = LargeIntegerAdd(toffset, pageBase);
907 /* look at each buffer, adding it into the list if it looks idle and
908 * filled with old data. One special case: wait for idle if it is the
909 * first buffer since we really need that one for our caller to make
913 collected = 0; /* now count how many we'll really use */
914 for(tqdp = heldBufListEndp;
916 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
917 /* get a ptr to the held buffer */
918 tbp = osi_GetQData(tqdp);
919 pageBase = tbp->offset;
921 /* now lock the buffer lock */
922 lock_ObtainMutex(&tbp->mx);
923 lock_ObtainMutex(&scp->mx);
925 /* don't bother fetching over data that is already current */
926 if (tbp->dataVersion == scp->dataVersion) {
927 /* we don't need this buffer, since it is current */
928 lock_ReleaseMutex(&scp->mx);
929 lock_ReleaseMutex(&tbp->mx);
933 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_FETCHDATA
934 | CM_SCACHESYNC_BUFLOCKED;
935 if (!isFirst) flags |= CM_SCACHESYNC_NOWAIT;
937 /* wait for the buffer to serialize, if required. Doesn't
938 * release the scp or buffer lock(s) if NOWAIT is specified.
940 code = cm_SyncOp(scp, tbp, up, reqp, 0, flags);
942 lock_ReleaseMutex(&scp->mx);
943 lock_ReleaseMutex(&tbp->mx);
947 /* don't fetch over dirty buffers */
948 if (tbp->flags & CM_BUF_DIRTY) {
949 cm_SyncOpDone(scp, tbp, flags);
950 lock_ReleaseMutex(&scp->mx);
951 lock_ReleaseMutex(&tbp->mx);
956 lock_ReleaseMutex(&scp->mx);
957 lock_ReleaseMutex(&tbp->mx);
959 /* add the buffer to the list */
961 osi_SetQData(qdp, tbp);
962 osi_QAdd((osi_queue_t **)&biop->bufListp, &qdp->q);
963 if (!biop->bufListEndp) biop->bufListEndp = qdp;
966 /* from now on, a failure just stops our collection process, but
967 * we still do the I/O to whatever we've already managed to collect.
970 collected += buf_bufferSize;
973 /* now, we've held in biop->bufListp all the buffer's we're really
974 * interested in. We also have holds left from heldBufListp, and we
975 * now release those holds on the buffers.
977 for(qdp = heldBufListp; qdp; qdp = tqdp) {
978 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
979 tbp = osi_GetQData(qdp);
984 /* Caller expects this */
985 lock_ObtainMutex(&scp->mx);
987 /* if we got a failure setting up the first buffer, then we don't have
988 * any side effects yet, and we also have failed an operation that the
989 * caller requires to make any progress. Give up now.
991 if (code && isFirst) {
992 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
996 /* otherwise, we're still OK, and should just return the I/O setup we've
999 biop->length = collected;
1000 biop->reserved = reserving;
1004 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1007 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore)
1011 osi_queueData_t *qdp;
1012 osi_queueData_t *nqdp;
1015 /* Give back reserved buffers */
1017 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1019 flags = CM_SCACHESYNC_NEEDCALLBACK;
1021 flags |= CM_SCACHESYNC_STOREDATA;
1023 flags |= CM_SCACHESYNC_FETCHDATA;
1026 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1027 /* lookup next guy first, since we're going to free this one */
1028 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1030 /* extract buffer and free queue data */
1031 bufp = osi_GetQData(qdp);
1034 /* now, mark I/O as done, unlock the buffer and release it */
1035 lock_ObtainMutex(&bufp->mx);
1036 lock_ObtainMutex(&scp->mx);
1037 cm_SyncOpDone(scp, bufp, flags);
1038 lock_ReleaseMutex(&scp->mx);
1040 /* turn off writing and wakeup users */
1042 if (bufp->flags & CM_BUF_WAITING) {
1043 osi_Wakeup((long) bufp);
1045 bufp->flags &= ~(CM_BUF_WAITING | CM_BUF_WRITING
1049 lock_ReleaseMutex(&bufp->mx);
1053 /* clean things out */
1054 biop->bufListp = NULL;
1055 biop->bufListEndp = NULL;
1058 /* Fetch a buffer. Called with scp locked.
1059 * The scp is locked on return.
1061 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *up,
1065 long nbytes; /* bytes in transfer */
1066 long rbytes; /* bytes in rx_Read call */
1068 AFSFetchStatus afsStatus;
1069 AFSCallBack callback;
1072 cm_buf_t *tbufp; /* buf we're filling */
1073 osi_queueData_t *qdp; /* q element we're scanning */
1075 struct rx_call *callp;
1076 cm_bulkIO_t biod; /* bulk IO descriptor */
1079 /* now, the buffer may or may not be filled with good data (buf_GetNew
1080 * drops lots of locks, and may indeed return a properly initialized
1081 * buffer, although more likely it will just return a new, empty, buffer.
1083 cm_AFSFidFromFid(&tfid, &scp->fid);
1085 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, up, reqp);
1087 /* couldn't even get the first page setup properly */
1088 osi_Log1(afsd_logp, "SetupFetchBIOD failure code %d", code);
1092 /* once we get here, we have the callback in place, we know that no one
1093 * is fetching the data now. Check one last time that we still have
1094 * the wrong data, and then fetch it if we're still wrong.
1096 * We can lose a race condition and end up with biod.length zero, in
1097 * which case we just retry.
1099 if (bufp->dataVersion == scp->dataVersion || biod.length == 0) {
1100 osi_Log3(afsd_logp, "Bad DVs %d, %d or length 0x%x",
1101 bufp->dataVersion, scp->dataVersion, biod.length);
1102 if ((bufp->dataVersion == -1
1103 || bufp->dataVersion < scp->dataVersion)
1104 && LargeIntegerGreaterThanOrEqualTo(bufp->offset,
1105 scp->serverLength)) {
1106 if (bufp->dataVersion == -1)
1107 memset(bufp->datap, 0, buf_bufferSize);
1108 bufp->dataVersion = scp->dataVersion;
1110 lock_ReleaseMutex(&scp->mx);
1111 cm_ReleaseBIOD(&biod, 0);
1112 lock_ObtainMutex(&scp->mx);
1116 lock_ReleaseMutex(&scp->mx);
1119 DPRINTF("cm_GetBuffer: fetching data scpDV=%d bufDV=%d scp=%x bp=%x dcp=%x\n",
1120 scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
1121 #endif /* DISKCACHE95 */
1123 /* now make the call */
1125 code = cm_Conn(&scp->fid, up, reqp, &connp);
1128 callp = rx_NewCall(connp->callp);
1130 osi_Log3(afsd_logp, "CALL FetchData vp %x, off 0x%x, size 0x%x",
1131 (long) scp, biod.offset.LowPart, biod.length);
1133 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1136 /* now copy the data out of the pipe and put it in the buffer */
1137 temp = rx_Read(callp, &nbytes, 4);
1139 nbytes = ntohl(nbytes);
1140 if (nbytes > biod.length) code = -1;
1145 qdp = biod.bufListEndp;
1147 tbufp = osi_GetQData(qdp);
1148 bufferp = tbufp->datap;
1150 else bufferp = NULL;
1151 /* fill nbytes of data from the pipe into the pages.
1152 * When we stop, qdp will point at the last page we're
1153 * dealing with, and bufferp will tell us where we
1154 * stopped. We'll need this info below when we clear
1155 * the remainder of the last page out (and potentially
1156 * clear later pages out, if we fetch past EOF).
1159 /* assert that there are still more buffers;
1160 * our check above for nbytes being less than
1161 * biod.length should ensure this.
1163 osi_assert(bufferp != NULL);
1165 /* read rbytes of data */
1166 rbytes = (nbytes > buf_bufferSize? buf_bufferSize : nbytes);
1167 temp = rx_Read(callp, bufferp, rbytes);
1168 if (temp < rbytes) {
1173 /* allow read-while-fetching.
1174 * if this is the last buffer, clear the
1175 * PREFETCHING flag, so the reader waiting for
1176 * this buffer will start a prefetch.
1178 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1179 lock_ObtainMutex(&scp->mx);
1180 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1181 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1182 osi_Wakeup((long) &scp->flags);
1184 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1186 cm_ClearPrefetchFlag(0, scp,
1189 lock_ReleaseMutex(&scp->mx);
1191 /* and adjust counters */
1194 /* and move to the next buffer */
1196 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1198 tbufp = osi_GetQData(qdp);
1199 bufferp = tbufp->datap;
1201 else bufferp = NULL;
1202 } else bufferp += temp;
1205 /* zero out remainder of last pages, in case we are
1206 * fetching past EOF. We were fetching an integral #
1207 * of pages, but stopped, potentially in the middle of
1208 * a page. Zero the remainder of that page, and then
1209 * all of the rest of the pages.
1212 rbytes = bufferp - tbufp->datap;
1213 /* bytes left to zero */
1214 rbytes = buf_bufferSize - rbytes;
1217 memset(bufferp, 0, rbytes);
1218 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1219 if (qdp == NULL) break;
1220 tbufp = osi_GetQData(qdp);
1221 bufferp = tbufp->datap;
1222 /* bytes to clear in this page */
1223 rbytes = buf_bufferSize;
1228 code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
1229 code = rx_EndCall(callp, code);
1230 osi_Log0(afsd_logp, "CALL FetchData DONE");
1232 } while (cm_Analyze(connp, up, reqp, &scp->fid, &volSync, NULL, code));
1233 code = cm_MapRPCError(code, reqp);
1235 lock_ObtainMutex(&scp->mx);
1236 /* we know that no one else has changed the buffer, since we still have
1237 * the fetching flag on the buffers, and we have the scp locked again.
1238 * Copy in the version # into the buffer if we got code 0 back from the
1242 for(qdp = biod.bufListp;
1244 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1245 tbufp = osi_GetQData(qdp);
1246 tbufp->dataVersion = afsStatus.DataVersion;
1249 /* write buffer out to disk cache */
1250 diskcache_Update(tbufp->dcp, tbufp->datap, buf_bufferSize,
1251 tbufp->dataVersion);
1252 #endif /* DISKCACHE95 */
1256 /* release scatter/gather I/O structure (buffers, locks) */
1257 lock_ReleaseMutex(&scp->mx);
1258 cm_ReleaseBIOD(&biod, 0);
1259 lock_ObtainMutex(&scp->mx);
1261 if (code == 0) cm_MergeStatus(scp, &afsStatus, &volSync, up, 0);