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>
26 extern void afsi_log(char *pattern, ...);
29 osi_mutex_t cm_bufGetMutex;
30 #ifdef AFS_FREELANCE_CLIENT
31 extern osi_mutex_t cm_Freelance_Lock;
34 /* functions called back from the buffer package when reading or writing data,
35 * or when holding or releasing a vnode pointer.
37 long cm_BufWrite(void *vfidp, osi_hyper_t *offsetp, long length, long flags,
38 cm_user_t *userp, cm_req_t *reqp)
40 /* store the data back from this buffer; the buffer is locked and held,
41 * but the vnode involved isn't locked, yet. It is held by its
42 * reference from the buffer, which won't change until the buffer is
43 * released by our caller. Thus, we don't have to worry about holding
47 cm_fid_t *fidp = vfidp;
51 AFSFetchStatus outStatus;
52 AFSStoreStatus inStatus;
56 struct rx_call *callp;
63 cm_bulkIO_t biod; /* bulk IO descriptor */
65 osi_assert(userp != NULL);
67 /* now, the buffer may or may not be filled with good data (buf_GetNew
68 * drops lots of locks, and may indeed return a properly initialized
69 * buffer, although more likely it will just return a new, empty, buffer.
71 scp = cm_FindSCache(fidp);
73 return CM_ERROR_NOSUCHFILE; /* shouldn't happen */
75 cm_AFSFidFromFid(&tfid, fidp);
77 lock_ObtainMutex(&scp->mx);
79 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
81 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
82 lock_ReleaseMutex(&scp->mx);
83 cm_ReleaseSCache(scp);
87 if (biod.length == 0) {
88 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
89 lock_ReleaseMutex(&scp->mx);
90 cm_ReleaseBIOD(&biod, 1); /* should be a NOOP */
91 cm_ReleaseSCache(scp);
95 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
96 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
98 /* prepare the output status for the store */
99 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
100 cm_StatusFromAttr(&inStatus, scp, NULL);
101 truncPos = scp->length.LowPart;
102 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
103 && scp->truncPos.LowPart < (unsigned long) truncPos)
104 truncPos = scp->truncPos.LowPart;
105 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
107 /* compute how many bytes to write from this buffer */
108 thyper = LargeIntegerSubtract(scp->length, biod.offset);
109 if (LargeIntegerLessThanZero(thyper)) {
110 /* entire buffer is past EOF */
114 /* otherwise write out part of buffer before EOF, but not
115 * more than bufferSize bytes.
117 nbytes = thyper.LowPart;
118 if (nbytes > biod.length)
119 nbytes = biod.length;
122 lock_ReleaseMutex(&scp->mx);
124 /* now we're ready to do the store operation */
126 code = cm_Conn(&scp->fid, userp, reqp, &connp);
130 callp = rx_NewCall(connp->callp);
132 osi_Log3(afsd_logp, "CALL StoreData vp %x, off 0x%x, size 0x%x",
133 (long) scp, biod.offset.LowPart, nbytes);
135 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
136 biod.offset.LowPart, nbytes, truncPos);
139 /* write the data from the the list of buffers */
143 qdp = biod.bufListEndp;
145 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
146 osi_assert(qdp != NULL);
147 bufp = osi_GetQData(qdp);
148 bufferp = bufp->datap;
150 if (wbytes > buf_bufferSize)
151 wbytes = buf_bufferSize;
153 /* write out wbytes of data from bufferp */
154 temp = rx_Write(callp, bufferp, wbytes);
155 if (temp != wbytes) {
156 osi_Log2(afsd_logp, "rx_Write failed %d != %d",temp,wbytes);
160 osi_Log1(afsd_logp, "rx_Write succeeded %d",temp);
163 } /* while more bytes to write */
164 } /* if RPC started successfully */
166 osi_Log1(afsd_logp, "StartRXAFS_StoreData failed (%lX)",code);
169 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
171 osi_Log1(afsd_logp, "EndRXAFS_StoreData failed (%lX)",code);
173 code = rx_EndCall(callp, code);
174 osi_Log0(afsd_logp, "CALL StoreData DONE");
176 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
177 code = cm_MapRPCError(code, reqp);
179 /* now, clean up our state */
180 lock_ObtainMutex(&scp->mx);
182 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
185 /* now, here's something a little tricky: in AFS 3, a dirty
186 * length can't be directly stored, instead, a dirty chunk is
187 * stored that sets the file's size (by writing and by using
188 * the truncate-first option in the store call).
190 * At this point, we've just finished a store, and so the trunc
191 * pos field is clean. If the file's size at the server is at
192 * least as big as we think it should be, then we turn off the
193 * length dirty bit, since all the other dirty buffers must
194 * precede this one in the file.
196 * The file's desired size shouldn't be smaller than what's
197 * stored at the server now, since we just did the trunc pos
200 * We have to turn off the length dirty bit as soon as we can,
201 * so that we see updates made by other machines.
203 if (outStatus.Length >= scp->length.LowPart)
204 scp->mask &= ~CM_SCACHEMASK_LENGTH;
205 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
207 if (code == CM_ERROR_SPACE)
208 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
209 else if (code == CM_ERROR_QUOTA)
210 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
212 lock_ReleaseMutex(&scp->mx);
213 cm_ReleaseBIOD(&biod, 1);
214 cm_ReleaseSCache(scp);
220 * Truncate the file, by sending a StoreData RPC with zero length.
222 * Called with scp locked. Releases and re-obtains the lock.
224 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
226 AFSFetchStatus outStatus;
227 AFSStoreStatus inStatus;
233 struct rx_call *callp;
235 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
236 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
237 CM_SCACHESYNC_STOREDATA_EXCL);
239 /* prepare the output status for the store */
240 inStatus.Mask = AFS_SETMODTIME;
241 inStatus.ClientModTime = scp->clientModTime;
242 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
244 /* calculate truncation position */
245 truncPos = scp->length.LowPart;
246 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
247 && scp->truncPos.LowPart < (unsigned long) truncPos)
248 truncPos = scp->truncPos.LowPart;
249 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
251 lock_ReleaseMutex(&scp->mx);
253 cm_AFSFidFromFid(&tfid, &scp->fid);
255 /* now we're ready to do the store operation */
257 code = cm_Conn(&scp->fid, userp, reqp, &connp);
261 callp = rx_NewCall(connp->callp);
263 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
267 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
268 code = rx_EndCall(callp, code);
269 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
270 code = cm_MapRPCError(code, reqp);
272 /* now, clean up our state */
273 lock_ObtainMutex(&scp->mx);
275 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
279 * For explanation of handling of CM_SCACHEMASK_LENGTH,
282 if (outStatus.Length >= scp->length.LowPart)
283 scp->mask &= ~CM_SCACHEMASK_LENGTH;
284 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
290 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
292 *bytesReadp = buf_bufferSize;
294 /* now return a code that means that I/O is done */
298 /* stabilize scache entry, and return with it locked so
301 long cm_BufStabilize(void *parmp, cm_user_t *userp, cm_req_t *reqp)
308 lock_ObtainMutex(&scp->mx);
309 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
310 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
312 lock_ReleaseMutex(&scp->mx);
319 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
320 long cm_BufUnstabilize(void *parmp, cm_user_t *userp)
326 lock_ReleaseMutex(&scp->mx);
328 /* always succeeds */
332 cm_buf_ops_t cm_bufOps = {
339 int cm_InitDCache(long chunkSize, long nbuffers)
341 lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
343 buf_nbuffers = nbuffers;
344 return buf_Init(&cm_bufOps);
347 /* check to see if we have an up-to-date buffer. The buffer must have
348 * previously been obtained by calling buf_Get.
350 * Make sure we have a callback, and that the dataversion matches.
352 * Scp must be locked.
354 * Bufp *may* be locked.
356 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
359 if (!cm_HaveCallback(scp))
362 & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
363 == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
365 if (bufp->dataVersion == scp->dataVersion)
368 code = lock_TryMutex(&bufp->mx);
370 /* don't have the lock, and can't lock it, then
377 /* remember dirty flag for later */
378 code = bufp->flags & CM_BUF_DIRTY;
380 /* release lock if we obtained it here */
382 lock_ReleaseMutex(&bufp->mx);
384 /* if buffer was dirty, buffer is acceptable for use */
391 /* used when deciding whether to do a prefetch or not */
392 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, long length,
393 cm_user_t *up, cm_req_t *reqp, osi_hyper_t *realBasep)
401 /* now scan all buffers in the range, looking for any that look like
406 lock_ObtainMutex(&scp->mx);
408 /* get callback so we can do a meaningful dataVersion comparison */
409 code = cm_SyncOp(scp, NULL, up, reqp, 0,
410 CM_SCACHESYNC_NEEDCALLBACK
411 | CM_SCACHESYNC_GETSTATUS);
413 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
414 lock_ReleaseMutex(&scp->mx);
418 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
419 /* we're past the end of file */
423 bp = buf_Find(scp, &tbase);
424 /* We cheat slightly by not locking the bp mutex. */
427 & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
428 && bp->dataVersion != scp->dataVersion)
435 /* if this buffer is essentially guaranteed to require a fetch,
436 * break out here and return this position.
441 toffset.LowPart = buf_bufferSize;
442 toffset.HighPart = 0;
443 tbase = LargeIntegerAdd(toffset, tbase);
444 length -= buf_bufferSize;
447 /* if we get here, either everything is fine or stop stopped us at a
448 * particular buffer in the range that definitely needs to be fetched.
451 /* return non-zero code since realBasep won't be valid */
452 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
456 /* successfully found a page that will need fetching */
460 lock_ReleaseMutex(&scp->mx);
464 void cm_BkgStore(cm_scache_t *scp, long p1, long p2, long p3, long p4,
472 req.flags |= CM_REQ_NORETRY;
474 toffset.LowPart = p1;
475 toffset.HighPart = p2;
478 osi_Log2(afsd_logp, "Starting BKG store vp 0x%x, base 0x%x", scp, p1);
480 cm_BufWrite(&scp->fid, &toffset, length, /* flags */ 0, userp, &req);
482 lock_ObtainMutex(&scp->mx);
483 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
484 lock_ReleaseMutex(&scp->mx);
487 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base)
492 thyper.LowPart = cm_chunkSize;
494 thyper = LargeIntegerAdd(*base, thyper);
495 thyper.LowPart &= (-cm_chunkSize);
496 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
497 scp->prefetch.base = *base;
498 if (LargeIntegerGreaterThan(thyper, scp->prefetch.end))
499 scp->prefetch.end = thyper;
501 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
504 /* do the prefetch */
505 void cm_BkgPrefetch(cm_scache_t *scp, long p1, long p2, long p3, long p4,
512 int cpff = 0; /* cleared prefetch flag */
516 req.flags |= CM_REQ_NORETRY;
522 osi_Log2(afsd_logp, "Starting BKG prefetch vp 0x%x, base 0x%x", scp, p1);
524 code = buf_Get(scp, &base, &bp);
526 lock_ObtainMutex(&scp->mx);
528 if (code || (bp->cmFlags & CM_BUF_CMFETCHING)) {
529 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
530 lock_ReleaseMutex(&scp->mx);
534 code = cm_GetBuffer(scp, bp, &cpff, userp, &req);
536 cm_ClearPrefetchFlag(code, scp, &base);
537 lock_ReleaseMutex(&scp->mx);
542 /* a read was issued to offsetp, and we have to determine whether we should
545 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
546 cm_user_t *userp, cm_req_t *reqp)
549 osi_hyper_t realBase;
550 osi_hyper_t readBase;
553 /* round up to chunk boundary */
554 readBase.LowPart += (cm_chunkSize-1);
555 readBase.LowPart &= (-cm_chunkSize);
557 lock_ObtainMutex(&scp->mx);
558 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
559 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
560 lock_ReleaseMutex(&scp->mx);
563 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
565 /* start the scan at the latter of the end of this read or
566 * the end of the last fetched region.
568 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
569 readBase = scp->prefetch.end;
571 lock_ReleaseMutex(&scp->mx);
573 code = cm_CheckFetchRange(scp, &readBase, cm_chunkSize, userp, reqp,
576 return; /* can't find something to prefetch */
578 osi_Log2(afsd_logp, "BKG Prefetch request vp 0x%x, base 0x%x",
579 scp, realBase.LowPart);
581 cm_QueueBKGRequest(scp, cm_BkgPrefetch, realBase.LowPart,
582 realBase.HighPart, cm_chunkSize, 0, userp);
585 /* scp must be locked; temporarily unlocked during processing.
586 * If returns 0, returns buffers held in biop, and with
587 * CM_BUF_CMSTORING set.
589 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
590 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
591 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
592 * must be woken, and the event must be set when the I/O is done. All of this
593 * is required so that buf_WaitIO synchronizes properly with the buffer as it
594 * is being written out.
596 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
597 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
600 osi_queueData_t *qdp;
603 osi_hyper_t scanStart; /* where to start scan for dirty pages */
604 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
605 osi_hyper_t firstModOffset; /* offset of first modified page in range */
608 long flags; /* flags to cm_SyncOp */
610 /* clear things out */
611 biop->scp = scp; /* don't hold */
612 biop->offset = *inOffsetp;
614 biop->bufListp = NULL;
615 biop->bufListEndp = NULL;
618 /* reserve a chunk's worth of buffers */
619 lock_ReleaseMutex(&scp->mx);
620 buf_ReserveBuffers(cm_chunkSize / buf_bufferSize);
621 lock_ObtainMutex(&scp->mx);
624 for(temp = 0; temp < inSize; temp += buf_bufferSize, bufp = NULL) {
626 thyper.LowPart = temp;
627 tbase = LargeIntegerAdd(*inOffsetp, thyper);
629 bufp = buf_Find(scp, &tbase);
631 /* get buffer mutex and scp mutex safely */
632 lock_ReleaseMutex(&scp->mx);
633 lock_ObtainMutex(&bufp->mx);
634 lock_ObtainMutex(&scp->mx);
636 flags = CM_SCACHESYNC_NEEDCALLBACK
637 | CM_SCACHESYNC_GETSTATUS
638 | CM_SCACHESYNC_STOREDATA
639 | CM_SCACHESYNC_BUFLOCKED;
640 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
642 lock_ReleaseMutex(&bufp->mx);
644 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
648 /* if the buffer is dirty, we're done */
649 if (bufp->flags & CM_BUF_DIRTY) {
650 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
651 "WRITING w/o CMSTORING in SetupStoreBIOD");
652 bufp->flags |= CM_BUF_WRITING;
656 /* this buffer is clean, so there's no reason to process it */
657 cm_SyncOpDone(scp, bufp, flags);
658 lock_ReleaseMutex(&bufp->mx);
665 /* if we get here, if bufp is null, we didn't find any dirty buffers
666 * that weren't already being stored back, so we just quit now.
672 /* don't need buffer mutex any more */
673 lock_ReleaseMutex(&bufp->mx);
675 /* put this element in the list */
677 osi_SetQData(qdp, bufp);
678 /* don't have to hold bufp, since held by buf_Find above */
679 osi_QAddH((osi_queue_t **) &biop->bufListp,
680 (osi_queue_t **) &biop->bufListEndp,
682 biop->length = buf_bufferSize;
683 firstModOffset = bufp->offset;
684 biop->offset = firstModOffset;
686 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
687 scanStart = *inOffsetp;
688 scanStart.LowPart &= (-cm_chunkSize);
689 thyper.LowPart = cm_chunkSize;
691 scanEnd = LargeIntegerAdd(scanStart, thyper);
693 flags = CM_SCACHESYNC_NEEDCALLBACK
694 | CM_SCACHESYNC_GETSTATUS
695 | CM_SCACHESYNC_STOREDATA
696 | CM_SCACHESYNC_BUFLOCKED
697 | CM_SCACHESYNC_NOWAIT;
699 /* start by looking backwards until scanStart */
700 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
701 thyper.LowPart = buf_bufferSize;
702 tbase = LargeIntegerSubtract(firstModOffset, thyper);
703 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
704 /* see if we can find the buffer */
705 bufp = buf_Find(scp, &tbase);
709 /* try to lock it, and quit if we can't (simplifies locking) */
710 code = lock_TryMutex(&bufp->mx);
716 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
718 lock_ReleaseMutex(&bufp->mx);
723 if (!(bufp->flags & CM_BUF_DIRTY)) {
724 /* buffer is clean, so we shouldn't add it */
725 cm_SyncOpDone(scp, bufp, flags);
726 lock_ReleaseMutex(&bufp->mx);
731 /* don't need buffer mutex any more */
732 lock_ReleaseMutex(&bufp->mx);
734 /* we have a dirty buffer ready for storing. Add it to the tail
735 * of the list, since it immediately precedes all of the disk
736 * addresses we've already collected.
739 osi_SetQData(qdp, bufp);
740 /* no buf_hold necessary, since we have it held from buf_Find */
741 osi_QAddT((osi_queue_t **) &biop->bufListp,
742 (osi_queue_t **) &biop->bufListEndp,
745 /* update biod info describing the transfer */
746 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
747 biop->length += buf_bufferSize;
749 /* update loop pointer */
750 tbase = LargeIntegerSubtract(tbase, thyper);
751 } /* while loop looking for pages preceding the one we found */
753 /* now, find later dirty, contiguous pages, and add them to the list */
754 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
755 thyper.LowPart = buf_bufferSize;
756 tbase = LargeIntegerAdd(firstModOffset, thyper);
757 while(LargeIntegerLessThan(tbase, scanEnd)) {
758 /* see if we can find the buffer */
759 bufp = buf_Find(scp, &tbase);
763 /* try to lock it, and quit if we can't (simplifies locking) */
764 code = lock_TryMutex(&bufp->mx);
770 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
772 lock_ReleaseMutex(&bufp->mx);
777 if (!(bufp->flags & CM_BUF_DIRTY)) {
778 /* buffer is clean, so we shouldn't add it */
779 cm_SyncOpDone(scp, bufp, flags);
780 lock_ReleaseMutex(&bufp->mx);
785 /* don't need buffer mutex any more */
786 lock_ReleaseMutex(&bufp->mx);
788 /* we have a dirty buffer ready for storing. Add it to the head
789 * of the list, since it immediately follows all of the disk
790 * addresses we've already collected.
793 osi_SetQData(qdp, bufp);
794 /* no buf_hold necessary, since we have it held from buf_Find */
795 osi_QAddH((osi_queue_t **) &biop->bufListp,
796 (osi_queue_t **) &biop->bufListEndp,
799 /* update biod info describing the transfer */
800 biop->length += buf_bufferSize;
802 /* update loop pointer */
803 tbase = LargeIntegerAdd(tbase, thyper);
804 } /* while loop looking for pages following the first page we found */
806 /* finally, we're done */
810 /* scp must be locked; temporarily unlocked during processing.
811 * If returns 0, returns buffers held in biop, and with
812 * CM_BUF_CMFETCHING flags set.
813 * If an error is returned, we don't return any buffers.
815 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
816 cm_bulkIO_t *biop, cm_user_t *up, cm_req_t *reqp)
820 osi_hyper_t toffset; /* a long long temp variable */
821 osi_hyper_t pageBase; /* base offset we're looking at */
822 osi_queueData_t *qdp; /* one temp queue structure */
823 osi_queueData_t *tqdp; /* another temp queue structure */
824 long collected; /* how many bytes have been collected */
827 osi_hyper_t fileSize; /* the # of bytes in the file */
828 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
829 osi_queueData_t *heldBufListEndp; /* first one */
833 biop->offset = *offsetp;
834 /* null out the list of buffers */
835 biop->bufListp = biop->bufListEndp = NULL;
838 /* first lookup the file's length, so we know when to stop */
839 code = cm_SyncOp(scp, NULL, up, reqp, 0, CM_SCACHESYNC_NEEDCALLBACK
840 | CM_SCACHESYNC_GETSTATUS);
844 /* copy out size, since it may change */
845 fileSize = scp->serverLength;
847 lock_ReleaseMutex(&scp->mx);
850 collected = pageBase.LowPart & (cm_chunkSize - 1);
852 heldBufListEndp = NULL;
855 * Obtaining buffers can cause dirty buffers to be recycled, which
856 * can cause a storeback, so cannot be done while we have buffers
859 * To get around this, we get buffers twice. Before reserving buffers,
860 * we obtain and release each one individually. After reserving
861 * buffers, we try to obtain them again, but only by lookup, not by
862 * recycling. If a buffer has gone away while we were waiting for
863 * the others, we just use whatever buffers we already have.
865 * On entry to this function, we are already holding a buffer, so we
866 * can't wait for reservation. So we call buf_TryReserveBuffers()
867 * instead. Not only that, we can't really even call buf_Get(), for
868 * the same reason. We can't avoid that, though. To avoid deadlock
869 * we allow only one thread to be executing the buf_Get()-buf_Release()
870 * sequence at a time.
873 /* first hold all buffers, since we can't hold any locks in buf_Get */
875 /* stop at chunk boundary */
876 if (collected >= cm_chunkSize) break;
878 /* see if the next page would be past EOF */
879 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) break;
881 lock_ObtainMutex(&cm_bufGetMutex);
883 code = buf_Get(scp, &pageBase, &tbp);
885 lock_ReleaseMutex(&cm_bufGetMutex);
886 lock_ObtainMutex(&scp->mx);
892 lock_ReleaseMutex(&cm_bufGetMutex);
894 toffset.HighPart = 0;
895 toffset.LowPart = buf_bufferSize;
896 pageBase = LargeIntegerAdd(toffset, pageBase);
897 collected += buf_bufferSize;
900 /* reserve a chunk's worth of buffers if possible */
901 reserving = buf_TryReserveBuffers(cm_chunkSize / buf_bufferSize);
904 collected = pageBase.LowPart & (cm_chunkSize - 1);
906 /* now hold all buffers, if they are still there */
908 /* stop at chunk boundary */
909 if (collected >= cm_chunkSize)
912 /* see if the next page would be past EOF */
913 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
916 tbp = buf_Find(scp, &pageBase);
920 /* add the buffer to the list */
922 osi_SetQData(qdp, tbp);
923 osi_QAdd((osi_queue_t **)&heldBufListp, &qdp->q);
924 if (!heldBufListEndp) heldBufListEndp = qdp;
925 /* leave tbp held (from buf_Get) */
930 collected += buf_bufferSize;
931 toffset.HighPart = 0;
932 toffset.LowPart = buf_bufferSize;
933 pageBase = LargeIntegerAdd(toffset, pageBase);
936 /* look at each buffer, adding it into the list if it looks idle and
937 * filled with old data. One special case: wait for idle if it is the
938 * first buffer since we really need that one for our caller to make
942 collected = 0; /* now count how many we'll really use */
943 for(tqdp = heldBufListEndp;
945 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
946 /* get a ptr to the held buffer */
947 tbp = osi_GetQData(tqdp);
948 pageBase = tbp->offset;
950 /* now lock the buffer lock */
951 lock_ObtainMutex(&tbp->mx);
952 lock_ObtainMutex(&scp->mx);
954 /* don't bother fetching over data that is already current */
955 if (tbp->dataVersion == scp->dataVersion) {
956 /* we don't need this buffer, since it is current */
957 lock_ReleaseMutex(&scp->mx);
958 lock_ReleaseMutex(&tbp->mx);
962 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_FETCHDATA
963 | CM_SCACHESYNC_BUFLOCKED;
965 flags |= CM_SCACHESYNC_NOWAIT;
967 /* wait for the buffer to serialize, if required. Doesn't
968 * release the scp or buffer lock(s) if NOWAIT is specified.
970 code = cm_SyncOp(scp, tbp, up, reqp, 0, flags);
972 lock_ReleaseMutex(&scp->mx);
973 lock_ReleaseMutex(&tbp->mx);
977 /* don't fetch over dirty buffers */
978 if (tbp->flags & CM_BUF_DIRTY) {
979 cm_SyncOpDone(scp, tbp, flags);
980 lock_ReleaseMutex(&scp->mx);
981 lock_ReleaseMutex(&tbp->mx);
986 lock_ReleaseMutex(&scp->mx);
987 lock_ReleaseMutex(&tbp->mx);
989 /* add the buffer to the list */
991 osi_SetQData(qdp, tbp);
992 osi_QAdd((osi_queue_t **)&biop->bufListp, &qdp->q);
993 if (!biop->bufListEndp)
994 biop->bufListEndp = qdp;
997 /* from now on, a failure just stops our collection process, but
998 * we still do the I/O to whatever we've already managed to collect.
1001 collected += buf_bufferSize;
1004 /* now, we've held in biop->bufListp all the buffer's we're really
1005 * interested in. We also have holds left from heldBufListp, and we
1006 * now release those holds on the buffers.
1008 for(qdp = heldBufListp; qdp; qdp = tqdp) {
1009 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1010 tbp = osi_GetQData(qdp);
1015 /* Caller expects this */
1016 lock_ObtainMutex(&scp->mx);
1018 /* if we got a failure setting up the first buffer, then we don't have
1019 * any side effects yet, and we also have failed an operation that the
1020 * caller requires to make any progress. Give up now.
1022 if (code && isFirst) {
1023 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1027 /* otherwise, we're still OK, and should just return the I/O setup we've
1030 biop->length = collected;
1031 biop->reserved = reserving;
1035 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1038 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore)
1042 osi_queueData_t *qdp;
1043 osi_queueData_t *nqdp;
1046 /* Give back reserved buffers */
1048 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1050 flags = CM_SCACHESYNC_NEEDCALLBACK;
1052 flags |= CM_SCACHESYNC_STOREDATA;
1054 flags |= CM_SCACHESYNC_FETCHDATA;
1057 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1058 /* lookup next guy first, since we're going to free this one */
1059 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1061 /* extract buffer and free queue data */
1062 bufp = osi_GetQData(qdp);
1065 /* now, mark I/O as done, unlock the buffer and release it */
1066 lock_ObtainMutex(&bufp->mx);
1067 lock_ObtainMutex(&scp->mx);
1068 cm_SyncOpDone(scp, bufp, flags);
1069 lock_ReleaseMutex(&scp->mx);
1071 /* turn off writing and wakeup users */
1073 if (bufp->flags & CM_BUF_WAITING) {
1074 osi_Wakeup((long) bufp);
1076 bufp->flags &= ~(CM_BUF_WAITING | CM_BUF_WRITING
1080 lock_ReleaseMutex(&bufp->mx);
1084 /* clean things out */
1085 biop->bufListp = NULL;
1086 biop->bufListEndp = NULL;
1089 /* Fetch a buffer. Called with scp locked.
1090 * The scp is locked on return.
1092 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *up,
1096 long nbytes; /* bytes in transfer */
1097 long rbytes; /* bytes in rx_Read call */
1099 AFSFetchStatus afsStatus;
1100 AFSCallBack callback;
1103 cm_buf_t *tbufp; /* buf we're filling */
1104 osi_queueData_t *qdp; /* q element we're scanning */
1106 struct rx_call *callp;
1107 cm_bulkIO_t biod; /* bulk IO descriptor */
1112 /* now, the buffer may or may not be filled with good data (buf_GetNew
1113 * drops lots of locks, and may indeed return a properly initialized
1114 * buffer, although more likely it will just return a new, empty, buffer.
1117 #ifdef AFS_FREELANCE_CLIENT
1119 // yj: if they're trying to get the /afs directory, we need to
1120 // handle it differently, since it's local rather than on any
1123 getroot = (scp==cm_rootSCachep);
1125 osi_Log1(afsd_logp,"GetBuffer returns cm_rootSCachep=%x",cm_rootSCachep);
1128 cm_AFSFidFromFid(&tfid, &scp->fid);
1130 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, up, reqp);
1132 /* couldn't even get the first page setup properly */
1133 osi_Log1(afsd_logp, "SetupFetchBIOD failure code %d", code);
1137 /* once we get here, we have the callback in place, we know that no one
1138 * is fetching the data now. Check one last time that we still have
1139 * the wrong data, and then fetch it if we're still wrong.
1141 * We can lose a race condition and end up with biod.length zero, in
1142 * which case we just retry.
1144 if (bufp->dataVersion == scp->dataVersion || biod.length == 0) {
1145 osi_Log3(afsd_logp, "Bad DVs %d, %d or length 0x%x",
1146 bufp->dataVersion, scp->dataVersion, biod.length);
1147 if ((bufp->dataVersion == -1
1148 || bufp->dataVersion < scp->dataVersion)
1149 && LargeIntegerGreaterThanOrEqualTo(bufp->offset,
1150 scp->serverLength)) {
1151 if (bufp->dataVersion == -1)
1152 memset(bufp->datap, 0, buf_bufferSize);
1153 bufp->dataVersion = scp->dataVersion;
1155 lock_ReleaseMutex(&scp->mx);
1156 cm_ReleaseBIOD(&biod, 0);
1157 lock_ObtainMutex(&scp->mx);
1161 lock_ReleaseMutex(&scp->mx);
1164 DPRINTF("cm_GetBuffer: fetching data scpDV=%d bufDV=%d scp=%x bp=%x dcp=%x\n",
1165 scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
1166 #endif /* DISKCACHE95 */
1168 #ifdef AFS_FREELANCE_CLIENT
1171 // if getroot then we don't need to make any calls
1172 // just return fake data
1174 if (cm_freelanceEnabled && getroot) {
1175 // setup the fake status
1176 afsStatus.InterfaceVersion = 0x1;
1177 afsStatus.FileType = 0x2;
1178 afsStatus.LinkCount = scp->linkCount;
1179 afsStatus.Length = cm_fakeDirSize;
1180 afsStatus.DataVersion = cm_fakeDirVersion;
1181 afsStatus.Author = 0x1;
1182 afsStatus.Owner = 0x0;
1183 afsStatus.CallerAccess = 0x9;
1184 afsStatus.AnonymousAccess = 0x9;
1185 afsStatus.UnixModeBits = 0x1ff;
1186 afsStatus.ParentVnode = 0x1;
1187 afsStatus.ParentUnique = 0x1;
1188 afsStatus.ResidencyMask = 0;
1189 afsStatus.ClientModTime = FakeFreelanceModTime;
1190 afsStatus.ServerModTime = FakeFreelanceModTime;
1191 afsStatus.Group = 0;
1192 afsStatus.SyncCounter = 0;
1193 afsStatus.dataVersionHigh = 0;
1195 // once we're done setting up the status info,
1196 // we just fill the buffer pages with fakedata
1197 // from cm_FakeRootDir. Extra pages are set to
1200 lock_ObtainMutex(&cm_Freelance_Lock);
1201 t1 = bufp->offset.LowPart;
1202 qdp = biod.bufListEndp;
1204 tbufp = osi_GetQData(qdp);
1205 bufferp=tbufp->datap;
1206 memset(bufferp, 0, buf_bufferSize);
1207 t2 = cm_fakeDirSize - t1;
1208 if (t2>buf_bufferSize) t2=buf_bufferSize;
1210 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1215 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1218 lock_ReleaseMutex(&cm_Freelance_Lock);
1220 // once we're done, we skip over the part of the
1221 // code that does the ACTUAL fetching of data for
1224 goto fetchingcompleted;
1227 #endif /* AFS_FREELANCE_CLIENT */
1229 /* now make the call */
1231 code = cm_Conn(&scp->fid, up, reqp, &connp);
1235 callp = rx_NewCall(connp->callp);
1237 osi_Log3(afsd_logp, "CALL FetchData vp %x, off 0x%x, size 0x%x",
1238 (long) scp, biod.offset.LowPart, biod.length);
1240 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1243 /* now copy the data out of the pipe and put it in the buffer */
1244 temp = rx_Read(callp, (char *)&nbytes, 4);
1246 nbytes = ntohl(nbytes);
1247 if (nbytes > biod.length)
1248 code = (callp->error < 0) ? callp->error : -1;
1251 code = (callp->error < 0) ? callp->error : -1;
1254 qdp = biod.bufListEndp;
1256 tbufp = osi_GetQData(qdp);
1257 bufferp = tbufp->datap;
1261 /* fill nbytes of data from the pipe into the pages.
1262 * When we stop, qdp will point at the last page we're
1263 * dealing with, and bufferp will tell us where we
1264 * stopped. We'll need this info below when we clear
1265 * the remainder of the last page out (and potentially
1266 * clear later pages out, if we fetch past EOF).
1269 /* assert that there are still more buffers;
1270 * our check above for nbytes being less than
1271 * biod.length should ensure this.
1273 osi_assert(bufferp != NULL);
1275 /* read rbytes of data */
1276 rbytes = (nbytes > buf_bufferSize? buf_bufferSize : nbytes);
1277 temp = rx_Read(callp, bufferp, rbytes);
1278 if (temp < rbytes) {
1279 code = (callp->error < 0) ? callp->error : -1;
1283 /* allow read-while-fetching.
1284 * if this is the last buffer, clear the
1285 * PREFETCHING flag, so the reader waiting for
1286 * this buffer will start a prefetch.
1288 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1289 lock_ObtainMutex(&scp->mx);
1290 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1291 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1292 osi_Wakeup((long) &scp->flags);
1294 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1296 cm_ClearPrefetchFlag(0, scp, &biod.offset);
1298 lock_ReleaseMutex(&scp->mx);
1300 /* and adjust counters */
1303 /* and move to the next buffer */
1305 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1307 tbufp = osi_GetQData(qdp);
1308 bufferp = tbufp->datap;
1316 /* zero out remainder of last pages, in case we are
1317 * fetching past EOF. We were fetching an integral #
1318 * of pages, but stopped, potentially in the middle of
1319 * a page. Zero the remainder of that page, and then
1320 * all of the rest of the pages.
1323 rbytes = bufferp - tbufp->datap;
1324 /* bytes left to zero */
1325 rbytes = buf_bufferSize - rbytes;
1328 memset(bufferp, 0, rbytes);
1329 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1332 tbufp = osi_GetQData(qdp);
1333 bufferp = tbufp->datap;
1334 /* bytes to clear in this page */
1335 rbytes = buf_bufferSize;
1340 code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
1342 osi_Log0(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error");
1343 code = rx_EndCall(callp, code);
1344 if (code == RXKADUNKNOWNKEY)
1345 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1346 osi_Log0(afsd_logp, "CALL FetchData DONE");
1348 } while (cm_Analyze(connp, up, reqp, &scp->fid, &volSync, NULL, NULL, code));
1351 code = cm_MapRPCError(code, reqp);
1353 lock_ObtainMutex(&scp->mx);
1354 /* we know that no one else has changed the buffer, since we still have
1355 * the fetching flag on the buffers, and we have the scp locked again.
1356 * Copy in the version # into the buffer if we got code 0 back from the
1360 for(qdp = biod.bufListp;
1362 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1363 tbufp = osi_GetQData(qdp);
1364 tbufp->dataVersion = afsStatus.DataVersion;
1367 /* write buffer out to disk cache */
1368 diskcache_Update(tbufp->dcp, tbufp->datap, buf_bufferSize,
1369 tbufp->dataVersion);
1370 #endif /* DISKCACHE95 */
1374 /* release scatter/gather I/O structure (buffers, locks) */
1375 lock_ReleaseMutex(&scp->mx);
1376 cm_ReleaseBIOD(&biod, 0);
1377 lock_ObtainMutex(&scp->mx);
1380 cm_MergeStatus(scp, &afsStatus, &volSync, up, 0);